This commit is contained in:
John Whitington
2022-09-19 16:21:14 +01:00
parent cab4a6c4a0
commit 4aa51e1554
3 changed files with 16 additions and 5 deletions

View File

@ -55,7 +55,7 @@ let embed_truetype pdf ~fontfile ~fontname ~text ~encoding =
(calc_accepted_unicodepoints (calc_accepted_unicodepoints
encoding_table glyphlist_table unicodepoints) encoding_table glyphlist_table unicodepoints)
in in
let f = Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile in let f = Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile ~encoding in
let name_1 = basename () in let name_1 = basename () in
let fontfile = let fontfile =
let len = Pdfio.bytes_size fontfile in let len = Pdfio.bytes_size fontfile in

View File

@ -186,14 +186,25 @@ let read_hmtx_table numOfLongHorMetrics b =
numOfLongHorMetrics numOfLongHorMetrics
(fun _ -> let r = read_ushort b in ignore (read_short b); r) (fun _ -> let r = read_ushort b in ignore (read_short b); r)
let calculate_widths firstchar lastchar subset (cmapdata : (int, int) Hashtbl.t) (hmtxdata : int array) = (* For widths, we need the unicode code, not the unencoded byte *)
let unicode_codepoint_of_pdfcode encoding_table glyphlist_table p =
try
hd (Hashtbl.find glyphlist_table (Hashtbl.find encoding_table p))
with
Not_found -> 0
let calculate_widths encoding firstchar lastchar subset (cmapdata : (int, int) Hashtbl.t) (hmtxdata : int array) =
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 glyphlist_table = Pdfglyphlist.glyph_hashes () in
Array.init Array.init
(lastchar - firstchar + 1) (lastchar - firstchar + 1)
(fun pos -> (fun pos ->
let code = pos + firstchar in let code = pos + firstchar in
if !dbg then Printf.printf "code %i --> " code; if !dbg then Printf.printf "code %i --> " code;
let code = unicode_codepoint_of_pdfcode encoding_table glyphlist_table code in
if !dbg then Printf.printf "unicode %i --> " code;
if subset <> [] && not (mem code subset) then 0 else if subset <> [] && not (mem code subset) then 0 else
try try
let glyphnum = Hashtbl.find cmapdata code in let glyphnum = Hashtbl.find cmapdata code in
@ -206,7 +217,7 @@ let calculate_widths firstchar lastchar subset (cmapdata : (int, int) Hashtbl.t)
let calculate_maxwidth hmtxdata = let calculate_maxwidth hmtxdata =
hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata)) hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata))
let parse ?(subset=[]) data = let parse ?(subset=[]) data ~encoding =
let subset = map fst subset in 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
@ -320,7 +331,7 @@ let parse ?(subset=[]) data =
| (_, _, o, _)::_ -> read_hmtx_table numOfLongHorMetrics (mk_b (i32toi o)) | (_, _, o, _)::_ -> read_hmtx_table numOfLongHorMetrics (mk_b (i32toi o))
| [] -> raise (Pdf.PDFError "No hmtx table found in TrueType font") | [] -> raise (Pdf.PDFError "No hmtx table found in TrueType font")
in in
let widths = calculate_widths firstchar lastchar subset !glyphcodes hmtxdata in let widths = calculate_widths encoding firstchar lastchar subset !glyphcodes hmtxdata in
let maxwidth = calculate_maxwidth hmtxdata in let maxwidth = calculate_maxwidth hmtxdata in
let stemv = calculate_stemv () in let stemv = calculate_stemv () in
let b = mk_b (i32toi locaoffset) in let b = mk_b (i32toi locaoffset) in

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 -> t val parse : ?subset:(int * int option) list -> Pdfio.bytes -> encoding:Pdftext.encoding -> t