This commit is contained in:
John Whitington 2022-10-04 16:09:20 +01:00
parent dcd3242ad2
commit 22f6f34686
3 changed files with 24 additions and 23 deletions

View File

@ -48,11 +48,7 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding =
let glyphlist_table = Pdfglyphlist.reverse_glyph_hashes () in let glyphlist_table = Pdfglyphlist.reverse_glyph_hashes () in
let encoding_table = Pdftext.reverse_table_of_encoding encoding in let encoding_table = Pdftext.reverse_table_of_encoding encoding in
let accepted_unicodepoints = let accepted_unicodepoints =
map calc_accepted_unicodepoints encoding_table glyphlist_table codepoints
(fun u ->
(u, pdfcode_of_unicode_codepoint encoding_table glyphlist_table u))
(calc_accepted_unicodepoints
encoding_table glyphlist_table codepoints)
in in
let f = Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile ~encoding in let f = Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile ~encoding in
let name_1 = basename () in let name_1 = basename () in

View File

@ -131,18 +131,24 @@ let read_loca_table indexToLocFormat numGlyphs b =
| 1 -> Array.init (numGlyphs + 1) (function _ -> read_ulong b) | 1 -> Array.init (numGlyphs + 1) (function _ -> read_ulong b)
| _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in read_loca_table") | _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in read_loca_table")
let write_loca_table indexToLocFormat bs arr = let write_loca_table subset encoding cmap indexToLocFormat bs arr =
Array.iter let missing_char_glyph_loca = arr.(0) in
(fun x -> let is_included u =
match indexToLocFormat with true
| 0 -> in
Printf.printf "%li\n" (i32div x 2l); Array.iter
putval bs 16 (i32div x 2l) (fun x ->
| 1 -> match indexToLocFormat with
Printf.printf "%li\n" x; | 0 ->
putval bs 32 x if is_included x
| _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in write_loca_table")) then putval bs 16 (i32div x 2l)
arr else putval bs 16 (i32div missing_char_glyph_loca 2l)
| 1 ->
if is_included x
then putval bs 32 x
else putval bs 32 missing_char_glyph_loca
| _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in write_loca_table"))
arr
let read_os2_table unitsPerEm b blength = let read_os2_table unitsPerEm b blength =
let version = read_ushort b in let version = read_ushort b in
@ -198,7 +204,7 @@ let unicode_codepoint_of_pdfcode encoding_table glyphlist_table p =
let calculate_widths unitsPerEm encoding firstchar lastchar subset cmapdata hmtxdata = let calculate_widths unitsPerEm encoding firstchar lastchar subset cmapdata hmtxdata =
if lastchar < firstchar then failwith "lastchar < firschar" else if lastchar < firstchar then failwith "lastchar < firschar" else
if !dbg then List.iter (fun (a, b) -> Printf.printf "%i -> %i\n" a b) (sort compare (list_of_hashtbl cmapdata)); (*if !dbg then List.iter (fun (a, b) -> Printf.printf "%i -> %i\n" a b) (sort compare (list_of_hashtbl cmapdata));*)
let encoding_table = Pdftext.table_of_encoding encoding in let encoding_table = Pdftext.table_of_encoding encoding in
let glyphlist_table = Pdfglyphlist.glyph_hashes () in let glyphlist_table = Pdfglyphlist.glyph_hashes () in
Array.init Array.init
@ -220,7 +226,7 @@ let calculate_widths unitsPerEm encoding firstchar lastchar subset cmapdata hmtx
let calculate_maxwidth unitsPerEm hmtxdata = let calculate_maxwidth unitsPerEm hmtxdata =
pdf_unit unitsPerEm (hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata))) pdf_unit unitsPerEm (hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata)))
let remove_unneeded_tables major minor tables indexToLocFormat loca data = let remove_unneeded_tables major minor tables indexToLocFormat subset encoding cmap loca 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
@ -288,7 +294,7 @@ let remove_unneeded_tables major minor tables indexToLocFormat loca data =
(fun (tag, _, _, _) -> (fun (tag, _, _, _) ->
if !dbg then Printf.printf "Writing %s table\n" (string_of_tag tag); if !dbg then Printf.printf "Writing %s table\n" (string_of_tag tag);
if string_of_tag tag = "loca" then if string_of_tag tag = "loca" then
write_loca_table indexToLocFormat bs loca write_loca_table subset encoding cmap indexToLocFormat bs loca
else else
match findtag tag with match findtag tag with
| (og_off, Some len) -> | (og_off, Some len) ->
@ -309,7 +315,6 @@ let remove_unneeded_tables major minor tables indexToLocFormat loca data =
bytes bytes
let parse ?(subset=[]) data ~encoding = let parse ?(subset=[]) data ~encoding =
let subset = map fst subset in
let mk_b byte_offset = bitbytes_of_input (let i = input_of_bytes data in i.seek_in byte_offset; i) in let mk_b byte_offset = bitbytes_of_input (let i = input_of_bytes data in i.seek_in byte_offset; i) in
let b = mk_b 0 in let b = mk_b 0 in
let major, minor = read_fixed b in let major, minor = read_fixed b in
@ -429,7 +434,7 @@ let parse ?(subset=[]) data ~encoding =
let stemv = calculate_stemv () in let stemv = calculate_stemv () in
let b = mk_b (i32toi locaoffset) in let b = mk_b (i32toi locaoffset) in
let loca = read_loca_table indexToLocFormat numGlyphs b in let loca = read_loca_table indexToLocFormat numGlyphs b in
let subset = remove_unneeded_tables major minor !tables indexToLocFormat loca data in let subset = remove_unneeded_tables major minor !tables indexToLocFormat subset encoding !glyphcodes loca data in
{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} widths; subset}

View File

@ -21,4 +21,4 @@ type t =
(* Parse the font, given the list of Unicode codepoints required for the subset (* Parse the font, given the list of Unicode codepoints required for the subset
and optionally their PDF codepoint too. Returns the information required for and optionally their PDF codepoint too. Returns the information required for
embedding this font in a PDF. *) embedding this font in a PDF. *)
val parse : ?subset:(int * int option) list -> Pdfio.bytes -> encoding:Pdftext.encoding -> t val parse : ?subset:int list -> Pdfio.bytes -> encoding:Pdftext.encoding -> t