Commit 62ea3b9197a9158d9c29261b9436da56e563c0b3

Authored by Jay Berkenbilt
1 parent ae9455bf

Add outlines to json at document level

Showing 1 changed file with 54 additions and 2 deletions
qpdf/qpdf.cc
@@ -342,6 +342,27 @@ static JSON json_schema(std::set<std::string>* keys = 0) @@ -342,6 +342,27 @@ static JSON json_schema(std::set<std::string>* keys = 0)
342 "label", 342 "label",
343 JSON::makeString("page label dictionary")); 343 JSON::makeString("page label dictionary"));
344 } 344 }
  345 + if (all_keys || keys->count("outlines"))
  346 + {
  347 + JSON outlines = schema.addDictionaryMember(
  348 + "outlines", JSON::makeArray()).
  349 + addArrayElement(JSON::makeDictionary());
  350 + outlines.addDictionaryMember(
  351 + "object",
  352 + JSON::makeString("reference to this outline"));
  353 + outlines.addDictionaryMember(
  354 + "title",
  355 + JSON::makeString("outline title"));
  356 + outlines.addDictionaryMember(
  357 + "dest",
  358 + JSON::makeString("outline destination dictionary"));
  359 + outlines.addDictionaryMember(
  360 + "kids",
  361 + JSON::makeString("array of descendent outlines"));
  362 + outlines.addDictionaryMember(
  363 + "open",
  364 + JSON::makeString("whether the outline is displayed expanded"));
  365 + }
345 return schema; 366 return schema;
346 } 367 }
347 368
@@ -657,7 +678,8 @@ ArgParser::initOptionTable() @@ -657,7 +678,8 @@ ArgParser::initOptionTable()
657 (*t)["json"] = oe_bare(&ArgParser::argJson); 678 (*t)["json"] = oe_bare(&ArgParser::argJson);
658 // The list of selectable top-level keys id duplicated in three 679 // The list of selectable top-level keys id duplicated in three
659 // places: json_schema, do_json, and initOptionTable. 680 // places: json_schema, do_json, and initOptionTable.
660 - char const* jsonKeyChoices[] = {"objects", "pages", "pagelabels", 0}; 681 + char const* jsonKeyChoices[] = {
  682 + "objects", "pages", "pagelabels", "outlines", 0};
661 (*t)["json-key"] = oe_requiredChoices( 683 (*t)["json-key"] = oe_requiredChoices(
662 &ArgParser::argJsonKey, jsonKeyChoices); 684 &ArgParser::argJsonKey, jsonKeyChoices);
663 (*t)["json-object"] = oe_requiredParameter( 685 (*t)["json-object"] = oe_requiredParameter(
@@ -802,7 +824,8 @@ ArgParser::argJsonHelp() @@ -802,7 +824,8 @@ ArgParser::argJsonHelp()
802 << "and \"parameters\" keys will always be present." 824 << "and \"parameters\" keys will always be present."
803 << std::endl 825 << std::endl
804 << std::endl 826 << std::endl
805 - << json_schema().serialize(); 827 + << json_schema().serialize()
  828 + << std::endl;
806 } 829 }
807 830
808 void 831 void
@@ -2755,6 +2778,31 @@ static void do_json_page_labels(QPDF&amp; pdf, Options&amp; o, JSON&amp; j) @@ -2755,6 +2778,31 @@ static void do_json_page_labels(QPDF&amp; pdf, Options&amp; o, JSON&amp; j)
2755 } 2778 }
2756 } 2779 }
2757 2780
  2781 +static void add_outlines_to_json(
  2782 + std::list<QPDFOutlineObjectHelper> outlines, JSON& j)
  2783 +{
  2784 + for (std::list<QPDFOutlineObjectHelper>::iterator iter = outlines.begin();
  2785 + iter != outlines.end(); ++iter)
  2786 + {
  2787 + QPDFOutlineObjectHelper& ol = *iter;
  2788 + JSON jo = j.addArrayElement(JSON::makeDictionary());
  2789 + jo.addDictionaryMember("object", ol.getObjectHandle().getJSON());
  2790 + jo.addDictionaryMember("title", JSON::makeString(ol.getTitle()));
  2791 + jo.addDictionaryMember("dest", ol.getDest().getJSON(true));
  2792 + jo.addDictionaryMember("open", JSON::makeBool(ol.getCount() >= 0));
  2793 + JSON j_kids = jo.addDictionaryMember("kids", JSON::makeArray());
  2794 + add_outlines_to_json(ol.getKids(), j_kids);
  2795 + }
  2796 +}
  2797 +
  2798 +static void do_json_outlines(QPDF& pdf, Options& o, JSON& j)
  2799 +{
  2800 + JSON j_outlines = j.addDictionaryMember(
  2801 + "outlines", JSON::makeArray());
  2802 + QPDFOutlineDocumentHelper odh(pdf);
  2803 + add_outlines_to_json(odh.getTopLevelOutlines(), j_outlines);
  2804 +}
  2805 +
2758 static void do_json(QPDF& pdf, Options& o) 2806 static void do_json(QPDF& pdf, Options& o)
2759 { 2807 {
2760 JSON j = JSON::makeDictionary(); 2808 JSON j = JSON::makeDictionary();
@@ -2799,6 +2847,10 @@ static void do_json(QPDF&amp; pdf, Options&amp; o) @@ -2799,6 +2847,10 @@ static void do_json(QPDF&amp; pdf, Options&amp; o)
2799 { 2847 {
2800 do_json_page_labels(pdf, o, j); 2848 do_json_page_labels(pdf, o, j);
2801 } 2849 }
  2850 + if (all_keys || o.json_keys.count("outlines"))
  2851 + {
  2852 + do_json_outlines(pdf, o, j);
  2853 + }
2802 2854
2803 // Check against schema 2855 // Check against schema
2804 2856