diff --git a/cpdfaddtext.ml b/cpdfaddtext.ml index 9199121..a2e0e56 100644 --- a/cpdfaddtext.ml +++ b/cpdfaddtext.ml @@ -535,7 +535,7 @@ let map (fun c -> unicode_codepoint_of_pdfcode encoding_table glyphlist_table (int_of_char c)) charcodes 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) end; !pdf diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 72b7858..bc1bd93 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3059,7 +3059,7 @@ let embed_font pdf = Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)), Filename.remove_extension filename 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) with 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 | OtherFont f -> None, None (* it's in fontname *) | 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) (* Main function *) diff --git a/cpdfembed.ml b/cpdfembed.ml index 8a8ec8b..45e8c07 100644 --- a/cpdfembed.ml +++ b/cpdfembed.ml @@ -50,7 +50,8 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = done; a in - SimpleFont + [(codepoints, + SimpleFont {fonttype = Truetype; basefont = Printf.sprintf "/%s+%s" name_1 fontname; fontmetrics = Some fontmetrics; @@ -72,4 +73,4 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = fontfile = Some (FontFile2 fontfile_num); charset = None; tounicode = None}; - encoding} + encoding})] diff --git a/cpdfembed.mli b/cpdfembed.mli index fd017e3..87257d8 100644 --- a/cpdfembed.mli +++ b/cpdfembed.mli @@ -1,5 +1,7 @@ (* 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 : Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list -> - encoding:Pdftext.encoding -> Pdftext.font + encoding:Pdftext.encoding -> (int list * Pdftext.font) list diff --git a/cpdftexttopdf.ml b/cpdftexttopdf.ml index cc45a23..8d9dca5 100644 --- a/cpdftexttopdf.ml +++ b/cpdftexttopdf.ml @@ -44,7 +44,7 @@ let typeset ?embedinfo ~papersize ~font ~fontsize text = match embedinfo with | None -> font | Some (pdf, fontfile, fontname, encoding) -> - Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding + snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding)) in let pages = Cpdftype.typeset diff --git a/cpdftoc.ml b/cpdftoc.ml index 1af56a0..8471830 100644 --- a/cpdftoc.ml +++ b/cpdftoc.ml @@ -115,7 +115,7 @@ let typeset_table_of_contents ?embedinfo ~font ~fontsize ~title ~bookmark pdf = match embedinfo with | None -> font | Some (pdf, fontfile, fontname, encoding) -> - Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding + snd (hd (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding)) in Cpdftype.typeset lm rm tm bm firstpage_papersize pdf ([Cpdftype.Font (font, bfs); Cpdftype.BeginDocument] @ title @ diff --git a/cpdftruetype.ml b/cpdftruetype.ml index 8826ef6..b3cbde3 100644 --- a/cpdftruetype.ml +++ b/cpdftruetype.ml @@ -161,15 +161,15 @@ let print_encoding_table (table : (int, int) Hashtbl.t) = let read_encoding_table fmt length version b = match fmt with | 0 -> - Printf.printf "read_encoding_table: format 0\n"; + (*Printf.printf "read_encoding_table: format 0\n";*) let t = null_hash () in for x = 0 to 255 do Hashtbl.add t x (read_byte b) done; t | 4 -> - Printf.printf "read_encoding_table: format 4\n"; + (*Printf.printf "read_encoding_table: format 4\n";*) read_format_4_encoding_table b | 6 -> - Printf.printf "read_encoding_table: format 6\n"; + (*Printf.printf "read_encoding_table: format 6\n";*) read_format_6_encoding_table b | 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 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 tablesout = ref [] in let cut = ref 0l in @@ -514,9 +514,9 @@ let parse ?(subset=[]) data encoding = let version = read_ushort b in 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 - print_encoding_table got_glyphcodes; + (*print_encoding_table 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; end; let maxpoffset, maxplength = @@ -555,11 +555,21 @@ let parse ?(subset=[]) data encoding = | (_, _, o, l)::_ -> o, l | [] -> raise (Pdf.PDFError "No glyf table found in TrueType font") in - let main_subset = remove_unneeded_tables major minor !tables indexToLocFormat (tl subset) encoding !glyphcodes loca mk_b glyfoffset data in - let second_subset = remove_unneeded_tables major minor !tables indexToLocFormat [hd subset] encoding !glyphcodes loca mk_b glyfoffset data in + let main_subset = + 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; 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; 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}]