Cache widths in runs_of_utf8

This commit is contained in:
John Whitington 2024-09-23 13:21:43 +01:00
parent c8006e8cdb
commit 86c7bed32a
1 changed files with 14 additions and 3 deletions

View File

@ -175,16 +175,27 @@ let font_widths f fontsize =
| _ -> raise (Pdf.PDFError "Cpdfdraw: Unsupported font") | _ -> raise (Pdf.PDFError "Cpdfdraw: Unsupported font")
let runs_of_utf8 s = let runs_of_utf8 s =
let widthcache = null_hash () in
let identifier, fontpack = (res ()).current_fontpack in let identifier, fontpack = (res ()).current_fontpack in
let codepoints = Pdftext.codepoints_of_utf8 s in let codepoints = Pdftext.codepoints_of_utf8 s in
let triples = option_map (Cpdfembed.get_char fontpack) codepoints in let triples = option_map (Cpdfembed.get_char fontpack) codepoints in
let collated = Cpdfembed.collate_runs triples in let collated = Cpdfembed.collate_runs triples in
(* FIXME Efficiency: runs, cacheing *) let font_widths fontnum font font_size =
(* Need to cache font widths here. TODO need to cache futher up too for
more speed. Check -typeset speed now we need widths. Or, make width
calculation optional? *)
match Hashtbl.find_opt widthcache (fontnum, font_size) with
| Some table -> table
| None ->
let widths = font_widths font font_size in
Hashtbl.add widthcache (fontnum, font_size) widths;
widths
in
let w = let w =
fold_left ( +. ) 0. fold_left ( +. ) 0.
(map (map
(fun (charcode, _, font) -> (fun (charcode, fontnum, font) ->
let widths = font_widths font (res ()).font_size in let widths = font_widths fontnum font (res ()).font_size in
widths.(charcode)) widths.(charcode))
triples) triples)
in in