From 8ab52fa55808541a25dffa15532dd19b2e3084b0 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 21 Aug 2017 20:56:14 -0400 Subject: Combine writePCLm with writeStandard Reduce code duplication --- libqpdf/QPDFWriter.cc | 125 +++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 78 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index a57d71f5..318c2fb7 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -2449,10 +2449,6 @@ QPDFWriter::write() { writeLinearized(); } - else if (this->pclm) - { - writePCLm(); - } else { writeStandard(); @@ -3160,18 +3156,8 @@ QPDFWriter::writeLinearized() } void -QPDFWriter::writeStandard() +QPDFWriter::enqueueObjectsStandard() { - if (this->deterministic_id) - { - pushMD5Pipeline(); - } - - // Start writing - - writeHeader(); - writeString(this->extra_header_text); - if (this->preserve_unreferenced_objects) { QTC::TC("qpdf", "QPDFWriter preserve unreferenced standard"); @@ -3197,61 +3183,11 @@ QPDFWriter::writeStandard() { enqueueObject(trailer.getKey(*iter)); } - - // Now start walking queue, output each object - while (this->object_queue.size()) - { - QPDFObjectHandle cur_object = this->object_queue.front(); - this->object_queue.pop_front(); - writeObject(cur_object); - } - - // Write out the encryption dictionary, if any - if (this->encrypted) - { - writeEncryptionDictionary(); - } - - // Now write out xref. next_objid is now the number of objects. - qpdf_offset_t xref_offset = this->pipeline->getCount(); - if (this->object_stream_to_objects.empty()) - { - // Write regular cross-reference table - writeXRefTable(t_normal, 0, this->next_objid - 1, this->next_objid); - } - else - { - // Write cross-reference stream. - int xref_id = this->next_objid++; - writeXRefStream(xref_id, xref_id, xref_offset, t_normal, - 0, this->next_objid - 1, this->next_objid); - } - writeString("startxref\n"); - writeString(QUtil::int_to_string(xref_offset)); - writeString("\n%%EOF\n"); - - if (this->deterministic_id) - { - QTC::TC("qpdf", "QPDFWriter standard deterministic ID", - this->object_stream_to_objects.empty() ? 0 : 1); - popPipelineStack(); - assert(this->md5_pipeline == 0); - } } void -QPDFWriter::writePCLm() +QPDFWriter::enqueueObjectsPCLm() { - if (this->deterministic_id) - { - pushMD5Pipeline(); - } - - // Start writing - - writeHeader(); - writeString(this->extra_header_text); - // Image transform stream content for page strip images. // Each of this new stream has to come after every page image // strip written in the pclm file. @@ -3260,7 +3196,7 @@ QPDFWriter::writePCLm() // enqueue all pages first std::vector all = this->pdf.getAllPages(); for (std::vector::iterator iter = all.begin(); - iter != all.end(); ++iter) + iter != all.end(); ++iter) { // enqueue page enqueueObject(*iter); @@ -3273,7 +3209,7 @@ QPDFWriter::writePCLm() (*iter).getKey("/Resources").getKey("/XObject"); std::set keys = strips.getKeys(); for (std::set::iterator image = keys.begin(); - image != keys.end(); ++image) + image != keys.end(); ++image) { enqueueObject(strips.getKey(*image)); enqueueObject(QPDFObjectHandle::newStream( @@ -3284,28 +3220,59 @@ QPDFWriter::writePCLm() // Put root in queue. QPDFObjectHandle trailer = getTrimmedTrailer(); enqueueObject(trailer.getKey("/Root")); +} + +void +QPDFWriter::writeStandard() +{ + if (this->deterministic_id) + { + pushMD5Pipeline(); + } + + // Start writing + + writeHeader(); + writeString(this->extra_header_text); - // Now start walking queue, output each object + if (this->pclm) + { + enqueueObjectsPCLm(); + } + else + { + enqueueObjectsStandard(); + } + + // Now start walking queue, outputing each object. There shouldn't + // really be any here, but this will catch anything that somehow + // got missed. while (this->object_queue.size()) { - QPDFObjectHandle cur_object = this->object_queue.front(); - this->object_queue.pop_front(); - writeObject(cur_object); + QPDFObjectHandle cur_object = this->object_queue.front(); + this->object_queue.pop_front(); + writeObject(cur_object); + } + + // Write out the encryption dictionary, if any + if (this->encrypted) + { + writeEncryptionDictionary(); } // Now write out xref. next_objid is now the number of objects. qpdf_offset_t xref_offset = this->pipeline->getCount(); if (this->object_stream_to_objects.empty()) { - // Write regular cross-reference table - writeXRefTable(t_normal, 0, this->next_objid - 1, this->next_objid); + // Write regular cross-reference table + writeXRefTable(t_normal, 0, this->next_objid - 1, this->next_objid); } else { - // Write cross-reference stream. - int xref_id = this->next_objid++; - writeXRefStream(xref_id, xref_id, xref_offset, t_normal, - 0, this->next_objid - 1, this->next_objid); + // Write cross-reference stream. + int xref_id = this->next_objid++; + writeXRefStream(xref_id, xref_id, xref_offset, t_normal, + 0, this->next_objid - 1, this->next_objid); } writeString("startxref\n"); writeString(QUtil::int_to_string(xref_offset)); @@ -3313,6 +3280,8 @@ QPDFWriter::writePCLm() if (this->deterministic_id) { + QTC::TC("qpdf", "QPDFWriter standard deterministic ID", + this->object_stream_to_objects.empty() ? 0 : 1); popPipelineStack(); assert(this->md5_pipeline == 0); } -- cgit v1.2.3-54-g00ecf