Implement -list-fonts-json

This commit is contained in:
John Whitington 2023-10-31 16:23:20 +00:00
parent e6c718f367
commit 442acf71cd
3 changed files with 13 additions and 5 deletions

View File

@ -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 ->

View File

@ -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)))

View File

@ -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