Working in new Font/FontPack split in Cpdfdraw

This commit is contained in:
John Whitington 2023-07-14 13:57:00 +01:00
parent 84c7926be6
commit c9f2a713d5
3 changed files with 43 additions and 27 deletions

View File

@ -1745,21 +1745,6 @@ let setidironlypdfs () =
let setnowarnrotate () = let setnowarnrotate () =
args.no_warn_rotate <- true args.no_warn_rotate <- true
let loadttf n =
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
try
let fontfile = Pdfio.bytes_of_string (contents_of_file filename) in
let fontname = Filename.remove_extension (Filename.basename filename) in
Hashtbl.replace
ttfs
name
(fontname, Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding})
with
_ -> error "addjpeg: could not load JPEG"
let setfontttf s = let setfontttf s =
args.font <- EmbeddedFont s args.font <- EmbeddedFont s
@ -1791,6 +1776,7 @@ let addop o =
| (n, ops)::t -> drawops := (n, (o::ops))::t | (n, ops)::t -> drawops := (n, (o::ops))::t
| [] -> error "no drawops" | [] -> error "no drawops"
let endxobj () = let endxobj () =
match !drawops with match !drawops with
| (n, ops)::t -> | (n, ops)::t ->
@ -2105,14 +2091,31 @@ let embed_font () =
with with
Not_found -> error (Printf.sprintf "Font %s not found" name) Not_found -> error (Printf.sprintf "Font %s not found" name)
let loadttf n =
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
try
let fontfile = Pdfio.bytes_of_string (contents_of_file filename) in
let fontname = Filename.remove_extension (Filename.basename filename) in
Hashtbl.replace
ttfs
name
(fontname, Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding});
addop (Cpdfdraw.FontPack (fontname, embed_font (), null_hash ()));
with
_ -> error "addjpeg: could not load JPEG"
let addtext s = let addtext s =
begin match !drawops with _::_::_ -> () | _ -> error "-text must be in a -bt / -et section" end; begin match !drawops with _::_::_ -> () | _ -> error "-text must be in a -bt / -et section" end;
addop (Cpdfdraw.FontPack (embed_font (), args.fontsize, null_hash ())); addop (Cpdfdraw.Font (args.fontname, args.fontsize));
addop (Cpdfdraw.Text s) addop (Cpdfdraw.Text s)
let addspecialtext s = let addspecialtext s =
begin match !drawops with _::_::_ -> () | _ -> error "-stext must be in a -bt / -et section" end; begin match !drawops with _::_::_ -> () | _ -> error "-stext must be in a -bt / -et section" end;
addop (Cpdfdraw.FontPack (embed_font (), args.fontsize, null_hash ())); addop (Cpdfdraw.Font (args.fontname, args.fontsize));
addop (Cpdfdraw.SpecialText s) addop (Cpdfdraw.SpecialText s)
let setstderrtostdout () = let setstderrtostdout () =

View File

@ -38,7 +38,8 @@ type drawops =
| NewPage | NewPage
| Opacity of float | Opacity of float
| SOpacity of float | SOpacity of float
| FontPack of Cpdfembed.cpdffont * float * (int, unit) Hashtbl.t | FontPack of string * Cpdfembed.cpdffont * (int, unit) Hashtbl.t
| Font of string * float
| TextSection of drawops list | TextSection of drawops list
| Text of string | Text of string
| SpecialText of string | SpecialText of string
@ -64,9 +65,10 @@ let rec string_of_drawop = function
| FillStroke -> "FillStroke" | FillStrokeEvenOdd -> "FillStrokeEvenOdd" | FillStroke -> "FillStroke" | FillStrokeEvenOdd -> "FillStrokeEvenOdd"
| Clip -> "Clip" | ClipEvenOdd -> "ClipEvenOdd" | Use _ -> "Use" | Clip -> "Clip" | ClipEvenOdd -> "ClipEvenOdd" | Use _ -> "Use"
| ImageXObject _ -> "ImageXObject" | Image _ -> "Image" | NewPage -> "NewPage" | ImageXObject _ -> "ImageXObject" | Image _ -> "Image" | NewPage -> "NewPage"
| Opacity _ -> "Opacity" | SOpacity _ -> "SOpacity" | FontPack _ -> "FontPack" | Text _ -> "Text" | Opacity _ -> "Opacity" | SOpacity _ -> "SOpacity" | FontPack _ -> "FontPack"
| SpecialText _ -> "SpecialText" | Newline -> "Newline" | Leading _ -> "Leading" | Font _ -> "Font" | Text _ -> "Text" | SpecialText _ -> "SpecialText"
| CharSpace _ -> "CharSpace" | WordSpace _ -> "WordSpace" | TextScale _ -> "TextScale" | Newline -> "Newline" | Leading _ -> "Leading" | CharSpace _ -> "CharSpace"
| WordSpace _ -> "WordSpace" | TextScale _ -> "TextScale"
| RenderMode _ -> "RenderMode" | Rise _ -> "Rise" | RenderMode _ -> "RenderMode" | Rise _ -> "Rise"
and string_of_drawops l = and string_of_drawops l =
@ -89,6 +91,8 @@ let default_fontpack =
Cpdfembed.fontpack_of_standardfont Cpdfembed.fontpack_of_standardfont
(Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding)) (Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding))
let fontpacks = null_hash ()
let empty_res () = let empty_res () =
{images = null_hash (); {images = null_hash ();
extgstates = null_hash (); extgstates = null_hash ();
@ -190,7 +194,11 @@ let update_resources pdf old_resources =
(Pdf.Dictionary new_fonts) (Pdf.Dictionary new_fonts)
let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = function let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = function
| Qq ops -> [Pdfops.Op_q] @ ops_of_drawops dryrun pdf endpage filename bates batespad num page ops @ [Pdfops.Op_Q] | Qq ops ->
respush (); (* FIXME Is this right or not vis-a-vis fonts? *)
let r = [Pdfops.Op_q] @ ops_of_drawops dryrun pdf endpage filename bates batespad num page ops @ [Pdfops.Op_Q] in
respop ();
r
| Matrix m -> [Pdfops.Op_cm m] | Matrix m -> [Pdfops.Op_cm m]
| Rect (x, y, w, h) -> [Pdfops.Op_re (x, y, w, h)] | Rect (x, y, w, h) -> [Pdfops.Op_re (x, y, w, h)]
| Bezier (a, b, c, d, e, f) -> [Pdfops.Op_c (a, b, c, d, e, f)] | Bezier (a, b, c, d, e, f) -> [Pdfops.Op_c (a, b, c, d, e, f)]
@ -242,7 +250,7 @@ let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = func
| NewPage -> Pdfe.log ("NewPage remaining in graphic stream"); assert false | NewPage -> Pdfe.log ("NewPage remaining in graphic stream"); assert false
| Opacity v -> [Pdfops.Op_gs (extgstate "/ca" v)] | Opacity v -> [Pdfops.Op_gs (extgstate "/ca" v)]
| SOpacity v -> [Pdfops.Op_gs (extgstate "/CA" v)] | SOpacity v -> [Pdfops.Op_gs (extgstate "/CA" v)]
| FontPack (cpdffont, size, codepoints) -> | FontPack (identifier, cpdffont, codepoints) ->
if dryrun then (res ()).current_fontpack_codepoints <- codepoints; if dryrun then (res ()).current_fontpack_codepoints <- codepoints;
let fontpack = let fontpack =
match cpdffont with match cpdffont with
@ -254,6 +262,7 @@ let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = func
| ExistingNamedFont -> | ExistingNamedFont ->
error "-draw does not support using an existing named font" error "-draw does not support using an existing named font"
in in
Hashtbl.add fontpacks identifier fontpack;
let ns = let ns =
map map
(fun font -> (fun font ->
@ -265,8 +274,10 @@ let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = func
n) n)
(fst fontpack) (fst fontpack)
in in
(res ()).current_fontpack <- fontpack;
(res ()).page_names <- ns @ (res ()).page_names; (res ()).page_names <- ns @ (res ()).page_names;
[]
| Font (identifier, size) ->
(res ()).current_fontpack <- Hashtbl.find fontpacks identifier;
(res ()).font_size <- size; (res ()).font_size <- size;
[] []
| TextSection ops -> [Pdfops.Op_BT] @ ops_of_drawops dryrun pdf endpage filename bates batespad num page ops @ [Pdfops.Op_ET] | TextSection ops -> [Pdfops.Op_BT] @ ops_of_drawops dryrun pdf endpage filename bates batespad num page ops @ [Pdfops.Op_ET]
@ -392,6 +403,7 @@ let draw_single ~fast ~underneath ~filename ~bates ~batespad fast range pdf draw
let draw ?(fast=false) ?(underneath=false) ~filename ~bates ~batespad fast range pdf drawops = let draw ?(fast=false) ?(underneath=false) ~filename ~bates ~batespad fast range pdf drawops =
resstack := [empty_res ()]; resstack := [empty_res ()];
Hashtbl.clear fontpacks;
(res ()).time <- Cpdfstrftime.current_time (); (res ()).time <- Cpdfstrftime.current_time ();
let pdf = ref pdf in let pdf = ref pdf in
let range = ref range in let range = ref range in

View File

@ -35,7 +35,8 @@ type drawops =
| NewPage | NewPage
| Opacity of float | Opacity of float
| SOpacity of float | SOpacity of float
| FontPack of Cpdfembed.cpdffont * float * (int, unit) Hashtbl.t | FontPack of string * Cpdfembed.cpdffont * (int, unit) Hashtbl.t
| Font of string * float
| TextSection of drawops list | TextSection of drawops list
| Text of string | Text of string
| SpecialText of string | SpecialText of string