/src/mupdf/source/pdf/pdf-xobject.c
Line | Count | Source |
1 | | // Copyright (C) 2004-2024 Artifex Software, Inc. |
2 | | // |
3 | | // This file is part of MuPDF. |
4 | | // |
5 | | // MuPDF is free software: you can redistribute it and/or modify it under the |
6 | | // terms of the GNU Affero General Public License as published by the Free |
7 | | // Software Foundation, either version 3 of the License, or (at your option) |
8 | | // any later version. |
9 | | // |
10 | | // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY |
11 | | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | | // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
13 | | // details. |
14 | | // |
15 | | // You should have received a copy of the GNU Affero General Public License |
16 | | // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> |
17 | | // |
18 | | // Alternative licensing terms are available from the licensor. |
19 | | // For commercial licensing, see <https://www.artifex.com/> or contact |
20 | | // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
21 | | // CA 94129, USA, for further information. |
22 | | |
23 | | #include "mupdf/fitz.h" |
24 | | #include "mupdf/pdf.h" |
25 | | |
26 | | pdf_obj * |
27 | | pdf_xobject_resources(fz_context *ctx, pdf_obj *xobj) |
28 | 0 | { |
29 | 0 | return pdf_dict_get(ctx, xobj, PDF_NAME(Resources)); |
30 | 0 | } |
31 | | |
32 | | fz_rect |
33 | | pdf_xobject_bbox(fz_context *ctx, pdf_obj *xobj) |
34 | 0 | { |
35 | 0 | return pdf_dict_get_rect(ctx, xobj, PDF_NAME(BBox)); |
36 | 0 | } |
37 | | |
38 | | fz_matrix |
39 | | pdf_xobject_matrix(fz_context *ctx, pdf_obj *xobj) |
40 | 0 | { |
41 | 0 | return pdf_dict_get_matrix(ctx, xobj, PDF_NAME(Matrix)); |
42 | 0 | } |
43 | | |
44 | | int pdf_xobject_isolated(fz_context *ctx, pdf_obj *xobj) |
45 | 0 | { |
46 | 0 | pdf_obj *group = pdf_dict_get(ctx, xobj, PDF_NAME(Group)); |
47 | 0 | if (group) |
48 | 0 | return pdf_dict_get_bool(ctx, group, PDF_NAME(I)); |
49 | 0 | return 0; |
50 | 0 | } |
51 | | |
52 | | int pdf_xobject_knockout(fz_context *ctx, pdf_obj *xobj) |
53 | 0 | { |
54 | 0 | pdf_obj *group = pdf_dict_get(ctx, xobj, PDF_NAME(Group)); |
55 | 0 | if (group) |
56 | 0 | return pdf_dict_get_bool(ctx, group, PDF_NAME(K)); |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | | int pdf_xobject_transparency(fz_context *ctx, pdf_obj *xobj) |
61 | 0 | { |
62 | 0 | pdf_obj *group = pdf_dict_get(ctx, xobj, PDF_NAME(Group)); |
63 | 0 | if (group) |
64 | 0 | if (pdf_name_eq(ctx, pdf_dict_get(ctx, group, PDF_NAME(S)), PDF_NAME(Transparency))) |
65 | 0 | return 1; |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | fz_colorspace * |
70 | | pdf_xobject_colorspace(fz_context *ctx, pdf_obj *xobj) |
71 | 0 | { |
72 | 0 | pdf_obj *group = pdf_dict_get(ctx, xobj, PDF_NAME(Group)); |
73 | 0 | if (group) |
74 | 0 | { |
75 | 0 | pdf_obj *cs = pdf_dict_get(ctx, group, PDF_NAME(CS)); |
76 | 0 | if (cs) |
77 | 0 | { |
78 | 0 | fz_colorspace *colorspace = NULL; |
79 | 0 | fz_try(ctx) |
80 | 0 | colorspace = pdf_load_colorspace(ctx, cs); |
81 | 0 | fz_catch(ctx) |
82 | 0 | { |
83 | 0 | fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); |
84 | 0 | fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); |
85 | 0 | fz_report_error(ctx); |
86 | 0 | fz_warn(ctx, "Ignoring XObject blending colorspace."); |
87 | 0 | } |
88 | |
|
89 | 0 | if (!fz_is_valid_blend_colorspace(ctx, colorspace)) |
90 | 0 | { |
91 | 0 | fz_warn(ctx, "Ignoring invalid XObject blending colorspace: %s.", fz_colorspace_name(ctx, colorspace)); |
92 | 0 | fz_drop_colorspace(ctx, colorspace); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | return colorspace; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | return NULL; |
99 | 0 | } |
100 | | |
101 | | pdf_obj * |
102 | | pdf_new_xobject(fz_context *ctx, pdf_document *doc, fz_rect bbox, fz_matrix matrix, pdf_obj *res, fz_buffer *contents) |
103 | 0 | { |
104 | 0 | pdf_obj *ind = NULL; |
105 | 0 | pdf_obj *form = pdf_new_dict(ctx, doc, 5); |
106 | 0 | fz_try(ctx) |
107 | 0 | { |
108 | 0 | pdf_dict_put(ctx, form, PDF_NAME(Type), PDF_NAME(XObject)); |
109 | 0 | pdf_dict_put(ctx, form, PDF_NAME(Subtype), PDF_NAME(Form)); |
110 | 0 | pdf_dict_put_rect(ctx, form, PDF_NAME(BBox), bbox); |
111 | 0 | pdf_dict_put_matrix(ctx, form, PDF_NAME(Matrix), matrix); |
112 | 0 | if (res) |
113 | 0 | pdf_dict_put(ctx, form, PDF_NAME(Resources), res); |
114 | 0 | ind = pdf_add_stream(ctx, doc, contents, form, 0); |
115 | 0 | } |
116 | 0 | fz_always(ctx) |
117 | 0 | pdf_drop_obj(ctx, form); |
118 | 0 | fz_catch(ctx) |
119 | 0 | fz_rethrow(ctx); |
120 | 0 | return ind; |
121 | 0 | } |
122 | | |
123 | | void |
124 | | pdf_update_xobject(fz_context *ctx, pdf_document *doc, pdf_obj *form, fz_rect bbox, fz_matrix matrix, pdf_obj *res, fz_buffer *contents) |
125 | 0 | { |
126 | 0 | pdf_dict_put_rect(ctx, form, PDF_NAME(BBox), bbox); |
127 | 0 | pdf_dict_put_matrix(ctx, form, PDF_NAME(Matrix), matrix); |
128 | 0 | if (res) |
129 | 0 | pdf_dict_put(ctx, form, PDF_NAME(Resources), res); |
130 | 0 | else |
131 | 0 | pdf_dict_del(ctx, form, PDF_NAME(Resources)); |
132 | 0 | pdf_update_stream(ctx, doc, form, contents, 0); |
133 | 0 | } |