-add-text with real font name done

This commit is contained in:
John Whitington 2021-11-03 13:40:54 +00:00
parent cd68d4852d
commit 7c4fbf589d
3 changed files with 37 additions and 18 deletions

View File

@ -1,7 +1,8 @@
2.5 (Upcoming 2022)
o Text added in existing fonts is now encoding-aware (plus new raw mode)
o New operation -print-font-encoding
o Can now search for a font by real name with -font
o Text added in existing fonts is now encoding-aware (plus new raw mode)
o New -print-dict-entry operation prints values for a given key
o Extend -remove-dict-entry to allow search
o New -replace-dict-entry function to search & replace e.g URLs

45
cpdf.ml
View File

@ -1077,26 +1077,22 @@ let print_metadata pdf =
Printf.printf "%c" (char_of_int (bget data x))
done
(* \section{Print font data} *)
(* List fonts *)
let list_font pdf page (name, dict) =
let subtype =
match Pdf.lookup_direct pdf "/Subtype" dict with
| Some (Pdf.Name n) -> n
| Some (Pdf.Name n) -> Pdfwrite.string_of_pdf (Pdf.Name n)
| _ -> ""
in let basefont =
match Pdf.lookup_direct pdf "/BaseFont" dict with
| Some (Pdf.Name n) -> n
| Some (Pdf.Name n) -> Pdfwrite.string_of_pdf (Pdf.Name n)
| _ -> ""
in let encoding =
match Pdf.lookup_direct pdf "/Encoding" dict with
| Some (Pdf.Name n) -> n
| Some (Pdf.Name n) -> Pdfwrite.string_of_pdf (Pdf.Name n)
| _ -> ""
in
(*i Printf.printf
"%i %s %s %s %s\n" i*)
page, name, subtype, basefont, encoding
(page, name, subtype, basefont, encoding)
let list_fonts pdf =
let pages = Pdfpage.pages_of_pagetree pdf in
@ -1116,7 +1112,6 @@ let print_fonts pdf =
flprint
(fold_left ( ^ ) "" (map string_of_font (list_fonts pdf)))
(* \section{Superimpose text, page numbers etc.} *)
(* Process UTF8 text to /WinAnsiEncoding string (for standard 14) or whatever
@ -1434,9 +1429,18 @@ let addtext
| Some fontdict ->
begin match Pdf.lookup_direct pdf fontname fontdict with
| Some font -> font
| _ -> failwith "addtext: font not found A"
| None ->
(* For each item in the fontdict, follow its value and find the basename. If it matches, return that font *)
let font = ref None in
iter
(fun (k, v) ->
match Pdf.lookup_direct pdf "/BaseFont" v with
| Some (Pdf.Name n) when n = fontname -> font := Some v
| _ -> ())
(match fontdict with Pdf.Dictionary d -> d | _ -> []);
match !font with Some f -> f | None -> failwith (Printf.sprintf "addtext: font %s not found" fontname)
end
| _ -> failwith "addtext: font not found B"
| _ -> failwith "addtext: font not found for width"
in
let rawwidth = width_of_text (Pdftext.read_font pdf font) text in
(rawwidth *. fontsize) /. 1000.
@ -1545,6 +1549,7 @@ let
Printf.printf "relative-to-cropbox = %b" cropbox;
flprint "\n";*)
ops_metrics := [];
let realfontname = ref fontname in
let fontpdfobj =
match font with
| Some f ->
@ -1557,9 +1562,19 @@ let
| Some fontdict ->
begin match Pdf.lookup_direct pdf fontname fontdict with
| Some font -> font
| _ -> failwith "addtext: font not found A"
| _ ->
(* For each item in the fontdict, follow its value and find the basename. If it matches, return that font *)
let font = ref None in
iter
(fun (k, v) ->
match Pdf.lookup_direct pdf "/BaseFont" v with
| Some (Pdf.Name n) when n = fontname ->
font := Some v; realfontname := k
| _ -> ())
(match fontdict with Pdf.Dictionary d -> d | _ -> []);
match !font with Some f -> f | None -> failwith (Printf.sprintf "addtext: font %s not found" fontname)
end
| _ -> failwith "addtext: font not found B"
| _ -> failwith "addtext: font dictionary not present"
in
let text = if raw then text else charcodes_of_utf8 pdf fontpdfobj text in
let lines = map unescape_string (split_at_newline text) in
@ -1608,7 +1623,7 @@ let
if orientation = Cpdfposition.Vertical then 0., -.(!voffset) else !voffset, 0.
in
pdf :=
addtext metrics lines linewidth outline fast colour fontname
addtext metrics lines linewidth outline fast colour !realfontname
embed bates batespad fontsize font underneath position hoff voff line
pages orientation cropbox opacity justification filename
extract_text_font_size shift

View File

@ -1032,12 +1032,15 @@ let setattachfile 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 -> OtherFont f
| None -> OtherFont (convert f)
end;
args.fontname <- f
args.fontname <- convert f
let setextracttextfontsize f =
args.extract_text_font_size <- Some f