reworking cpdfdraw for new font system

This commit is contained in:
John Whitington 2023-07-14 14:40:59 +01:00
parent c9f2a713d5
commit 9753e9fb9b
2 changed files with 30 additions and 20 deletions

View File

@ -1114,23 +1114,6 @@ let setattachfile s =
setop (AttachFile [s]) () setop (AttachFile [s]) ()
| Some _ -> detect_duplicate_op (AttachFile [s]) | Some _ -> detect_duplicate_op (AttachFile [s])
let setfont f =
let convert f = (* convert from written PDF representation to internal PDF string e.g # sequences *)
match Pdfread.lex_name (Pdfio.input_of_string f) with Pdfgenlex.LexName s -> s | _ -> assert false
in
args.font <-
begin match Pdftext.standard_font_of_name ("/" ^ f) with
| Some x -> StandardFont x
| None ->
if f <> "" && hd (explode f) <> '/' then error "Custom font names must begin with /";
OtherFont (convert f)
end;
args.fontname <-
begin match Pdftext.standard_font_of_name ("/" ^ f) with
| Some x -> f
| None -> convert f
end
let setextracttextfontsize f = let setextracttextfontsize f =
args.extract_text_font_size <- Some f args.extract_text_font_size <- Some f
@ -1777,6 +1760,8 @@ let addop o =
| [] -> error "no drawops" | [] -> error "no drawops"
let endxobj () = let endxobj () =
match !drawops with match !drawops with
| (n, ops)::t -> | (n, ops)::t ->
@ -2091,6 +2076,25 @@ 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 setfont f =
let convert f = (* convert from written PDF representation to internal PDF string e.g # sequences *)
match Pdfread.lex_name (Pdfio.input_of_string f) with Pdfgenlex.LexName s -> s | _ -> assert false
in
args.font <-
begin match Pdftext.standard_font_of_name ("/" ^ f) with
| Some x -> StandardFont x
| None ->
if f <> "" && hd (explode f) <> '/' then error "Custom font names must begin with /";
OtherFont (convert f)
end;
args.fontname <-
begin match Pdftext.standard_font_of_name ("/" ^ f) with
| Some x -> f
| None -> convert f
end;
(* If drawing, add the font pack as an op. *)
begin match args.op with Some Draw -> addop (Cpdfdraw.FontPack (f, embed_font (), null_hash ())) | _ -> () end
let loadttf n = let loadttf n =
let name, filename = let name, filename =
match String.split_on_char '=' n with match String.split_on_char '=' n with
@ -2104,7 +2108,8 @@ let loadttf n =
ttfs ttfs
name name
(fontname, Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding}); (fontname, Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding});
addop (Cpdfdraw.FontPack (fontname, embed_font (), null_hash ())); (* If drawing, add the font pack as an op. *)
begin match args.op with Some Draw -> addop (Cpdfdraw.FontPack (fontname, embed_font (), null_hash ())) | _ -> () end
with with
_ -> error "addjpeg: could not load JPEG" _ -> error "addjpeg: could not load JPEG"
@ -4458,6 +4463,8 @@ let go () =
| Some Draw -> | Some Draw ->
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
(* Whatever the font is going in to -draw, add its fontpack. *)
addop (Cpdfdraw.FontPack (args.fontname, embed_font (), null_hash ()));
let ops = match !drawops with [("_MAIN", ops)] -> rev ops | _ -> error "not enough -end-xobj or -et" in let ops = match !drawops with [("_MAIN", ops)] -> rev ops | _ -> error "not enough -end-xobj or -et" in
write_pdf write_pdf
false false

View File

@ -65,8 +65,8 @@ 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" | Opacity _ -> "Opacity" | SOpacity _ -> "SOpacity" | FontPack (n, _, _) -> "FontPack " ^ n ^ " "
| Font _ -> "Font" | Text _ -> "Text" | SpecialText _ -> "SpecialText" | Font (f, _) -> "Font " ^ f ^ " " | Text _ -> "Text" | SpecialText _ -> "SpecialText"
| Newline -> "Newline" | Leading _ -> "Leading" | CharSpace _ -> "CharSpace" | Newline -> "Newline" | Leading _ -> "Leading" | CharSpace _ -> "CharSpace"
| WordSpace _ -> "WordSpace" | TextScale _ -> "TextScale" | WordSpace _ -> "WordSpace" | TextScale _ -> "TextScale"
| RenderMode _ -> "RenderMode" | Rise _ -> "Rise" | RenderMode _ -> "RenderMode" | Rise _ -> "Rise"
@ -251,6 +251,7 @@ let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = func
| 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 (identifier, cpdffont, codepoints) -> | FontPack (identifier, cpdffont, codepoints) ->
Printf.printf "Cpdfdraw: storing fontpack %s\n" identifier;
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
@ -277,6 +278,7 @@ let rec ops_of_drawop dryrun pdf endpage filename bates batespad num page = func
(res ()).page_names <- ns @ (res ()).page_names; (res ()).page_names <- ns @ (res ()).page_names;
[] []
| Font (identifier, size) -> | Font (identifier, size) ->
Printf.printf "Changing to stored font %s\n" identifier;
(res ()).current_fontpack <- Hashtbl.find fontpacks identifier; (res ()).current_fontpack <- Hashtbl.find fontpacks identifier;
(res ()).font_size <- size; (res ()).font_size <- size;
[] []
@ -402,6 +404,7 @@ let draw_single ~fast ~underneath ~filename ~bates ~batespad fast range pdf draw
Pdfpage.change_pages true pdf pages Pdfpage.change_pages true pdf pages
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 =
Printf.printf "%s\n" (string_of_drawops drawops);
resstack := [empty_res ()]; resstack := [empty_res ()];
Hashtbl.clear fontpacks; Hashtbl.clear fontpacks;
(res ()).time <- Cpdfstrftime.current_time (); (res ()).time <- Cpdfstrftime.current_time ();