This commit is contained in:
John Whitington 2022-10-10 16:08:07 +01:00
parent 42e4c6689d
commit 337139a75f
7 changed files with 32 additions and 19 deletions

View File

@ -535,7 +535,7 @@ let
map (fun c -> unicode_codepoint_of_pdfcode encoding_table glyphlist_table (int_of_char c)) charcodes map (fun c -> unicode_codepoint_of_pdfcode encoding_table glyphlist_table (int_of_char c)) charcodes
in in
let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in
let font = Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding in let font = snd (hd (Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding)) in
ignore (Pdftext.write_font ~objnum !pdf font) ignore (Pdftext.write_font ~objnum !pdf font)
end; end;
!pdf !pdf

View File

@ -3059,7 +3059,7 @@ let embed_font pdf =
Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)), Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)),
Filename.remove_extension filename Filename.remove_extension filename
in in
let font = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding:args.fontencoding in let font = snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding:args.fontencoding)) in
Some font, Some (pdf, fontfile, fontname, args.fontencoding) Some font, Some (pdf, fontfile, fontname, args.fontencoding)
with with
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e)) e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
@ -3069,7 +3069,7 @@ let embed_font pdf =
end end
| OtherFont f -> None, None (* it's in fontname *) | OtherFont f -> None, None (* it's in fontname *)
| FontToEmbed fontfile -> | FontToEmbed fontfile ->
Some (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding), Some (snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding))),
Some (pdf, fontfile, args.fontname, args.fontencoding) Some (pdf, fontfile, args.fontname, args.fontencoding)
(* Main function *) (* Main function *)

View File

@ -50,7 +50,8 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding =
done; done;
a a
in in
SimpleFont [(codepoints,
SimpleFont
{fonttype = Truetype; {fonttype = Truetype;
basefont = Printf.sprintf "/%s+%s" name_1 fontname; basefont = Printf.sprintf "/%s+%s" name_1 fontname;
fontmetrics = Some fontmetrics; fontmetrics = Some fontmetrics;
@ -72,4 +73,4 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding =
fontfile = Some (FontFile2 fontfile_num); fontfile = Some (FontFile2 fontfile_num);
charset = None; charset = None;
tounicode = None}; tounicode = None};
encoding} encoding})]

View File

@ -1,5 +1,7 @@
(* Embed a TrueType font for the given set of unicode codepoints in the given (* Embed a TrueType font for the given set of unicode codepoints in the given
encoding, adding the fontfile to the PDF and returning the font object. *) encoding, adding the fontfiles to the PDF and returning the font objects,
together with the codepoints which appear in each. *)
val embed_truetype : val embed_truetype :
Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list -> Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list ->
encoding:Pdftext.encoding -> Pdftext.font encoding:Pdftext.encoding -> (int list * Pdftext.font) list

View File

@ -44,7 +44,7 @@ let typeset ?embedinfo ~papersize ~font ~fontsize text =
match embedinfo with match embedinfo with
| None -> font | None -> font
| Some (pdf, fontfile, fontname, encoding) -> | Some (pdf, fontfile, fontname, encoding) ->
Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding))
in in
let pages = let pages =
Cpdftype.typeset Cpdftype.typeset

View File

@ -115,7 +115,7 @@ let typeset_table_of_contents ?embedinfo ~font ~fontsize ~title ~bookmark pdf =
match embedinfo with match embedinfo with
| None -> font | None -> font
| Some (pdf, fontfile, fontname, encoding) -> | Some (pdf, fontfile, fontname, encoding) ->
Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding))
in in
Cpdftype.typeset lm rm tm bm firstpage_papersize pdf Cpdftype.typeset lm rm tm bm firstpage_papersize pdf
([Cpdftype.Font (font, bfs); Cpdftype.BeginDocument] @ title @ ([Cpdftype.Font (font, bfs); Cpdftype.BeginDocument] @ title @

View File

@ -161,15 +161,15 @@ let print_encoding_table (table : (int, int) Hashtbl.t) =
let read_encoding_table fmt length version b = let read_encoding_table fmt length version b =
match fmt with match fmt with
| 0 -> | 0 ->
Printf.printf "read_encoding_table: format 0\n"; (*Printf.printf "read_encoding_table: format 0\n";*)
let t = null_hash () in let t = null_hash () in
for x = 0 to 255 do Hashtbl.add t x (read_byte b) done; for x = 0 to 255 do Hashtbl.add t x (read_byte b) done;
t t
| 4 -> | 4 ->
Printf.printf "read_encoding_table: format 4\n"; (*Printf.printf "read_encoding_table: format 4\n";*)
read_format_4_encoding_table b read_format_4_encoding_table b
| 6 -> | 6 ->
Printf.printf "read_encoding_table: format 6\n"; (*Printf.printf "read_encoding_table: format 6\n";*)
read_format_6_encoding_table b read_format_6_encoding_table b
| n -> raise (Pdf.PDFError "read_encoding_table: format %i not known\n%!") | n -> raise (Pdf.PDFError "read_encoding_table: format %i not known\n%!")
@ -324,7 +324,7 @@ let padword n =
let r = n + (if n mod 4 = 0 then 0 else 4 - n mod 4) in let r = n + (if n mod 4 = 0 then 0 else 4 - n mod 4) in
i32ofi r i32ofi r
let remove_unneeded_tables major minor tables indexToLocFormat subset encoding cmap loca mk_b glyfoffset data = let subset_font major minor tables indexToLocFormat subset encoding cmap loca mk_b glyfoffset data =
let tables = Array.of_list (sort (fun (_, _, o, _) (_, _, o', _) -> compare o o') tables) in let tables = Array.of_list (sort (fun (_, _, o, _) (_, _, o', _) -> compare o o') tables) in
let tablesout = ref [] in let tablesout = ref [] in
let cut = ref 0l in let cut = ref 0l in
@ -514,9 +514,9 @@ let parse ?(subset=[]) data encoding =
let version = read_ushort b in let version = read_ushort b in
if !dbg then Printf.printf "subtable has format %i, length %i, version %i\n" fmt lngth version; if !dbg then Printf.printf "subtable has format %i, length %i, version %i\n" fmt lngth version;
let got_glyphcodes = read_encoding_table fmt length version b in let got_glyphcodes = read_encoding_table fmt length version b in
print_encoding_table got_glyphcodes; (*print_encoding_table got_glyphcodes; *)
Hashtbl.iter (Hashtbl.add !glyphcodes) got_glyphcodes; Hashtbl.iter (Hashtbl.add !glyphcodes) got_glyphcodes;
Printf.printf "Retrieved %i cmap entries in total\n" (length (list_of_hashtbl !glyphcodes)) (*Printf.printf "Retrieved %i cmap entries in total\n" (length (list_of_hashtbl !glyphcodes))*)
done; done;
end; end;
let maxpoffset, maxplength = let maxpoffset, maxplength =
@ -555,11 +555,21 @@ let parse ?(subset=[]) data encoding =
| (_, _, o, l)::_ -> o, l | (_, _, o, l)::_ -> o, l
| [] -> raise (Pdf.PDFError "No glyf table found in TrueType font") | [] -> raise (Pdf.PDFError "No glyf table found in TrueType font")
in in
let main_subset = remove_unneeded_tables major minor !tables indexToLocFormat (tl subset) encoding !glyphcodes loca mk_b glyfoffset data in let main_subset =
let second_subset = remove_unneeded_tables major minor !tables indexToLocFormat [hd subset] encoding !glyphcodes loca mk_b glyfoffset data in subset_font major minor !tables indexToLocFormat (if subset = [] then [] else tl subset)
encoding !glyphcodes loca mk_b glyfoffset data
in
let second_subset =
subset_font major minor !tables indexToLocFormat (if subset = [] then [] else [hd subset])
encoding !glyphcodes loca mk_b glyfoffset data
in
let subset_tounicode =
if subset = [] then None else Some (tounicode_map 0 [hd subset])
in
begin match subset_tounicode with Some x -> Printf.printf "%S\n" (string_of_bytes x) | None -> () end;
[{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; [{flags; minx; miny; maxx; maxy; italicangle; ascent; descent;
capheight; stemv; xheight; avgwidth; maxwidth; firstchar; lastchar; capheight; stemv; xheight; avgwidth; maxwidth; firstchar; lastchar;
widths; subset_fontfile = main_subset; subset = tl subset; tounicode = None}; widths; subset_fontfile = main_subset; subset = if subset = [] then [] else tl subset; tounicode = None};
{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; {flags; minx; miny; maxx; maxy; italicangle; ascent; descent;
capheight; stemv; xheight; avgwidth; maxwidth; firstchar; lastchar; capheight; stemv; xheight; avgwidth; maxwidth; firstchar; lastchar;
widths; subset_fontfile = second_subset; subset = [hd subset]; tounicode = None}] widths; subset_fontfile = second_subset; subset = if subset = [] then [] else [hd subset]; tounicode = subset_tounicode}]