mirror of
https://github.com/johnwhitington/cpdf-source.git
synced 2025-02-16 11:50:54 +01:00
-add-text now only a single caller to Cpdfembed
This commit is contained in:
parent
566b4ba304
commit
6cbfc84150
@ -2,13 +2,6 @@
|
|||||||
open Pdfutil
|
open Pdfutil
|
||||||
open Cpdferror
|
open Cpdferror
|
||||||
|
|
||||||
(* Keep a record of charcodes used for font subsetting. *)
|
|
||||||
let used = null_hash ()
|
|
||||||
|
|
||||||
(* If we have used a TTF font, put its name and object number here. Then we
|
|
||||||
know we put it there, and can avoid writing it anew across ANDS. *)
|
|
||||||
let glob_pdfobjnum = null_hash ()
|
|
||||||
|
|
||||||
type color =
|
type color =
|
||||||
Grey of float
|
Grey of float
|
||||||
| RGB of float * float * float
|
| RGB of float * float * float
|
||||||
@ -58,14 +51,6 @@ let colour_op_stroke = function
|
|||||||
| CYMK (c, y, m, k) -> Pdfops.Op_K (c, y, m, k)
|
| CYMK (c, y, m, k) -> Pdfops.Op_K (c, y, m, k)
|
||||||
|
|
||||||
let ops fontname longest_w x y rotate hoffset voffset outline linewidth unique_fontname unique_extgstatename colour fontsize text =
|
let ops fontname longest_w x y rotate hoffset voffset outline linewidth unique_fontname unique_extgstatename colour fontsize text =
|
||||||
begin match Hashtbl.find used fontname with
|
|
||||||
| exception Not_found ->
|
|
||||||
let thisused = null_hash () in
|
|
||||||
String.iter (fun x -> Hashtbl.replace thisused x ()) text;
|
|
||||||
Hashtbl.add used fontname thisused
|
|
||||||
| thisused ->
|
|
||||||
String.iter (fun x -> Hashtbl.replace thisused x ()) text;
|
|
||||||
end;
|
|
||||||
[Pdfops.Op_q;
|
[Pdfops.Op_q;
|
||||||
Pdfops.Op_BMC "/CPDFSTAMP";
|
Pdfops.Op_BMC "/CPDFSTAMP";
|
||||||
Pdfops.Op_cm
|
Pdfops.Op_cm
|
||||||
@ -234,6 +219,16 @@ let replace_pairs pdf endpage extract_text_font_size filename bates batespad num
|
|||||||
then numstring
|
then numstring
|
||||||
else implode (many '0' (w - String.length numstring)) ^ numstring))]
|
else implode (many '0' (w - String.length numstring)) ^ numstring))]
|
||||||
|
|
||||||
|
let expand_lines text time pdf endpage extract_text_font_size filename bates batespad num page lines =
|
||||||
|
let expanded_lines =
|
||||||
|
map
|
||||||
|
(function text ->
|
||||||
|
process_text time text (replace_pairs pdf endpage extract_text_font_size filename bates batespad num page))
|
||||||
|
lines
|
||||||
|
in
|
||||||
|
(* process URLs for justification too *)
|
||||||
|
map (fun line -> fst (get_urls_line line)) expanded_lines
|
||||||
|
|
||||||
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 font fontpdfobj underneath position hoffset voffset text pages
|
fontsize font fontpdfobj underneath position hoffset voffset text pages
|
||||||
@ -300,16 +295,7 @@ let addtext
|
|||||||
let ops, urls, x, y, hoffset, voffset, text, joffset =
|
let ops, urls, x, y, hoffset, voffset, text, joffset =
|
||||||
let text = process_text time text (replace_pairs pdf endpage extract_text_font_size filename bates batespad num page) in
|
let text = process_text time text (replace_pairs pdf endpage extract_text_font_size filename bates batespad num page) in
|
||||||
let text, urls = get_urls_line text in
|
let text, urls = get_urls_line text in
|
||||||
|
let expanded_lines = expand_lines text time pdf endpage extract_text_font_size filename bates batespad num page lines in
|
||||||
let expanded_lines =
|
|
||||||
map
|
|
||||||
(function text ->
|
|
||||||
process_text time text (replace_pairs pdf endpage extract_text_font_size filename bates batespad num page))
|
|
||||||
lines
|
|
||||||
in
|
|
||||||
let expanded_lines = (* process URLs for justification too *)
|
|
||||||
map (fun line -> fst (get_urls_line line)) expanded_lines
|
|
||||||
in
|
|
||||||
let textwidth = calc_textwidth text
|
let textwidth = calc_textwidth text
|
||||||
and allwidths = map calc_textwidth expanded_lines in
|
and allwidths = map calc_textwidth expanded_lines in
|
||||||
let longest_w = last (sort compare allwidths) in
|
let longest_w = last (sort compare allwidths) in
|
||||||
@ -426,8 +412,19 @@ let
|
|||||||
justification midline topline filename extract_text_font_size shift
|
justification midline topline filename extract_text_font_size shift
|
||||||
?(raw=false) pdf
|
?(raw=false) pdf
|
||||||
=
|
=
|
||||||
let time = Cpdfstrftime.current_time () in
|
|
||||||
if pages = [] then error "addtexts: empty page range" else
|
if pages = [] then error "addtexts: empty page range" else
|
||||||
|
let time = Cpdfstrftime.current_time () in
|
||||||
|
let endpage = Pdfpage.endpage pdf in
|
||||||
|
let ps = Pdfpage.pages_of_pagetree pdf in
|
||||||
|
let used = null_hash () in
|
||||||
|
let lines = map unescape_string (split_at_newline text) in
|
||||||
|
iter2
|
||||||
|
(fun num page ->
|
||||||
|
let expanded_lines = expand_lines text time pdf endpage extract_text_font_size filename bates batespad num page lines in
|
||||||
|
let codepoints = map Pdftext.codepoints_of_utf8 expanded_lines in
|
||||||
|
iter (iter (fun x -> Hashtbl.replace used x ())) codepoints)
|
||||||
|
pages
|
||||||
|
(map (fun x -> List.nth ps (x - 1)) pages);
|
||||||
let realfontname = ref fontname in
|
let realfontname = ref fontname in
|
||||||
let font =
|
let font =
|
||||||
match cpdffont with
|
match cpdffont with
|
||||||
@ -436,7 +433,7 @@ let
|
|||||||
Some (hd (fst f))
|
Some (hd (fst f))
|
||||||
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
|
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
|
||||||
(*Printf.printf "Cpdfaddtext.addtexts: EmbedInfo\n";*)
|
(*Printf.printf "Cpdfaddtext.addtexts: EmbedInfo\n";*)
|
||||||
let embedded = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding in
|
let embedded = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:(map fst (list_of_hashtbl used)) ~encoding in
|
||||||
Some (hd (fst embedded))
|
Some (hd (fst embedded))
|
||||||
| Cpdfembed.ExistingNamedFont -> None
|
| Cpdfembed.ExistingNamedFont -> None
|
||||||
in
|
in
|
||||||
@ -445,13 +442,7 @@ let
|
|||||||
| Some (Pdftext.StandardFont _ as font) ->
|
| Some (Pdftext.StandardFont _ as font) ->
|
||||||
Pdf.Indirect (Pdftext.write_font pdf font)
|
Pdf.Indirect (Pdftext.write_font pdf font)
|
||||||
| Some f ->
|
| Some f ->
|
||||||
begin match Hashtbl.find glob_pdfobjnum fontname with
|
Pdf.Indirect (Pdftext.write_font pdf f)
|
||||||
| exception Not_found ->
|
|
||||||
let i = Pdftext.write_font pdf f in
|
|
||||||
Hashtbl.add glob_pdfobjnum fontname i; Pdf.Indirect i
|
|
||||||
| i ->
|
|
||||||
Pdf.Indirect i
|
|
||||||
end
|
|
||||||
| None ->
|
| None ->
|
||||||
let firstpage =
|
let firstpage =
|
||||||
List.nth (Pdfpage.pages_of_pagetree pdf) (hd pages - 1)
|
List.nth (Pdfpage.pages_of_pagetree pdf) (hd pages - 1)
|
||||||
@ -532,23 +523,14 @@ let
|
|||||||
!pdf;
|
!pdf;
|
||||||
voffset := !voffset +. (linespacing *. fontsize))
|
voffset := !voffset +. (linespacing *. fontsize))
|
||||||
lines;
|
lines;
|
||||||
begin match cpdffont with
|
(*begin match cpdffont with
|
||||||
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
|
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
|
||||||
let charcodes =
|
let codepoints = map fst (list_of_hashtbl used) in
|
||||||
match Hashtbl.find used fontname with
|
|
||||||
| exception Not_found -> []
|
|
||||||
| thisused -> map fst (list_of_hashtbl thisused)
|
|
||||||
in
|
|
||||||
let encoding_table = Pdftext.table_of_encoding encoding in
|
|
||||||
let glyphlist_table = Pdfglyphlist.glyph_hashes () in
|
|
||||||
let codepoints =
|
|
||||||
map (fun c -> unicode_codepoint_of_pdfcode encoding_table glyphlist_table (int_of_char c)) charcodes
|
|
||||||
in
|
|
||||||
let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in
|
let objnum = match fontpdfobj with Pdf.Indirect i -> i | _ -> failwith "bad fontpdfobj" in
|
||||||
let font = hd (fst (Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding)) in
|
let font = hd (fst (Cpdfembed.embed_truetype !pdf ~fontfile ~fontname ~codepoints ~encoding)) in
|
||||||
ignore (Pdftext.write_font ~objnum !pdf font)
|
ignore (Pdftext.write_font ~objnum !pdf font)
|
||||||
| _ -> ()
|
| _ -> ()
|
||||||
end;
|
end;*)
|
||||||
!pdf
|
!pdf
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,9 +2,6 @@
|
|||||||
open Pdfutil
|
open Pdfutil
|
||||||
open Pdfio
|
open Pdfio
|
||||||
|
|
||||||
(* FIXME Make sure -add-text calls subset once only *)
|
|
||||||
(* FIXME Make it work with -draw *)
|
|
||||||
(* FIXME Check proper use of fontpacks and maximal sharing in -toc -typeset -add-text -draw *)
|
|
||||||
(* FIXME Proper widths for .notdef, and warn on .notdef being produced *)
|
(* FIXME Proper widths for .notdef, and warn on .notdef being produced *)
|
||||||
(* FIXME Add suport for composite glyphs *)
|
(* FIXME Add suport for composite glyphs *)
|
||||||
(* FIXME Make sure -embed-std14 works for all commands *)
|
(* FIXME Make sure -embed-std14 works for all commands *)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user