cpdf-source/cpdfcomposition.ml

122 lines
5.0 KiB
OCaml
Raw Normal View History

2023-04-13 17:51:11 +02:00
open Pdfutil
2023-04-13 20:11:29 +02:00
let size pdf i =
String.length (Pdfwrite.string_of_pdf_including_data (Pdf.lookup_obj pdf i))
2023-04-13 17:51:11 +02:00
let find_composition_images pdf i obj marked =
2023-04-13 20:11:29 +02:00
match Hashtbl.find marked i with () -> 0 | exception Not_found ->
2023-04-13 17:51:11 +02:00
match Pdf.lookup_direct pdf "/Subtype" obj with
| Some (Pdf.Name "/Image") ->
Hashtbl.add marked i ();
2023-04-14 18:13:49 +02:00
(*Printf.printf "obj = %s\n" (Pdfwrite.string_of_pdf obj);*)
2023-04-13 20:11:29 +02:00
String.length (Pdfwrite.string_of_pdf_including_data obj)
2023-04-13 17:51:11 +02:00
| _ -> 0
2023-04-13 20:11:29 +02:00
(* If it has /Font, find all objects referenced from it, and add
any not already marked to the count *)
let find_composition_fonts pdf i obj marked =
match Hashtbl.find marked i with () -> 0 | exception Not_found ->
let l = ref 0 in
match Pdf.lookup_direct pdf "/Type" obj with
| Some (Pdf.Name "/Font") ->
iter
(fun i ->
2023-04-13 21:23:59 +02:00
(*Printf.printf "Object %i\n%s\n" i (Pdfwrite.string_of_pdf (Pdf.lookup_obj pdf i));*)
2023-04-13 20:11:29 +02:00
match Hashtbl.find marked i with
| () -> ()
| exception Not_found -> l += size pdf i; Hashtbl.add marked i ())
(Pdf.objects_referenced [] [] pdf (Pdf.Indirect i));
!l
| _ -> 0
2023-04-13 17:51:11 +02:00
let find_composition_content_streams pdf i obj marked =
2023-04-13 20:11:29 +02:00
match Hashtbl.find marked i with () -> 0 | exception Not_found ->
2023-04-13 17:51:11 +02:00
match Pdf.lookup_direct pdf "/Type" obj with
| Some (Pdf.Name "/Page") ->
2023-04-13 21:23:59 +02:00
(*Printf.printf "Found a page...%s\n" (Pdfwrite.string_of_pdf (Pdf.direct pdf obj));*)
2023-04-13 17:51:11 +02:00
let cs =
2023-04-13 21:23:59 +02:00
match obj with Pdf.Dictionary d ->
begin match lookup "/Contents" d with
| Some (Pdf.Indirect i) -> [i]
| Some (Pdf.Array is) -> option_map (function Pdf.Indirect i -> Some i | _ -> None) is
| _ -> []
end
2023-04-13 17:51:11 +02:00
| _ -> []
in
2023-04-13 21:23:59 +02:00
(*Printf.printf "Found %i content streams\n" (length cs);*)
2023-04-13 17:51:11 +02:00
let l = ref 0 in
iter
(fun i ->
2023-04-13 21:23:59 +02:00
(*Printf.printf "Considering content stream %i\n" i;*)
2023-04-13 20:11:29 +02:00
match Hashtbl.find marked i with
| () -> ()
| exception Not_found -> Hashtbl.add marked i (); l += size pdf i)
2023-04-13 17:51:11 +02:00
cs;
!l
2023-04-14 18:13:49 +02:00
| _ ->
match Pdf.lookup_direct pdf "/Subtype" obj with
| Some (Pdf.Name "/Form") ->
Hashtbl.add marked i ();
size pdf i
| _ -> 0
2023-04-13 17:51:11 +02:00
let find_composition_structure_info pdf i obj marked = 0
let find_composition_link_annotations pdf i obj marked = 0
let find_composition_embedded_files pdf i obj marked = 0
let find_composition pdf =
let marked = null_hash () in
let images = ref 0 in
let fonts = ref 0 in
let content_streams = ref 0 in
let structure_info = ref 0 in
let link_annotations = ref 0 in
let embedded_files = ref 0 in
Pdf.objiter
(fun i obj ->
2023-04-14 18:13:49 +02:00
(*Printf.printf "Looking at object %i\n" i;
Printf.printf "Which is %s\n" (Pdfwrite.string_of_pdf (Pdf.lookup_obj pdf i));
Printf.printf "Marked objects at beginning: ";
2023-04-13 21:23:59 +02:00
Hashtbl.iter (fun k () -> Printf.printf "%i " k) marked;
Printf.printf "\n";*)
2023-04-13 17:51:11 +02:00
match Hashtbl.find marked i with _ -> () | exception Not_found ->
2023-04-14 18:13:49 +02:00
embedded_files += find_composition_embedded_files pdf i obj marked;
images += find_composition_images pdf i obj marked;
2023-04-13 17:51:11 +02:00
content_streams += find_composition_content_streams pdf i obj marked;
2023-04-14 18:13:49 +02:00
structure_info += find_composition_structure_info pdf i obj marked;
link_annotations += find_composition_link_annotations pdf i obj marked;
2023-04-13 20:11:29 +02:00
fonts += find_composition_fonts pdf i obj marked)
2023-04-13 17:51:11 +02:00
pdf;
(!images, !fonts, !content_streams, !structure_info, !link_annotations, !embedded_files)
(* First go: images, fonts, content streams, structure info, link annotations, embedded files *)
let show_composition_json filesize pdf =
let perc x = float_of_int x /. float_of_int filesize *. 100. in
let images, fonts, content_streams, structure_info, link_annotations, embedded_files =
find_composition pdf
in
let r = images + fonts + content_streams + structure_info + link_annotations + embedded_files in
`List [`Tuple [`String "Images"; `Int images; `Float (perc images)];
`Tuple [`String "Fonts"; `Int fonts; `Float (perc fonts)];
`Tuple [`String "Content streams"; `Int content_streams; `Float (perc content_streams)];
`Tuple [`String "Structure Info"; `Int structure_info; `Float (perc structure_info)];
`Tuple [`String "Link Annotations"; `Int link_annotations; `Float (perc link_annotations)];
`Tuple [`String "Embedded Files"; `Int embedded_files; `Float (perc embedded_files)];
`Tuple [`String "Unclassified"; `Int (filesize - r); `Float (perc (filesize - r))]]
let show_composition filesize json pdf =
let module J = Cpdfyojson.Safe in
let j = show_composition_json filesize pdf in
if json then (flprint (J.pretty_to_string j); flprint "\n") else
match j with
| `List js ->
2023-04-13 20:11:29 +02:00
iter
(function
| `Tuple [`String a; `Int b; `Float c] -> Printf.printf "%s: %i bytes (%.2f%%)\n" a b c
| _ -> ())
js
2023-04-13 17:51:11 +02:00
| _ -> ()