Debian Codegraph @

texk/dvipdfm-x/epdf.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2025 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 /*
24 * Concatinating content streams are only supported for streams that only uses
25 * single FlateDecode filter, i.e.,
26 *
27 * /Filter /FlateDecode or /Filter [/FlateDecode]
28 *
29 * TrimBox, BleedBox, ArtBox, Rotate ...
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "system.h"
37 #include "mem.h"
38 #include "mfileio.h"
39 #include "error.h"
40 #include "dvipdfmx.h"
41
42 #include "pdfobj.h"
43 #include "pdfdev.h"
44 #include "pdfdoc.h"
45
46 #include "pdfdraw.h"
47 #include "pdfparse.h"
48
49 #include "pdfximage.h"
50
51 #include "epdf.h"
52
53 static pdf_obj*
54 get_page_content (pdf_file *pf, pdf_obj* page)
55 {
56 pdf_obj *contents, *content_new;
57
58 contents = pdf_deref_obj(pdf_lookup_dict(page, "Contents"));
59 if (!contents)
60 return NULL;
61
62 if (PDF_OBJ_NULLTYPE(contents)) {
63 /* empty page */
64 pdf_release_obj(contents);
65 /* TODO: better don't include anything if the page is empty */
66 contents = pdf_new_stream(0);
67 } else if (PDF_OBJ_ARRAYTYPE(contents)) {
68 /*
69 * Concatenate all content streams.
70 */
71 pdf_obj *content_seg;
72 int i;
73 content_new = pdf_new_stream(STREAM_COMPRESS);
74 for (i = 0; i < pdf_array_length(contents); i++) {
75 content_seg = pdf_deref_obj(pdf_get_array(contents, i));
76 if (!content_seg) {
77 WARN("Could not read page content stream.");
78 pdf_release_obj(content_new);
79 pdf_release_obj(contents);
80 return NULL;
81 }
82 if (PDF_OBJ_STREAMTYPE(content_seg)) {
83 pdf_concat_stream(content_new, content_seg);
84 } else if (!PDF_OBJ_NULLTYPE(content_seg)) {
85 WARN("Page content not a stream object. Broken PDF file?");
86 pdf_release_obj(content_seg);
87 pdf_release_obj(content_new);
88 pdf_release_obj(contents);
89 return NULL;
90 }
91 pdf_release_obj(content_seg);
92 }
93 pdf_release_obj(contents);
94 contents = content_new;
95 } else {
96 if (!PDF_OBJ_STREAMTYPE(contents)) {
97 WARN("Page content not a stream object. Broken PDF file?");
98 pdf_release_obj(contents);
99 return NULL;
100 }
101 /* Flate the contents if necessary. */
102 content_new = pdf_new_stream(STREAM_COMPRESS);
103 pdf_concat_stream(content_new, contents);
104 pdf_release_obj(contents);
105 contents = content_new;
106 }
107
108 return contents;
109 }
110
111 /* ximage here is the result. DONT USE IT FOR PASSING OPTIONS! */
112 int
113 pdf_include_page (pdf_ximage *ximage,
114 FILE *image_file,
115 const char *ident,
116 load_options options)
117 {
118 pdf_file *pf;
119 xform_info info;
120 pdf_obj *contents = NULL, *catalog;
121 pdf_obj *page = NULL, *resources = NULL, *markinfo = NULL;
122 pdf_obj *group = NULL, *group_obj = NULL;
123
124 pf = pdf_open(ident, image_file);
125 if (!pf)
126 return -1;
127
128 pdf_ximage_init_form_info(&info);
129
130 if (options.page_no == 0)
131 options.page_no = 1;
132 page = pdf_doc_get_page(pf,
133 options.page_no, options.page_name,
134 options.bbox_type, &info.bbox, &info.matrix,
135 &resources);
136
137 if(!page)
138 goto error_silent;
139
140 catalog = pdf_file_get_catalog(pf);
141 markinfo = pdf_deref_obj(pdf_lookup_dict(catalog, "MarkInfo"));
142 if (markinfo) {
143 pdf_obj *tmp = pdf_deref_obj(pdf_lookup_dict(markinfo, "Marked"));
144 pdf_release_obj(markinfo);
145 if (!PDF_OBJ_BOOLEANTYPE(tmp)) {
146 if (tmp)
147 pdf_release_obj(tmp);
148 goto error;
149 } else if (pdf_boolean_value(tmp)) {
150 WARN("PDF file is tagged... Ignoring tags.");
151 }
152 pdf_release_obj(tmp);
153 }
154
155 /*
156 * Handle page's Group
157 */
158 group_obj = pdf_lookup_dict(page, "Group");
159 if (group_obj)
160 group = pdf_import_object(group_obj);
161 /*
162 * Handle page content stream.
163 */
164 contents = get_page_content(pf, page);
165 pdf_release_obj(page);
166 page = NULL;
167
168 /*
169 * Add entries to contents stream dictionary.
170 */
171 {
172 pdf_obj *contents_dict, *bbox, *matrix;
173
174 contents_dict = pdf_stream_dict(contents);
175 pdf_add_dict(contents_dict,
176 pdf_new_name("Type"), pdf_new_name("XObject"));
177 pdf_add_dict(contents_dict,
178 pdf_new_name("Subtype"), pdf_new_name("Form"));
179 pdf_add_dict(contents_dict,
180 pdf_new_name("FormType"), pdf_new_number(1.0));
181
182 bbox = pdf_new_array();
183 pdf_add_array(bbox, pdf_new_number(info.bbox.llx));
184 pdf_add_array(bbox, pdf_new_number(info.bbox.lly));
185 pdf_add_array(bbox, pdf_new_number(info.bbox.urx));
186 pdf_add_array(bbox, pdf_new_number(info.bbox.ury));
187
188 pdf_add_dict(contents_dict, pdf_new_name("BBox"), bbox);
189
190 matrix = pdf_new_array();
191 pdf_add_array(matrix, pdf_new_number(info.matrix.a));
192 pdf_add_array(matrix, pdf_new_number(info.matrix.b));
193 pdf_add_array(matrix, pdf_new_number(info.matrix.c));
194 pdf_add_array(matrix, pdf_new_number(info.matrix.d));
195 pdf_add_array(matrix, pdf_new_number(info.matrix.e));
196 pdf_add_array(matrix, pdf_new_number(info.matrix.f));
197
198 pdf_add_dict(contents_dict, pdf_new_name("Matrix"), matrix);
199
200 pdf_add_dict(contents_dict, pdf_new_name("Resources"),
201 pdf_import_object(resources));
202 pdf_release_obj(resources);
203
204 if (group)
205 pdf_add_dict(contents_dict, pdf_new_name("Group"), group);
206
207 if (options.dict)
208 pdf_merge_dict(contents_dict, options.dict);
209 }
210
211 pdf_close(pf);
212
213 pdf_ximage_set_form(ximage, &info, contents);
214
215 return 0;
216
217 error:
218 WARN("Cannot parse document. Broken PDF file?");
219 error_silent:
220 if (resources)
221 pdf_release_obj(resources);
222 if (markinfo)
223 pdf_release_obj(markinfo);
224 if (page)
225 pdf_release_obj(page);
226 if (group)
227 pdf_release_obj(group);
228 if (contents)
229 pdf_release_obj(contents);
230
231 pdf_close(pf);
232
233 return -1;
234 }
235
236 enum action {
237 action_unknown,
238 action_discard,
239 action_path,
240 action_rect,
241 action_trans,
242 action_clip,
243 action_save,
244 action_restore
245 };
246
247 static struct operator
248 {
249 const char *token;
250 enum action action;
251 int n_args;
252 } operators[] = {
253 {"b*", action_clip, 0},
254 {"B*", action_clip, 0},
255 {"cm", action_trans, 6},
256 {"f*", action_clip, 0},
257 {"re", action_rect, 4},
258 {"W*", action_path, 0},
259 {"b", action_clip, 0},
260 {"B", action_clip, 0},
261 {"c", action_path, 6},
262 {"f", action_clip, 0},
263 {"F", action_clip, 0},
264 {"h", action_path, 0},
265 {"l", action_path, 2},
266 {"m", action_path, 2},
267 {"n", action_path, 0},
268 {"q", action_save, 0},
269 {"Q", action_restore, 0},
270 {"s", action_clip, 0},
271 {"S", action_clip, 0},
272 {"v", action_path, 4},
273 {"W", action_path, 0},
274 {"y", action_path, 4}
275 };
276
277 #include "dpxutil.h"
278
279 static int
280 get_numbers_from_stack (dpx_stack *stack, double *v, int n)
281 {
282 int error = 0;
283 int i;
284
285 for (i = 0; i < n; i++) {
286 pdf_obj *obj;
287 obj = dpx_stack_pop(stack);
288 if (!obj) {
289 error = -1;
290 break;
291 } else if (!PDF_OBJ_NUMBERTYPE(obj)) {
292 pdf_release_obj(obj);
293 error = -1;
294 break;
295 }
296 v[n-i-1] = pdf_number_value(obj);
297 pdf_release_obj(obj);
298 }
299 return error;
300 }
301
302 int
303 pdf_copy_clip (FILE *image_file, int pageNo, double x_user, double y_user)
304 {
305 pdf_obj *page_tree, *contents;
306 int depth = 0;
307 const char *p, *endptr;
308 pdf_tmatrix M;
309 pdf_rect bbox;
310 pdf_tmatrix mtrx;
311 pdf_file *pf;
312 dpx_stack stack;
313 int error = 0;
314
315 pf = pdf_open(NULL, image_file);
316 if (!pf)
317 return -1;
318
319 pdf_dev_currentmatrix(&M);
320 pdf_invertmatrix(&M);
321 M.e += x_user; M.f += y_user;
322
323 page_tree = pdf_doc_get_page(pf, pageNo, NULL, 0, &bbox, &mtrx, NULL);
324 if (!page_tree) {
325 pdf_close(pf);
326 return -1;
327 }
328
329 contents = get_page_content(pf, page_tree);
330 pdf_release_obj(page_tree);
331 if (!contents) {
332 pdf_close(pf);
333 return -1;
334 }
335
336 pdf_doc_add_page_content(" ", 1);
337
338 p = pdf_stream_dataptr(contents);
339 endptr = p + pdf_stream_length(contents);
340 depth = 0;
341 dpx_stack_init(&stack);
342
343 skip_white(&p, endptr);
344 while (p < endptr && !error) {
345 enum action action = action_discard;
346 char *token = NULL;
347 pdf_obj *obj = NULL;
348 int n_args = 0;
349 char buf[1024];
350 size_t len = 0;
351
352 if (depth > 1) {
353 if (*p == 'q')
354 depth++;
355 if (*p == 'Q')
356 depth--;
357 token = parse_ident(&p, endptr);
358 skip_white(&p, endptr);
359 RELEASE(token);
360 continue;
361 }
362
363 switch (*p) {
364 case '-': case '+': case'.':
365 case '0': case '1': case '2': case '3': case '4':
366 case '5': case '6': case '7': case '8': case '9':
367 obj = parse_pdf_number(&p, endptr);
368 break;
369 case '[':
370 obj = parse_pdf_array(&p, endptr, NULL); /* No indirect reference allowed here */
371 break;
372 case '/':
373 obj = parse_pdf_name(&p, endptr);
374 break;
375 case '(':
376 obj = parse_pdf_string(&p, endptr);
377 break;
378 case '<':
379 if (p < endptr - 1 && p[1] == '<') {
380 obj = parse_pdf_dict(&p, endptr, NULL);
381 } else {
382 obj = parse_pdf_string(&p, endptr);
383 }
384 break;
385 }
386 if (obj) {
387 skip_white(&p, endptr);
388 dpx_stack_push(&stack, obj);
389 continue;
390 }
391
392 /* operator */
393 token = parse_ident(&p, endptr);
394 skip_white(&p, endptr);
395 if (!token) {
396 break;
397 } else {
398 int i;
399 for (i = 0; i < sizeof(operators) / sizeof(operators[0]); i++) {
400 if (!strcmp(token, operators[i].token)) {
401 action = operators[i].action;
402 n_args = operators[i].n_args;
403 break;
404 }
405 }
406 }
407 switch (action) {
408 case action_rect:
409 {
410 double v[4];
411
412 error = get_numbers_from_stack(&stack, v, n_args); /* n_args = 4 */
413 if (!error) {
414 /* Not sure if this switch is required */
415 if (M.b == 0.0 && M.c == 0.0) {
416 /* Use "re" operator */
417 pdf_coord p0;
418 double w, h;
419
420 p0.x = v[0]; p0.y = v[1];
421 w = M.a * v[2]; h = M.d * v[3];
422 pdf_dev_transform(&p0, &M);
423 buf[len++] = ' ';
424 len += pdf_sprint_coord(buf+len, &p0);
425 buf[len++] = ' ';
426 len += pdf_sprint_length(buf+len, w);
427 buf[len++] = ' ';
428 len += pdf_sprint_length(buf+len, h);
429 len += sprintf(buf+len, " re");
430 } else {
431 /* Converted to lineto */
432 pdf_coord p0, p1, p2, p3;
433 double w, h;
434
435 w = v[2]; h = v[3];
436 p0.x = v[0]; p0.y = v[1];
437 p1.x = p0.x + w; p1.y = p0.y;
438 p2.x = p1.x; p2.y = p1.y + h;
439 p3.x = p0.x; p3.y = p2.y;
440 pdf_dev_transform(&p0, &M);
441 pdf_dev_transform(&p1, &M);
442 pdf_dev_transform(&p2, &M);
443 pdf_dev_transform(&p3, &M);
444 buf[len++] = ' ';
445 len += pdf_sprint_coord(buf+len, &p0);
446 len += sprintf(buf+len, " m");
447 buf[len++] = ' ';
448 len += pdf_sprint_coord(buf+len, &p1);
449 len += sprintf(buf+len, " l");
450 buf[len++] = ' ';
451 len += pdf_sprint_coord(buf+len, &p2);
452 len += sprintf(buf+len, " l");
453 buf[len++] = ' ';
454 len += pdf_sprint_coord(buf+len, &p3);
455 len += sprintf(buf+len, " l h");
456 }
457 pdf_doc_add_page_content(buf, len);
458 }
459 }
460 break;
461 case action_path:
462 {
463 double v[6];
464 pdf_coord pt;
465 int i;
466
467 error = get_numbers_from_stack(&stack, v, n_args);
468 if (!error) {
469 for (i = 0; i < n_args/2; i++) {
470 pt.x = v[2*i];
471 pt.y = v[2*i+1];
472 pdf_dev_transform(&pt, &M);
473 buf[len++] = ' ';
474 len += pdf_sprint_coord(buf+len, &pt);
475 }
476 len += sprintf(buf+len, " %s", token);
477 pdf_doc_add_page_content(buf, len);
478 }
479 }
480 break;
481 case action_trans:
482 {
483 double v[6];
484 pdf_tmatrix T;
485 error = get_numbers_from_stack(&stack, v, n_args);
486 if (!error) {
487 T.a = v[0]; T.b = v[1]; T.c = v[2]; T.d = v[3];
488 T.e = v[4]; T.f = v[5];
489 pdf_concatmatrix(&M, &T);
490 }
491 }
492 break;
493 case action_clip:
494 if (token[0] >= 'a' && token[0] <= 'z') {
495 /* close path */
496 len += sprintf(buf+len, " h");
497 }
498 if (strlen(token) >= 2 && token[1] == '*') {
499 len += sprintf(buf+len, " W* n");
500 } else {
501 len += sprintf(buf+len, " W n");
502 }
503 pdf_doc_add_page_content(buf, len);
504 break;
505 case action_save:
506 depth++;
507 break;
508 case action_restore:
509 depth--;
510 break;
511 case action_discard:
512 /* stack clearing behavior */
513 while ((obj = dpx_stack_pop(&stack)) != NULL) {
514 pdf_release_obj(obj);
515 }
516 break;
517 case action_unknown:
518 error = -1;
519 break;
520 }
521 RELEASE(token);
522 }
523 {
524 pdf_obj *obj;
525
526 while ((obj = dpx_stack_pop(&stack)) != NULL) {
527 pdf_release_obj(obj);
528 }
529 }
530 pdf_release_obj(contents);
531 pdf_close(pf);
532
533 return error;
534 }