1#include "mupdf/fitz.h"
2#include "mupdf/pdf.h"
3
4static fz_outline *
5pdf_load_outline_imp(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
6{
7 fz_outline *node, **prev, *first = NULL;
8 pdf_obj *obj;
9 pdf_obj *odict = dict;
10
11 fz_var(dict);
12 fz_var(first);
13
14 fz_try(ctx)
15 {
16 prev = &first;
17 while (dict && pdf_is_dict(ctx, dict))
18 {
19 if (pdf_mark_obj(ctx, dict))
20 break;
21 node = fz_new_outline(ctx);
22 *prev = node;
23 prev = &node->next;
24
25 obj = pdf_dict_get(ctx, dict, PDF_NAME(Title));
26 if (obj)
27 node->title = fz_strdup(ctx, pdf_to_text_string(ctx, obj));
28
29 if ((obj = pdf_dict_get(ctx, dict, PDF_NAME(Dest))) != NULL)
30 node->uri = pdf_parse_link_dest(ctx, doc, obj);
31 else if ((obj = pdf_dict_get(ctx, dict, PDF_NAME(A))) != NULL)
32 node->uri = pdf_parse_link_action(ctx, doc, obj, -1);
33 else
34 node->uri = NULL;
35
36 if (node->uri && !fz_is_external_link(ctx, node->uri))
37 node->page = pdf_resolve_link(ctx, doc, node->uri, &node->x, &node->y);
38 else
39 node->page = -1;
40
41 obj = pdf_dict_get(ctx, dict, PDF_NAME(First));
42 if (obj)
43 {
44 node->down = pdf_load_outline_imp(ctx, doc, obj);
45
46 obj = pdf_dict_get(ctx, dict, PDF_NAME(Count));
47 if (pdf_to_int(ctx, obj) > 0)
48 node->is_open = 1;
49 }
50
51 dict = pdf_dict_get(ctx, dict, PDF_NAME(Next));
52 }
53 }
54 fz_always(ctx)
55 {
56 for (dict = odict; dict && pdf_obj_marked(ctx, dict); dict = pdf_dict_get(ctx, dict, PDF_NAME(Next)))
57 pdf_unmark_obj(ctx, dict);
58 }
59 fz_catch(ctx)
60 {
61 fz_drop_outline(ctx, first);
62 fz_rethrow(ctx);
63 }
64
65 return first;
66}
67
68fz_outline *
69pdf_load_outline(fz_context *ctx, pdf_document *doc)
70{
71 pdf_obj *root, *obj, *first;
72 fz_outline *outline = NULL;
73
74 root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root));
75 obj = pdf_dict_get(ctx, root, PDF_NAME(Outlines));
76 first = pdf_dict_get(ctx, obj, PDF_NAME(First));
77 if (first)
78 {
79 /* cache page tree for fast link destination lookups */
80 pdf_load_page_tree(ctx, doc);
81 fz_try(ctx)
82 outline = pdf_load_outline_imp(ctx, doc, first);
83 fz_always(ctx)
84 pdf_drop_page_tree(ctx, doc);
85 fz_catch(ctx)
86 fz_rethrow(ctx);
87 }
88
89 return outline;
90}
91