From db2a22ead07b9036c4a42867cc15d72f7d9d4203 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 18 Oct 2025 12:06:41 +0100 Subject: [PATCH] Refactor `Writer` and `Members` in `QPDFWriter`: consolidate member variables into `impl::Writer`, streamline initialization, and enhance encapsulation. --- libqpdf/QPDFWriter.cc | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------ 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index d1de01b..55d7656 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -267,9 +267,35 @@ namespace qpdf::impl class Writer: protected Doc::Common { public: - Writer(QPDF& qpdf) : + // flags used by unparseObject + static int const f_stream = 1 << 0; + static int const f_filtered = 1 << 1; + static int const f_in_ostream = 1 << 2; + static int const f_hex_string = 1 << 3; + static int const f_no_encryption = 1 << 4; + + enum trailer_e { t_normal, t_lin_first, t_lin_second }; + + Writer() = delete; + Writer(Writer const&) = delete; + Writer(Writer&&) = delete; + Writer& operator=(Writer const&) = delete; + Writer& operator=(Writer&&) = delete; + ~Writer() + { + if (file && close_file) { + fclose(file); + } + delete output_buffer; + } + Writer(QPDF& qpdf, QPDFWriter& w) : Common(qpdf.doc()), - lin(qpdf.doc().linearization()) + lin(qpdf.doc().linearization()), + w(w), + root_og( + qpdf.getRoot().getObjGen().isIndirect() ? qpdf.getRoot().getObjGen() + : QPDFObjGen(-1, 0)), + pipeline_stack(pipeline) { } @@ -277,6 +303,50 @@ namespace qpdf::impl Doc::Linearization& lin; qpdf::Writer::Config cfg; + + QPDFWriter& w; + QPDFObjGen root_og{-1, 0}; + char const* filename{"unspecified"}; + FILE* file{nullptr}; + bool close_file{false}; + std::unique_ptr buffer_pipeline{nullptr}; + Buffer* output_buffer{nullptr}; + + std::unique_ptr encryption; + std::string encryption_key; + + std::string id1; // for /ID key of + std::string id2; // trailer dictionary + std::string final_pdf_version; + int final_extension_level{0}; + std::string min_pdf_version; + int min_extension_level{0}; + int encryption_dict_objid{0}; + std::string cur_data_key; + std::unique_ptr file_pl; + qpdf::pl::Count* pipeline{nullptr}; + std::vector object_queue; + size_t object_queue_front{0}; + QPDFWriter::ObjTable obj; + QPDFWriter::NewObjTable new_obj; + int next_objid{1}; + int cur_stream_length_id{0}; + size_t cur_stream_length{0}; + bool added_newline{false}; + size_t max_ostream_index{0}; + std::set normalized_streams; + std::map page_object_to_seq; + std::map contents_to_page_seq; + std::map> object_stream_to_objects; + Pl_stack pipeline_stack; + std::string deterministic_id_data; + bool did_write_setup{false}; + + // For progress reporting + std::shared_ptr progress_reporter; + int events_expected{0}; + int events_seen{0}; + int next_progress_report{0}; }; } // namespace qpdf::impl @@ -285,35 +355,11 @@ class QPDFWriter::Members: impl::Writer friend class QPDFWriter; public: - // flags used by unparseObject - static int const f_stream = 1 << 0; - static int const f_filtered = 1 << 1; - static int const f_in_ostream = 1 << 2; - static int const f_hex_string = 1 << 3; - static int const f_no_encryption = 1 << 4; - - enum trailer_e { t_normal, t_lin_first, t_lin_second }; - Members(QPDFWriter& w, QPDF& qpdf) : - impl::Writer(qpdf), - w(w), - root_og( - qpdf.getRoot().getObjGen().isIndirect() ? qpdf.getRoot().getObjGen() - : QPDFObjGen(-1, 0)), - pipeline_stack(pipeline) + impl::Writer(qpdf, w) { } - Members(Members const&) = delete; - - ~Members() - { - if (file && close_file) { - fclose(file); - } - delete output_buffer; - } - void write(); std::map getWrittenXRefTable(); void setMinimumPDFVersion(std::string const& version, int extension_level); @@ -430,51 +476,6 @@ class QPDFWriter::Members: impl::Writer void adjustAESStreamLength(size_t& length); void computeDeterministicIDData(); - - private: - QPDFWriter& w; - QPDFObjGen root_og{-1, 0}; - char const* filename{"unspecified"}; - FILE* file{nullptr}; - bool close_file{false}; - std::unique_ptr buffer_pipeline{nullptr}; - Buffer* output_buffer{nullptr}; - - std::unique_ptr encryption; - std::string encryption_key; - - std::string id1; // for /ID key of - std::string id2; // trailer dictionary - std::string final_pdf_version; - int final_extension_level{0}; - std::string min_pdf_version; - int min_extension_level{0}; - int encryption_dict_objid{0}; - std::string cur_data_key; - std::unique_ptr file_pl; - qpdf::pl::Count* pipeline{nullptr}; - std::vector object_queue; - size_t object_queue_front{0}; - QPDFWriter::ObjTable obj; - QPDFWriter::NewObjTable new_obj; - int next_objid{1}; - int cur_stream_length_id{0}; - size_t cur_stream_length{0}; - bool added_newline{false}; - size_t max_ostream_index{0}; - std::set normalized_streams; - std::map page_object_to_seq; - std::map contents_to_page_seq; - std::map> object_stream_to_objects; - Pl_stack pipeline_stack; - std::string deterministic_id_data; - bool did_write_setup{false}; - - // For progress reporting - std::shared_ptr progress_reporter; - int events_expected{0}; - int events_seen{0}; - int next_progress_report{0}; }; QPDFWriter::QPDFWriter(QPDF& pdf) : -- libgit2 0.21.4