Document.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright (c) 2021-2022, 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. DeprecatedString OutlineItem::to_deprecated_string(int indent) const
  11. {
  12. auto indent_str = DeprecatedString::repeated(" "sv, indent + 1);
  13. StringBuilder child_builder;
  14. child_builder.append('[');
  15. for (auto& child : children)
  16. child_builder.appendff("{}\n", child->to_deprecated_string(indent + 1));
  17. child_builder.appendff("{}]", indent_str);
  18. StringBuilder builder;
  19. builder.append("OutlineItem {{\n"sv);
  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_deprecated_string());
  27. builder.appendff("{}}}", DeprecatedString::repeated(" "sv, indent));
  28. return builder.to_deprecated_string();
  29. }
  30. PDFErrorOr<Optional<DeprecatedString>> InfoDict::title() const
  31. {
  32. return get(CommonNames::Title);
  33. }
  34. PDFErrorOr<Optional<DeprecatedString>> InfoDict::author() const
  35. {
  36. return get(CommonNames::Author);
  37. }
  38. PDFErrorOr<Optional<DeprecatedString>> InfoDict::subject() const
  39. {
  40. return get(CommonNames::Subject);
  41. }
  42. PDFErrorOr<Optional<DeprecatedString>> InfoDict::keywords() const
  43. {
  44. return get(CommonNames::Keywords);
  45. }
  46. PDFErrorOr<Optional<DeprecatedString>> InfoDict::creator() const
  47. {
  48. return get(CommonNames::Creator);
  49. }
  50. PDFErrorOr<Optional<DeprecatedString>> InfoDict::producer() const
  51. {
  52. return get(CommonNames::Producer);
  53. }
  54. PDFErrorOr<Optional<DeprecatedString>> InfoDict::creation_date() const
  55. {
  56. return get(CommonNames::CreationDate);
  57. }
  58. PDFErrorOr<Optional<DeprecatedString>> InfoDict::modification_date() const
  59. {
  60. return get(CommonNames::ModDate);
  61. }
  62. PDFErrorOr<NonnullRefPtr<Document>> Document::create(ReadonlyBytes bytes)
  63. {
  64. auto parser = adopt_ref(*new DocumentParser({}, bytes));
  65. auto document = adopt_ref(*new Document(parser));
  66. document->m_version = TRY(parser->initialize());
  67. document->m_trailer = parser->trailer();
  68. document->m_catalog = TRY(parser->trailer()->get_dict(document, CommonNames::Root));
  69. if (document->m_trailer->contains(CommonNames::Encrypt)) {
  70. auto encryption_dict = TRY(document->m_trailer->get_dict(document, CommonNames::Encrypt));
  71. document->m_security_handler = TRY(SecurityHandler::create(document, encryption_dict));
  72. // Automatically attempt to unencrypt the document with the empty string. The
  73. // result is not important; it is the caller's responsibility to ensure the
  74. // document is unencrypted before calling initialize().
  75. document->m_security_handler->try_provide_user_password(""sv);
  76. }
  77. return document;
  78. }
  79. Document::Document(NonnullRefPtr<DocumentParser> const& parser)
  80. : m_parser(parser)
  81. {
  82. m_parser->set_document(this);
  83. }
  84. PDFErrorOr<void> Document::initialize()
  85. {
  86. if (m_security_handler)
  87. VERIFY(m_security_handler->has_user_password());
  88. TRY(build_page_tree());
  89. TRY(build_outline());
  90. return {};
  91. }
  92. PDFErrorOr<Value> Document::get_or_load_value(u32 index)
  93. {
  94. auto value = get_value(index);
  95. if (!value.has<Empty>()) // FIXME: Use Optional instead?
  96. return value;
  97. auto object = TRY(m_parser->parse_object_with_index(index));
  98. m_values.set(index, object);
  99. return object;
  100. }
  101. u32 Document::get_first_page_index() const
  102. {
  103. // FIXME: A PDF can have a different default first page, which
  104. // should be fetched and returned here
  105. return 0;
  106. }
  107. u32 Document::get_page_count() const
  108. {
  109. return m_page_object_indices.size();
  110. }
  111. static ErrorOr<void> collect_referenced_indices(Value const& value, Vector<int>& referenced_indices)
  112. {
  113. TRY(value.visit(
  114. [&](Empty const&) -> ErrorOr<void> { return {}; },
  115. [&](nullptr_t const&) -> ErrorOr<void> { return {}; },
  116. [&](bool const&) -> ErrorOr<void> { return {}; },
  117. [&](int const&) -> ErrorOr<void> { return {}; },
  118. [&](float const&) -> ErrorOr<void> { return {}; },
  119. [&](Reference const& ref) -> ErrorOr<void> {
  120. TRY(referenced_indices.try_append(ref.as_ref_index()));
  121. return {};
  122. },
  123. [&](NonnullRefPtr<Object> const& object) -> ErrorOr<void> {
  124. if (object->is<ArrayObject>()) {
  125. for (auto& element : object->cast<ArrayObject>()->elements())
  126. TRY(collect_referenced_indices(element, referenced_indices));
  127. } else if (object->is<DictObject>()) {
  128. for (auto& [key, value] : object->cast<DictObject>()->map()) {
  129. if (key != CommonNames::Parent)
  130. TRY(collect_referenced_indices(value, referenced_indices));
  131. }
  132. } else if (object->is<StreamObject>()) {
  133. for (auto& [key, value] : object->cast<StreamObject>()->dict()->map()) {
  134. if (key != CommonNames::Parent)
  135. TRY(collect_referenced_indices(value, referenced_indices));
  136. }
  137. }
  138. return {};
  139. }));
  140. return {};
  141. }
  142. static PDFErrorOr<void> dump_tree(Document& document, size_t index, HashTable<int>& seen)
  143. {
  144. if (seen.contains(index))
  145. return {};
  146. seen.set(index);
  147. auto const& value = TRY(document.get_or_load_value(index));
  148. outln("{} 0 obj", index);
  149. outln("{}", value.to_deprecated_string(0));
  150. outln("endobj");
  151. Vector<int> referenced_indices;
  152. TRY(collect_referenced_indices(value, referenced_indices));
  153. for (auto index : referenced_indices)
  154. TRY(dump_tree(document, index, seen));
  155. return {};
  156. }
  157. PDFErrorOr<void> Document::dump_page(u32 index)
  158. {
  159. VERIFY(index < m_page_object_indices.size());
  160. auto page_object_index = m_page_object_indices[index];
  161. HashTable<int> seen;
  162. TRY(dump_tree(*this, page_object_index, seen));
  163. return {};
  164. }
  165. PDFErrorOr<Page> Document::get_page(u32 index)
  166. {
  167. VERIFY(index < m_page_object_indices.size());
  168. auto cached_page = m_pages.get(index);
  169. if (cached_page.has_value())
  170. return cached_page.value();
  171. auto page_object_index = m_page_object_indices[index];
  172. auto page_object = TRY(get_or_load_value(page_object_index));
  173. auto raw_page_object = TRY(resolve_to<DictObject>(page_object));
  174. RefPtr<DictObject> resources;
  175. auto maybe_resources_object = TRY(get_inheritable_object(CommonNames::Resources, raw_page_object));
  176. if (maybe_resources_object.has_value())
  177. resources = maybe_resources_object.value()->cast<DictObject>();
  178. else
  179. resources = adopt_ref(*new DictObject({}));
  180. RefPtr<Object> contents;
  181. if (raw_page_object->contains(CommonNames::Contents))
  182. contents = TRY(raw_page_object->get_object(this, CommonNames::Contents));
  183. Rectangle media_box;
  184. auto maybe_media_box_object = TRY(get_inheritable_object(CommonNames::MediaBox, raw_page_object));
  185. if (maybe_media_box_object.has_value()) {
  186. auto media_box_array = maybe_media_box_object.value()->cast<ArrayObject>();
  187. media_box = Rectangle {
  188. media_box_array->at(0).to_float(),
  189. media_box_array->at(1).to_float(),
  190. media_box_array->at(2).to_float(),
  191. media_box_array->at(3).to_float(),
  192. };
  193. } else {
  194. // As most other libraries seem to do, we default to the standard
  195. // US letter size of 8.5" x 11" (612 x 792 Postscript units).
  196. media_box = Rectangle {
  197. 0, 0,
  198. 612, 792
  199. };
  200. }
  201. Rectangle crop_box;
  202. auto maybe_crop_box_object = TRY(get_inheritable_object(CommonNames::CropBox, raw_page_object));
  203. if (maybe_crop_box_object.has_value()) {
  204. auto crop_box_array = maybe_crop_box_object.value()->cast<ArrayObject>();
  205. crop_box = Rectangle {
  206. crop_box_array->at(0).to_float(),
  207. crop_box_array->at(1).to_float(),
  208. crop_box_array->at(2).to_float(),
  209. crop_box_array->at(3).to_float(),
  210. };
  211. } else {
  212. crop_box = media_box;
  213. }
  214. float user_unit = 1.0f;
  215. if (raw_page_object->contains(CommonNames::UserUnit))
  216. user_unit = raw_page_object->get_value(CommonNames::UserUnit).to_float();
  217. int rotate = 0;
  218. auto maybe_rotate = TRY(get_inheritable_value(CommonNames::Rotate, raw_page_object));
  219. if (maybe_rotate.has_value()) {
  220. rotate = TRY(resolve_to<int>(maybe_rotate.value()));
  221. VERIFY(rotate % 90 == 0);
  222. }
  223. Page page { resources.release_nonnull(), move(contents), media_box, crop_box, user_unit, rotate };
  224. m_pages.set(index, page);
  225. return page;
  226. }
  227. PDFErrorOr<Value> Document::resolve(Value const& value)
  228. {
  229. if (value.has<Reference>()) {
  230. // FIXME: Surely indirect PDF objects can't contain another indirect PDF object,
  231. // right? Unsure from the spec, but if they can, these return values would have
  232. // to be wrapped with another resolve() call.
  233. return get_or_load_value(value.as_ref_index());
  234. }
  235. if (!value.has<NonnullRefPtr<Object>>())
  236. return value;
  237. auto& obj = value.get<NonnullRefPtr<Object>>();
  238. if (obj->is<IndirectValue>())
  239. return static_ptr_cast<IndirectValue>(obj)->value();
  240. return value;
  241. }
  242. PDFErrorOr<Optional<InfoDict>> Document::info_dict()
  243. {
  244. if (!trailer()->contains(CommonNames::Info))
  245. return OptionalNone {};
  246. return InfoDict(this, TRY(trailer()->get_dict(this, CommonNames::Info)));
  247. }
  248. PDFErrorOr<Vector<DeprecatedFlyString>> Document::read_filters(NonnullRefPtr<DictObject> dict)
  249. {
  250. Vector<DeprecatedFlyString> filters;
  251. // We may either get a single filter or an array of cascading filters
  252. auto filter_object = TRY(dict->get_object(this, CommonNames::Filter));
  253. if (filter_object->is<ArrayObject>()) {
  254. auto filter_array = filter_object->cast<ArrayObject>();
  255. for (size_t i = 0; i < filter_array->size(); ++i)
  256. filters.append(TRY(filter_array->get_name_at(this, i))->name());
  257. } else {
  258. filters.append(filter_object->cast<NameObject>()->name());
  259. }
  260. return filters;
  261. }
  262. PDFErrorOr<void> Document::build_page_tree()
  263. {
  264. auto page_tree = TRY(m_catalog->get_dict(this, CommonNames::Pages));
  265. return add_page_tree_node_to_page_tree(page_tree);
  266. }
  267. PDFErrorOr<void> Document::add_page_tree_node_to_page_tree(NonnullRefPtr<DictObject> const& page_tree)
  268. {
  269. auto kids_array = TRY(page_tree->get_array(this, CommonNames::Kids));
  270. auto page_count = page_tree->get(CommonNames::Count).value().get<int>();
  271. if (static_cast<size_t>(page_count) != kids_array->elements().size()) {
  272. // This page tree contains child page trees, so we recursively add
  273. // these pages to the overall page tree
  274. for (auto& value : *kids_array) {
  275. auto reference_index = value.as_ref_index();
  276. auto maybe_page_tree_node = TRY(m_parser->conditionally_parse_page_tree_node(reference_index));
  277. if (maybe_page_tree_node) {
  278. TRY(add_page_tree_node_to_page_tree(maybe_page_tree_node.release_nonnull()));
  279. } else {
  280. m_page_object_indices.append(reference_index);
  281. }
  282. }
  283. } else {
  284. // We know all of the kids are leaf nodes
  285. for (auto& value : *kids_array)
  286. m_page_object_indices.append(value.as_ref_index());
  287. }
  288. return {};
  289. }
  290. PDFErrorOr<NonnullRefPtr<Object>> Document::find_in_name_tree(NonnullRefPtr<DictObject> tree, DeprecatedFlyString name)
  291. {
  292. if (tree->contains(CommonNames::Kids)) {
  293. return find_in_name_tree_nodes(tree->get_array(CommonNames::Kids), name);
  294. }
  295. if (!tree->contains(CommonNames::Names))
  296. return Error { Error::Type::MalformedPDF, "name tree has neither Kids nor Names" };
  297. auto key_value_names_array = TRY(tree->get_array(this, CommonNames::Names));
  298. return find_in_key_value_array(key_value_names_array, name);
  299. }
  300. PDFErrorOr<NonnullRefPtr<Object>> Document::find_in_name_tree_nodes(NonnullRefPtr<ArrayObject> siblings, DeprecatedFlyString name)
  301. {
  302. for (size_t i = 0; i < siblings->size(); i++) {
  303. auto sibling = TRY(resolve_to<DictObject>(siblings->at(i)));
  304. auto limits = sibling->get_array(CommonNames::Limits);
  305. if (limits->size() != 2)
  306. return Error { Error::Type::MalformedPDF, "Expected 2-element Limits array" };
  307. auto start = limits->get_string_at(0);
  308. auto end = limits->get_string_at(1);
  309. if (start->string() <= name && end->string() >= name) {
  310. return find_in_name_tree(sibling, name);
  311. }
  312. }
  313. return Error { Error::Type::MalformedPDF, DeprecatedString::formatted("Didn't find node in name tree containing name {}", name) };
  314. }
  315. PDFErrorOr<NonnullRefPtr<Object>> Document::find_in_key_value_array(NonnullRefPtr<ArrayObject> key_value_array, DeprecatedFlyString name)
  316. {
  317. if (key_value_array->size() % 2 == 1)
  318. return Error { Error::Type::MalformedPDF, "key/value array has dangling key" };
  319. for (size_t i = 0; i < key_value_array->size() / 2; i++) {
  320. auto key = key_value_array->get_string_at(2 * i);
  321. if (key->string() == name) {
  322. return key_value_array->get_object_at(this, 2 * i + 1);
  323. }
  324. }
  325. return Error { Error::Type::MalformedPDF, DeprecatedString::formatted("Didn't find expected name {} in key/value array", name) };
  326. }
  327. PDFErrorOr<void> Document::build_outline()
  328. {
  329. if (!m_catalog->contains(CommonNames::Outlines))
  330. return {};
  331. auto outline_dict = TRY(m_catalog->get_dict(this, CommonNames::Outlines));
  332. if (!outline_dict->contains(CommonNames::First))
  333. return {};
  334. if (!outline_dict->contains(CommonNames::Last))
  335. return {};
  336. HashMap<u32, u32> page_number_by_index_ref;
  337. for (u32 page_number = 0; page_number < m_page_object_indices.size(); ++page_number) {
  338. page_number_by_index_ref.set(m_page_object_indices[page_number], page_number);
  339. }
  340. auto first_ref = outline_dict->get_value(CommonNames::First);
  341. auto children = TRY(build_outline_item_chain(first_ref, page_number_by_index_ref));
  342. m_outline = adopt_ref(*new OutlineDict());
  343. m_outline->children = move(children);
  344. if (outline_dict->contains(CommonNames::Count))
  345. m_outline->count = outline_dict->get_value(CommonNames::Count).get<int>();
  346. return {};
  347. }
  348. PDFErrorOr<Destination> Document::create_destination_from_parameters(NonnullRefPtr<ArrayObject> array, HashMap<u32, u32> const& page_number_by_index_ref)
  349. {
  350. auto page_ref = array->at(0);
  351. auto type_name = TRY(array->get_name_at(this, 1))->name();
  352. Vector<Optional<float>> parameters;
  353. TRY(parameters.try_ensure_capacity(array->size() - 2));
  354. for (size_t i = 2; i < array->size(); i++) {
  355. auto& param = array->at(i);
  356. if (param.has<nullptr_t>())
  357. parameters.unchecked_append({});
  358. else
  359. parameters.append(param.to_float());
  360. }
  361. Destination::Type type;
  362. if (type_name == CommonNames::XYZ) {
  363. type = Destination::Type::XYZ;
  364. } else if (type_name == CommonNames::Fit) {
  365. type = Destination::Type::Fit;
  366. } else if (type_name == CommonNames::FitH) {
  367. type = Destination::Type::FitH;
  368. } else if (type_name == CommonNames::FitV) {
  369. type = Destination::Type::FitV;
  370. } else if (type_name == CommonNames::FitR) {
  371. type = Destination::Type::FitR;
  372. } else if (type_name == CommonNames::FitB) {
  373. type = Destination::Type::FitB;
  374. } else if (type_name == CommonNames::FitBH) {
  375. type = Destination::Type::FitBH;
  376. } else if (type_name == CommonNames::FitBV) {
  377. type = Destination::Type::FitBV;
  378. } else {
  379. VERIFY_NOT_REACHED();
  380. }
  381. // The spec requires page_ref to be an indirect reference to a page object,
  382. // but in practice it's sometimes a page index.
  383. Optional<u32> page_number;
  384. if (page_ref.has<int>())
  385. page_number = page_ref.get<int>();
  386. else
  387. page_number = page_number_by_index_ref.get(page_ref.as_ref_index());
  388. return Destination { type, page_number, parameters };
  389. }
  390. PDFErrorOr<Optional<NonnullRefPtr<Object>>> Document::get_inheritable_object(DeprecatedFlyString const& name, NonnullRefPtr<DictObject> object)
  391. {
  392. if (!object->contains(name)) {
  393. if (!object->contains(CommonNames::Parent))
  394. return { OptionalNone() };
  395. auto parent = TRY(object->get_dict(this, CommonNames::Parent));
  396. return get_inheritable_object(name, parent);
  397. }
  398. return TRY(object->get_object(this, name));
  399. }
  400. PDFErrorOr<Optional<Value>> Document::get_inheritable_value(DeprecatedFlyString const& name, NonnullRefPtr<DictObject> object)
  401. {
  402. if (!object->contains(name)) {
  403. if (!object->contains(CommonNames::Parent))
  404. return { OptionalNone() };
  405. auto parent = TRY(object->get_dict(this, CommonNames::Parent));
  406. return get_inheritable_value(name, parent);
  407. }
  408. return object->get(name);
  409. }
  410. PDFErrorOr<Destination> Document::create_destination_from_dictionary_entry(NonnullRefPtr<Object> const& entry, HashMap<u32, u32> const& page_number_by_index_ref)
  411. {
  412. if (entry->is<ArrayObject>()) {
  413. auto entry_array = entry->cast<ArrayObject>();
  414. return create_destination_from_parameters(entry_array, page_number_by_index_ref);
  415. }
  416. auto entry_dictionary = entry->cast<DictObject>();
  417. auto d_array = MUST(entry_dictionary->get_array(this, CommonNames::D));
  418. return create_destination_from_parameters(d_array, page_number_by_index_ref);
  419. }
  420. PDFErrorOr<Destination> Document::create_destination_from_object(NonnullRefPtr<Object> const& dest_obj, HashMap<u32, u32> const& page_number_by_index_ref)
  421. {
  422. // PDF 1.7 spec, "8.2.1 Destinations"
  423. if (dest_obj->is<ArrayObject>()) {
  424. auto dest_arr = dest_obj->cast<ArrayObject>();
  425. return TRY(create_destination_from_parameters(dest_arr, page_number_by_index_ref));
  426. }
  427. if (dest_obj->is<NameObject>() || dest_obj->is<StringObject>()) {
  428. DeprecatedFlyString dest_name;
  429. if (dest_obj->is<NameObject>())
  430. dest_name = dest_obj->cast<NameObject>()->name();
  431. else
  432. dest_name = dest_obj->cast<StringObject>()->string();
  433. if (auto dests_value = m_catalog->get(CommonNames::Dests); dests_value.has_value()) {
  434. auto dests = TRY(resolve_to<DictObject>(dests_value.value()));
  435. auto entry = MUST(dests->get_object(this, dest_name));
  436. return TRY(create_destination_from_dictionary_entry(entry, page_number_by_index_ref));
  437. }
  438. if (auto names_value = m_catalog->get(CommonNames::Names); names_value.has_value()) {
  439. auto names = TRY(resolve_to<DictObject>(names_value.release_value()));
  440. if (!names->contains(CommonNames::Dests))
  441. return Error { Error::Type::MalformedPDF, "Missing Dests key in document catalogue's Names dictionary" };
  442. auto dest_obj = TRY(find_in_name_tree(TRY(names->get_dict(this, CommonNames::Dests)), dest_name));
  443. return TRY(create_destination_from_dictionary_entry(dest_obj, page_number_by_index_ref));
  444. }
  445. }
  446. return Error { Error::Type::MalformedPDF, "Malformed outline destination" };
  447. }
  448. PDFErrorOr<NonnullRefPtr<OutlineItem>> Document::build_outline_item(NonnullRefPtr<DictObject> const& outline_item_dict, HashMap<u32, u32> const& page_number_by_index_ref)
  449. {
  450. auto outline_item = adopt_ref(*new OutlineItem {});
  451. if (outline_item_dict->contains(CommonNames::First)) {
  452. VERIFY(outline_item_dict->contains(CommonNames::Last));
  453. auto first_ref = outline_item_dict->get_value(CommonNames::First);
  454. auto children = TRY(build_outline_item_chain(first_ref, page_number_by_index_ref));
  455. for (auto& child : children) {
  456. child->parent = outline_item;
  457. }
  458. outline_item->children = move(children);
  459. }
  460. outline_item->title = TRY(outline_item_dict->get_string(this, CommonNames::Title))->string();
  461. if (outline_item_dict->contains(CommonNames::Count))
  462. outline_item->count = outline_item_dict->get_value(CommonNames::Count).get<int>();
  463. if (outline_item_dict->contains(CommonNames::Dest)) {
  464. auto dest_obj = TRY(outline_item_dict->get_object(this, CommonNames::Dest));
  465. outline_item->dest = TRY(create_destination_from_object(dest_obj, page_number_by_index_ref));
  466. } else if (outline_item_dict->contains(CommonNames::A)) {
  467. // PDF 1.7 spec, "8.5 Actions"
  468. auto action_dict = TRY(outline_item_dict->get_dict(this, CommonNames::A));
  469. if (action_dict->contains(CommonNames::S)) {
  470. // PDF 1.7 spec, "TABLE 8.48 Action types"
  471. auto action_type = TRY(action_dict->get_name(this, CommonNames::S))->name();
  472. if (action_type == "GoTo") {
  473. // PDF 1.7 spec, "Go-To Actions"
  474. if (action_dict->contains(CommonNames::D)) {
  475. auto dest_obj = TRY(action_dict->get_object(this, CommonNames::D));
  476. outline_item->dest = TRY(create_destination_from_object(dest_obj, page_number_by_index_ref));
  477. }
  478. } else {
  479. dbgln("Unhandled action type {}", action_type);
  480. }
  481. }
  482. }
  483. if (outline_item_dict->contains(CommonNames::C)) {
  484. auto color_array = TRY(outline_item_dict->get_array(this, CommonNames::C));
  485. auto r = static_cast<int>(255.0f * color_array->at(0).to_float());
  486. auto g = static_cast<int>(255.0f * color_array->at(1).to_float());
  487. auto b = static_cast<int>(255.0f * color_array->at(2).to_float());
  488. outline_item->color = Color(r, g, b);
  489. }
  490. if (outline_item_dict->contains(CommonNames::F)) {
  491. auto bitfield = outline_item_dict->get_value(CommonNames::F).get<int>();
  492. outline_item->italic = bitfield & 0x1;
  493. outline_item->bold = bitfield & 0x2;
  494. }
  495. return outline_item;
  496. }
  497. PDFErrorOr<Vector<NonnullRefPtr<OutlineItem>>> Document::build_outline_item_chain(Value const& first_ref, HashMap<u32, u32> const& page_number_by_index_ref)
  498. {
  499. // We used to receive a last_ref parameter, which was what the parent of this chain
  500. // thought was this chain's last child. There are documents out there in the wild
  501. // where this cross-references don't match though, and it seems like simply following
  502. // the /First and /Next links is the way to go to construct the whole Outline
  503. // (we already ignore the /Parent attribute too, which can also be out of sync).
  504. VERIFY(first_ref.has<Reference>());
  505. Vector<NonnullRefPtr<OutlineItem>> children;
  506. auto first_value = TRY(get_or_load_value(first_ref.as_ref_index())).get<NonnullRefPtr<Object>>();
  507. auto first_dict = first_value->cast<DictObject>();
  508. auto first = TRY(build_outline_item(first_dict, page_number_by_index_ref));
  509. children.append(first);
  510. auto current_child_dict = first_dict;
  511. u32 current_child_index = first_ref.as_ref_index();
  512. while (current_child_dict->contains(CommonNames::Next)) {
  513. auto next_child_dict_ref = current_child_dict->get_value(CommonNames::Next);
  514. current_child_index = next_child_dict_ref.as_ref_index();
  515. auto next_child_value = TRY(get_or_load_value(current_child_index)).get<NonnullRefPtr<Object>>();
  516. auto next_child_dict = next_child_value->cast<DictObject>();
  517. auto next_child = TRY(build_outline_item(next_child_dict, page_number_by_index_ref));
  518. children.append(next_child);
  519. current_child_dict = move(next_child_dict);
  520. }
  521. return children;
  522. }
  523. }