comparison mupdf-source/source/pdf/pdf-util.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2021 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 fz_display_list *
27 pdf_new_display_list_from_annot(fz_context *ctx, pdf_annot *annot)
28 {
29 fz_display_list *list;
30 fz_device *dev = NULL;
31
32 fz_var(dev);
33
34 list = fz_new_display_list(ctx, pdf_bound_annot(ctx, annot));
35
36 fz_try(ctx)
37 {
38 dev = fz_new_list_device(ctx, list);
39 pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
40 fz_close_device(ctx, dev);
41 }
42 fz_always(ctx)
43 {
44 fz_drop_device(ctx, dev);
45 }
46 fz_catch(ctx)
47 {
48 fz_drop_display_list(ctx, list);
49 fz_rethrow(ctx);
50 }
51
52 return list;
53 }
54
55 fz_pixmap *
56 pdf_new_pixmap_from_annot(fz_context *ctx, pdf_annot *annot, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
57 {
58 fz_rect rect;
59 fz_irect bbox;
60 fz_pixmap *pix;
61 fz_device *dev = NULL;
62
63 fz_var(dev);
64
65 rect = pdf_bound_annot(ctx, annot);
66 rect = fz_transform_rect(rect, ctm);
67 bbox = fz_round_rect(rect);
68
69 pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
70 if (alpha)
71 fz_clear_pixmap(ctx, pix);
72 else
73 fz_clear_pixmap_with_value(ctx, pix, 0xFF);
74
75 fz_try(ctx)
76 {
77 dev = fz_new_draw_device(ctx, ctm, pix);
78 pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
79 fz_close_device(ctx, dev);
80 }
81 fz_always(ctx)
82 {
83 fz_drop_device(ctx, dev);
84 }
85 fz_catch(ctx)
86 {
87 fz_drop_pixmap(ctx, pix);
88 fz_rethrow(ctx);
89 }
90
91 return pix;
92 }
93
94 fz_stext_page *
95 pdf_new_stext_page_from_annot(fz_context *ctx, pdf_annot *annot, const fz_stext_options *options)
96 {
97 fz_stext_page *text;
98 fz_device *dev = NULL;
99
100 fz_var(dev);
101
102 if (annot == NULL)
103 return NULL;
104
105 text = fz_new_stext_page(ctx, pdf_bound_annot(ctx, annot));
106 fz_try(ctx)
107 {
108 dev = fz_new_stext_device(ctx, text, options);
109 pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
110 fz_close_device(ctx, dev);
111 }
112 fz_always(ctx)
113 {
114 fz_drop_device(ctx, dev);
115 }
116 fz_catch(ctx)
117 {
118 fz_drop_stext_page(ctx, text);
119 fz_rethrow(ctx);
120 }
121
122 return text;
123 }
124
125 fz_pixmap *
126 pdf_new_pixmap_from_page_contents_with_separations_and_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha, const char *usage, fz_box_type box)
127 {
128 fz_rect rect;
129 fz_irect bbox;
130 fz_pixmap *pix;
131 fz_device *dev = NULL;
132
133 fz_var(dev);
134
135 rect = pdf_bound_page(ctx, page, box);
136 rect = fz_transform_rect(rect, ctm);
137 bbox = fz_round_rect(rect);
138
139 pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
140 if (alpha)
141 fz_clear_pixmap(ctx, pix);
142 else
143 fz_clear_pixmap_with_value(ctx, pix, 0xFF);
144
145 fz_try(ctx)
146 {
147 dev = fz_new_draw_device(ctx, ctm, pix);
148 fz_try(ctx)
149 {
150 pdf_run_page_contents_with_usage(ctx, page, dev, fz_identity, usage, NULL);
151 }
152 fz_catch(ctx)
153 {
154 dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
155 fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
156 fz_ignore_error(ctx);
157 }
158 fz_close_device(ctx, dev);
159 }
160 fz_always(ctx)
161 {
162 fz_drop_device(ctx, dev);
163 }
164 fz_catch(ctx)
165 {
166 fz_drop_pixmap(ctx, pix);
167 fz_rethrow(ctx);
168 }
169
170 return pix;
171 }
172
173 fz_pixmap *
174 pdf_new_pixmap_from_page_contents_with_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha, const char *usage, fz_box_type box)
175 {
176 return pdf_new_pixmap_from_page_contents_with_separations_and_usage(ctx, page, ctm, cs, NULL, alpha, usage, box);
177 }
178
179 fz_pixmap *
180 pdf_new_pixmap_from_page_with_separations_and_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha, const char *usage, fz_box_type box)
181 {
182 fz_rect rect;
183 fz_irect bbox;
184 fz_pixmap *pix;
185 fz_device *dev = NULL;
186
187 fz_var(dev);
188
189 rect = pdf_bound_page(ctx, page, box);
190 rect = fz_transform_rect(rect, ctm);
191 bbox = fz_round_rect(rect);
192
193 pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
194
195 fz_try(ctx)
196 {
197 if (alpha)
198 fz_clear_pixmap(ctx, pix);
199 else
200 fz_clear_pixmap_with_value(ctx, pix, 0xFF);
201
202 dev = fz_new_draw_device(ctx, ctm, pix);
203 fz_try(ctx)
204 {
205 pdf_run_page_with_usage(ctx, page, dev, fz_identity, usage, NULL);
206 }
207 fz_catch(ctx)
208 {
209 dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
210 fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
211 fz_ignore_error(ctx);
212 }
213 fz_close_device(ctx, dev);
214 }
215 fz_always(ctx)
216 {
217 fz_drop_device(ctx, dev);
218 }
219 fz_catch(ctx)
220 {
221 fz_drop_pixmap(ctx, pix);
222 fz_rethrow(ctx);
223 }
224
225 return pix;
226 }
227
228 fz_pixmap *
229 pdf_new_pixmap_from_page_with_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha, const char *usage, fz_box_type box)
230 {
231 return pdf_new_pixmap_from_page_with_separations_and_usage(ctx, page, ctm, cs, NULL, alpha, usage, box);
232 }