| 1 |
/* Copyright (C) 2019-2026 Artifex Software, Inc. |
| 2 |
All Rights Reserved. |
| 3 |
|
| 4 |
This software is provided AS-IS with no warranty, either express or |
| 5 |
implied. |
| 6 |
|
| 7 |
This software is distributed under license and may not be copied, |
| 8 |
modified or distributed except as expressly authorized under the terms |
| 9 |
of the license contained in the file LICENSE in this distribution. |
| 10 |
|
| 11 |
Refer to licensing information at http://www.artifex.com or contact |
| 12 |
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
CA 94129, USA, for further information. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* Page-level operations for the PDF interpreter */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_stack.h" |
| 20 |
#include "pdf_doc.h" |
| 21 |
#include "pdf_deref.h" |
| 22 |
#include "pdf_page.h" |
| 23 |
#include "pdf_file.h" |
| 24 |
#include "pdf_dict.h" |
| 25 |
#include "pdf_array.h" |
| 26 |
#include "pdf_loop_detect.h" |
| 27 |
#include "pdf_colour.h" |
| 28 |
#include "pdf_trans.h" |
| 29 |
#include "pdf_font_types.h" |
| 30 |
#include "pdf_gstate.h" |
| 31 |
#include "pdf_misc.h" |
| 32 |
#include "pdf_optcontent.h" |
| 33 |
#include "pdf_device.h" |
| 34 |
#include "pdf_annot.h" |
| 35 |
#include "pdf_check.h" |
| 36 |
#include "pdf_mark.h" |
| 37 |
#include "pdf_font.h" |
| 38 |
|
| 39 |
#include "gscoord.h" /* for gs_concat() and others */ |
| 40 |
#include "gspaint.h" /* For gs_erasepage() */ |
| 41 |
#include "gsstate.h" /* For gs_initgraphics() */ |
| 42 |
#include "gspath2.h" /* For gs_rectclip() */ |
| 43 |
#include "gxdevsop.h" /* For special ops */ |
| 44 |
|
| 45 |
static int pdfi_process_page_contents(pdf_context *ctx, pdf_dict *page_dict) |
| 46 |
{ |
| 47 |
int i, code = 0; |
| 48 |
pdf_obj *o, *o1; |
| 49 |
|
| 50 |
#if PURGE_CACHE_PER_PAGE |
| 51 |
pdfi_purge_obj_cache(ctx); |
| 52 |
#endif |
| 53 |
|
| 54 |
code = pdfi_dict_get(ctx, page_dict, "Contents", &o); |
| 55 |
if (code == gs_error_undefined) |
| 56 |
/* Don't throw an error if there are no contents, just render nothing.... */ |
| 57 |
return 0; |
| 58 |
if (code < 0) |
| 59 |
return code; |
| 60 |
|
| 61 |
if (pdfi_type_of(o) == PDF_INDIRECT) { |
| 62 |
if (((pdf_indirect_ref *)o)->ref_object_num == page_dict->object_num) |
| 63 |
return_error(gs_error_circular_reference); |
| 64 |
|
| 65 |
code = pdfi_dereference(ctx, ((pdf_indirect_ref *)o)->ref_object_num, ((pdf_indirect_ref *)o)->ref_generation_num, &o1); |
| 66 |
pdfi_countdown(o); |
| 67 |
if (code < 0) { |
| 68 |
if (code == gs_error_VMerror) |
| 69 |
return code; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
o = o1; |
| 73 |
} |
| 74 |
|
| 75 |
code = pdfi_gsave(ctx); |
| 76 |
if (code < 0) { |
| 77 |
pdfi_countdown(o); |
| 78 |
return code; |
| 79 |
} |
| 80 |
/* Increment the saved gsave_level by 1 to allow for the gsave we've just |
| 81 |
* done. Otherwise excess 'Q' operators in the stream can cause us to pop |
| 82 |
* one higher than we should. Bug 707753. */ |
| 83 |
ctx->current_stream_save.gsave_level++; |
| 84 |
|
| 85 |
ctx->encryption.decrypt_strings = false; |
| 86 |
if (pdfi_type_of(o) == PDF_ARRAY) { |
| 87 |
pdf_array *a = (pdf_array *)o; |
| 88 |
|
| 89 |
for (i=0;i < pdfi_array_size(a); i++) { |
| 90 |
pdf_indirect_ref *r; |
| 91 |
code = pdfi_array_get_no_deref(ctx, a, i, (pdf_obj **)&r); |
| 92 |
if (code < 0) |
| 93 |
goto page_error; |
| 94 |
if (pdfi_type_of (r) == PDF_STREAM) { |
| 95 |
code = pdfi_interpret_content_stream(ctx, NULL, (pdf_stream *)r, page_dict); |
| 96 |
pdfi_countdown(r); |
| 97 |
if (code < 0) |
| 98 |
goto page_error; |
| 99 |
} else { |
| 100 |
if (pdfi_type_of(r) != PDF_INDIRECT) { |
| 101 |
pdfi_countdown(r); |
| 102 |
code = gs_note_error(gs_error_typecheck); |
| 103 |
goto page_error; |
| 104 |
} else { |
| 105 |
if (r->ref_object_num == page_dict->object_num) { |
| 106 |
pdfi_countdown(r); |
| 107 |
code = gs_note_error(gs_error_circular_reference); |
| 108 |
goto page_error; |
| 109 |
} |
| 110 |
code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o1); |
| 111 |
pdfi_countdown(r); |
| 112 |
if (code < 0) { |
| 113 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_NOERROR, "pdfi_process_page_contents", NULL)) < 0) { |
| 114 |
goto page_error; |
| 115 |
} |
| 116 |
code = 0; |
| 117 |
goto page_error; |
| 118 |
} |
| 119 |
if (pdfi_type_of(o1) != PDF_STREAM) { |
| 120 |
pdfi_countdown(o1); |
| 121 |
code = gs_note_error(gs_error_typecheck); |
| 122 |
goto page_error; |
| 123 |
} |
| 124 |
code = pdfi_interpret_content_stream(ctx, NULL, (pdf_stream *)o1, page_dict); |
| 125 |
pdfi_countdown(o1); |
| 126 |
if (code < 0) { |
| 127 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_NOERROR, "pdfi_process_page_contents", NULL)) < 0) { |
| 128 |
goto page_error; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} else { |
| 135 |
if (pdfi_type_of(o) == PDF_STREAM) { |
| 136 |
code = pdfi_interpret_content_stream(ctx, NULL, (pdf_stream *)o, page_dict); |
| 137 |
} else |
| 138 |
code = gs_note_error(gs_error_typecheck); |
| 139 |
} |
| 140 |
page_error: |
| 141 |
/* Decrement the stream level to counterbalance the increment above. */ |
| 142 |
ctx->current_stream_save.gsave_level--; |
| 143 |
ctx->encryption.decrypt_strings = true; |
| 144 |
pdfi_clearstack(ctx); |
| 145 |
pdfi_grestore(ctx); |
| 146 |
pdfi_countdown(o); |
| 147 |
return code; |
| 148 |
} |
| 149 |
|
| 150 |
/* Render one page (including annotations) (see pdf_main.ps/showpagecontents) */ |
| 151 |
static int pdfi_process_one_page(pdf_context *ctx, pdf_dict *page_dict) |
| 152 |
{ |
| 153 |
stream_save local_entry_save; |
| 154 |
int code, code1; |
| 155 |
|
| 156 |
/* Save the current stream state, for later cleanup, in a local variable */ |
| 157 |
local_save_stream_state(ctx, &local_entry_save); |
| 158 |
initialise_stream_save(ctx); |
| 159 |
|
| 160 |
code = pdfi_process_page_contents(ctx, page_dict); |
| 161 |
|
| 162 |
/* Put our state back the way it was before we ran the contents |
| 163 |
* and check if the stream had problems |
| 164 |
*/ |
| 165 |
#if PROBE_STREAMS |
| 166 |
if (ctx->pgs->level > ctx->current_stream_save.gsave_level || |
| 167 |
pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count) |
| 168 |
code = ((pdf_context *)0)->first_page; |
| 169 |
#endif |
| 170 |
|
| 171 |
cleanup_context_interpretation(ctx, &local_entry_save); |
| 172 |
local_restore_stream_state(ctx, &local_entry_save); |
| 173 |
|
| 174 |
local_save_stream_state(ctx, &local_entry_save); |
| 175 |
initialise_stream_save(ctx); |
| 176 |
|
| 177 |
code1 = pdfi_do_annotations(ctx, page_dict); |
| 178 |
if (code >= 0) code = code1; |
| 179 |
|
| 180 |
cleanup_context_interpretation(ctx, &local_entry_save); |
| 181 |
local_restore_stream_state(ctx, &local_entry_save); |
| 182 |
|
| 183 |
local_save_stream_state(ctx, &local_entry_save); |
| 184 |
initialise_stream_save(ctx); |
| 185 |
|
| 186 |
code1 = pdfi_do_acroform(ctx, page_dict); |
| 187 |
if (code >= 0) code = code1; |
| 188 |
|
| 189 |
cleanup_context_interpretation(ctx, &local_entry_save); |
| 190 |
local_restore_stream_state(ctx, &local_entry_save); |
| 191 |
|
| 192 |
if (ctx->text.BlockDepth != 0) { |
| 193 |
ctx->text.BlockDepth = 0; |
| 194 |
if (ctx->text.TextClip) { |
| 195 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 196 |
|
| 197 |
ctx->text.TextClip = false; |
| 198 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 199 |
} |
| 200 |
code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_UNBLANACED_BT, "pdfi_process_one_page", NULL); |
| 201 |
if (code < 0) |
| 202 |
return code; |
| 203 |
} |
| 204 |
|
| 205 |
return code; |
| 206 |
} |
| 207 |
|
| 208 |
/* See pdf_PDF2PS_matrix and .pdfshowpage_Install */ |
| 209 |
static void pdfi_set_ctm(pdf_context *ctx) |
| 210 |
{ |
| 211 |
gs_matrix mat; |
| 212 |
|
| 213 |
/* Adjust for page.UserUnits */ |
| 214 |
mat.xx = ctx->page.UserUnit; |
| 215 |
mat.xy = 0; |
| 216 |
mat.yx = 0; |
| 217 |
mat.yy = ctx->page.UserUnit; |
| 218 |
mat.tx = 0; |
| 219 |
mat.ty = 0; |
| 220 |
gs_concat(ctx->pgs, &mat); |
| 221 |
|
| 222 |
/* We need to make sure the default matrix is properly set. |
| 223 |
* If we do gs_initgraphics() later (such as for annotations) |
| 224 |
* then it uses this default matrix if it is set. |
| 225 |
* Needed for page rotations to work correctly with Annotations. |
| 226 |
*/ |
| 227 |
gs_setdefaultmatrix(ctx->pgs, &ctm_only(ctx->pgs)); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
/* Get .MediaSize from the device to setup page.Size in context */ |
| 232 |
static int pdfi_get_media_size(pdf_context *ctx, pdf_dict *page_dict) |
| 233 |
{ |
| 234 |
pdf_array *a = NULL, *default_media = NULL; |
| 235 |
double d[4]; |
| 236 |
int code = 0; |
| 237 |
uint64_t i; |
| 238 |
double userunit = 1.0; |
| 239 |
|
| 240 |
code = pdfi_dict_get_type(ctx, page_dict, "MediaBox", PDF_ARRAY, (pdf_obj **)&default_media); |
| 241 |
if (code < 0) { |
| 242 |
pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_MEDIABOX, "pdfi_get_media_size", NULL); |
| 243 |
code = gs_erasepage(ctx->pgs); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
if (ctx->args.usecropbox) { |
| 248 |
if (a != NULL) |
| 249 |
pdfi_countdown(a); |
| 250 |
(void)pdfi_dict_get_type(ctx, page_dict, "CropBox", PDF_ARRAY, (pdf_obj **)&a); |
| 251 |
} |
| 252 |
if (ctx->args.useartbox) { |
| 253 |
if (a != NULL) |
| 254 |
pdfi_countdown(a); |
| 255 |
(void)pdfi_dict_get_type(ctx, page_dict, "ArtBox", PDF_ARRAY, (pdf_obj **)&a); |
| 256 |
} |
| 257 |
if (ctx->args.usebleedbox) { |
| 258 |
if (a != NULL) |
| 259 |
pdfi_countdown(a); |
| 260 |
(void)pdfi_dict_get_type(ctx, page_dict, "BleedBox", PDF_ARRAY, (pdf_obj **)&a); |
| 261 |
} |
| 262 |
if (ctx->args.usetrimbox) { |
| 263 |
if (a != NULL) |
| 264 |
pdfi_countdown(a); |
| 265 |
(void)pdfi_dict_get_type(ctx, page_dict, "TrimBox", PDF_ARRAY, (pdf_obj **)&a); |
| 266 |
} |
| 267 |
if (a == NULL) { |
| 268 |
a = default_media; |
| 269 |
pdfi_countup(a); |
| 270 |
} |
| 271 |
|
| 272 |
if (!ctx->args.nouserunit && !ctx->device_state.PassUserUnit) { |
| 273 |
(void)pdfi_dict_knownget_number(ctx, page_dict, "UserUnit", &userunit); |
| 274 |
} |
| 275 |
ctx->page.UserUnit = userunit; |
| 276 |
|
| 277 |
for (i=0;i<4;i++) { |
| 278 |
code = pdfi_array_get_number(ctx, a, i, &d[i]); |
| 279 |
d[i] *= userunit; |
| 280 |
} |
| 281 |
pdfi_countdown(a); |
| 282 |
pdfi_countdown(default_media); |
| 283 |
|
| 284 |
normalize_rectangle(d); |
| 285 |
memcpy(ctx->page.Size, d, 4 * sizeof(double)); |
| 286 |
|
| 287 |
return code; |
| 288 |
} |
| 289 |
|
| 290 |
static int pdfi_set_media_size(pdf_context *ctx, pdf_dict *page_dict) |
| 291 |
{ |
| 292 |
gs_c_param_list list; |
| 293 |
gs_param_float_array fa; |
| 294 |
pdf_array *a = NULL, *default_media = NULL; |
| 295 |
float fv[2]; |
| 296 |
double d[4], d_crop[4]; |
| 297 |
gs_rect bbox; |
| 298 |
int code, do_crop = false; |
| 299 |
uint64_t i; |
| 300 |
int64_t rotate = 0; |
| 301 |
double userunit = 1.0; |
| 302 |
|
| 303 |
code = pdfi_dict_get_type(ctx, page_dict, "MediaBox", PDF_ARRAY, (pdf_obj **)&default_media); |
| 304 |
if (code < 0) { |
| 305 |
pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_MEDIABOX, "pdfi_get_media_size", NULL); |
| 306 |
code = gs_erasepage(ctx->pgs); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
if (ctx->args.usecropbox) { |
| 311 |
if (a != NULL) |
| 312 |
pdfi_countdown(a); |
| 313 |
(void)pdfi_dict_get_type(ctx, page_dict, "CropBox", PDF_ARRAY, (pdf_obj **)&a); |
| 314 |
} |
| 315 |
if (ctx->args.useartbox) { |
| 316 |
if (a != NULL) |
| 317 |
pdfi_countdown(a); |
| 318 |
(void)pdfi_dict_get_type(ctx, page_dict, "ArtBox", PDF_ARRAY, (pdf_obj **)&a); |
| 319 |
} |
| 320 |
if (ctx->args.usebleedbox) { |
| 321 |
if (a != NULL) |
| 322 |
pdfi_countdown(a); |
| 323 |
(void)pdfi_dict_get_type(ctx, page_dict, "BleedBox", PDF_ARRAY, (pdf_obj **)&a); |
| 324 |
} |
| 325 |
if (ctx->args.usetrimbox) { |
| 326 |
if (a != NULL) |
| 327 |
pdfi_countdown(a); |
| 328 |
(void)pdfi_dict_get_type(ctx, page_dict, "TrimBox", PDF_ARRAY, (pdf_obj **)&a); |
| 329 |
} |
| 330 |
if (a == NULL) { |
| 331 |
code = pdfi_dict_get_type(ctx, page_dict, "CropBox", PDF_ARRAY, (pdf_obj **)&a); |
| 332 |
if (code >= 0 && pdfi_array_size(a) >= 4) { |
| 333 |
pdf_obj *box_obj = NULL; |
| 334 |
|
| 335 |
for (i=0;i<4;i++) { |
| 336 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 337 |
if (code >= 0) { |
| 338 |
code = pdfi_obj_to_real(ctx, box_obj, &d_crop[i]); |
| 339 |
pdfi_countdown(box_obj); |
| 340 |
} |
| 341 |
if (code < 0) |
| 342 |
break; |
| 343 |
} |
| 344 |
pdfi_countdown(a); |
| 345 |
if (code >= 0) { |
| 346 |
normalize_rectangle(d_crop); |
| 347 |
memcpy(ctx->page.Crop, d_crop, 4 * sizeof(double)); |
| 348 |
do_crop = true; |
| 349 |
} |
| 350 |
} |
| 351 |
a = default_media; |
| 352 |
} |
| 353 |
|
| 354 |
if (!ctx->args.nouserunit) { |
| 355 |
if (ctx->device_state.PassUserUnit) { |
| 356 |
double unit = 1.0; |
| 357 |
(void)pdfi_dict_knownget_number(ctx, page_dict, "UserUnit", &unit); |
| 358 |
(void)pdfi_device_set_param_float(ctx->pgs->device, "UserUnit", unit); |
| 359 |
} else { |
| 360 |
(void)pdfi_dict_knownget_number(ctx, page_dict, "UserUnit", &userunit); |
| 361 |
} |
| 362 |
} |
| 363 |
ctx->page.UserUnit = userunit; |
| 364 |
|
| 365 |
for (i=0;i<4;i++) { |
| 366 |
pdf_obj *box_obj = NULL; |
| 367 |
|
| 368 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 369 |
if (code >= 0) { |
| 370 |
code = pdfi_obj_to_real(ctx, box_obj, &d[i]); |
| 371 |
pdfi_countdown(box_obj); |
| 372 |
} |
| 373 |
|
| 374 |
if (code < 0) { |
| 375 |
pdfi_countdown(a); |
| 376 |
pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_MEDIABOX, "pdfi_get_media_size", NULL); |
| 377 |
code = gs_erasepage(ctx->pgs); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
d[i] *= userunit; |
| 381 |
} |
| 382 |
pdfi_countdown(a); |
| 383 |
|
| 384 |
normalize_rectangle(d); |
| 385 |
memcpy(ctx->page.Size, d, 4 * sizeof(double)); |
| 386 |
|
| 387 |
code = pdfi_dict_get_int(ctx, page_dict, "Rotate", &rotate); |
| 388 |
|
| 389 |
rotate = rotate % 360; |
| 390 |
|
| 391 |
switch(rotate) { |
| 392 |
default: |
| 393 |
case 0: |
| 394 |
case 360: |
| 395 |
case -180: |
| 396 |
case 180: |
| 397 |
fv[0] = (float)(d[2] - d[0]); |
| 398 |
fv[1] = (float)(d[3] - d[1]); |
| 399 |
break; |
| 400 |
case -90: |
| 401 |
case 90: |
| 402 |
case -270: |
| 403 |
case 270: |
| 404 |
fv[1] = (float)(d[2] - d[0]); |
| 405 |
fv[0] = (float)(d[3] - d[1]); |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
fa.persistent = false; |
| 410 |
fa.data = fv; |
| 411 |
fa.size = 2; |
| 412 |
|
| 413 |
/* ----- setup specific device parameters ----- */ |
| 414 |
gs_c_param_list_write(&list, ctx->memory); |
| 415 |
|
| 416 |
code = param_write_float_array((gs_param_list *)&list, ".MediaSize", &fa); |
| 417 |
if (code >= 0) |
| 418 |
{ |
| 419 |
gx_device *dev = gs_currentdevice(ctx->pgs); |
| 420 |
|
| 421 |
gs_c_param_list_read(&list); |
| 422 |
code = gs_putdeviceparams(dev, (gs_param_list *)&list); |
| 423 |
if (code < 0) { |
| 424 |
gs_c_param_list_release(&list); |
| 425 |
return code; |
| 426 |
} |
| 427 |
} |
| 428 |
gs_c_param_list_release(&list); |
| 429 |
/* ----- end setup specific device parameters ----- */ |
| 430 |
|
| 431 |
/* Resets the default matrix to NULL before doing initgraphics, because |
| 432 |
* otherwise initgraphics would keep old matrix. |
| 433 |
* (see pdfi_set_ctm()) |
| 434 |
*/ |
| 435 |
gs_setdefaultmatrix(ctx->pgs, NULL); |
| 436 |
gs_initgraphics(ctx->pgs); |
| 437 |
|
| 438 |
switch(rotate) { |
| 439 |
case 0: |
| 440 |
break; |
| 441 |
case -270: |
| 442 |
case 90: |
| 443 |
gs_translate(ctx->pgs, 0, fv[1]); |
| 444 |
gs_rotate(ctx->pgs, -90); |
| 445 |
break; |
| 446 |
case -180: |
| 447 |
case 180: |
| 448 |
gs_translate(ctx->pgs, fv[0], fv[1]); |
| 449 |
gs_rotate(ctx->pgs, 180); |
| 450 |
break; |
| 451 |
case -90: |
| 452 |
case 270: |
| 453 |
gs_translate(ctx->pgs, fv[0], 0); |
| 454 |
gs_rotate(ctx->pgs, 90); |
| 455 |
break; |
| 456 |
default: |
| 457 |
break; |
| 458 |
} |
| 459 |
|
| 460 |
if (do_crop) { |
| 461 |
bbox.p.x = d_crop[0] - d[0]; |
| 462 |
bbox.p.y = d_crop[1] - d[1]; |
| 463 |
bbox.q.x = d_crop[2] - d[0]; |
| 464 |
bbox.q.y = d_crop[3] - d[1]; |
| 465 |
|
| 466 |
code = gs_rectclip(ctx->pgs, &bbox, 1); |
| 467 |
if (code < 0) |
| 468 |
return code; |
| 469 |
} |
| 470 |
|
| 471 |
gs_translate(ctx->pgs, d[0] * -1, d[1] * -1); |
| 472 |
|
| 473 |
code = gs_erasepage(ctx->pgs); |
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
/* Setup default transfer functions */ |
| 478 |
static void pdfi_setup_transfers(pdf_context *ctx) |
| 479 |
{ |
| 480 |
if (ctx->pgs->set_transfer.red == 0x00) { |
| 481 |
ctx->page.DefaultTransfers[0].proc = gs_identity_transfer; |
| 482 |
memset(ctx->page.DefaultTransfers[0].values, 0x00, transfer_map_size * sizeof(frac)); |
| 483 |
} else { |
| 484 |
ctx->page.DefaultTransfers[0].proc = ctx->pgs->set_transfer.red->proc; |
| 485 |
memcpy(ctx->page.DefaultTransfers[0].values, ctx->pgs->set_transfer.red->values, transfer_map_size * sizeof(frac)); |
| 486 |
} |
| 487 |
if (ctx->pgs->set_transfer.green == 0x00) { |
| 488 |
ctx->page.DefaultTransfers[1].proc = gs_identity_transfer; |
| 489 |
memset(ctx->page.DefaultTransfers[1].values, 0x00, transfer_map_size * sizeof(frac)); |
| 490 |
} else { |
| 491 |
ctx->page.DefaultTransfers[1].proc = ctx->pgs->set_transfer.green->proc; |
| 492 |
memcpy(ctx->page.DefaultTransfers[1].values, ctx->pgs->set_transfer.green->values, transfer_map_size * sizeof(frac)); |
| 493 |
} |
| 494 |
if (ctx->pgs->set_transfer.blue == 0x00) { |
| 495 |
ctx->page.DefaultTransfers[2].proc = gs_identity_transfer; |
| 496 |
memset(ctx->page.DefaultTransfers[2].values, 0x00, transfer_map_size * sizeof(frac)); |
| 497 |
} else { |
| 498 |
ctx->page.DefaultTransfers[2].proc = ctx->pgs->set_transfer.blue->proc; |
| 499 |
memcpy(ctx->page.DefaultTransfers[2].values, ctx->pgs->set_transfer.blue->values, transfer_map_size * sizeof(frac)); |
| 500 |
} |
| 501 |
if (ctx->pgs->set_transfer.gray == 0x00) { |
| 502 |
ctx->page.DefaultTransfers[3].proc = gs_identity_transfer; |
| 503 |
memset(ctx->page.DefaultTransfers[3].values, 0x00, transfer_map_size * sizeof(frac)); |
| 504 |
} else { |
| 505 |
ctx->page.DefaultTransfers[3].proc = ctx->pgs->set_transfer.gray->proc; |
| 506 |
memcpy(ctx->page.DefaultTransfers[3].values, ctx->pgs->set_transfer.gray->values, transfer_map_size * sizeof(frac)); |
| 507 |
} |
| 508 |
if (ctx->pgs->black_generation == 0x00) { |
| 509 |
ctx->page.DefaultBG.proc = gs_identity_transfer; |
| 510 |
memset(ctx->page.DefaultBG.values, 0x00, transfer_map_size * sizeof(frac)); |
| 511 |
} else { |
| 512 |
ctx->page.DefaultBG.proc = ctx->pgs->black_generation->proc; |
| 513 |
memcpy(ctx->page.DefaultBG.values, ctx->pgs->black_generation->values, transfer_map_size * sizeof(frac)); |
| 514 |
} |
| 515 |
if (ctx->pgs->undercolor_removal == 0x00) { |
| 516 |
ctx->page.DefaultUCR.proc = gs_identity_transfer; |
| 517 |
memset(ctx->page.DefaultUCR.values, 0x00, transfer_map_size * sizeof(frac)); |
| 518 |
} else { |
| 519 |
ctx->page.DefaultUCR.proc = ctx->pgs->undercolor_removal->proc; |
| 520 |
memcpy(ctx->page.DefaultUCR.values, ctx->pgs->undercolor_removal->values, transfer_map_size * sizeof(frac)); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
/* Return a dictionary containing information about the page. Basic information is that |
| 525 |
* required to render the page; if extended is true then additionally contains an |
| 526 |
* array of spot ink names and an array of dictionaries each of which contains |
| 527 |
* information about a font used on the page. THis is normally only used for tools |
| 528 |
* like pdf_info.ps |
| 529 |
*/ |
| 530 |
int pdfi_page_info(pdf_context *ctx, uint64_t page_num, pdf_dict **info, bool extended) |
| 531 |
{ |
| 532 |
int code = 0, i=0; |
| 533 |
pdf_dict *page_dict = NULL, *info_dict = NULL; |
| 534 |
pdf_array *fonts_array = NULL, *spots_array = NULL; |
| 535 |
pdf_array *a = NULL; |
| 536 |
pdf_obj *o = NULL; |
| 537 |
bool known = false; |
| 538 |
double dummy; |
| 539 |
|
| 540 |
code = pdfi_page_get_dict(ctx, page_num, &page_dict); |
| 541 |
if (code < 0) |
| 542 |
return code; |
| 543 |
|
| 544 |
if (code > 0) { |
| 545 |
code = gs_note_error(gs_error_unknownerror); |
| 546 |
goto done; |
| 547 |
} |
| 548 |
|
| 549 |
code = pdfi_dict_alloc(ctx, 6, &info_dict); |
| 550 |
if (code < 0) |
| 551 |
goto done; |
| 552 |
|
| 553 |
pdfi_countup(info_dict); |
| 554 |
|
| 555 |
if (extended) |
| 556 |
code = pdfi_check_page(ctx, page_dict, &fonts_array, &spots_array, false); |
| 557 |
else |
| 558 |
code = pdfi_check_page(ctx, page_dict, NULL, NULL, false); |
| 559 |
if (code < 0) |
| 560 |
goto done; |
| 561 |
|
| 562 |
if (spots_array != NULL) { |
| 563 |
code = pdfi_dict_put(ctx, info_dict, "Spots", (pdf_obj *)spots_array); |
| 564 |
if (code < 0) |
| 565 |
goto done; |
| 566 |
pdfi_countdown(spots_array); |
| 567 |
} |
| 568 |
|
| 569 |
if (fonts_array != NULL) { |
| 570 |
code = pdfi_dict_put(ctx, info_dict, "Fonts", (pdf_obj *)fonts_array); |
| 571 |
if (code < 0) |
| 572 |
goto done; |
| 573 |
pdfi_countdown(fonts_array); |
| 574 |
} |
| 575 |
|
| 576 |
code = pdfi_dict_get_type(ctx, page_dict, "MediaBox", PDF_ARRAY, (pdf_obj **)&a); |
| 577 |
if (code < 0) |
| 578 |
pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_MEDIABOX, "pdfi_page_info", NULL); |
| 579 |
|
| 580 |
if (code >= 0) { |
| 581 |
pdf_obj *box_obj = NULL; |
| 582 |
|
| 583 |
for (i = 0;i < pdfi_array_size(a); i++) { |
| 584 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 585 |
if (code >= 0) { |
| 586 |
code = pdfi_obj_to_real(ctx, box_obj, &dummy); |
| 587 |
pdfi_countdown(box_obj); |
| 588 |
} |
| 589 |
if (code < 0) { |
| 590 |
pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_MEDIABOX, "pdfi_page_info", NULL); |
| 591 |
goto done; |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
code = pdfi_dict_put(ctx, info_dict, "MediaBox", (pdf_obj *)a); |
| 596 |
if (code < 0) |
| 597 |
goto done; |
| 598 |
pdfi_countdown(a); |
| 599 |
a = NULL; |
| 600 |
} |
| 601 |
|
| 602 |
code = pdfi_dict_get_type(ctx, page_dict, "ArtBox", PDF_ARRAY, (pdf_obj **)&a); |
| 603 |
if (code >= 0) { |
| 604 |
pdf_obj *box_obj = NULL; |
| 605 |
|
| 606 |
for (i = 0;i < pdfi_array_size(a); i++) { |
| 607 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 608 |
if (code >= 0) { |
| 609 |
code = pdfi_obj_to_real(ctx, box_obj, &dummy); |
| 610 |
pdfi_countdown(box_obj); |
| 611 |
} |
| 612 |
if (code < 0) |
| 613 |
break; |
| 614 |
} |
| 615 |
if (code >= 0) { |
| 616 |
code = pdfi_dict_put(ctx, info_dict, "ArtBox", (pdf_obj *)a); |
| 617 |
if (code < 0) |
| 618 |
goto done; |
| 619 |
} |
| 620 |
pdfi_countdown(a); |
| 621 |
a = NULL; |
| 622 |
} |
| 623 |
|
| 624 |
code = pdfi_dict_get_type(ctx, page_dict, "CropBox", PDF_ARRAY, (pdf_obj **)&a); |
| 625 |
if (code >= 0) { |
| 626 |
pdf_obj *box_obj = NULL; |
| 627 |
|
| 628 |
for (i = 0;i < pdfi_array_size(a); i++) { |
| 629 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 630 |
if (code >= 0) { |
| 631 |
code = pdfi_obj_to_real(ctx, box_obj, &dummy); |
| 632 |
pdfi_countdown(box_obj); |
| 633 |
} |
| 634 |
if (code < 0) |
| 635 |
break; |
| 636 |
} |
| 637 |
if (code >= 0) { |
| 638 |
code = pdfi_dict_put(ctx, info_dict, "CropBox", (pdf_obj *)a); |
| 639 |
if (code < 0) |
| 640 |
goto done; |
| 641 |
} |
| 642 |
pdfi_countdown(a); |
| 643 |
a = NULL; |
| 644 |
} |
| 645 |
|
| 646 |
code = pdfi_dict_get_type(ctx, page_dict, "TrimBox", PDF_ARRAY, (pdf_obj **)&a); |
| 647 |
if (code >= 0) { |
| 648 |
pdf_obj *box_obj = NULL; |
| 649 |
|
| 650 |
for (i = 0;i < pdfi_array_size(a); i++) { |
| 651 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 652 |
if (code >= 0) { |
| 653 |
code = pdfi_obj_to_real(ctx, box_obj, &dummy); |
| 654 |
pdfi_countdown(box_obj); |
| 655 |
} |
| 656 |
if (code < 0) |
| 657 |
break; |
| 658 |
} |
| 659 |
if (code >= 0) { |
| 660 |
code = pdfi_dict_put(ctx, info_dict, "TrimBox", (pdf_obj *)a); |
| 661 |
if (code < 0) |
| 662 |
goto done; |
| 663 |
} |
| 664 |
pdfi_countdown(a); |
| 665 |
a = NULL; |
| 666 |
} |
| 667 |
|
| 668 |
code = pdfi_dict_get_type(ctx, page_dict, "BleedBox", PDF_ARRAY, (pdf_obj **)&a); |
| 669 |
if (code >= 0) { |
| 670 |
pdf_obj *box_obj = NULL; |
| 671 |
|
| 672 |
for (i = 0;i < pdfi_array_size(a); i++) { |
| 673 |
code = pdfi_array_get_no_store_R(ctx, a, i, &box_obj); |
| 674 |
if (code >= 0) { |
| 675 |
code = pdfi_obj_to_real(ctx, box_obj, &dummy); |
| 676 |
pdfi_countdown(box_obj); |
| 677 |
} |
| 678 |
if (code < 0) |
| 679 |
break; |
| 680 |
} |
| 681 |
if (code >= 0) { |
| 682 |
code = pdfi_dict_put(ctx, info_dict, "BleedBox", (pdf_obj *)a); |
| 683 |
if (code < 0) |
| 684 |
goto done; |
| 685 |
} |
| 686 |
pdfi_countdown(a); |
| 687 |
a = NULL; |
| 688 |
} |
| 689 |
code = 0; |
| 690 |
|
| 691 |
code = pdfi_dict_get(ctx, page_dict, "Rotate", &o); |
| 692 |
if (code >= 0) { |
| 693 |
if (pdfi_type_of(o) == PDF_INT || pdfi_type_of(o) == PDF_REAL) { |
| 694 |
code = pdfi_dict_put(ctx, info_dict, "Rotate", o); |
| 695 |
if (code < 0) |
| 696 |
goto done; |
| 697 |
} |
| 698 |
pdfi_countdown(o); |
| 699 |
} |
| 700 |
|
| 701 |
code = pdfi_dict_get(ctx, page_dict, "UserUnit", &o); |
| 702 |
if (code >= 0) { |
| 703 |
if (pdfi_type_of(o) == PDF_INT || pdfi_type_of(o) == PDF_REAL) { |
| 704 |
code = pdfi_dict_put(ctx, info_dict, "UserUnit", o); |
| 705 |
if (code < 0) |
| 706 |
goto done; |
| 707 |
} |
| 708 |
pdfi_countdown(o); |
| 709 |
} |
| 710 |
|
| 711 |
if (ctx->page.has_transparency) |
| 712 |
code = pdfi_dict_put(ctx, info_dict, "UsesTransparency", PDF_TRUE_OBJ); |
| 713 |
else |
| 714 |
code = pdfi_dict_put(ctx, info_dict, "UsesTransparency", PDF_FALSE_OBJ); |
| 715 |
if (code < 0) |
| 716 |
goto done; |
| 717 |
|
| 718 |
code = pdfi_dict_known(ctx, page_dict, "Annots", &known); |
| 719 |
if (code >= 0 && known) |
| 720 |
code = pdfi_dict_put(ctx, info_dict, "Annots", PDF_TRUE_OBJ); |
| 721 |
else |
| 722 |
code = pdfi_dict_put(ctx, info_dict, "Annots", PDF_FALSE_OBJ); |
| 723 |
if (code < 0) |
| 724 |
goto done; |
| 725 |
|
| 726 |
code = pdfi_object_alloc(ctx, PDF_INT, 0, &o); |
| 727 |
if (code >= 0) { |
| 728 |
pdfi_countup(o); |
| 729 |
((pdf_num *)o)->value.i = ctx->page.num_spots; |
| 730 |
code = pdfi_dict_put(ctx, info_dict, "NumSpots", o); |
| 731 |
pdfi_countdown(o); |
| 732 |
o = NULL; |
| 733 |
if (code < 0) |
| 734 |
goto done; |
| 735 |
} |
| 736 |
|
| 737 |
done: |
| 738 |
if (code < 0) { |
| 739 |
pdfi_countdown(info_dict); |
| 740 |
info_dict = NULL; |
| 741 |
*info = NULL; |
| 742 |
} else |
| 743 |
*info = info_dict; |
| 744 |
|
| 745 |
pdfi_countdown(a); |
| 746 |
pdfi_countdown(page_dict); |
| 747 |
return code; |
| 748 |
} |
| 749 |
|
| 750 |
int pdfi_page_get_dict(pdf_context *ctx, uint64_t page_num, pdf_dict **dict) |
| 751 |
{ |
| 752 |
int code = 0; |
| 753 |
uint64_t page_offset = 0; |
| 754 |
|
| 755 |
code = pdfi_loop_detector_mark(ctx); |
| 756 |
if (code < 0) |
| 757 |
return code; |
| 758 |
|
| 759 |
if (ctx->PagesTree == NULL) { |
| 760 |
pdf_obj *o = NULL; |
| 761 |
pdf_name *n = NULL; |
| 762 |
/* The only way this should be true is if the Pages entry in the Root dictionary |
| 763 |
* points to a single instance of a Page dictionary, instead of to a Pages dictionary. |
| 764 |
* in which case, simply retrieve that dictionary and return. |
| 765 |
*/ |
| 766 |
code = pdfi_dict_get(ctx, ctx->Root, "Pages", &o); |
| 767 |
if (code < 0) |
| 768 |
goto page_error; |
| 769 |
if (pdfi_type_of(o) != PDF_DICT) { |
| 770 |
code = gs_note_error(gs_error_typecheck); |
| 771 |
goto page_error; |
| 772 |
} |
| 773 |
code = pdfi_dict_get_type(ctx, (pdf_dict *)o, "Type", PDF_NAME, (pdf_obj **)&n); |
| 774 |
if (code == 0) { |
| 775 |
if(pdfi_name_is(n, "Page")) { |
| 776 |
*dict = (pdf_dict *)o; |
| 777 |
pdfi_countup(*dict); |
| 778 |
} else |
| 779 |
code = gs_note_error(gs_error_undefined); |
| 780 |
} |
| 781 |
page_error: |
| 782 |
pdfi_loop_detector_cleartomark(ctx); |
| 783 |
pdfi_countdown(o); |
| 784 |
pdfi_countdown(n); |
| 785 |
return code; |
| 786 |
} |
| 787 |
|
| 788 |
code = pdfi_loop_detector_add_object(ctx, ctx->PagesTree->object_num); |
| 789 |
if (code < 0) |
| 790 |
goto exit; |
| 791 |
|
| 792 |
code = pdfi_get_page_dict(ctx, ctx->PagesTree, page_num, &page_offset, dict, NULL); |
| 793 |
if (code > 0) |
| 794 |
code = gs_error_unknownerror; |
| 795 |
|
| 796 |
/* Cache the page_dict number in page_array */ |
| 797 |
if (*dict) |
| 798 |
ctx->page_array[page_num] = (*dict)->object_num; |
| 799 |
|
| 800 |
exit: |
| 801 |
pdfi_loop_detector_cleartomark(ctx); |
| 802 |
return code; |
| 803 |
} |
| 804 |
|
| 805 |
/* Find the page number that corresponds to a page dictionary |
| 806 |
* Uses page_array cache to minimize the number of times a page_dict needs to |
| 807 |
* be fetched, because this is expensive. |
| 808 |
*/ |
| 809 |
int pdfi_page_get_number(pdf_context *ctx, pdf_dict *target_dict, uint64_t *page_num) |
| 810 |
{ |
| 811 |
uint64_t i; |
| 812 |
int code = 0; |
| 813 |
pdf_dict *page_dict = NULL; |
| 814 |
uint32_t object_num; |
| 815 |
|
| 816 |
for (i=0; i<ctx->num_pages; i++) { |
| 817 |
/* If the page has been processed before, then its object_num should already |
| 818 |
* be cached in the page_array, so check that first |
| 819 |
*/ |
| 820 |
object_num = ctx->page_array[i]; |
| 821 |
if (object_num == 0) { |
| 822 |
/* It wasn't cached, so this will cache it */ |
| 823 |
code = pdfi_page_get_dict(ctx, i, &page_dict); |
| 824 |
if (code < 0) |
| 825 |
continue; |
| 826 |
object_num = ctx->page_array[i]; |
| 827 |
} |
| 828 |
if (target_dict->object_num == object_num) { |
| 829 |
*page_num = i; |
| 830 |
goto exit; |
| 831 |
} |
| 832 |
|
| 833 |
pdfi_countdown(page_dict); |
| 834 |
page_dict = NULL; |
| 835 |
} |
| 836 |
|
| 837 |
code = gs_note_error(gs_error_undefined); |
| 838 |
|
| 839 |
exit: |
| 840 |
pdfi_countdown(page_dict); |
| 841 |
return code; |
| 842 |
} |
| 843 |
|
| 844 |
static void release_page_DefaultSpaces(pdf_context *ctx) |
| 845 |
{ |
| 846 |
if (ctx->page.DefaultGray_cs != NULL) { |
| 847 |
if (ctx->page.DefaultGray_cs->interpreter_data != NULL) { |
| 848 |
pdf_obj *o = (pdf_obj *)(ctx->page.DefaultGray_cs->interpreter_data); |
| 849 |
if (o != NULL && pdfi_type_of(o) == PDF_NAME) { |
| 850 |
pdfi_countdown(o); |
| 851 |
ctx->page.DefaultGray_cs->interpreter_data = NULL; |
| 852 |
} |
| 853 |
} |
| 854 |
rc_decrement(ctx->page.DefaultGray_cs, "pdfi_page_render"); |
| 855 |
ctx->page.DefaultGray_cs = NULL; |
| 856 |
} |
| 857 |
if (ctx->page.DefaultRGB_cs != NULL) { |
| 858 |
if (ctx->page.DefaultRGB_cs->interpreter_data != NULL) { |
| 859 |
pdf_obj *o = (pdf_obj *)(ctx->page.DefaultRGB_cs->interpreter_data); |
| 860 |
if (o != NULL && pdfi_type_of(o) == PDF_NAME) { |
| 861 |
pdfi_countdown(o); |
| 862 |
ctx->page.DefaultRGB_cs->interpreter_data = NULL; |
| 863 |
} |
| 864 |
} |
| 865 |
rc_decrement(ctx->page.DefaultRGB_cs, "pdfi_page_render"); |
| 866 |
ctx->page.DefaultRGB_cs = NULL; |
| 867 |
} |
| 868 |
if (ctx->page.DefaultCMYK_cs != NULL) { |
| 869 |
if (ctx->page.DefaultCMYK_cs->interpreter_data != NULL) { |
| 870 |
pdf_obj *o = (pdf_obj *)(ctx->page.DefaultCMYK_cs->interpreter_data); |
| 871 |
if (o != NULL && pdfi_type_of(o) == PDF_NAME) { |
| 872 |
pdfi_countdown(o); |
| 873 |
ctx->page.DefaultCMYK_cs->interpreter_data = NULL; |
| 874 |
} |
| 875 |
} |
| 876 |
rc_decrement(ctx->page.DefaultCMYK_cs, "pdfi_page_render"); |
| 877 |
ctx->page.DefaultCMYK_cs = NULL; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
static int setup_page_DefaultSpaces(pdf_context *ctx, pdf_dict *page_dict) |
| 882 |
{ |
| 883 |
/* First off, discard any dangling Default* colour spaces, just in case. */ |
| 884 |
release_page_DefaultSpaces(ctx); |
| 885 |
|
| 886 |
return(pdfi_setup_DefaultSpaces(ctx, page_dict)); |
| 887 |
} |
| 888 |
|
| 889 |
int pdfi_page_render(pdf_context *ctx, uint64_t page_num, bool init_graphics) |
| 890 |
{ |
| 891 |
int code, code1=0; |
| 892 |
pdf_dict *page_dict = NULL; |
| 893 |
bool page_group_known = false; |
| 894 |
pdf_dict *group_dict = NULL; |
| 895 |
bool page_dict_error = false; |
| 896 |
bool need_pdf14 = false; /* true if the device is needed and was successfully pushed */ |
| 897 |
int trans_depth = 0; /* -1 means special mode for transparency simulation */ |
| 898 |
|
| 899 |
if (page_num > ctx->num_pages) |
| 900 |
return_error(gs_error_rangecheck); |
| 901 |
|
| 902 |
if (ctx->args.pdfdebug) |
| 903 |
outprintf(ctx->memory, "%% Processing Page %"PRIi64" content stream\n", page_num + 1); |
| 904 |
|
| 905 |
code = pdfi_page_get_dict(ctx, page_num, &page_dict); |
| 906 |
if (code < 0) { |
| 907 |
char extra_info[256]; |
| 908 |
|
| 909 |
page_dict_error = true; |
| 910 |
gs_snprintf(extra_info, sizeof(extra_info), "*** ERROR: Page %ld has invalid Page dict, skipping\n", page_num+1); |
| 911 |
if (code == gs_error_VMerror || |
| 912 |
((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_PAGEDICTERROR, "pdfi_page_render", extra_info)) < 0)) { |
| 913 |
goto exit3; |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
code = pdfi_check_page(ctx, page_dict, NULL, NULL, init_graphics); |
| 918 |
if (code < 0) |
| 919 |
goto exit3; |
| 920 |
|
| 921 |
if (ctx->args.pdfdebug) { |
| 922 |
dbgmprintf2(ctx->memory, "Current page %ld transparency setting is %d", page_num+1, |
| 923 |
ctx->page.has_transparency); |
| 924 |
|
| 925 |
if (ctx->device_state.spot_capable) |
| 926 |
dbgmprintf1(ctx->memory, ", spots=%d\n", ctx->page.num_spots); |
| 927 |
else |
| 928 |
dbgmprintf(ctx->memory, "\n"); |
| 929 |
} |
| 930 |
|
| 931 |
code = pdfi_dict_knownget_type(ctx, page_dict, "Group", PDF_DICT, (pdf_obj **)&group_dict); |
| 932 |
/* Ignore errors retrieving the Group dictionary, we will just ignore it. This allows us |
| 933 |
* to handle files such as Bug #705206 where the Group dictionary is a free object in a |
| 934 |
* compressed object stream. |
| 935 |
*/ |
| 936 |
if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_BAD_GROUP_DICT, "pdfi_page_render", NULL)) < 0) |
| 937 |
goto exit2; |
| 938 |
|
| 939 |
if (group_dict != NULL) |
| 940 |
page_group_known = true; |
| 941 |
|
| 942 |
pdfi_countdown(ctx->page.CurrentPageDict); |
| 943 |
ctx->page.CurrentPageDict = page_dict; |
| 944 |
pdfi_countup(ctx->page.CurrentPageDict); |
| 945 |
|
| 946 |
/* In case we don't call pdfi_set_media_size, which sets this up. |
| 947 |
* We shouldn't ever use it in that case, but best to be safe. |
| 948 |
*/ |
| 949 |
ctx->page.UserUnit = 1.0f; |
| 950 |
/* If we are being called from the PDF interpreter then |
| 951 |
* we need to set up the page and the default graphics state |
| 952 |
* but if we are being called from PostScript we do *not* |
| 953 |
* want to alter any of the graphics state or the media size. |
| 954 |
*/ |
| 955 |
/* TODO: I think this is a mix of things we might need to |
| 956 |
* still be setting up. |
| 957 |
* (for example, I noticed the blendmode and moved it outside the if) |
| 958 |
*/ |
| 959 |
if (init_graphics) { |
| 960 |
code = pdfi_set_media_size(ctx, page_dict); |
| 961 |
if (code < 0) |
| 962 |
goto exit2; |
| 963 |
|
| 964 |
pdfi_set_ctm(ctx); |
| 965 |
|
| 966 |
} else { |
| 967 |
/* Gets ctx->page.Size setup correctly |
| 968 |
* TODO: Probably not right if the page is rotated? |
| 969 |
* page.Size is needed by the transparency code, |
| 970 |
* not sure where else it might be used, if anywhere. |
| 971 |
*/ |
| 972 |
pdfi_get_media_size(ctx, page_dict); |
| 973 |
} |
| 974 |
|
| 975 |
/* Write the various CropBox, TrimBox etc to the device */ |
| 976 |
pdfi_pdfmark_write_boxes(ctx, page_dict); |
| 977 |
|
| 978 |
code = setup_page_DefaultSpaces(ctx, page_dict); |
| 979 |
if (code < 0) |
| 980 |
goto exit2; |
| 981 |
|
| 982 |
pdfi_setup_transfers(ctx); |
| 983 |
|
| 984 |
/* Set whether device needs OP support |
| 985 |
* This needs to be before transparency device is pushed, if applicable |
| 986 |
*/ |
| 987 |
pdfi_trans_set_needs_OP(ctx); |
| 988 |
pdfi_oc_init(ctx); |
| 989 |
|
| 990 |
code = pdfi_gsave(ctx); |
| 991 |
if (code < 0) |
| 992 |
goto exit2; |
| 993 |
|
| 994 |
/* Figure out if pdf14 device is needed. |
| 995 |
* This can be either for normal transparency deviceN, or because we are using |
| 996 |
* Overprint=/simulate for other devices |
| 997 |
*/ |
| 998 |
if (ctx->page.has_transparency) { |
| 999 |
need_pdf14 = true; |
| 1000 |
if (ctx->page.simulate_op) |
| 1001 |
trans_depth = -1; |
| 1002 |
} else { |
| 1003 |
/* This is the case where we are simulating overprint without transparency */ |
| 1004 |
if (ctx->page.simulate_op) { |
| 1005 |
need_pdf14 = true; |
| 1006 |
trans_depth = -1; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if (need_pdf14) { |
| 1010 |
/* We don't retain the PDF14 device */ |
| 1011 |
code = gs_push_pdf14trans_device(ctx->pgs, false, false, trans_depth, ctx->page.num_spots); |
| 1012 |
if (code >= 0) { |
| 1013 |
if (ctx->page.has_transparency && page_group_known) { |
| 1014 |
code = pdfi_trans_begin_page_group(ctx, page_dict, group_dict); |
| 1015 |
/* If setting the page group failed for some reason, abandon the page group, |
| 1016 |
* but continue with the page |
| 1017 |
*/ |
| 1018 |
if (code < 0) |
| 1019 |
page_group_known = false; |
| 1020 |
} |
| 1021 |
} else { |
| 1022 |
/* Couldn't push the transparency compositor. |
| 1023 |
* This is probably fatal, but attempt to recover by abandoning transparency |
| 1024 |
*/ |
| 1025 |
ctx->page.has_transparency = false; |
| 1026 |
need_pdf14 = false; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
/* Init a base_pgs graphics state for Patterns |
| 1031 |
* (this has to be after transparency device pushed, if applicable) |
| 1032 |
*/ |
| 1033 |
pdfi_set_DefaultQState(ctx, ctx->pgs); |
| 1034 |
|
| 1035 |
/* Render one page (including annotations) */ |
| 1036 |
if (!ctx->args.QUIET) |
| 1037 |
outprintf(ctx->memory, "Page %"PRId64"\n", page_num + 1); |
| 1038 |
|
| 1039 |
code = pdfi_process_one_page(ctx, page_dict); |
| 1040 |
|
| 1041 |
if (need_pdf14 && ctx->page.has_transparency && page_group_known) { |
| 1042 |
code1 = pdfi_trans_end_group(ctx); |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (need_pdf14) { |
| 1046 |
if (code1 < 0) { |
| 1047 |
(void)gs_abort_pdf14trans_device(ctx->pgs); |
| 1048 |
code = code1; |
| 1049 |
goto exit1; |
| 1050 |
} |
| 1051 |
|
| 1052 |
code1 = gs_pop_pdf14trans_device(ctx->pgs, false); |
| 1053 |
if (code1 < 0) { |
| 1054 |
code = code1; |
| 1055 |
goto exit1; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
exit1: |
| 1060 |
pdfi_free_DefaultQState(ctx); |
| 1061 |
pdfi_grestore(ctx); |
| 1062 |
|
| 1063 |
exit2: |
| 1064 |
pdfi_countdown(ctx->page.CurrentPageDict); |
| 1065 |
ctx->page.CurrentPageDict = NULL; |
| 1066 |
|
| 1067 |
exit3: |
| 1068 |
pdfi_countdown(page_dict); |
| 1069 |
pdfi_countdown(group_dict); |
| 1070 |
|
| 1071 |
release_page_DefaultSpaces(ctx); |
| 1072 |
|
| 1073 |
/* Flush any pattern tiles. We don't want to (potentially) return to PostScript |
| 1074 |
* with any pattern tiles referencing our objects, in case the garbager runs. |
| 1075 |
*/ |
| 1076 |
gx_pattern_cache_flush(gstate_pattern_cache(ctx->pgs)); |
| 1077 |
/* We could be smarter, but for now.. purge for each page */ |
| 1078 |
pdfi_purge_cache_resource_font(ctx); |
| 1079 |
|
| 1080 |
if (code == 0 || (!ctx->args.pdfstoponerror && code != gs_error_pdf_stackoverflow)) |
| 1081 |
if (!page_dict_error && ctx->finish_page != NULL) |
| 1082 |
code = ctx->finish_page(ctx); |
| 1083 |
return code; |
| 1084 |
} |