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

fz_error
pdf_load_xobject(pdf_xobject **formp, pdf_xref *xref, fz_obj *dict)
{
	fz_error error;
	pdf_xobject *form;
	fz_obj *obj;

	if ((*formp = pdf_find_item(xref->store, pdf_drop_xobject, dict)))
	{
		pdf_keep_xobject(*formp);
		return fz_okay;
	}

	form = fz_malloc(sizeof(pdf_xobject));
	form->refs = 1;
	form->resources = NULL;
	form->contents = NULL;
	form->colorspace = NULL;

	/* Store item immediately, to avoid possible recursion if objects refer back to this one */
	pdf_store_item(xref->store, pdf_keep_xobject, pdf_drop_xobject, dict, form);

	obj = fz_dict_gets(dict, "BBox");
	form->bbox = pdf_to_rect(obj);

	obj = fz_dict_gets(dict, "Matrix");
	if (obj)
		form->matrix = pdf_to_matrix(obj);
	else
		form->matrix = fz_identity;

	form->isolated = 0;
	form->knockout = 0;
	form->transparency = 0;

	obj = fz_dict_gets(dict, "Group");
	if (obj)
	{
		fz_obj *attrs = obj;

		form->isolated = fz_to_bool(fz_dict_gets(attrs, "I"));
		form->knockout = fz_to_bool(fz_dict_gets(attrs, "K"));

		obj = fz_dict_gets(attrs, "S");
		if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Transparency"))
			form->transparency = 1;

		obj = fz_dict_gets(attrs, "CS");
		if (obj)
		{
			error = pdf_load_colorspace(&form->colorspace, xref, obj);
			if (error)
				fz_catch(error, "cannot load xobject colorspace");
		}
	}

	form->resources = fz_dict_gets(dict, "Resources");
	if (form->resources)
		fz_keep_obj(form->resources);

	error = pdf_load_stream(&form->contents, xref, fz_to_num(dict), fz_to_gen(dict));
	if (error)
	{
		pdf_remove_item(xref->store, pdf_drop_xobject, dict);
		pdf_drop_xobject(form);
		return fz_rethrow(error, "cannot load xobject content stream (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
	}

	*formp = form;
	return fz_okay;
}

pdf_xobject *
pdf_keep_xobject(pdf_xobject *xobj)
{
	xobj->refs ++;
	return xobj;
}

void
pdf_drop_xobject(pdf_xobject *xobj)
{
	if (xobj && --xobj->refs == 0)
	{
		if (xobj->colorspace)
			fz_drop_colorspace(xobj->colorspace);
		if (xobj->resources)
			fz_drop_obj(xobj->resources);
		if (xobj->contents)
			fz_drop_buffer(xobj->contents);
		fz_free(xobj);
	}
}