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 342 "label",
343 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 366 return schema;
346 367 }
347 368  
... ... @@ -657,7 +678,8 @@ ArgParser::initOptionTable()
657 678 (*t)["json"] = oe_bare(&ArgParser::argJson);
658 679 // The list of selectable top-level keys id duplicated in three
659 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 683 (*t)["json-key"] = oe_requiredChoices(
662 684 &ArgParser::argJsonKey, jsonKeyChoices);
663 685 (*t)["json-object"] = oe_requiredParameter(
... ... @@ -802,7 +824,8 @@ ArgParser::argJsonHelp()
802 824 << "and \"parameters\" keys will always be present."
803 825 << std::endl
804 826 << std::endl
805   - << json_schema().serialize();
  827 + << json_schema().serialize()
  828 + << std::endl;
806 829 }
807 830  
808 831 void
... ... @@ -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 2806 static void do_json(QPDF& pdf, Options& o)
2759 2807 {
2760 2808 JSON j = JSON::makeDictionary();
... ... @@ -2799,6 +2847,10 @@ static void do_json(QPDF&amp; pdf, Options&amp; o)
2799 2847 {
2800 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 2855 // Check against schema
2804 2856  
... ...