DocumentParser.h 3.7 KB

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