comparison mupdf-source/source/pdf/pdf-graft.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 #include <assert.h>
27
28 struct pdf_graft_map
29 {
30 int refs;
31 int len;
32 pdf_document *src;
33 pdf_document *dst;
34 int *dst_from_src;
35 };
36
37 pdf_graft_map *
38 pdf_new_graft_map(fz_context *ctx, pdf_document *dst)
39 {
40 pdf_graft_map *map = NULL;
41
42 if (dst == NULL)
43 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create graft make without a destination document");
44
45 map = fz_malloc_struct(ctx, pdf_graft_map);
46
47 map->dst = pdf_keep_document(ctx, dst);
48 map->refs = 1;
49 return map;
50 }
51
52 pdf_graft_map *
53 pdf_keep_graft_map(fz_context *ctx, pdf_graft_map *map)
54 {
55 return fz_keep_imp(ctx, map, &map->refs);
56 }
57
58 void
59 pdf_drop_graft_map(fz_context *ctx, pdf_graft_map *map)
60 {
61 if (fz_drop_imp(ctx, map, &map->refs))
62 {
63 pdf_drop_document(ctx, map->src);
64 pdf_drop_document(ctx, map->dst);
65 fz_free(ctx, map->dst_from_src);
66 fz_free(ctx, map);
67 }
68 }
69
70 pdf_obj *
71 pdf_graft_object(fz_context *ctx, pdf_document *dst, pdf_obj *obj)
72 {
73 pdf_document *src;
74 pdf_graft_map *map;
75
76 /* Primitive objects are not bound to a document, so can be re-used as is. */
77 src = pdf_get_bound_document(ctx, obj);
78 if (src == NULL)
79 return pdf_keep_obj(ctx, obj);
80
81 map = pdf_new_graft_map(ctx, dst);
82
83 fz_try(ctx)
84 obj = pdf_graft_mapped_object(ctx, map, obj);
85 fz_always(ctx)
86 pdf_drop_graft_map(ctx, map);
87 fz_catch(ctx)
88 fz_rethrow(ctx);
89
90 return obj;
91 }
92
93 pdf_obj *
94 pdf_graft_mapped_object(fz_context *ctx, pdf_graft_map *map, pdf_obj *obj)
95 {
96 pdf_obj *val, *key;
97 pdf_obj *new_obj = NULL;
98 pdf_obj *new_dict;
99 pdf_obj *new_array;
100 pdf_obj *ref = NULL;
101 fz_buffer *buffer = NULL;
102 pdf_document *src;
103 int new_num, src_num, len, i;
104
105 /* Primitive objects are not bound to a document, so can be re-used as is. */
106 src = pdf_get_bound_document(ctx, obj);
107 if (!src)
108 return pdf_keep_obj(ctx, obj);
109
110 if (map->src && src != map->src)
111 fz_throw(ctx, FZ_ERROR_ARGUMENT, "grafted objects must all belong to the same source document");
112
113 if (pdf_is_indirect(ctx, obj))
114 {
115 src_num = pdf_to_num(ctx, obj);
116
117 if (map->src == NULL)
118 {
119 fz_try(ctx)
120 {
121 map->src = pdf_keep_document(ctx, src);
122 map->len = pdf_xref_len(ctx, src);
123 map->dst_from_src = fz_calloc(ctx, map->len, sizeof(int));
124 }
125 fz_catch(ctx)
126 {
127 pdf_drop_document(ctx, map->src);
128 map->src = NULL;
129 fz_rethrow(ctx);
130 }
131 }
132
133 if (src_num < 1 || src_num >= map->len)
134 fz_throw(ctx, FZ_ERROR_ARGUMENT, "source object number out of range");
135
136 /* Check if we have done this one. If yes, then just
137 * return our indirect ref */
138 if (map->dst_from_src[src_num] != 0)
139 {
140 int dest_num = map->dst_from_src[src_num];
141 return pdf_new_indirect(ctx, map->dst, dest_num, 0);
142 }
143
144 fz_var(buffer);
145 fz_var(ref);
146 fz_var(new_obj);
147
148 fz_try(ctx)
149 {
150 /* Create new slot for our src object, set the mapping and call again
151 * using the resolved indirect reference */
152 new_num = pdf_create_object(ctx, map->dst);
153 map->dst_from_src[src_num] = new_num;
154 new_obj = pdf_graft_mapped_object(ctx, map, pdf_resolve_indirect(ctx, obj));
155
156 /* Return a ref to the new_obj making sure to attach any stream */
157 pdf_update_object(ctx, map->dst, new_num, new_obj);
158 ref = pdf_new_indirect(ctx, map->dst, new_num, 0);
159 if (pdf_is_stream(ctx, obj))
160 {
161 buffer = pdf_load_raw_stream_number(ctx, src, src_num);
162 pdf_update_stream(ctx, map->dst, ref, buffer, 1);
163 }
164 }
165 fz_always(ctx)
166 {
167 pdf_drop_obj(ctx, new_obj);
168 fz_drop_buffer(ctx, buffer);
169 }
170 fz_catch(ctx)
171 {
172 pdf_drop_obj(ctx, ref);
173 fz_rethrow(ctx);
174 }
175 return ref;
176 }
177 else if (pdf_is_dict(ctx, obj))
178 {
179 len = pdf_dict_len(ctx, obj);
180 new_dict = pdf_new_dict(ctx, map->dst, len);
181
182 fz_try(ctx)
183 {
184 for (i = 0; i < len; i++)
185 {
186 key = pdf_dict_get_key(ctx, obj, i);
187 val = pdf_dict_get_val(ctx, obj, i);
188 pdf_dict_put_drop(ctx, new_dict, key, pdf_graft_mapped_object(ctx, map, val));
189 }
190 }
191 fz_catch(ctx)
192 {
193 pdf_drop_obj(ctx, new_dict);
194 fz_rethrow(ctx);
195 }
196 return new_dict;
197 }
198 else if (pdf_is_array(ctx, obj))
199 {
200 /* Step through the array items handling indirect refs */
201 len = pdf_array_len(ctx, obj);
202 new_array = pdf_new_array(ctx, map->dst, len);
203
204 fz_try(ctx)
205 {
206 for (i = 0; i < len; i++)
207 {
208 val = pdf_array_get(ctx, obj, i);
209 pdf_array_push_drop(ctx, new_array, pdf_graft_mapped_object(ctx, map, val));
210 }
211 }
212 fz_catch(ctx)
213 {
214 pdf_drop_obj(ctx, new_array);
215 fz_rethrow(ctx);
216 }
217 return new_array;
218 }
219 else
220 {
221 assert("This never happens" == NULL);
222 return NULL;
223 }
224 }
225
226 void pdf_graft_mapped_page(fz_context *ctx, pdf_graft_map *map, int page_to, pdf_document *src, int page_from)
227 {
228 pdf_obj *page_ref;
229 pdf_obj *page_dict = NULL;
230 pdf_obj *obj;
231 pdf_obj *ref = NULL;
232 int i;
233 pdf_document *dst = map->dst;
234
235 /* Copy as few key/value pairs as we can. Do not include items that reference other pages. */
236 static pdf_obj * const copy_list[] = {
237 PDF_NAME(Contents),
238 PDF_NAME(Resources),
239 PDF_NAME(MediaBox),
240 PDF_NAME(CropBox),
241 PDF_NAME(BleedBox),
242 PDF_NAME(TrimBox),
243 PDF_NAME(ArtBox),
244 PDF_NAME(Rotate),
245 PDF_NAME(UserUnit)
246 };
247
248 fz_var(ref);
249 fz_var(page_dict);
250
251 fz_try(ctx)
252 {
253 page_ref = pdf_lookup_page_obj(ctx, src, page_from);
254
255 /* Make a new page object dictionary to hold the items we copy from the source page. */
256 page_dict = pdf_new_dict(ctx, dst, 4);
257
258 pdf_dict_put(ctx, page_dict, PDF_NAME(Type), PDF_NAME(Page));
259
260 for (i = 0; i < (int)nelem(copy_list); i++)
261 {
262 obj = pdf_dict_get_inheritable(ctx, page_ref, copy_list[i]);
263 if (obj != NULL)
264 pdf_dict_put_drop(ctx, page_dict, copy_list[i], pdf_graft_mapped_object(ctx, map, obj));
265 }
266
267 /* Add the page object to the destination document. */
268 ref = pdf_add_object(ctx, dst, page_dict);
269
270 /* Insert it into the page tree. */
271 pdf_insert_page(ctx, dst, page_to, ref);
272 }
273 fz_always(ctx)
274 {
275 pdf_drop_obj(ctx, page_dict);
276 pdf_drop_obj(ctx, ref);
277 }
278 fz_catch(ctx)
279 {
280 fz_rethrow(ctx);
281 }
282 }
283
284 void pdf_graft_page(fz_context *ctx, pdf_document *dst, int page_to, pdf_document *src, int page_from)
285 {
286 pdf_graft_map *map = pdf_new_graft_map(ctx, dst);
287 fz_try(ctx)
288 pdf_graft_mapped_page(ctx, map, page_to, src, page_from);
289 fz_always(ctx)
290 pdf_drop_graft_map(ctx, map);
291 fz_catch(ctx)
292 fz_rethrow(ctx);
293 }