This commit is contained in:
John Whitington 2022-10-19 14:36:26 +01:00
parent 733a94d882
commit be610916d4
3 changed files with 23 additions and 13 deletions

View File

@ -3068,7 +3068,7 @@ let embed_font () =
Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)), Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)),
Filename.remove_extension filename Filename.remove_extension filename
in in
Cpdfembed.EmbedInfo {fontfile; fontname; fontencoding = args.fontencoding} Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding}
with with
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e)) e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
end end
@ -3078,7 +3078,7 @@ let embed_font () =
| OtherFont f -> | OtherFont f ->
ExistingNamedFont f ExistingNamedFont f
| FontToEmbed fontfile -> | FontToEmbed fontfile ->
EmbedInfo {fontfile; fontname = args.fontname; fontencoding = args.fontencoding} EmbedInfo {fontfile; fontname = args.fontname; encoding = args.fontencoding}
(* Main function *) (* Main function *)
let go () = let go () =

View File

@ -1,7 +1,7 @@
(* Embed a font *) (* Embed a font *)
open Pdfutil open Pdfutil
type t = Pdftext.font list * (int, int * int) Hashtbl.t type t = Pdftext.font list * (int, int * int) Hashtbl.t (* Table returns font number and charcode for given unicode codepoint *)
type cpdffont = type cpdffont =
PreMadeFontPack of t PreMadeFontPack of t

View File

@ -1,14 +1,25 @@
open Pdfutil open Pdfutil
let rec of_utf8_with_newlines used charcode_extractor t = (* Return set of unicode characters in this text *)
let used_characters t =
let codepoints = Pdftext.codepoints_of_utf8 t in
setify codepoints
(* Just first font, expand later. Move into cpdfembed? *)
let get_char (fonts, table) u =
match Hashtbl.find table u with
| (n, charcode) -> Some charcode
| exception Not_found -> None
let rec of_utf8_with_newlines fontpack t =
let items = ref [] in let items = ref [] in
let buf = ref [] in let buf = ref [] in
let codepoints = Pdftext.codepoints_of_utf8 t in let codepoints = Pdftext.codepoints_of_utf8 t in
let charcodes_of_codepoints cs = let charcodes_of_codepoints cs =
option_map option_map
(fun u -> (fun u ->
match charcode_extractor u with match get_char fontpack u with
| Some c -> Hashtbl.replace used c (); Some (char_of_int c) | Some c -> Some (char_of_int c)
| None -> Printf.printf "No glyph for unicode U+%04X in this font\n" u; None) | None -> Printf.printf "No glyph for unicode U+%04X in this font\n" u; None)
cs cs
in in
@ -30,21 +41,20 @@ let rec of_utf8_with_newlines used charcode_extractor t =
let typeset ~papersize ~font ~fontsize text = let typeset ~papersize ~font ~fontsize text =
let pdf = Pdf.empty () in let pdf = Pdf.empty () in
let font = let codepoints = used_characters (Pdfio.string_of_bytes text) in
let font, fontpack =
match font with match font with
| Cpdfembed.PreMadeFontPack t -> hd (fst t) | Cpdfembed.PreMadeFontPack t -> (hd (fst t), t)
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} -> | Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding)) let embedded = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding in
(hd (fst embedded), embedded)
| Cpdfembed.ExistingNamedFont _ -> raise (Pdf.PDFError "Can't use existing named font for text-to-PDF") | Cpdfembed.ExistingNamedFont _ -> raise (Pdf.PDFError "Can't use existing named font for text-to-PDF")
in in
let charcode_extractor = Pdftext.charcode_extractor_of_font_real font in let instrs = of_utf8_with_newlines fontpack (Pdfio.string_of_bytes text) in
let margin = let margin =
Pdfunits.convert Pdfunits.convert
72. (Pdfpaper.unit papersize) (Pdfunits.PdfPoint) (Pdfpaper.width papersize) /. 15. 72. (Pdfpaper.unit papersize) (Pdfunits.PdfPoint) (Pdfpaper.width papersize) /. 15.
in in
let used = null_hash () in
let instrs = of_utf8_with_newlines used charcode_extractor (Pdfio.string_of_bytes text) in
let codepoints = map fst (list_of_hashtbl used) in
let pages = let pages =
Cpdftype.typeset Cpdftype.typeset
margin margin margin margin papersize pdf margin margin margin margin papersize pdf