From 442acf71cd899311e286965b07b0aeb6e3e4eb10 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 31 Oct 2023 16:23:20 +0000 Subject: [PATCH] Implement -list-fonts-json --- cpdfcommand.ml | 2 +- cpdffont.ml | 14 +++++++++++--- cpdffont.mli | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 87250cb..1be56d6 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3499,7 +3499,7 @@ let go () = | (_, pagespec, _, _, _, _)::_, _ -> let pdf = get_single_pdf (Some Fonts) true in let range = parse_pagespec_allow_empty pdf pagespec in - Cpdffont.print_fonts pdf range + Cpdffont.print_fonts ~json:args.format_json pdf range | _ -> error "-list-fonts: bad command line" end | Some ListBookmarks -> diff --git a/cpdffont.ml b/cpdffont.ml index 6eb3d41..2ec4953 100644 --- a/cpdffont.ml +++ b/cpdffont.ml @@ -297,7 +297,15 @@ let list_fonts pdf range = let string_of_font (p, n, s, b, e) = Printf.sprintf "%i %s %s %s %s\n" p n s b e -let print_fonts pdf range = - flprint - (fold_left ( ^ ) "" (map string_of_font (list_fonts pdf range))) +let json_of_font (pagenum, name, subtype, basefont, encoding) = + `Assoc + [("page", `Int pagenum); + ("name", `String name); + ("subtype", `String subtype); + ("basefont", `String basefont); + ("encoding", `String encoding)] +let print_fonts ?(json=false) pdf range = + if json + then flprint (Cpdfyojson.Safe.pretty_to_string (`List (map json_of_font (list_fonts pdf range)))) + else flprint (fold_left ( ^ ) "" (map string_of_font (list_fonts pdf range))) diff --git a/cpdffont.mli b/cpdffont.mli index 85168e6..8e6c61d 100644 --- a/cpdffont.mli +++ b/cpdffont.mli @@ -3,7 +3,7 @@ (** {2 Listing fonts} *) (** Print font list to stdout *) -val print_fonts : Pdf.t -> int list -> unit +val print_fonts : ?json:bool -> Pdf.t -> int list -> unit (** Return font list. Page number, name, subtype, basefont, encoding. *) val list_fonts : Pdf.t -> int list -> (int * string * string * string * string) list