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
|
#include "fitz.h"
#include "mupdf.h"
pdf_pattern *
pdf_keep_pattern(pdf_pattern *pat)
{
return (pdf_pattern *)fz_keep_storable(&pat->storable);
}
void
pdf_drop_pattern(fz_context *ctx, pdf_pattern *pat)
{
fz_drop_storable(ctx, &pat->storable);
}
static void
pdf_free_pattern_imp(fz_context *ctx, pdf_pattern *pat)
{
if (pat->resources)
fz_drop_obj(pat->resources);
if (pat->contents)
fz_drop_buffer(ctx, pat->contents);
fz_free(ctx, pat);
}
static unsigned int
pdf_pattern_size(pdf_pattern *pat)
{
if (pat == NULL)
return 0;
return sizeof(*pat) + (pat->contents ? pat->contents->cap : 0);
}
pdf_pattern *
pdf_load_pattern(pdf_xref *xref, fz_obj *dict)
{
pdf_pattern *pat;
fz_obj *obj;
fz_context *ctx = xref->ctx;
if ((pat = fz_find_item(ctx, pdf_free_pattern_imp, dict)))
{
return pat;
}
pat = fz_malloc(ctx, sizeof(pdf_pattern));
FZ_INIT_STORABLE(pat, 1, pdf_free_pattern_imp);
pat->resources = NULL;
pat->contents = NULL;
/* Store pattern now, to avoid possible recursion if objects refer back to this one */
fz_store_item(ctx, dict, pat, pdf_pattern_size(pat));
pat->ismask = fz_to_int(fz_dict_gets(dict, "PaintType")) == 2;
pat->xstep = fz_to_real(fz_dict_gets(dict, "XStep"));
pat->ystep = fz_to_real(fz_dict_gets(dict, "YStep"));
obj = fz_dict_gets(dict, "BBox");
pat->bbox = pdf_to_rect(ctx, obj);
obj = fz_dict_gets(dict, "Matrix");
if (obj)
pat->matrix = pdf_to_matrix(ctx, obj);
else
pat->matrix = fz_identity;
pat->resources = fz_dict_gets(dict, "Resources");
if (pat->resources)
fz_keep_obj(pat->resources);
fz_try(ctx)
{
pat->contents = pdf_load_stream(xref, fz_to_num(dict), fz_to_gen(dict));
}
fz_catch(ctx)
{
fz_remove_item(ctx, pdf_free_pattern_imp, dict);
pdf_drop_pattern(ctx, pat);
fz_throw(ctx, "cannot load pattern stream (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
return pat;
}
|