From 759c56e1fed2849b77bff18f2a50639876395e5e Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Wed, 10 Aug 2011 16:34:29 -0400 Subject: implement ability to save PDF to memory, also update ChangeLog --- libqpdf/QPDFWriter.cc | 121 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 34 deletions(-) (limited to 'libqpdf/QPDFWriter.cc') diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 40368f69..7217ded7 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -21,32 +20,65 @@ #include +QPDFWriter::QPDFWriter(QPDF& pdf) : + pdf(pdf) +{ + init(); +} + QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) : - pdf(pdf), - filename(filename), - file(0), - close_file(false), - normalize_content_set(false), - normalize_content(false), - stream_data_mode_set(false), - stream_data_mode(qpdf_s_compress), - qdf_mode(false), - static_id(false), - suppress_original_object_ids(false), - direct_stream_lengths(true), - encrypted(false), - preserve_encryption(true), - linearized(false), - object_stream_mode(qpdf_o_preserve), - encrypt_metadata(true), - encrypt_use_aes(false), - encryption_dict_objid(0), - next_objid(1), - cur_stream_length_id(0), - cur_stream_length(0), - added_newline(false), - max_ostream_index(0) + pdf(pdf) +{ + init(); + setOutputFilename(filename); +} + +void +QPDFWriter::init() +{ + filename = 0; + file = 0; + close_file = false; + buffer_pipeline = 0; + output_buffer = 0; + normalize_content_set = false; + normalize_content = false; + stream_data_mode_set = false; + stream_data_mode = qpdf_s_compress; + qdf_mode = false; + static_id = false; + suppress_original_object_ids = false; + direct_stream_lengths = true; + encrypted = false; + preserve_encryption = true; + linearized = false; + object_stream_mode = qpdf_o_preserve; + encrypt_metadata = true; + encrypt_use_aes = false; + encryption_dict_objid = 0; + next_objid = 1; + cur_stream_length_id = 0; + cur_stream_length = 0; + added_newline = false; + max_ostream_index = 0; +} + +QPDFWriter::~QPDFWriter() +{ + if (file && close_file) + { + fclose(file); + } + if (output_buffer) + { + delete output_buffer; + } +} + +void +QPDFWriter::setOutputFilename(char const* filename) { + this->filename = filename; if (filename == 0) { this->filename = "standard output"; @@ -61,19 +93,25 @@ QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) : fopen(filename, "wb+")); close_file = true; } - Pipeline* p = new Pl_StdioFile("qdf output", file); + Pipeline* p = new Pl_StdioFile("qpdf output", file); to_delete.push_back(p); - pipeline = new Pl_Count("qdf count", p); - to_delete.push_back(pipeline); - pipeline_stack.push_back(pipeline); + initializePipelineStack(p); } -QPDFWriter::~QPDFWriter() +void +QPDFWriter::setOutputMemory() { - if (file && close_file) - { - fclose(file); - } + this->buffer_pipeline = new Pl_Buffer("qpdf output"); + to_delete.push_back(this->buffer_pipeline); + initializePipelineStack(this->buffer_pipeline); +} + +Buffer* +QPDFWriter::getBuffer() +{ + Buffer* result = this->output_buffer; + this->output_buffer = 0; + return result; } void @@ -565,6 +603,14 @@ QPDFWriter::pushPipeline(Pipeline* p) return p; } +void +QPDFWriter::initializePipelineStack(Pipeline *p) +{ + this->pipeline = new Pl_Count("qpdf count", p); + to_delete.push_back(this->pipeline); + this->pipeline_stack.push_back(this->pipeline); +} + void QPDFWriter::activatePipelineStack() { @@ -1503,6 +1549,8 @@ QPDFWriter::generateObjectStreams() void QPDFWriter::write() { + // XXX Check output + // Do preliminary setup if (this->linearized) @@ -1656,6 +1704,11 @@ QPDFWriter::write() fclose(this->file); } this->file = 0; + if (this->buffer_pipeline) + { + this->output_buffer = this->buffer_pipeline->getBuffer(); + this->buffer_pipeline = 0; + } } void -- cgit v1.2.3-54-g00ecf