Commit db2a22ead07b9036c4a42867cc15d72f7d9d4203
1 parent
de2e46cc
Refactor `Writer` and `Members` in `QPDFWriter`: consolidate member variables in…
…to `impl::Writer`, streamline initialization, and enhance encapsulation.
Showing
1 changed file
with
73 additions
and
72 deletions
libqpdf/QPDFWriter.cc
| @@ -267,9 +267,35 @@ namespace qpdf::impl | @@ -267,9 +267,35 @@ namespace qpdf::impl | ||
| 267 | class Writer: protected Doc::Common | 267 | class Writer: protected Doc::Common |
| 268 | { | 268 | { |
| 269 | public: | 269 | public: |
| 270 | - Writer(QPDF& qpdf) : | 270 | + // flags used by unparseObject |
| 271 | + static int const f_stream = 1 << 0; | ||
| 272 | + static int const f_filtered = 1 << 1; | ||
| 273 | + static int const f_in_ostream = 1 << 2; | ||
| 274 | + static int const f_hex_string = 1 << 3; | ||
| 275 | + static int const f_no_encryption = 1 << 4; | ||
| 276 | + | ||
| 277 | + enum trailer_e { t_normal, t_lin_first, t_lin_second }; | ||
| 278 | + | ||
| 279 | + Writer() = delete; | ||
| 280 | + Writer(Writer const&) = delete; | ||
| 281 | + Writer(Writer&&) = delete; | ||
| 282 | + Writer& operator=(Writer const&) = delete; | ||
| 283 | + Writer& operator=(Writer&&) = delete; | ||
| 284 | + ~Writer() | ||
| 285 | + { | ||
| 286 | + if (file && close_file) { | ||
| 287 | + fclose(file); | ||
| 288 | + } | ||
| 289 | + delete output_buffer; | ||
| 290 | + } | ||
| 291 | + Writer(QPDF& qpdf, QPDFWriter& w) : | ||
| 271 | Common(qpdf.doc()), | 292 | Common(qpdf.doc()), |
| 272 | - lin(qpdf.doc().linearization()) | 293 | + lin(qpdf.doc().linearization()), |
| 294 | + w(w), | ||
| 295 | + root_og( | ||
| 296 | + qpdf.getRoot().getObjGen().isIndirect() ? qpdf.getRoot().getObjGen() | ||
| 297 | + : QPDFObjGen(-1, 0)), | ||
| 298 | + pipeline_stack(pipeline) | ||
| 273 | { | 299 | { |
| 274 | } | 300 | } |
| 275 | 301 | ||
| @@ -277,6 +303,50 @@ namespace qpdf::impl | @@ -277,6 +303,50 @@ namespace qpdf::impl | ||
| 277 | Doc::Linearization& lin; | 303 | Doc::Linearization& lin; |
| 278 | 304 | ||
| 279 | qpdf::Writer::Config cfg; | 305 | qpdf::Writer::Config cfg; |
| 306 | + | ||
| 307 | + QPDFWriter& w; | ||
| 308 | + QPDFObjGen root_og{-1, 0}; | ||
| 309 | + char const* filename{"unspecified"}; | ||
| 310 | + FILE* file{nullptr}; | ||
| 311 | + bool close_file{false}; | ||
| 312 | + std::unique_ptr<Pl_Buffer> buffer_pipeline{nullptr}; | ||
| 313 | + Buffer* output_buffer{nullptr}; | ||
| 314 | + | ||
| 315 | + std::unique_ptr<QPDF::Doc::Encryption> encryption; | ||
| 316 | + std::string encryption_key; | ||
| 317 | + | ||
| 318 | + std::string id1; // for /ID key of | ||
| 319 | + std::string id2; // trailer dictionary | ||
| 320 | + std::string final_pdf_version; | ||
| 321 | + int final_extension_level{0}; | ||
| 322 | + std::string min_pdf_version; | ||
| 323 | + int min_extension_level{0}; | ||
| 324 | + int encryption_dict_objid{0}; | ||
| 325 | + std::string cur_data_key; | ||
| 326 | + std::unique_ptr<Pipeline> file_pl; | ||
| 327 | + qpdf::pl::Count* pipeline{nullptr}; | ||
| 328 | + std::vector<QPDFObjectHandle> object_queue; | ||
| 329 | + size_t object_queue_front{0}; | ||
| 330 | + QPDFWriter::ObjTable obj; | ||
| 331 | + QPDFWriter::NewObjTable new_obj; | ||
| 332 | + int next_objid{1}; | ||
| 333 | + int cur_stream_length_id{0}; | ||
| 334 | + size_t cur_stream_length{0}; | ||
| 335 | + bool added_newline{false}; | ||
| 336 | + size_t max_ostream_index{0}; | ||
| 337 | + std::set<QPDFObjGen> normalized_streams; | ||
| 338 | + std::map<QPDFObjGen, int> page_object_to_seq; | ||
| 339 | + std::map<QPDFObjGen, int> contents_to_page_seq; | ||
| 340 | + std::map<int, std::vector<QPDFObjGen>> object_stream_to_objects; | ||
| 341 | + Pl_stack pipeline_stack; | ||
| 342 | + std::string deterministic_id_data; | ||
| 343 | + bool did_write_setup{false}; | ||
| 344 | + | ||
| 345 | + // For progress reporting | ||
| 346 | + std::shared_ptr<QPDFWriter::ProgressReporter> progress_reporter; | ||
| 347 | + int events_expected{0}; | ||
| 348 | + int events_seen{0}; | ||
| 349 | + int next_progress_report{0}; | ||
| 280 | }; | 350 | }; |
| 281 | } // namespace qpdf::impl | 351 | } // namespace qpdf::impl |
| 282 | 352 | ||
| @@ -285,35 +355,11 @@ class QPDFWriter::Members: impl::Writer | @@ -285,35 +355,11 @@ class QPDFWriter::Members: impl::Writer | ||
| 285 | friend class QPDFWriter; | 355 | friend class QPDFWriter; |
| 286 | 356 | ||
| 287 | public: | 357 | public: |
| 288 | - // flags used by unparseObject | ||
| 289 | - static int const f_stream = 1 << 0; | ||
| 290 | - static int const f_filtered = 1 << 1; | ||
| 291 | - static int const f_in_ostream = 1 << 2; | ||
| 292 | - static int const f_hex_string = 1 << 3; | ||
| 293 | - static int const f_no_encryption = 1 << 4; | ||
| 294 | - | ||
| 295 | - enum trailer_e { t_normal, t_lin_first, t_lin_second }; | ||
| 296 | - | ||
| 297 | Members(QPDFWriter& w, QPDF& qpdf) : | 358 | Members(QPDFWriter& w, QPDF& qpdf) : |
| 298 | - impl::Writer(qpdf), | ||
| 299 | - w(w), | ||
| 300 | - root_og( | ||
| 301 | - qpdf.getRoot().getObjGen().isIndirect() ? qpdf.getRoot().getObjGen() | ||
| 302 | - : QPDFObjGen(-1, 0)), | ||
| 303 | - pipeline_stack(pipeline) | 359 | + impl::Writer(qpdf, w) |
| 304 | { | 360 | { |
| 305 | } | 361 | } |
| 306 | 362 | ||
| 307 | - Members(Members const&) = delete; | ||
| 308 | - | ||
| 309 | - ~Members() | ||
| 310 | - { | ||
| 311 | - if (file && close_file) { | ||
| 312 | - fclose(file); | ||
| 313 | - } | ||
| 314 | - delete output_buffer; | ||
| 315 | - } | ||
| 316 | - | ||
| 317 | void write(); | 363 | void write(); |
| 318 | std::map<QPDFObjGen, QPDFXRefEntry> getWrittenXRefTable(); | 364 | std::map<QPDFObjGen, QPDFXRefEntry> getWrittenXRefTable(); |
| 319 | void setMinimumPDFVersion(std::string const& version, int extension_level); | 365 | void setMinimumPDFVersion(std::string const& version, int extension_level); |
| @@ -430,51 +476,6 @@ class QPDFWriter::Members: impl::Writer | @@ -430,51 +476,6 @@ class QPDFWriter::Members: impl::Writer | ||
| 430 | 476 | ||
| 431 | void adjustAESStreamLength(size_t& length); | 477 | void adjustAESStreamLength(size_t& length); |
| 432 | void computeDeterministicIDData(); | 478 | void computeDeterministicIDData(); |
| 433 | - | ||
| 434 | - private: | ||
| 435 | - QPDFWriter& w; | ||
| 436 | - QPDFObjGen root_og{-1, 0}; | ||
| 437 | - char const* filename{"unspecified"}; | ||
| 438 | - FILE* file{nullptr}; | ||
| 439 | - bool close_file{false}; | ||
| 440 | - std::unique_ptr<Pl_Buffer> buffer_pipeline{nullptr}; | ||
| 441 | - Buffer* output_buffer{nullptr}; | ||
| 442 | - | ||
| 443 | - std::unique_ptr<QPDF::Doc::Encryption> encryption; | ||
| 444 | - std::string encryption_key; | ||
| 445 | - | ||
| 446 | - std::string id1; // for /ID key of | ||
| 447 | - std::string id2; // trailer dictionary | ||
| 448 | - std::string final_pdf_version; | ||
| 449 | - int final_extension_level{0}; | ||
| 450 | - std::string min_pdf_version; | ||
| 451 | - int min_extension_level{0}; | ||
| 452 | - int encryption_dict_objid{0}; | ||
| 453 | - std::string cur_data_key; | ||
| 454 | - std::unique_ptr<Pipeline> file_pl; | ||
| 455 | - qpdf::pl::Count* pipeline{nullptr}; | ||
| 456 | - std::vector<QPDFObjectHandle> object_queue; | ||
| 457 | - size_t object_queue_front{0}; | ||
| 458 | - QPDFWriter::ObjTable obj; | ||
| 459 | - QPDFWriter::NewObjTable new_obj; | ||
| 460 | - int next_objid{1}; | ||
| 461 | - int cur_stream_length_id{0}; | ||
| 462 | - size_t cur_stream_length{0}; | ||
| 463 | - bool added_newline{false}; | ||
| 464 | - size_t max_ostream_index{0}; | ||
| 465 | - std::set<QPDFObjGen> normalized_streams; | ||
| 466 | - std::map<QPDFObjGen, int> page_object_to_seq; | ||
| 467 | - std::map<QPDFObjGen, int> contents_to_page_seq; | ||
| 468 | - std::map<int, std::vector<QPDFObjGen>> object_stream_to_objects; | ||
| 469 | - Pl_stack pipeline_stack; | ||
| 470 | - std::string deterministic_id_data; | ||
| 471 | - bool did_write_setup{false}; | ||
| 472 | - | ||
| 473 | - // For progress reporting | ||
| 474 | - std::shared_ptr<QPDFWriter::ProgressReporter> progress_reporter; | ||
| 475 | - int events_expected{0}; | ||
| 476 | - int events_seen{0}; | ||
| 477 | - int next_progress_report{0}; | ||
| 478 | }; | 479 | }; |
| 479 | 480 | ||
| 480 | QPDFWriter::QPDFWriter(QPDF& pdf) : | 481 | QPDFWriter::QPDFWriter(QPDF& pdf) : |