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
|
#include "fitz.h"
#include "mupdf.h"
/*
Go through resource dictionary and resolve some levels of
indirect references so we end up with a stylized structure.
The resources referenced are all pre-loaded which inserts
them into the resource store for later lookup when interpreting
content streams.
All resources except colorspaces are automatically inserted
in the resource store when they are parsed. For colorspaces
named in resource dictionaries, we have to insert them ourselves
since we cannot take the risk of having to resolve objects
while in the middle of parsing a content stream.
<<
/Font <<
/F0 1 0 R
/F1 2 0 R
/F2 3 0 R
>>
/ExtGState <<
/Gs0 << ... /Font 1 0 R ... >>
/Gs1 << ... >>
>>
/ColorSpace <<
/Cs0 5 0 R
/Cs1 [ /ICCBased 5 0 R ]
/Cs2 [ /CalRGB << ... >> ]
/CsX [ /Pattern /DeviceRGB ]
>>
/Pattern <<
/Pat0 20 0 R
>>
/Shading <<
/Sh0 30 0 R
>>
/XObject <<
/Im0 10 0 R
/Fm0 11 0 R
>>
>>
*/
static fz_error *
preloadcolorspace(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
fz_colorspace *colorspace;
fz_obj *obj = ref;
if (pdf_finditem(xref->store, PDF_KCOLORSPACE, ref))
return fz_okay;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve colorspace resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
error = pdf_loadcolorspace(&colorspace, xref, obj);
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load colorspace resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
pdf_logrsrc("rsrc colorspace %s\n", colorspace->name);
error = pdf_storeitem(xref->store, PDF_KCOLORSPACE, ref, colorspace);
fz_dropcolorspace(colorspace); /* we did this just to fill the store, no need to hold on to it */
if (error)
return fz_rethrow(error, "cannot store colorspace resource");
return fz_okay;
}
static fz_error *
preloadpattern(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
pdf_pattern *pattern;
fz_shade *shade;
fz_obj *type;
fz_obj *obj = ref;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve pattern/shade resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
type = fz_dictgets(obj, "PatternType");
if (fz_toint(type) == 1)
{
error = pdf_loadpattern(&pattern, xref, obj, ref);
pdf_droppattern(pattern); /* we did this just to fill the store, no need to hold on to it */
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load pattern resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
else if (fz_toint(type) == 2)
{
error = pdf_loadshade(&shade, xref, obj, ref);
fz_dropshade(shade); /* we did this just to fill the store, no need to hold on to it */
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load shade resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
else
{
fz_dropobj(obj);
return fz_throw("unknown pattern resource type");
}
}
static fz_error *
preloadshading(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
fz_shade *shade;
fz_obj *obj = ref;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve shade resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
error = pdf_loadshade(&shade, xref, obj, ref);
fz_dropshade(shade); /* we did this just to fill the store, no need to hold on to it */
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load shade resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
static fz_error *
preloadxobject(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
pdf_xobject *xobject;
pdf_image *image;
fz_obj *obj = ref;
fz_obj *subtype;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve xobject/image resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
subtype = fz_dictgets(obj, "Subtype");
if (!strcmp(fz_toname(subtype), "Form"))
{
error = pdf_loadxobject(&xobject, xref, obj, ref);
pdf_dropxobject(xobject); /* we did this just to fill the store, no need to hold on to it */
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load xobject resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
else if (!strcmp(fz_toname(subtype), "Image"))
{
error = pdf_loadimage(&image, xref, obj, ref);
fz_dropimage((fz_image*)image); /* we did this just to fill the store, no need to hold on to it */
fz_dropobj(obj);
if (error)
return fz_rethrow(error, "cannot load image resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
else
{
fz_dropobj(obj);
return fz_throw("unknown type '%s' for xobject resource (%d %d R)", fz_toname(subtype), fz_tonum(ref), fz_togen(ref));
}
}
static fz_error *
preloadfont(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
pdf_fontdesc *font;
fz_obj *obj = ref;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve font resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
error = pdf_loadfont(&font, xref, obj, ref);
pdf_dropfont(font); /* we did this just to fill the store, no need to hold on to it */
if (error)
return fz_rethrow(error, "cannot load font resource (%d %d R)", fz_tonum(ref), fz_togen(ref));
return fz_okay;
}
static fz_error *
preloadmask(pdf_xref *xref, fz_obj *ref)
{
fz_error *error;
fz_obj *obj = ref;
fz_obj *grp;
error = pdf_resolve(&obj, xref);
if (error)
return fz_rethrow(error, "cannot resolve mask dictionary (%d %d R)", fz_tonum(ref), fz_togen(ref));
if (fz_isdict(obj))
{
grp = fz_dictgets(obj, "G");
if (grp)
{
error = preloadxobject(xref, grp);
if (error)
return fz_rethrow(error, "cannot resolve mask xobject");
}
}
return fz_okay;
}
static fz_error *
scanfontsandmasks(pdf_xref *xref, fz_obj *rdb)
{
fz_error *error;
fz_obj *dict;
fz_obj *obj;
int i;
dict = fz_dictgets(rdb, "ExtGState");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
obj = fz_dictgets(obj, "Font");
if (obj)
{
pdf_logrsrc("extgstate font\n");
error = preloadfont(xref, fz_arrayget(obj, 0));
if (error)
return fz_rethrow(error, "cannot preload font listed in ExtGState");
}
obj = fz_dictgetval(dict, i);
obj = fz_dictgets(obj, "SMask");
if (obj)
{
pdf_logrsrc("extgstate smask\n");
error = preloadmask(xref, obj);
if (error)
return fz_rethrow(error, "cannot preload mask listed in ExtGState");
}
}
}
dict = fz_dictgets(rdb, "Font");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
error = preloadfont(xref, obj);
if (error)
return fz_rethrow(error, "cannot preload font resource");
}
}
return fz_okay;
}
static fz_error *
copyresolved(fz_obj **outp, pdf_xref *xref, fz_obj *dict)
{
fz_error *error;
fz_obj *key, *val, *obj;
fz_obj *copy;
int i;
error = fz_newdict(©, fz_dictlen(dict));
if (error)
return fz_rethrow(error, "cannot create dictionary");
for (i = 0; i < fz_dictlen(dict); i++)
{
key = fz_dictgetkey(dict, i);
val = fz_dictgetval(dict, i);
if (fz_isindirect(val))
{
error = pdf_loadindirect(&obj, xref, val);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load object");
}
error = fz_dictput(copy, key, obj);
fz_dropobj(obj);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot save object");
}
}
else
{
error = fz_dictput(copy, key, val);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot copy object");
}
}
}
*outp = copy;
return fz_okay;
}
fz_error *
pdf_loadresources(fz_obj **rdbp, pdf_xref *xref, fz_obj *orig)
{
fz_error *error;
fz_obj *copy;
fz_obj *old;
fz_obj *new;
fz_obj *dict;
fz_obj *obj;
int i;
/*
* We need a store for resources.
*/
if (!xref->store)
{
error = pdf_newstore(&xref->store);
if (error)
return fz_rethrow(error, "cannot create resource store");
}
pdf_logrsrc("load resources {\n");
/*
* Resolve indirect objects
*/
error = copyresolved(©, xref, orig);
if (error)
return fz_rethrow(error, "cannot resolve indirect objects");
old = fz_dictgets(copy, "ExtGState");
if (old)
{
error = copyresolved(&new, xref, old);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot resolve indirect objects");
}
error = fz_dictputs(copy, "ExtGState", new);
fz_dropobj(new);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot copy extgstate");
}
}
/*
* Load ColorSpace objects
*/
dict = fz_dictgets(copy, "ColorSpace");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
error = preloadcolorspace(xref, obj);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load colorspace resource");
}
}
}
/*
* Load Patterns (and Shadings)
*/
dict = fz_dictgets(copy, "Pattern");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
error = preloadpattern(xref, obj);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load pattern resource");
}
}
}
dict = fz_dictgets(copy, "Shading");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
error = preloadshading(xref, obj);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load shade resource");
}
}
}
/*
* Load XObjects and Images
*/
dict = fz_dictgets(copy, "XObject");
if (dict)
{
for (i = 0; i < fz_dictlen(dict); i++)
{
obj = fz_dictgetval(dict, i);
error = preloadxobject(xref, obj);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load xobject resource");
}
}
}
/*
* Load Font objects
*/
error = scanfontsandmasks(xref, copy);
if (error)
{
fz_dropobj(copy);
return fz_rethrow(error, "cannot load font resources");
}
pdf_logrsrc("}\n");
*rdbp = copy;
return fz_okay;
}
|