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
let embed_font pdf =
match args.font with
| StandardFont f ->
(* FIXME proper error handling *)
begin match args.embedstd14 with
| Some dirname ->
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
let font = hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding:args.fontencoding)) in
Some font, Some (pdf, fontfile, fontname, args.fontencoding)
with
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
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
| StandardFont f ->
(* FIXME proper error handling for missing file etc. *)
begin match args.embedstd14 with
| Some dirname ->
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
| None ->
Some (Pdftext.StandardFont (f, args.fontencoding)), None
end
| OtherFont f -> None, None (* it's in fontname *)
| FontToEmbed fontfile ->
Some (hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname:args.fontname ~codepoints:[] ~encoding:args.fontencoding))),
Some (pdf, fontfile, args.fontname, args.fontencoding)
| OtherFont f ->
ExistingNamedFont f
| FontToEmbed fontfile ->
EmbedInfo {fontfile; fontname = args.fontname; fontencoding = args.fontencoding}
(* Main function *)
let go () =
@ -3737,7 +3745,7 @@ let go () =
| Some (AddText text) ->
let pdf = get_single_pdf args.op false 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;
let 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 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 =
try
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 cpdffont =
PreMadeFontPack of t
| EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; fontencoding : Pdftext.encoding}
| ExistingNamedFont of string
val embed_truetype :
Pdf.t -> fontfile:Pdfio.bytes -> fontname:string -> codepoints:int list ->
encoding:Pdftext.encoding -> t

View File

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