summaryrefslogtreecommitdiff
path: root/pdf/pdf_page.c
blob: e3bed190329fbf2bbc558bb33c71945acfee87af (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
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
#include "fitz.h"
#include "mupdf.h"

struct info
{
	fz_obj *resources;
	fz_obj *mediabox;
	fz_obj *cropbox;
	fz_obj *rotate;
};

int
pdf_count_pages(pdf_xref *xref)
{
	return xref->page_len;
}

int
pdf_find_page_number(pdf_xref *xref, fz_obj *page)
{
	int i, num = fz_to_num(page);
	for (i = 0; i < xref->page_len; i++)
		if (num == fz_to_num(xref->page_refs[i]))
			return i;
	return -1;
}

static void
put_marker_bool(fz_context *ctx, fz_obj *rdb, char *marker, int val)
{
	fz_obj *tmp;

	tmp = fz_new_bool(ctx, val);
	fz_try(ctx)
	{
		fz_dict_puts(rdb, marker, tmp);
	}
	fz_catch(ctx)
	{
		fz_drop_obj(tmp);
		fz_rethrow(ctx);
	}
	fz_drop_obj(tmp);
}

static void
pdf_load_page_tree_node(pdf_xref *xref, fz_obj *node, struct info info)
{
	fz_obj *dict, *kids, *count;
	fz_obj *obj;
	int i, n;
	fz_context *ctx = xref->ctx;

	/* prevent infinite recursion */
	if (!node || fz_dict_mark(node))
		return;

	fz_try(ctx)
	{
		kids = fz_dict_gets(node, "Kids");
		count = fz_dict_gets(node, "Count");

		if (fz_is_array(kids) && fz_is_int(count))
		{
			obj = fz_dict_gets(node, "Resources");
			if (obj)
				info.resources = obj;
			obj = fz_dict_gets(node, "MediaBox");
			if (obj)
				info.mediabox = obj;
			obj = fz_dict_gets(node, "CropBox");
			if (obj)
				info.cropbox = obj;
			obj = fz_dict_gets(node, "Rotate");
			if (obj)
				info.rotate = obj;

			n = fz_array_len(kids);
			for (i = 0; i < n; i++)
			{
				obj = fz_array_get(kids, i);
				pdf_load_page_tree_node(xref, obj, info);
			}
		}
		else
		{
			dict = fz_resolve_indirect(node);

			if (info.resources && !fz_dict_gets(dict, "Resources"))
				fz_dict_puts(dict, "Resources", info.resources);
			if (info.mediabox && !fz_dict_gets(dict, "MediaBox"))
				fz_dict_puts(dict, "MediaBox", info.mediabox);
			if (info.cropbox && !fz_dict_gets(dict, "CropBox"))
				fz_dict_puts(dict, "CropBox", info.cropbox);
			if (info.rotate && !fz_dict_gets(dict, "Rotate"))
				fz_dict_puts(dict, "Rotate", info.rotate);

			if (xref->page_len == xref->page_cap)
			{
				fz_warn(ctx, "found more pages than expected");
				xref->page_cap ++;
				xref->page_refs = fz_resize_array(ctx, xref->page_refs, xref->page_cap, sizeof(fz_obj*));
				xref->page_objs = fz_resize_array(ctx, xref->page_objs, xref->page_cap, sizeof(fz_obj*));
			}

			xref->page_refs[xref->page_len] = fz_keep_obj(node);
			xref->page_objs[xref->page_len] = fz_keep_obj(dict);
			xref->page_len ++;
		}
	}
	fz_catch(ctx)
	{
		fz_dict_unmark(node);
		fz_rethrow(ctx);
	}
	fz_dict_unmark(node);
}

void
pdf_load_page_tree(pdf_xref *xref)
{
	struct info info;
	fz_context *ctx = xref->ctx;
	fz_obj *catalog = fz_dict_gets(xref->trailer, "Root");
	fz_obj *pages = fz_dict_gets(catalog, "Pages");
	fz_obj *count = fz_dict_gets(pages, "Count");

	if (!fz_is_dict(pages))
		fz_throw(ctx, "missing page tree");
	if (!fz_is_int(count))
		fz_throw(ctx, "missing page count");

	xref->page_cap = fz_to_int(count);
	xref->page_len = 0;
	xref->page_refs = fz_malloc_array(ctx, xref->page_cap, sizeof(fz_obj*));
	xref->page_objs = fz_malloc_array(ctx, xref->page_cap, sizeof(fz_obj*));

	info.resources = NULL;
	info.mediabox = NULL;
	info.cropbox = NULL;
	info.rotate = NULL;

	pdf_load_page_tree_node(xref, pages, info);
}

/* We need to know whether to install a page-level transparency group */

static int pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb);

static int
pdf_extgstate_uses_blending(fz_context *ctx, fz_obj *dict)
{
	fz_obj *obj = fz_dict_gets(dict, "BM");
	if (fz_is_name(obj) && strcmp(fz_to_name(obj), "Normal"))
		return 1;
	return 0;
}

static int
pdf_pattern_uses_blending(fz_context *ctx, fz_obj *dict)
{
	fz_obj *obj;
	obj = fz_dict_gets(dict, "Resources");
	if (pdf_resources_use_blending(ctx, obj))
		return 1;
	obj = fz_dict_gets(dict, "ExtGState");
	return pdf_extgstate_uses_blending(ctx, obj);
}

static int
pdf_xobject_uses_blending(fz_context *ctx, fz_obj *dict)
{
	fz_obj *obj = fz_dict_gets(dict, "Resources");
	return pdf_resources_use_blending(ctx, obj);
}

static int
pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb)
{
	fz_obj *obj;
	int i, n, useBM = 0;

	if (!rdb)
		return 0;

	/* Have we been here before and stashed an answer? */
	obj = fz_dict_gets(rdb, ".useBM");
	if (obj)
		return fz_to_bool(obj);

	/* stop on cyclic resource dependencies */
	if (fz_dict_mark(rdb))
		return 0;

	fz_try(ctx)
	{
		obj = fz_dict_gets(rdb, "ExtGState");
		n = fz_dict_len(obj);
		for (i = 0; i < n; i++)
			if (pdf_extgstate_uses_blending(ctx, fz_dict_get_val(obj, i)))
				goto found;

		obj = fz_dict_gets(rdb, "Pattern");
		n = fz_dict_len(obj);
		for (i = 0; i < n; i++)
			if (pdf_pattern_uses_blending(ctx, fz_dict_get_val(obj, i)))
				goto found;

		obj = fz_dict_gets(rdb, "XObject");
		n = fz_dict_len(obj);
		for (i = 0; i < n; i++)
			if (pdf_xobject_uses_blending(ctx, fz_dict_get_val(obj, i)))
				goto found;
		if (0)
		{
found:
			useBM = 1;
		}
	}
	fz_catch(ctx)
	{
		fz_dict_unmark(rdb);
		fz_rethrow(ctx);
	}
	fz_dict_unmark(rdb);

	put_marker_bool(ctx, rdb, ".useBM", useBM);
	return useBM;
}

/* we need to combine all sub-streams into one for the content stream interpreter */

static fz_buffer *
pdf_load_page_contents_array(pdf_xref *xref, fz_obj *list)
{
	fz_buffer *big;
	fz_buffer *one;
	int i, n;
	fz_context *ctx = xref->ctx;

	big = fz_new_buffer(ctx, 32 * 1024);

	n = fz_array_len(list);
	for (i = 0; i < n; i++)
	{
		fz_obj *stm = fz_array_get(list, i);
		fz_try(ctx)
		{
			one = pdf_load_stream(xref, fz_to_num(stm), fz_to_gen(stm));
		}
		fz_catch(ctx)
		{
			fz_warn(ctx, "cannot load content stream part %d/%d", i + 1, n);
			continue;
		}

		if (big->len + one->len + 1 > big->cap)
			fz_resize_buffer(ctx, big, big->len + one->len + 1);
		memcpy(big->data + big->len, one->data, one->len);
		big->data[big->len + one->len] = ' ';
		big->len += one->len + 1;

		fz_drop_buffer(ctx, one);
	}

	if (n > 0 && big->len == 0)
	{
		fz_drop_buffer(ctx, big);
		fz_throw(ctx, "cannot load content stream");
	}

	return big;
}

static fz_buffer *
pdf_load_page_contents(pdf_xref *xref, fz_obj *obj)
{
	fz_context *ctx = xref->ctx;

	if (fz_is_array(obj))
	{
		return pdf_load_page_contents_array(xref, obj);
		/* RJW: "cannot load content stream array" */
	}
	else if (pdf_is_stream(xref, fz_to_num(obj), fz_to_gen(obj)))
	{
		return pdf_load_stream(xref, fz_to_num(obj), fz_to_gen(obj));
		/* RJW: "cannot load content stream (%d 0 R)", fz_to_num(obj) */
	}

	fz_warn(ctx, "page contents missing, leaving page blank");
	return fz_new_buffer(ctx, 0);
}

pdf_page *
pdf_load_page(pdf_xref *xref, int number)
{
	pdf_page *page;
	pdf_annot *annot;
	fz_obj *pageobj, *pageref;
	fz_obj *obj;
	fz_rect mediabox, cropbox;
	fz_context *ctx = xref->ctx;

	if (number < 0 || number >= xref->page_len)
		fz_throw(ctx, "cannot find page %d", number + 1);

	pageobj = xref->page_objs[number];
	pageref = xref->page_refs[number];

	page = fz_malloc_struct(ctx, pdf_page);
	page->resources = NULL;
	page->contents = NULL;
	page->transparency = 0;
	page->links = NULL;
	page->annots = NULL;

	mediabox = pdf_to_rect(ctx, fz_dict_gets(pageobj, "MediaBox"));
	if (fz_is_empty_rect(mediabox))
	{
		fz_warn(ctx, "cannot find page size for page %d", number + 1);
		mediabox.x0 = 0;
		mediabox.y0 = 0;
		mediabox.x1 = 612;
		mediabox.y1 = 792;
	}

	cropbox = pdf_to_rect(ctx, fz_dict_gets(pageobj, "CropBox"));
	if (!fz_is_empty_rect(cropbox))
		mediabox = fz_intersect_rect(mediabox, cropbox);

	page->mediabox.x0 = MIN(mediabox.x0, mediabox.x1);
	page->mediabox.y0 = MIN(mediabox.y0, mediabox.y1);
	page->mediabox.x1 = MAX(mediabox.x0, mediabox.x1);
	page->mediabox.y1 = MAX(mediabox.y0, mediabox.y1);

	if (page->mediabox.x1 - page->mediabox.x0 < 1 || page->mediabox.y1 - page->mediabox.y0 < 1)
	{
		fz_warn(ctx, "invalid page size in page %d", number + 1);
		page->mediabox = fz_unit_rect;
	}

	page->rotate = fz_to_int(fz_dict_gets(pageobj, "Rotate"));

	obj = fz_dict_gets(pageobj, "Annots");
	if (obj)
	{
		pdf_load_links(&page->links, xref, obj);
		pdf_load_annots(&page->annots, xref, obj);
	}

	page->resources = fz_dict_gets(pageobj, "Resources");
	if (page->resources)
		fz_keep_obj(page->resources);

	obj = fz_dict_gets(pageobj, "Contents");
	fz_try(ctx)
	{
		page->contents = pdf_load_page_contents(xref, obj);

		if (pdf_resources_use_blending(ctx, page->resources))
			page->transparency = 1;

		for (annot = page->annots; annot && !page->transparency; annot = annot->next)
			if (pdf_resources_use_blending(ctx, annot->ap->resources))
				page->transparency = 1;
	}
	fz_catch(ctx)
	{
		pdf_free_page(ctx, page);
		fz_throw(ctx, "cannot load page %d contents (%d 0 R)", number + 1, fz_to_num(pageref));
	}

	return page;
}

void
pdf_free_page(fz_context *ctx, pdf_page *page)
{
	if (page->resources)
		fz_drop_obj(page->resources);
	if (page->contents)
		fz_drop_buffer(ctx, page->contents);
	if (page->links)
		fz_free_link(ctx, page->links);
	if (page->annots)
		pdf_free_annot(ctx, page->annots);
	fz_free(ctx, page);
}