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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
#include "mupdf/xps.h"
#define REL_START_PART \
"http://schemas.microsoft.com/xps/2005/06/fixedrepresentation"
#define REL_DOC_STRUCTURE \
"http://schemas.microsoft.com/xps/2005/06/documentstructure"
#define REL_REQUIRED_RESOURCE \
"http://schemas.microsoft.com/xps/2005/06/required-resource"
#define REL_REQUIRED_RESOURCE_RECURSIVE \
"http://schemas.microsoft.com/xps/2005/06/required-resource#recursive"
#define REL_START_PART_OXPS \
"http://schemas.openxps.org/oxps/v1.0/fixedrepresentation"
#define REL_DOC_STRUCTURE_OXPS \
"http://schemas.openxps.org/oxps/v1.0/documentstructure"
static void
xps_rels_for_part(char *buf, char *name, int buflen)
{
char *p, *basename;
p = strrchr(name, '/');
basename = p ? p + 1 : name;
fz_strlcpy(buf, name, buflen);
p = strrchr(buf, '/');
if (p) *p = 0;
fz_strlcat(buf, "/_rels/", buflen);
fz_strlcat(buf, basename, buflen);
fz_strlcat(buf, ".rels", buflen);
}
/*
* The FixedDocumentSequence and FixedDocument parts determine
* which parts correspond to actual pages, and the page order.
*/
void
xps_print_page_list(xps_document *doc)
{
xps_fixdoc *fixdoc = doc->first_fixdoc;
xps_page *page = doc->first_page;
if (doc->start_part)
printf("start part %s\n", doc->start_part);
while (fixdoc)
{
printf("fixdoc %s\n", fixdoc->name);
fixdoc = fixdoc->next;
}
while (page)
{
printf("page[%d] %s w=%d h=%d\n", page->number, page->name, page->width, page->height);
page = page->next;
}
}
static void
xps_add_fixed_document(xps_document *doc, char *name)
{
xps_fixdoc *fixdoc;
/* Check for duplicates first */
for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next)
if (!strcmp(fixdoc->name, name))
return;
fixdoc = fz_malloc_struct(doc->ctx, xps_fixdoc);
fixdoc->name = fz_strdup(doc->ctx, name);
fixdoc->outline = NULL;
fixdoc->next = NULL;
if (!doc->first_fixdoc)
{
doc->first_fixdoc = fixdoc;
doc->last_fixdoc = fixdoc;
}
else
{
doc->last_fixdoc->next = fixdoc;
doc->last_fixdoc = fixdoc;
}
}
void
xps_add_link(xps_document *doc, const fz_rect *area, char *base_uri, char *target_uri)
{
int len;
char *buffer = NULL;
char *uri;
xps_target *target;
fz_link_dest dest;
fz_link *link;
fz_context *ctx = doc->ctx;
fz_var(buffer);
if (doc->current_page == NULL || doc->current_page->links_resolved)
return;
fz_try(ctx)
{
len = 2 + (base_uri ? strlen(base_uri) : 0) +
(target_uri ? strlen(target_uri) : 0);
buffer = fz_malloc(doc->ctx, len);
xps_resolve_url(buffer, base_uri, target_uri, len);
if (xps_url_is_remote(buffer))
{
dest.kind = FZ_LINK_URI;
dest.ld.uri.is_map = 0;
dest.ld.uri.uri = buffer;
buffer = NULL;
}
else
{
uri = buffer;
/* FIXME: This won't work for remote docs */
/* Skip until we find the fragment marker */
while (*uri && *uri != '#')
uri++;
if (*uri == '#')
uri++;
for (target = doc->target; target; target = target->next)
if (!strcmp(target->name, uri))
break;
if (target == NULL)
break;
dest.kind = FZ_LINK_GOTO;
dest.ld.gotor.flags = 0;
dest.ld.gotor.lt.x = 0;
dest.ld.gotor.lt.y = 0;
dest.ld.gotor.rb.x = 0;
dest.ld.gotor.rb.y = 0;
dest.ld.gotor.page = target->page;
dest.ld.gotor.file_spec = NULL;
dest.ld.gotor.new_window = 0;
}
link = fz_new_link(doc->ctx, area, dest);
link->next = doc->current_page->links;
doc->current_page->links = link;
}
fz_always(ctx)
{
fz_free(doc->ctx, buffer);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
fz_link *
xps_load_links(xps_document *doc, xps_page *page)
{
if (!page->links_resolved)
fz_warn(doc->ctx, "xps_load_links before page has been executed!");
return fz_keep_link(doc->ctx, page->links);
}
static void
xps_add_fixed_page(xps_document *doc, char *name, int width, int height)
{
xps_page *page;
/* Check for duplicates first */
for (page = doc->first_page; page; page = page->next)
if (!strcmp(page->name, name))
return;
page = fz_malloc_struct(doc->ctx, xps_page);
page->name = fz_strdup(doc->ctx, name);
page->number = doc->page_count++;
page->width = width;
page->height = height;
page->links = NULL;
page->links_resolved = 0;
page->root = NULL;
page->next = NULL;
if (!doc->first_page)
{
doc->first_page = page;
doc->last_page = page;
}
else
{
doc->last_page->next = page;
doc->last_page = page;
}
}
static void
xps_add_link_target(xps_document *doc, char *name)
{
xps_page *page = doc->last_page;
xps_target *target = fz_malloc_struct(doc->ctx, xps_target);
target->name = fz_strdup(doc->ctx, name);
target->page = page->number;
target->next = doc->target;
doc->target = target;
}
int
xps_lookup_link_target(xps_document *doc, char *target_uri)
{
xps_target *target;
char *needle = strrchr(target_uri, '#');
needle = needle ? needle + 1 : target_uri;
for (target = doc->target; target; target = target->next)
if (!strcmp(target->name, needle))
return target->page;
return 0;
}
static void
xps_free_link_targets(xps_document *doc)
{
xps_target *target = doc->target, *next;
while (target)
{
next = target->next;
fz_free(doc->ctx, target->name);
fz_free(doc->ctx, target);
target = next;
}
}
static void
xps_free_fixed_pages(xps_document *doc)
{
xps_page *page = doc->first_page;
while (page)
{
xps_page *next = page->next;
xps_free_page(doc, page);
fz_drop_link(doc->ctx, page->links);
fz_free(doc->ctx, page->name);
fz_free(doc->ctx, page);
page = next;
}
doc->first_page = NULL;
doc->last_page = NULL;
}
static void
xps_free_fixed_documents(xps_document *doc)
{
xps_fixdoc *fixdoc = doc->first_fixdoc;
while (fixdoc)
{
xps_fixdoc *next = fixdoc->next;
fz_free(doc->ctx, fixdoc->name);
fz_free(doc->ctx, fixdoc->outline);
fz_free(doc->ctx, fixdoc);
fixdoc = next;
}
doc->first_fixdoc = NULL;
doc->last_fixdoc = NULL;
}
void
xps_free_page_list(xps_document *doc)
{
xps_free_fixed_documents(doc);
xps_free_fixed_pages(doc);
xps_free_link_targets(doc);
}
/*
* Parse the fixed document sequence structure and _rels/.rels to find the start part.
*/
static void
xps_parse_metadata_imp(xps_document *doc, fz_xml *item, xps_fixdoc *fixdoc)
{
while (item)
{
if (!strcmp(fz_xml_tag(item), "Relationship"))
{
char *target = fz_xml_att(item, "Target");
char *type = fz_xml_att(item, "Type");
if (target && type)
{
char tgtbuf[1024];
xps_resolve_url(tgtbuf, doc->base_uri, target, sizeof tgtbuf);
if (!strcmp(type, REL_START_PART) || !strcmp(type, REL_START_PART_OXPS))
doc->start_part = fz_strdup(doc->ctx, tgtbuf);
if ((!strcmp(type, REL_DOC_STRUCTURE) || !strcmp(type, REL_DOC_STRUCTURE_OXPS)) && fixdoc)
fixdoc->outline = fz_strdup(doc->ctx, tgtbuf);
if (!fz_xml_att(item, "Id"))
fz_warn(doc->ctx, "missing relationship id for %s", target);
}
}
if (!strcmp(fz_xml_tag(item), "DocumentReference"))
{
char *source = fz_xml_att(item, "Source");
if (source)
{
char srcbuf[1024];
xps_resolve_url(srcbuf, doc->base_uri, source, sizeof srcbuf);
xps_add_fixed_document(doc, srcbuf);
}
}
if (!strcmp(fz_xml_tag(item), "PageContent"))
{
char *source = fz_xml_att(item, "Source");
char *width_att = fz_xml_att(item, "Width");
char *height_att = fz_xml_att(item, "Height");
int width = width_att ? atoi(width_att) : 0;
int height = height_att ? atoi(height_att) : 0;
if (source)
{
char srcbuf[1024];
xps_resolve_url(srcbuf, doc->base_uri, source, sizeof srcbuf);
xps_add_fixed_page(doc, srcbuf, width, height);
}
}
if (!strcmp(fz_xml_tag(item), "LinkTarget"))
{
char *name = fz_xml_att(item, "Name");
if (name)
xps_add_link_target(doc, name);
}
xps_parse_metadata_imp(doc, fz_xml_down(item), fixdoc);
item = fz_xml_next(item);
}
}
static void
xps_parse_metadata(xps_document *doc, xps_part *part, xps_fixdoc *fixdoc)
{
fz_xml *root;
char buf[1024];
char *s;
/* Save directory name part */
fz_strlcpy(buf, part->name, sizeof buf);
s = strrchr(buf, '/');
if (s)
s[0] = 0;
/* _rels parts are voodoo: their URI references are from
* the part they are associated with, not the actual _rels
* part being parsed.
*/
s = strstr(buf, "/_rels");
if (s)
*s = 0;
doc->base_uri = buf;
doc->part_uri = part->name;
root = fz_parse_xml(doc->ctx, part->data, part->size);
xps_parse_metadata_imp(doc, root, fixdoc);
fz_free_xml(doc->ctx, root);
doc->base_uri = NULL;
doc->part_uri = NULL;
}
static void
xps_read_and_process_metadata_part(xps_document *doc, char *name, xps_fixdoc *fixdoc)
{
fz_context *ctx = doc->ctx;
xps_part *part;
if (!xps_has_part(doc, name))
return;
part = xps_read_part(doc, name);
fz_try(ctx)
{
xps_parse_metadata(doc, part, fixdoc);
}
fz_always(ctx)
{
xps_free_part(doc, part);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
void
xps_read_page_list(xps_document *doc)
{
xps_fixdoc *fixdoc;
xps_read_and_process_metadata_part(doc, "/_rels/.rels", NULL);
if (!doc->start_part)
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "cannot find fixed document sequence start part");
xps_read_and_process_metadata_part(doc, doc->start_part, NULL);
for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next)
{
char relbuf[1024];
fz_try(doc->ctx)
{
xps_rels_for_part(relbuf, fixdoc->name, sizeof relbuf);
xps_read_and_process_metadata_part(doc, relbuf, fixdoc);
}
fz_catch(doc->ctx)
{
fz_rethrow_if(doc->ctx, FZ_ERROR_TRYLATER);
fz_warn(doc->ctx, "cannot process FixedDocument rels part");
}
xps_read_and_process_metadata_part(doc, fixdoc->name, fixdoc);
}
}
int
xps_count_pages(xps_document *doc)
{
return doc->page_count;
}
static void
xps_load_fixed_page(xps_document *doc, xps_page *page)
{
xps_part *part;
fz_xml *root;
char *width_att;
char *height_att;
fz_context *ctx = doc->ctx;
part = xps_read_part(doc, page->name);
fz_try(ctx)
{
root = fz_parse_xml(doc->ctx, part->data, part->size);
}
fz_always(ctx)
{
xps_free_part(doc, part);
}
fz_catch(ctx)
{
fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
root = NULL;
}
if (!root)
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "FixedPage missing root element");
if (!strcmp(fz_xml_tag(root), "mc:AlternateContent"))
{
fz_xml *node = xps_lookup_alternate_content(root);
if (!node)
{
fz_free_xml(doc->ctx, root);
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "FixedPage missing alternate root element");
}
fz_detach_xml(node);
fz_free_xml(doc->ctx, root);
root = node;
}
if (strcmp(fz_xml_tag(root), "FixedPage"))
{
fz_free_xml(doc->ctx, root);
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "expected FixedPage element");
}
width_att = fz_xml_att(root, "Width");
if (!width_att)
{
fz_free_xml(doc->ctx, root);
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "FixedPage missing required attribute: Width");
}
height_att = fz_xml_att(root, "Height");
if (!height_att)
{
fz_free_xml(doc->ctx, root);
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "FixedPage missing required attribute: Height");
}
page->width = atoi(width_att);
page->height = atoi(height_att);
page->root = root;
}
xps_page *
xps_load_page(xps_document *doc, int number)
{
xps_page *page;
int n = 0;
for (page = doc->first_page; page; page = page->next)
{
if (n == number)
{
doc->current_page = page;
if (!page->root)
xps_load_fixed_page(doc, page);
return page;
}
n ++;
}
fz_throw(doc->ctx, FZ_ERROR_GENERIC, "cannot find page %d", number + 1);
}
fz_rect *
xps_bound_page(xps_document *doc, xps_page *page, fz_rect *bounds)
{
bounds->x0 = bounds->y0 = 0;
bounds->x1 = page->width * 72.0f / 96.0f;
bounds->y1 = page->height * 72.0f / 96.0f;
return bounds;
}
void
xps_free_page(xps_document *doc, xps_page *page)
{
if (page == NULL)
return;
/* only free the XML contents */
if (page->root)
fz_free_xml(doc->ctx, page->root);
page->root = NULL;
}
|