From ad442568dbd32a8521e4183808f9580723d724e7 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Sat, 24 Sep 2022 11:58:09 +0100 Subject: [PATCH] more --- Makefile | 9 ++++----- cpdfaddtext.ml | 21 +++++++++++++++++++-- cpdfcommand.ml | 11 ++++++----- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 63a0f44..7025bd9 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,10 @@ NONDOC = cpdfyojson cpdfxmlm cpdfutil DOC = cpdfunicodedata cpdferror cpdfdebug cpdfjson cpdfstrftime cpdfcoord \ cpdfattach cpdfpagespec cpdfposition cpdfpresent cpdfmetadata \ - cpdfbookmarks cpdfpage cpdfaddtext cpdfimage cpdffont cpdftype \ - cpdfpad cpdfocg cpdfsqueeze cpdfdraft cpdfspot \ + cpdfbookmarks cpdfpage cpdftruetype cpdfembed cpdfaddtext cpdfimage \ + cpdffont cpdftype cpdfpad cpdfocg cpdfsqueeze cpdfdraft cpdfspot \ cpdfpagelabels cpdfcreate cpdfannot cpdfxobject cpdfimpose cpdftweak \ - cpdftruetype cpdfembed cpdftexttopdf cpdftoc\ - cpdfcommand + cpdftexttopdf cpdftoc cpdfcommand MODS = $(NONDOC) $(DOC) @@ -31,7 +30,7 @@ $(foreach x,$(MODS),$x.cmti) ifneq ($(shell ocamlopt -version),) TARGETS += native-code native-code-library - LIBINSTALL_FILES += cpdf.a cpdf.cmxa $(foreach x,$(PDFMODS),$x.cmx) + LIBINSTALL_FILES += cpdf.a cpdf.cmxa $(foreach x,$(MODS),$x.cmx) endif all : $(TARGETS) diff --git a/cpdfaddtext.ml b/cpdfaddtext.ml index d7972ed..2981268 100644 --- a/cpdfaddtext.ml +++ b/cpdfaddtext.ml @@ -21,6 +21,12 @@ let charcodes_of_utf8 font s = in implode (map char_of_int charcodes) +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 + (* Get the width of some text in the given font *) let width_of_text font text = match font with @@ -586,8 +592,19 @@ let !pdf; voffset := !voffset +. (linespacing *. fontsize)) lines; - let charcodes = map fst (list_of_hashtbl used) in - Printf.printf "%i charcodes used\n" (length charcodes); + begin match embedinfo with + | None -> () + | Some (_, fontfile, fontname, encoding) -> + let charcodes = map fst (list_of_hashtbl used) in + let encoding_table = Pdftext.table_of_encoding encoding in + let glyphlist_table = Pdfglyphlist.glyph_hashes () in + let codepoints = + map (fun c -> unicode_codepoint_of_pdfcode encoding_table glyphlist_table (int_of_char c)) charcodes + in + let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in + let font = Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding in + ignore (Pdftext.write_font ~objnum !pdf font) + end; !pdf let removetext range pdf = diff --git a/cpdfcommand.ml b/cpdfcommand.ml index bb906aa..19f5921 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3705,12 +3705,13 @@ let go () = | Some (AddText text) -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - let font = + let font, embedinfo = match args.font with - | StandardFont f -> Some (Pdftext.StandardFont (f, args.fontencoding)) - | OtherFont f -> None (* it's in fontname *) + | StandardFont f -> Some (Pdftext.StandardFont (f, args.fontencoding)), None + | OtherFont f -> None, None (* it's in fontname *) | FontToEmbed fontfile -> - Some (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding) + Some (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding), + Some (pdf, fontfile, args.fontname, args.fontencoding) in warn_prerotate range pdf; let pdf = @@ -3722,7 +3723,7 @@ let go () = in write_pdf false (Cpdfaddtext.addtexts - args.linewidth args.outline args.fast args.fontname + ?embedinfo args.linewidth args.outline args.fast args.fontname font args.embedfonts args.bates args.batespad args.color args.position args.linespacing args.fontsize args.underneath text range () args.relative_to_cropbox args.opacity