From 12f1eb15ca3fed6310402847559a7c99d3c77847 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 2 Apr 2022 17:14:10 -0400 Subject: Programmatically apply new formatting to code Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done --- examples/pdf-attach-file.cc | 134 +++++++++++------------ examples/pdf-bookmarks.cc | 166 +++++++++++------------------ examples/pdf-c-objects.c | 53 ++++------ examples/pdf-count-strings.cc | 38 +++---- examples/pdf-create.cc | 223 +++++++++++++++++---------------------- examples/pdf-custom-filter.cc | 158 +++++++++++---------------- examples/pdf-double-page-size.cc | 56 +++++----- examples/pdf-filter-tokens.cc | 72 ++++++------- examples/pdf-invert-images.cc | 71 +++++-------- examples/pdf-linearize.c | 49 ++++----- examples/pdf-mod-info.cc | 149 ++++++++++---------------- examples/pdf-name-number-tree.cc | 78 ++++++-------- examples/pdf-npages.cc | 21 ++-- examples/pdf-overlay-page.cc | 47 ++++----- examples/pdf-parse-content.cc | 36 +++---- examples/pdf-set-form-values.cc | 38 +++---- examples/pdf-split-pages.cc | 35 +++--- examples/qpdf-job.cc | 50 ++++----- examples/qpdfjob-c.c | 20 ++-- 19 files changed, 619 insertions(+), 875 deletions(-) (limited to 'examples') diff --git a/examples/pdf-attach-file.cc b/examples/pdf-attach-file.cc index 2cc30262..964d247d 100644 --- a/examples/pdf-attach-file.cc +++ b/examples/pdf-attach-file.cc @@ -1,11 +1,11 @@ #include -#include -#include #include #include +#include +#include -#include #include +#include // // This example attaches a file to an input file, adds a page to the @@ -17,9 +17,11 @@ static char const* whoami = 0; -static void usage(std::string const& msg) +static void +usage(std::string const& msg) { - std::cerr << msg << std::endl << std::endl + std::cerr << msg << std::endl + << std::endl << "Usage: " << whoami << " options" << std::endl << "Options:" << std::endl << " --infile infile.pdf" << std::endl @@ -30,33 +32,36 @@ static void usage(std::string const& msg) exit(2); } -static void process(char const* infilename, char const* password, - char const* attachment, char const* mimetype, - char const* outfilename) +static void +process( + char const* infilename, + char const* password, + char const* attachment, + char const* mimetype, + char const* outfilename) { QPDF q; q.processFile(infilename, password); // Create an indirect object for the built-in Helvetica font. This // uses the qpdf literal syntax introduced in qpdf 10.6. - auto f1 = q.makeIndirectObject( - "<<" - " /Type /Font" - " /Subtype /Type1" - " /Name /F1" - " /BaseFont /Helvetica" - " /Encoding /WinAnsiEncoding" - ">>"_qpdf); + auto f1 = q.makeIndirectObject("<<" + " /Type /Font" + " /Subtype /Type1" + " /Name /F1" + " /BaseFont /Helvetica" + " /Encoding /WinAnsiEncoding" + ">>"_qpdf); // Create a resources dictionary with fonts. This uses the new // parse introduced in qpdf 10.2 that takes a QPDF* and allows // indirect object references. - auto resources = q.makeIndirectObject( - QPDFObjectHandle::parse( - &q, - "<<" - " /Font <<" - " /F1 " + f1.unparse() + + auto resources = q.makeIndirectObject(QPDFObjectHandle::parse( + &q, + "<<" + " /Font <<" + " /F1 " + + f1.unparse() + " >>" ">>")); @@ -66,8 +71,7 @@ static void process(char const* infilename, char const* password, << std::endl; auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment); - if (mimetype) - { + if (mimetype) { // Get an embedded file stream and set mimetype auto ef = QPDFEFStreamObjectHelper(fs.getEmbeddedFileStream()); ef.setSubtype(mimetype); @@ -102,17 +106,16 @@ static void process(char const* infilename, char const* password, apdict.replaceKey("/Type", "/XObject"_qpdf); apdict.replaceKey("/Subtype", "/Form"_qpdf); apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf); - auto annot = q.makeIndirectObject( - QPDFObjectHandle::parse( - &q, - "<<" - " /AP <<" - " /N " + ap.unparse() + + auto annot = q.makeIndirectObject(QPDFObjectHandle::parse( + &q, + "<<" + " /AP <<" + " /N " + + ap.unparse() + " >>" - " /Contents " - + QPDFObjectHandle::newUnicodeString(attachment).unparse() + - " /FS " + fs.getObjectHandle().unparse() + - " /NM " + + " /Contents " + + QPDFObjectHandle::newUnicodeString(attachment).unparse() + + " /FS " + fs.getObjectHandle().unparse() + " /NM " + QPDFObjectHandle::newUnicodeString(attachment).unparse() + " /Rect [ 72 700 92 720 ]" " /Subtype /FileAttachment" @@ -134,12 +137,16 @@ static void process(char const* infilename, char const* password, auto page = QPDFObjectHandle::parse( &q, "<<" - " /Annots [ " + annot.unparse() + " ]" - " /Contents " + contents.unparse() + - " /MediaBox [0 0 612 792]" - " /Resources " + resources.unparse() + - " /Type /Page" - ">>"); + " /Annots [ " + + annot.unparse() + + " ]" + " /Contents " + + contents.unparse() + + " /MediaBox [0 0 612 792]" + " /Resources " + + resources.unparse() + + " /Type /Page" + ">>"); // Add the page. q.addPage(page, true); @@ -151,7 +158,8 @@ static void process(char const* infilename, char const* password, w.write(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); @@ -162,64 +170,46 @@ int main(int argc, char* argv[]) char const* mimetype = 0; auto check_arg = [](char const* arg, std::string const& msg) { - if (arg == nullptr) - { + if (arg == nullptr) { usage(msg); } }; - for (int i = 1; i < argc; ++i) - { + for (int i = 1; i < argc; ++i) { char* arg = argv[i]; - char* next = argv[i+1]; - if (strcmp(arg, "--infile") == 0) - { + char* next = argv[i + 1]; + if (strcmp(arg, "--infile") == 0) { check_arg(next, "--infile takes an argument"); infilename = next; ++i; - } - else if (strcmp(arg, "--password") == 0) - { + } else if (strcmp(arg, "--password") == 0) { check_arg(next, "--password takes an argument"); password = next; ++i; - } - else if (strcmp(arg, "--attachment") == 0) - { + } else if (strcmp(arg, "--attachment") == 0) { check_arg(next, "--attachment takes an argument"); attachment = next; ++i; - } - else if (strcmp(arg, "--outfile") == 0) - { + } else if (strcmp(arg, "--outfile") == 0) { check_arg(next, "--outfile takes an argument"); outfilename = next; ++i; - } - else if (strcmp(arg, "--mimetype") == 0) - { + } else if (strcmp(arg, "--mimetype") == 0) { check_arg(next, "--mimetype takes an argument"); mimetype = next; ++i; - } - else - { + } else { usage("unknown argument " + std::string(arg)); } } - if (! (infilename && attachment && outfilename)) - { + if (!(infilename && attachment && outfilename)) { usage("required arguments were not provided"); } - try - { + try { process(infilename, password, attachment, mimetype, outfilename); - } - catch (std::exception &e) - { - std::cerr << whoami << " exception: " - << e.what() << std::endl; + } catch (std::exception& e) { + std::cerr << whoami << " exception: " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-bookmarks.cc b/examples/pdf-bookmarks.cc index e049e423..de0af8e6 100644 --- a/examples/pdf-bookmarks.cc +++ b/examples/pdf-bookmarks.cc @@ -1,12 +1,12 @@ -#include -#include -#include +#include #include -#include #include -#include -#include +#include #include +#include +#include +#include +#include // This program demonstrates extraction of bookmarks using the qpdf // outlines API. Note that all the information shown by this program @@ -20,67 +20,62 @@ static bool show_open = false; static bool show_targets = false; static std::map page_map; -void usage() +void +usage() { - std::cerr << "Usage: " << whoami << " [options] file.pdf [password]" - << std::endl - << "Options:" << std::endl - << " --numbers give bookmarks outline-style numbers" - << std::endl - << " --lines draw lines to show bookmark hierarchy" - << std::endl - << " --show-open indicate whether a bookmark is initially open" - << std::endl - << " --show-targets show target if possible" - << std::endl; + std::cerr + << "Usage: " << whoami << " [options] file.pdf [password]" << std::endl + << "Options:" << std::endl + << " --numbers give bookmarks outline-style numbers" + << std::endl + << " --lines draw lines to show bookmark hierarchy" + << std::endl + << " --show-open indicate whether a bookmark is initially open" + << std::endl + << " --show-targets show target if possible" << std::endl; exit(2); } -void print_lines(std::vector& numbers) +void +print_lines(std::vector& numbers) { - for (unsigned int i = 0; i < numbers.size() - 1; ++i) - { - if (numbers.at(i)) - { + for (unsigned int i = 0; i < numbers.size() - 1; ++i) { + if (numbers.at(i)) { std::cout << "| "; - } - else - { + } else { std::cout << " "; } } } -void generate_page_map(QPDF& qpdf) +void +generate_page_map(QPDF& qpdf) { QPDFPageDocumentHelper dh(qpdf); int n = 0; - for (auto const& page : dh.getAllPages()) - { + for (auto const& page : dh.getAllPages()) { page_map[page.getObjectHandle().getObjGen()] = ++n; } } -void show_bookmark_details(QPDFOutlineObjectHelper outline, - std::vector numbers) +void +show_bookmark_details(QPDFOutlineObjectHelper outline, std::vector numbers) { // No default so gcc will warn on missing tag - switch (style) - { - case st_none: + switch (style) { + case st_none: QTC::TC("examples", "pdf-bookmarks none"); break; - case st_numbers: + case st_numbers: QTC::TC("examples", "pdf-bookmarks numbers"); - for (auto const& number : numbers) - { + for (auto const& number : numbers) { std::cout << number << "."; } std::cout << " "; break; - case st_lines: + case st_lines: QTC::TC("examples", "pdf-bookmarks lines"); print_lines(numbers); std::cout << "|" << std::endl; @@ -89,42 +84,32 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, break; } - if (show_open) - { + if (show_open) { int count = outline.getCount(); - if (count) - { + if (count) { QTC::TC("examples", "pdf-bookmarks has count"); - if (count > 0) - { + if (count > 0) { // hierarchy is open at this point QTC::TC("examples", "pdf-bookmarks open"); std::cout << "(v) "; - } - else - { + } else { QTC::TC("examples", "pdf-bookmarks closed"); std::cout << "(>) "; } - } - else - { + } else { QTC::TC("examples", "pdf-bookmarks no count"); std::cout << "( ) "; } } - if (show_targets) - { + if (show_targets) { QTC::TC("examples", "pdf-bookmarks targets"); std::string target = "unknown"; QPDFObjectHandle dest_page = outline.getDestPage(); - if (! dest_page.isNull()) - { + if (!dest_page.isNull()) { QTC::TC("examples", "pdf-bookmarks dest"); QPDFObjGen og = dest_page.getObjGen(); - if (page_map.count(og)) - { + if (page_map.count(og)) { target = QUtil::int_to_string(page_map[og]); } } @@ -134,8 +119,9 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, std::cout << outline.getTitle() << std::endl; } -void extract_bookmarks(std::vector outlines, - std::vector& numbers) +void +extract_bookmarks( + std::vector outlines, std::vector& numbers) { // For style == st_numbers, numbers.at(n) contains the numerical // label for the outline, so we count up from 1. @@ -144,8 +130,7 @@ void extract_bookmarks(std::vector outlines, // is, so we count up to zero. numbers.push_back( (style == st_lines) ? -QIntC::to_int(outlines.size()) : 0); - for (auto& outline : outlines) - { + for (auto& outline : outlines) { ++(numbers.back()); show_bookmark_details(outline, numbers); extract_bookmarks(outline.getKids(), numbers); @@ -153,87 +138,64 @@ void extract_bookmarks(std::vector outlines, numbers.pop_back(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) - { + if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) { std::cout << whoami << " version 1.5" << std::endl; exit(0); } int arg; - for (arg = 1; arg < argc; ++arg) - { - if (argv[arg][0] == '-') - { - if (strcmp(argv[arg], "--numbers") == 0) - { + for (arg = 1; arg < argc; ++arg) { + if (argv[arg][0] == '-') { + if (strcmp(argv[arg], "--numbers") == 0) { style = st_numbers; - } - else if (strcmp(argv[arg], "--lines") == 0) - { + } else if (strcmp(argv[arg], "--lines") == 0) { style = st_lines; - } - else if (strcmp(argv[arg], "--show-open") == 0) - { + } else if (strcmp(argv[arg], "--show-open") == 0) { show_open = true; - } - else if (strcmp(argv[arg], "--show-targets") == 0) - { + } else if (strcmp(argv[arg], "--show-targets") == 0) { show_targets = true; - } - else - { + } else { usage(); } - } - else - { + } else { break; } } - if (arg >= argc) - { + if (arg >= argc) { usage(); } char const* filename = argv[arg++]; char const* password = ""; - if (arg < argc) - { + if (arg < argc) { password = argv[arg++]; } - if (arg != argc) - { + if (arg != argc) { usage(); } - try - { + try { QPDF qpdf; qpdf.processFile(filename, password); QPDFOutlineDocumentHelper odh(qpdf); - if (odh.hasOutlines()) - { + if (odh.hasOutlines()) { std::vector numbers; - if (show_targets) - { + if (show_targets) { generate_page_map(qpdf); } extract_bookmarks(odh.getTopLevelOutlines(), numbers); - } - else - { + } else { std::cout << filename << " has no bookmarks" << std::endl; } - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << " processing file " << filename << ": " << e.what() << std::endl; exit(2); diff --git a/examples/pdf-c-objects.c b/examples/pdf-c-objects.c index c616997a..78a4a746 100644 --- a/examples/pdf-c-objects.c +++ b/examples/pdf-c-objects.c @@ -10,7 +10,8 @@ static char const* whoami = 0; -static void usage() +static void +usage() { fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami); exit(2); @@ -33,12 +34,9 @@ modify_file(qpdf_data qpdf) /* 0 is never a valid qpdf_oh */ qpdf_oh pagemode = 0; if (qpdf_oh_is_dictionary( - qpdf, qpdf_oh_get_key(qpdf, root, "/PageLabels"))) - { + qpdf, qpdf_oh_get_key(qpdf, root, "/PageLabels"))) { pagemode = qpdf_oh_new_name(qpdf, "/UseOutlines"); - } - else - { + } else { pagemode = qpdf_oh_new_null(qpdf); } qpdf_oh_replace_or_remove_key(qpdf, root, "/PageMode", pagemode); @@ -46,7 +44,8 @@ modify_file(qpdf_data qpdf) return QPDF_TRUE; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { char* infile = NULL; char* password = NULL; @@ -56,21 +55,15 @@ int main(int argc, char* argv[]) int errors = 0; char* p = 0; - if ((p = strrchr(argv[0], '/')) != NULL) - { + if ((p = strrchr(argv[0], '/')) != NULL) { whoami = p + 1; - } - else if ((p = strrchr(argv[0], '\\')) != NULL) - { + } else if ((p = strrchr(argv[0], '\\')) != NULL) { whoami = p + 1; - } - else - { + } else { whoami = argv[0]; } - if (argc != 4) - { + if (argc != 4) { usage(); } @@ -80,33 +73,29 @@ int main(int argc, char* argv[]) if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) && modify_file(qpdf) && - ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) - { + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) { /* Use static ID for testing only. For production, a * non-static ID is used. See also * qpdf_set_deterministic_ID. */ qpdf_set_static_ID(qpdf, QPDF_TRUE); /* for testing only */ qpdf_write(qpdf); } - while (qpdf_more_warnings(qpdf)) - { + while (qpdf_more_warnings(qpdf)) { warnings = 1; - printf("warning: %s\n", - qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); + printf( + "warning: %s\n", + qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); } - if (qpdf_has_error(qpdf)) - { + if (qpdf_has_error(qpdf)) { errors = 1; - printf("error: %s\n", - qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); + printf( + "error: %s\n", + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); } qpdf_cleanup(&qpdf); - if (errors) - { + if (errors) { return 2; - } - else if (warnings) - { + } else if (warnings) { return 3; } diff --git a/examples/pdf-count-strings.cc b/examples/pdf-count-strings.cc index 4a1a8e4e..a52b7a37 100644 --- a/examples/pdf-count-strings.cc +++ b/examples/pdf-count-strings.cc @@ -5,23 +5,23 @@ // #include -#include #include +#include +#include #include +#include #include #include #include -#include -#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " infile" << std::endl - << "Applies token filters to infile" - << std::endl; + << "Applies token filters to infile" << std::endl; exit(2); } @@ -47,8 +47,7 @@ void StringCounter::handleToken(QPDFTokenizer::Token const& token) { // Count string tokens - if (token.getType() == QPDFTokenizer::tt_string) - { + if (token.getType() == QPDFTokenizer::tt_string) { ++this->count; } // Preserve input verbatim by passing each token to any specified @@ -71,36 +70,31 @@ StringCounter::getCount() const return this->count; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 2) - { + if (argc != 2) { usage(); } char const* infilename = argv[1]; - try - { + try { QPDF pdf; pdf.processFile(infilename); int pageno = 0; - for (auto& page : QPDFPageDocumentHelper(pdf).getAllPages()) - { + for (auto& page : QPDFPageDocumentHelper(pdf).getAllPages()) { ++pageno; // Pass the contents of a page through our string counter. // If it's an even page, capture the output. This // illustrates that you may capture any output generated // by the filter, or you may ignore it. StringCounter counter; - if (pageno % 2) - { + if (pageno % 2) { // Ignore output for odd pages. page.filterContents(&counter); - } - else - { + } else { // Write output to stdout for even pages. Pl_StdioFile out("stdout", stdout); std::cout << "% Contents of page " << pageno << std::endl; @@ -110,9 +104,7 @@ int main(int argc, char* argv[]) std::cout << "Page " << pageno << ": strings = " << counter.getCount() << std::endl; } - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << whoami << ": " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-create.cc b/examples/pdf-create.cc index b5d87618..5274d0a2 100644 --- a/examples/pdf-create.cc +++ b/examples/pdf-create.cc @@ -5,20 +5,20 @@ // StreamDataProvider with different types of filters. // +#include +#include +#include +#include #include +#include #include #include #include -#include #include -#include -#include -#include -#include #include #include -#include #include +#include static char const* whoami = 0; @@ -27,11 +27,10 @@ static char const* whoami = 0; class ImageProvider: public QPDFObjectHandle::StreamDataProvider { public: - ImageProvider(std::string const& color_space, - std::string const& filter); + ImageProvider(std::string const& color_space, std::string const& filter); virtual ~ImageProvider(); - virtual void provideStreamData(int objid, int generation, - Pipeline* pipeline); + virtual void + provideStreamData(int objid, int generation, Pipeline* pipeline); size_t getWidth() const; size_t getHeight() const; @@ -45,8 +44,8 @@ class ImageProvider: public QPDFObjectHandle::StreamDataProvider J_COLOR_SPACE j_color_space; }; -ImageProvider::ImageProvider(std::string const& color_space, - std::string const& filter) : +ImageProvider::ImageProvider( + std::string const& color_space, std::string const& filter) : width(400), stripe_height(80), color_space(color_space), @@ -54,8 +53,7 @@ ImageProvider::ImageProvider(std::string const& color_space, n_stripes(6), j_color_space(JCS_UNKNOWN) { - if (color_space == "/DeviceCMYK") - { + if (color_space == "/DeviceCMYK") { j_color_space = JCS_CMYK; stripes.push_back(std::string("\xff\x00\x00\x00", 4)); stripes.push_back(std::string("\x00\xff\x00\x00", 4)); @@ -63,9 +61,7 @@ ImageProvider::ImageProvider(std::string const& color_space, stripes.push_back(std::string("\xff\x00\xff\x00", 4)); stripes.push_back(std::string("\xff\xff\x00\x00", 4)); stripes.push_back(std::string("\x00\x00\x00\xff", 4)); - } - else if (color_space == "/DeviceRGB") - { + } else if (color_space == "/DeviceRGB") { j_color_space = JCS_RGB; stripes.push_back(std::string("\xff\x00\x00", 3)); stripes.push_back(std::string("\x00\xff\x00", 3)); @@ -73,9 +69,7 @@ ImageProvider::ImageProvider(std::string const& color_space, stripes.push_back(std::string("\xff\x00\xff", 3)); stripes.push_back(std::string("\xff\xff\x00", 3)); stripes.push_back(std::string("\x00\x00\x00", 3)); - } - else if (color_space == "/DeviceGray") - { + } else if (color_space == "/DeviceGray") { j_color_space = JCS_GRAYSCALE; stripes.push_back(std::string("\xee", 1)); stripes.push_back(std::string("\xcc", 1)); @@ -103,34 +97,31 @@ ImageProvider::getHeight() const } void -ImageProvider::provideStreamData(int objid, int generation, - Pipeline* pipeline) +ImageProvider::provideStreamData(int objid, int generation, Pipeline* pipeline) { std::vector> to_delete; Pipeline* p = pipeline; std::shared_ptr p_new; - if (filter == "/DCTDecode") - { + if (filter == "/DCTDecode") { p_new = std::make_shared( - "image encoder", pipeline, - QIntC::to_uint(width), QIntC::to_uint(getHeight()), - QIntC::to_int(stripes[0].length()), j_color_space); + "image encoder", + pipeline, + QIntC::to_uint(width), + QIntC::to_uint(getHeight()), + QIntC::to_int(stripes[0].length()), + j_color_space); to_delete.push_back(p_new); p = p_new.get(); - } - else if (filter == "/RunLengthDecode") - { + } else if (filter == "/RunLengthDecode") { p_new = std::make_shared( "image encoder", pipeline, Pl_RunLength::a_encode); to_delete.push_back(p_new); p = p_new.get(); } - for (size_t i = 0; i < n_stripes; ++i) - { - for (size_t j = 0; j < width * stripe_height; ++j) - { + for (size_t i = 0; i < n_stripes; ++i) { + for (size_t j = 0; j < width * stripe_height; ++j) { p->write( QUtil::unsigned_char_pointer(stripes[i].c_str()), stripes[i].length()); @@ -139,36 +130,43 @@ ImageProvider::provideStreamData(int objid, int generation, p->finish(); } -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " filename" << std::endl << "Creates a simple PDF and writes it to filename" << std::endl; exit(2); } -static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) +static QPDFObjectHandle +createPageContents(QPDF& pdf, std::string const& text) { // Create a stream that displays our image and the given text in // our font. - std::string contents = - "BT /F1 24 Tf 72 320 Td (" + text + ") Tj ET\n" + std::string contents = "BT /F1 24 Tf 72 320 Td (" + text + + ") Tj ET\n" "q 244 0 0 144 184 100 cm /Im1 Do Q\n"; return QPDFObjectHandle::newStream(&pdf, contents); } -QPDFObjectHandle newName(std::string const& name) +QPDFObjectHandle +newName(std::string const& name) { return QPDFObjectHandle::newName(name); } -QPDFObjectHandle newInteger(size_t val) +QPDFObjectHandle +newInteger(size_t val) { return QPDFObjectHandle::newInteger(QIntC::to_int(val)); } -void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font, - std::string const& color_space, - std::string const& filter) +void +add_page( + QPDFPageDocumentHelper& dh, + QPDFObjectHandle font, + std::string const& color_space, + std::string const& filter) { QPDF& pdf(dh.getQPDF()); @@ -193,9 +191,8 @@ void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font, image.replaceDict(image_dict); // Provide the stream data. - image.replaceStreamData(provider, - QPDFObjectHandle::parse(filter), - QPDFObjectHandle::newNull()); + image.replaceStreamData( + provider, QPDFObjectHandle::parse(filter), QPDFObjectHandle::newNull()); // Create direct objects as needed by the page dictionary. QPDFObjectHandle procset = "[/PDF /Text /ImageC]"_qpdf; @@ -212,15 +209,14 @@ void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font, resources.replaceKey("/XObject", xobject); // Create the page content stream - QPDFObjectHandle contents = createPageContents( - pdf, color_space + " with filter " + filter); + QPDFObjectHandle contents = + createPageContents(pdf, color_space + " with filter " + filter); // Create the page dictionary - QPDFObjectHandle page = pdf.makeIndirectObject( - "<<" - " /Type /Page" - " /MediaBox [0 0 612 392]" - ">>"_qpdf); + QPDFObjectHandle page = pdf.makeIndirectObject("<<" + " /Type /Page" + " /MediaBox [0 0 612 392]" + ">>"_qpdf); page.replaceKey("/Contents", contents); page.replaceKey("/Resources", resources); @@ -228,9 +224,11 @@ void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font, dh.addPage(page, false); } -static void check(char const* filename, - std::vector const& color_spaces, - std::vector const& filters) +static void +check( + char const* filename, + std::vector const& color_spaces, + std::vector const& filters) { // Each stream is compressed the way it is supposed to be. We will // add additional tests in qpdf.test to exercise QPDFWriter more @@ -253,17 +251,14 @@ static void check(char const* filename, QPDF pdf; pdf.processFile(filename); auto pages = QPDFPageDocumentHelper(pdf).getAllPages(); - if (n_color_spaces * n_filters != pages.size()) - { + if (n_color_spaces * n_filters != pages.size()) { throw std::logic_error("incorrect number of pages"); } size_t pageno = 1; bool errors = false; - for (auto& page : pages) - { + for (auto& page : pages) { auto images = page.getImages(); - if (images.size() != 1) - { + if (images.size() != 1) { throw std::logic_error("incorrect number of images on page"); } @@ -273,8 +268,7 @@ static void check(char const* filename, std::string desired_filter = filters[(pageno - 1) % n_filters]; // In the default mode, QPDFWriter will compress with // /FlateDecode if no filters are provided. - if (desired_filter == "null") - { + if (desired_filter == "null") { desired_filter = "/FlateDecode"; } QPDFObjectHandle image = images.begin()->second; @@ -282,40 +276,34 @@ static void check(char const* filename, QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace"); QPDFObjectHandle filter = image_dict.getKey("/Filter"); bool this_errors = false; - if (! filter.isNameAndEquals(desired_filter)) - { + if (!filter.isNameAndEquals(desired_filter)) { this_errors = errors = true; std::cout << "page " << pageno << ": expected filter " - << desired_filter << "; actual filter = " - << filter.unparse() << std::endl; + << desired_filter + << "; actual filter = " << filter.unparse() << std::endl; } - if (! color_space.isNameAndEquals(desired_color_space)) - { + if (!color_space.isNameAndEquals(desired_color_space)) { this_errors = errors = true; std::cout << "page " << pageno << ": expected color space " - << desired_color_space << "; actual color space = " - << color_space.unparse() << std::endl; + << desired_color_space + << "; actual color space = " << color_space.unparse() + << std::endl; } - if (! this_errors) - { + if (!this_errors) { // Check image data - auto actual_data = - image.getStreamData(qpdf_dl_all); + auto actual_data = image.getStreamData(qpdf_dl_all); ImageProvider* p = new ImageProvider(desired_color_space, "null"); PointerHolder provider(p); Pl_Buffer b_p("get image data"); provider->provideStreamData(0, 0, &b_p); PointerHolder desired_data(b_p.getBuffer()); - if (desired_data->getSize() != actual_data->getSize()) - { - std::cout << "page " << pageno - << ": image data length mismatch" << std::endl; + if (desired_data->getSize() != actual_data->getSize()) { + std::cout << "page " << pageno << ": image data length mismatch" + << std::endl; this_errors = errors = true; - } - else - { + } else { // Compare bytes. For JPEG, allow a certain number of // the bytes to be off desired by more than a given // tolerance. Any of the samples may be a little off @@ -326,25 +314,20 @@ static void check(char const* filename, unsigned char const* desired_bytes = desired_data->getBuffer(); size_t len = actual_data->getSize(); unsigned int mismatches = 0; - int tolerance = ( - desired_filter == "/DCTDecode" ? 10 : 0); - size_t threshold = ( - desired_filter == "/DCTDecode" ? len / 40U : 0); - for (size_t i = 0; i < len; ++i) - { + int tolerance = (desired_filter == "/DCTDecode" ? 10 : 0); + size_t threshold = + (desired_filter == "/DCTDecode" ? len / 40U : 0); + for (size_t i = 0; i < len; ++i) { int delta = actual_bytes[i] - desired_bytes[i]; - if ((delta > tolerance) || (delta < -tolerance)) - { + if ((delta > tolerance) || (delta < -tolerance)) { ++mismatches; } } - if (mismatches > threshold) - { - std::cout << "page " << pageno - << ": " << desired_color_space << ", " - << desired_filter - << ": mismatches: " << mismatches - << " of " << len << std::endl; + if (mismatches > threshold) { + std::cout << "page " << pageno << ": " + << desired_color_space << ", " << desired_filter + << ": mismatches: " << mismatches << " of " << len + << std::endl; this_errors = errors = true; } } @@ -352,17 +335,15 @@ static void check(char const* filename, ++pageno; } - if (errors) - { + if (errors) { throw std::logic_error("errors found"); - } - else - { + } else { std::cout << "all checks passed" << std::endl; } } -static void create_pdf(char const* filename) +static void +create_pdf(char const* filename) { QPDF pdf; @@ -371,14 +352,13 @@ static void create_pdf(char const* filename) // Add an indirect object to contain a font descriptor for the // built-in Helvetica font. - QPDFObjectHandle font = pdf.makeIndirectObject( - "<<" - " /Type /Font" - " /Subtype /Type1" - " /Name /F1" - " /BaseFont /Helvetica" - " /Encoding /WinAnsiEncoding" - ">>"_qpdf); + QPDFObjectHandle font = pdf.makeIndirectObject("<<" + " /Type /Font" + " /Subtype /Type1" + " /Name /F1" + " /BaseFont /Helvetica" + " /Encoding /WinAnsiEncoding" + ">>"_qpdf); std::vector color_spaces; color_spaces.push_back("/DeviceCMYK"); @@ -389,10 +369,8 @@ static void create_pdf(char const* filename) filters.push_back("/DCTDecode"); filters.push_back("/RunLengthDecode"); QPDFPageDocumentHelper dh(pdf); - for (auto const& color_space : color_spaces) - { - for (auto const& filter : filters) - { + for (auto const& color_space : color_spaces) { + for (auto const& filter : filters) { add_page(dh, font, color_space, filter); } } @@ -405,22 +383,19 @@ static void create_pdf(char const* filename) check(filename, color_spaces, filters); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 2) - { + if (argc != 2) { usage(); } char const* filename = argv[1]; - try - { + try { create_pdf(filename); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-custom-filter.cc b/examples/pdf-custom-filter.cc index b986cfcc..b6d88684 100644 --- a/examples/pdf-custom-filter.cc +++ b/examples/pdf-custom-filter.cc @@ -1,7 +1,7 @@ #include -#include -#include #include +#include +#include #include #include @@ -39,7 +39,6 @@ static char const* whoami = 0; - class Pl_XOR: public Pipeline { // This class implements a Pipeline for the made-up XOR decoder. @@ -66,8 +65,7 @@ Pl_XOR::Pl_XOR(char const* identifier, Pipeline* next, unsigned char key) : void Pl_XOR::write(unsigned char* data, size_t len) { - for (size_t i = 0; i < len; ++i) - { + for (size_t i = 0; i < len; ++i) { unsigned char p = data[i] ^ this->key; getNext()->write(&p, 1); } @@ -118,8 +116,7 @@ SF_XORDecode::setDecodeParms(QPDFObjectHandle decode_parms) // to handle the /JBIG2Globals key, which points to a stream. See // comments in SF_XORDecode::registerStream for additional notes // on this. - try - { + try { // Expect /DecodeParms to be a dictionary with a /KeyStream // key that points to a one-byte stream whose single byte is // the key. If we are successful at retrieving the key, return @@ -129,17 +126,14 @@ SF_XORDecode::setDecodeParms(QPDFObjectHandle decode_parms) // implementations, look at the classes whose names start with // SF_ in the qpdf library implementation. auto buf = decode_parms.getKey("/KeyStream").getStreamData(); - if (buf->getSize() != 1) - { + if (buf->getSize() != 1) { return false; } this->key = buf->getBuffer()[0]; return true; - } - catch (std::exception& e) - { - std::cerr << "Error extracting key for /XORDecode: " - << e.what() << std::endl; + } catch (std::exception& e) { + std::cerr << "Error extracting key for /XORDecode: " << e.what() + << std::endl; } return false; } @@ -206,17 +200,19 @@ class StreamReplacer: public QPDFObjectHandle::StreamDataProvider public: StreamReplacer(QPDF* pdf); virtual ~StreamReplacer() = default; - virtual void provideStreamData(int objid, int generation, - Pipeline* pipeline) override; + virtual void + provideStreamData(int objid, int generation, Pipeline* pipeline) override; void registerStream( QPDFObjectHandle stream, PointerHolder self); private: - bool maybeReplace(QPDFObjGen const& og, - QPDFObjectHandle& stream, Pipeline* pipeline, - QPDFObjectHandle* dict_updates); + bool maybeReplace( + QPDFObjGen const& og, + QPDFObjectHandle& stream, + Pipeline* pipeline, + QPDFObjectHandle* dict_updates); // Hang onto a reference to the QPDF object containing the streams // we are replacing. We need this to create a new stream. @@ -238,10 +234,11 @@ StreamReplacer::StreamReplacer(QPDF* pdf) : } bool -StreamReplacer::maybeReplace(QPDFObjGen const& og, - QPDFObjectHandle& stream, - Pipeline* pipeline, - QPDFObjectHandle* dict_updates) +StreamReplacer::maybeReplace( + QPDFObjGen const& og, + QPDFObjectHandle& stream, + Pipeline* pipeline, + QPDFObjectHandle* dict_updates) { // As described in the class comments, this method is called // twice. Before writing has started pipeline is nullptr, and @@ -276,8 +273,7 @@ StreamReplacer::maybeReplace(QPDFObjGen const& og, // density. auto dict = stream.getDict(); auto mark = dict.getKey("/DoXOR"); - if (! (mark.isBool() && mark.getBoolValue())) - { + if (!(mark.isBool() && mark.getBoolValue())) { return false; } @@ -288,17 +284,13 @@ StreamReplacer::maybeReplace(QPDFObjGen const& og, // it's a good idea to make sure we can retrieve the filtered data // if we are going to need it later. PointerHolder out; - try - { + try { out = stream.getStreamData(); - } - catch (...) - { + } catch (...) { return false; } - if (dict_updates) - { + if (dict_updates) { // It's not safe to make any modifications to any objects // during the writing process since the updated objects may // have already been written. In this mode, when dict_updates @@ -309,16 +301,15 @@ StreamReplacer::maybeReplace(QPDFObjGen const& og, // changes. For example, an image resampler might change the // dimensions or other properties of the image. dict_updates->replaceKey( - "/OrigLength", QPDFObjectHandle::newInteger( - QIntC::to_longlong(out->getSize()))); + "/OrigLength", + QPDFObjectHandle::newInteger(QIntC::to_longlong(out->getSize()))); // We are also storing the "key" that we will access when // writing the data. this->keys[og] = QIntC::to_uchar( (og.getObj() * QIntC::to_int(out->getSize())) & 0xff); } - if (pipeline) - { + if (pipeline) { unsigned char key = this->keys[og]; Pl_XOR p("xor", pipeline, key); p.write(out->getBuffer(), out->getSize()); @@ -338,8 +329,7 @@ StreamReplacer::registerStream( // example, we are just iterating through objects, but if we were // doing something like iterating through images on pages, we // might realistically encounter the same stream more than once. - if (this->copied_streams.count(og) > 0) - { + if (this->copied_streams.count(og) > 0) { return; } // Store something in copied_streams so that we don't @@ -352,19 +342,14 @@ StreamReplacer::registerStream( // so, supplies dictionary updates we should make. bool should_replace = false; QPDFObjectHandle dict_updates = QPDFObjectHandle::newDictionary(); - try - { + try { should_replace = maybeReplace(og, stream, nullptr, &dict_updates); - } - catch (std::exception& e) - { + } catch (std::exception& e) { stream.warnIfPossible( - std::string("exception while attempting to replace: ") + - e.what()); + std::string("exception while attempting to replace: ") + e.what()); } - if (should_replace) - { + if (should_replace) { // Copy the stream so we can get to the original data from the // stream data provider. This doesn't actually copy any data, // but the copy retains the original stream data after the @@ -372,14 +357,13 @@ StreamReplacer::registerStream( this->copied_streams[og] = stream.copyStream(); // Update the stream dictionary with any changes. auto dict = stream.getDict(); - for (auto const& k: dict_updates.getKeys()) - { + for (auto const& k : dict_updates.getKeys()) { dict.replaceKey(k, dict_updates.getKey(k)); } // Create the key stream that will be referenced from // /DecodeParms. We have to do this now since you can't modify // or create objects during write. - char p[1] = { static_cast(this->keys[og]) }; + char p[1] = {static_cast(this->keys[og])}; std::string p_str(p, 1); QPDFObjectHandle dp_stream = QPDFObjectHandle::newStream(this->pdf, p_str); @@ -388,23 +372,19 @@ StreamReplacer::registerStream( QPDFObjectHandle decode_parms = QPDFObjectHandle::newDictionary({{"/KeyStream", dp_stream}}); stream.replaceStreamData( - self, - QPDFObjectHandle::newName("/XORDecode"), - decode_parms); + self, QPDFObjectHandle::newName("/XORDecode"), decode_parms); // Further, if /ProtectXOR = true, we disable filtering on write // so that QPDFWriter will not decode the stream even though we // have registered a stream filter for /XORDecode. auto protect = dict.getKey("/ProtectXOR"); - if (protect.isBool() && protect.getBoolValue()) - { + if (protect.isBool() && protect.getBoolValue()) { stream.setFilterOnWrite(false); } } } void -StreamReplacer::provideStreamData(int objid, int generation, - Pipeline* pipeline) +StreamReplacer::provideStreamData(int objid, int generation, Pipeline* pipeline) { QPDFObjGen og(objid, generation); QPDFObjectHandle orig = this->copied_streams[og]; @@ -412,8 +392,7 @@ StreamReplacer::provideStreamData(int objid, int generation, // dict_updates. In this mode, maybeReplace doesn't make any // changes. We have to hand it the original stream data, which we // get from copied_streams. - if (! maybeReplace(og, orig, pipeline, nullptr)) - { + if (!maybeReplace(og, orig, pipeline, nullptr)) { // Since this only gets called for streams we already // determined we are replacing, a false return would indicate // a logic error. @@ -422,8 +401,9 @@ StreamReplacer::provideStreamData(int objid, int generation, } } -static void process(char const* infilename, char const* outfilename, - bool decode_specialized) +static void +process( + char const* infilename, char const* outfilename, bool decode_specialized) { QPDF qpdf; qpdf.processFile(infilename); @@ -434,10 +414,8 @@ static void process(char const* infilename, char const* outfilename, StreamReplacer* replacer = new StreamReplacer(&qpdf); PointerHolder p(replacer); - for (auto& o: qpdf.getAllObjects()) - { - if (o.isStream()) - { + for (auto& o : qpdf.getAllObjects()) { + if (o.isStream()) { // Call registerStream for every stream. Only ones that // registerStream decides to replace will actually be // replaced. @@ -446,70 +424,58 @@ static void process(char const* infilename, char const* outfilename, } QPDFWriter w(qpdf, outfilename); - if (decode_specialized) - { + if (decode_specialized) { w.setDecodeLevel(qpdf_dl_specialized); } // For the test suite, use static IDs. w.setStaticID(true); // for testing only w.write(); - std::cout << whoami << ": new file written to " << outfilename - << std::endl; + std::cout << whoami << ": new file written to " << outfilename << std::endl; } -static void usage() +static void +usage() { - std::cerr - << "\n" - << "Usage: " << whoami << " [--decode-specialized] infile outfile\n" - << std::endl; + std::cerr << "\n" + << "Usage: " << whoami + << " [--decode-specialized] infile outfile\n" + << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); char const* infilename = 0; char const* outfilename = 0; bool decode_specialized = false; - for (int i = 1; i < argc; ++i) - { - if (strcmp(argv[i], "--decode-specialized") == 0) - { + for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "--decode-specialized") == 0) { decode_specialized = true; - } - else if (! infilename) - { + } else if (!infilename) { infilename = argv[i]; - } - else if (! outfilename) - { + } else if (!outfilename) { outfilename = argv[i]; - } - else - { + } else { usage(); } } - if (! (infilename && outfilename)) - { + if (!(infilename && outfilename)) { usage(); } - try - { + try { // Register our fictitious filter. This enables QPDFWriter to // decode our streams. This is not a real filter, so no real // PDF reading application would be able to interpret it. This // is just for illustrative purposes. QPDF::registerStreamFilter( - "/XORDecode", []{ return std::make_shared(); }); + "/XORDecode", [] { return std::make_shared(); }); // Do the actual processing. process(infilename, outfilename, decode_specialized); - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << ": exception: " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-double-page-size.cc b/examples/pdf-double-page-size.cc index a2f79301..37e07ff8 100644 --- a/examples/pdf-double-page-size.cc +++ b/examples/pdf-double-page-size.cc @@ -1,16 +1,17 @@ -#include -#include -#include +#include #include #include #include -#include -#include #include +#include +#include +#include +#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]" << std::endl @@ -21,45 +22,43 @@ void usage() // If there is a box of name box_name, replace it with a new box whose // elements are double the values of the original box. -static void doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name) +static void +doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name) { // We need to use getAttribute rather than getKey as some boxes could // be inherited. auto box = page.getAttribute(box_name, true); - if (box.isNull()) - { + if (box.isNull()) { return; } - if (! box.isRectangle()) - { - throw std::runtime_error(std::string("box ") + box_name + - " is not an array of four elements"); + if (!box.isRectangle()) { + throw std::runtime_error( + std::string("box ") + box_name + + " is not an array of four elements"); } std::vector doubled; - for (auto& item : box.aitems()) - { + for (auto& item : box.aitems()) { doubled.push_back( QPDFObjectHandle::newReal(item.getNumericValue() * 2.0, 2)); } - page.getObjectHandle() - .replaceKey(box_name, QPDFObjectHandle::newArray(doubled)); + page.getObjectHandle().replaceKey( + box_name, QPDFObjectHandle::newArray(doubled)); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); // For test suite bool static_id = false; - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) - { + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) { static_id = true; --argc; ++argv; } - if (! ((argc == 3) || (argc == 4))) - { + if (!((argc == 3) || (argc == 4))) { usage(); } @@ -70,13 +69,11 @@ int main(int argc, char* argv[]) // Text to prepend to each page's contents std::string content = "2 0 0 2 0 0 cm\n"; - try - { + try { QPDF qpdf; qpdf.processFile(infilename, password); - for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages()) - { + for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages()) { // Prepend the buffer to the page's contents page.addPageContents( QPDFObjectHandle::newStream(&qpdf, content), true); @@ -91,8 +88,7 @@ int main(int argc, char* argv[]) // Write out a new file QPDFWriter w(qpdf, outfilename); - if (static_id) - { + if (static_id) { // For the test suite, uncompress streams and use static IDs. w.setStaticID(true); // for testing only w.setStreamDataMode(qpdf_s_uncompress); @@ -100,9 +96,7 @@ int main(int argc, char* argv[]) w.write(); std::cout << whoami << ": new file written to " << outfilename << std::endl; - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << " processing file " << infilename << ": " << e.what() << std::endl; exit(2); diff --git a/examples/pdf-filter-tokens.cc b/examples/pdf-filter-tokens.cc index 02c3829e..39950752 100644 --- a/examples/pdf-filter-tokens.cc +++ b/examples/pdf-filter-tokens.cc @@ -5,22 +5,23 @@ // QPDFObjectHandle::TokenFilter with filterContents. // -#include -#include -#include #include #include +#include +#include +#include #include +#include #include #include -#include #include -#include +#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " infile outfile" << std::endl << "Applies token filters to infile and writes outfile" @@ -52,14 +53,11 @@ StringReverser::handleToken(QPDFTokenizer::Token const& token) // strings. It's just intended to give a simple example of a // pretty minimal filter and to show an example of writing a // constructed token. - if (token.getType() == QPDFTokenizer::tt_string) - { + if (token.getType() == QPDFTokenizer::tt_string) { std::string value = token.getValue(); std::reverse(value.begin(), value.end()); writeToken(QPDFTokenizer::Token(QPDFTokenizer::tt_string, value)); - } - else - { + } else { writeToken(token); } } @@ -90,15 +88,17 @@ class ColorToGray: public QPDFObjectHandle::TokenFilter bool ColorToGray::isNumeric(QPDFTokenizer::token_type_e token_type) { - return ((token_type == QPDFTokenizer::tt_integer) || - (token_type == QPDFTokenizer::tt_real)); + return ( + (token_type == QPDFTokenizer::tt_integer) || + (token_type == QPDFTokenizer::tt_real)); } bool ColorToGray::isIgnorable(QPDFTokenizer::token_type_e token_type) { - return ((token_type == QPDFTokenizer::tt_space) || - (token_type == QPDFTokenizer::tt_comment)); + return ( + (token_type == QPDFTokenizer::tt_space) || + (token_type == QPDFTokenizer::tt_comment)); } double @@ -134,33 +134,28 @@ ColorToGray::handleToken(QPDFTokenizer::Token const& token) // kinds of operands, replace the command. Flush any additional // accumulated tokens to keep the stack only four tokens deep. - while ((! this->all_stack.empty()) && - isIgnorable(this->all_stack.at(0).getType())) - { + while ((!this->all_stack.empty()) && + isIgnorable(this->all_stack.at(0).getType())) { writeToken(this->all_stack.at(0)); this->all_stack.pop_front(); } this->all_stack.push_back(token); QPDFTokenizer::token_type_e token_type = token.getType(); - if (! isIgnorable(token_type)) - { + if (!isIgnorable(token_type)) { this->stack.push_back(token); if ((this->stack.size() == 4) && (token == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "rg")) && (isNumeric(this->stack.at(0).getType())) && (isNumeric(this->stack.at(1).getType())) && - (isNumeric(this->stack.at(2).getType()))) - { + (isNumeric(this->stack.at(2).getType()))) { double r = numericValue(this->stack.at(0)); double g = numericValue(this->stack.at(1)); double b = numericValue(this->stack.at(2)); double gray = ((0.3 * r) + (0.59 * b) + (0.11 * g)); - if (gray > 1.0) - { + if (gray > 1.0) { gray = 1.0; } - if (gray < 0.0) - { + if (gray < 0.0) { gray = 0.0; } write(QUtil::double_to_string(gray, 3)); @@ -169,8 +164,7 @@ ColorToGray::handleToken(QPDFTokenizer::Token const& token) this->all_stack.clear(); } } - if (this->stack.size() == 4) - { + if (this->stack.size() == 4) { writeToken(this->all_stack.at(0)); this->all_stack.pop_front(); this->stack.pop_front(); @@ -181,33 +175,31 @@ void ColorToGray::handleEOF() { // Flush out any remaining accumulated tokens. - while (! this->all_stack.empty()) - { + while (!this->all_stack.empty()) { writeToken(this->all_stack.at(0)); this->all_stack.pop_front(); } } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 3) - { + if (argc != 3) { usage(); } char const* infilename = argv[1]; char const* outfilename = argv[2]; - try - { + try { QPDF pdf; pdf.processFile(infilename); std::vector pages = QPDFPageDocumentHelper(pdf).getAllPages(); for (std::vector::iterator iter = pages.begin(); - iter != pages.end(); ++iter) - { + iter != pages.end(); + ++iter) { // Attach two token filters to each page of this file. // When the file is written, or when the pages' contents // are retrieved in any other way, the filters will be @@ -222,11 +214,9 @@ int main(int argc, char* argv[]) } QPDFWriter w(pdf, outfilename); - w.setStaticID(true); // for testing only + w.setStaticID(true); // for testing only w.write(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << whoami << ": " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-invert-images.cc b/examples/pdf-invert-images.cc index def1dd19..df9e7362 100644 --- a/examples/pdf-invert-images.cc +++ b/examples/pdf-invert-images.cc @@ -1,17 +1,18 @@ -#include -#include -#include +#include +#include #include #include #include -#include -#include #include -#include +#include +#include +#include +#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]" << std::endl @@ -35,8 +36,8 @@ class ImageInverter: public QPDFObjectHandle::StreamDataProvider virtual ~ImageInverter() { } - virtual void provideStreamData(int objid, int generation, - Pipeline* pipeline) override; + virtual void + provideStreamData(int objid, int generation, Pipeline* pipeline) override; void registerImage( QPDFObjectHandle image, @@ -64,8 +65,7 @@ ImageInverter::registerImage( // generation number. Recall that a single image object may be // used more than once, so no need to update the same stream // multiple times. - if (this->copied_images.count(og) > 0) - { + if (this->copied_images.count(og) > 0) { return; } this->copied_images[og] = image.copyStream(); @@ -79,14 +79,12 @@ ImageInverter::registerImage( // filterable in the input QPDF object, so we don't have to deal // with it explicitly here. We could explicitly use /DCTDecode and // write through a DCT filter if we wanted. - image.replaceStreamData(self, - QPDFObjectHandle::newNull(), - QPDFObjectHandle::newNull()); + image.replaceStreamData( + self, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull()); } void -ImageInverter::provideStreamData(int objid, int generation, - Pipeline* pipeline) +ImageInverter::provideStreamData(int objid, int generation, Pipeline* pipeline) { // Use the object and generation number supplied to look up the // image data. Then invert the image data and write the inverted @@ -97,29 +95,27 @@ ImageInverter::provideStreamData(int objid, int generation, size_t size = data->getSize(); unsigned char* buf = data->getBuffer(); unsigned char ch; - for (size_t i = 0; i < size; ++i) - { + for (size_t i = 0; i < size; ++i) { ch = QIntC::to_uchar(0xff - buf[i]); pipeline->write(&ch, 1); } pipeline->finish(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); // For test suite bool static_id = false; - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) - { + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) { static_id = true; --argc; ++argv; } - if (! ((argc == 3) || (argc == 4))) - { + if (!((argc == 3) || (argc == 4))) { usage(); } @@ -127,8 +123,7 @@ int main(int argc, char* argv[]) char const* outfilename = argv[2]; char const* password = (argc == 4) ? argv[3] : ""; - try - { + try { QPDF qpdf; qpdf.processFile(infilename, password); @@ -139,18 +134,15 @@ int main(int argc, char* argv[]) std::vector pages = QPDFPageDocumentHelper(qpdf).getAllPages(); for (std::vector::iterator iter = pages.begin(); - iter != pages.end(); ++iter) - { + iter != pages.end(); + ++iter) { QPDFPageObjectHelper& page(*iter); // Get all images on the page. - std::map images = - page.getImages(); - for (auto& iter2: images) - { + std::map images = page.getImages(); + for (auto& iter2 : images) { QPDFObjectHandle& image = iter2.second; QPDFObjectHandle image_dict = image.getDict(); - QPDFObjectHandle color_space = - image_dict.getKey("/ColorSpace"); + QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace"); QPDFObjectHandle bits_per_component = image_dict.getKey("/BitsPerComponent"); @@ -159,12 +151,10 @@ int main(int argc, char* argv[]) // pipeStreamData with a null pipeline to determine // whether the image is filterable. Directly inspect // keys to determine the image type. - if (image.pipeStreamData(0, qpdf_ef_compress, - qpdf_dl_all) && + if (image.pipeStreamData(0, qpdf_ef_compress, qpdf_dl_all) && color_space.isNameAndEquals("/DeviceGray") && bits_per_component.isInteger() && - (bits_per_component.getIntValue() == 8)) - { + (bits_per_component.getIntValue() == 8)) { inv->registerImage(image, p); } } @@ -172,8 +162,7 @@ int main(int argc, char* argv[]) // Write out a new file QPDFWriter w(qpdf, outfilename); - if (static_id) - { + if (static_id) { // For the test suite, uncompress streams and use static // IDs. w.setStaticID(true); // for testing only @@ -181,9 +170,7 @@ int main(int argc, char* argv[]) w.write(); std::cout << whoami << ": new file written to " << outfilename << std::endl; - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << " processing file " << infilename << ": " << e.what() << std::endl; exit(2); diff --git a/examples/pdf-linearize.c b/examples/pdf-linearize.c index 2b099066..f972fa71 100644 --- a/examples/pdf-linearize.c +++ b/examples/pdf-linearize.c @@ -9,18 +9,21 @@ static char const* whoami = 0; -static void usage() +static void +usage() { fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami); exit(2); } -static void write_progress(int percent, void* data) +static void +write_progress(int percent, void* data) { printf("%s progress: %d%%\n", (char const*)(data), percent); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { char* infile = NULL; char* password = NULL; @@ -30,21 +33,15 @@ int main(int argc, char* argv[]) int errors = 0; char* p = 0; - if ((p = strrchr(argv[0], '/')) != NULL) - { + if ((p = strrchr(argv[0], '/')) != NULL) { whoami = p + 1; - } - else if ((p = strrchr(argv[0], '\\')) != NULL) - { + } else if ((p = strrchr(argv[0], '\\')) != NULL) { whoami = p + 1; - } - else - { + } else { whoami = argv[0]; } - if (argc != 4) - { + if (argc != 4) { usage(); } @@ -53,8 +50,7 @@ int main(int argc, char* argv[]) outfile = argv[3]; if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) && - ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) - { + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) { /* Use static ID for testing only. For production, a * non-static ID is used. See also * qpdf_set_deterministic_ID. */ @@ -63,25 +59,22 @@ int main(int argc, char* argv[]) qpdf_register_progress_reporter(qpdf, write_progress, infile); qpdf_write(qpdf); } - while (qpdf_more_warnings(qpdf)) - { + while (qpdf_more_warnings(qpdf)) { warnings = 1; - printf("warning: %s\n", - qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); + printf( + "warning: %s\n", + qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); } - if (qpdf_has_error(qpdf)) - { + if (qpdf_has_error(qpdf)) { errors = 1; - printf("error: %s\n", - qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); + printf( + "error: %s\n", + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); } qpdf_cleanup(&qpdf); - if (errors) - { + if (errors) { return 2; - } - else if (warnings) - { + } else if (warnings) { return 3; } diff --git a/examples/pdf-mod-info.cc b/examples/pdf-mod-info.cc index 4ee4cdd0..8666474f 100644 --- a/examples/pdf-mod-info.cc +++ b/examples/pdf-mod-info.cc @@ -1,49 +1,43 @@ // Author: Vitaliy Pavlyuk #include -#include #include -#include +#include #include +#include #include -#include -#include #include +#include +#include static char const* version = "1.1"; static char const* whoami = 0; -void usage() +void +usage() { - std::cerr - << "Usage: " << whoami - << " --in in_file [--out out_file] [--key key [--val val]?]+\n" - << "Modifies/Adds/Removes PDF /Info entries in the in_file\n" - << "and stores the result in out_file\n" - << "Special mode: " << whoami << " --dump file\n" - << "dumps all /Info entries to stdout\n"; + std::cerr << "Usage: " << whoami + << " --in in_file [--out out_file] [--key key [--val val]?]+\n" + << "Modifies/Adds/Removes PDF /Info entries in the in_file\n" + << "and stores the result in out_file\n" + << "Special mode: " << whoami << " --dump file\n" + << "dumps all /Info entries to stdout\n"; exit(2); } -void dumpInfoDict(QPDF& pdf, - std::ostream& os = std::cout, - std::string const& sep = ":\t") +void +dumpInfoDict( + QPDF& pdf, std::ostream& os = std::cout, std::string const& sep = ":\t") { QPDFObjectHandle trailer = pdf.getTrailer(); - if (trailer.hasKey("/Info")) - { - for (auto& it: trailer.getKey("/Info").ditems()) - { + if (trailer.hasKey("/Info")) { + for (auto& it : trailer.getKey("/Info").ditems()) { std::string val; - if (it.second.isString()) - { + if (it.second.isString()) { val = it.second.getStringValue(); - } - else if (it.second.isName()) - { + } else if (it.second.isName()) { val = it.second.getName(); - } - else // according to PDF Spec 1.5, shouldn't happen + } else // according to PDF Spec 1.5, shouldn't happen { val = it.second.unparseResolved(); } @@ -52,36 +46,32 @@ void dumpInfoDict(QPDF& pdf, } } -void pdfDumpInfoDict(char const* fname) +void +pdfDumpInfoDict(char const* fname) { - try - { + try { QPDF pdf; pdf.processFile(fname); dumpInfoDict(pdf); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - bool static_id = false; std::map Keys; whoami = QUtil::getWhoami(argv[0]); - if ((argc == 2) && (! strcmp(argv[1], "--version")) ) - { + if ((argc == 2) && (!strcmp(argv[1], "--version"))) { std::cout << whoami << " version " << version << std::endl; exit(0); } - if ((argc == 3) && (! strcmp(argv[1], "--dump"))) - { + if ((argc == 3) && (!strcmp(argv[1], "--dump"))) { QTC::TC("examples", "pdf-mod-info --dump"); pdfDumpInfoDict(argv[2]); exit(0); @@ -91,59 +81,43 @@ int main(int argc, char* argv[]) char* fl_out = 0; std::string cur_key; - for (int i = 1; i < argc; ++i) - { - if ((! strcmp(argv[i], "--in")) && (++i < argc)) - { + for (int i = 1; i < argc; ++i) { + if ((!strcmp(argv[i], "--in")) && (++i < argc)) { fl_in = argv[i]; - } - else if ((! strcmp(argv[i], "--out")) && (++i < argc)) - { + } else if ((!strcmp(argv[i], "--out")) && (++i < argc)) { fl_out = argv[i]; - } - else if (! strcmp(argv[i], "--static-id")) // don't document + } else if (!strcmp(argv[i], "--static-id")) // don't document { static_id = true; // this should be used in test suites only - } - else if ((! strcmp(argv[i], "--key")) && (++i < argc)) - { + } else if ((!strcmp(argv[i], "--key")) && (++i < argc)) { QTC::TC("examples", "pdf-mod-info -key"); cur_key = argv[i]; - if (! ((cur_key.length() > 0) && (cur_key.at(0) == '/'))) - { + if (!((cur_key.length() > 0) && (cur_key.at(0) == '/'))) { cur_key = "/" + cur_key; } Keys[cur_key] = ""; - } - else if ((! strcmp(argv[i], "--val")) && (++i < argc)) - { - if (cur_key.empty()) - { + } else if ((!strcmp(argv[i], "--val")) && (++i < argc)) { + if (cur_key.empty()) { QTC::TC("examples", "pdf-mod-info usage wrong val"); usage(); } QTC::TC("examples", "pdf-mod-info -val"); Keys[cur_key] = argv[i]; cur_key.clear(); - } - else - { + } else { QTC::TC("examples", "pdf-mod-info usage junk"); usage(); } } - if (! fl_in) - { + if (!fl_in) { QTC::TC("examples", "pdf-mod-info no in file"); usage(); } - if (! fl_out) - { + if (!fl_out) { QTC::TC("examples", "pdf-mod-info in-place"); fl_out = fl_in; } - if (Keys.size() == 0) - { + if (Keys.size() == 0) { QTC::TC("examples", "pdf-mod-info no keys"); usage(); } @@ -151,8 +125,7 @@ int main(int argc, char* argv[]) std::string fl_tmp = fl_out; fl_tmp += ".tmp"; - try - { + try { QPDF file; file.processFile(fl_in); @@ -160,28 +133,22 @@ int main(int argc, char* argv[]) QPDFObjectHandle fileinfo; for (std::map::const_iterator it = - Keys.begin(); Keys.end() != it; ++it) - { - if (! fileinfo.isInitialized()) - { - if (filetrailer.hasKey("/Info")) - { + Keys.begin(); + Keys.end() != it; + ++it) { + if (!fileinfo.isInitialized()) { + if (filetrailer.hasKey("/Info")) { QTC::TC("examples", "pdf-mod-info has info"); fileinfo = filetrailer.getKey("/Info"); - } - else - { + } else { QTC::TC("examples", "pdf-mod-info file no info"); fileinfo = QPDFObjectHandle::newDictionary(); filetrailer.replaceKey("/Info", fileinfo); } } - if (it->second == "") - { + if (it->second == "") { fileinfo.removeKey(it->first); - } - else - { + } else { QPDFObjectHandle elt = fileinfo.newString(it->second); elt.makeDirect(); fileinfo.replaceKey(it->first, elt); @@ -192,21 +159,17 @@ int main(int argc, char* argv[]) w.setLinearization(true); w.setStaticID(static_id); // for testing only w.write(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } - try - { - (void) remove(fl_out); - QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out), - rename(fl_tmp.c_str(), fl_out)); - } - catch (std::exception& e) - { + try { + (void)remove(fl_out); + QUtil::os_wrapper( + "rename " + fl_tmp + " " + std::string(fl_out), + rename(fl_tmp.c_str(), fl_out)); + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-name-number-tree.cc b/examples/pdf-name-number-tree.cc index d8bb233e..347bfb69 100644 --- a/examples/pdf-name-number-tree.cc +++ b/examples/pdf-name-number-tree.cc @@ -3,26 +3,26 @@ #include #include #include -#include #include +#include static char const* whoami = 0; -void usage() +void +usage() { - std::cerr << "Usage: " << whoami << " outfile.pdf" - << std::endl + std::cerr << "Usage: " << whoami << " outfile.pdf" << std::endl << "Create some name/number trees and write to a file" << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 2) - { + if (argc != 2) { usage(); } @@ -62,8 +62,8 @@ int main(int argc, char* argv[]) name_tree.insert("R", QPDFObjectHandle::newUnicodeString("rook")); name_tree.insert("B", QPDFObjectHandle::newUnicodeString("bishop")); name_tree.insert("N", QPDFObjectHandle::newUnicodeString("knight")); - auto iter = name_tree.insert( - "P", QPDFObjectHandle::newUnicodeString("pawn")); + auto iter = + name_tree.insert("P", QPDFObjectHandle::newUnicodeString("pawn")); // Look at the iterator std::cout << "just inserted " << iter->first << " -> " << iter->second.unparse() << std::endl; @@ -77,28 +77,24 @@ int main(int argc, char* argv[]) // Use range-for iteration std::cout << "Name tree items:" << std::endl; - for (auto i: name_tree) - { - std::cout << " " << i.first << " -> " - << i.second.unparse() << std::endl; + for (auto i : name_tree) { + std::cout << " " << i.first << " -> " << i.second.unparse() + << std::endl; } // This is a small tree, so everything will be at the root. We can // look at it using dictionary and array iterators. std::cout << "Keys in name tree object:" << std::endl; QPDFObjectHandle names; - for (auto const& i: name_tree_oh.ditems()) - { + for (auto const& i : name_tree_oh.ditems()) { std::cout << i.first << std::endl; - if (i.first == "/Names") - { + if (i.first == "/Names") { names = i.second; } } // Values in names array: std::cout << "Values in names:" << std::endl; - for (auto& i: names.aitems()) - { + for (auto& i : names.aitems()) { std::cout << " " << i.unparse() << std::endl; } @@ -112,8 +108,8 @@ int main(int argc, char* argv[]) // 10.2 API iter = name_tree.find("Q"); - std::cout << "Q: " << iter->first << " -> " - << iter->second.unparse() << std::endl; + std::cout << "Q: " << iter->first << " -> " << iter->second.unparse() + << std::endl; iter = name_tree.find("W"); std::cout << "W found: " << (iter != name_tree.end()) << std::endl; // Allow find to return predecessor @@ -146,22 +142,18 @@ int main(int argc, char* argv[]) auto number_tree_oh = number_tree.getObjectHandle(); example.replaceKey("/NumberTree", number_tree_oh); auto iter2 = number_tree.begin(); - for (int i = 7; i <= 350; i += 7) - { - iter2.insertAfter(i, QPDFObjectHandle::newString( - "-" + QUtil::int_to_string(i) + "-")); + for (int i = 7; i <= 350; i += 7) { + iter2.insertAfter( + i, + QPDFObjectHandle::newString("-" + QUtil::int_to_string(i) + "-")); } std::cout << "Numbers:" << std::endl; int n = 1; - for (auto& i: number_tree) - { + for (auto& i : number_tree) { std::cout << i.first << " -> " << i.second.getUTF8Value(); - if (n % 5) - { + if (n % 5) { std::cout << ", "; - } - else - { + } else { std::cout << std::endl; } ++n; @@ -171,28 +163,20 @@ int main(int argc, char* argv[]) // advances. This makes it possible to filter while iterating. // Remove all items that are multiples of 5. iter2 = number_tree.begin(); - while (iter2 != number_tree.end()) - { - if (iter2->first % 5 == 0) - { - iter2.remove(); // also advances - } - else - { + while (iter2 != number_tree.end()) { + if (iter2->first % 5 == 0) { + iter2.remove(); // also advances + } else { ++iter2; } } std::cout << "Numbers after filtering:" << std::endl; n = 1; - for (auto& i: number_tree) - { + for (auto& i : number_tree) { std::cout << i.first << " -> " << i.second.getUTF8Value(); - if (n % 5) - { + if (n % 5) { std::cout << ", "; - } - else - { + } else { std::cout << std::endl; } ++n; diff --git a/examples/pdf-npages.cc b/examples/pdf-npages.cc index 93378c71..f430935c 100644 --- a/examples/pdf-npages.cc +++ b/examples/pdf-npages.cc @@ -1,46 +1,43 @@ #include -#include #include +#include #include #include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " filename" << std::endl << "Prints the number of pages in filename" << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) - { + if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) { std::cout << whoami << " version 1.3" << std::endl; exit(0); } - if (argc != 2) - { + if (argc != 2) { usage(); } char const* filename = argv[1]; - try - { + try { QPDF pdf; pdf.processFile(filename); QPDFObjectHandle root = pdf.getRoot(); QPDFObjectHandle pages = root.getKey("/Pages"); QPDFObjectHandle count = pages.getKey("/Count"); std::cout << count.getIntValue() << std::endl; - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << whoami << ": " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-overlay-page.cc b/examples/pdf-overlay-page.cc index 5d5468f1..64a8d48f 100644 --- a/examples/pdf-overlay-page.cc +++ b/examples/pdf-overlay-page.cc @@ -1,11 +1,11 @@ -#include -#include -#include #include #include #include #include #include +#include +#include +#include // This program demonstrates use of form XObjects to overlay a page // from one file onto all pages of another file. The qpdf program's @@ -14,19 +14,17 @@ static char const* whoami = 0; -void usage() +void +usage() { - std::cerr << "Usage: " << whoami << " infile pagefile outfile" - << std::endl + std::cerr << "Usage: " << whoami << " infile pagefile outfile" << std::endl << "Stamp page 1 of pagefile on every page of infile," - << " writing to outfile" - << std::endl; + << " writing to outfile" << std::endl; exit(2); } -static void stamp_page(char const* infile, - char const* stampfile, - char const* outfile) +static void +stamp_page(char const* infile, char const* stampfile, char const* outfile) { QPDF inpdf; inpdf.processFile(infile); @@ -45,8 +43,8 @@ static void stamp_page(char const* infile, std::vector pages = QPDFPageDocumentHelper(inpdf).getAllPages(); for (std::vector::iterator iter = pages.begin(); - iter != pages.end(); ++iter) - { + iter != pages.end(); + ++iter) { QPDFPageObjectHelper& ph = *iter; // Find a unique resource name for the new form XObject @@ -57,11 +55,9 @@ static void stamp_page(char const* infile, // Generate content to place the form XObject centered within // destination page's trim box. QPDFMatrix m; - std::string content = - ph.placeFormXObject( - stamp_fo, name, ph.getTrimBox().getArrayAsRectangle(), m); - if (! content.empty()) - { + std::string content = ph.placeFormXObject( + stamp_fo, name, ph.getTrimBox().getArrayAsRectangle(), m); + if (!content.empty()) { // Append the content to the page's content. Surround the // original content with q...Q to the new content from the // page's original content. @@ -80,28 +76,25 @@ static void stamp_page(char const* infile, } QPDFWriter w(inpdf, outfile); - w.setStaticID(true); // for testing only + w.setStaticID(true); // for testing only w.write(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 4) - { + if (argc != 4) { usage(); } char const* infile = argv[1]; char const* stampfile = argv[2]; char const* outfile = argv[3]; - try - { + try { stamp_page(infile, stampfile, outfile); - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << ": " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-parse-content.cc b/examples/pdf-parse-content.cc index dbe3a451..f1f7ab55 100644 --- a/examples/pdf-parse-content.cc +++ b/examples/pdf-parse-content.cc @@ -1,16 +1,17 @@ #include -#include #include +#include +#include #include #include #include #include -#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " filename page-number" << std::endl << "Prints a dump of the objects in the content streams" @@ -38,17 +39,14 @@ ParserCallbacks::contentSize(size_t size) } void -ParserCallbacks::handleObject(QPDFObjectHandle obj, - size_t offset, size_t length) +ParserCallbacks::handleObject( + QPDFObjectHandle obj, size_t offset, size_t length) { std::cout << obj.getTypeName() << ", offset=" << offset << ", length=" << length << ": "; - if (obj.isInlineImage()) - { + if (obj.isInlineImage()) { std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl; - } - else - { + } else { std::cout << obj.unparse() << std::endl; } } @@ -59,34 +57,30 @@ ParserCallbacks::handleEOF() std::cout << "-EOF-" << std::endl; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 3) - { + if (argc != 3) { usage(); } char const* filename = argv[1]; int pageno = QUtil::string_to_int(argv[2]); - try - { + try { QPDF pdf; pdf.processFile(filename); std::vector pages = QPDFPageDocumentHelper(pdf).getAllPages(); - if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size())) - { + if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size())) { usage(); } - QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno-1)); + QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno - 1)); ParserCallbacks cb; page.parseContents(&cb); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << whoami << ": " << e.what() << std::endl; exit(2); } diff --git a/examples/pdf-set-form-values.cc b/examples/pdf-set-form-values.cc index eab24e4f..10394c7b 100644 --- a/examples/pdf-set-form-values.cc +++ b/examples/pdf-set-form-values.cc @@ -1,16 +1,17 @@ -#include -#include -#include -#include #include -#include #include +#include #include #include +#include +#include +#include +#include static char const* whoami = 0; -void usage() +void +usage() { std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf value" << std::endl @@ -19,13 +20,12 @@ void usage() exit(2); } - -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 4) - { + if (argc != 4) { usage(); } @@ -39,8 +39,7 @@ int main(int argc, char* argv[]) // to illustrate use of the helper classes around interactive // forms. - try - { + try { QPDF qpdf; qpdf.processFile(infilename); @@ -55,8 +54,8 @@ int main(int argc, char* argv[]) std::vector pages = pdh.getAllPages(); for (std::vector::iterator page_iter = pages.begin(); - page_iter != pages.end(); ++page_iter) - { + page_iter != pages.end(); + ++page_iter) { // Get all widget annotations for each page. Widget // annotations are the ones that contain the details of // what's in a form field. @@ -64,14 +63,13 @@ int main(int argc, char* argv[]) afdh.getWidgetAnnotationsForPage(*page_iter); for (std::vector::iterator annot_iter = annotations.begin(); - annot_iter != annotations.end(); ++annot_iter) - { + annot_iter != annotations.end(); + ++annot_iter) { // For each annotation, find its associated field. If // it's a text field, set its value. QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(*annot_iter); - if (ffh.getFieldType() == "/Tx") - { + if (ffh.getFieldType() == "/Tx") { // Set the value. Passing false as the second // value prevents qpdf from setting // /NeedAppearances to true (but will not turn it @@ -92,9 +90,7 @@ int main(int argc, char* argv[]) QPDFWriter w(qpdf, outfilename); w.setStaticID(true); // for testing only w.write(); - } - catch (std::exception &e) - { + } catch (std::exception& e) { std::cerr << whoami << " processing file " << infilename << ": " << e.what() << std::endl; exit(2); diff --git a/examples/pdf-split-pages.cc b/examples/pdf-split-pages.cc index c79177ee..a8e65260 100644 --- a/examples/pdf-split-pages.cc +++ b/examples/pdf-split-pages.cc @@ -4,22 +4,21 @@ // does. // +#include #include #include #include #include -#include +#include #include #include #include -#include static bool static_id = false; -static void process(char const* whoami, - char const* infile, - std::string outprefix) +static void +process(char const* whoami, char const* infile, std::string outprefix) { QPDF inpdf; inpdf.processFile(infile); @@ -29,8 +28,8 @@ static void process(char const* whoami, QIntC::to_int(QUtil::uint_to_string(pages.size()).length()); int pageno = 0; for (std::vector::iterator iter = pages.begin(); - iter != pages.end(); ++iter) - { + iter != pages.end(); + ++iter) { QPDFPageObjectHelper& page(*iter); std::string outfile = outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf"; @@ -38,8 +37,7 @@ static void process(char const* whoami, outpdf.emptyPDF(); QPDFPageDocumentHelper(outpdf).addPage(page, false); QPDFWriter outpdfw(outpdf, outfile.c_str()); - if (static_id) - { + if (static_id) { // For the test suite, uncompress streams and use static // IDs. outpdfw.setStaticID(true); // for testing only @@ -49,34 +47,31 @@ static void process(char const* whoami, } } -void usage(char const* whoami) +void +usage(char const* whoami) { std::cerr << "Usage: " << whoami << " infile outprefix" << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { char const* whoami = QUtil::getWhoami(argv[0]); // For test suite - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) - { + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) { static_id = true; --argc; ++argv; } - if (argc != 3) - { + if (argc != 3) { usage(whoami); } - try - { + try { process(whoami, argv[1], argv[2]); - } - catch (std::exception const& e) - { + } catch (std::exception const& e) { std::cerr << whoami << ": exception: " << e.what() << std::endl; return 2; } diff --git a/examples/qpdf-job.cc b/examples/qpdf-job.cc index ecebca3b..b71b612a 100644 --- a/examples/qpdf-job.cc +++ b/examples/qpdf-job.cc @@ -1,34 +1,32 @@ +#include #include #include -#include -#include #include +#include // This program is a simple demonstration of different ways to use the // QPDFJob API. static char const* whoami = 0; -static void usage() +static void +usage() { - std::cerr - << "Usage: " << whoami << std::endl - << "This program linearizes the first page of in.pdf to out1.pdf," - << " out2.pdf, and" - << std::endl - << " out3.pdf, each demonstrating a different way to use the" - << " QPDFJob API" - << std::endl; + std::cerr << "Usage: " << whoami << std::endl + << "This program linearizes the first page of in.pdf to out1.pdf," + << " out2.pdf, and" << std::endl + << " out3.pdf, each demonstrating a different way to use the" + << " QPDFJob API" << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { whoami = QUtil::getWhoami(argv[0]); - if (argc != 1) - { + if (argc != 1) { usage(); } @@ -39,8 +37,7 @@ int main(int argc, char* argv[]) // Note that staticId is used for testing only. - try - { + try { // Use the config API QPDFJob j; j.config() @@ -54,15 +51,12 @@ int main(int argc, char* argv[]) ->checkConfiguration(); j.run(); std::cout << "out1 status: " << j.getExitCode() << std::endl; - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << "exception: " << e.what() << std::endl; return 2; } - try - { + try { char const* new_argv[] = { whoami, "in.pdf", @@ -73,21 +67,17 @@ int main(int argc, char* argv[]) "1", "--", "--static-id", - nullptr - }; + nullptr}; QPDFJob j; j.initializeFromArgv(new_argv); j.run(); std::cout << "out2 status: " << j.getExitCode() << std::endl; - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << "exception: " << e.what() << std::endl; return 2; } - try - { + try { // Use the JSON API QPDFJob j; j.initializeFromJson(R"({ @@ -105,9 +95,7 @@ int main(int argc, char* argv[]) )"); j.run(); std::cout << "out3 status: " << j.getExitCode() << std::endl; - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << "exception: " << e.what() << std::endl; return 2; } diff --git a/examples/qpdfjob-c.c b/examples/qpdfjob-c.c index ad81655a..ee2ef4ab 100644 --- a/examples/qpdfjob-c.c +++ b/examples/qpdfjob-c.c @@ -10,13 +10,15 @@ static char const* whoami = 0; -static void usage() +static void +usage() { fprintf(stderr, "Usage: %s infile outfile\n", whoami); exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { char* infile = NULL; char* outfile = NULL; @@ -24,21 +26,15 @@ int main(int argc, char* argv[]) int r = 0; char* p = 0; - if ((p = strrchr(argv[0], '/')) != NULL) - { + if ((p = strrchr(argv[0], '/')) != NULL) { whoami = p + 1; - } - else if ((p = strrchr(argv[0], '\\')) != NULL) - { + } else if ((p = strrchr(argv[0], '\\')) != NULL) { whoami = p + 1; - } - else - { + } else { whoami = argv[0]; } - if (argc != 3) - { + if (argc != 3) { usage(); } -- cgit v1.2.3-54-g00ecf