Commit 2394dd8519feed0fd6e04eb903d75029316b36e7

Authored by Jay Berkenbilt
1 parent e2975b9e

QPDFJob increment: static functions to member functions

Convert remaining static functions that take QPDFJob& as a parameter
to member functions. Utility functions that don't take QPDFJob& remain
static functions and can probably just stay that way since the keep
extra complexity out of QPDFJob.hh.
include/qpdf/QPDFJob.hh
... ... @@ -23,9 +23,10 @@
23 23 #define QPDFJOB_HH
24 24  
25 25 #include <qpdf/DLL.h>
  26 +#include <qpdf/Constants.h>
26 27 #include <qpdf/PointerHolder.hh>
27 28 #include <qpdf/QPDF.hh>
28   -#include <qpdf/Constants.h>
  29 +#include <qpdf/QPDFPageObjectHelper.hh>
29 30  
30 31 #include <string>
31 32 #include <list>
... ... @@ -169,6 +170,12 @@ class QPDFJob
169 170 std::string prefix;
170 171 };
171 172  
  173 + PointerHolder<QPDF> doProcessOnce(
  174 + std::function<void(QPDF*, char const*)> fn,
  175 + char const* password, bool empty);
  176 + PointerHolder<QPDF> doProcess(
  177 + std::function<void(QPDF*, char const*)> fn,
  178 + char const* password, bool empty);
172 179 PointerHolder<QPDF> processFile(
173 180 char const* filename, char const* password);
174 181 void validateUnderOverlay(QPDF& pdf, QPDFJob::UnderOverlay* uo);
... ... @@ -181,6 +188,7 @@ class QPDFJob
181 188 void writeOutfile(QPDF& pdf);
182 189 void doJSON(QPDF& pdf);
183 190 void doInspection(QPDF& pdf);
  191 + void setQPDFOptions(QPDF& pdf);
184 192 void showEncryption(QPDF& pdf);
185 193 void doCheck(QPDF& pdf);
186 194 void doShowObj(QPDF& pdf);
... ... @@ -189,6 +197,33 @@ class QPDFJob
189 197 void setEncryptionOptions(QPDF&, QPDFWriter&);
190 198 void maybeFixWritePassword(int R, std::string& password);
191 199  
  200 + void doShowAttachment(QPDF& pdf);
  201 + std::set<QPDFObjGen> getWantedJSONObjects();
  202 + void doJSONObjects(QPDF& pdf, JSON& j);
  203 + void doJSONObjectinfo(QPDF& pdf, JSON& j);
  204 + void doJSONPages(QPDF& pdf, JSON& j);
  205 + void doJSONPageLabels(QPDF& pdf, JSON& j);
  206 + void doJSONOutlines(QPDF& pdf, JSON& j);
  207 + void doJSONAcroform(QPDF& pdf, JSON& j);
  208 + void doJSONEncrypt(QPDF& pdf, JSON& j);
  209 + void doJSONAttachments(QPDF& pdf, JSON& j);
  210 + PointerHolder<QPDF> processInputSource(
  211 + PointerHolder<InputSource> is, char const* password);
  212 + void doUnderOverlayForPage(
  213 + QPDF& pdf,
  214 + QPDFJob::UnderOverlay& uo,
  215 + std::map<int, std::vector<int> >& pagenos,
  216 + size_t page_idx,
  217 + std::map<int, QPDFObjectHandle>& fo,
  218 + std::vector<QPDFPageObjectHelper>& pages,
  219 + QPDFPageObjectHelper& dest_page,
  220 + bool before);
  221 + bool shouldRemoveUnreferencedResources(QPDF& pdf);
  222 + void handlePageSpecs(
  223 + QPDF& pdf, bool& warnings,
  224 + std::vector<PointerHolder<QPDF>>& page_heap);
  225 + void handleRotations(QPDF& pdf);
  226 +
192 227 enum remove_unref_e { re_auto, re_yes, re_no };
193 228  
194 229 char const* password;
... ...
libqpdf/QPDFJob.cc
... ... @@ -284,8 +284,10 @@ static void parse_version(std::string const&amp; full_version_string,
284 284 version = v;
285 285 }
286 286  
287   -static void set_qpdf_options(QPDF& pdf, QPDFJob& o)
  287 +void
  288 +QPDFJob::setQPDFOptions(QPDF& pdf)
288 289 {
  290 + QPDFJob& o = *this; // QXXXQ
289 291 if (o.ignore_xref_streams)
290 292 {
291 293 pdf.setIgnoreXRefStreams(true);
... ... @@ -648,8 +650,10 @@ QPDFJob::doListAttachments(QPDF&amp; pdf)
648 650 }
649 651 }
650 652  
651   -static void do_show_attachment(QPDF& pdf, QPDFJob& o)
  653 +void
  654 +QPDFJob::doShowAttachment(QPDF& pdf)
652 655 {
  656 + QPDFJob& o = *this; // QXXXQ
653 657 QPDFEmbeddedFileDocumentHelper efdh(pdf);
654 658 auto fs = efdh.getEmbeddedFile(o.attachment_to_show);
655 659 if (! fs)
... ... @@ -684,9 +688,10 @@ QPDFJob::parse_object_id(std::string const&amp; objspec,
684 688 }
685 689 }
686 690  
687   -static std::set<QPDFObjGen>
688   -get_wanted_json_objects(QPDFJob& o)
  691 +std::set<QPDFObjGen>
  692 +QPDFJob::getWantedJSONObjects()
689 693 {
  694 + QPDFJob& o = *this; // QXXXQ
690 695 std::set<QPDFObjGen> wanted_og;
691 696 for (auto const& iter: o.json_objects)
692 697 {
... ... @@ -702,13 +707,15 @@ get_wanted_json_objects(QPDFJob&amp; o)
702 707 return wanted_og;
703 708 }
704 709  
705   -static void do_json_objects(QPDF& pdf, QPDFJob& o, JSON& j)
  710 +void
  711 +QPDFJob::doJSONObjects(QPDF& pdf, JSON& j)
706 712 {
  713 + QPDFJob& o = *this; // QXXXQ
707 714 // Add all objects. Do this first before other code below modifies
708 715 // things by doing stuff like calling
709 716 // pushInheritedAttributesToPage.
710 717 bool all_objects = o.json_objects.empty();
711   - std::set<QPDFObjGen> wanted_og = get_wanted_json_objects(o);
  718 + std::set<QPDFObjGen> wanted_og = getWantedJSONObjects();
712 719 JSON j_objects = j.addDictionaryMember("objects", JSON::makeDictionary());
713 720 if (all_objects || o.json_objects.count("trailer"))
714 721 {
... ... @@ -727,12 +734,14 @@ static void do_json_objects(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
727 734 }
728 735 }
729 736  
730   -static void do_json_objectinfo(QPDF& pdf, QPDFJob& o, JSON& j)
  737 +void
  738 +QPDFJob::doJSONObjectinfo(QPDF& pdf, JSON& j)
731 739 {
  740 + QPDFJob& o = *this; // QXXXQ
732 741 // Do this first before other code below modifies things by doing
733 742 // stuff like calling pushInheritedAttributesToPage.
734 743 bool all_objects = o.json_objects.empty();
735   - std::set<QPDFObjGen> wanted_og = get_wanted_json_objects(o);
  744 + std::set<QPDFObjGen> wanted_og = getWantedJSONObjects();
736 745 JSON j_objectinfo = j.addDictionaryMember(
737 746 "objectinfo", JSON::makeDictionary());
738 747 for (auto& obj: pdf.getAllObjects())
... ... @@ -760,8 +769,10 @@ static void do_json_objectinfo(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
760 769 }
761 770 }
762 771  
763   -static void do_json_pages(QPDF& pdf, QPDFJob& o, JSON& j)
  772 +void
  773 +QPDFJob::doJSONPages(QPDF& pdf, JSON& j)
764 774 {
  775 + QPDFJob& o = *this; // QXXXQ
765 776 JSON j_pages = j.addDictionaryMember("pages", JSON::makeArray());
766 777 QPDFPageDocumentHelper pdh(pdf);
767 778 QPDFPageLabelDocumentHelper pldh(pdf);
... ... @@ -848,7 +859,8 @@ static void do_json_pages(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
848 859 }
849 860 }
850 861  
851   -static void do_json_page_labels(QPDF& pdf, QPDFJob& o, JSON& j)
  862 +void
  863 +QPDFJob::doJSONPageLabels(QPDF& pdf, JSON& j)
852 864 {
853 865 JSON j_labels = j.addDictionaryMember("pagelabels", JSON::makeArray());
854 866 QPDFPageLabelDocumentHelper pldh(pdf);
... ... @@ -908,7 +920,8 @@ static void add_outlines_to_json(
908 920 }
909 921 }
910 922  
911   -static void do_json_outlines(QPDF& pdf, QPDFJob& o, JSON& j)
  923 +void
  924 +QPDFJob::doJSONOutlines(QPDF& pdf, JSON& j)
912 925 {
913 926 std::map<QPDFObjGen, int> page_numbers;
914 927 QPDFPageDocumentHelper dh(pdf);
... ... @@ -927,7 +940,8 @@ static void do_json_outlines(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
927 940 add_outlines_to_json(odh.getTopLevelOutlines(), j_outlines, page_numbers);
928 941 }
929 942  
930   -static void do_json_acroform(QPDF& pdf, QPDFJob& o, JSON& j)
  943 +void
  944 +QPDFJob::doJSONAcroform(QPDF& pdf, JSON& j)
931 945 {
932 946 JSON j_acroform = j.addDictionaryMember(
933 947 "acroform", JSON::makeDictionary());
... ... @@ -1030,8 +1044,10 @@ static void do_json_acroform(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
1030 1044 }
1031 1045 }
1032 1046  
1033   -static void do_json_encrypt(QPDF& pdf, QPDFJob& o, JSON& j)
  1047 +void
  1048 +QPDFJob::doJSONEncrypt(QPDF& pdf, JSON& j)
1034 1049 {
  1050 + QPDFJob& o = *this; // QXXXQ
1035 1051 int R = 0;
1036 1052 int P = 0;
1037 1053 int V = 0;
... ... @@ -1130,7 +1146,8 @@ static void do_json_encrypt(QPDF&amp; pdf, QPDFJob&amp; o, JSON&amp; j)
1130 1146 "filemethod", JSON::makeString(s_file_method));
1131 1147 }
1132 1148  
1133   -static void do_json_attachments(QPDF& pdf, QPDFJob& o, JSON& j)
  1149 +void
  1150 +QPDFJob::doJSONAttachments(QPDF& pdf, JSON& j)
1134 1151 {
1135 1152 JSON j_attachments = j.addDictionaryMember(
1136 1153 "attachments", JSON::makeDictionary());
... ... @@ -1528,35 +1545,35 @@ QPDFJob::doJSON(QPDF&amp; pdf)
1528 1545 // places: json_schema, doJSON, and initOptionTable.
1529 1546 if (all_keys || o.json_keys.count("objects"))
1530 1547 {
1531   - do_json_objects(pdf, o, j);
  1548 + doJSONObjects(pdf, j);
1532 1549 }
1533 1550 if (all_keys || o.json_keys.count("objectinfo"))
1534 1551 {
1535   - do_json_objectinfo(pdf, o, j);
  1552 + doJSONObjectinfo(pdf, j);
1536 1553 }
1537 1554 if (all_keys || o.json_keys.count("pages"))
1538 1555 {
1539   - do_json_pages(pdf, o, j);
  1556 + doJSONPages(pdf, j);
1540 1557 }
1541 1558 if (all_keys || o.json_keys.count("pagelabels"))
1542 1559 {
1543   - do_json_page_labels(pdf, o, j);
  1560 + doJSONPageLabels(pdf, j);
1544 1561 }
1545 1562 if (all_keys || o.json_keys.count("outlines"))
1546 1563 {
1547   - do_json_outlines(pdf, o, j);
  1564 + doJSONOutlines(pdf, j);
1548 1565 }
1549 1566 if (all_keys || o.json_keys.count("acroform"))
1550 1567 {
1551   - do_json_acroform(pdf, o, j);
  1568 + doJSONAcroform(pdf, j);
1552 1569 }
1553 1570 if (all_keys || o.json_keys.count("encrypt"))
1554 1571 {
1555   - do_json_encrypt(pdf, o, j);
  1572 + doJSONEncrypt(pdf, j);
1556 1573 }
1557 1574 if (all_keys || o.json_keys.count("attachments"))
1558 1575 {
1559   - do_json_attachments(pdf, o, j);
  1576 + doJSONAttachments(pdf, j);
1560 1577 }
1561 1578  
1562 1579 // Check against schema
... ... @@ -1645,7 +1662,7 @@ QPDFJob::doInspection(QPDF&amp; pdf)
1645 1662 }
1646 1663 if (! o.attachment_to_show.empty())
1647 1664 {
1648   - do_show_attachment(pdf, o);
  1665 + doShowAttachment(pdf);
1649 1666 }
1650 1667 if (! pdf.getWarnings().empty())
1651 1668 {
... ... @@ -1830,12 +1847,13 @@ ImageOptimizer::provideStreamData(int, int, Pipeline* pipeline)
1830 1847 false, false);
1831 1848 }
1832 1849  
1833   -static PointerHolder<QPDF> do_process_once(
  1850 +PointerHolder<QPDF>
  1851 +QPDFJob::doProcessOnce(
1834 1852 std::function<void(QPDF*, char const*)> fn,
1835   - char const* password, QPDFJob& o, bool empty)
  1853 + char const* password, bool empty)
1836 1854 {
1837 1855 PointerHolder<QPDF> pdf = new QPDF;
1838   - set_qpdf_options(*pdf, o);
  1856 + setQPDFOptions(*pdf);
1839 1857 if (empty)
1840 1858 {
1841 1859 pdf->emptyPDF();
... ... @@ -1847,10 +1865,12 @@ static PointerHolder&lt;QPDF&gt; do_process_once(
1847 1865 return pdf;
1848 1866 }
1849 1867  
1850   -static PointerHolder<QPDF> do_process(
  1868 +PointerHolder<QPDF>
  1869 +QPDFJob::doProcess(
1851 1870 std::function<void(QPDF*, char const*)> fn,
1852   - char const* password, QPDFJob& o, bool empty)
  1871 + char const* password, bool empty)
1853 1872 {
  1873 + QPDFJob& o = *this; // QXXXQ
1854 1874 // If a password has been specified but doesn't work, try other
1855 1875 // passwords that are equivalent in different character encodings.
1856 1876 // This makes it possible to open PDF files that were encrypted
... ... @@ -1878,7 +1898,7 @@ static PointerHolder&lt;QPDF&gt; do_process(
1878 1898 {
1879 1899 // There is no password, or we're not doing recovery, so just
1880 1900 // do the normal processing with the supplied password.
1881   - return do_process_once(fn, password, o, empty);
  1901 + return doProcessOnce(fn, password, empty);
1882 1902 }
1883 1903  
1884 1904 // Get a list of otherwise encoded strings. Keep in scope for this
... ... @@ -1912,7 +1932,7 @@ static PointerHolder&lt;QPDF&gt; do_process(
1912 1932 {
1913 1933 try
1914 1934 {
1915   - return do_process_once(fn, *iter, o, empty);
  1935 + return doProcessOnce(fn, *iter, empty);
1916 1936 }
1917 1937 catch (QPDFExc& e)
1918 1938 {
... ... @@ -1941,20 +1961,20 @@ static PointerHolder&lt;QPDF&gt; do_process(
1941 1961 PointerHolder<QPDF>
1942 1962 QPDFJob::processFile(char const* filename, char const* password)
1943 1963 {
1944   - QPDFJob& o = *this; // QXXXQ
1945 1964 auto f1 = std::mem_fn<void(char const*, char const*)>(&QPDF::processFile);
1946 1965 auto fn = std::bind(
1947 1966 f1, std::placeholders::_1, filename, std::placeholders::_2);
1948   - return do_process(fn, password, o, strcmp(filename, "") == 0);
  1967 + return doProcess(fn, password, strcmp(filename, "") == 0);
1949 1968 }
1950 1969  
1951   -static PointerHolder<QPDF> process_input_source(
1952   - PointerHolder<InputSource> is, char const* password, QPDFJob& o)
  1970 +PointerHolder<QPDF>
  1971 +QPDFJob::processInputSource(
  1972 + PointerHolder<InputSource> is, char const* password)
1953 1973 {
1954 1974 auto f1 = std::mem_fn(&QPDF::processInputSource);
1955 1975 auto fn = std::bind(
1956 1976 f1, std::placeholders::_1, is, std::placeholders::_2);
1957   - return do_process(fn, password, o, false);
  1977 + return doProcess(fn, password, false);
1958 1978 }
1959 1979  
1960 1980 void
... ... @@ -2035,9 +2055,9 @@ static QPDFAcroFormDocumentHelper* get_afdh_for_qpdf(
2035 2055 return afdh_map[uid].getPointer();
2036 2056 }
2037 2057  
2038   -static void do_under_overlay_for_page(
  2058 +void
  2059 +QPDFJob::doUnderOverlayForPage(
2039 2060 QPDF& pdf,
2040   - QPDFJob& o,
2041 2061 QPDFJob::UnderOverlay& uo,
2042 2062 std::map<int, std::vector<int> >& pagenos,
2043 2063 size_t page_idx,
... ... @@ -2046,6 +2066,7 @@ static void do_under_overlay_for_page(
2046 2066 QPDFPageObjectHelper& dest_page,
2047 2067 bool before)
2048 2068 {
  2069 + QPDFJob& o = *this; // QXXXQ
2049 2070 int pageno = 1 + QIntC::to_int(page_idx);
2050 2071 if (! pagenos.count(pageno))
2051 2072 {
... ... @@ -2164,12 +2185,12 @@ QPDFJob::handleUnderOverlay(QPDF&amp; pdf)
2164 2185 o.doIfVerbose([&](std::ostream& cout, std::string const& prefix) {
2165 2186 cout << " page " << 1+i << std::endl;
2166 2187 });
2167   - do_under_overlay_for_page(pdf, o, o.underlay, underlay_pagenos, i,
2168   - underlay_fo, upages, main_pages.at(i),
2169   - true);
2170   - do_under_overlay_for_page(pdf, o, o.overlay, overlay_pagenos, i,
2171   - overlay_fo, opages, main_pages.at(i),
2172   - false);
  2188 + doUnderOverlayForPage(pdf, o.underlay, underlay_pagenos, i,
  2189 + underlay_fo, upages, main_pages.at(i),
  2190 + true);
  2191 + doUnderOverlayForPage(pdf, o.overlay, overlay_pagenos, i,
  2192 + overlay_fo, opages, main_pages.at(i),
  2193 + false);
2173 2194 }
2174 2195 }
2175 2196  
... ... @@ -2419,8 +2440,10 @@ QPDFJob::handleTransformations(QPDF&amp; pdf)
2419 2440 }
2420 2441 }
2421 2442  
2422   -static bool should_remove_unreferenced_resources(QPDF& pdf, QPDFJob& o)
  2443 +bool
  2444 +QPDFJob::shouldRemoveUnreferencedResources(QPDF& pdf)
2423 2445 {
  2446 + QPDFJob& o = *this; // QXXXQ
2424 2447 if (o.remove_unreferenced_page_resources == QPDFJob::re_no)
2425 2448 {
2426 2449 return false;
... ... @@ -2570,10 +2593,12 @@ static QPDFObjectHandle added_page(QPDF&amp; pdf, QPDFPageObjectHelper page)
2570 2593 return added_page(pdf, page.getObjectHandle());
2571 2594 }
2572 2595  
2573   -static void handle_page_specs(
2574   - QPDF& pdf, QPDFJob& o, bool& warnings,
  2596 +void
  2597 +QPDFJob::handlePageSpecs(
  2598 + QPDF& pdf, bool& warnings,
2575 2599 std::vector<PointerHolder<QPDF>>& page_heap)
2576 2600 {
  2601 + QPDFJob& o = *this; // QXXXQ
2577 2602 // Parse all page specifications and translate them into lists of
2578 2603 // actual pages.
2579 2604  
... ... @@ -2668,7 +2693,7 @@ static void handle_page_specs(
2668 2693 is = fis;
2669 2694 fis->setFilename(page_spec.filename.c_str());
2670 2695 }
2671   - PointerHolder<QPDF> qpdf_ph = process_input_source(is, password, o);
  2696 + PointerHolder<QPDF> qpdf_ph = processInputSource(is, password);
2672 2697 page_heap.push_back(qpdf_ph);
2673 2698 page_spec_qpdfs[page_spec.filename] = qpdf_ph.getPointer();
2674 2699 if (cis)
... ... @@ -2705,7 +2730,7 @@ static void handle_page_specs(
2705 2730 if (remove_unreferenced.count(other_uuid) == 0)
2706 2731 {
2707 2732 remove_unreferenced[other_uuid] =
2708   - should_remove_unreferenced_resources(other, o);
  2733 + shouldRemoveUnreferencedResources(other);
2709 2734 }
2710 2735 if (cis)
2711 2736 {
... ... @@ -2946,8 +2971,10 @@ static void handle_page_specs(
2946 2971 }
2947 2972 }
2948 2973  
2949   -static void handle_rotations(QPDF& pdf, QPDFJob& o)
  2974 +void
  2975 +QPDFJob::handleRotations(QPDF& pdf)
2950 2976 {
  2977 + QPDFJob& o = *this; // QXXXQ
2951 2978 QPDFPageDocumentHelper dh(pdf);
2952 2979 std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
2953 2980 int npages = QIntC::to_int(pages.size());
... ... @@ -3314,7 +3341,7 @@ QPDFJob::doSplitPages(QPDF&amp; pdf, bool&amp; warnings)
3314 3341 before = std::string(o.outfilename) + "-";
3315 3342 }
3316 3343  
3317   - if (should_remove_unreferenced_resources(pdf, o))
  3344 + if (shouldRemoveUnreferencedResources(pdf))
3318 3345 {
3319 3346 QPDFPageDocumentHelper dh(pdf);
3320 3347 dh.removeUnreferencedResources();
... ... @@ -3515,11 +3542,11 @@ QPDFJob::run()
3515 3542 std::vector<PointerHolder<QPDF>> page_heap;
3516 3543 if (! o.page_specs.empty())
3517 3544 {
3518   - handle_page_specs(pdf, o, other_warnings, page_heap);
  3545 + handlePageSpecs(pdf, other_warnings, page_heap);
3519 3546 }
3520 3547 if (! o.rotations.empty())
3521 3548 {
3522   - handle_rotations(pdf, o);
  3549 + handleRotations(pdf);
3523 3550 }
3524 3551 handleUnderOverlay(pdf);
3525 3552 handleTransformations(pdf);
... ...