First reworking of fontpacks

This commit is contained in:
John Whitington 2022-10-18 19:13:02 +01:00
parent 07574323f7
commit c05fbc0164
4 changed files with 48 additions and 27 deletions

View File

@ -3048,29 +3048,37 @@ let prerotate range pdf =
Cpdfpage.upright ~fast:args.fast range pdf Cpdfpage.upright ~fast:args.fast range pdf
let embed_font pdf = let embed_font pdf =
match args.font with let fontpack_of_standardfont sf =
| StandardFont f -> let te = Pdftext.text_extractor_of_font_real sf in
(* FIXME proper error handling *) let table = null_hash () in
begin match args.embedstd14 with for x = 0 to 255 do
| Some dirname -> let u = hd (Pdftext.codepoints_of_text te (string_of_char (char_of_int x))) in
begin try Hashtbl.add table u (0, x)
let fontfile, fontname = done;
let filename = hd (List.assoc f fontnames) in ([sf], table)
Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)), in
Filename.remove_extension filename match args.font with
in | StandardFont f ->
let font = hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding:args.fontencoding)) in (* FIXME proper error handling for missing file etc. *)
Some font, Some (pdf, fontfile, fontname, args.fontencoding) begin match args.embedstd14 with
with | Some dirname ->
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e)) begin try
let fontfile, fontname =
let filename = hd (List.assoc f fontnames) in
Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)),
Filename.remove_extension filename
in
Cpdfembed.EmbedInfo {fontfile; fontname; fontencoding = args.fontencoding}
with
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
end
| None ->
PreMadeFontPack (fontpack_of_standardfont (Pdftext.StandardFont (f, args.fontencoding)))
end end
| None -> | OtherFont f ->
Some (Pdftext.StandardFont (f, args.fontencoding)), None ExistingNamedFont f
end | FontToEmbed fontfile ->
| OtherFont f -> None, None (* it's in fontname *) EmbedInfo {fontfile; fontname = args.fontname; fontencoding = args.fontencoding}
| FontToEmbed fontfile ->
Some (hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding))),
Some (pdf, fontfile, args.fontname, args.fontencoding)
(* Main function *) (* Main function *)
let go () = let go () =
@ -3737,7 +3745,7 @@ let go () =
| Some (AddText text) -> | Some (AddText text) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in
let font, embedinfo = embed_font pdf in let cpdffont = embed_font pdf in
warn_prerotate range pdf; warn_prerotate range pdf;
let pdf = let pdf =
if args.prerotate then prerotate range pdf else pdf if args.prerotate then prerotate range pdf else pdf

View File

@ -3,6 +3,11 @@ open Pdfutil
type t = Pdftext.font list * (int, int * int) Hashtbl.t type t = Pdftext.font list * (int, int * int) Hashtbl.t
type cpdffont =
PreMadeFontPack of t
| EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; fontencoding : Pdftext.encoding}
| ExistingNamedFont of string
let pdfcode_of_unicode_codepoint encoding_table glyphlist_table u = let pdfcode_of_unicode_codepoint encoding_table glyphlist_table u =
try try
Some (Hashtbl.find encoding_table (Hashtbl.find glyphlist_table [u])) Some (Hashtbl.find encoding_table (Hashtbl.find glyphlist_table [u]))

View File

@ -4,6 +4,11 @@
type t = Pdftext.font list * (int, int * int) Hashtbl.t type t = Pdftext.font list * (int, int * int) Hashtbl.t
type cpdffont =
PreMadeFontPack of t
| EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; fontencoding : Pdftext.encoding}
| ExistingNamedFont of string
val embed_truetype : val embed_truetype :
Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list -> Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list ->
encoding:Pdftext.encoding -> t encoding:Pdftext.encoding -> t

View File

@ -2,6 +2,8 @@
open Pdfutil open Pdfutil
open Pdfio open Pdfio
let fontpack_experiment = false
type t = type t =
{flags : int; {flags : int;
minx : int; minx : int;
@ -499,7 +501,7 @@ let parse ?(subset=[]) data encoding =
| (_, _, o, l)::_ -> o, l | (_, _, o, l)::_ -> o, l
| [] -> raise (Pdf.PDFError "No loca table found in TrueType font") | [] -> raise (Pdf.PDFError "No loca table found in TrueType font")
in in
let subset_1 = if subset = [] then [] else tl subset in let subset_1 = if subset = [] then [] else if fontpack_experiment then tl subset else subset in
let subset_2 = if subset = [] then [] else [hd subset] in let subset_2 = if subset = [] then [] else [hd subset] in
let flags = calculate_flags italicangle in let flags = calculate_flags italicangle in
let firstchar_1, lastchar_1 = calculate_limits subset_1 in let firstchar_1, lastchar_1 = calculate_limits subset_1 in
@ -541,8 +543,9 @@ let parse ?(subset=[]) data encoding =
in in
[{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; [{flags; minx; miny; maxx; maxy; italicangle; ascent; descent;
capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_1; lastchar = lastchar_1; capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_1; lastchar = lastchar_1;
widths = widths_1; subset_fontfile = main_subset; subset = subset_1; tounicode = None}; widths = widths_1; subset_fontfile = main_subset; subset = subset_1; tounicode = None}]
{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; @ if fontpack_experiment then
[{flags; minx; miny; maxx; maxy; italicangle; ascent; descent;
capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_2; lastchar = lastchar_2; capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_2; lastchar = lastchar_2;
widths = widths_2; subset_fontfile = second_subset; subset = subset_2; widths = widths_2; subset_fontfile = second_subset; subset = subset_2;
tounicode = second_tounicode}] tounicode = second_tounicode}] else []