From de0c2b0863c2fd49046deca07c24b38195c06f05 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 4 Jun 2025 11:43:30 +0100 Subject: [PATCH] Allow Int/Float to appear in CPDFJSON and CPDF bookmark JSON --- cpdfbookmarks.ml | 16 +++++++++++----- cpdfjson.ml | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cpdfbookmarks.ml b/cpdfbookmarks.ml index 3d93282..22a540e 100644 --- a/cpdfbookmarks.ml +++ b/cpdfbookmarks.ml @@ -65,18 +65,24 @@ let bookmark_of_data pdf i s i' isopen optionaldest = let target_of_json_target pdf pagenumber target = target_of_markfile_obj pdf pagenumber (Cpdfjson.object_of_json target) +let rec json_make_all_float = function + | `Assoc l -> `Assoc (map (fun (k, v) -> (k, json_make_all_float v)) l) + | `List l -> `List (map json_make_all_float l) + | `Int i -> `Float (float_of_int i) + | x -> x + let mark_of_json pdf = function | `Assoc [("bold", `Bool bold); ("colour", `List [`Float r; `Float g; `Float b]); ("italic", `Bool italic); - ("level", `Int level); + ("level", `Float level); ("open", `Bool openstatus); - ("page", `Int pagenumber); + ("page", `Float pagenumber); ("target", target); ("text", `String text)] -> - {Pdfmarks.level = level; + {Pdfmarks.level = int_of_float level; Pdfmarks.text = Pdftext.pdfdocstring_of_utf8 text; - Pdfmarks.target = target_of_json_target pdf pagenumber target; + Pdfmarks.target = target_of_json_target pdf (int_of_float pagenumber) target; Pdfmarks.isopen = openstatus; Pdfmarks.colour = (r, g, b); Pdfmarks.flags = (if italic then 1 else 0) lor ((if bold then 1 else 0) lsl 1)} @@ -87,7 +93,7 @@ let mark_of_json_sort pdf = function | _ -> error "malformed mark in mark_of_json_sort" let marks_of_json pdf = function - | `List ms -> map (mark_of_json_sort pdf) ms + | `List ms -> map (mark_of_json_sort pdf) (map json_make_all_float ms) | _ -> error "top level of JSON boomark file not a list" let parse_bookmark_file_json verify pdf i = diff --git a/cpdfjson.ml b/cpdfjson.ml index 3b7530a..ee55491 100644 --- a/cpdfjson.ml +++ b/cpdfjson.ml @@ -222,7 +222,9 @@ and object_of_json = function _ -> Pdfe.log (Printf.sprintf "Could not read UTF8 string %S\n" u); P.String u end | `Assoc ["I", `Int i] -> P.Integer i + | `Assoc ["I", `Float f] -> P.Integer (int_of_float f) | `Assoc ["F", `Float f] -> P.Real f + | `Assoc ["F", `Int i] -> P.Real (float_of_int i) | `Assoc ["N", `String n] -> P.Name n | `Assoc ["S", `List [dict; `String data]] -> let d' =