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 =
| 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 <infile> -gs <path> -gs-malformed-force -o <outfile>\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

View File

@ -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))

View File

@ -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