Document.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/CommonNames.h>
  7. #include <LibPDF/Document.h>
  8. #include <LibPDF/Parser.h>
  9. namespace PDF {
  10. String OutlineItem::to_string(int indent) const
  11. {
  12. auto indent_str = String::repeated(" ", indent + 1);
  13. StringBuilder child_builder;
  14. child_builder.append('[');
  15. for (auto& child : children)
  16. child_builder.appendff("{}\n", child.to_string(indent + 1));
  17. child_builder.appendff("{}]", indent_str);
  18. StringBuilder builder;
  19. builder.append("OutlineItem {{\n");
  20. builder.appendff("{}title={}\n", indent_str, title);
  21. builder.appendff("{}count={}\n", indent_str, count);
  22. builder.appendff("{}dest={}\n", indent_str, dest);
  23. builder.appendff("{}color={}\n", indent_str, color);
  24. builder.appendff("{}italic={}\n", indent_str, italic);
  25. builder.appendff("{}bold={}\n", indent_str, bold);
  26. builder.appendff("{}children={}\n", indent_str, child_builder.to_string());
  27. builder.appendff("{}}}", String::repeated(" ", indent));
  28. return builder.to_string();
  29. }
  30. RefPtr<Document> Document::create(const ReadonlyBytes& bytes)
  31. {
  32. auto parser = adopt_ref(*new Parser({}, bytes));
  33. auto document = adopt_ref(*new Document(parser));
  34. VERIFY(parser->perform_validation());
  35. auto xref_table_and_trailer_opt = parser->parse_last_xref_table_and_trailer();
  36. if (!xref_table_and_trailer_opt.has_value())
  37. return {};
  38. auto [xref_table, trailer] = xref_table_and_trailer_opt.value();
  39. document->m_xref_table = xref_table;
  40. document->m_trailer = trailer;
  41. document->m_catalog = document->m_trailer->get_dict(document, CommonNames::Root);
  42. document->build_page_tree();
  43. document->build_outline();
  44. return document;
  45. }
  46. Document::Document(const NonnullRefPtr<Parser>& parser)
  47. : m_parser(parser)
  48. {
  49. m_parser->set_document(this);
  50. }
  51. Value Document::get_or_load_value(u32 index)
  52. {
  53. auto value = get_value(index);
  54. if (value)
  55. return value;
  56. VERIFY(m_xref_table.has_object(index));
  57. auto byte_offset = m_xref_table.byte_offset_for_object(index);
  58. auto indirect_value = m_parser->parse_indirect_value_at_offset(byte_offset);
  59. VERIFY(indirect_value->index() == index);
  60. value = indirect_value->value();
  61. m_values.set(index, value);
  62. return value;
  63. }
  64. u32 Document::get_first_page_index() const
  65. {
  66. // FIXME: A PDF can have a different default first page, which
  67. // should be fetched and returned here
  68. return 0;
  69. }
  70. u32 Document::get_page_count() const
  71. {
  72. return m_page_object_indices.size();
  73. }
  74. Page Document::get_page(u32 index)
  75. {
  76. VERIFY(index < m_page_object_indices.size());
  77. auto cached_page = m_pages.get(index);
  78. if (cached_page.has_value())
  79. return cached_page.value();
  80. auto page_object_index = m_page_object_indices[index];
  81. auto raw_page_object = resolve_to<DictObject>(get_or_load_value(page_object_index));
  82. auto resources = raw_page_object->get_dict(this, CommonNames::Resources);
  83. auto contents = raw_page_object->get_object(this, CommonNames::Contents);
  84. auto media_box_array = raw_page_object->get_array(this, CommonNames::MediaBox);
  85. auto media_box = Rectangle {
  86. media_box_array->at(0).to_float(),
  87. media_box_array->at(1).to_float(),
  88. media_box_array->at(2).to_float(),
  89. media_box_array->at(3).to_float(),
  90. };
  91. auto crop_box = media_box;
  92. if (raw_page_object->contains(CommonNames::CropBox)) {
  93. auto crop_box_array = raw_page_object->get_array(this, CommonNames::CropBox);
  94. crop_box = Rectangle {
  95. crop_box_array->at(0).to_float(),
  96. crop_box_array->at(1).to_float(),
  97. crop_box_array->at(2).to_float(),
  98. crop_box_array->at(3).to_float(),
  99. };
  100. }
  101. float user_unit = 1.0f;
  102. if (raw_page_object->contains(CommonNames::UserUnit))
  103. user_unit = raw_page_object->get_value(CommonNames::UserUnit).to_float();
  104. int rotate = 0;
  105. if (raw_page_object->contains(CommonNames::Rotate)) {
  106. rotate = raw_page_object->get_value(CommonNames::Rotate).as_int();
  107. VERIFY(rotate % 90 == 0);
  108. }
  109. Page page { move(resources), move(contents), media_box, crop_box, user_unit, rotate };
  110. m_pages.set(index, page);
  111. return page;
  112. }
  113. Value Document::resolve(const Value& value)
  114. {
  115. if (value.is_ref()) {
  116. // FIXME: Surely indirect PDF objects can't contain another indirect PDF object,
  117. // right? Unsure from the spec, but if they can, these return values would have
  118. // to be wrapped with another resolve() call.
  119. return get_or_load_value(value.as_ref_index());
  120. }
  121. if (!value.is_object())
  122. return value;
  123. auto obj = value.as_object();
  124. if (obj->is_indirect_value())
  125. return static_cast<NonnullRefPtr<IndirectValue>>(obj)->value();
  126. return obj;
  127. }
  128. bool Document::build_page_tree()
  129. {
  130. if (!m_catalog->contains(CommonNames::Pages))
  131. return false;
  132. auto page_tree = m_catalog->get_dict(this, CommonNames::Pages);
  133. return add_page_tree_node_to_page_tree(page_tree);
  134. }
  135. bool Document::add_page_tree_node_to_page_tree(NonnullRefPtr<DictObject> page_tree)
  136. {
  137. if (!page_tree->contains(CommonNames::Kids) || !page_tree->contains(CommonNames::Count))
  138. return false;
  139. auto kids_array = page_tree->get_array(this, CommonNames::Kids);
  140. auto page_count = page_tree->get(CommonNames::Count).value().as_int();
  141. if (static_cast<size_t>(page_count) != kids_array->elements().size()) {
  142. // This page tree contains child page trees, so we recursively add
  143. // these pages to the overall page tree
  144. for (auto& value : *kids_array) {
  145. auto reference_index = value.as_ref_index();
  146. auto byte_offset = m_xref_table.byte_offset_for_object(reference_index);
  147. bool ok;
  148. auto maybe_page_tree_node = m_parser->conditionally_parse_page_tree_node_at_offset(byte_offset, ok);
  149. if (!ok)
  150. return false;
  151. if (maybe_page_tree_node) {
  152. if (!add_page_tree_node_to_page_tree(maybe_page_tree_node.release_nonnull()))
  153. return false;
  154. } else {
  155. m_page_object_indices.append(reference_index);
  156. }
  157. }
  158. } else {
  159. // We know all of the kids are leaf nodes
  160. for (auto& value : *kids_array)
  161. m_page_object_indices.append(value.as_ref_index());
  162. }
  163. return true;
  164. }
  165. void Document::build_outline()
  166. {
  167. if (!m_catalog->contains(CommonNames::Outlines))
  168. return;
  169. auto outline_dict = m_catalog->get_dict(this, CommonNames::Outlines);
  170. if (!outline_dict->contains(CommonNames::First))
  171. return;
  172. if (!outline_dict->contains(CommonNames::Last))
  173. return;
  174. auto first_ref = outline_dict->get_value(CommonNames::First);
  175. auto last_ref = outline_dict->get_value(CommonNames::Last);
  176. auto children = build_outline_item_chain(first_ref, last_ref);
  177. m_outline = adopt_ref(*new OutlineDict());
  178. m_outline->children = move(children);
  179. if (outline_dict->contains(CommonNames::Count))
  180. m_outline->count = outline_dict->get_value(CommonNames::Count).as_int();
  181. }
  182. NonnullRefPtr<OutlineItem> Document::build_outline_item(NonnullRefPtr<DictObject> outline_item_dict)
  183. {
  184. auto outline_item = adopt_ref(*new OutlineItem {});
  185. if (outline_item_dict->contains(CommonNames::First)) {
  186. VERIFY(outline_item_dict->contains(CommonNames::Last));
  187. auto first_ref = outline_item_dict->get_value(CommonNames::First);
  188. auto last_ref = outline_item_dict->get_value(CommonNames::Last);
  189. auto children = build_outline_item_chain(first_ref, last_ref);
  190. outline_item->children = move(children);
  191. }
  192. outline_item->title = outline_item_dict->get_string(this, CommonNames::Title)->string();
  193. if (outline_item_dict->contains(CommonNames::Count))
  194. outline_item->count = outline_item_dict->get_value(CommonNames::Count).as_int();
  195. if (outline_item_dict->contains(CommonNames::Dest)) {
  196. auto dest_arr = outline_item_dict->get_array(this, CommonNames::Dest);
  197. auto page_ref = dest_arr->at(0);
  198. auto type_name = dest_arr->get_name_at(this, 1)->name();
  199. Vector<float> parameters;
  200. for (size_t i = 2; i < dest_arr->size(); i++)
  201. parameters.append(dest_arr->at(i).to_float());
  202. Destination::Type type;
  203. if (type_name == CommonNames::XYZ) {
  204. type = Destination::Type::XYZ;
  205. } else if (type_name == CommonNames::Fit) {
  206. type = Destination::Type::Fit;
  207. } else if (type_name == CommonNames::FitH) {
  208. type = Destination::Type::FitH;
  209. } else if (type_name == CommonNames::FitV) {
  210. type = Destination::Type::FitV;
  211. } else if (type_name == CommonNames::FitR) {
  212. type = Destination::Type::FitR;
  213. } else if (type_name == CommonNames::FitB) {
  214. type = Destination::Type::FitB;
  215. } else if (type_name == CommonNames::FitBH) {
  216. type = Destination::Type::FitBH;
  217. } else if (type_name == CommonNames::FitBV) {
  218. type = Destination::Type::FitBV;
  219. } else {
  220. VERIFY_NOT_REACHED();
  221. }
  222. outline_item->dest = Destination { type, page_ref, parameters };
  223. }
  224. if (outline_item_dict->contains(CommonNames::C)) {
  225. auto color_array = outline_item_dict->get_array(this, CommonNames::C);
  226. auto r = static_cast<int>(255.0f * color_array->at(0).as_float());
  227. auto g = static_cast<int>(255.0f * color_array->at(1).as_float());
  228. auto b = static_cast<int>(255.0f * color_array->at(2).as_float());
  229. outline_item->color = Color(r, g, b);
  230. }
  231. if (outline_item_dict->contains(CommonNames::F)) {
  232. auto bitfield = outline_item_dict->get_value(CommonNames::F).as_int();
  233. outline_item->italic = bitfield & 0x1;
  234. outline_item->bold = bitfield & 0x2;
  235. }
  236. return outline_item;
  237. }
  238. NonnullRefPtrVector<OutlineItem> Document::build_outline_item_chain(const Value& first_ref, const Value& last_ref)
  239. {
  240. VERIFY(first_ref.is_ref());
  241. VERIFY(last_ref.is_ref());
  242. NonnullRefPtrVector<OutlineItem> children;
  243. auto first_dict = object_cast<DictObject>(get_or_load_value(first_ref.as_ref_index()).as_object());
  244. auto first = build_outline_item(first_dict);
  245. children.append(first);
  246. auto current_child_dict = first_dict;
  247. u32 current_child_index = first_ref.as_ref_index();
  248. while (current_child_dict->contains(CommonNames::Next)) {
  249. auto next_child_dict_ref = current_child_dict->get_value(CommonNames::Next);
  250. current_child_index = next_child_dict_ref.as_ref_index();
  251. auto next_child_dict = object_cast<DictObject>(get_or_load_value(current_child_index).as_object());
  252. auto next_child = build_outline_item(next_child_dict);
  253. children.append(next_child);
  254. current_child_dict = next_child_dict;
  255. }
  256. VERIFY(last_ref.as_ref_index() == current_child_index);
  257. return children;
  258. }
  259. }