Document.cpp 10 KB

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