| 1 |
/* Copyright (C) 2001-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 |
/* Graphics state operations for the PDF interpreter */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_doc.h" |
| 20 |
#include "pdf_font_types.h" |
| 21 |
#include "pdf_gstate.h" |
| 22 |
#include "pdf_stack.h" |
| 23 |
#include "pdf_dict.h" |
| 24 |
#include "pdf_array.h" |
| 25 |
#include "pdf_func.h" |
| 26 |
#include "pdf_file.h" |
| 27 |
#include "pdf_misc.h" |
| 28 |
#include "pdf_loop_detect.h" |
| 29 |
#include "pdf_image.h" |
| 30 |
#include "pdf_pattern.h" |
| 31 |
#include "pdf_font.h" |
| 32 |
#include "pdf_colour.h" |
| 33 |
#include "pdf_trans.h" |
| 34 |
|
| 35 |
#include "gsstate.h" /* For gs_gstate */ |
| 36 |
#include "gsmatrix.h" |
| 37 |
#include "gslparam.h" |
| 38 |
#include "gstparam.h" |
| 39 |
|
| 40 |
#include "gxdht.h" |
| 41 |
#include "gxht.h" |
| 42 |
#include "gzht.h" |
| 43 |
#include "gsht.h" |
| 44 |
#include "gscoord.h" /* For gs_concat() */ |
| 45 |
#include "gsutil.h" /* For gs_next_ids() */ |
| 46 |
#include "gscolor3.h" /* For gs_setsmoothness() */ |
| 47 |
#include "gzpath.h" |
| 48 |
#include "gspenum.h" |
| 49 |
#include "gxdevsop.h" /* For special ops */ |
| 50 |
|
| 51 |
static const char *blend_mode_names[] = { |
| 52 |
GS_BLEND_MODE_NAMES, 0 |
| 53 |
}; |
| 54 |
|
| 55 |
int pdfi_get_blend_mode(pdf_context *ctx, pdf_name *name, gs_blend_mode_t *mode) |
| 56 |
{ |
| 57 |
const char **p; |
| 58 |
|
| 59 |
for (p = blend_mode_names; *p; ++p) { |
| 60 |
if (pdfi_name_is(name, *p)) { |
| 61 |
*mode = p - blend_mode_names; |
| 62 |
return 0; |
| 63 |
} |
| 64 |
} |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
void pdfi_gstate_smask_install(pdfi_int_gstate *igs, gs_memory_t *memory, pdf_dict *SMask, gs_gstate *gstate) |
| 69 |
{ |
| 70 |
void *client_data_save; |
| 71 |
|
| 72 |
if (!SMask) |
| 73 |
return; |
| 74 |
igs->memory = memory; |
| 75 |
igs->SMask = SMask; |
| 76 |
pdfi_countup(SMask); |
| 77 |
client_data_save = gstate->client_data; |
| 78 |
gstate->client_data = NULL; |
| 79 |
igs->GroupGState = gs_gstate_copy(gstate, memory); |
| 80 |
gstate->client_data = client_data_save; |
| 81 |
} |
| 82 |
|
| 83 |
void pdfi_gstate_smask_free(pdfi_int_gstate *igs) |
| 84 |
{ |
| 85 |
if (!igs->SMask) |
| 86 |
return; |
| 87 |
pdfi_countdown(igs->SMask); |
| 88 |
igs->SMask = NULL; |
| 89 |
if (igs->GroupGState) |
| 90 |
gs_gstate_free(igs->GroupGState); |
| 91 |
igs->GroupGState = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/* Allocate the interpreter's part of a graphics state. */ |
| 96 |
static void * |
| 97 |
pdfi_gstate_alloc_cb(gs_memory_t * mem) |
| 98 |
{ |
| 99 |
pdfi_int_gstate *igs; |
| 100 |
|
| 101 |
igs = (pdfi_int_gstate *)gs_alloc_bytes(mem, sizeof(pdfi_int_gstate), "pdfi_gstate_alloc"); |
| 102 |
if (igs == NULL) |
| 103 |
return NULL; |
| 104 |
memset(igs, 0, sizeof(pdfi_int_gstate)); |
| 105 |
return igs; |
| 106 |
} |
| 107 |
|
| 108 |
/* Copy the interpreter's part of a graphics state. */ |
| 109 |
static int |
| 110 |
pdfi_gstate_copy_cb(void *to, const void *from) |
| 111 |
{ |
| 112 |
const pdfi_int_gstate *igs_from = (const pdfi_int_gstate *)from; |
| 113 |
pdfi_int_gstate *igs_to = (pdfi_int_gstate *)to; |
| 114 |
|
| 115 |
/* Need to free destination contents before overwriting. |
| 116 |
* On grestore, they might be non-empty. |
| 117 |
*/ |
| 118 |
if (igs_to != NULL) { |
| 119 |
pdfi_gstate_smask_free(igs_to); |
| 120 |
pdfi_countdown(igs_to->current_font); |
| 121 |
*(pdfi_int_gstate *) igs_to = *igs_from; |
| 122 |
pdfi_countup(igs_to->current_font); |
| 123 |
pdfi_gstate_smask_install(igs_to, igs_from->memory, igs_from->SMask, igs_from->GroupGState); |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
/* Free the interpreter's part of a graphics state. */ |
| 129 |
static void |
| 130 |
pdfi_gstate_free_cb(void *old, gs_memory_t * mem, gs_gstate *pgs) |
| 131 |
{ |
| 132 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)old; |
| 133 |
if (old == NULL) |
| 134 |
return; |
| 135 |
pdfi_gstate_smask_free(igs); |
| 136 |
pdfi_countdown(igs->current_font); |
| 137 |
/* We need to use the graphics state memory, in case we are running under Ghostscript. */ |
| 138 |
gs_free_object(pgs->memory, igs, "pdfi_gstate_free"); |
| 139 |
} |
| 140 |
|
| 141 |
static const gs_gstate_client_procs pdfi_gstate_procs = { |
| 142 |
pdfi_gstate_alloc_cb, |
| 143 |
pdfi_gstate_copy_cb, |
| 144 |
pdfi_gstate_free_cb, |
| 145 |
NULL, /* copy_for */ |
| 146 |
}; |
| 147 |
|
| 148 |
int |
| 149 |
pdfi_gstate_set_client(pdf_context *ctx, gs_gstate *pgs) |
| 150 |
{ |
| 151 |
pdfi_int_gstate *igs; |
| 152 |
|
| 153 |
/* We need to use the graphics state memory, in case we are running under Ghostscript. */ |
| 154 |
igs = pdfi_gstate_alloc_cb(pgs->memory); |
| 155 |
if (igs == NULL) |
| 156 |
return_error(gs_error_VMerror); |
| 157 |
igs->ctx = ctx; |
| 158 |
gs_gstate_set_client(pgs, igs, &pdfi_gstate_procs, true /* TODO: client_has_pattern_streams ? */); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
int pdfi_concat(pdf_context *ctx) |
| 163 |
{ |
| 164 |
int code; |
| 165 |
double Values[6]; |
| 166 |
gs_matrix m; |
| 167 |
|
| 168 |
if (pdfi_count_stack(ctx) < 6) { |
| 169 |
pdfi_clearstack(ctx); |
| 170 |
return_error(gs_error_stackunderflow); |
| 171 |
} |
| 172 |
|
| 173 |
if (ctx->text.BlockDepth != 0) { |
| 174 |
/* We deliberately do not set BlockDepth to 0 here, because we can recover from a 'cm' inside a text block |
| 175 |
* even if it is illegal. So we preserve the text block and just do the cm. |
| 176 |
*/ |
| 177 |
if (ctx->text.TextClip) { |
| 178 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 179 |
|
| 180 |
ctx->text.TextClip = false; |
| 181 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 182 |
} |
| 183 |
code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_concat", NULL); |
| 184 |
if (code < 0) |
| 185 |
return code; |
| 186 |
} |
| 187 |
|
| 188 |
code = pdfi_destack_reals(ctx, Values, 6); |
| 189 |
if (code < 0) |
| 190 |
return code; |
| 191 |
|
| 192 |
m.xx = (float)Values[0]; |
| 193 |
m.xy = (float)Values[1]; |
| 194 |
m.yx = (float)Values[2]; |
| 195 |
m.yy = (float)Values[3]; |
| 196 |
m.tx = (float)Values[4]; |
| 197 |
m.ty = (float)Values[5]; |
| 198 |
|
| 199 |
return gs_concat(ctx->pgs, (const gs_matrix *)&m); |
| 200 |
} |
| 201 |
|
| 202 |
int pdfi_op_q(pdf_context *ctx) |
| 203 |
{ |
| 204 |
int code; |
| 205 |
|
| 206 |
#if DEBUG_GSAVE |
| 207 |
dbgmprintf(ctx->memory, "(doing q)\n"); /* TODO: Spammy, delete me at some point */ |
| 208 |
#endif |
| 209 |
code = pdfi_gsave(ctx); |
| 210 |
|
| 211 |
if (code < 0) |
| 212 |
return code; |
| 213 |
else { |
| 214 |
if (ctx->page.has_transparency) |
| 215 |
return gs_push_transparency_state(ctx->pgs); |
| 216 |
} |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
int pdfi_op_Q(pdf_context *ctx) |
| 221 |
{ |
| 222 |
int code = 0; |
| 223 |
|
| 224 |
#if DEBUG_GSAVE |
| 225 |
dbgmprintf(ctx->memory, "(doing Q)\n"); /* TODO: Spammy, delete me at some point */ |
| 226 |
#endif |
| 227 |
if (ctx->pgs->level <= ctx->current_stream_save.gsave_level) { |
| 228 |
/* We don't throw an error here, we just ignore it and continue */ |
| 229 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TOOMANYQ, "pdfi_op_Q", (char *)"ignoring Q"); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
if (ctx->page.has_transparency) { |
| 233 |
code = gs_pop_transparency_state(ctx->pgs, false); |
| 234 |
if (code < 0) |
| 235 |
return code; |
| 236 |
} |
| 237 |
|
| 238 |
code = pdfi_grestore(ctx); |
| 239 |
|
| 240 |
/* Special hackery for pdfwrite. If we have started doing text with a clip render mode |
| 241 |
* and the output is pdfwrite, and the current (restored gstate does not have a text clipping |
| 242 |
* render mode, then we need to tell pdfwrite that it must emit a 'Q'. |
| 243 |
*/ |
| 244 |
if (code >= 0 && ctx->device_state.preserve_tr_mode && ctx->text.TextClip && gs_currenttextrenderingmode(ctx->pgs) < 4) { |
| 245 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 246 |
|
| 247 |
ctx->text.TextClip = false; |
| 248 |
code = dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 249 |
if (code < 0 && code == gs_error_undefined) |
| 250 |
code = 0; |
| 251 |
} |
| 252 |
|
| 253 |
return code; |
| 254 |
} |
| 255 |
|
| 256 |
/* We want pdfi_grestore() so we can track and warn of "too many Qs" |
| 257 |
* in the interests of symmetry, we also have pdfi_gsave() |
| 258 |
*/ |
| 259 |
int pdfi_gsave(pdf_context *ctx) |
| 260 |
{ |
| 261 |
return gs_gsave(ctx->pgs); |
| 262 |
} |
| 263 |
|
| 264 |
int pdfi_grestore(pdf_context *ctx) |
| 265 |
{ |
| 266 |
int code = 0; |
| 267 |
|
| 268 |
/* Make sure we have encountered as many gsave operations in this |
| 269 |
* stream as grestores. If not, log an error |
| 270 |
*/ |
| 271 |
if (ctx->pgs->level > ctx->current_stream_save.gsave_level) { |
| 272 |
code = gs_grestore(ctx->pgs); |
| 273 |
} else { |
| 274 |
/* We don't throw an error here, we just ignore it and continue */ |
| 275 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TOOMANYQ, "pdfi_grestore", (char *)"ignoring q"); |
| 276 |
} |
| 277 |
return code; |
| 278 |
} |
| 279 |
|
| 280 |
int pdfi_gs_setgstate(gs_gstate * pgs, const gs_gstate * pfrom) |
| 281 |
{ |
| 282 |
int code = 0; |
| 283 |
|
| 284 |
code = gs_setgstate(pgs, pfrom); |
| 285 |
if (code < 0) |
| 286 |
return code; |
| 287 |
|
| 288 |
return code; |
| 289 |
} |
| 290 |
|
| 291 |
int pdfi_setlinewidth(pdf_context *ctx) |
| 292 |
{ |
| 293 |
int code; |
| 294 |
double d1; |
| 295 |
|
| 296 |
if (pdfi_count_stack(ctx) < 1) |
| 297 |
return_error(gs_error_stackunderflow); |
| 298 |
|
| 299 |
code = pdfi_destack_real(ctx, &d1); |
| 300 |
if (code < 0) |
| 301 |
return code; |
| 302 |
|
| 303 |
return gs_setlinewidth(ctx->pgs, d1); |
| 304 |
} |
| 305 |
|
| 306 |
int pdfi_setlinejoin(pdf_context *ctx) |
| 307 |
{ |
| 308 |
int code; |
| 309 |
int64_t i; |
| 310 |
|
| 311 |
if (pdfi_count_stack(ctx) < 1) |
| 312 |
return_error(gs_error_stackunderflow); |
| 313 |
|
| 314 |
code = pdfi_destack_int(ctx, &i); |
| 315 |
if (code < 0) |
| 316 |
return code; |
| 317 |
|
| 318 |
return gs_setlinejoin(ctx->pgs, (int)i); |
| 319 |
} |
| 320 |
|
| 321 |
int pdfi_setlinecap(pdf_context *ctx) |
| 322 |
{ |
| 323 |
int code; |
| 324 |
int64_t i; |
| 325 |
|
| 326 |
if (pdfi_count_stack(ctx) < 1) |
| 327 |
return_error(gs_error_stackunderflow); |
| 328 |
|
| 329 |
code = pdfi_destack_int(ctx, &i); |
| 330 |
if (code < 0) |
| 331 |
return code; |
| 332 |
|
| 333 |
return gs_setlinecap(ctx->pgs, i); |
| 334 |
} |
| 335 |
|
| 336 |
int pdfi_setflat(pdf_context *ctx) |
| 337 |
{ |
| 338 |
int code; |
| 339 |
double d1; |
| 340 |
|
| 341 |
if (pdfi_count_stack(ctx) < 1) |
| 342 |
return_error(gs_error_stackunderflow); |
| 343 |
|
| 344 |
code = pdfi_destack_real(ctx, &d1); |
| 345 |
if (code < 0) |
| 346 |
return code; |
| 347 |
|
| 348 |
/* PDF spec says the value is 1-100, with 0 meaning "use the default" |
| 349 |
* But gs code (and now our code) forces the value to be <= 1 |
| 350 |
* This matches what Adobe and evince seem to do (see Bug 555657). |
| 351 |
* Apparently mupdf implements this as a no-op, which is essentially |
| 352 |
* what this does now. |
| 353 |
*/ |
| 354 |
if (d1 > 1.0) |
| 355 |
d1 = 1.0; |
| 356 |
return gs_setflat(ctx->pgs, d1); |
| 357 |
} |
| 358 |
|
| 359 |
int pdfi_setdash_impl(pdf_context *ctx, pdf_array *a, double phase_d) |
| 360 |
{ |
| 361 |
float *dash_array; |
| 362 |
double temp; |
| 363 |
int i, code; |
| 364 |
|
| 365 |
dash_array = (float *)gs_alloc_bytes(ctx->memory, (size_t)pdfi_array_size(a) * sizeof (float), |
| 366 |
"temporary float array for setdash"); |
| 367 |
if (dash_array == NULL) |
| 368 |
return_error(gs_error_VMerror); |
| 369 |
|
| 370 |
for (i=0;i < pdfi_array_size(a);i++){ |
| 371 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &temp); |
| 372 |
if (code < 0) { |
| 373 |
gs_free_object(ctx->memory, dash_array, "error in setdash"); |
| 374 |
return code; |
| 375 |
} |
| 376 |
dash_array[i] = (float)temp; |
| 377 |
} |
| 378 |
code = gs_setdash(ctx->pgs, dash_array, pdfi_array_size(a), phase_d); |
| 379 |
gs_free_object(ctx->memory, dash_array, "error in setdash"); |
| 380 |
return code; |
| 381 |
} |
| 382 |
|
| 383 |
int pdfi_setdash(pdf_context *ctx) |
| 384 |
{ |
| 385 |
pdf_array *a; |
| 386 |
double phase_d; |
| 387 |
int code; |
| 388 |
|
| 389 |
if (pdfi_count_stack(ctx) < 2) { |
| 390 |
pdfi_clearstack(ctx); |
| 391 |
return_error(gs_error_stackunderflow); |
| 392 |
} |
| 393 |
|
| 394 |
code = pdfi_destack_real(ctx, &phase_d); |
| 395 |
if (code < 0) { |
| 396 |
pdfi_pop(ctx, 1); |
| 397 |
return code; |
| 398 |
} |
| 399 |
|
| 400 |
a = (pdf_array *)ctx->stack_top[-1]; |
| 401 |
pdfi_countup(a); |
| 402 |
pdfi_pop(ctx, 1); |
| 403 |
|
| 404 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 405 |
pdfi_countdown(a); |
| 406 |
return_error(gs_error_typecheck); |
| 407 |
} |
| 408 |
|
| 409 |
code = pdfi_setdash_impl(ctx, a, phase_d); |
| 410 |
pdfi_countdown(a); |
| 411 |
return code; |
| 412 |
} |
| 413 |
|
| 414 |
int pdfi_setmiterlimit(pdf_context *ctx) |
| 415 |
{ |
| 416 |
int code; |
| 417 |
double d1; |
| 418 |
|
| 419 |
if (pdfi_count_stack(ctx) < 1) |
| 420 |
return_error(gs_error_stackunderflow); |
| 421 |
|
| 422 |
code = pdfi_destack_real(ctx, &d1); |
| 423 |
if (code < 0) |
| 424 |
return code; |
| 425 |
|
| 426 |
/* PostScript (and therefore the graphics library) impose a minimum |
| 427 |
* value of 1.0 on miter limit. PDF does not specify a minimum, but less |
| 428 |
* than 1 doesn't make a lot of sense. This code brought over from the old |
| 429 |
* PDF interpreter which silently clamped the value to 1. |
| 430 |
*/ |
| 431 |
if (d1 < 1.0) |
| 432 |
d1 = 1.0; |
| 433 |
|
| 434 |
return gs_setmiterlimit(ctx->pgs, d1); |
| 435 |
} |
| 436 |
|
| 437 |
static int GS_LW(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 438 |
{ |
| 439 |
double d1; |
| 440 |
int code; |
| 441 |
|
| 442 |
code = pdfi_dict_get_number(ctx, GS, "LW", &d1); |
| 443 |
if (code < 0) |
| 444 |
return code; |
| 445 |
|
| 446 |
code = gs_setlinewidth(ctx->pgs, d1); |
| 447 |
return code; |
| 448 |
} |
| 449 |
|
| 450 |
static int GS_LC(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 451 |
{ |
| 452 |
int64_t i; |
| 453 |
int code; |
| 454 |
|
| 455 |
code = pdfi_dict_get_int(ctx, GS, "LC", &i); |
| 456 |
if (code < 0) |
| 457 |
return code; |
| 458 |
|
| 459 |
code = gs_setlinecap(ctx->pgs, i); |
| 460 |
return code; |
| 461 |
} |
| 462 |
|
| 463 |
static int GS_LJ(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 464 |
{ |
| 465 |
int64_t i; |
| 466 |
int code; |
| 467 |
|
| 468 |
code = pdfi_dict_get_int(ctx, GS, "LJ", &i); |
| 469 |
if (code < 0) |
| 470 |
return code; |
| 471 |
|
| 472 |
code = gs_setlinejoin(ctx->pgs, i); |
| 473 |
return code; |
| 474 |
} |
| 475 |
|
| 476 |
static int GS_ML(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 477 |
{ |
| 478 |
int code; |
| 479 |
double d1; |
| 480 |
|
| 481 |
code = pdfi_dict_get_number(ctx, GS, "ML", &d1); |
| 482 |
if (code < 0) |
| 483 |
return code; |
| 484 |
|
| 485 |
code = gs_setmiterlimit(ctx->pgs, d1); |
| 486 |
return code; |
| 487 |
} |
| 488 |
|
| 489 |
static int GS_D(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 490 |
{ |
| 491 |
pdf_array *a, *a1; |
| 492 |
double d; |
| 493 |
int code; |
| 494 |
|
| 495 |
code = pdfi_dict_get_type(ctx, GS, "D", PDF_ARRAY, (pdf_obj **)&a); |
| 496 |
if (code < 0) |
| 497 |
return code; |
| 498 |
|
| 499 |
code = pdfi_array_get_type(ctx, a, (int64_t)0, PDF_ARRAY, (pdf_obj **)&a1); |
| 500 |
if (code < 0) { |
| 501 |
pdfi_countdown(a); |
| 502 |
return code; |
| 503 |
} |
| 504 |
|
| 505 |
code = pdfi_array_get_number(ctx, a, (int64_t)1, &d); |
| 506 |
if (code < 0) { |
| 507 |
pdfi_countdown(a1); |
| 508 |
pdfi_countdown(a); |
| 509 |
return code; |
| 510 |
} |
| 511 |
|
| 512 |
code = pdfi_setdash_impl(ctx, a1, d); |
| 513 |
pdfi_countdown(a1); |
| 514 |
pdfi_countdown(a); |
| 515 |
return code; |
| 516 |
} |
| 517 |
|
| 518 |
static int GS_RI(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 519 |
{ |
| 520 |
pdf_name *n; |
| 521 |
int code; |
| 522 |
|
| 523 |
code = pdfi_dict_get_type(ctx, GS, "RI", PDF_NAME, (pdf_obj **)&n); |
| 524 |
if (code < 0) |
| 525 |
return code; |
| 526 |
|
| 527 |
code = pdfi_setrenderingintent(ctx, n); |
| 528 |
pdfi_countdown(n); |
| 529 |
return code; |
| 530 |
} |
| 531 |
|
| 532 |
static int GS_OP(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 533 |
{ |
| 534 |
bool b; |
| 535 |
int code; |
| 536 |
bool known=false; |
| 537 |
|
| 538 |
code = pdfi_dict_get_bool(ctx, GS, "OP", &b); |
| 539 |
if (code < 0) |
| 540 |
return code; |
| 541 |
|
| 542 |
gs_setstrokeoverprint(ctx->pgs, b); |
| 543 |
|
| 544 |
/* If op not in the dict, then also set it with OP |
| 545 |
* Because that's what gs does pdf_draw.ps/gsparamdict/OP |
| 546 |
*/ |
| 547 |
code = pdfi_dict_known(ctx, GS, "op", &known); |
| 548 |
if (!known) |
| 549 |
gs_setfilloverprint(ctx->pgs, b); |
| 550 |
|
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
static int GS_op(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 555 |
{ |
| 556 |
bool b; |
| 557 |
int code; |
| 558 |
|
| 559 |
code = pdfi_dict_get_bool(ctx, GS, "op", &b); |
| 560 |
if (code < 0) |
| 561 |
return code; |
| 562 |
|
| 563 |
gs_setfilloverprint(ctx->pgs, b); |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
static int GS_OPM(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 568 |
{ |
| 569 |
int64_t i; |
| 570 |
int code; |
| 571 |
|
| 572 |
code = pdfi_dict_get_int(ctx, GS, "OPM", &i); |
| 573 |
if (code < 0) |
| 574 |
return code; |
| 575 |
|
| 576 |
code = gs_setoverprintmode(ctx->pgs, i); |
| 577 |
return code; |
| 578 |
} |
| 579 |
|
| 580 |
static int GS_Font(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 581 |
{ |
| 582 |
pdf_array *font_array = NULL; |
| 583 |
pdf_dict *font_dict = NULL; |
| 584 |
int code = 0; |
| 585 |
double point_size = 0.0; |
| 586 |
|
| 587 |
code = pdfi_dict_get_type(ctx, GS, "Font", PDF_ARRAY, (pdf_obj **)&font_array); |
| 588 |
if (code < 0) |
| 589 |
return code; |
| 590 |
|
| 591 |
if (pdfi_array_size(font_array) != 2) |
| 592 |
return_error(gs_error_rangecheck); |
| 593 |
|
| 594 |
code = pdfi_array_get(ctx, font_array, 0, (pdf_obj **)&font_dict); |
| 595 |
if (code < 0) |
| 596 |
goto GS_Font_error; |
| 597 |
|
| 598 |
code = pdfi_array_get_number(ctx, font_array, 1, &point_size); |
| 599 |
if (code < 0) |
| 600 |
goto GS_Font_error; |
| 601 |
|
| 602 |
code = pdfi_load_dict_font(ctx, stream_dict, page_dict, font_dict, point_size); |
| 603 |
|
| 604 |
GS_Font_error: |
| 605 |
pdfi_countdown(font_array); |
| 606 |
pdfi_countdown(font_dict); |
| 607 |
return code; |
| 608 |
} |
| 609 |
|
| 610 |
static int pdfi_set_blackgeneration(pdf_context *ctx, pdf_obj *obj, pdf_dict *page_dict, bool is_BG) |
| 611 |
{ |
| 612 |
int code = 0, i; |
| 613 |
gs_function_t *pfn; |
| 614 |
|
| 615 |
switch (pdfi_type_of(obj)) { |
| 616 |
case PDF_NAME: |
| 617 |
if (is_BG) { |
| 618 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BG_ISNAME, "pdfi_set_blackgeneration", ""); |
| 619 |
} else { |
| 620 |
if (pdfi_name_is((const pdf_name *)obj, "Identity")) |
| 621 |
code = gs_setblackgeneration_remap(ctx->pgs, gs_identity_transfer, false); |
| 622 |
else if (pdfi_name_is((const pdf_name *)obj, "Default")) { |
| 623 |
code = gs_setblackgeneration_remap(ctx->pgs, ctx->page.DefaultBG.proc, false); |
| 624 |
memcpy(ctx->pgs->black_generation->values, ctx->page.DefaultBG.values, transfer_map_size * sizeof(frac)); |
| 625 |
} else |
| 626 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_BG_ISNAME, "pdfi_set_blackgeneration", ""); |
| 627 |
} |
| 628 |
goto exit; |
| 629 |
|
| 630 |
case PDF_DICT: |
| 631 |
case PDF_STREAM: |
| 632 |
code = pdfi_build_function(ctx, &pfn, NULL, 1, obj, page_dict); |
| 633 |
if (code < 0) |
| 634 |
return code; |
| 635 |
|
| 636 |
if (pfn->params.n != 1) { |
| 637 |
pdfi_free_function(ctx, pfn); |
| 638 |
return_error(gs_error_rangecheck); |
| 639 |
} |
| 640 |
|
| 641 |
gs_setblackgeneration_remap(ctx->pgs, gs_mapped_transfer, false); |
| 642 |
for (i = 0; i < transfer_map_size; i++) { |
| 643 |
float v, f; |
| 644 |
|
| 645 |
f = (1.0f / (transfer_map_size - 1)) * i; |
| 646 |
|
| 647 |
code = gs_function_evaluate(pfn, (const float *)&f, &v); |
| 648 |
if (code < 0) { |
| 649 |
pdfi_free_function(ctx, pfn); |
| 650 |
return code; |
| 651 |
} |
| 652 |
|
| 653 |
ctx->pgs->black_generation->values[i] = |
| 654 |
(v < 0.0 ? float2frac(0.0) : |
| 655 |
v >= 1.0 ? frac_1 : |
| 656 |
float2frac(v)); |
| 657 |
} |
| 658 |
code = pdfi_free_function(ctx, pfn); |
| 659 |
break; |
| 660 |
|
| 661 |
default: |
| 662 |
return_error(gs_error_typecheck); |
| 663 |
} |
| 664 |
exit: |
| 665 |
return code; |
| 666 |
} |
| 667 |
|
| 668 |
static int GS_BG(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 669 |
{ |
| 670 |
int code; |
| 671 |
pdf_obj *obj = NULL; |
| 672 |
|
| 673 |
/* If the dictionary also has a BG2, then we must use that */ |
| 674 |
code = pdfi_dict_get(ctx, GS, "BG2", &obj); |
| 675 |
if (code == 0) { |
| 676 |
pdfi_countdown(obj); |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
code = pdfi_dict_get(ctx, GS, "BG", &obj); |
| 681 |
if (code < 0) |
| 682 |
return code; |
| 683 |
|
| 684 |
code = pdfi_set_blackgeneration(ctx, obj, page_dict, true); |
| 685 |
|
| 686 |
pdfi_countdown(obj); |
| 687 |
|
| 688 |
return code; |
| 689 |
} |
| 690 |
|
| 691 |
static int GS_BG2(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 692 |
{ |
| 693 |
int code; |
| 694 |
pdf_obj *obj = NULL; |
| 695 |
|
| 696 |
code = pdfi_dict_get(ctx, GS, "BG2", &obj); |
| 697 |
if (code < 0) |
| 698 |
return code; |
| 699 |
|
| 700 |
code = pdfi_set_blackgeneration(ctx, obj, page_dict, false); |
| 701 |
|
| 702 |
pdfi_countdown(obj); |
| 703 |
|
| 704 |
return code; |
| 705 |
} |
| 706 |
|
| 707 |
static int pdfi_set_undercolorremoval(pdf_context *ctx, pdf_obj *obj, pdf_dict *page_dict, bool is_UCR) |
| 708 |
{ |
| 709 |
int code = 0, i; |
| 710 |
gs_function_t *pfn; |
| 711 |
|
| 712 |
switch (pdfi_type_of(obj)) { |
| 713 |
case PDF_NAME: |
| 714 |
if (is_UCR) { |
| 715 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_UCR_ISNAME, "pdfi_set_undercolorremoval", ""); |
| 716 |
} else { |
| 717 |
if (pdfi_name_is((const pdf_name *)obj, "Identity")) { |
| 718 |
code = gs_setundercolorremoval_remap(ctx->pgs, gs_identity_transfer, false); |
| 719 |
} else if (pdfi_name_is((const pdf_name *)obj, "Default")) { |
| 720 |
code = gs_setundercolorremoval_remap(ctx->pgs, ctx->page.DefaultUCR.proc, false); |
| 721 |
memcpy(ctx->pgs->undercolor_removal->values, ctx->page.DefaultUCR.values, transfer_map_size * sizeof(frac)); |
| 722 |
} else { |
| 723 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_UCR_ISNAME, "pdfi_set_undercolorremoval", ""); |
| 724 |
} |
| 725 |
} |
| 726 |
goto exit; |
| 727 |
|
| 728 |
case PDF_DICT: |
| 729 |
case PDF_STREAM: |
| 730 |
code = pdfi_build_function(ctx, &pfn, NULL, 1, obj, page_dict); |
| 731 |
if (code < 0) |
| 732 |
return code; |
| 733 |
|
| 734 |
if (pfn->params.n == 1) { |
| 735 |
gs_setundercolorremoval_remap(ctx->pgs, gs_mapped_transfer, false); |
| 736 |
for (i = 0; i < transfer_map_size; i++) { |
| 737 |
float v, f; |
| 738 |
|
| 739 |
f = (1.0f / (transfer_map_size - 1)) * i; |
| 740 |
|
| 741 |
code = gs_function_evaluate(pfn, (const float *)&f, &v); |
| 742 |
if (code < 0) { |
| 743 |
pdfi_free_function(ctx, pfn); |
| 744 |
return code; |
| 745 |
} |
| 746 |
|
| 747 |
ctx->pgs->undercolor_removal->values[i] = |
| 748 |
(v < 0.0 ? float2frac(0.0) : |
| 749 |
v >= 1.0 ? frac_1 : |
| 750 |
float2frac(v)); |
| 751 |
} |
| 752 |
code = pdfi_free_function(ctx, pfn); |
| 753 |
} |
| 754 |
else { |
| 755 |
(void)pdfi_free_function(ctx, pfn); |
| 756 |
code = gs_note_error(gs_error_rangecheck); |
| 757 |
} |
| 758 |
break; |
| 759 |
|
| 760 |
default: |
| 761 |
return_error(gs_error_typecheck); |
| 762 |
} |
| 763 |
exit: |
| 764 |
return code; |
| 765 |
} |
| 766 |
|
| 767 |
static int GS_UCR(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 768 |
{ |
| 769 |
int code; |
| 770 |
pdf_obj *obj = NULL; |
| 771 |
|
| 772 |
/* If the dictionary also has a UCR2, then we must use that and ignore the UCR */ |
| 773 |
code = pdfi_dict_get(ctx, GS, "UCR2", &obj); |
| 774 |
if (code == 0) { |
| 775 |
pdfi_countdown(obj); |
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
code = pdfi_dict_get(ctx, GS, "UCR", &obj); |
| 780 |
if (code < 0) |
| 781 |
return code; |
| 782 |
|
| 783 |
code = pdfi_set_undercolorremoval(ctx, obj, page_dict, true); |
| 784 |
|
| 785 |
pdfi_countdown(obj); |
| 786 |
|
| 787 |
return code; |
| 788 |
} |
| 789 |
|
| 790 |
static int GS_UCR2(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 791 |
{ |
| 792 |
int code; |
| 793 |
pdf_obj *obj = NULL; |
| 794 |
|
| 795 |
code = pdfi_dict_get(ctx, GS, "UCR2", &obj); |
| 796 |
if (code < 0) |
| 797 |
return code; |
| 798 |
|
| 799 |
code = pdfi_set_undercolorremoval(ctx, obj, page_dict, false); |
| 800 |
|
| 801 |
pdfi_countdown(obj); |
| 802 |
|
| 803 |
return code; |
| 804 |
} |
| 805 |
|
| 806 |
typedef enum { |
| 807 |
E_IDENTITY, |
| 808 |
E_DEFAULT, |
| 809 |
E_FUNCTION |
| 810 |
} pdf_transfer_function_type_e; |
| 811 |
|
| 812 |
/* We use this for both TR and TR2, is_TR is true if this is a TR, in which case we don't want |
| 813 |
* to permit /Default names for fucntions. |
| 814 |
*/ |
| 815 |
static int pdfi_set_all_transfers(pdf_context *ctx, pdf_array *a, pdf_dict *page_dict, bool is_TR) |
| 816 |
{ |
| 817 |
int code = 0, i, j; |
| 818 |
pdf_obj *o = NULL; |
| 819 |
int proc_types[4]; |
| 820 |
gs_mapping_proc map_procs[4]; |
| 821 |
gs_function_t *pfn[4]; |
| 822 |
|
| 823 |
memset(pfn, 0x00, 4 * sizeof(gs_function_t *)); |
| 824 |
memset(map_procs, 0x00, 4 * sizeof(gs_mapping_proc *)); |
| 825 |
|
| 826 |
/* Two passes, the first one is to find the appropriate transfer procedures |
| 827 |
* and do the majorty of the error checking; |
| 828 |
*/ |
| 829 |
for (i = 0; i < 4; i++) { |
| 830 |
code = pdfi_array_get(ctx, a, (uint64_t)i, &o); |
| 831 |
if (code < 0) |
| 832 |
goto exit; |
| 833 |
switch (pdfi_type_of(o)) { |
| 834 |
case PDF_NAME: |
| 835 |
if (pdfi_name_is((const pdf_name *)o, "Identity")) { |
| 836 |
proc_types[i] = E_IDENTITY; |
| 837 |
map_procs[i] = gs_identity_transfer; |
| 838 |
} else if (!is_TR && pdfi_name_is((const pdf_name *)o, "Default")) { |
| 839 |
proc_types[i] = E_DEFAULT; |
| 840 |
map_procs[i] = ctx->page.DefaultTransfers[i].proc; |
| 841 |
} else { |
| 842 |
pdfi_countdown(o); |
| 843 |
code = gs_note_error(gs_error_typecheck); |
| 844 |
goto exit; |
| 845 |
} |
| 846 |
break; |
| 847 |
case PDF_STREAM: |
| 848 |
case PDF_DICT: |
| 849 |
proc_types[i] = E_FUNCTION; |
| 850 |
map_procs[i] = gs_mapped_transfer; |
| 851 |
code = pdfi_build_function(ctx, &pfn[i], NULL, 1, o, page_dict); |
| 852 |
if (code < 0) { |
| 853 |
pdfi_countdown(o); |
| 854 |
goto exit; |
| 855 |
} |
| 856 |
if (pfn[i]->params.m != 1 || pfn[i]->params.n != 1) { |
| 857 |
pdfi_countdown(o); |
| 858 |
code = gs_note_error(gs_error_rangecheck); |
| 859 |
goto exit; |
| 860 |
} |
| 861 |
break; |
| 862 |
default: |
| 863 |
pdfi_countdown(o); |
| 864 |
code = gs_note_error(gs_error_typecheck); |
| 865 |
goto exit; |
| 866 |
} |
| 867 |
pdfi_countdown(o); |
| 868 |
} |
| 869 |
code = gs_setcolortransfer_remap(ctx->pgs, map_procs[0], map_procs[1], map_procs[2], map_procs[3], false); |
| 870 |
if (code < 0) |
| 871 |
goto exit; |
| 872 |
|
| 873 |
/* Second pass is to evaluate and set the transfer maps */ |
| 874 |
for (j = 0; j < 4; j++) { |
| 875 |
if (proc_types[j] == E_DEFAULT) { |
| 876 |
switch(j) { |
| 877 |
case 0: |
| 878 |
memcpy(ctx->pgs->set_transfer.red->values, ctx->page.DefaultTransfers[j].values, transfer_map_size * sizeof(frac)); |
| 879 |
break; |
| 880 |
case 1: |
| 881 |
memcpy(ctx->pgs->set_transfer.green->values, ctx->page.DefaultTransfers[j].values, transfer_map_size * sizeof(frac)); |
| 882 |
break; |
| 883 |
case 2: |
| 884 |
memcpy(ctx->pgs->set_transfer.blue->values, ctx->page.DefaultTransfers[j].values, transfer_map_size * sizeof(frac)); |
| 885 |
break; |
| 886 |
case 3: |
| 887 |
memcpy(ctx->pgs->set_transfer.gray->values, ctx->page.DefaultTransfers[j].values, transfer_map_size * sizeof(frac)); |
| 888 |
break; |
| 889 |
} |
| 890 |
} |
| 891 |
if (proc_types[j] == E_FUNCTION) { |
| 892 |
for (i = 0; i < transfer_map_size; i++) { |
| 893 |
float v, f; |
| 894 |
frac value; |
| 895 |
|
| 896 |
f = (1.0f / (transfer_map_size - 1)) * i; |
| 897 |
|
| 898 |
code = gs_function_evaluate(pfn[j], (const float *)&f, &v); |
| 899 |
if (code < 0) |
| 900 |
goto exit; |
| 901 |
|
| 902 |
value = |
| 903 |
(v < 0.0 ? float2frac(0.0) : |
| 904 |
v >= 1.0 ? frac_1 : |
| 905 |
float2frac(v)); |
| 906 |
switch(j) { |
| 907 |
case 0: |
| 908 |
ctx->pgs->set_transfer.red->values[i] = value; |
| 909 |
break; |
| 910 |
case 1: |
| 911 |
ctx->pgs->set_transfer.green->values[i] = value; |
| 912 |
break; |
| 913 |
case 2: |
| 914 |
ctx->pgs->set_transfer.blue->values[i] = value; |
| 915 |
break; |
| 916 |
case 3: |
| 917 |
ctx->pgs->set_transfer.gray->values[i] = value; |
| 918 |
break; |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
} |
| 923 |
exit: |
| 924 |
for (i = 0; i < 4; i++) { |
| 925 |
pdfi_free_function(ctx, pfn[i]); |
| 926 |
} |
| 927 |
return code; |
| 928 |
} |
| 929 |
|
| 930 |
static int pdfi_set_gray_transfer(pdf_context *ctx, pdf_obj *tr_obj, pdf_dict *page_dict) |
| 931 |
{ |
| 932 |
int code = 0, i; |
| 933 |
gs_function_t *pfn; |
| 934 |
|
| 935 |
if (pdfi_type_of(tr_obj) != PDF_DICT && pdfi_type_of(tr_obj) != PDF_STREAM) |
| 936 |
return_error(gs_error_typecheck); |
| 937 |
|
| 938 |
code = pdfi_build_function(ctx, &pfn, NULL, 1, tr_obj, page_dict); |
| 939 |
if (code < 0) |
| 940 |
return code; |
| 941 |
|
| 942 |
if (pfn->params.m != 1 || pfn->params.n != 1) { |
| 943 |
(void)pdfi_free_function(ctx, pfn); |
| 944 |
return_error(gs_error_rangecheck); |
| 945 |
} |
| 946 |
|
| 947 |
gs_settransfer_remap(ctx->pgs, gs_mapped_transfer, false); |
| 948 |
for (i = 0; i < transfer_map_size; i++) { |
| 949 |
float v, f; |
| 950 |
|
| 951 |
f = (1.0f / (transfer_map_size - 1)) * i; |
| 952 |
|
| 953 |
code = gs_function_evaluate(pfn, (const float *)&f, &v); |
| 954 |
if (code < 0) { |
| 955 |
pdfi_free_function(ctx, pfn); |
| 956 |
return code; |
| 957 |
} |
| 958 |
|
| 959 |
ctx->pgs->set_transfer.gray->values[i] = |
| 960 |
(v < 0.0 ? float2frac(0.0) : |
| 961 |
v >= 1.0 ? frac_1 : |
| 962 |
float2frac(v)); |
| 963 |
} |
| 964 |
return pdfi_free_function(ctx, pfn); |
| 965 |
} |
| 966 |
|
| 967 |
static int pdfi_set_transfer(pdf_context *ctx, pdf_obj *obj, pdf_dict *page_dict, bool is_TR) |
| 968 |
{ |
| 969 |
int code = 0; |
| 970 |
|
| 971 |
if (pdfi_type_of(obj) == PDF_NAME) { |
| 972 |
if (pdfi_name_is((const pdf_name *)obj, "Identity")) { |
| 973 |
code = gs_settransfer_remap(ctx->pgs, gs_identity_transfer, false); |
| 974 |
goto exit; |
| 975 |
} else { |
| 976 |
if (is_TR) { |
| 977 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_TR_NAME_NOT_IDENTITY, "pdfi_set_undercolorremoval", ""); |
| 978 |
goto exit; |
| 979 |
} else { |
| 980 |
if (pdfi_name_is((const pdf_name *)obj, "Default")) { |
| 981 |
code = gs_settransfer_remap(ctx->pgs, ctx->page.DefaultTransfers[3].proc, false); |
| 982 |
memcpy(ctx->pgs->set_transfer.gray->values, ctx->page.DefaultTransfers[3].values, transfer_map_size * sizeof(frac)); |
| 983 |
goto exit; |
| 984 |
} else { |
| 985 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_TR_NAME_NOT_IDENTITY, "pdfi_set_undercolorremoval", ""); |
| 986 |
goto exit; |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
if (pdfi_type_of(obj) == PDF_ARRAY) { |
| 993 |
if (pdfi_array_size((pdf_array *)obj) != 4) { |
| 994 |
code = gs_note_error(gs_error_rangecheck); |
| 995 |
goto exit; |
| 996 |
} else { |
| 997 |
code = pdfi_set_all_transfers(ctx, (pdf_array *)obj, page_dict, false); |
| 998 |
} |
| 999 |
} else |
| 1000 |
code = pdfi_set_gray_transfer(ctx, obj, page_dict); |
| 1001 |
|
| 1002 |
exit: |
| 1003 |
return code; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static int GS_TR(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 1007 |
{ |
| 1008 |
int code; |
| 1009 |
pdf_obj *obj = NULL; |
| 1010 |
|
| 1011 |
code = pdfi_dict_get(ctx, GS, "TR", &obj); |
| 1012 |
if (code < 0) |
| 1013 |
return code; |
| 1014 |
|
| 1015 |
code = pdfi_set_transfer(ctx, obj, page_dict, true); |
| 1016 |
|
| 1017 |
pdfi_countdown(obj); |
| 1018 |
|
| 1019 |
return code; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static int GS_TR2(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 1023 |
{ |
| 1024 |
int code; |
| 1025 |
pdf_obj *obj = NULL; |
| 1026 |
code = pdfi_dict_get(ctx, GS, "TR2", &obj); |
| 1027 |
if (code < 0) |
| 1028 |
return code; |
| 1029 |
|
| 1030 |
code = pdfi_set_transfer(ctx, obj, page_dict, false); |
| 1031 |
|
| 1032 |
pdfi_countdown(obj); |
| 1033 |
|
| 1034 |
return code; |
| 1035 |
} |
| 1036 |
|
| 1037 |
static const char *spot_functions[] = { |
| 1038 |
"{dup mul exch dup mul add 1 exch sub}", /* SimpleDot */ |
| 1039 |
"{dup mul exch dup mul add 1 sub}", /* InvertedSimpleDot */ |
| 1040 |
"{360 mul sin 2 div exch 360 mul sin 2 div add",/* DoubleDot */ |
| 1041 |
"360 mul sin 2 div exch 360 mul\ |
| 1042 |
sin 2 div add neg}", /* InvertedDoubleDot */ |
| 1043 |
"{180 mul cos exch 180 mul cos add 2 div}", /* CosineDot */ |
| 1044 |
"{360 mul sin 2 div exch 2 div \ |
| 1045 |
360 mul sin 2 div add}", /* Double */ |
| 1046 |
"{360 mul sin 2 div exch 2 div \ |
| 1047 |
360 mul sin 2 div add neg}", /* InvertedDouble */ |
| 1048 |
"{exch pop abs neg}", /* Line */ |
| 1049 |
"{pop}", /* LineX */ |
| 1050 |
"{exch pop}", /* LineY */ |
| 1051 |
"{abs exch abs 2 copy add 1.0 le\ |
| 1052 |
{dup mul exch dup mul add 1 exch sub}\ |
| 1053 |
{1 sub dup mul exch 1 sub dup mul add 1 sub}\ |
| 1054 |
ifelse}", /* Round */ |
| 1055 |
"{abs exch abs 2 copy 3 mul exch 4 mul\ |
| 1056 |
add 3 sub dup 0 lt\ |
| 1057 |
{pop dup mul exch 0.75 div dup mul add\ |
| 1058 |
4 div 1 exch sub}\ |
| 1059 |
{dup 1 gt\ |
| 1060 |
{pop 1 exch sub dup mul exch 1 exch sub\ |
| 1061 |
0.75 div dup mul add 4 div 1 sub}\ |
| 1062 |
{0.5 exch sub exch pop exch pop}}\ |
| 1063 |
ifelse}ifelse}}", /* Ellipse */ |
| 1064 |
"{dup mul 0.9 mul exch dup mul add 1 exch sub}",/* EllipseA */ |
| 1065 |
"{dup mul 0.9 mul exch dup mul add 1 sub}", /* InvertedEllipseA */ |
| 1066 |
"{dup 5 mul 8 div mul exch dup mul exch add\ |
| 1067 |
sqrt 1 exch sub}", /* EllipseB */ |
| 1068 |
"{dup mul exch dup mul 0.9 mul add 1 exch sub}",/* EllipseC */ |
| 1069 |
"{dup mul exch dup mul 0.9 mul add 1 sub}", /* InvertedEllipseC */ |
| 1070 |
"{abs exch abs 2 copy lt {exch} if pop neg}", /* Square */ |
| 1071 |
"{abs exch abs 2 copy gt {exch} if pop neg}", /* Cross */ |
| 1072 |
"{abs exch abs 0.9 mul add 2 div}", /* Rhomboid */ |
| 1073 |
"{abs exch abs 2 copy add 0.75 le\ |
| 1074 |
{dup mul exch dup mul add 1 exch sub}\ |
| 1075 |
{2 copy add 1.23 le\ |
| 1076 |
{0.85 mul add 1 exch sub}\ |
| 1077 |
{1 sub dup mul exch 1 sub dup mul add 1 sub}\ |
| 1078 |
ifelse} ifelse}" /* Diamond */ |
| 1079 |
}; |
| 1080 |
|
| 1081 |
static const char *spot_table[] = { |
| 1082 |
"SimpleDot", |
| 1083 |
"InvertedSimpleDot", |
| 1084 |
"DoubleDot", |
| 1085 |
"InvertedDoubleDot", |
| 1086 |
"CosineDot", |
| 1087 |
"Double", |
| 1088 |
"InvertedDouble", |
| 1089 |
"Line", |
| 1090 |
"LineX", |
| 1091 |
"LineY", |
| 1092 |
"Round", |
| 1093 |
"Ellipse", |
| 1094 |
"EllipseA", |
| 1095 |
"InvertedEllipseA", |
| 1096 |
"EllipseB", |
| 1097 |
"EllipseC", |
| 1098 |
"InvertedEllipseC", |
| 1099 |
"Square", |
| 1100 |
"Cross", |
| 1101 |
"Rhomboid", |
| 1102 |
"Diamond" |
| 1103 |
}; |
| 1104 |
|
| 1105 |
/* Dummy spot function */ |
| 1106 |
static float |
| 1107 |
pdfi_spot1_dummy(double x, double y) |
| 1108 |
{ |
| 1109 |
return (x + y) / 2; |
| 1110 |
} |
| 1111 |
|
| 1112 |
static int pdfi_evaluate_transfer(pdf_context *ctx, pdf_obj *transfer, pdf_dict *page_dict, gx_transfer_map **pmap) |
| 1113 |
{ |
| 1114 |
int t_ix = 0, code = 0; |
| 1115 |
float value, out; |
| 1116 |
gs_function_t *transfer_fn = NULL; |
| 1117 |
|
| 1118 |
rc_alloc_struct_1(*pmap, gx_transfer_map, &st_transfer_map, ctx->memory, |
| 1119 |
return_error(gs_error_VMerror), |
| 1120 |
"pdfi process_transfer"); |
| 1121 |
(*pmap)->proc = gs_mapped_transfer; /* 0 => use closure */ |
| 1122 |
(*pmap)->closure.proc = NULL; |
| 1123 |
(*pmap)->closure.data = NULL; |
| 1124 |
(*pmap)->id = gs_next_ids(ctx->memory, 1); |
| 1125 |
|
| 1126 |
code = pdfi_build_function(ctx, &transfer_fn, (const float *)NULL, 1, transfer, page_dict); |
| 1127 |
if (code >= 0) { |
| 1128 |
for (t_ix = 0;t_ix < 256;t_ix++) { |
| 1129 |
value = (float)t_ix * 1.0f / 255.0f; |
| 1130 |
code = gs_function_evaluate(transfer_fn, (const float *)&value, &out); |
| 1131 |
if (code < 0) |
| 1132 |
goto error; |
| 1133 |
(*pmap)->values[t_ix] = float2frac(out); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
error: |
| 1137 |
pdfi_free_function(ctx, transfer_fn); |
| 1138 |
return code; |
| 1139 |
} |
| 1140 |
|
| 1141 |
static int build_type1_halftone(pdf_context *ctx, pdf_dict *halftone_dict, pdf_dict *page_dict, gx_ht_order *porder, gs_halftone_component *phtc, char *name, int len, int comp_num) |
| 1142 |
{ |
| 1143 |
int code, i, j; |
| 1144 |
pdf_obj *obj = NULL, *transfer = NULL; |
| 1145 |
double f, a; |
| 1146 |
float values[2] = {0, 0}, domain[4] = {-1, 1, -1, 1}, out; |
| 1147 |
gs_function_t *pfn = NULL; |
| 1148 |
gx_ht_order *order = NULL; |
| 1149 |
gs_screen_enum *penum = NULL; |
| 1150 |
gs_point pt; |
| 1151 |
gx_transfer_map *pmap = NULL; |
| 1152 |
bool as; |
| 1153 |
|
| 1154 |
code = pdfi_dict_get_number(ctx, halftone_dict, "Frequency", &f); |
| 1155 |
if (code < 0) |
| 1156 |
return code; |
| 1157 |
|
| 1158 |
code = pdfi_dict_get_number(ctx, halftone_dict, "Angle", &a); |
| 1159 |
if (code < 0) |
| 1160 |
return code; |
| 1161 |
|
| 1162 |
code = pdfi_dict_get(ctx, halftone_dict, "SpotFunction", &obj); |
| 1163 |
if (code < 0) |
| 1164 |
return code; |
| 1165 |
|
| 1166 |
code = pdfi_dict_get_bool(ctx, halftone_dict, "AccurateScreens", &as); |
| 1167 |
if (code == gs_error_undefined) |
| 1168 |
as = 0; |
| 1169 |
else if (code < 0) |
| 1170 |
return code; |
| 1171 |
|
| 1172 |
order = (gx_ht_order *)gs_alloc_bytes(ctx->memory, sizeof(gx_ht_order), "build_type1_halftone"); |
| 1173 |
if (order == NULL) { |
| 1174 |
code = gs_note_error(gs_error_VMerror); |
| 1175 |
goto error; |
| 1176 |
} |
| 1177 |
memset(order, 0x00, sizeof(gx_ht_order)); |
| 1178 |
|
| 1179 |
switch (pdfi_type_of(obj)) { |
| 1180 |
case PDF_NAME: |
| 1181 |
if (pdfi_name_is((pdf_name *)obj, "Default")) { |
| 1182 |
i = 0; |
| 1183 |
} else { |
| 1184 |
for (i = 0; i < (sizeof(spot_table) / sizeof (char *)); i++) { |
| 1185 |
if (pdfi_name_is((pdf_name *)obj, spot_table[i])) |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
if (i >= (sizeof(spot_table) / sizeof (char *))) { |
| 1189 |
code = gs_note_error(gs_error_rangecheck); |
| 1190 |
goto error; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
code = pdfi_build_halftone_function(ctx, &pfn, (byte *)spot_functions[i], strlen(spot_functions[i])); |
| 1194 |
if (code < 0) |
| 1195 |
goto error; |
| 1196 |
break; |
| 1197 |
case PDF_DICT: |
| 1198 |
case PDF_STREAM: |
| 1199 |
code = pdfi_build_function(ctx, &pfn, (const float *)domain, 2, obj, page_dict); |
| 1200 |
if (code < 0) |
| 1201 |
goto error; |
| 1202 |
if (pfn->params.m != 2 || pfn->params.n != 1) { |
| 1203 |
code = gs_note_error(gs_error_rangecheck); |
| 1204 |
goto error; |
| 1205 |
} |
| 1206 |
break; |
| 1207 |
case PDF_ARRAY: |
| 1208 |
for (j = 0; j < pdfi_array_size((pdf_array *)obj); j++) { |
| 1209 |
pdf_name *n = NULL; |
| 1210 |
|
| 1211 |
code = pdfi_array_get(ctx, (pdf_array *)obj, j, (pdf_obj **)&n); |
| 1212 |
if (code < 0) |
| 1213 |
goto error; |
| 1214 |
if (pdfi_type_of(n) != PDF_NAME) { |
| 1215 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BAD_TYPE, "build_type1_halftone", "Halftone array element is not a name")) < 0) { |
| 1216 |
pdfi_countdown(n); |
| 1217 |
goto error; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
else { |
| 1221 |
for (i = 0; i < (sizeof(spot_table) / sizeof (char *)); i++) { |
| 1222 |
if (pdfi_name_is((pdf_name *)n, spot_table[i])) { |
| 1223 |
pdfi_countdown(n); |
| 1224 |
n = NULL; |
| 1225 |
code = pdfi_build_halftone_function(ctx, &pfn, (byte *)spot_functions[i], strlen(spot_functions[i])); |
| 1226 |
if (code < 0) |
| 1227 |
goto error; |
| 1228 |
break; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
if (i >= (sizeof(spot_table) / sizeof (char *))) { |
| 1232 |
pdfi_countdown(n); |
| 1233 |
n = NULL; |
| 1234 |
} else |
| 1235 |
break; |
| 1236 |
} |
| 1237 |
pdfi_countdown(n); |
| 1238 |
} |
| 1239 |
if (j >= pdfi_array_size((pdf_array *)obj)) { |
| 1240 |
code = gs_note_error(gs_error_rangecheck); |
| 1241 |
goto error; |
| 1242 |
} |
| 1243 |
break; |
| 1244 |
default: |
| 1245 |
code = gs_note_error(gs_error_typecheck); |
| 1246 |
goto error; |
| 1247 |
} |
| 1248 |
|
| 1249 |
if (pdfi_dict_knownget(ctx, halftone_dict, "TransferFunction", &transfer) > 0) { |
| 1250 |
switch (pdfi_type_of(transfer)) { |
| 1251 |
case PDF_NAME: |
| 1252 |
/* As far as I can tell, only /Identity is valid as a name, so we can just ignore |
| 1253 |
* names, if it's not Identity it would be an error (which we would ignore) and if |
| 1254 |
* it is, it has no effect. So what's the point ? |
| 1255 |
*/ |
| 1256 |
break; |
| 1257 |
case PDF_STREAM: |
| 1258 |
pdfi_evaluate_transfer(ctx, transfer, page_dict, &pmap); |
| 1259 |
break; |
| 1260 |
default: |
| 1261 |
/* should be an error, but we can just ignore it */ |
| 1262 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TYPECHECK, "build_type1_halftone", NULL); |
| 1263 |
break; |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
phtc->params.spot.screen.frequency = f; |
| 1268 |
phtc->params.spot.screen.angle = a; |
| 1269 |
phtc->params.spot.screen.spot_function = pdfi_spot1_dummy; |
| 1270 |
phtc->params.spot.transfer = (code > 0 ? (gs_mapping_proc) 0 : gs_identity_transfer); |
| 1271 |
phtc->params.spot.transfer_closure.proc = 0; |
| 1272 |
phtc->params.spot.transfer_closure.data = 0; |
| 1273 |
phtc->params.spot.accurate_screens = as; |
| 1274 |
phtc->type = ht_type_spot; |
| 1275 |
code = pdfi_get_name_index(ctx, name, len, (unsigned int *)&phtc->cname); |
| 1276 |
if (code < 0) |
| 1277 |
goto error; |
| 1278 |
|
| 1279 |
if (comp_num == -1) |
| 1280 |
phtc->comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)name, len, 1); |
| 1281 |
else |
| 1282 |
phtc->comp_number = comp_num; |
| 1283 |
|
| 1284 |
code = gs_screen_order_init_memory(order, ctx->pgs, &phtc->params.spot.screen, |
| 1285 |
gs_currentaccuratescreens(ctx->memory), ctx->memory); |
| 1286 |
if (code < 0) |
| 1287 |
goto error; |
| 1288 |
|
| 1289 |
penum = gs_screen_enum_alloc(ctx->memory, "build_type1_halftone"); |
| 1290 |
if (penum == 0) { |
| 1291 |
code = gs_error_VMerror; |
| 1292 |
goto error; |
| 1293 |
} |
| 1294 |
|
| 1295 |
code = gs_screen_enum_init_memory(penum, order, ctx->pgs, &phtc->params.spot.screen, ctx->memory); |
| 1296 |
if (code < 0) |
| 1297 |
goto error; |
| 1298 |
|
| 1299 |
do { |
| 1300 |
/* Generate x and y, the parameteric variables */ |
| 1301 |
code = gs_screen_currentpoint(penum, &pt); |
| 1302 |
if (code < 0) |
| 1303 |
goto error; |
| 1304 |
|
| 1305 |
if (code == 1) |
| 1306 |
break; |
| 1307 |
|
| 1308 |
/* Process sample */ |
| 1309 |
values[0] = pt.x, values[1] = pt.y; |
| 1310 |
code = gs_function_evaluate(pfn, (const float *)&values, &out); |
| 1311 |
if (code < 0) |
| 1312 |
goto error; |
| 1313 |
|
| 1314 |
/* Store the sample */ |
| 1315 |
code = gs_screen_next(penum, out); |
| 1316 |
if (code < 0) |
| 1317 |
goto error; |
| 1318 |
|
| 1319 |
} while (1); |
| 1320 |
code = 0; |
| 1321 |
*porder = penum->order; |
| 1322 |
(*porder).transfer = pmap; |
| 1323 |
|
| 1324 |
error: |
| 1325 |
pdfi_countdown(transfer); |
| 1326 |
pdfi_countdown(obj); |
| 1327 |
pdfi_free_function(ctx, pfn); |
| 1328 |
if (code < 0 && order != NULL) { |
| 1329 |
gs_free_object(ctx->memory, order->bit_data, "build_type1_halftone error"); |
| 1330 |
gs_free_object(ctx->memory, order->levels, "build_type1_halftone error"); |
| 1331 |
} |
| 1332 |
if (code < 0 && pmap != NULL) |
| 1333 |
rc_decrement(pmap, "pdfi process_transfer"); |
| 1334 |
gs_free_object(ctx->memory, order, "build_type1_halftone"); |
| 1335 |
gs_free_object(ctx->memory, penum, "build_type1_halftone"); |
| 1336 |
return code; |
| 1337 |
} |
| 1338 |
|
| 1339 |
static int build_type6_halftone(pdf_context *ctx, pdf_stream *halftone_stream, pdf_dict *page_dict, |
| 1340 |
gx_ht_order *porder, gs_halftone_component *phtc, char *name, int len) |
| 1341 |
{ |
| 1342 |
int code; |
| 1343 |
int64_t w, h, length = 0, returned_length = 0; |
| 1344 |
gs_threshold2_halftone *ptp = &phtc->params.threshold2; |
| 1345 |
pdf_dict *halftone_dict = NULL; |
| 1346 |
|
| 1347 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)halftone_stream, &halftone_dict); |
| 1348 |
if (code < 0) |
| 1349 |
return code; |
| 1350 |
|
| 1351 |
ptp->thresholds.data = NULL; |
| 1352 |
ptp->thresholds.size = 0; |
| 1353 |
|
| 1354 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Width", &w); |
| 1355 |
if (code < 0) |
| 1356 |
return code; |
| 1357 |
ptp->width = w; |
| 1358 |
ptp->width2 = 0; |
| 1359 |
|
| 1360 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Height", &h); |
| 1361 |
if (code < 0) |
| 1362 |
return code; |
| 1363 |
ptp->height = h; |
| 1364 |
ptp->height2 = 0; |
| 1365 |
|
| 1366 |
if (ptp->width < 1 || w > max_int || |
| 1367 |
ptp->height < 1 || h > max_int) |
| 1368 |
return_error(gs_error_rangecheck); |
| 1369 |
|
| 1370 |
ptp->bytes_per_sample = 1; |
| 1371 |
ptp->transfer = 0; |
| 1372 |
ptp->transfer_closure.proc = 0; |
| 1373 |
ptp->transfer_closure.data = 0; |
| 1374 |
|
| 1375 |
code = pdfi_get_name_index(ctx, name, len, (unsigned int *)&phtc->cname); |
| 1376 |
if (code < 0) |
| 1377 |
goto error; |
| 1378 |
|
| 1379 |
phtc->comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)name, len, 1); |
| 1380 |
|
| 1381 |
returned_length = length = w * h; |
| 1382 |
code = pdfi_stream_to_buffer(ctx, halftone_stream, |
| 1383 |
(byte **)&ptp->thresholds.data, &returned_length); |
| 1384 |
if (code < 0) |
| 1385 |
goto error; |
| 1386 |
|
| 1387 |
/* Guard against a returned buffer larger than a gs_const_bytestring can hold */ |
| 1388 |
/* We must also take care that we have sufficient data for the process_threshold2() |
| 1389 |
* function, which will attempt to read width * height bytes. |
| 1390 |
*/ |
| 1391 |
if (length > max_uint || returned_length < length) { |
| 1392 |
code = gs_note_error(gs_error_rangecheck); |
| 1393 |
goto error; |
| 1394 |
} |
| 1395 |
|
| 1396 |
ptp->thresholds.size = returned_length; |
| 1397 |
phtc->type = ht_type_threshold2; |
| 1398 |
return code; |
| 1399 |
|
| 1400 |
error: |
| 1401 |
gs_free_object(ctx->memory, (byte *)ptp->thresholds.data, "build_type6_halftone"); |
| 1402 |
return code; |
| 1403 |
} |
| 1404 |
|
| 1405 |
static int build_type10_halftone(pdf_context *ctx, pdf_stream *halftone_stream, pdf_dict *page_dict, gx_ht_order *porder, gs_halftone_component *phtc, char *name, int len) |
| 1406 |
{ |
| 1407 |
int code; |
| 1408 |
int64_t w, h, length = 0, returned_length = 0; |
| 1409 |
gs_threshold2_halftone *ptp = &phtc->params.threshold2; |
| 1410 |
pdf_dict *halftone_dict = NULL; |
| 1411 |
|
| 1412 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)halftone_stream, &halftone_dict); |
| 1413 |
|
| 1414 |
ptp->thresholds.data = NULL; |
| 1415 |
ptp->thresholds.size = 0; |
| 1416 |
|
| 1417 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Xsquare", &w); |
| 1418 |
if (code < 0) |
| 1419 |
return code; |
| 1420 |
ptp->width = ptp->height = w; |
| 1421 |
|
| 1422 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Ysquare", &h); |
| 1423 |
if (code < 0) |
| 1424 |
return code; |
| 1425 |
ptp->width2 = ptp->height2 = h; |
| 1426 |
|
| 1427 |
if (w < 1 || w > max_int || |
| 1428 |
h < 1 || h > max_int) |
| 1429 |
return_error(gs_error_rangecheck); |
| 1430 |
|
| 1431 |
ptp->bytes_per_sample = 1; |
| 1432 |
ptp->transfer = 0; |
| 1433 |
ptp->transfer_closure.proc = 0; |
| 1434 |
ptp->transfer_closure.data = 0; |
| 1435 |
|
| 1436 |
code = pdfi_get_name_index(ctx, name, len, (unsigned int *)&phtc->cname); |
| 1437 |
if (code < 0) |
| 1438 |
goto error; |
| 1439 |
|
| 1440 |
phtc->comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)name, len, 1); |
| 1441 |
|
| 1442 |
returned_length = length = (w * w) + (h * h); |
| 1443 |
code = pdfi_stream_to_buffer(ctx, halftone_stream, |
| 1444 |
(byte **)&ptp->thresholds.data, &returned_length); |
| 1445 |
if (code < 0) |
| 1446 |
goto error; |
| 1447 |
|
| 1448 |
/* Guard against a returned buffer larger than a gs_const_bytestring can hold */ |
| 1449 |
/* We must also take care that we have sufficient data for the process_threshold2() |
| 1450 |
* function, which will attempt to read width * height bytes. |
| 1451 |
*/ |
| 1452 |
if (length > max_uint || returned_length < length) { |
| 1453 |
code = gs_note_error(gs_error_rangecheck); |
| 1454 |
goto error; |
| 1455 |
} |
| 1456 |
|
| 1457 |
ptp->thresholds.size = returned_length; |
| 1458 |
phtc->type = ht_type_threshold2; |
| 1459 |
return code; |
| 1460 |
|
| 1461 |
error: |
| 1462 |
gs_free_object(ctx->memory, (byte *)ptp->thresholds.data, "build_type10_halftone"); |
| 1463 |
return code; |
| 1464 |
} |
| 1465 |
|
| 1466 |
static int build_type16_halftone(pdf_context *ctx, pdf_stream *halftone_stream, pdf_dict *page_dict, gx_ht_order *porder, gs_halftone_component *phtc, char *name, int len) |
| 1467 |
{ |
| 1468 |
int code; |
| 1469 |
int64_t w, h, length = 0, returned_length = 0; |
| 1470 |
gs_threshold2_halftone *ptp = &phtc->params.threshold2; |
| 1471 |
pdf_dict *halftone_dict = NULL; |
| 1472 |
|
| 1473 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)halftone_stream, &halftone_dict); |
| 1474 |
if (code < 0) |
| 1475 |
return code; |
| 1476 |
|
| 1477 |
ptp->thresholds.data = NULL; |
| 1478 |
ptp->thresholds.size = 0; |
| 1479 |
|
| 1480 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Width", &w); |
| 1481 |
if (code < 0) |
| 1482 |
return code; |
| 1483 |
ptp->width = w; |
| 1484 |
|
| 1485 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Height", &h); |
| 1486 |
if (code < 0) |
| 1487 |
return code; |
| 1488 |
ptp->height = h; |
| 1489 |
|
| 1490 |
if (ptp->width < 1 || w > max_int || |
| 1491 |
ptp->height < 1 || h > max_int) |
| 1492 |
return_error(gs_error_rangecheck); |
| 1493 |
|
| 1494 |
w = 0; |
| 1495 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Width2", &w); |
| 1496 |
if (code < 0 && code != gs_error_undefined) |
| 1497 |
return code; |
| 1498 |
ptp->width2 = w; |
| 1499 |
|
| 1500 |
h = 0; |
| 1501 |
code = pdfi_dict_get_int(ctx, halftone_dict, "Height2", &h); |
| 1502 |
if (code < 0 && code != gs_error_undefined) |
| 1503 |
return code; |
| 1504 |
ptp->height2 = h; |
| 1505 |
|
| 1506 |
if (ptp->width2 < 0 || w > max_int || |
| 1507 |
ptp->height2 < 0 || h > max_int) |
| 1508 |
return_error(gs_error_rangecheck); |
| 1509 |
|
| 1510 |
ptp->bytes_per_sample = 2; |
| 1511 |
ptp->transfer = 0; |
| 1512 |
ptp->transfer_closure.proc = 0; |
| 1513 |
ptp->transfer_closure.data = 0; |
| 1514 |
|
| 1515 |
code = pdfi_get_name_index(ctx, name, len, (unsigned int *)&phtc->cname); |
| 1516 |
if (code < 0) |
| 1517 |
goto error; |
| 1518 |
|
| 1519 |
phtc->comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)name, len, 1); |
| 1520 |
|
| 1521 |
if (ptp->width2 != 0 && ptp->height2 != 0) { |
| 1522 |
returned_length = length = (((int64_t)ptp->width * ptp->height) + ((int64_t)ptp->width2 * ptp->height2)) * 2; |
| 1523 |
} else { |
| 1524 |
returned_length = length = (int64_t)ptp->width * (int64_t)ptp->height * 2; |
| 1525 |
} |
| 1526 |
|
| 1527 |
code = pdfi_stream_to_buffer(ctx, halftone_stream, |
| 1528 |
(byte **)&ptp->thresholds.data, &returned_length); |
| 1529 |
if (code < 0) |
| 1530 |
goto error; |
| 1531 |
|
| 1532 |
/* Guard against a returned buffer larger than a gs_const_bytestring can hold */ |
| 1533 |
/* We must also take care that we have sufficient data for the process_threshold2() |
| 1534 |
* function, which will attempt to read width * height bytes. |
| 1535 |
*/ |
| 1536 |
if (length > max_uint || returned_length < length) { |
| 1537 |
code = gs_note_error(gs_error_rangecheck); |
| 1538 |
goto error; |
| 1539 |
} |
| 1540 |
|
| 1541 |
ptp->thresholds.size = returned_length; |
| 1542 |
phtc->type = ht_type_threshold2; |
| 1543 |
return code; |
| 1544 |
|
| 1545 |
error: |
| 1546 |
gs_free_object(ctx->memory, (byte *)ptp->thresholds.data, "build_type16_halftone"); |
| 1547 |
return code; |
| 1548 |
} |
| 1549 |
|
| 1550 |
static void pdfi_free_halftone(gs_memory_t *memory, void *data, client_name_t cname) |
| 1551 |
{ |
| 1552 |
int i=0; |
| 1553 |
gs_halftone *pht = (gs_halftone *)data; |
| 1554 |
gs_halftone_component comp; |
| 1555 |
|
| 1556 |
for (i=0;i< pht->params.multiple.num_comp;i++) { |
| 1557 |
comp = pht->params.multiple.components[i]; |
| 1558 |
switch(comp.type) { |
| 1559 |
case ht_type_threshold: |
| 1560 |
if (comp.params.threshold.thresholds.data != NULL) |
| 1561 |
gs_free_object(memory, (byte *)comp.params.threshold.thresholds.data, "pdfi_free_halftone - thresholds"); |
| 1562 |
break; |
| 1563 |
case ht_type_threshold2: |
| 1564 |
if (comp.params.threshold2.thresholds.data != NULL) |
| 1565 |
gs_free_object(memory, (byte *)comp.params.threshold2.thresholds.data, "pdfi_free_halftone - thresholds"); |
| 1566 |
break; |
| 1567 |
default: |
| 1568 |
break; |
| 1569 |
} |
| 1570 |
} |
| 1571 |
gs_free_object(memory, pht->params.multiple.components, "pdfi_free_halftone"); |
| 1572 |
gs_free_object(memory, pht, "pdfi_free_halftone"); |
| 1573 |
} |
| 1574 |
|
| 1575 |
static int build_type5_halftone(pdf_context *ctx, pdf_dict *halftone_dict, pdf_dict *page_dict, gx_device_halftone *pdht, gs_halftone *pht) |
| 1576 |
{ |
| 1577 |
int code, code1, str_len, comp_number; |
| 1578 |
int64_t type; |
| 1579 |
char *str = NULL; |
| 1580 |
bool known = false; |
| 1581 |
gs_halftone_component *phtc = NULL, *phtc1; |
| 1582 |
gx_ht_order_component *pocs = 0; |
| 1583 |
pdf_obj *Key = NULL, *Value = NULL; |
| 1584 |
uint64_t index = 0, ix = 0; |
| 1585 |
int NumComponents = 0; |
| 1586 |
gx_ht_order *porder1 = NULL; |
| 1587 |
pdf_dict *subdict = NULL; |
| 1588 |
|
| 1589 |
/* The only case involving multiple halftones, we need to enumerate each entry |
| 1590 |
* in the dictionary |
| 1591 |
*/ |
| 1592 |
/* Type 5 halftone dictionaries are required to have a Default */ |
| 1593 |
code = pdfi_dict_known(ctx, halftone_dict, "Default", &known); |
| 1594 |
if (code < 0) |
| 1595 |
return code; |
| 1596 |
if (!known) { |
| 1597 |
code = gs_note_error(gs_error_undefined); |
| 1598 |
return code; |
| 1599 |
} |
| 1600 |
|
| 1601 |
code = pdfi_loop_detector_mark(ctx); |
| 1602 |
if (code < 0) |
| 1603 |
goto error; |
| 1604 |
code = pdfi_dict_first(ctx, halftone_dict, &Key, &Value, &index); |
| 1605 |
code1 = pdfi_loop_detector_cleartomark(ctx); |
| 1606 |
if (code < 0) |
| 1607 |
goto error; |
| 1608 |
if (code1 < 0) { |
| 1609 |
code = code1; |
| 1610 |
goto error; |
| 1611 |
} |
| 1612 |
|
| 1613 |
/* First establish the number of components from the halftone which we will use. |
| 1614 |
* If the component number is GX_DEVICE_COLOR_MAX_COMPONENTS then its the default, |
| 1615 |
* if its < 0 then its not available in the device. Otherwise its a colorant which is |
| 1616 |
* being rendered, so we need to set that halftone component. The Default will be |
| 1617 |
* stored in the device halftone order rather than in the component array order |
| 1618 |
* members. |
| 1619 |
*/ |
| 1620 |
do { |
| 1621 |
if (pdfi_type_of(Key) != PDF_NAME) { |
| 1622 |
code = gs_note_error(gs_error_typecheck); |
| 1623 |
goto error; |
| 1624 |
} |
| 1625 |
if (!pdfi_name_is((const pdf_name *)Key, "HalftoneName") && !pdfi_name_is((const pdf_name *)Key, "HalftoneType") && !pdfi_name_is((const pdf_name *)Key, "Type")) { |
| 1626 |
code = pdfi_string_from_name(ctx, (pdf_name *)Key, &str, &str_len); |
| 1627 |
if (code < 0) |
| 1628 |
goto error; |
| 1629 |
|
| 1630 |
comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)str, str_len, |
| 1631 |
ht_type_multiple); |
| 1632 |
if (comp_number >= 0) |
| 1633 |
NumComponents++; |
| 1634 |
gs_free_object(ctx->memory, str, "pdfi_string_from_name"); |
| 1635 |
str = NULL; |
| 1636 |
} |
| 1637 |
|
| 1638 |
pdfi_countdown(Key); |
| 1639 |
pdfi_countdown(Value); |
| 1640 |
Key = Value = NULL; |
| 1641 |
|
| 1642 |
code = pdfi_loop_detector_mark(ctx); |
| 1643 |
if (code < 0) |
| 1644 |
goto error; |
| 1645 |
code = pdfi_dict_next(ctx, halftone_dict, &Key, &Value, &index); |
| 1646 |
code1 = pdfi_loop_detector_cleartomark(ctx); |
| 1647 |
if (code < 0 && code != gs_error_undefined) |
| 1648 |
goto error; |
| 1649 |
else if (code1 < 0) { |
| 1650 |
code = code1; |
| 1651 |
goto error; |
| 1652 |
} |
| 1653 |
} while (code >= 0); |
| 1654 |
|
| 1655 |
if (NumComponents == 0) { |
| 1656 |
code = gs_note_error(gs_error_syntaxerror); |
| 1657 |
goto error; |
| 1658 |
} |
| 1659 |
|
| 1660 |
pocs = gs_alloc_struct_array(ctx->memory, NumComponents, |
| 1661 |
gx_ht_order_component, |
| 1662 |
&st_ht_order_component_element, |
| 1663 |
"gs_sethalftone"); |
| 1664 |
if (pocs == NULL) |
| 1665 |
goto error; |
| 1666 |
|
| 1667 |
memset(pocs, 0x00, NumComponents * sizeof(gx_ht_order_component)); |
| 1668 |
pdht->components = pocs; |
| 1669 |
pdht->num_comp = NumComponents; |
| 1670 |
phtc = (gs_halftone_component *)gs_alloc_bytes(ctx->memory, (size_t)sizeof(gs_halftone_component) * NumComponents, "pdfi_do_halftone"); |
| 1671 |
if (phtc == 0) { |
| 1672 |
code = gs_note_error(gs_error_VMerror); |
| 1673 |
goto error; |
| 1674 |
} |
| 1675 |
|
| 1676 |
code = pdfi_loop_detector_mark(ctx); |
| 1677 |
if (code < 0) |
| 1678 |
goto error; |
| 1679 |
code = pdfi_dict_first(ctx, halftone_dict, &Key, &Value, &index); |
| 1680 |
code1 = pdfi_loop_detector_cleartomark(ctx); |
| 1681 |
if (code < 0) |
| 1682 |
goto error; |
| 1683 |
else if (code1 < 0) { |
| 1684 |
code = code1; |
| 1685 |
goto error; |
| 1686 |
} |
| 1687 |
|
| 1688 |
/* index 0 in the component array is reserved for the Default, we can't get here without |
| 1689 |
* having a /Default, so we just leave room for it and start filing the other inks from |
| 1690 |
* index 1. |
| 1691 |
*/ |
| 1692 |
ix = 1; |
| 1693 |
do { |
| 1694 |
if (pdfi_type_of(Key) != PDF_NAME) { |
| 1695 |
code = gs_note_error(gs_error_typecheck); |
| 1696 |
goto error; |
| 1697 |
} |
| 1698 |
if (!pdfi_name_is((const pdf_name *)Key, "HalftoneName") && !pdfi_name_is((const pdf_name *)Key, "HalftoneType") && !pdfi_name_is((const pdf_name *)Key, "Type")) { |
| 1699 |
if (!pdfi_name_is((const pdf_name *)Key, "HalftoneName") && !pdfi_name_is((const pdf_name *)Key, "HalftoneType") && !pdfi_name_is((const pdf_name *)Key, "Type")) { |
| 1700 |
code = pdfi_dict_from_obj(ctx, Value, &subdict); |
| 1701 |
if (code < 0) |
| 1702 |
goto error; |
| 1703 |
|
| 1704 |
code = pdfi_string_from_name(ctx, (pdf_name *)Key, &str, &str_len); |
| 1705 |
if (code < 0) |
| 1706 |
goto error; |
| 1707 |
|
| 1708 |
comp_number = gs_cname_to_colorant_number(ctx->pgs, (byte *)str, str_len, |
| 1709 |
ht_type_multiple); |
| 1710 |
if (comp_number >= 0) { |
| 1711 |
/* If comp_number == GX_DEVICE_COLOR_MAX_COMPONENTS then it is the /Default |
| 1712 |
* In that case we want to store it in index 0 of the halftone components array |
| 1713 |
*/ |
| 1714 |
if (comp_number == GX_DEVICE_COLOR_MAX_COMPONENTS) { |
| 1715 |
phtc[0].comp_number = comp_number; |
| 1716 |
porder1 = &(pdht->components[0].corder); |
| 1717 |
pdht->components[0].comp_number = comp_number; |
| 1718 |
phtc1 = &phtc[0]; |
| 1719 |
} else { |
| 1720 |
phtc[ix].comp_number = comp_number; |
| 1721 |
porder1 = &(pdht->components[ix].corder); |
| 1722 |
pdht->components[ix].comp_number = phtc[ix].comp_number; |
| 1723 |
phtc1 = &phtc[ix++]; |
| 1724 |
} |
| 1725 |
|
| 1726 |
code = pdfi_dict_get_int(ctx, subdict, "HalftoneType", &type); |
| 1727 |
if (code < 0) |
| 1728 |
goto error; |
| 1729 |
|
| 1730 |
switch(type) { |
| 1731 |
case 1: |
| 1732 |
code = build_type1_halftone(ctx, (pdf_dict *)Value, page_dict, porder1, phtc1, str, str_len, comp_number); |
| 1733 |
if (code < 0) |
| 1734 |
goto error; |
| 1735 |
break; |
| 1736 |
case 6: |
| 1737 |
if (pdfi_type_of(Value) != PDF_STREAM) { |
| 1738 |
code = gs_note_error(gs_error_typecheck); |
| 1739 |
goto error; |
| 1740 |
} |
| 1741 |
code = build_type6_halftone(ctx, (pdf_stream *)Value, page_dict, porder1, phtc1, str, str_len); |
| 1742 |
if (code < 0) |
| 1743 |
goto error; |
| 1744 |
code = process_threshold2(porder1, ctx->pgs, &phtc1->params.threshold2, ctx->memory); |
| 1745 |
if (code < 0) |
| 1746 |
goto error; |
| 1747 |
break; |
| 1748 |
case 10: |
| 1749 |
if (pdfi_type_of(Value) != PDF_STREAM) { |
| 1750 |
code = gs_note_error(gs_error_typecheck); |
| 1751 |
goto error; |
| 1752 |
} |
| 1753 |
code = build_type10_halftone(ctx, (pdf_stream *)Value, page_dict, porder1, phtc1, str, str_len); |
| 1754 |
if (code < 0) |
| 1755 |
goto error; |
| 1756 |
code = process_threshold2(porder1, ctx->pgs, &phtc1->params.threshold2, ctx->memory); |
| 1757 |
if (code < 0) |
| 1758 |
goto error; |
| 1759 |
break; |
| 1760 |
case 16: |
| 1761 |
if (pdfi_type_of(Value) != PDF_STREAM) { |
| 1762 |
code = gs_note_error(gs_error_typecheck); |
| 1763 |
goto error; |
| 1764 |
} |
| 1765 |
code = build_type16_halftone(ctx, (pdf_stream *)Value, page_dict, porder1, phtc1, str, str_len); |
| 1766 |
if (code < 0) |
| 1767 |
goto error; |
| 1768 |
code = process_threshold2(porder1, ctx->pgs, &phtc1->params.threshold2, ctx->memory); |
| 1769 |
if (code < 0) |
| 1770 |
goto error; |
| 1771 |
break; |
| 1772 |
default: |
| 1773 |
code = gs_note_error(gs_error_rangecheck); |
| 1774 |
goto error; |
| 1775 |
break; |
| 1776 |
|
| 1777 |
} |
| 1778 |
gs_free_object(ctx->memory, str, "pdfi_string_from_name"); |
| 1779 |
str = NULL; |
| 1780 |
} else { |
| 1781 |
gs_free_object(ctx->memory, str, "pdfi_string_from_name"); |
| 1782 |
str = NULL; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
pdfi_countdown(Key); |
| 1788 |
pdfi_countdown(Value); |
| 1789 |
Key = Value = NULL; |
| 1790 |
|
| 1791 |
code = pdfi_loop_detector_mark(ctx); |
| 1792 |
if (code < 0) |
| 1793 |
goto error; |
| 1794 |
code = pdfi_dict_next(ctx, halftone_dict, &Key, &Value, &index); |
| 1795 |
code1 = pdfi_loop_detector_cleartomark(ctx); |
| 1796 |
if (code < 0 && code != gs_error_undefined) |
| 1797 |
goto error; |
| 1798 |
else if (code1 < 0) { |
| 1799 |
code = code1; |
| 1800 |
goto error; |
| 1801 |
} |
| 1802 |
} while (code >= 0); |
| 1803 |
code = 0; |
| 1804 |
|
| 1805 |
/* If we only had one component, it must be the Default, in which case we |
| 1806 |
* do not need the components array. So we can copy the order from the 0th |
| 1807 |
* index of the components array (Default is stored at index 0) to the |
| 1808 |
* device halftone order, and free the components array. |
| 1809 |
*/ |
| 1810 |
pdht->order = pdht->components[0].corder; |
| 1811 |
if (ix == 1) { |
| 1812 |
gs_free_object(ctx->memory, pocs, "pdfi_build_type5_halftone"); |
| 1813 |
pdht->components = 0; |
| 1814 |
pdht->num_comp = 0; |
| 1815 |
} else { |
| 1816 |
pdht->components = pocs; |
| 1817 |
pdht->num_comp = ix; |
| 1818 |
} |
| 1819 |
|
| 1820 |
pht->type = ht_type_multiple; |
| 1821 |
pht->params.multiple.components = phtc; |
| 1822 |
pht->params.multiple.num_comp = NumComponents; |
| 1823 |
pht->params.multiple.get_colorname_string = pdfi_separation_name_from_index; |
| 1824 |
|
| 1825 |
return 0; |
| 1826 |
|
| 1827 |
error: |
| 1828 |
pdfi_countdown(Key); |
| 1829 |
pdfi_countdown(Value); |
| 1830 |
gs_free_object(ctx->memory, str, "pdfi_string_from_name"); |
| 1831 |
gs_free_object(ctx->memory, pocs, "pdfi_build_type5_halftone"); |
| 1832 |
gs_free_object(ctx->memory, phtc, "pdfi_build_type5_halftone"); |
| 1833 |
pht->params.multiple.components = NULL; |
| 1834 |
pht->params.multiple.num_comp = 0; |
| 1835 |
pdht->components = NULL; |
| 1836 |
pdht->num_comp = 0; |
| 1837 |
return code; |
| 1838 |
} |
| 1839 |
|
| 1840 |
static int pdfi_do_halftone(pdf_context *ctx, pdf_obj *halftone_obj, pdf_dict *page_dict) |
| 1841 |
{ |
| 1842 |
int code; |
| 1843 |
char *str = NULL; |
| 1844 |
int64_t type; |
| 1845 |
gs_halftone *pht = NULL; |
| 1846 |
gx_device_halftone *pdht = NULL; |
| 1847 |
gs_halftone_component *phtc = NULL; |
| 1848 |
pdf_obj *Key = NULL, *Value = NULL, *transfer = NULL; |
| 1849 |
pdf_dict *halftone_dict = NULL; |
| 1850 |
gx_transfer_map *pmap = NULL; |
| 1851 |
|
| 1852 |
code = pdfi_dict_from_obj(ctx, halftone_obj, &halftone_dict); |
| 1853 |
if (code < 0) |
| 1854 |
return code; |
| 1855 |
|
| 1856 |
code = pdfi_dict_get_int(ctx, halftone_dict, "HalftoneType", &type); |
| 1857 |
if (code < 0) |
| 1858 |
return code; |
| 1859 |
|
| 1860 |
pht = (gs_halftone *)gs_alloc_bytes(ctx->memory, sizeof(gs_halftone), "pdfi_do_halftone"); |
| 1861 |
if (pht == 0) { |
| 1862 |
code = gs_note_error(gs_error_VMerror); |
| 1863 |
goto error; |
| 1864 |
} |
| 1865 |
memset(pht, 0x00, sizeof(gs_halftone)); |
| 1866 |
pht->rc.memory = ctx->memory; |
| 1867 |
pht->rc.free = pdfi_free_halftone; |
| 1868 |
|
| 1869 |
pdht = (gx_device_halftone *)gs_alloc_bytes(ctx->memory, sizeof(gx_device_halftone), "pdfi_do_halftone"); |
| 1870 |
if (pdht == 0) { |
| 1871 |
code = gs_note_error(gs_error_VMerror); |
| 1872 |
goto error; |
| 1873 |
} |
| 1874 |
memset(pdht, 0x00, sizeof(gx_device_halftone)); |
| 1875 |
pdht->num_dev_comp = ctx->pgs->device->color_info.num_components; |
| 1876 |
pdht->rc.memory = ctx->memory; |
| 1877 |
|
| 1878 |
switch(type) { |
| 1879 |
case 1: |
| 1880 |
phtc = (gs_halftone_component *)gs_alloc_bytes(ctx->memory, sizeof(gs_halftone_component), "pdfi_do_halftone"); |
| 1881 |
if (phtc == 0) { |
| 1882 |
code = gs_note_error(gs_error_VMerror); |
| 1883 |
goto error; |
| 1884 |
} |
| 1885 |
|
| 1886 |
code = build_type1_halftone(ctx, halftone_dict, page_dict, &pdht->order, phtc, (char *)"Default", 7, -1); |
| 1887 |
if (code < 0) |
| 1888 |
goto error; |
| 1889 |
|
| 1890 |
pht->type = ht_type_multiple; |
| 1891 |
pht->params.multiple.components = phtc; |
| 1892 |
pht->params.multiple.num_comp = 1; |
| 1893 |
pht->params.multiple.get_colorname_string = pdfi_separation_name_from_index; |
| 1894 |
code = gx_gstate_dev_ht_install(ctx->pgs, pdht, pht->type, gs_currentdevice_inline(ctx->pgs), HT_OBJTYPE_DEFAULT); |
| 1895 |
if (code < 0) |
| 1896 |
goto error; |
| 1897 |
|
| 1898 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 1899 |
rc_decrement(ctx->pgs->halftone, "pdfi_do_halftone(halftone)"); |
| 1900 |
ctx->pgs->halftone = pht; |
| 1901 |
rc_increment(ctx->pgs->halftone); |
| 1902 |
gx_unset_both_dev_colors(ctx->pgs); |
| 1903 |
break; |
| 1904 |
|
| 1905 |
case 5: |
| 1906 |
code = build_type5_halftone(ctx, halftone_dict, page_dict, pdht, pht); |
| 1907 |
if (code < 0) |
| 1908 |
goto error; |
| 1909 |
|
| 1910 |
/* build_type5_halftone does the work of gs_sethalftone_prepare as well, so we don't need that here */ |
| 1911 |
|
| 1912 |
code = gx_gstate_dev_ht_install(ctx->pgs, pdht, pht->type, gs_currentdevice_inline(ctx->pgs), HT_OBJTYPE_DEFAULT); |
| 1913 |
if (code < 0) |
| 1914 |
goto error; |
| 1915 |
|
| 1916 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 1917 |
rc_decrement(ctx->pgs->halftone, ""); |
| 1918 |
ctx->pgs->halftone = pht; |
| 1919 |
rc_increment(ctx->pgs->halftone); |
| 1920 |
gx_unset_both_dev_colors(ctx->pgs); |
| 1921 |
break; |
| 1922 |
|
| 1923 |
case 6: |
| 1924 |
if (pdfi_type_of(halftone_obj) != PDF_STREAM) |
| 1925 |
return_error(gs_error_typecheck); |
| 1926 |
phtc = (gs_halftone_component *)gs_alloc_bytes(ctx->memory, sizeof(gs_halftone_component), "pdfi_do_halftone"); |
| 1927 |
if (phtc == 0) { |
| 1928 |
code = gs_note_error(gs_error_VMerror); |
| 1929 |
goto error; |
| 1930 |
} |
| 1931 |
|
| 1932 |
code = build_type6_halftone(ctx, (pdf_stream *)halftone_obj, page_dict, &pdht->order, phtc, (char *)"Default", 7); |
| 1933 |
if (code < 0) |
| 1934 |
goto error; |
| 1935 |
|
| 1936 |
pht->type = ht_type_multiple; |
| 1937 |
pht->params.multiple.components = phtc; |
| 1938 |
pht->params.multiple.num_comp = 1; |
| 1939 |
pht->params.multiple.get_colorname_string = pdfi_separation_name_from_index; |
| 1940 |
|
| 1941 |
code = gs_sethalftone_prepare(ctx->pgs, pht, pdht); |
| 1942 |
if (code < 0) |
| 1943 |
goto error; |
| 1944 |
|
| 1945 |
/* Transfer function pdht->order->transfer */ |
| 1946 |
if (pdfi_dict_knownget(ctx, ((pdf_stream *)halftone_obj)->stream_dict, "TransferFunction", &transfer) > 0) { |
| 1947 |
switch (pdfi_type_of(transfer)) { |
| 1948 |
case PDF_NAME: |
| 1949 |
/* As far as I can tell, only /Identity is valid as a name, so we can just ignore |
| 1950 |
* names, if it's not Identity it would be an error (which we would ignore) and if |
| 1951 |
* it is, it has no effect. So what's the point ? |
| 1952 |
*/ |
| 1953 |
break; |
| 1954 |
case PDF_STREAM: |
| 1955 |
/* If we get an error here, we can just ignore it, and not apply the transfer */ |
| 1956 |
code = pdfi_evaluate_transfer(ctx, transfer, page_dict, &pmap); |
| 1957 |
if (code >= 0) { |
| 1958 |
pdht->order.transfer = pmap; |
| 1959 |
} |
| 1960 |
break; |
| 1961 |
default: |
| 1962 |
/* should be an error, but we can just ignore it */ |
| 1963 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TYPECHECK, "do_halftone", NULL); |
| 1964 |
break; |
| 1965 |
} |
| 1966 |
pdfi_countdown(transfer); |
| 1967 |
} |
| 1968 |
|
| 1969 |
code = gx_gstate_dev_ht_install(ctx->pgs, pdht, pht->type, gs_currentdevice_inline(ctx->pgs), HT_OBJTYPE_DEFAULT); |
| 1970 |
if (code < 0) |
| 1971 |
goto error; |
| 1972 |
|
| 1973 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 1974 |
rc_decrement(ctx->pgs->halftone, "pdfi_do_halftone(halftone)"); |
| 1975 |
ctx->pgs->halftone = pht; |
| 1976 |
rc_increment(ctx->pgs->halftone); |
| 1977 |
gx_unset_both_dev_colors(ctx->pgs); |
| 1978 |
break; |
| 1979 |
case 10: |
| 1980 |
if (pdfi_type_of(halftone_obj) != PDF_STREAM) |
| 1981 |
return_error(gs_error_typecheck); |
| 1982 |
phtc = (gs_halftone_component *)gs_alloc_bytes(ctx->memory, sizeof(gs_halftone_component), "pdfi_do_halftone"); |
| 1983 |
if (phtc == 0) { |
| 1984 |
code = gs_note_error(gs_error_VMerror); |
| 1985 |
goto error; |
| 1986 |
} |
| 1987 |
|
| 1988 |
code = build_type10_halftone(ctx, (pdf_stream *)halftone_obj, page_dict, &pdht->order, phtc, (char *)"Default", 7); |
| 1989 |
if (code < 0) |
| 1990 |
goto error; |
| 1991 |
|
| 1992 |
pht->type = ht_type_multiple; |
| 1993 |
pht->params.multiple.components = phtc; |
| 1994 |
pht->params.multiple.num_comp = 1; |
| 1995 |
pht->params.multiple.get_colorname_string = pdfi_separation_name_from_index; |
| 1996 |
|
| 1997 |
code = gs_sethalftone_prepare(ctx->pgs, pht, pdht); |
| 1998 |
if (code < 0) |
| 1999 |
goto error; |
| 2000 |
|
| 2001 |
/* Transfer function pdht->order->transfer */ |
| 2002 |
if (pdfi_dict_knownget(ctx, ((pdf_stream *)halftone_obj)->stream_dict, "TransferFunction", &transfer) > 0) { |
| 2003 |
switch (pdfi_type_of(transfer)) { |
| 2004 |
case PDF_NAME: |
| 2005 |
/* As far as I can tell, only /Identity is valid as a name, so we can just ignore |
| 2006 |
* names, if it's not Identity it would be an error (which we would ignore) and if |
| 2007 |
* it is, it has no effect. So what's the point ? |
| 2008 |
*/ |
| 2009 |
break; |
| 2010 |
case PDF_STREAM: |
| 2011 |
/* If we get an error here, we can just ignore it, and not apply the transfer */ |
| 2012 |
code = pdfi_evaluate_transfer(ctx, transfer, page_dict, &pmap); |
| 2013 |
if (code >= 0) { |
| 2014 |
pdht->order.transfer = pmap; |
| 2015 |
} |
| 2016 |
break; |
| 2017 |
default: |
| 2018 |
/* should be an error, but we can just ignore it */ |
| 2019 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TYPECHECK, "do_halftone", NULL); |
| 2020 |
break; |
| 2021 |
} |
| 2022 |
pdfi_countdown(transfer); |
| 2023 |
} |
| 2024 |
|
| 2025 |
code = gx_gstate_dev_ht_install(ctx->pgs, pdht, pht->type, gs_currentdevice_inline(ctx->pgs), HT_OBJTYPE_DEFAULT); |
| 2026 |
if (code < 0) |
| 2027 |
goto error; |
| 2028 |
|
| 2029 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 2030 |
rc_decrement(ctx->pgs->halftone, "pdfi_do_halftone(halftone)"); |
| 2031 |
ctx->pgs->halftone = pht; |
| 2032 |
rc_increment(ctx->pgs->halftone); |
| 2033 |
gx_unset_both_dev_colors(ctx->pgs); |
| 2034 |
break; |
| 2035 |
case 16: |
| 2036 |
if (pdfi_type_of(halftone_obj) != PDF_STREAM) |
| 2037 |
return_error(gs_error_typecheck); |
| 2038 |
phtc = (gs_halftone_component *)gs_alloc_bytes(ctx->memory, sizeof(gs_halftone_component), "pdfi_do_halftone"); |
| 2039 |
if (phtc == 0) { |
| 2040 |
code = gs_note_error(gs_error_VMerror); |
| 2041 |
goto error; |
| 2042 |
} |
| 2043 |
|
| 2044 |
code = build_type16_halftone(ctx, (pdf_stream *)halftone_obj, page_dict, &pdht->order, phtc, (char *)"Default", 7); |
| 2045 |
if (code < 0) |
| 2046 |
goto error; |
| 2047 |
|
| 2048 |
pht->type = ht_type_multiple; |
| 2049 |
pht->params.multiple.components = phtc; |
| 2050 |
pht->params.multiple.num_comp = 1; |
| 2051 |
pht->params.multiple.get_colorname_string = pdfi_separation_name_from_index; |
| 2052 |
|
| 2053 |
code = gs_sethalftone_prepare(ctx->pgs, pht, pdht); |
| 2054 |
if (code < 0) |
| 2055 |
goto error; |
| 2056 |
|
| 2057 |
/* Transfer function pdht->order->transfer */ |
| 2058 |
if (pdfi_dict_knownget(ctx, ((pdf_stream *)halftone_obj)->stream_dict, "TransferFunction", &transfer) > 0) { |
| 2059 |
switch (pdfi_type_of(transfer)) { |
| 2060 |
case PDF_NAME: |
| 2061 |
/* As far as I can tell, only /Identity is valid as a name, so we can just ignore |
| 2062 |
* names, if it's not Identity it would be an error (which we would ignore) and if |
| 2063 |
* it is, it has no effect. So what's the point ? |
| 2064 |
*/ |
| 2065 |
break; |
| 2066 |
case PDF_STREAM: |
| 2067 |
/* If we get an error here, we can just ignore it, and not apply the transfer */ |
| 2068 |
code = pdfi_evaluate_transfer(ctx, transfer, page_dict, &pmap); |
| 2069 |
if (code >= 0) { |
| 2070 |
pdht->order.transfer = pmap; |
| 2071 |
} |
| 2072 |
break; |
| 2073 |
default: |
| 2074 |
/* should be an error, but we can just ignore it */ |
| 2075 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_TYPECHECK, "do_halftone", NULL); |
| 2076 |
break; |
| 2077 |
} |
| 2078 |
pdfi_countdown(transfer); |
| 2079 |
} |
| 2080 |
|
| 2081 |
code = gx_gstate_dev_ht_install(ctx->pgs, pdht, pht->type, gs_currentdevice_inline(ctx->pgs), HT_OBJTYPE_DEFAULT); |
| 2082 |
if (code < 0) |
| 2083 |
goto error; |
| 2084 |
|
| 2085 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 2086 |
rc_decrement(ctx->pgs->halftone, "pdfi_do_halftone(halftone)"); |
| 2087 |
ctx->pgs->halftone = pht; |
| 2088 |
rc_increment(ctx->pgs->halftone); |
| 2089 |
gx_unset_both_dev_colors(ctx->pgs); |
| 2090 |
break; |
| 2091 |
default: |
| 2092 |
code = gs_note_error(gs_error_rangecheck); |
| 2093 |
goto error; |
| 2094 |
break; |
| 2095 |
} |
| 2096 |
gs_free_object(ctx->memory, pdht, "pdfi_do_halftone"); |
| 2097 |
return 0; |
| 2098 |
|
| 2099 |
error: |
| 2100 |
if (pdht != NULL) |
| 2101 |
gx_device_halftone_release(pdht, pdht->rc.memory); |
| 2102 |
gs_free_object(ctx->memory, str, "pdfi_string_from_name"); |
| 2103 |
pdfi_countdown(Key); |
| 2104 |
pdfi_countdown(Value); |
| 2105 |
gs_free_object(ctx->memory, pht, "pdfi_do_halftone"); |
| 2106 |
gs_free_object(ctx->memory, phtc, "pdfi_do_halftone"); |
| 2107 |
gs_free_object(ctx->memory, pdht, "pdfi_do_halftone"); |
| 2108 |
return code; |
| 2109 |
} |
| 2110 |
|
| 2111 |
static int GS_HT(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2112 |
{ |
| 2113 |
int code; |
| 2114 |
pdf_obj *obj = NULL; |
| 2115 |
|
| 2116 |
code = pdfi_dict_get(ctx, GS, "HT", &obj); |
| 2117 |
if (code < 0) |
| 2118 |
return code; |
| 2119 |
|
| 2120 |
|
| 2121 |
if (pdfi_type_of(obj) == PDF_NAME) { |
| 2122 |
if (pdfi_name_is((const pdf_name *)obj, "Default")) { |
| 2123 |
goto exit; |
| 2124 |
} else { |
| 2125 |
code = gs_note_error(gs_error_rangecheck); |
| 2126 |
goto exit; |
| 2127 |
} |
| 2128 |
} else { |
| 2129 |
code = pdfi_do_halftone(ctx, obj, page_dict); |
| 2130 |
} |
| 2131 |
if (code < 0) { |
| 2132 |
code = pdfi_set_error_stop(ctx, code, NULL, E_BAD_HALFTONE, "GS_HT", "Halftone will be ignored"); |
| 2133 |
} |
| 2134 |
|
| 2135 |
exit: |
| 2136 |
pdfi_countdown(obj); |
| 2137 |
return code; |
| 2138 |
} |
| 2139 |
|
| 2140 |
static int GS_FL(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2141 |
{ |
| 2142 |
int code; |
| 2143 |
double d1; |
| 2144 |
|
| 2145 |
code = pdfi_dict_get_number(ctx, GS, "FL", &d1); |
| 2146 |
if (code < 0) |
| 2147 |
return code; |
| 2148 |
|
| 2149 |
code = gs_setflat(ctx->pgs, d1); |
| 2150 |
return code; |
| 2151 |
} |
| 2152 |
|
| 2153 |
static int GS_SM(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2154 |
{ |
| 2155 |
int code; |
| 2156 |
double d1; |
| 2157 |
|
| 2158 |
code = pdfi_dict_get_number(ctx, GS, "SM", &d1); |
| 2159 |
if (code < 0) |
| 2160 |
return code; |
| 2161 |
|
| 2162 |
code = gs_setsmoothness(ctx->pgs, d1); |
| 2163 |
return code; |
| 2164 |
} |
| 2165 |
|
| 2166 |
static int GS_SA(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2167 |
{ |
| 2168 |
bool b; |
| 2169 |
int code; |
| 2170 |
|
| 2171 |
code = pdfi_dict_get_bool(ctx, GS, "SA", &b); |
| 2172 |
if (code < 0) |
| 2173 |
return code; |
| 2174 |
|
| 2175 |
return gs_setstrokeadjust(ctx->pgs, b); |
| 2176 |
} |
| 2177 |
|
| 2178 |
static int GS_BM(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2179 |
{ |
| 2180 |
pdf_name *n; |
| 2181 |
int code; |
| 2182 |
gs_blend_mode_t mode = 0; /* Start with /Normal */ |
| 2183 |
|
| 2184 |
code = pdfi_dict_get(ctx, GS, "BM", (pdf_obj **)&n); |
| 2185 |
if (code < 0) |
| 2186 |
return code; |
| 2187 |
|
| 2188 |
if (pdfi_type_of(n) == PDF_ARRAY) { |
| 2189 |
int i; |
| 2190 |
pdf_array *a = (pdf_array *)n; |
| 2191 |
|
| 2192 |
for (i=0;i < pdfi_array_size(a);i++){ |
| 2193 |
code = pdfi_array_get_type(ctx, a, i, PDF_NAME, (pdf_obj **)&n); |
| 2194 |
if (code < 0) |
| 2195 |
continue; |
| 2196 |
code = pdfi_get_blend_mode(ctx, n, &mode); |
| 2197 |
pdfi_countdown(n); |
| 2198 |
if (code == 0) |
| 2199 |
break; |
| 2200 |
} |
| 2201 |
pdfi_countdown(a); |
| 2202 |
return gs_setblendmode(ctx->pgs, mode); |
| 2203 |
} |
| 2204 |
|
| 2205 |
if (pdfi_type_of(n) == PDF_NAME) { |
| 2206 |
code = pdfi_get_blend_mode(ctx, n, &mode); |
| 2207 |
pdfi_countdown(n); |
| 2208 |
if (code == 0) |
| 2209 |
return gs_setblendmode(ctx->pgs, mode); |
| 2210 |
return_error(gs_error_undefined); |
| 2211 |
} |
| 2212 |
return_error(gs_error_typecheck); |
| 2213 |
} |
| 2214 |
|
| 2215 |
static int GS_SMask(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2216 |
{ |
| 2217 |
pdf_obj *o = NULL; |
| 2218 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)ctx->pgs->client_data; |
| 2219 |
int code; |
| 2220 |
bool Processed; |
| 2221 |
|
| 2222 |
if (ctx->page.has_transparency == false || ctx->args.notransparency == true) |
| 2223 |
return 0; |
| 2224 |
|
| 2225 |
code = pdfi_dict_get(ctx, GS, "SMask", (pdf_obj **)&o); |
| 2226 |
if (code < 0) |
| 2227 |
return code; |
| 2228 |
|
| 2229 |
switch (pdfi_type_of(o)) { |
| 2230 |
case PDF_NAME: |
| 2231 |
{ |
| 2232 |
pdf_name *n = (pdf_name *)o; |
| 2233 |
|
| 2234 |
if (pdfi_name_is(n, "None")) { |
| 2235 |
if (igs->SMask) { |
| 2236 |
pdfi_gstate_smask_free(igs); |
| 2237 |
code = pdfi_trans_end_smask_notify(ctx); |
| 2238 |
} |
| 2239 |
goto exit; |
| 2240 |
} |
| 2241 |
code = pdfi_find_resource(ctx, (unsigned char *)"ExtGState", n, stream_dict, page_dict, &o); |
| 2242 |
pdfi_countdown(n); |
| 2243 |
if (code < 0) |
| 2244 |
return code; |
| 2245 |
break; |
| 2246 |
} |
| 2247 |
|
| 2248 |
case PDF_DICT: |
| 2249 |
{ |
| 2250 |
code = pdfi_dict_knownget_bool(ctx, (pdf_dict *)o, "Processed", &Processed); |
| 2251 |
/* Need to clear the Processed flag in the SMask if another value is set |
| 2252 |
* (even if it's the same SMask?) |
| 2253 |
* TODO: I think there is a better way to do this that doesn't require sticking this |
| 2254 |
* flag in the SMask dictionary. But for now, let's get correct behavior. |
| 2255 |
*/ |
| 2256 |
if (code > 0 && Processed) { |
| 2257 |
code = pdfi_dict_put_bool(ctx, (pdf_dict *)o, "Processed", false); |
| 2258 |
if (code < 0) |
| 2259 |
return code; |
| 2260 |
} |
| 2261 |
if (igs->SMask) |
| 2262 |
pdfi_gstate_smask_free(igs); |
| 2263 |
/* We need to use the graphics state memory, in case we are running under Ghostscript. */ |
| 2264 |
pdfi_gstate_smask_install(igs, ctx->pgs->memory, (pdf_dict *)o, ctx->pgs); |
| 2265 |
break; |
| 2266 |
} |
| 2267 |
|
| 2268 |
default: |
| 2269 |
break; |
| 2270 |
} |
| 2271 |
|
| 2272 |
exit: |
| 2273 |
pdfi_countdown(o); |
| 2274 |
return 0; |
| 2275 |
} |
| 2276 |
|
| 2277 |
static int GS_CA(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2278 |
{ |
| 2279 |
int code; |
| 2280 |
double d1; |
| 2281 |
|
| 2282 |
code = pdfi_dict_get_number(ctx, GS, "CA", &d1); |
| 2283 |
if (code < 0) |
| 2284 |
return code; |
| 2285 |
|
| 2286 |
if (d1 > 1.0) { |
| 2287 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_CA_OUTOFRANGE, "GS_CA", NULL); |
| 2288 |
d1 = 1.0; |
| 2289 |
} |
| 2290 |
|
| 2291 |
if (d1 < 0.0) { |
| 2292 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_CA_OUTOFRANGE, "GS_CA", NULL); |
| 2293 |
d1 = 0.0; |
| 2294 |
} |
| 2295 |
|
| 2296 |
code = gs_setstrokeconstantalpha(ctx->pgs, d1); |
| 2297 |
return code; |
| 2298 |
} |
| 2299 |
|
| 2300 |
static int GS_ca(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2301 |
{ |
| 2302 |
int code; |
| 2303 |
double d1; |
| 2304 |
|
| 2305 |
code = pdfi_dict_get_number(ctx, GS, "ca", &d1); |
| 2306 |
if (code < 0) |
| 2307 |
return code; |
| 2308 |
|
| 2309 |
if (d1 > 1.0) { |
| 2310 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_CA_OUTOFRANGE, "GS_ca", NULL); |
| 2311 |
d1 = 1.0; |
| 2312 |
} |
| 2313 |
|
| 2314 |
if (d1 < 0.0) { |
| 2315 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_CA_OUTOFRANGE, "GS_ca", NULL); |
| 2316 |
d1 = 0.0; |
| 2317 |
} |
| 2318 |
|
| 2319 |
code = gs_setfillconstantalpha(ctx->pgs, d1); |
| 2320 |
return code; |
| 2321 |
} |
| 2322 |
|
| 2323 |
static int GS_AIS(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2324 |
{ |
| 2325 |
bool b; |
| 2326 |
int code; |
| 2327 |
|
| 2328 |
code = pdfi_dict_get_bool(ctx, GS, "AIS", &b); |
| 2329 |
if (code < 0) |
| 2330 |
return code; |
| 2331 |
|
| 2332 |
return gs_setalphaisshape(ctx->pgs, b); |
| 2333 |
} |
| 2334 |
|
| 2335 |
static int GS_TK(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2336 |
{ |
| 2337 |
bool b; |
| 2338 |
int code; |
| 2339 |
|
| 2340 |
code = pdfi_dict_get_bool(ctx, GS, "TK", &b); |
| 2341 |
if (code < 0) |
| 2342 |
return code; |
| 2343 |
|
| 2344 |
return gs_settextknockout(ctx->pgs, b); |
| 2345 |
} |
| 2346 |
|
| 2347 |
typedef int (*GS_proc)(pdf_context *ctx, pdf_dict *GS, pdf_dict *stream_dict, pdf_dict *page_dict); |
| 2348 |
|
| 2349 |
typedef struct GS_Func { |
| 2350 |
const char *Name; |
| 2351 |
GS_proc proc; |
| 2352 |
} GS_Func_t; |
| 2353 |
|
| 2354 |
GS_Func_t ExtGStateTable[] = { |
| 2355 |
{"LW", GS_LW}, |
| 2356 |
{"LC", GS_LC}, |
| 2357 |
{"LJ", GS_LJ}, |
| 2358 |
{"ML", GS_ML}, |
| 2359 |
{"D", GS_D}, |
| 2360 |
{"RI", GS_RI}, |
| 2361 |
{"OP", GS_OP}, |
| 2362 |
{"op", GS_op}, |
| 2363 |
{"OPM", GS_OPM}, |
| 2364 |
{"Font", GS_Font}, |
| 2365 |
{"BG", GS_BG}, |
| 2366 |
{"BG2", GS_BG2}, |
| 2367 |
{"UCR", GS_UCR}, |
| 2368 |
{"UCR2", GS_UCR2}, |
| 2369 |
{"TR", GS_TR}, |
| 2370 |
{"TR2", GS_TR2}, |
| 2371 |
{"HT", GS_HT}, |
| 2372 |
{"FL", GS_FL}, |
| 2373 |
{"SM", GS_SM}, |
| 2374 |
{"SA", GS_SA}, |
| 2375 |
{"BM", GS_BM}, |
| 2376 |
{"SMask", GS_SMask}, |
| 2377 |
{"CA", GS_CA}, |
| 2378 |
{"ca", GS_ca}, |
| 2379 |
{"AIS", GS_AIS}, |
| 2380 |
{"TK", GS_TK}, |
| 2381 |
}; |
| 2382 |
|
| 2383 |
/* Set gstate from dictionary |
| 2384 |
* NOTE: stream_dict may not be needed and can currently be NULL. |
| 2385 |
* If we decide it can't be NULL, check Patterns implementation to refactor it to pass that param. |
| 2386 |
*/ |
| 2387 |
int pdfi_set_ExtGState(pdf_context *ctx, pdf_dict *stream_dict, |
| 2388 |
pdf_dict *page_dict, pdf_dict *gstate_dict) |
| 2389 |
{ |
| 2390 |
int code, i, limit = sizeof(ExtGStateTable) / sizeof (GS_Func_t); |
| 2391 |
bool known; |
| 2392 |
|
| 2393 |
for (i=0;i < limit; i++) { |
| 2394 |
code = pdfi_dict_known(ctx, gstate_dict, ExtGStateTable[i].Name, &known); |
| 2395 |
if (code < 0) |
| 2396 |
break; |
| 2397 |
if (known) { |
| 2398 |
code = ExtGStateTable[i].proc(ctx, gstate_dict, NULL, page_dict); |
| 2399 |
if (code < 0) |
| 2400 |
break; |
| 2401 |
} |
| 2402 |
} |
| 2403 |
return code; |
| 2404 |
} |
| 2405 |
|
| 2406 |
int pdfi_setgstate(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2407 |
{ |
| 2408 |
pdf_name *n = NULL; |
| 2409 |
pdf_obj *o = NULL; |
| 2410 |
int code=0, code1 = 0; |
| 2411 |
|
| 2412 |
code = pdfi_loop_detector_mark(ctx); |
| 2413 |
if (code < 0) |
| 2414 |
return code; |
| 2415 |
|
| 2416 |
if (pdfi_count_stack(ctx) < 1) { |
| 2417 |
code = gs_note_error(gs_error_stackunderflow); |
| 2418 |
goto setgstate_error; |
| 2419 |
} |
| 2420 |
n = (pdf_name *)ctx->stack_top[-1]; |
| 2421 |
pdfi_countup(n); |
| 2422 |
pdfi_pop(ctx, 1); |
| 2423 |
|
| 2424 |
if (pdfi_type_of(n) != PDF_NAME) { |
| 2425 |
code = gs_note_error(gs_error_typecheck); |
| 2426 |
goto setgstate_error; |
| 2427 |
} |
| 2428 |
|
| 2429 |
code = pdfi_find_resource(ctx, (unsigned char *)"ExtGState", n, (pdf_dict *)stream_dict, |
| 2430 |
page_dict, &o); |
| 2431 |
if (code < 0) |
| 2432 |
goto setgstate_error; |
| 2433 |
|
| 2434 |
if (pdfi_type_of(o) != PDF_DICT) { |
| 2435 |
code = gs_note_error(gs_error_typecheck); |
| 2436 |
goto setgstate_error; |
| 2437 |
} |
| 2438 |
|
| 2439 |
code = pdfi_set_ExtGState(ctx, stream_dict, page_dict, (pdf_dict *)o); |
| 2440 |
|
| 2441 |
setgstate_error: |
| 2442 |
code1 = pdfi_loop_detector_cleartomark(ctx); |
| 2443 |
if (code == 0) code = code1; |
| 2444 |
|
| 2445 |
pdfi_countdown(n); |
| 2446 |
pdfi_countdown(o); |
| 2447 |
return code; |
| 2448 |
} |
| 2449 |
|
| 2450 |
|
| 2451 |
int pdfi_free_DefaultQState(pdf_context *ctx) |
| 2452 |
{ |
| 2453 |
if (ctx->DefaultQState) |
| 2454 |
gs_gstate_free(ctx->DefaultQState); |
| 2455 |
ctx->DefaultQState = NULL; |
| 2456 |
return 0; |
| 2457 |
} |
| 2458 |
|
| 2459 |
int pdfi_set_DefaultQState(pdf_context *ctx, gs_gstate *pgs) |
| 2460 |
{ |
| 2461 |
pdfi_free_DefaultQState(ctx); |
| 2462 |
/* We need to use the graphics state memory, in case we are running under Ghostscript. */ |
| 2463 |
ctx->DefaultQState = gs_gstate_copy(pgs, ctx->pgs->memory); |
| 2464 |
if (ctx->DefaultQState == NULL) |
| 2465 |
return_error(gs_error_VMerror); |
| 2466 |
return 0; |
| 2467 |
} |
| 2468 |
|
| 2469 |
gs_gstate *pdfi_get_DefaultQState(pdf_context *ctx) |
| 2470 |
{ |
| 2471 |
return ctx->DefaultQState; |
| 2472 |
} |
| 2473 |
|
| 2474 |
int pdfi_copy_DefaultQState(pdf_context *ctx, gs_gstate **pgs) |
| 2475 |
{ |
| 2476 |
/* We need to use the graphics state memory, in case we are running under Ghostscript. */ |
| 2477 |
*pgs = gs_gstate_copy(ctx->DefaultQState, ctx->pgs->memory); |
| 2478 |
if (*pgs == NULL) |
| 2479 |
return_error(gs_error_VMerror); |
| 2480 |
return 0; |
| 2481 |
} |
| 2482 |
|
| 2483 |
int pdfi_restore_DefaultQState(pdf_context *ctx, gs_gstate **pgs) |
| 2484 |
{ |
| 2485 |
int code; |
| 2486 |
|
| 2487 |
code = pdfi_set_DefaultQState(ctx, *pgs); |
| 2488 |
gs_gstate_free(*pgs); |
| 2489 |
*pgs = NULL; |
| 2490 |
return code; |
| 2491 |
} |