DocumentParser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibPDF/Parser.h>
  8. namespace PDF {
  9. struct Version {
  10. int major { 0 };
  11. int minor { 0 };
  12. };
  13. class DocumentParser final : public RefCounted<DocumentParser>
  14. , public Parser {
  15. public:
  16. static PDFErrorOr<size_t> scan_for_header_start(ReadonlyBytes);
  17. DocumentParser(Document*, ReadonlyBytes);
  18. enum class LinearizationResult {
  19. NotLinearized,
  20. Linearized,
  21. };
  22. [[nodiscard]] ALWAYS_INLINE RefPtr<DictObject> const& trailer() const { return m_xref_table->trailer(); }
  23. // Parses the header and initializes the xref table and trailer
  24. PDFErrorOr<Version> initialize();
  25. bool can_resolve_references() { return m_xref_table; }
  26. PDFErrorOr<Value> parse_object_with_index(u32 index);
  27. // Specialized version of parse_dict which aborts early if the dict being parsed
  28. // is not a page object
  29. PDFErrorOr<RefPtr<DictObject>> conditionally_parse_page_tree_node(u32 object_index);
  30. private:
  31. struct LinearizationDictionary {
  32. u32 length_of_file { 0 };
  33. u32 primary_hint_stream_offset { 0 };
  34. u32 primary_hint_stream_length { 0 };
  35. u32 overflow_hint_stream_offset { 0 };
  36. u32 overflow_hint_stream_length { 0 };
  37. u32 first_page_object_number { 0 };
  38. u32 offset_of_first_page_end { 0 };
  39. u16 number_of_pages { 0 };
  40. u32 offset_of_main_xref_table { 0 };
  41. u32 first_page { 0 }; // The page to initially open (I think, the spec isn't all that clear here)
  42. };
  43. struct PageOffsetHintTable {
  44. u32 least_number_of_objects_in_a_page { 0 };
  45. u32 location_of_first_page_object { 0 };
  46. u16 bits_required_for_object_number { 0 };
  47. u32 least_length_of_a_page { 0 };
  48. u16 bits_required_for_page_length { 0 };
  49. u32 least_offset_of_any_content_stream { 0 };
  50. u16 bits_required_for_content_stream_offsets { 0 };
  51. u32 least_content_stream_length { 0 };
  52. u16 bits_required_for_content_stream_length { 0 };
  53. u16 bits_required_for_number_of_shared_obj_refs { 0 };
  54. u16 bits_required_for_greatest_shared_obj_identifier { 0 };
  55. u16 bits_required_for_fraction_numerator { 0 };
  56. u16 shared_object_reference_fraction_denominator { 0 };
  57. };
  58. struct PageOffsetHintTableEntry {
  59. u32 objects_in_page_number { 0 };
  60. u32 page_length_number { 0 };
  61. u32 number_of_shared_objects { 0 };
  62. Vector<u32> shared_object_identifiers {};
  63. Vector<u32> shared_object_location_numerators {};
  64. u32 page_content_stream_offset_number { 0 };
  65. u32 page_content_stream_length_number { 0 };
  66. };
  67. friend struct AK::Formatter<LinearizationDictionary>;
  68. friend struct AK::Formatter<PageOffsetHintTable>;
  69. friend struct AK::Formatter<PageOffsetHintTableEntry>;
  70. PDFErrorOr<Version> parse_header();
  71. PDFErrorOr<LinearizationResult> initialize_linearization_dict();
  72. PDFErrorOr<void> initialize_linearized_xref_table();
  73. PDFErrorOr<void> initialize_non_linearized_xref_table();
  74. PDFErrorOr<void> validate_xref_table_and_fix_if_necessary();
  75. PDFErrorOr<void> initialize_hint_tables();
  76. PDFErrorOr<PageOffsetHintTable> parse_page_offset_hint_table(ReadonlyBytes hint_stream_bytes);
  77. PDFErrorOr<Vector<PageOffsetHintTableEntry>> parse_all_page_offset_hint_table_entries(PageOffsetHintTable const&, ReadonlyBytes hint_stream_bytes);
  78. PDFErrorOr<NonnullRefPtr<XRefTable>> parse_xref_stream();
  79. PDFErrorOr<NonnullRefPtr<XRefTable>> parse_xref_table();
  80. PDFErrorOr<NonnullRefPtr<DictObject>> parse_file_trailer();
  81. PDFErrorOr<Value> parse_compressed_object_with_index(u32 index);
  82. bool navigate_to_before_eof_marker();
  83. bool navigate_to_after_startxref();
  84. RefPtr<XRefTable> m_xref_table;
  85. Optional<LinearizationDictionary> m_linearization_dictionary;
  86. };
  87. }