Beginning -add-text/TTF

This commit is contained in:
John Whitington 2023-07-18 14:05:17 +01:00
parent 7408a79186
commit 6a92be56cf
3 changed files with 28 additions and 28 deletions

View File

@ -231,7 +231,7 @@ let expand_lines text time pdf endpage extract_text_font_size filename bates bat
let addtext let addtext
time lines linewidth outline fast colour fontname encoding bates batespad time lines linewidth outline fast colour fontname encoding bates batespad
fontsize fontpack font fontpdfobj underneath position hoffset voffset text pages fontsize fontpack font fontpdfobj fontpackpdfobjs underneath position hoffset voffset text pages
cropbox opacity justification filename extract_text_font_size shift raw pdf cropbox opacity justification filename extract_text_font_size shift raw pdf
= =
let endpage = Pdfpage.endpage pdf in let endpage = Pdfpage.endpage pdf in
@ -513,27 +513,25 @@ let
| Some (Pdftext.SimpleFont {encoding}) -> encoding | Some (Pdftext.SimpleFont {encoding}) -> encoding
| _ -> Pdftext.WinAnsiEncoding | _ -> Pdftext.WinAnsiEncoding
in in
iter let fontpackpdfobjs =
(fun line -> match cpdffont with
let voff, hoff = !voffset, 0. in
pdf :=
addtext time lines linewidth outline fast colour !realfontname encoding
bates batespad fontsize fontpack font fontpdfobj underneath position hoff voff line
pages cropbox opacity justification filename extract_text_font_size shift
raw !pdf;
voffset := !voffset +. (linespacing *. fontsize))
lines;
(* FIXME Here we need to embed all the fonts, not just one *)
begin match cpdffont with
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} -> | Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
let codepoints = map fst (list_of_hashtbl used) in let codepoints = map fst (list_of_hashtbl used) in
let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in let fonts = fst (Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding) in
let font = hd (fst (Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding)) in map (Pdftext.write_font !pdf) fonts
ignore (Pdftext.write_font ~objnum !pdf font) | _ -> []
| _ -> () in
end; iter
!pdf (fun line ->
let voff, hoff = !voffset, 0. in
pdf :=
addtext time lines linewidth outline fast colour !realfontname encoding
bates batespad fontsize fontpack font fontpdfobj fontpackpdfobjs underneath
position hoff voff line pages cropbox opacity justification filename
extract_text_font_size shift raw !pdf;
voffset := !voffset +. (linespacing *. fontsize))
lines;
!pdf
let addrectangle let addrectangle
fast (w, h) colour outline linewidth opacity position relative_to_cropbox fast (w, h) colour outline linewidth opacity position relative_to_cropbox

View File

@ -106,4 +106,3 @@ let rec collate_runs cfn a = function
let collate_runs = function let collate_runs = function
| [] -> [] | [] -> []
| (_, fontnum, _)::_ as l -> collate_runs fontnum [] l | (_, fontnum, _)::_ as l -> collate_runs fontnum [] l

View File

@ -6,6 +6,8 @@ let dbg =
(* Pdfe.logger := (fun s -> print_string s; flush stdout) *) (* Pdfe.logger := (fun s -> print_string s; flush stdout) *)
ref false ref false
let test_subsetting = true
type t = type t =
{flags : int; {flags : int;
minx : int; minx : int;
@ -496,13 +498,14 @@ let write_font filename data =
close_out fh close_out fh
let find_main encoding subset = let find_main encoding subset =
let encoding_table = Pdftext.table_of_encoding encoding in if test_subsetting then (take subset 3, [drop subset 3]) else
let first, rest = let encoding_table = Pdftext.table_of_encoding encoding in
List.partition let first, rest =
(fun u -> try ignore (Hashtbl.find encoding_table u); true with Not_found -> false) List.partition
subset (fun u -> try ignore (Hashtbl.find encoding_table u); true with Not_found -> false)
in subset
(first, splitinto 224 rest) in
(first, splitinto 224 rest)
let parse ~subset data encoding = let parse ~subset data encoding =
let mk_b byte_offset = bitbytes_of_input (let i = input_of_bytes data in i.seek_in byte_offset; i) in let mk_b byte_offset = bitbytes_of_input (let i = input_of_bytes data in i.seek_in byte_offset; i) in