1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/* Copyright (C) 2001-2012 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* PostScript language support for FunctionType 4 (PS Calculator) Functions */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "opextern.h"
#include "gsfunc.h"
#include "gsfunc4.h"
#include "gsutil.h"
#include "idict.h"
#include "ifunc.h"
#include "iname.h"
#include "dstack.h"
#include "ialloc.h"
#include "gzstate.h" /* these are needed to check device parameters */
#include "gsparam.h" /* these are needed to check device parameters */
#include "zfunc.h"
#include "zcolor.h"
/*
* FunctionType 4 functions are not defined in the PostScript language. We
* provide support for them because they are needed for PDF 1.3. In
* addition to the standard FunctionType, Domain, and Range keys, they have
* a Function key whose value is a procedure in a restricted subset of the
* PostScript language. Specifically, the procedure must (recursively)
* contain only integer, real, Boolean, and procedure constants (only as
* literal operands of if and and ifelse), and operators chosen from the set
* given below. Note that names other than true and false are not allowed:
* the procedure must be 'bound'.
*
* The following list is taken directly from the PDF 1.3 documentation.
*/
#define XOP(zfn) int zfn(i_ctx_t *)
XOP(zabs); XOP(zand); XOP(zatan); XOP(zbitshift);
XOP(zceiling); XOP(zcos); XOP(zcvi); XOP(zcvr);
XOP(zdiv); XOP(zexp); XOP(zfloor); XOP(zidiv);
XOP(zln); XOP(zlog); XOP(zmod); XOP(zmul);
XOP(zneg); XOP(znot); XOP(zor); XOP(zround);
XOP(zsin); XOP(zsqrt); XOP(ztruncate); XOP(zxor);
XOP(zeq); XOP(zge); XOP(zgt); XOP(zle); XOP(zlt); XOP(zne);
XOP(z2copy);
#undef XOP
typedef struct calc_op_s {
op_proc_t proc;
gs_PtCr_opcode_t opcode;
} calc_op_t;
static const calc_op_t calc_ops[] = {
/* Arithmetic operators */
{zabs, PtCr_abs},
{zadd, PtCr_add},
{zand, PtCr_and},
{zatan, PtCr_atan},
{zbitshift, PtCr_bitshift},
{zceiling, PtCr_ceiling},
{zcos, PtCr_cos},
{zcvi, PtCr_cvi},
{zcvr, PtCr_cvr},
{zdiv, PtCr_div},
{zexp, PtCr_exp},
{zfloor, PtCr_floor},
{zidiv, PtCr_idiv},
{zln, PtCr_ln},
{zlog, PtCr_log},
{zmod, PtCr_mod},
{zmul, PtCr_mul},
{zneg, PtCr_neg},
{znot, PtCr_not},
{zor, PtCr_or},
{zround, PtCr_round},
{zsin, PtCr_sin},
{zsqrt, PtCr_sqrt},
{zsub, PtCr_sub},
{ztruncate, PtCr_truncate},
{zxor, PtCr_xor},
/* Comparison operators */
{zeq, PtCr_eq},
{zge, PtCr_ge},
{zgt, PtCr_gt},
{zle, PtCr_le},
{zlt, PtCr_lt},
{zne, PtCr_ne},
/* Stack operators */
{zcopy, PtCr_copy},
{z2copy, PtCr_copy},
{zdup, PtCr_dup},
{zexch, PtCr_exch},
{zindex, PtCr_index},
{zpop, PtCr_pop},
{zroll, PtCr_roll}
/* Special operators */
/*{zif, PtCr_if},*/
/*{zifelse, PtCr_ifelse},*/
/*{ztrue, PtCr_true},*/
/*{zfalse, PtCr_false}*/
};
/* Fix up an if or ifelse forward reference. */
static void
psc_fixup(byte *p, byte *to)
{
int skip = to - (p + 3);
p[1] = (byte)(skip >> 8);
p[2] = (byte)skip;
}
/* Check whether the ref is a given operator or resolves to it */
static bool
resolves_to_oper(i_ctx_t *i_ctx_p, const ref *pref, const op_proc_t proc)
{
if (!r_has_attr(pref, a_executable))
return false;
if (r_btype(pref) == t_operator) {
return pref->value.opproc == proc;
} else if (r_btype(pref) == t_name) {
ref * val;
if (dict_find(systemdict, pref, &val) <= 0)
return false;
if (r_btype(val) != t_operator)
return false;
if (!r_has_attr(val, a_executable))
return false;
return val->value.opproc == proc;
}
else
return false;
}
/* Store an int in the buffer */
static int
put_int(byte **p, int n) {
if (n == (byte)n) {
if (*p) {
(*p)[0] = PtCr_byte;
(*p)[1] = (byte)n;
*p += 2;
}
return 2;
} else {
if (*p) {
**p = PtCr_int;
memcpy(*p + 1, &n, sizeof(int));
*p += sizeof(int) + 1;
}
return (sizeof(int) + 1);
}
}
/* Store a float in the buffer */
static int
put_float(byte **p, float n) {
if (*p) {
**p = PtCr_float;
memcpy(*p + 1, &n, sizeof(float));
*p += sizeof(float) + 1;
}
return (sizeof(float) + 1);
}
/* Store an op code in the buffer */
static int
put_op(byte **p, byte op) {
if (*p)
*(*p)++ = op;
return 1;
}
/*
* Check a calculator function for validity, optionally storing its encoded
* representation and add the size of the encoded representation to *psize.
* Note that we arbitrarily limit the depth of procedure nesting. pref is
* known to be a procedure.
*/
static int
check_psc_function(i_ctx_t *i_ctx_p, const ref *pref, int depth, byte *ops, int *psize)
{
int code;
uint i, j;
uint size = r_size(pref);
byte *p;
if (size == 2 && depth == 0) {
/* Bug 690986. Recognize and replace Corel tint transform function.
{ tint_params CorelTintTransformFunction }
Where tint_params resolves to an arrey like:
[ 1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
0.2 0.81 0.76 0.61
]
And CorelTintTransformFunction is:
{ /colorantSpecArray exch def
/nColorants colorantSpecArray length 4 idiv def
/inColor nColorants 1 add 1 roll nColorants array astore def
/outColor 4 array def
0 1 3 {
/nOutInk exch def
1
0 1 nColorants 1 sub {
dup inColor exch get
exch 4 mul nOutInk add colorantSpecArray exch get mul
1 exch sub mul
} for
1 exch sub
outColor nOutInk 3 -1 roll put
} for
outColor aload pop
}
*/
ref r_tp, r_cttf; /* original references */
ref n_tp, n_cttf; /* names */
ref *v_tp, *v_cttf; /* values */
int sz;
p = ops;
sz = *psize;
if(array_get(imemory, pref, 0, &r_tp) < 0)
goto idiom_failed;
if (array_get(imemory, pref, 1, &r_cttf) < 0)
goto idiom_failed;
if (r_has_type(&r_tp, t_name) && r_has_type(&r_cttf, t_name)) {
if ((code = name_enter_string(imemory, "tint_params", &n_tp)) < 0)
return code;
if (r_tp.value.pname == n_tp.value.pname) {
if ((code = name_enter_string(imemory, "CorelTintTransformFunction", &n_cttf)) < 0)
return code;
if (r_cttf.value.pname == n_cttf.value.pname) {
v_tp = dict_find_name(&n_tp);
v_cttf = dict_find_name(&n_cttf);
if (v_tp && v_cttf && r_is_array(v_tp) && r_is_array(v_cttf)) {
uint n_elem = r_size(v_tp);
if ((n_elem & 3) == 0 && r_size(v_cttf) == 31) {
/* Enough testing, idiom recognition tests less. */
uint n_col = n_elem/4;
for (i = 0; i < 4; i ++) {
ref v;
float fv;
bool first = true;
for (j = 0; j < n_col; j++) {
if (array_get(imemory, v_tp, j*4 + i, &v) < 0)
goto idiom_failed;
if (r_type(&v) == t_integer)
fv = (float)v.value.intval;
else if (r_type(&v) == t_real)
fv = v.value.realval;
else
goto idiom_failed;
if (fv != 0.) {
if (first)
sz += put_int(&p, 1);
sz += put_int(&p, 1);
sz += put_int(&p, n_col + 1 - j + i + !first);
sz += put_op(&p, PtCr_index);
if (fv != 1.) {
sz += put_float(&p, fv);
sz += put_op(&p, PtCr_mul);
}
sz += put_op(&p, PtCr_sub);
if (first)
first = false;
else
sz += put_op(&p, PtCr_mul);
}
}
if (first)
sz += put_int(&p, 0);
else
sz += put_op(&p, PtCr_sub);
}
/* n_col+4 4 roll pop ... pop */
sz += put_int(&p, n_col + 4);
sz += put_int(&p, 4);
sz += put_op(&p, PtCr_roll);
for (j = 0; j < n_col; j++)
sz += put_op(&p, PtCr_pop);
*psize = sz;
return 0;
}
}
}
}
}
}
idiom_failed:;
for (i = 0; i < size; ++i) {
byte no_ops[1 + max(sizeof(int), sizeof(float))];
ref elt, elt2, elt3;
ref * delp;
p = (ops ? ops + *psize : no_ops);
array_get(imemory, pref, i, &elt);
switch (r_btype(&elt)) {
case t_integer:
*psize += put_int(&p, elt.value.intval);
break;
case t_real:
*psize += put_float(&p, elt.value.realval);
break;
case t_boolean:
*p = (elt.value.boolval ? PtCr_true : PtCr_false);
++*psize;
break;
case t_name:
if (!r_has_attr(&elt, a_executable))
return_error(e_rangecheck);
name_string_ref(imemory, &elt, &elt);
if (!bytes_compare(elt.value.bytes, r_size(&elt),
(const byte *)"true", 4)) {
*p = PtCr_true;
++*psize;
break;
}
if (!bytes_compare(elt.value.bytes, r_size(&elt),
(const byte *)"false", 5)) {
*p = PtCr_false;
++*psize;
break;
}
/* Check if the name is a valid operator in systemdict */
if (dict_find(systemdict, &elt, &delp) <= 0)
return_error(e_undefined);
if (r_btype(delp) != t_operator)
return_error(e_typecheck);
if (!r_has_attr(delp, a_executable))
return_error(e_rangecheck);
elt = *delp;
/* Fall into the operator case */
case t_operator: {
int j;
for (j = 0; j < countof(calc_ops); ++j)
if (elt.value.opproc == calc_ops[j].proc) {
*p = calc_ops[j].opcode;
++*psize;
goto next;
}
return_error(e_rangecheck);
}
default: {
if (!r_is_proc(&elt))
return_error(e_typecheck);
if (depth == MAX_PSC_FUNCTION_NESTING)
return_error(e_limitcheck);
if ((code = array_get(imemory, pref, ++i, &elt2)) < 0)
return code;
*psize += 3;
code = check_psc_function(i_ctx_p, &elt, depth + 1, ops, psize);
if (code < 0)
return code;
/* Check for { proc } repeat | {proc} if | {proc1} {proc2} ifelse */
if (resolves_to_oper(i_ctx_p, &elt2, zrepeat)) {
gs_c_param_list list;
int AllowRepeat = 1;
/* Check if the device allows the use of repeat in functions */
/* We can't handle 'repeat' with pdfwrite since it emits FunctionType 4 */
gs_c_param_list_write(&list, i_ctx_p->pgs->device->memory);
code = gs_getdeviceparams(i_ctx_p->pgs->device, (gs_param_list *)&list);
if (code < 0)
return code;
gs_c_param_list_read(&list);
code = param_read_bool((gs_param_list *)&list,
"AllowPSRepeatFunctions",
&AllowRepeat);
if (code < 0)
return code;
gs_c_param_list_release(&list);
if (!AllowRepeat)
return_error(e_rangecheck);
if (ops) {
*p = PtCr_repeat;
psc_fixup(p, ops + *psize);
p = ops + *psize;
*p++ = PtCr_repeat_end;
}
*psize += 1; /* extra room for repeat_end */
} else if (resolves_to_oper(i_ctx_p, &elt2, zif)) {
if (ops) {
*p = PtCr_if;
psc_fixup(p, ops + *psize);
}
} else if (!r_is_proc(&elt2))
return_error(e_rangecheck);
else if ((code = array_get(imemory, pref, ++i, &elt3)) < 0)
return code;
else if (resolves_to_oper(i_ctx_p, &elt3, zifelse)) {
if (ops) {
*p = PtCr_if;
psc_fixup(p, ops + *psize + 3);
p = ops + *psize;
*p = PtCr_else;
}
*psize += 3;
code = check_psc_function(i_ctx_p, &elt2, depth + 1, ops, psize);
if (code < 0)
return code;
if (ops)
psc_fixup(p, ops + *psize);
} else
return_error(e_rangecheck);
} /* end 'default' */
}
next:
DO_NOTHING;
}
return 0;
}
#undef MAX_PSC_FUNCTION_NESTING
/* Check prototype */
build_function_proc(gs_build_function_4);
/* Finish building a FunctionType 4 (PostScript Calculator) function. */
int
gs_build_function_4(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
int depth, gs_function_t ** ppfn, gs_memory_t *mem)
{
gs_function_PtCr_params_t params;
ref *proc;
int code;
byte *ops;
int size;
*(gs_function_params_t *)¶ms = *mnDR;
params.ops.data = 0; /* in case of failure */
params.ops.size = 0; /* ditto */
if (dict_find_string(op, "Function", &proc) <= 0) {
code = gs_note_error(e_rangecheck);
goto fail;
}
if (!r_is_proc(proc)) {
code = gs_note_error(e_typecheck);
goto fail;
}
size = 0;
code = check_psc_function(i_ctx_p, proc, 0, NULL, &size);
if (code < 0)
goto fail;
ops = gs_alloc_string(mem, size + 1, "gs_build_function_4(ops)");
if (ops == 0) {
code = gs_note_error(e_VMerror);
goto fail;
}
size = 0;
check_psc_function(i_ctx_p, proc, 0, ops, &size); /* can't fail */
ops[size] = PtCr_return;
params.ops.data = ops;
params.ops.size = size + 1;
code = gs_function_PtCr_init(ppfn, ¶ms, mem);
if (code >= 0)
return 0;
/* free_params will free the ops string */
fail:
gs_function_PtCr_free_params(¶ms, mem);
return (code < 0 ? code : gs_note_error(e_rangecheck));
}
int make_type4_function(i_ctx_t * i_ctx_p, ref *arr, ref *pproc, gs_function_t **func)
{
int code, size, num_components, CIESubst;
byte *ops;
gs_function_PtCr_params_t params;
float *ptr;
ref alternatespace, *palternatespace = &alternatespace;
PS_colour_space_t *space, *altspace;
code = get_space_object(i_ctx_p, arr, &space);
if (code < 0)
return code;
if (!space->alternateproc)
return e_typecheck;
code = space->alternateproc(i_ctx_p, arr, &palternatespace, &CIESubst);
if (code < 0)
return code;
code = get_space_object(i_ctx_p, palternatespace, &altspace);
if (code < 0)
return code;
code = space->numcomponents(i_ctx_p, arr, &num_components);
if (code < 0)
return code;
ptr = (float *)gs_alloc_byte_array(imemory, num_components * 2, sizeof(float), "make_type4_function(Domain)");
if (!ptr)
return e_VMerror;
code = space->domain(i_ctx_p, arr, ptr);
if (code < 0) {
gs_free_const_object(imemory, ptr, "make_type4_function(Domain)");
return code;
}
params.Domain = ptr;
params.m = num_components;
code = altspace->numcomponents(i_ctx_p, &alternatespace, &num_components);
if (code < 0) {
gs_free_const_object(imemory, params.Domain, "make_type4_function(Domain)");
return code;
}
ptr = (float *)gs_alloc_byte_array(imemory, num_components * 2, sizeof(float), "make_type4_function(Range)");
if (!ptr) {
gs_free_const_object(imemory, params.Domain, "make_type4_function(Domain)");
return e_VMerror;
}
code = altspace->range(i_ctx_p, &alternatespace, ptr);
if (code < 0) {
gs_free_const_object(imemory, ptr, "make_type4_function(Domain)");
gs_free_const_object(imemory, params.Domain, "make_type4_function(Range)");
return code;
}
params.Range = ptr;
params.n = num_components;
params.ops.data = 0; /* in case of failure, see gs_function_PtCr_free_params */
params.ops.size = 0; /* ditto */
size = 0;
code = check_psc_function(i_ctx_p, (const ref *)pproc, 0, NULL, &size);
if (code < 0) {
gs_function_PtCr_free_params(¶ms, imemory);
return code;
}
ops = gs_alloc_string(imemory, size + 1, "make_type4_function(ops)");
size = 0;
check_psc_function(i_ctx_p, (const ref *)pproc, 0, ops, &size); /* can't fail */
ops[size] = PtCr_return;
params.ops.data = ops;
params.ops.size = size + 1;
code = gs_function_PtCr_init(func, ¶ms, imemory);
if (code < 0)
gs_function_PtCr_free_params(¶ms, imemory);
return code;
}
|