summaryrefslogtreecommitdiff
path: root/pdf/pdf_outline.c
blob: e5c87f8cef364478e5ca45deaf5d42327c4a9e6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "fitz.h"
#include "mupdf.h"

static fz_outline *
pdf_load_outline_imp(pdf_document *xref, fz_obj *dict)
{
	fz_context *ctx = xref->ctx;
	fz_outline *node, **prev, *first;
	fz_obj *obj;
	fz_obj *odict = dict;

	fz_var(dict);

	fz_try(ctx)
	{
		first = NULL;
		prev = &first;
		while (dict && fz_is_dict(dict))
		{
			if (fz_dict_mark(dict))
				break;
			node = fz_malloc_struct(ctx, fz_outline);
			node->title = NULL;
			node->dest.kind = FZ_LINK_NONE;
			node->down = NULL;
			node->next = NULL;
			*prev = node;
			prev = &node->next;

			obj = fz_dict_gets(dict, "Title");
			if (obj)
				node->title = pdf_to_utf8(ctx, obj);

			if ((obj = fz_dict_gets(dict, "Dest")))
				node->dest = pdf_parse_link_dest(xref, obj);
			else if ((obj = fz_dict_gets(dict, "A")))
				node->dest = pdf_parse_action(xref, obj);

			obj = fz_dict_gets(dict, "First");
			if (obj)
				node->down = pdf_load_outline_imp(xref, obj);

			dict = fz_dict_gets(dict, "Next");
		}
	}
	fz_catch(ctx)
	{
		for (dict = odict; dict && fz_dict_marked(dict); dict = fz_dict_gets(dict, "Next"))
			fz_dict_unmark(dict);
		fz_rethrow(ctx);
	}

	for (dict = odict; dict && fz_dict_marked(dict); dict = fz_dict_gets(dict, "Next"))
		fz_dict_unmark(dict);

	return first;
}

fz_outline *
pdf_load_outline(pdf_document *xref)
{
	fz_obj *root, *obj, *first;

	root = fz_dict_gets(xref->trailer, "Root");
	obj = fz_dict_gets(root, "Outlines");
	first = fz_dict_gets(obj, "First");
	if (first)
		return pdf_load_outline_imp(xref, first);

	return NULL;
}