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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "mupdf/fitz.h"
#include "xps-imp.h"
#include <stdlib.h>
#include <math.h>
/*
* Parse the document structure / outline parts referenced from fixdoc relationships.
*/
static fz_outline *
xps_lookup_last_outline_at_level(fz_context *ctx, xps_document *doc, fz_outline *node, int level, int target_level)
{
while (node->next)
node = node->next;
if (level == target_level || !node->down)
return node;
return xps_lookup_last_outline_at_level(ctx, doc, node->down, level + 1, target_level);
}
static fz_outline *
xps_parse_document_outline(fz_context *ctx, xps_document *doc, fz_xml *root)
{
fz_xml *node;
fz_outline *head = NULL, *entry, *tail;
int last_level = 1, this_level;
for (node = fz_xml_down(root); node; node = fz_xml_next(node))
{
if (fz_xml_is_tag(node, "OutlineEntry"))
{
char *level = fz_xml_att(node, "OutlineLevel");
char *target = fz_xml_att(node, "OutlineTarget");
char *description = fz_xml_att(node, "Description");
if (!target || !description)
continue;
entry = fz_new_outline(ctx);
entry->title = fz_strdup(ctx, description);
entry->uri = fz_strdup(ctx, target);
entry->page = xps_lookup_link_target(ctx, (fz_document*)doc, target, NULL, NULL);
entry->down = NULL;
entry->next = NULL;
this_level = level ? atoi(level) : 1;
if (!head)
{
head = entry;
}
else
{
tail = xps_lookup_last_outline_at_level(ctx, doc, head, 1, this_level);
if (this_level > last_level)
tail->down = entry;
else
tail->next = entry;
}
last_level = this_level;
}
}
return head;
}
static fz_outline *
xps_parse_document_structure(fz_context *ctx, xps_document *doc, fz_xml *root)
{
fz_xml *node;
if (fz_xml_is_tag(root, "DocumentStructure"))
{
node = fz_xml_down(root);
if (node && fz_xml_is_tag(node, "DocumentStructure.Outline"))
{
node = fz_xml_down(node);
if (node && fz_xml_is_tag(node, "DocumentOutline"))
return xps_parse_document_outline(ctx, doc, node);
}
}
return NULL;
}
static fz_outline *
xps_load_document_structure(fz_context *ctx, xps_document *doc, xps_fixdoc *fixdoc)
{
xps_part *part;
fz_xml *root = NULL;
fz_outline *outline = NULL;
part = xps_read_part(ctx, doc, fixdoc->outline);
fz_try(ctx)
{
root = fz_parse_xml(ctx, part->data, 0);
}
fz_always(ctx)
{
xps_drop_part(ctx, doc, part);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
if (!root)
return NULL;
fz_try(ctx)
{
outline = xps_parse_document_structure(ctx, doc, root);
}
fz_always(ctx)
{
fz_drop_xml(ctx, root);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
return outline;
}
fz_outline *
xps_load_outline(fz_context *ctx, fz_document *doc_)
{
xps_document *doc = (xps_document*)doc_;
xps_fixdoc *fixdoc;
fz_outline *head = NULL, *tail, *outline = NULL;
for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next)
{
if (fixdoc->outline)
{
fz_try(ctx)
{
outline = xps_load_document_structure(ctx, doc, fixdoc);
}
fz_catch(ctx)
{
fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
outline = NULL;
}
if (!outline)
continue;
if (!head)
head = outline;
else
{
while (tail->next)
tail = tail->next;
tail->next = outline;
}
tail = outline;
}
}
return head;
}
|