zgstate.c 16.1 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   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
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15
*/

16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* Graphics state operators */
#include "math_.h"
#include "ghost.h"
#include "oper.h"
#include "ialloc.h"
#include "icremap.h"
#include "idict.h"
#include "istruct.h"
#include "igstate.h"
#include "gsmatrix.h"
#include "store.h"
#include "gscspace.h"
#include "iname.h"

/* Structure descriptors */
private_st_int_gstate();
private_st_int_remap_color_info();

/* ------ Utilities ------ */

static int
38
zset_real(i_ctx_t *i_ctx_p, int (*set_proc)(gs_gstate *, double))
39 40 41 42 43 44
{
    os_ptr op = osp;
    double param;
    int code = real_param(op, &param);

    if (code < 0)
45
        return_op_typecheck(op);
46
    code = set_proc(igs, param);
47
    if (code == 0)
48
        pop(1);
49 50 51
    return code;
}

52 53 54 55 56 57 58 59 60 61
static int
zcurrent_real(i_ctx_t *i_ctx_p, double (*current_proc)(const gs_gstate *))
{
    os_ptr op = osp;

    push(1);
    make_real(op, current_proc(igs));
    return 0;
}

62
static int
63
zset_bool(i_ctx_t *i_ctx_p, void (*set_proc)(gs_gstate *, bool))
64 65 66 67 68 69 70 71 72 73
{
    os_ptr op = osp;

    check_type(*op, t_boolean);
    set_proc(igs, op->value.boolval);
    pop(1);
    return 0;
}

static int
74
zcurrent_bool(i_ctx_t *i_ctx_p, bool (*current_proc)(const gs_gstate *))
75 76 77 78 79 80 81 82 83
{
    os_ptr op = osp;

    push(1);
    make_bool(op, current_proc(igs));
    return 0;
}

static int
84
zset_uint(i_ctx_t *i_ctx_p, void (*set_proc)(gs_gstate *, uint))
85 86 87 88 89 90 91 92 93 94
{
    os_ptr op = osp;

    check_type(*op, t_integer);
    set_proc(igs, op->value.intval);
    pop(1);
    return 0;
}

static int
95
zcurrent_uint(i_ctx_t *i_ctx_p, uint (*current_proc)(const gs_gstate *))
96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
    os_ptr op = osp;

    push(1);
    make_int(op, current_proc(igs));
    return 0;
}

/* ------ Operations on the entire graphics state ------ */

/* "Client" procedures */
static void *gs_istate_alloc(gs_memory_t * mem);
static int gs_istate_copy(void *to, const void *from);
static void gs_istate_free(void *old, gs_memory_t * mem);
110
static const gs_gstate_client_procs istate_procs = {
111 112 113 114 115 116 117
    gs_istate_alloc,
    gs_istate_copy,
    gs_istate_free,
    0,			/* copy_for */
};

/* Initialize the graphics stack. */
118
gs_gstate *
119 120 121 122 123 124 125
int_gstate_alloc(const gs_dual_memory_t * dmem)
{
    int_gstate *iigs;
    ref proc0;
    int_remap_color_info_t *prci;
    gs_ref_memory_t *lmem = dmem->space_local;
    gs_ref_memory_t *gmem = dmem->space_global;
126
    gs_gstate *pgs = gs_gstate_alloc((gs_memory_t *)lmem);
127 128

    iigs = gs_alloc_struct((gs_memory_t *)lmem, int_gstate, &st_int_gstate,
129
                           "int_gstate_alloc(int_gstate)");
130 131
    if (iigs == NULL)
        return NULL;
132 133
    int_gstate_map_refs(iigs, make_null);
    make_empty_array(&iigs->dash_pattern_array, a_all);
134 135 136
    if (gs_alloc_ref_array(lmem, &proc0, a_readonly + a_executable, 2,
                       "int_gstate_alloc(proc0)") < 0)
        return NULL;
137 138 139 140 141 142 143 144 145 146 147
    make_oper(proc0.value.refs, 0, zpop);
    make_real(proc0.value.refs + 1, 0.0);
    iigs->black_generation = proc0;
    iigs->undercolor_removal = proc0;
    make_false(&iigs->use_cie_color);
    /*
     * Even though the gstate itself is allocated in local VM, the
     * container for the color remapping procedure must be allocated in
     * global VM so that the gstate can be copied into global VM.
     */
    prci = gs_alloc_struct((gs_memory_t *)gmem, int_remap_color_info_t,
148 149
                           &st_int_remap_color_info,
                           "int_gstate_alloc(remap color info)");
150 151
    if (prci == NULL)
        return NULL;
152 153
    make_struct(&iigs->remap_color_info, imemory_space(gmem), prci);
    clear_pagedevice(iigs);
154
    gs_gstate_set_client(pgs, iigs, &istate_procs, true);
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
    /* PostScript code wants limit clamping enabled. */
    gs_setlimitclamp(pgs, true);
    /*
     * gsave and grestore only work properly
     * if there are always at least 2 entries on the stack.
     * We count on the PostScript initialization code to do a gsave.
     */
    return pgs;
}

/* - gsave - */
int
zgsave(i_ctx_t *i_ctx_p)
{
    return gs_gsave(igs);
}

/* - grestore - */
int
zgrestore(i_ctx_t *i_ctx_p)
{
    return gs_grestore(igs);
}

/* - grestoreall - */
int
zgrestoreall(i_ctx_t *i_ctx_p)
{
    return gs_grestoreall(igs);
}

/* - initgraphics - */
static int
zinitgraphics(i_ctx_t *i_ctx_p)
{
    /*
191 192 193 194
     * Although gs_initgraphics resets the color space to DeviceGray, it does
     * not modify the 'interpreter' gstate, which stores a copy of the PostScript
     * object used to set the colour space. We could do this here, with effort,
     * but instead we choose t do it in gs_cspace.ps and handle it all in PostScript.
195 196 197 198 199 200 201 202 203 204 205 206
     */
     make_empty_array(&istate->dash_pattern_array, a_all);
     return gs_initgraphics(igs);
}

/* ------ Operations on graphics state elements ------ */

/* <num> setlinewidth - */
static int
zsetlinewidth(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
207 208 209 210 211
        /*
         * The Red Book doesn't say anything about this, but Adobe
         * interpreters return (or perhaps store) the absolute value
         * of the width.
         */
212 213 214 215
    double width;
    int code = real_param(op, &width);

    if (code < 0)
216
        return_op_typecheck(op);
217 218
    code = gs_setlinewidth(igs, fabs(width));
    if (code >= 0)
219
        pop(1);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    return code;
}

/* - currentlinewidth <num> */
static int
zcurrentlinewidth(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_real(op, gs_currentlinewidth(igs));
    return 0;
}

/* <cap_int> .setlinecap - */
static int
zsetlinecap(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    int param;
    int code = int_param(op, max_int, &param);

    if (code < 0 || (code = gs_setlinecap(igs, (gs_line_cap) param)) < 0)
243
        return code;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    pop(1);
    return 0;
}

/* - currentlinecap <cap_int> */
static int
zcurrentlinecap(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_int(op, (int)gs_currentlinecap(igs));
    return 0;
}

/* <join_int> .setlinejoin - */
static int
zsetlinejoin(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    int param;
    int code = int_param(op, max_int, &param);

    if (code < 0 || (code = gs_setlinejoin(igs, (gs_line_join) param)) < 0)
268
        return code;
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
    pop(1);
    return 0;
}

/* - currentlinejoin <join_int> */
static int
zcurrentlinejoin(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_int(op, (int)gs_currentlinejoin(igs));
    return 0;
}

/* <num> setmiterlimit - */
static int
zsetmiterlimit(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_setmiterlimit);
}

/* - currentmiterlimit <num> */
static int
zcurrentmiterlimit(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_real(op, gs_currentmiterlimit(igs));
    return 0;
}

/* <array> <offset> setdash - */
static int
zsetdash(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    double offset;
    int code = real_param(op, &offset);
    uint i, n;
    gs_memory_t *mem = imemory;
    float *pattern;

    if (code < 0)
315
        return_op_typecheck(op);
316
    if (!r_is_array(op1))
317
        return_op_typecheck(op1);
318 319 320 321 322 323
    /* Adobe interpreters apparently don't check the array for */
    /* read access, so we won't either. */
    /*check_read(*op1); */
    /* Unpack the dash pattern and check it */
    n = r_size(op1);
    pattern =
324
        (float *)gs_alloc_byte_array(mem, n, sizeof(float), "setdash");
325 326

    if (pattern == 0)
327
        return_error(gs_error_VMerror);
328
    for (i = 0, code = 0; i < n && code >= 0; ++i) {
329
        ref element;
330

331 332
        array_get(mem, op1, (long)i, &element);
        code = float_param(&element, &pattern[i]);
333 334
    }
    if (code >= 0)
335
        code = gs_setdash(igs, pattern, n, offset);
336 337
    gs_free_object(mem, pattern, "setdash");	/* gs_setdash copies this */
    if (code < 0)
338
        return code;
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
    ref_assign(&istate->dash_pattern_array, op1);
    pop(2);
    return code;
}

/* - currentdash <array> <offset> */
static int
zcurrentdash(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(2);
    ref_assign(op - 1, &istate->dash_pattern_array);
    make_real(op, gs_currentdash_offset(igs));
    return 0;
}

/* <num> setflat - */
static int
zsetflat(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_setflat);
}

/* - currentflat <num> */
static int
zcurrentflat(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_real(op, gs_currentflat(igs));
    return 0;
}

/* ------ Extensions ------ */

/* <bool> .setaccuratecurves - */
static int
zsetaccuratecurves(i_ctx_t *i_ctx_p)
{
    return zset_bool(i_ctx_p, gs_setaccuratecurves);
}

/* - .currentaccuratecurves <bool> */
static int
zcurrentaccuratecurves(i_ctx_t *i_ctx_p)
{
    return zcurrent_bool(i_ctx_p, gs_currentaccuratecurves);
}

/* <join_int|-1> .setcurvejoin - */
static int
zsetcurvejoin(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    int code;

    check_type(*op, t_integer);
    if (op->value.intval < -1 || op->value.intval > max_int)
399
        return_error(gs_error_rangecheck);
400 401
    code = gs_setcurvejoin(igs, (int)op->value.intval);
    if (code < 0)
402
        return code;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    pop(1);
    return 0;
}

/* - .currentcurvejoin <join_int|-1> */
static int
zcurrentcurvejoin(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_int(op, gs_currentcurvejoin(igs));
    return 0;
}

/* <adjust.x> <adjust.y> .setfilladjust2 - */
static int
zsetfilladjust2(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    double adjust[2];
    int code = num_params(op, 2, adjust);

    if (code < 0)
427
        return code;
428 429
    code = gs_setfilladjust(igs, adjust[0], adjust[1]);
    if (code < 0)
430
        return code;
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
    pop(2);
    return 0;
}

/* - .currentfilladjust2 <adjust.x> <adjust.y> */
static int
zcurrentfilladjust2(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_point adjust;

    push(2);
    gs_currentfilladjust(igs, &adjust);
    make_real(op - 1, adjust.x);
    make_real(op, adjust.y);
    return 0;
}

/* <bool> .setdashadapt - */
static int
zsetdashadapt(i_ctx_t *i_ctx_p)
{
    return zset_bool(i_ctx_p, gs_setdashadapt);
}

/* - .currentdashadapt <bool> */
static int
zcurrentdashadapt(i_ctx_t *i_ctx_p)
{
    return zcurrent_bool(i_ctx_p, gs_currentdashadapt);
}

/* <num> <bool> .setdotlength - */
static int
zsetdotlength(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    double length;
    int code = real_param(op - 1, &length);

    if (code < 0)
472
        return code;
473 474 475
    check_type(*op, t_boolean);
    code = gs_setdotlength(igs, length, op->value.boolval);
    if (code < 0)
476
        return code;
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
    pop(2);
    return 0;
}

/* - .currentdotlength <num> <bool> */
static int
zcurrentdotlength(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(2);
    make_real(op - 1, gs_currentdotlength(igs));
    make_bool(op, gs_currentdotlength_absolute(igs));
    return 0;
}

/* - .setdotorientation - */
static int
zsetdotorientation(i_ctx_t *i_ctx_p)
{
    return gs_setdotorientation(igs);
}

/* - .dotorientation - */
static int
zdotorientation(i_ctx_t *i_ctx_p)
{
    return gs_dotorientation(igs);
}

/* <bool> .setlimitclamp - */
static int
zsetlimitclamp(i_ctx_t *i_ctx_p)
{
    return zset_bool(i_ctx_p, gs_setlimitclamp);
}

/* - .currentlimitclamp <bool> */
static int
zcurrentlimitclamp(i_ctx_t *i_ctx_p)
{
    return zcurrent_bool(i_ctx_p, gs_currentlimitclamp);
}

/* <int> .settextrenderingmode - */
static int
zsettextrenderingmode(i_ctx_t *i_ctx_p)
{
    return zset_uint(i_ctx_p, gs_settextrenderingmode);
}

/* - .currenttextrenderingmode <int> */
static int
zcurrenttextrenderingmode(i_ctx_t *i_ctx_p)
{
    return zcurrent_uint(i_ctx_p, gs_currenttextrenderingmode);
}
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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static int
zsettextspacing(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_settextspacing);
}
static int
zcurrenttextspacing(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currenttextspacing);
}
static int
zsettextleading(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_settextleading);
}
static int
zcurrenttextleading(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currenttextleading);
}
static int
zsettextrise(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_settextrise);
}
static int
zcurrenttextrise(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currenttextrise);
}
static int
zsetwordspacing(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_setwordspacing);
}
static int
zcurrentwordspacing(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currentwordspacing);
}

static int
zsettexthscaling(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_settexthscaling);
}
static int
zcurrenttexthscaling(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currenttexthscaling);
}
585

586 587 588 589 590 591 592 593 594 595 596 597
static int
zsetPDFfontsize(i_ctx_t *i_ctx_p)
{
    return zset_real(i_ctx_p, gs_setPDFfontsize);
}
static int
zcurrentPDFfontsize(i_ctx_t *i_ctx_p)
{
    return zcurrent_real(i_ctx_p, gs_currentPDFfontsize);
}


598
/* <bool> .sethpglpathmode - */
599 600 601
static int
zsethpglpathmode(i_ctx_t *i_ctx_p)
{
602
    return zset_bool(i_ctx_p, gs_sethpglpathmode);
603 604 605 606 607 608
}

/* - .currenthpglpathmode <int> */
static int
zcurrenthpglpathmode(i_ctx_t *i_ctx_p)
{
609
    return zcurrent_bool(i_ctx_p, gs_currenthpglpathmode);
610 611
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
/* ------ Initialization procedure ------ */

/* We need to split the table because of the 16-element limit. */
const op_def zgstate1_op_defs[] = {
    {"0.currentaccuratecurves", zcurrentaccuratecurves},
    {"0.currentcurvejoin", zcurrentcurvejoin},
    {"0currentdash", zcurrentdash},
    {"0.currentdashadapt", zcurrentdashadapt},
    {"0.currentdotlength", zcurrentdotlength},
    {"0.currentfilladjust2", zcurrentfilladjust2},
    {"0currentflat", zcurrentflat},
    {"0.currentlimitclamp", zcurrentlimitclamp},
    {"0currentlinecap", zcurrentlinecap},
    {"0currentlinejoin", zcurrentlinejoin},
    {"0currentlinewidth", zcurrentlinewidth},
    {"0currentmiterlimit", zcurrentmiterlimit},
    {"0.dotorientation", zdotorientation},
    {"0grestore", zgrestore},
    {"0grestoreall", zgrestoreall},
    op_def_end(0)
};
const op_def zgstate2_op_defs[] = {
    {"0gsave", zgsave},
    {"0initgraphics", zinitgraphics},
    {"1.setaccuratecurves", zsetaccuratecurves},
    {"1.setcurvejoin", zsetcurvejoin},
    {"2setdash", zsetdash},
    {"1.setdashadapt", zsetdashadapt},
    {"2.setdotlength", zsetdotlength},
    {"0.setdotorientation", zsetdotorientation},
    {"2.setfilladjust2", zsetfilladjust2},
    {"1.setlimitclamp", zsetlimitclamp},
    {"1setflat", zsetflat},
    {"1.setlinecap", zsetlinecap},
    {"1.setlinejoin", zsetlinejoin},
    {"1setlinewidth", zsetlinewidth},
    {"1setmiterlimit", zsetmiterlimit},
    op_def_end(0)
};
const op_def zgstate3_op_defs[] = {
652
    {"1.settextrenderingmode", zsettextrenderingmode},
653
    {"0.currenttextrenderingmode", zcurrenttextrenderingmode},
654 655 656 657 658 659 660 661 662 663
    {"1.settextspacing", zsettextspacing},
    {"0.currenttextspacing", zcurrenttextspacing},
    {"1.settextleading", zsettextleading},
    {"0.currenttextleading", zcurrenttextleading},
    {"1.settextrise", zsettextrise},
    {"0.currenttextrise", zcurrenttextrise},
    {"1.setwordspacing", zsetwordspacing},
    {"0.currentwordspacing", zcurrentwordspacing},
    {"1.settexthscaling", zsettexthscaling},
    {"0.currenttexthscaling", zcurrenttexthscaling},
664 665
    {"0.sethpglpathmode", zsethpglpathmode},
    {"0.currenthpglpathmode", zcurrenthpglpathmode},
666 667
    op_def_end(0)
};
668 669 670 671 672
const op_def zgstate4_op_defs[] = {
    {"1.setPDFfontsize", zsetPDFfontsize},
    {"0.currentPDFfontsize", zcurrentPDFfontsize},
    op_def_end(0)
};
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

/* ------ Internal routines ------ */

/* Allocate the interpreter's part of a graphics state. */
static void *
gs_istate_alloc(gs_memory_t * mem)
{
    return gs_alloc_struct(mem, int_gstate, &st_int_gstate, "int_gsave");
}

/* Copy the interpreter's part of a graphics state. */
static int
gs_istate_copy(void *to, const void *from)
{
    *(int_gstate *) to = *(const int_gstate *)from;
    return 0;
}

/* Free the interpreter's part of a graphics state. */
static void
gs_istate_free(void *old, gs_memory_t * mem)
{
    gs_free_object(mem, old, "int_grestore");
}