This commit is contained in:
John Whitington 2023-02-20 12:56:59 +00:00
parent 3bb87b6352
commit c803c9e511
5 changed files with 34 additions and 39 deletions

View File

@ -200,7 +200,7 @@ let get_annotations_json pdf =
let json = `List (flatten (map2 (annotations_json_page pdf) pages pagenums)) in let json = `List (flatten (map2 (annotations_json_page pdf) pages pagenums)) in
Pdfio.bytes_of_string (J.to_string json) Pdfio.bytes_of_string (J.to_string json)
(** Set annotations from JSON. Existing annotations will be removed. *) (** Set annotations from JSON. Existing annotations? *)
let set_annotations_json pdf json = () let set_annotations_json pdf json = ()
(* Equalise the page lengths of two PDFs by chopping or extending the first one. (* Equalise the page lengths of two PDFs by chopping or extending the first one.

View File

@ -3369,37 +3369,28 @@ let prerotate range pdf =
Cpdfpage.upright ~fast:args.fast range pdf Cpdfpage.upright ~fast:args.fast range pdf
let embed_font () = let embed_font () =
let fontpack_of_standardfont sf = match args.font with
let te = Pdftext.text_extractor_of_font_real sf in | StandardFont f ->
let table = null_hash () in (* FIXME proper error handling for missing file etc. *)
for x = 0 to 255 do begin match args.embedstd14 with
let u = hd (Pdftext.codepoints_of_text te (string_of_char (char_of_int x))) in | Some dirname ->
Hashtbl.add table u (0, x) begin try
done; let fontfile, fontname =
([sf], table) let filename = hd (List.assoc f fontnames) in
in Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)),
match args.font with Filename.remove_extension filename
| StandardFont f -> in
(* FIXME proper error handling for missing file etc. *) Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding}
begin match args.embedstd14 with with
| Some dirname -> e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
begin try
let fontfile, fontname =
let filename = hd (List.assoc f fontnames) in
Pdfio.bytes_of_string (contents_of_file (Filename.concat dirname filename)),
Filename.remove_extension filename
in
Cpdfembed.EmbedInfo {fontfile; fontname; encoding = args.fontencoding}
with
e -> error (Printf.sprintf "Can't load font for embedding: %s\n" (Printexc.to_string e))
end
| None ->
PreMadeFontPack (fontpack_of_standardfont (Pdftext.StandardFont (f, args.fontencoding)))
end end
| OtherFont f -> | None ->
ExistingNamedFont PreMadeFontPack (Cpdfembed.fontpack_of_standardfont (Pdftext.StandardFont (f, args.fontencoding)))
| FontToEmbed fontfile -> end
EmbedInfo {fontfile; fontname = args.fontname; encoding = args.fontencoding} | OtherFont f ->
ExistingNamedFont
| FontToEmbed fontfile ->
EmbedInfo {fontfile; fontname = args.fontname; encoding = args.fontencoding}
(* Main function *) (* Main function *)
let go () = let go () =

View File

@ -8,6 +8,15 @@ type cpdffont =
| EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; encoding : Pdftext.encoding} | EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; encoding : Pdftext.encoding}
| ExistingNamedFont | ExistingNamedFont
let fontpack_of_standardfont sf =
let te = Pdftext.text_extractor_of_font_real sf in
let table = null_hash () in
for x = 0 to 255 do
let u = hd (Pdftext.codepoints_of_text te (string_of_char (char_of_int x))) in
Hashtbl.add table u (0, x)
done;
([sf], table)
let get_char (fonts, table) u = let get_char (fonts, table) u =
match Hashtbl.find table u with match Hashtbl.find table u with
| (n, charcode) -> Some (charcode, n, List.nth fonts n) | (n, charcode) -> Some (charcode, n, List.nth fonts n)

View File

@ -15,6 +15,8 @@ type cpdffont =
| EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; encoding : Pdftext.encoding} | EmbedInfo of {fontfile : Pdfio.bytes; fontname : string; encoding : Pdftext.encoding}
| ExistingNamedFont | ExistingNamedFont
val fontpack_of_standardfont : Pdftext.font -> t
val get_char : t -> int -> (int * int * Pdftext.font) option val get_char : t -> int -> (int * int * Pdftext.font) option
val embed_truetype : val embed_truetype :

View File

@ -234,16 +234,9 @@ let squeeze ?logto ?(pagedata=true) ?(recompress=true) pdf =
end; end;
if recompress then if recompress then
begin begin
ignore (decompress_pdf pdf);
log (Printf.sprintf "Recompressing document\n"); log (Printf.sprintf "Recompressing document\n");
let n = !Pdfcodec.flate_level in ignore (recompress_pdf pdf);
Pdfcodec.flate_level := 9;
ignore (recompress_pdf pdf);
Pdfcodec.flate_level := n
end end
else
(* Re-compress processed pagedata, even if recompress not set *)
if pagedata then ignore (recompress_pdf pdf)
with with
e -> e ->
raise raise