From e1d01c391481cd8d1ad5ae28d9635628531a1676 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 10 Oct 2022 16:40:57 +0100 Subject: [PATCH] more --- cpdfembed.ml | 23 +++++++++++++---------- cpdftruetype.ml | 44 +++++--------------------------------------- cpdftruetype.mli | 2 +- 3 files changed, 19 insertions(+), 50 deletions(-) diff --git a/cpdfembed.ml b/cpdfembed.ml index 45e8c07..28b8916 100644 --- a/cpdfembed.ml +++ b/cpdfembed.ml @@ -24,13 +24,7 @@ let basename () = incr fontnum; "AAAAA" ^ string_of_char (char_of_int (!fontnum + 65)) -let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = - let glyphlist_table = Pdfglyphlist.reverse_glyph_hashes () in - let encoding_table = Pdftext.reverse_table_of_encoding encoding in - let accepted_unicodepoints = - calc_accepted_unicodepoints encoding_table glyphlist_table codepoints - in - let f = hd (Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile encoding) in +let make_single_font ~fontname ~encoding pdf f = let name_1 = basename () in let module TT = Cpdftruetype in let fontfile = @@ -50,7 +44,7 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = done; a in - [(codepoints, + (f.TT.subset, SimpleFont {fonttype = Truetype; basefont = Printf.sprintf "/%s+%s" name_1 fontname; @@ -72,5 +66,14 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = float_of_int f.TT.maxx, float_of_int f.TT.maxy); fontfile = Some (FontFile2 fontfile_num); charset = None; - tounicode = None}; - encoding})] + tounicode = f.TT.tounicode}; + encoding}) + +let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding = + let glyphlist_table = Pdfglyphlist.reverse_glyph_hashes () in + let encoding_table = Pdftext.reverse_table_of_encoding encoding in + let accepted_unicodepoints = + calc_accepted_unicodepoints encoding_table glyphlist_table codepoints + in + let fs = Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile encoding in + map (make_single_font ~fontname ~encoding pdf) fs diff --git a/cpdftruetype.ml b/cpdftruetype.ml index b3cbde3..fa7398c 100644 --- a/cpdftruetype.ml +++ b/cpdftruetype.ml @@ -21,44 +21,10 @@ type t = widths : int array; subset_fontfile : Pdfio.bytes; subset : int list; - tounicode : Pdfio.bytes option} + tounicode : (int, string) Hashtbl.t option} let dbg = ref false -let tounicode_preamble = -"/CIDInit /ProcSet findresource begin\n\ -12 dict begin\n\ -begincmap\n\ -/CIDSystemInfo <<\n\ - /Registry (Adobe)\n\ - /Ordering (UCS)\n\ - /Supplement 0\n\ ->> def\n\ -/CMapName /Adobe-Identity-UCS def\n\ -/CMapType 2 def\n\ -1 begincodespacerange\n\ -<00>\n\ -endcodespacerange\n" - -let tounicode_postamble = -"endbfrange\n\ -endcmap\n\ -CMapName currentdict /CMap defineresource pop\n\ -end\n\ -end\n" - -let tounicode_map s us = - let b = Buffer.create 1024 in - let s = ref s in - Buffer.add_string b tounicode_preamble; - Buffer.add_string b (Printf.sprintf "%i beginbfrange\n" (length us)); - iter - (fun u -> Buffer.add_string b (Printf.sprintf "<%02x><%02x><%04x>" !s !s u); - s := !s + 1) - us; - Buffer.add_string b tounicode_postamble; - bytes_of_string (Buffer.contents b) - let required_tables = ["head"; "hhea"; "loca"; "cmap"; "maxp"; "cvt "; "glyf"; "prep"; "hmtx"; "fpgm"] @@ -563,13 +529,13 @@ let parse ?(subset=[]) data encoding = subset_font major minor !tables indexToLocFormat (if subset = [] then [] else [hd subset]) encoding !glyphcodes loca mk_b glyfoffset data in - let subset_tounicode = + (*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; + 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 = 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 = if subset = [] then [] else [hd subset]; tounicode = subset_tounicode}] + widths; subset_fontfile = second_subset; subset = if subset = [] then [] else [hd subset]; tounicode = None}] diff --git a/cpdftruetype.mli b/cpdftruetype.mli index f15b75c..2235aa4 100644 --- a/cpdftruetype.mli +++ b/cpdftruetype.mli @@ -20,7 +20,7 @@ type t = widths : int array; subset_fontfile : Pdfio.bytes; subset : int list; - tounicode : Pdfio.bytes option} + tounicode : (int, string) Hashtbl.t option} (* Parse the given TrueType font file. It will return one or more fonts. The first, a plain Latin font in the given encoding. Others are for the