1#include "mupdf/fitz.h"
2#include "mupdf/pdf.h"
3
4pdf_pattern *
5pdf_keep_pattern(fz_context *ctx, pdf_pattern *pat)
6{
7 return fz_keep_storable(ctx, &pat->storable);
8}
9
10void
11pdf_drop_pattern(fz_context *ctx, pdf_pattern *pat)
12{
13 fz_drop_storable(ctx, &pat->storable);
14}
15
16static void
17pdf_drop_pattern_imp(fz_context *ctx, fz_storable *pat_)
18{
19 pdf_pattern *pat = (pdf_pattern *)pat_;
20 pdf_drop_obj(ctx, pat->resources);
21 pdf_drop_obj(ctx, pat->contents);
22 fz_free(ctx, pat);
23}
24
25static unsigned int
26pdf_pattern_size(pdf_pattern *pat)
27{
28 if (pat == NULL)
29 return 0;
30 return sizeof(*pat);
31}
32
33pdf_pattern *
34pdf_load_pattern(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
35{
36 pdf_pattern *pat;
37
38 if ((pat = pdf_find_item(ctx, pdf_drop_pattern_imp, dict)) != NULL)
39 {
40 return pat;
41 }
42
43 pat = fz_malloc_struct(ctx, pdf_pattern);
44 FZ_INIT_STORABLE(pat, 1, pdf_drop_pattern_imp);
45 pat->document = doc;
46 pat->resources = NULL;
47 pat->contents = NULL;
48 pat->id = pdf_to_num(ctx, dict);
49
50 fz_try(ctx)
51 {
52 /* Store pattern now, to avoid possible recursion if objects refer back to this one */
53 pdf_store_item(ctx, dict, pat, pdf_pattern_size(pat));
54
55 pat->ismask = pdf_dict_get_int(ctx, dict, PDF_NAME(PaintType)) == 2;
56 pat->xstep = pdf_dict_get_real(ctx, dict, PDF_NAME(XStep));
57 pat->ystep = pdf_dict_get_real(ctx, dict, PDF_NAME(YStep));
58 pat->bbox = pdf_dict_get_rect(ctx, dict, PDF_NAME(BBox));
59 pat->matrix = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix));
60
61 pat->resources = pdf_dict_get(ctx, dict, PDF_NAME(Resources));
62 if (pat->resources)
63 pdf_keep_obj(ctx, pat->resources);
64
65 pat->contents = pdf_keep_obj(ctx, dict);
66 }
67 fz_catch(ctx)
68 {
69 pdf_remove_item(ctx, pdf_drop_pattern_imp, dict);
70 pdf_drop_pattern(ctx, pat);
71 fz_rethrow(ctx);
72 }
73 return pat;
74}
75