2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-30 08:50:51 +00:00

Split QPDFJob::run into createQPDF and writeQPDF

Also, change QPDFJob to use unique_ptr<QPDF> instead of shared pointers.
This commit is contained in:
m-holger 2023-03-05 12:27:15 +00:00
parent 4359de9038
commit 43d9ee56ea
2 changed files with 52 additions and 23 deletions

View File

@ -397,6 +397,21 @@ class QPDFJob
QPDF_DLL QPDF_DLL
void run(); void run();
// The following two methods allow a job to be run in two stages - creation
// of a QPDF object and writing of the QPDF object. This allows the QPDF
// object to be modified prior to writing it out. See
// examples/qpdfjob-remove-annotations for an illustration of its use.
// Run the first stage of the job. Return a nullptr if the configuration is
// not valid.
QPDF_DLL
std::unique_ptr<QPDF> createQPDF();
// Run the second stage of the job. Do nothing if a nullptr is passed as
// parameter.
QPDF_DLL
void writeQPDF(QPDF& qpdf);
// CHECK STATUS -- these methods provide information known after // CHECK STATUS -- these methods provide information known after
// run() is called. // run() is called.
@ -474,7 +489,7 @@ class QPDFJob
std::string to_nr; std::string to_nr;
std::string from_nr; std::string from_nr;
std::string repeat_nr; std::string repeat_nr;
std::shared_ptr<QPDF> pdf; std::unique_ptr<QPDF> pdf;
std::vector<int> to_pagenos; std::vector<int> to_pagenos;
std::vector<int> from_pagenos; std::vector<int> from_pagenos;
std::vector<int> repeat_pagenos; std::vector<int> repeat_pagenos;
@ -490,25 +505,25 @@ class QPDFJob
// Basic file processing // Basic file processing
void processFile( void processFile(
std::shared_ptr<QPDF>&, std::unique_ptr<QPDF>&,
char const* filename, char const* filename,
char const* password, char const* password,
bool used_for_input, bool used_for_input,
bool main_input); bool main_input);
void processInputSource( void processInputSource(
std::shared_ptr<QPDF>&, std::unique_ptr<QPDF>&,
std::shared_ptr<InputSource> is, std::shared_ptr<InputSource> is,
char const* password, char const* password,
bool used_for_input); bool used_for_input);
void doProcess( void doProcess(
std::shared_ptr<QPDF>&, std::unique_ptr<QPDF>&,
std::function<void(QPDF*, char const*)> fn, std::function<void(QPDF*, char const*)> fn,
char const* password, char const* password,
bool empty, bool empty,
bool used_for_input, bool used_for_input,
bool main_input); bool main_input);
void doProcessOnce( void doProcessOnce(
std::shared_ptr<QPDF>&, std::unique_ptr<QPDF>&,
std::function<void(QPDF*, char const*)> fn, std::function<void(QPDF*, char const*)> fn,
char const* password, char const* password,
bool empty, bool empty,
@ -518,7 +533,7 @@ class QPDFJob
// Transformations // Transformations
void setQPDFOptions(QPDF& pdf); void setQPDFOptions(QPDF& pdf);
void void
handlePageSpecs(QPDF& pdf, std::vector<std::shared_ptr<QPDF>>& page_heap); handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap);
bool shouldRemoveUnreferencedResources(QPDF& pdf); bool shouldRemoveUnreferencedResources(QPDF& pdf);
void handleRotations(QPDF& pdf); void handleRotations(QPDF& pdf);
void void

View File

@ -447,11 +447,11 @@ QPDFJob::parseNumrange(char const* range, int max)
return std::vector<int>(); return std::vector<int>();
} }
void std::unique_ptr<QPDF>
QPDFJob::run() QPDFJob::createQPDF()
{ {
checkConfiguration(); checkConfiguration();
std::shared_ptr<QPDF> pdf_sp; std::unique_ptr<QPDF> pdf_sp;
try { try {
processFile(pdf_sp, m->infilename.get(), m->password.get(), true, true); processFile(pdf_sp, m->infilename.get(), m->password.get(), true, true);
} catch (QPDFExc& e) { } catch (QPDFExc& e) {
@ -461,12 +461,12 @@ QPDFJob::run()
if (m->check_is_encrypted || m->check_requires_password) { if (m->check_is_encrypted || m->check_requires_password) {
this->m->encryption_status = this->m->encryption_status =
qpdf_es_encrypted | qpdf_es_password_incorrect; qpdf_es_encrypted | qpdf_es_password_incorrect;
return; return nullptr;
} }
if (m->show_encryption && pdf_sp) { if (m->show_encryption && pdf_sp) {
this->m->log->info("Incorrect password supplied\n"); this->m->log->info("Incorrect password supplied\n");
showEncryption(*pdf_sp); showEncryption(*pdf_sp);
return; return nullptr;
} }
} }
throw e; throw e;
@ -477,7 +477,7 @@ QPDFJob::run()
} }
if (m->check_is_encrypted || m->check_requires_password) { if (m->check_is_encrypted || m->check_requires_password) {
return; return nullptr;
} }
// If we are updating from JSON, this has to be done first before // If we are updating from JSON, this has to be done first before
@ -486,7 +486,7 @@ QPDFJob::run()
pdf.updateFromJSON(this->m->update_from_json); pdf.updateFromJSON(this->m->update_from_json);
} }
std::vector<std::shared_ptr<QPDF>> page_heap; std::vector<std::unique_ptr<QPDF>> page_heap;
if (!m->page_specs.empty()) { if (!m->page_specs.empty()) {
handlePageSpecs(pdf, page_heap); handlePageSpecs(pdf, page_heap);
} }
@ -495,7 +495,12 @@ QPDFJob::run()
} }
handleUnderOverlay(pdf); handleUnderOverlay(pdf);
handleTransformations(pdf); handleTransformations(pdf);
return pdf_sp;
}
void
QPDFJob::writeQPDF(QPDF& pdf)
{
if (!createsOutput()) { if (!createsOutput()) {
doInspection(pdf); doInspection(pdf);
} else if (m->split_pages) { } else if (m->split_pages) {
@ -527,6 +532,15 @@ QPDFJob::run()
} }
} }
void
QPDFJob::run()
{
auto pdf = createQPDF();
if (pdf) {
writeQPDF(*pdf);
}
}
bool bool
QPDFJob::hasWarnings() const QPDFJob::hasWarnings() const
{ {
@ -1868,14 +1882,14 @@ QPDFJob::doInspection(QPDF& pdf)
void void
QPDFJob::doProcessOnce( QPDFJob::doProcessOnce(
std::shared_ptr<QPDF>& pdf, std::unique_ptr<QPDF>& pdf,
std::function<void(QPDF*, char const*)> fn, std::function<void(QPDF*, char const*)> fn,
char const* password, char const* password,
bool empty, bool empty,
bool used_for_input, bool used_for_input,
bool main_input) bool main_input)
{ {
pdf = QPDF::create(); pdf = std::make_unique<QPDF>();
setQPDFOptions(*pdf); setQPDFOptions(*pdf);
if (empty) { if (empty) {
pdf->emptyPDF(); pdf->emptyPDF();
@ -1892,7 +1906,7 @@ QPDFJob::doProcessOnce(
void void
QPDFJob::doProcess( QPDFJob::doProcess(
std::shared_ptr<QPDF>& pdf, std::unique_ptr<QPDF>& pdf,
std::function<void(QPDF*, char const*)> fn, std::function<void(QPDF*, char const*)> fn,
char const* password, char const* password,
bool empty, bool empty,
@ -1976,7 +1990,7 @@ QPDFJob::doProcess(
void void
QPDFJob::processFile( QPDFJob::processFile(
std::shared_ptr<QPDF>& pdf, std::unique_ptr<QPDF>& pdf,
char const* filename, char const* filename,
char const* password, char const* password,
bool used_for_input, bool used_for_input,
@ -1996,7 +2010,7 @@ QPDFJob::processFile(
void void
QPDFJob::processInputSource( QPDFJob::processInputSource(
std::shared_ptr<QPDF>& pdf, std::unique_ptr<QPDF>& pdf,
std::shared_ptr<InputSource> is, std::shared_ptr<InputSource> is,
char const* password, char const* password,
bool used_for_input) bool used_for_input)
@ -2278,7 +2292,7 @@ QPDFJob::copyAttachments(QPDF& pdf)
v << prefix << ": copying attachments from " << to_copy.path v << prefix << ": copying attachments from " << to_copy.path
<< "\n"; << "\n";
}); });
std::shared_ptr<QPDF> other; std::unique_ptr<QPDF> other;
processFile( processFile(
other, other,
to_copy.path.c_str(), to_copy.path.c_str(),
@ -2540,7 +2554,7 @@ added_page(QPDF& pdf, QPDFPageObjectHelper page)
void void
QPDFJob::handlePageSpecs( QPDFJob::handlePageSpecs(
QPDF& pdf, std::vector<std::shared_ptr<QPDF>>& page_heap) QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap)
{ {
// Parse all page specifications and translate them into lists of // Parse all page specifications and translate them into lists of
// actual pages. // actual pages.
@ -2612,10 +2626,10 @@ QPDFJob::handlePageSpecs(
new FileInputSource(page_spec.filename.c_str()); new FileInputSource(page_spec.filename.c_str());
is = std::shared_ptr<InputSource>(fis); is = std::shared_ptr<InputSource>(fis);
} }
std::shared_ptr<QPDF> qpdf_sp; std::unique_ptr<QPDF> qpdf_sp;
processInputSource(qpdf_sp, is, password, true); processInputSource(qpdf_sp, is, password, true);
page_heap.push_back(qpdf_sp);
page_spec_qpdfs[page_spec.filename] = qpdf_sp.get(); page_spec_qpdfs[page_spec.filename] = qpdf_sp.get();
page_heap.push_back(std::move(qpdf_sp));
if (cis) { if (cis) {
cis->stayOpen(false); cis->stayOpen(false);
page_spec_cfis[page_spec.filename] = cis; page_spec_cfis[page_spec.filename] = cis;
@ -3116,7 +3130,7 @@ QPDFJob::setWriterOptions(QPDF& pdf, QPDFWriter& w)
w.setSuppressOriginalObjectIDs(true); w.setSuppressOriginalObjectIDs(true);
} }
if (m->copy_encryption) { if (m->copy_encryption) {
std::shared_ptr<QPDF> encryption_pdf; std::unique_ptr<QPDF> encryption_pdf;
processFile( processFile(
encryption_pdf, encryption_pdf,
m->encryption_file.c_str(), m->encryption_file.c_str(),