From a19a78c9b220867c81dda024b14442b8116df162 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 14 Sep 2022 18:08:14 +0100 Subject: [PATCH] more --- cpdfcommand.ml | 25 +++++++++++++++---------- cpdftruetype.ml | 12 ++++++------ cpdftype.ml | 21 +++++++++++---------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 060c227..5b28de0 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -360,6 +360,7 @@ let output_pdfs : Pdf.t list ref = ref [] type font = | StandardFont of Pdftext.standard_font + | FontToEmbed of Pdfio.bytes * Pdftext.encoding | OtherFont of string type args = @@ -380,8 +381,6 @@ type args = mutable direction : int; mutable effect_duration : float; mutable font : font; - mutable fontfile : Pdfio.bytes option; - mutable fontencoding : Pdftext.encoding; mutable fontname : string; mutable fontsize : float; mutable fontttfmore : bool; @@ -504,8 +503,6 @@ let args = direction = 0; effect_duration = 1.; font = StandardFont Pdftext.TimesRoman; - fontfile = None; - fontencoding = Pdftext.WinAnsiEncoding; fontname = "Times-Roman"; fontsize = 12.; fontttfmore = false; @@ -628,8 +625,6 @@ let reset_arguments () = args.direction <- 0; args.effect_duration <- 1.; args.font <- StandardFont Pdftext.TimesRoman; - args.fontfile <- None; - args.fontencoding <- Pdftext.WinAnsiEncoding; args.fontname <- "Times-Roman"; args.fontsize <- 12.; args.fontttfmore <- false; @@ -1727,18 +1722,25 @@ let setnowarnrotate () = args.no_warn_rotate <- true let setfontttf s = - args.fontfile <- Some (Pdfio.bytes_of_string (contents_of_file s)) + args.font <- FontToEmbed (Pdfio.bytes_of_string (contents_of_file s), Pdftext.WinAnsiEncoding); + args.fontname <- Filename.basename s let setfontttfmore () = args.fontttfmore <- true let setfontttfencoding s = - args.fontencoding <- + let e = match s with | "MacRomanEncoding" -> Pdftext.MacRomanEncoding | "WinAnsiEncoding" -> Pdftext.WinAnsiEncoding | "StandardEncoding" -> Pdftext.StandardEncoding | _ -> error "Unknown encoding" + in + match args.font with + | FontToEmbed (b, _) -> + args.font <- FontToEmbed (b, e) + | _ -> + error "Must specift -font-ttf before -font-ttf-encoding" let whingemalformed () = prerr_string "Command line must be of exactly the form\ncpdf -gs -gs-malformed-force -o \n"; @@ -3722,6 +3724,7 @@ let go () = match args.font with | StandardFont f -> Some f | OtherFont f -> None (* it's in fontname *) + | FontToEmbed (_, _) -> error "-add-text can't use TTF fonts yet" in warn_prerotate range pdf; let pdf = @@ -3987,7 +3990,8 @@ let go () = let font = match args.font with | StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding) - | _ -> error "TOC requires standard font only" + | FontToEmbed (fontfile, encoding) -> Cpdfembed.font_of_truetype ~fontfile ~fontname:args.fontname ~encoding + | _ -> error "TOC: not a standard or embedded font" in let pdf = Cpdftoc.typeset_table_of_contents ~font ~fontsize:args.fontsize ~title:args.toc_title ~bookmark:args.toc_bookmark pdf in write_pdf false pdf @@ -3996,7 +4000,8 @@ let go () = let font = match args.font with | StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding) - | _ -> error "text to PDF: not a standard font" + | FontToEmbed (fontfile, encoding) -> Cpdfembed.font_of_truetype ~fontfile ~fontname:args.fontname ~encoding + | _ -> error "text to PDF: not a standard or embedded font" in let pdf = Cpdftexttopdf.typeset ~papersize:args.createpdf_pagesize ~font ~fontsize:args.fontsize text diff --git a/cpdftruetype.ml b/cpdftruetype.ml index c1c6799..899d858 100644 --- a/cpdftruetype.ml +++ b/cpdftruetype.ml @@ -21,7 +21,7 @@ type t = widths : int array; subset : Pdfio.bytes} -let dbg = ref true (* text-based debug *) +let dbg = ref false (* text-based debug *) (* 32-bit signed fixed-point number (16.16) returned as two ints *) let read_fixed b = @@ -189,20 +189,20 @@ let read_hmtx_table numOfLongHorMetrics b = let calculate_widths firstchar lastchar subset (cmapdata : (int, int) Hashtbl.t) (hmtxdata : int array) = if lastchar < firstchar then failwith "lastchar < firschar" else - 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)); Array.init (lastchar - firstchar + 1) (fun pos -> let code = pos + firstchar in - Printf.printf "code %i --> " code; + if !dbg then Printf.printf "code %i --> " code; if not (mem code subset) then 0 else try let glyphnum = Hashtbl.find cmapdata code in - Printf.printf "glyph number %i --> " glyphnum; + if !dbg then Printf.printf "glyph number %i --> " glyphnum; let width = hmtxdata.(glyphnum) in - Printf.printf "width %i\n" width; + if !dbg then Printf.printf "width %i\n" width; width - with e -> Printf.printf "no width for %i\n" code; 0) + with e -> if !dbg then Printf.printf "no width for %i\n" code; 0) let calculate_maxwidth hmtxdata = hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata)) diff --git a/cpdftype.ml b/cpdftype.ml index 1bccc66..0f756f4 100644 --- a/cpdftype.ml +++ b/cpdftype.ml @@ -55,16 +55,17 @@ let initial_state () = dest = None} let font_widths f fontsize = - let stdfont = - match f with Pdftext.StandardFont (sf, _) -> sf | _ -> failwith "not a standard font" - in - Array.init - 256 - (fun x -> - fontsize - *. float_of_int - (Pdfstandard14.textwidth false Pdftext.WinAnsiEncoding stdfont (string_of_char (char_of_int x))) - /. 1000.) + match f with + | Pdftext.StandardFont (sf, _) -> + Array.init + 256 + (fun x -> + fontsize + *. float_of_int + (Pdfstandard14.textwidth false Pdftext.WinAnsiEncoding sf (string_of_char (char_of_int x))) + /. 1000.) + | Pdftext.SimpleFont {fontmetrics = Some m} -> m + | _ -> raise (Pdf.PDFError "Cpdftype: Unsupported font") let width_of_string ws s = let w = ref 0. in