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,9 +3048,18 @@ 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 =
let fontpack_of_standardfont sf =
let te = Pdftext.text_extractor_of_font_real sf in
let table = null_hash () in
for x = 0 to 255 do
let u = hd (Pdftext.codepoints_of_text te (string_of_char (char_of_int x))) in
Hashtbl.add table u (0, x)
done;
([sf], table)
in
match args.font with match args.font with
| StandardFont f -> | StandardFont f ->
(* FIXME proper error handling *) (* FIXME proper error handling for missing file etc. *)
begin match args.embedstd14 with begin match args.embedstd14 with
| Some dirname -> | Some dirname ->
begin try begin try
@ -3059,18 +3068,17 @@ let embed_font pdf =
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
let font = hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding:args.fontencoding)) in Cpdfembed.EmbedInfo {fontfile; fontname; fontencoding = args.fontencoding}
Some font, Some (pdf, fontfile, fontname, 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
| None -> | None ->
Some (Pdftext.StandardFont (f, args.fontencoding)), None PreMadeFontPack (fontpack_of_standardfont (Pdftext.StandardFont (f, args.fontencoding)))
end end
| OtherFont f -> None, None (* it's in fontname *) | OtherFont f ->
ExistingNamedFont f
| FontToEmbed fontfile -> | FontToEmbed fontfile ->
Some (hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding))), EmbedInfo {fontfile; fontname = args.fontname; fontencoding = 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 []