This commit is contained in:
John Whitington 2022-09-14 18:08:14 +01:00
parent 550f809896
commit a19a78c9b2
3 changed files with 32 additions and 26 deletions

View File

@ -360,6 +360,7 @@ let output_pdfs : Pdf.t list ref = ref []
type font = type font =
| StandardFont of Pdftext.standard_font | StandardFont of Pdftext.standard_font
| FontToEmbed of Pdfio.bytes * Pdftext.encoding
| OtherFont of string | OtherFont of string
type args = type args =
@ -380,8 +381,6 @@ type args =
mutable direction : int; mutable direction : int;
mutable effect_duration : float; mutable effect_duration : float;
mutable font : font; mutable font : font;
mutable fontfile : Pdfio.bytes option;
mutable fontencoding : Pdftext.encoding;
mutable fontname : string; mutable fontname : string;
mutable fontsize : float; mutable fontsize : float;
mutable fontttfmore : bool; mutable fontttfmore : bool;
@ -504,8 +503,6 @@ let args =
direction = 0; direction = 0;
effect_duration = 1.; effect_duration = 1.;
font = StandardFont Pdftext.TimesRoman; font = StandardFont Pdftext.TimesRoman;
fontfile = None;
fontencoding = Pdftext.WinAnsiEncoding;
fontname = "Times-Roman"; fontname = "Times-Roman";
fontsize = 12.; fontsize = 12.;
fontttfmore = false; fontttfmore = false;
@ -628,8 +625,6 @@ let reset_arguments () =
args.direction <- 0; args.direction <- 0;
args.effect_duration <- 1.; args.effect_duration <- 1.;
args.font <- StandardFont Pdftext.TimesRoman; args.font <- StandardFont Pdftext.TimesRoman;
args.fontfile <- None;
args.fontencoding <- Pdftext.WinAnsiEncoding;
args.fontname <- "Times-Roman"; args.fontname <- "Times-Roman";
args.fontsize <- 12.; args.fontsize <- 12.;
args.fontttfmore <- false; args.fontttfmore <- false;
@ -1727,18 +1722,25 @@ let setnowarnrotate () =
args.no_warn_rotate <- true args.no_warn_rotate <- true
let setfontttf s = 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 () = let setfontttfmore () =
args.fontttfmore <- true args.fontttfmore <- true
let setfontttfencoding s = let setfontttfencoding s =
args.fontencoding <- let e =
match s with match s with
| "MacRomanEncoding" -> Pdftext.MacRomanEncoding | "MacRomanEncoding" -> Pdftext.MacRomanEncoding
| "WinAnsiEncoding" -> Pdftext.WinAnsiEncoding | "WinAnsiEncoding" -> Pdftext.WinAnsiEncoding
| "StandardEncoding" -> Pdftext.StandardEncoding | "StandardEncoding" -> Pdftext.StandardEncoding
| _ -> error "Unknown encoding" | _ -> 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 () = let whingemalformed () =
prerr_string "Command line must be of exactly the form\ncpdf <infile> -gs <path> -gs-malformed-force -o <outfile>\n"; prerr_string "Command line must be of exactly the form\ncpdf <infile> -gs <path> -gs-malformed-force -o <outfile>\n";
@ -3722,6 +3724,7 @@ let go () =
match args.font with match args.font with
| StandardFont f -> Some f | StandardFont f -> Some f
| OtherFont f -> None (* it's in fontname *) | OtherFont f -> None (* it's in fontname *)
| FontToEmbed (_, _) -> error "-add-text can't use TTF fonts yet"
in in
warn_prerotate range pdf; warn_prerotate range pdf;
let pdf = let pdf =
@ -3987,7 +3990,8 @@ let go () =
let font = let font =
match args.font with match args.font with
| StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding) | 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 in
let pdf = Cpdftoc.typeset_table_of_contents ~font ~fontsize:args.fontsize ~title:args.toc_title ~bookmark:args.toc_bookmark pdf 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 write_pdf false pdf
@ -3996,7 +4000,8 @@ let go () =
let font = let font =
match args.font with match args.font with
| StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding) | 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 in
let pdf = let pdf =
Cpdftexttopdf.typeset ~papersize:args.createpdf_pagesize ~font ~fontsize:args.fontsize text Cpdftexttopdf.typeset ~papersize:args.createpdf_pagesize ~font ~fontsize:args.fontsize text

View File

@ -21,7 +21,7 @@ type t =
widths : int array; widths : int array;
subset : Pdfio.bytes} 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 *) (* 32-bit signed fixed-point number (16.16) returned as two ints *)
let read_fixed b = 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) = let calculate_widths firstchar lastchar subset (cmapdata : (int, int) Hashtbl.t) (hmtxdata : int array) =
if lastchar < firstchar then failwith "lastchar < firschar" else 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 Array.init
(lastchar - firstchar + 1) (lastchar - firstchar + 1)
(fun pos -> (fun pos ->
let code = pos + firstchar in 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 if not (mem code subset) then 0 else
try try
let glyphnum = Hashtbl.find cmapdata code in 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 let width = hmtxdata.(glyphnum) in
Printf.printf "width %i\n" width; if !dbg then Printf.printf "width %i\n" width;
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 = let calculate_maxwidth hmtxdata =
hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata)) hd (sort (fun a b -> compare b a) (Array.to_list hmtxdata))

View File

@ -55,16 +55,17 @@ let initial_state () =
dest = None} dest = None}
let font_widths f fontsize = let font_widths f fontsize =
let stdfont = match f with
match f with Pdftext.StandardFont (sf, _) -> sf | _ -> failwith "not a standard font" | Pdftext.StandardFont (sf, _) ->
in Array.init
Array.init 256
256 (fun x ->
(fun x -> fontsize
fontsize *. float_of_int
*. float_of_int (Pdfstandard14.textwidth false Pdftext.WinAnsiEncoding sf (string_of_char (char_of_int x)))
(Pdfstandard14.textwidth false Pdftext.WinAnsiEncoding stdfont (string_of_char (char_of_int x))) /. 1000.)
/. 1000.) | Pdftext.SimpleFont {fontmetrics = Some m} -> m
| _ -> raise (Pdf.PDFError "Cpdftype: Unsupported font")
let width_of_string ws s = let width_of_string ws s =
let w = ref 0. in let w = ref 0. in