Move collate into cpdfembed, patch up -draw

This commit is contained in:
John Whitington 2023-07-13 14:44:57 +01:00
parent acddfb1ac7
commit a9204a0365
4 changed files with 34 additions and 27 deletions

View File

@ -136,17 +136,21 @@ let process_specials pdf endpage filename bates batespad num page s =
in in
Cpdfaddtext.process_text (res ()).time s pairs Cpdfaddtext.process_text (res ()).time s pairs
(* FIXME: implement for other kinds of font *)
let runs_of_utf8 s = let runs_of_utf8 s =
match (res ()).current_fontpack with let fontpack = (res ()).current_fontpack in
| ((f::_, _) as fontpack) -> 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 charcodes = option_map (Cpdfembed.get_char fontpack) codepoints in let collated = Cpdfembed.collate_runs triples in
let fontname = flatten
fst (Hashtbl.find (res ()).fonts f) (map
in (fun l ->
[Pdfops.Op_Tf (fontname, (res ()).font_size); Pdfops.Op_Tj (implode (map (fun (c, _, _) -> char_of_int c) charcodes))] if l = [] then [] else
| _ -> failwith "charcodes_of_utf8: unknown font" let f = match l with (_, _, f)::_ -> f | _ -> assert false in
let fontname = fst (Hashtbl.find (res ()).fonts f) in
let charcodes = map (fun (c, _, _) -> char_of_int c) l in
[Pdfops.Op_Tf (fontname, (res ()).font_size);
Pdfops.Op_Tj (implode charcodes)])
collated)
let extgstate kind v = let extgstate kind v =
try Hashtbl.find (res ()).extgstates (kind, v) with try Hashtbl.find (res ()).extgstates (kind, v) with
@ -239,9 +243,9 @@ let rec ops_of_drawop pdf endpage filename bates batespad num page = function
match cpdffont with match cpdffont with
| PreMadeFontPack fp -> fp | PreMadeFontPack fp -> fp
| EmbedInfo {fontfile; fontname; encoding} -> | EmbedInfo {fontfile; fontname; encoding} ->
Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[int_of_char 'a'] ~encoding Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[int_of_char 'H'] ~encoding
| ExistingNamedFont -> | ExistingNamedFont ->
error "-draw does not support using an exsiting named font" error "-draw does not support using an existing named font"
in in
let ns = let ns =
map map

View File

@ -92,3 +92,18 @@ let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding =
let fs = Cpdftruetype.parse ~subset:codepoints fontfile encoding in let fs = Cpdftruetype.parse ~subset:codepoints fontfile encoding in
let subsets_and_their_fonts = map (make_single_font ~fontname ~encoding pdf) fs in let subsets_and_their_fonts = map (make_single_font ~fontname ~encoding pdf) fs in
(map snd subsets_and_their_fonts, make_fontpack_hashtable subsets_and_their_fonts) (map snd subsets_and_their_fonts, make_fontpack_hashtable subsets_and_their_fonts)
let rec collate_runs cfn a = function
| [] -> rev (map rev a)
| (charcode, fontnum, font) as h::t ->
match a with
| [] -> collate_runs fontnum [[h]] t
| this::rest ->
if fontnum = cfn
then collate_runs cfn ((h::this)::rest) t
else collate_runs fontnum ([h]::this::rest) t
let collate_runs = function
| [] -> []
| (_, fontnum, _)::_ as l -> collate_runs fontnum [] l

View File

@ -16,3 +16,5 @@ val get_char : t -> int -> (int * int * Pdftext.font) option
val embed_truetype : val embed_truetype :
Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list -> Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list ->
encoding:Pdftext.encoding -> t encoding:Pdftext.encoding -> t
val collate_runs : ('a * 'b * 'c) list -> ('a * 'b * 'c) list list

View File

@ -14,20 +14,6 @@ let rec real_newline = function
| x::r -> x::real_newline r | x::r -> x::real_newline r
| [] -> [] | [] -> []
let rec collate_runs cfn a = function
| [] -> rev (map rev a)
| (charcode, fontnum, font) as h::t ->
match a with
| [] -> collate_runs fontnum [[h]] t
| this::rest ->
if fontnum = cfn
then collate_runs cfn ((h::this)::rest) t
else collate_runs fontnum ([h]::this::rest) t
let collate_runs = function
| [] -> []
| (_, fontnum, _)::_ as l -> collate_runs fontnum [] l
let rec width_of_runs runs = let rec width_of_runs runs =
match runs with match runs with
| Cpdftype.Font (f, fontsize)::Cpdftype.Text t::more -> | Cpdftype.Font (f, fontsize)::Cpdftype.Text t::more ->
@ -39,7 +25,7 @@ let rec width_of_runs runs =
let of_utf8 fontpack fontsize t = let of_utf8 fontpack fontsize t =
let codepoints = Pdftext.codepoints_of_utf8 t in let codepoints = Pdftext.codepoints_of_utf8 t in
let fonted = option_map (Cpdfembed.get_char fontpack) codepoints in let fonted = option_map (Cpdfembed.get_char fontpack) codepoints in
let collated = collate_runs fonted in let collated = Cpdfembed.collate_runs fonted in
flatten flatten
(map (map
(function (function