~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to src/gsstate.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2001-2006 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, modified
8
 
   or distributed except as expressly authorized under the terms of that
9
 
   license.  Refer to licensing information at http://www.artifex.com/
10
 
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11
 
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12
 
*/
13
 
 
14
 
/* $Id: gsstate.c 8407 2007-11-27 18:11:10Z leonardo $ */
15
 
/* Miscellaneous graphics state operators for Ghostscript library */
16
 
#include "gx.h"
17
 
#include "memory_.h"
18
 
#include "gserrors.h"
19
 
#include "gsstruct.h"
20
 
#include "gsutil.h"             /* for gs_next_ids */
21
 
#include "gzstate.h"
22
 
#include "gxcspace.h"           /* here for gscolor2.h */
23
 
#include "gsalpha.h"
24
 
#include "gscolor2.h"
25
 
#include "gscoord.h"            /* for gs_initmatrix */
26
 
#include "gscie.h"
27
 
#include "gxclipsr.h"
28
 
#include "gxcmap.h"
29
 
#include "gxdevice.h"
30
 
#include "gxpcache.h"
31
 
#include "gzht.h"
32
 
#include "gzline.h"
33
 
#include "gspath.h"
34
 
#include "gzpath.h"
35
 
#include "gzcpath.h"
36
 
#include "gsovrc.h"
37
 
#include "gxcolor2.h"
38
 
#include "gxpcolor.h"
39
 
 
40
 
/* Forward references */
41
 
static gs_state *gstate_alloc(gs_memory_t *, client_name_t,
42
 
                               const gs_state *);
43
 
static gs_state *gstate_clone(gs_state *, gs_memory_t *, client_name_t,
44
 
                               gs_state_copy_reason_t);
45
 
static void gstate_free_contents(gs_state *);
46
 
static int gstate_copy(gs_state *, const gs_state *,
47
 
                        gs_state_copy_reason_t, client_name_t);
48
 
static void clip_stack_rc_adjust(gx_clip_stack_t *cs, int delta, client_name_t cname);
49
 
 
50
 
/*
51
 
 * Graphics state storage management is complicated.  There are many
52
 
 * different classes of storage associated with a graphics state:
53
 
 *
54
 
 * (1) The gstate object itself.  This includes some objects physically
55
 
 *      embedded within the gstate object, but because of garbage collection
56
 
 *      requirements, there are no embedded objects that can be
57
 
 *      referenced by non-transient pointers.  We assume that the gstate
58
 
 *      stack "owns" its gstates and that we can free the top gstate when
59
 
 *      doing a restore.
60
 
 *
61
 
 * (2) Objects that are referenced directly by the gstate and whose lifetime
62
 
 *      is independent of the gstate.  These are garbage collected, not
63
 
 *      reference counted, so we don't need to do anything special with them
64
 
 *      when manipulating gstates.  Currently this includes:
65
 
 *              font, device
66
 
 *
67
 
 * (3) Objects that are referenced directly by the gstate, may be shared
68
 
 *      among gstates, and should disappear when no gstates reference them.
69
 
 *      These fall into two groups:
70
 
 *
71
 
 *   (3a) Objects that are logically connected to individual gstates.
72
 
 *      We use reference counting to manage these.  Currently these are:
73
 
 *              halftone, dev_ht, cie_render, black_generation,
74
 
 *              undercolor_removal, set_transfer.*, cie_joint_caches,
75
 
 *              clip_stack, {opacity,shape}.mask
76
 
 *      effective_transfer.* may point to some of the same objects as
77
 
 *      set_transfer.*, but don't contribute to the reference count.
78
 
 *      Similarly, dev_color may point to the dev_ht object.  For
79
 
 *      simplicity, we initialize all of these pointers to 0 and then
80
 
 *      allocate the object itself when needed.
81
 
 *
82
 
 *   (3b) Objects whose lifetimes are associated with something else.
83
 
 *      Currently these are:
84
 
 *              pattern_cache, which is associated with the entire
85
 
 *                stack, is allocated when first needed, and currently
86
 
 *                is never freed;
87
 
 *              view_clip, which is associated with the current
88
 
 *                save level (effectively, with the gstate sub-stack
89
 
 *                back to the save) and is managed specially;
90
 
 *              transparency_stack, which is associated with the entire
91
 
 *                stack but only stored in the topmost graphics state.
92
 
 *
93
 
 * (4) Objects that are referenced directly by exactly one gstate and that
94
 
 *      are not referenced (except transiently) from any other object.
95
 
 *      These fall into two groups:
96
 
 *
97
 
 *   (4b) Objects allocated individually, for the given reason:
98
 
 *              line_params.dash.pattern (variable-length),
99
 
 *              color_space, path, clip_path, effective_clip.path,
100
 
 *              ccolor, dev_color
101
 
 *                  (may be referenced from image enumerators or elsewhere)
102
 
 *
103
 
 *   (4b) The "client data" for a gstate.  For the interpreter, this is
104
 
 *      the refs associated with the gstate, such as the screen procedures.
105
 
 *      Client-supplied procedures manage client data.
106
 
 *
107
 
 * (5) Objects referenced indirectly from gstate objects of category (4),
108
 
 *      including objects that may also be referenced directly by the gstate.
109
 
 *      The individual routines that manipulate these are responsible
110
 
 *      for doing the right kind of reference counting or whatever.
111
 
 *      Currently:
112
 
 *              path, clip_path, and (if different from both clip_path
113
 
 *                and view_clip) effective_clip.path require
114
 
 *                gx_path_assign/free, which uses a reference count;
115
 
 *              color_space and ccolor require cs_adjust_color/cspace_count
116
 
 *                or cs_adjust_counts, which use a reference count;
117
 
 *              dev_color has no references to storage that it owns.
118
 
 *      We count on garbage collection or restore to deallocate
119
 
 *        sub-objects of halftone.
120
 
 *
121
 
 * Note that when after a gsave, the existing gstate references the related
122
 
 * objects that we allocate at the same time, and the newly allocated gstate
123
 
 * references the old related objects.  Similarly, during a grestore, we
124
 
 * free the related objects referenced by the current gstate, but after the
125
 
 * grestore, we free the saved gstate, not the current one.  However, when
126
 
 * we allocate gstates off-stack, the newly allocated gstate does reference
127
 
 * the newly allocated component objects.  Note also that setgstate /
128
 
 * currentgstate may produce gstates in which different allocators own
129
 
 * different sub-objects; this is OK, because restore guarantees that there
130
 
 * won't be any dangling pointers (as long as we don't allow pointers from
131
 
 * global gstates to local objects).
132
 
 */
133
 
 
134
 
/*
135
 
 * Define these elements of the graphics state that are allocated
136
 
 * individually for each state, except for line_params.dash.pattern.
137
 
 * Note that effective_clip_shared is not on the list.
138
 
 */
139
 
typedef struct gs_state_parts_s {
140
 
    gx_path *path;
141
 
    gx_clip_path *clip_path;
142
 
    gx_clip_path *effective_clip_path;
143
 
    gs_client_color *ccolor;
144
 
    gx_device_color *dev_color;
145
 
} gs_state_parts;
146
 
 
147
 
#define GSTATE_ASSIGN_PARTS(pto, pfrom)\
148
 
  ((pto)->path = (pfrom)->path, (pto)->clip_path = (pfrom)->clip_path,\
149
 
   (pto)->effective_clip_path = (pfrom)->effective_clip_path,\
150
 
   (pto)->ccolor = (pfrom)->ccolor, (pto)->dev_color = (pfrom)->dev_color)
151
 
 
152
 
/* GC descriptors */
153
 
extern_st(st_imager_state);
154
 
public_st_gs_state();
155
 
 
156
 
/* GC procedures for gs_state */
157
 
static ENUM_PTRS_WITH(gs_state_enum_ptrs, gs_state *gsvptr)
158
 
ENUM_PREFIX(st_imager_state, gs_state_num_ptrs + 2);
159
 
#define e1(i,elt) ENUM_PTR(i,gs_state,elt);
160
 
gs_state_do_ptrs(e1)
161
 
case gs_state_num_ptrs: /* handle device specially */
162
 
ENUM_RETURN(gx_device_enum_ptr(gsvptr->device));
163
 
case gs_state_num_ptrs + 1:     /* handle device filter stack specially */
164
 
ENUM_RETURN(gsvptr->dfilter_stack);
165
 
#undef e1
166
 
ENUM_PTRS_END
167
 
static RELOC_PTRS_WITH(gs_state_reloc_ptrs, gs_state *gsvptr)
168
 
{
169
 
    RELOC_PREFIX(st_imager_state);
170
 
    {
171
 
#define r1(i,elt) RELOC_PTR(gs_state,elt);
172
 
        gs_state_do_ptrs(r1)
173
 
#undef r1
174
 
        gsvptr->device = gx_device_reloc_ptr(gsvptr->device, gcst);
175
 
        RELOC_PTR(gs_state, dfilter_stack);
176
 
    }
177
 
}
178
 
RELOC_PTRS_END
179
 
 
180
 
/* Copy client data, using the copy_for procedure if available, */
181
 
/* the copy procedure otherwise. */
182
 
static int
183
 
gstate_copy_client_data(gs_state * pgs, void *dto, void *dfrom,
184
 
                        gs_state_copy_reason_t reason)
185
 
{
186
 
    return (pgs->client_procs.copy_for != 0 ?
187
 
            (*pgs->client_procs.copy_for) (dto, dfrom, reason) :
188
 
            (*pgs->client_procs.copy) (dto, dfrom));
189
 
}
190
 
 
191
 
/* ------ Operations on the entire graphics state ------ */
192
 
 
193
 
/* Define the initial value of the graphics state. */
194
 
static const gs_imager_state gstate_initial = {
195
 
    gs_imager_state_initial(1.0)
196
 
};
197
 
 
198
 
/*
199
 
 * Allocate a path for the graphics state.  We use stable memory because
200
 
 * some PostScript files have Type 3 fonts whose BuildChar procedure
201
 
 * uses the sequence save ... setcachedevice ... restore, and the path
202
 
 * built between the setcachedevice and the restore must not be freed.
203
 
 * If it weren't for this, we don't think stable memory would be needed.
204
 
 */
205
 
static gs_memory_t *
206
 
gstate_path_memory(gs_memory_t *mem)
207
 
{
208
 
    return gs_memory_stable(mem);
209
 
}
210
 
 
211
 
/* Allocate and initialize a graphics state. */
212
 
gs_state *
213
 
gs_state_alloc(gs_memory_t * mem)
214
 
{
215
 
    gs_state *pgs = gstate_alloc(mem, "gs_state_alloc", NULL);
216
 
    int code;
217
 
 
218
 
    if (pgs == 0)
219
 
        return 0;
220
 
    pgs->saved = 0;
221
 
    *(gs_imager_state *)pgs = gstate_initial;
222
 
 
223
 
    /*
224
 
     * Just enough of the state is initialized at this point
225
 
     * that it's OK to call gs_state_free if an allocation fails.
226
 
     */
227
 
 
228
 
    code = gs_imager_state_initialize((gs_imager_state *) pgs, mem);
229
 
    if (code < 0)
230
 
        goto fail;
231
 
 
232
 
    /* Finish initializing the color rendering state. */
233
 
 
234
 
    rc_alloc_struct_1(pgs->halftone, gs_halftone, &st_halftone, mem,
235
 
                      goto fail, "gs_state_alloc(halftone)");
236
 
    pgs->halftone->type = ht_type_none;
237
 
 
238
 
    /* Initialize other things not covered by initgraphics */
239
 
 
240
 
    pgs->path = gx_path_alloc(gstate_path_memory(mem), "gs_state_alloc(path)");
241
 
    pgs->clip_path = gx_cpath_alloc(mem, "gs_state_alloc(clip_path)");
242
 
    pgs->clip_stack = 0;
243
 
    pgs->view_clip = gx_cpath_alloc(mem, "gs_state_alloc(view_clip)");
244
 
    pgs->view_clip->rule = 0;   /* no clipping */
245
 
    pgs->effective_clip_id = pgs->clip_path->id;
246
 
    pgs->effective_view_clip_id = gs_no_id;
247
 
    pgs->effective_clip_path = pgs->clip_path;
248
 
    pgs->effective_clip_shared = true;
249
 
    /* Initialize things so that gx_remap_color won't crash. */
250
 
    pgs->color_space = gs_cspace_new_DeviceGray(pgs->memory);
251
 
    pgs->in_cachedevice = 0;
252
 
    gx_set_device_color_1(pgs); /* sets colorspace and client color */
253
 
    pgs->device = 0;            /* setting device adjusts refcts */
254
 
    gs_nulldevice(pgs);
255
 
    gs_setalpha(pgs, 1.0);
256
 
    gs_settransfer(pgs, gs_identity_transfer);
257
 
    gs_setflat(pgs, 1.0);
258
 
    gs_setfilladjust(pgs, 0.25, 0.25);
259
 
    gs_setlimitclamp(pgs, false);
260
 
    gs_setstrokeadjust(pgs, true);
261
 
    pgs->font = 0;              /* Not right, but acceptable until the */
262
 
    /* PostScript code does the first setfont. */
263
 
    pgs->root_font = 0;         /* ditto */
264
 
    pgs->in_charpath = (gs_char_path_mode) 0;
265
 
    pgs->show_gstate = 0;
266
 
    pgs->level = 0;
267
 
    pgs->dfilter_stack = 0;
268
 
    pgs->transparency_group_stack = 0;
269
 
    if (gs_initgraphics(pgs) >= 0) 
270
 
        return pgs;
271
 
    /* Something went very wrong. */
272
 
fail:
273
 
    gs_state_free(pgs);
274
 
    return 0;
275
 
}
276
 
 
277
 
/* Set the client data in a graphics state. */
278
 
/* This should only be done to a newly created state. */
279
 
void
280
 
gs_state_set_client(gs_state * pgs, void *pdata,
281
 
                    const gs_state_client_procs * pprocs, bool client_has_pattern_streams)
282
 
{
283
 
    pgs->client_data = pdata;
284
 
    pgs->client_procs = *pprocs;
285
 
    pgs->have_pattern_streams = client_has_pattern_streams;
286
 
}
287
 
 
288
 
/* Get the client data from a graphics state. */
289
 
#undef gs_state_client_data     /* gzstate.h makes this a macro */
290
 
void *
291
 
gs_state_client_data(const gs_state * pgs)
292
 
{
293
 
    return pgs->client_data;
294
 
}
295
 
 
296
 
/* Free the chain of gstates.*/
297
 
int
298
 
gs_state_free_chain(gs_state * pgs)
299
 
{
300
 
   gs_state *saved = pgs, *tmp; 
301
 
 
302
 
   while(saved != 0) {
303
 
       tmp = saved->saved;
304
 
       gs_state_free(saved);
305
 
       saved = tmp;
306
 
   }
307
 
   return 0;
308
 
}
309
 
 
310
 
/* Free a graphics state. */
311
 
int
312
 
gs_state_free(gs_state * pgs)
313
 
{
314
 
    gstate_free_contents(pgs);
315
 
    gs_free_object(pgs->memory, pgs, "gs_state_free");
316
 
    return 0;
317
 
}
318
 
 
319
 
/* Save the graphics state. */
320
 
int
321
 
gs_gsave(gs_state * pgs)
322
 
{
323
 
    gs_state *pnew = gstate_clone(pgs, pgs->memory, "gs_gsave",
324
 
                                  copy_for_gsave);
325
 
 
326
 
    if (pnew == 0)
327
 
        return_error(gs_error_VMerror);
328
 
    /* As of PLRM3, the interaction between gsave and the clip stack is
329
 
     * now clear. gsave stores the clip stack into the saved graphics
330
 
     * state, but then clears it in the current graphics state.
331
 
     *
332
 
     * Ordinarily, reference count rules would indicate an rc_decrement()
333
 
     * on pgs->clip_stack, but gstate_clone() has an exception for
334
 
     * the clip_stack and dfilter_stack fields.
335
 
     */
336
 
    pgs->clip_stack = 0;
337
 
    rc_increment(pnew->dfilter_stack);
338
 
    pgs->saved = pnew;
339
 
    if (pgs->show_gstate == pgs)
340
 
        pgs->show_gstate = pnew->show_gstate = pnew;
341
 
    pgs->level++;
342
 
    if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
343
 
              (ulong) pnew, pgs->level);
344
 
    return 0;
345
 
}
346
 
 
347
 
/*
348
 
 * Save the graphics state for a 'save'.
349
 
 * We cut the stack below the new gstate, and return the old one.
350
 
 * In addition to an ordinary gsave, we create a new view clip path.
351
 
 */
352
 
int
353
 
gs_gsave_for_save(gs_state * pgs, gs_state ** psaved)
354
 
{
355
 
    int code;
356
 
    gx_clip_path *old_cpath = pgs->view_clip;
357
 
    gx_clip_path *new_cpath;
358
 
 
359
 
    if (old_cpath) {
360
 
        new_cpath =
361
 
            gx_cpath_alloc_shared(old_cpath, pgs->memory,
362
 
                                  "gs_gsave_for_save(view_clip)");
363
 
        if (new_cpath == 0)
364
 
            return_error(gs_error_VMerror);
365
 
    } else {
366
 
        new_cpath = 0;
367
 
    }
368
 
    code = gs_gsave(pgs);
369
 
    if (code < 0)
370
 
        goto fail;
371
 
    if (pgs->effective_clip_path == pgs->view_clip)
372
 
        pgs->effective_clip_path = new_cpath;
373
 
    pgs->view_clip = new_cpath;
374
 
    /* Cut the stack so we can't grestore past here. */
375
 
    *psaved = pgs->saved;
376
 
    pgs->saved = 0;
377
 
    return code;
378
 
fail:
379
 
    if (new_cpath)
380
 
        gx_cpath_free(new_cpath, "gs_gsave_for_save(view_clip)");
381
 
    return code;
382
 
}
383
 
 
384
 
/* Restore the graphics state. Can fully empty graphics stack */
385
 
int     /* return 0 if ok, 1 if stack was empty */
386
 
gs_grestore_only(gs_state * pgs)
387
 
{
388
 
    gs_state *saved = pgs->saved;
389
 
    void *pdata = pgs->client_data;
390
 
    void *sdata;
391
 
    gs_transparency_state_t *tstack = pgs->transparency_stack;
392
 
    bool prior_overprint = pgs->overprint;
393
 
 
394
 
    if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
395
 
              (ulong) saved, pgs->level);
396
 
    if (!saved)
397
 
        return 1;
398
 
    sdata = saved->client_data;
399
 
    if (saved->pattern_cache == 0)
400
 
        saved->pattern_cache = pgs->pattern_cache;
401
 
    /* Swap back the client data pointers. */
402
 
    pgs->client_data = sdata;
403
 
    saved->client_data = pdata;
404
 
    if (pdata != 0 && sdata != 0)
405
 
        gstate_copy_client_data(pgs, pdata, sdata, copy_for_grestore);
406
 
    gstate_free_contents(pgs);
407
 
    *pgs = *saved;
408
 
    pgs->transparency_stack = tstack;
409
 
    if (pgs->show_gstate == saved)
410
 
        pgs->show_gstate = pgs;
411
 
    gs_free_object(pgs->memory, saved, "gs_grestore");
412
 
 
413
 
    /* update the overprint compositor, if necessary */
414
 
    if (prior_overprint != pgs->overprint)
415
 
        return gs_do_set_overprint(pgs);
416
 
    else
417
 
        return 0;
418
 
}
419
 
 
420
 
/* Restore the graphics state per PostScript semantics */
421
 
int
422
 
gs_grestore(gs_state * pgs)
423
 
{
424
 
    int code;
425
 
    if (!pgs->saved)
426
 
        return gs_gsave(pgs);   /* shouldn't ever happen */
427
 
    code = gs_grestore_only(pgs);
428
 
    if (code < 0)
429
 
        return code;
430
 
 
431
 
    /* Wraparound: make sure there are always >= 1 saves on stack */
432
 
    if (pgs->saved)
433
 
        return 0;
434
 
    return gs_gsave(pgs);
435
 
}
436
 
 
437
 
/* Restore the graphics state for a 'restore', splicing the old stack */
438
 
/* back on.  Note that we actually do a grestoreall + 2 grestores. */
439
 
int
440
 
gs_grestoreall_for_restore(gs_state * pgs, gs_state * saved)
441
 
{
442
 
    int code;
443
 
 
444
 
    while (pgs->saved->saved) {
445
 
        code = gs_grestore(pgs);
446
 
        if (code < 0)
447
 
            return code;
448
 
    }
449
 
    /* Make sure we don't leave dangling pointers in the caches. */
450
 
    if (pgs->pattern_cache)
451
 
        (*pgs->pattern_cache->free_all) (pgs->pattern_cache);
452
 
    pgs->saved->saved = saved;
453
 
    code = gs_grestore(pgs);
454
 
    if (code < 0)
455
 
        return code;
456
 
    if (pgs->view_clip) {
457
 
        gx_cpath_free(pgs->view_clip, "gs_grestoreall_for_restore");
458
 
        pgs->view_clip = 0;
459
 
    }
460
 
    return gs_grestore(pgs);
461
 
}
462
 
 
463
 
 
464
 
/* Restore to the bottommost graphics state (at this save level). */
465
 
int
466
 
gs_grestoreall(gs_state * pgs)
467
 
{
468
 
    if (!pgs->saved)            /* shouldn't happen */
469
 
        return gs_gsave(pgs);
470
 
    while (pgs->saved->saved) {
471
 
        int code = gs_grestore(pgs);
472
 
 
473
 
        if (code < 0)
474
 
            return code;
475
 
    }
476
 
    return gs_grestore(pgs);
477
 
}
478
 
 
479
 
/* Allocate and return a new graphics state. */
480
 
gs_state *
481
 
gs_gstate(gs_state * pgs)
482
 
{
483
 
    return gs_state_copy(pgs, pgs->memory);
484
 
}
485
 
gs_state *
486
 
gs_state_copy(gs_state * pgs, gs_memory_t * mem)
487
 
{
488
 
    gs_state *pnew;
489
 
    /* Prevent 'capturing' the view clip path. */
490
 
    gx_clip_path *view_clip = pgs->view_clip;
491
 
 
492
 
    pgs->view_clip = 0;
493
 
    pnew = gstate_clone(pgs, mem, "gs_gstate", copy_for_gstate);
494
 
    clip_stack_rc_adjust(pnew->clip_stack, 1, "gs_state_copy");
495
 
    rc_increment(pnew->dfilter_stack);
496
 
    pgs->view_clip = view_clip;
497
 
    if (pnew == 0)
498
 
        return 0;
499
 
    pnew->saved = 0;
500
 
    /*
501
 
     * Prevent dangling references from the show_gstate pointer.  If
502
 
     * this context is its own show_gstate, set the pointer in the clone
503
 
     * to point to the clone; otherwise, set the pointer in the clone to
504
 
     * 0, and let gs_setgstate fix it up.
505
 
     */
506
 
    pnew->show_gstate =
507
 
        (pgs->show_gstate == pgs ? pnew : 0);
508
 
    return pnew;
509
 
}
510
 
 
511
 
/* Copy one previously allocated graphics state to another. */
512
 
int
513
 
gs_copygstate(gs_state * pto, const gs_state * pfrom)
514
 
{
515
 
    return gstate_copy(pto, pfrom, copy_for_copygstate, "gs_copygstate");
516
 
}
517
 
 
518
 
/* Copy the current graphics state to a previously allocated one. */
519
 
int
520
 
gs_currentgstate(gs_state * pto, const gs_state * pgs)
521
 
{
522
 
    int code =
523
 
        gstate_copy(pto, pgs, copy_for_currentgstate, "gs_currentgstate");
524
 
 
525
 
    if (code >= 0)
526
 
        pto->view_clip = 0;
527
 
    return code;
528
 
}
529
 
 
530
 
/* Restore the current graphics state from a previously allocated one. */
531
 
int
532
 
gs_setgstate(gs_state * pgs, const gs_state * pfrom)
533
 
{
534
 
    /*
535
 
     * The implementation is the same as currentgstate,
536
 
     * except we must preserve the saved pointer, the level,
537
 
     * the view clip, and possibly the show_gstate.
538
 
     */
539
 
    gs_state *saved_show = pgs->show_gstate;
540
 
    int level = pgs->level;
541
 
    gx_clip_path *view_clip = pgs->view_clip;
542
 
    gs_transparency_state_t *tstack = pgs->transparency_stack;
543
 
    int code;
544
 
 
545
 
    pgs->view_clip = 0;         /* prevent refcount decrementing */
546
 
    code = gstate_copy(pgs, pfrom, copy_for_setgstate, "gs_setgstate");
547
 
    if (code < 0)
548
 
        return code;
549
 
    pgs->level = level;
550
 
    pgs->view_clip = view_clip;
551
 
    pgs->show_gstate =
552
 
        (pgs->show_gstate == pfrom ? pgs : saved_show);
553
 
    pgs->transparency_stack = tstack;
554
 
 
555
 
    /* update the overprint compositor */
556
 
    return gs_do_set_overprint(pgs);
557
 
}
558
 
 
559
 
/* Get the allocator pointer of a graphics state. */
560
 
/* This is provided only for the interpreter */
561
 
/* and for color space implementation. */
562
 
gs_memory_t *
563
 
gs_state_memory(const gs_state * pgs)
564
 
{
565
 
    return pgs->memory;
566
 
}
567
 
 
568
 
/* Get the saved pointer of the graphics state. */
569
 
/* This is provided only for Level 2 grestore. */
570
 
gs_state *
571
 
gs_state_saved(const gs_state * pgs)
572
 
{
573
 
    return pgs->saved;
574
 
}
575
 
 
576
 
/* Swap the saved pointer of the graphics state. */
577
 
/* This is provided only for save/restore. */
578
 
gs_state *
579
 
gs_state_swap_saved(gs_state * pgs, gs_state * new_saved)
580
 
{
581
 
    gs_state *saved = pgs->saved;
582
 
 
583
 
    pgs->saved = new_saved;
584
 
    return saved;
585
 
}
586
 
 
587
 
/* Swap the memory pointer of the graphics state. */
588
 
/* This is provided only for the interpreter. */
589
 
gs_memory_t *
590
 
gs_state_swap_memory(gs_state * pgs, gs_memory_t * mem)
591
 
{
592
 
    gs_memory_t *memory = pgs->memory;
593
 
 
594
 
    pgs->memory = mem;
595
 
    return memory;
596
 
}
597
 
 
598
 
/* ------ Operations on components ------ */
599
 
 
600
 
/*
601
 
 * Push an overprint compositor onto the current device. Note that if
602
 
 * the current device already is an overprint compositor, the
603
 
 * create_compositor will update its parameters but not create a new
604
 
 * compositor device.
605
 
 */
606
 
int
607
 
gs_state_update_overprint(gs_state * pgs, const gs_overprint_params_t * pparams)
608
 
{
609
 
    gs_composite_t *    pct = 0;
610
 
    gs_imager_state *   pis = (gs_imager_state *)pgs;
611
 
    int                 code;
612
 
    gx_device *         dev = pgs->device;
613
 
    gx_device *         ovptdev;
614
 
 
615
 
    if ( (code = gs_create_overprint(&pct, pparams, pgs->memory)) >= 0 &&
616
 
         (code = dev_proc(dev, create_compositor)( dev,
617
 
                                                   &ovptdev,
618
 
                                                   pct,
619
 
                                                   pis,
620
 
                                                   pgs->memory )) >= 0   ) {
621
 
        if (ovptdev != dev)
622
 
            gx_set_device_only(pgs, ovptdev);
623
 
    }
624
 
    if (pct != 0)
625
 
        gs_free_object(pgs->memory, pct, "gs_state_update_overprint");
626
 
 
627
 
    /* the following hack handles devices that don't support compositors */
628
 
    if (code == gs_error_unknownerror && !pparams->retain_any_comps)
629
 
        code = 0;
630
 
    return code;
631
 
}
632
 
 
633
 
/*
634
 
 * Reset the overprint mode for the current color space and color. This
635
 
 * routine should be called  whenever the current device (i.e.: color
636
 
 * model), overprint, overprint mode, color space, or color are modified.
637
 
 *
638
 
 * The need reason this routine must be called for changes in the current
639
 
 * color and must consider the current color involves the Pattern color
640
 
 * space. In that space, the "color" (pattern) can determine if the base
641
 
 * color space is used (PatternType 1 with PaintType 2), or may provide
642
 
 * is own color space (PatternType 1 with PaintType 1, PatternType 2).
643
 
 *
644
 
 * The most general situation (PatternType 1 with PaintType 1) cannot be
645
 
 * handled properly due to limitations of the pattern cache mechanism,
646
 
 * so in this case overprint is effectively disable by making all color
647
 
 * components "drawn".
648
 
 */
649
 
int
650
 
gs_do_set_overprint(gs_state * pgs)
651
 
{
652
 
    const gs_color_space *  pcs = pgs->color_space;
653
 
    const gs_client_color * pcc = pgs->ccolor;
654
 
    int                     code = 0;
655
 
 
656
 
    if (cs_num_components(pcs) < 0 && pcc->pattern != 0)
657
 
        code = pcc->pattern->type->procs.set_color(pcc, pgs);
658
 
    else
659
 
        pcs->type->set_overprint(pcs, pgs);
660
 
    return code;
661
 
}
662
 
 
663
 
/* setoverprint */
664
 
void
665
 
gs_setoverprint(gs_state * pgs, bool ovp)
666
 
{
667
 
    bool    prior_ovp = pgs->overprint;
668
 
 
669
 
    pgs->overprint = ovp;
670
 
    if (prior_ovp != ovp)
671
 
        (void)gs_do_set_overprint(pgs);
672
 
}
673
 
 
674
 
/* currentoverprint */
675
 
bool
676
 
gs_currentoverprint(const gs_state * pgs)
677
 
{
678
 
    return pgs->overprint;
679
 
}
680
 
 
681
 
/* setoverprintmode */
682
 
int
683
 
gs_setoverprintmode(gs_state * pgs, int mode)
684
 
{
685
 
    int     prior_mode = pgs->effective_overprint_mode;
686
 
    int     code = 0;
687
 
 
688
 
    if (mode < 0 || mode > 1)
689
 
        return_error(gs_error_rangecheck);
690
 
    pgs->overprint_mode = mode;
691
 
    if (pgs->overprint && prior_mode != mode)
692
 
        code = gs_do_set_overprint(pgs);
693
 
    return code;
694
 
}
695
 
 
696
 
/* currentoverprintmode */
697
 
int
698
 
gs_currentoverprintmode(const gs_state * pgs)
699
 
{
700
 
    return pgs->overprint_mode;
701
 
}
702
 
 
703
 
/* setrenderingintent
704
 
 *
705
 
 *  Use ICC numbers from Table 18 (section 6.1.11) rather than the PDF order
706
 
 *  to reduce re-coding and confusion.
707
 
 *    Perceptual            0
708
 
 *    Relative Colorimetric 1
709
 
 *    Saturation            2
710
 
 *    AbsoluteColorimetric  3
711
 
 */
712
 
int
713
 
gs_setrenderingintent(gs_state *pgs, int ri) {
714
 
    if (ri < 0 || ri > 3)
715
 
        return_error(gs_error_rangecheck);
716
 
    pgs->renderingintent = ri;
717
 
    return 0;
718
 
}
719
 
 
720
 
/* currentrenderingintent */
721
 
int
722
 
gs_currentrenderingintent(const gs_state * pgs)
723
 
{
724
 
    return pgs->renderingintent;
725
 
}
726
 
 
727
 
/*
728
 
 * Reset most of the graphics state.
729
 
 *
730
 
 * NB: This routine no longer resets the current color or current color
731
 
 *     space. It cannot do this for PostScript, due to color substitution.
732
 
 *     Clients should perform the appropriate color/colorspace
733
 
 *     initializaion themselves.
734
 
 */
735
 
int
736
 
gs_initgraphics(gs_state * pgs)
737
 
{
738
 
    int code;
739
 
 
740
 
    gs_initmatrix(pgs);
741
 
    if ((code = gs_newpath(pgs)) < 0 ||
742
 
        (code = gs_initclip(pgs)) < 0 ||
743
 
        (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
744
 
        (code = gs_setlinecap(pgs, gstate_initial.line_params.cap)) < 0 ||
745
 
        (code = gs_setlinejoin(pgs, gstate_initial.line_params.join)) < 0 ||
746
 
        (code = gs_setcurvejoin(pgs, gstate_initial.line_params.curve_join)) < 0 ||
747
 
        (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
748
 
        (gs_setdashadapt(pgs, false),
749
 
         (code = gs_setdotlength(pgs, 0.0, false))) < 0 ||
750
 
        (code = gs_setdotorientation(pgs)) < 0 ||
751
 
        (code = gs_setmiterlimit(pgs, gstate_initial.line_params.miter_limit)) < 0
752
 
        )
753
 
        return code;
754
 
    gs_init_rop(pgs);
755
 
    return 0;
756
 
}
757
 
 
758
 
/* setfilladjust */
759
 
int
760
 
gs_setfilladjust(gs_state * pgs, floatp adjust_x, floatp adjust_y)
761
 
{
762
 
#define CLAMP_TO_HALF(v)\
763
 
    ((v) <= 0 ? fixed_0 : (v) >= 0.5 ? fixed_half : float2fixed(v));
764
 
 
765
 
    pgs->fill_adjust.x = CLAMP_TO_HALF(adjust_x);
766
 
    pgs->fill_adjust.y = CLAMP_TO_HALF(adjust_y);
767
 
    return 0;
768
 
#undef CLAMP_TO_HALF
769
 
}
770
 
 
771
 
/* currentfilladjust */
772
 
int
773
 
gs_currentfilladjust(const gs_state * pgs, gs_point * adjust)
774
 
{
775
 
    adjust->x = fixed2float(pgs->fill_adjust.x);
776
 
    adjust->y = fixed2float(pgs->fill_adjust.y);
777
 
    return 0;
778
 
}
779
 
 
780
 
/* setlimitclamp */
781
 
void
782
 
gs_setlimitclamp(gs_state * pgs, bool clamp)
783
 
{
784
 
    pgs->clamp_coordinates = clamp;
785
 
}
786
 
 
787
 
/* currentlimitclamp */
788
 
bool
789
 
gs_currentlimitclamp(const gs_state * pgs)
790
 
{
791
 
    return pgs->clamp_coordinates;
792
 
}
793
 
 
794
 
/* settextrenderingmode */
795
 
void
796
 
gs_settextrenderingmode(gs_state * pgs, uint trm)
797
 
{
798
 
    pgs->text_rendering_mode = trm;
799
 
}
800
 
 
801
 
/* currenttextrenderingmode */
802
 
uint
803
 
gs_currenttextrenderingmode(const gs_state * pgs)
804
 
{
805
 
    return pgs->text_rendering_mode;
806
 
}
807
 
 
808
 
/* ------ Internal routines ------ */
809
 
 
810
 
/* Free the privately allocated parts of a gstate. */
811
 
static void
812
 
gstate_free_parts(const gs_state * parts, gs_memory_t * mem, client_name_t cname)
813
 
{
814
 
    gs_free_object(mem, parts->dev_color, cname);
815
 
    gs_free_object(mem, parts->ccolor, cname);
816
 
    if (!parts->effective_clip_shared)
817
 
        gx_cpath_free(parts->effective_clip_path, cname);
818
 
    gx_cpath_free(parts->clip_path, cname);
819
 
    gx_path_free(parts->path, cname);
820
 
}
821
 
 
822
 
/* Allocate the privately allocated parts of a gstate. */
823
 
static int
824
 
gstate_alloc_parts(gs_state * parts, const gs_state * shared,
825
 
                   gs_memory_t * mem, client_name_t cname)
826
 
{
827
 
    gs_memory_t *path_mem = gstate_path_memory(mem);
828
 
 
829
 
    parts->path =
830
 
        (shared ?
831
 
         gx_path_alloc_shared(shared->path, path_mem,
832
 
                              "gstate_alloc_parts(path)") :
833
 
         gx_path_alloc(path_mem, "gstate_alloc_parts(path)"));
834
 
    parts->clip_path =
835
 
        (shared ?
836
 
         gx_cpath_alloc_shared(shared->clip_path, mem,
837
 
                               "gstate_alloc_parts(clip_path)") :
838
 
         gx_cpath_alloc(mem, "gstate_alloc_parts(clip_path)"));
839
 
    if (!shared || shared->effective_clip_shared) {
840
 
        parts->effective_clip_path = parts->clip_path;
841
 
        parts->effective_clip_shared = true;
842
 
    } else {
843
 
        parts->effective_clip_path =
844
 
            gx_cpath_alloc_shared(shared->effective_clip_path, mem,
845
 
                                  "gstate_alloc_parts(effective_clip_path)");
846
 
        parts->effective_clip_shared = false;
847
 
    }
848
 
    parts->color_space = NULL;
849
 
    parts->ccolor =
850
 
        gs_alloc_struct(mem, gs_client_color, &st_client_color, cname);
851
 
    parts->dev_color =
852
 
        gs_alloc_struct(mem, gx_device_color, &st_device_color, cname);
853
 
    if (parts->path == 0 || parts->clip_path == 0 ||
854
 
        parts->effective_clip_path == 0 ||
855
 
        parts->ccolor == 0 || parts->dev_color == 0
856
 
        ) {
857
 
        gstate_free_parts(parts, mem, cname);
858
 
        return_error(gs_error_VMerror);
859
 
    }
860
 
    return 0;
861
 
}
862
 
 
863
 
/*
864
 
 * Allocate a gstate and its contents.
865
 
 * If pfrom is not NULL, the path, clip_path, and (if distinct from both
866
 
 * clip_path and view_clip) effective_clip_path share the segments of
867
 
 * pfrom's corresponding path(s).
868
 
 */
869
 
static gs_state *
870
 
gstate_alloc(gs_memory_t * mem, client_name_t cname, const gs_state * pfrom)
871
 
{
872
 
    gs_state *pgs =
873
 
        gs_alloc_struct(mem, gs_state, &st_gs_state, cname);
874
 
 
875
 
    if (pgs == 0)
876
 
        return 0;
877
 
    if (gstate_alloc_parts(pgs, pfrom, mem, cname) < 0) {
878
 
        gs_free_object(mem, pgs, cname);
879
 
        return 0;
880
 
    }
881
 
    pgs->memory = mem;
882
 
    return pgs;
883
 
}
884
 
 
885
 
/* Copy the dash pattern from one gstate to another. */
886
 
static int
887
 
gstate_copy_dash(gs_state * pto, const gs_state * pfrom)
888
 
{
889
 
    return gs_setdash(pto, pfrom->line_params.dash.pattern,
890
 
                      pfrom->line_params.dash.pattern_size,
891
 
                      pfrom->line_params.dash.offset);
892
 
}
893
 
 
894
 
/* Clone an existing graphics state. */
895
 
/* Return 0 if the allocation fails. */
896
 
/* If reason is for_gsave, the clone refers to the old contents, */
897
 
/* and we switch the old state to refer to the new contents. */
898
 
static gs_state *
899
 
gstate_clone(gs_state * pfrom, gs_memory_t * mem, client_name_t cname,
900
 
             gs_state_copy_reason_t reason)
901
 
{
902
 
    gs_state *pgs = gstate_alloc(mem, cname, pfrom);
903
 
    gs_state_parts parts;
904
 
 
905
 
    if (pgs == 0)
906
 
        return 0;
907
 
    GSTATE_ASSIGN_PARTS(&parts, pgs);
908
 
    *pgs = *pfrom;
909
 
    pgs->transparency_stack = 0;
910
 
    /* Copy the dash pattern if necessary. */
911
 
    if (pgs->line_params.dash.pattern) {
912
 
        int code;
913
 
 
914
 
        pgs->line_params.dash.pattern = 0;      /* force allocation */
915
 
        code = gstate_copy_dash(pgs, pfrom);
916
 
        if (code < 0)
917
 
            goto fail;
918
 
    }
919
 
    if (pgs->client_data != 0) {
920
 
        void *pdata = pgs->client_data = (*pgs->client_procs.alloc) (mem);
921
 
 
922
 
        if (pdata == 0 ||
923
 
         gstate_copy_client_data(pgs, pdata, pfrom->client_data, reason) < 0
924
 
            )
925
 
            goto fail;
926
 
    }
927
 
    gs_imager_state_copied((gs_imager_state *)pgs);
928
 
    /* Don't do anything to clip_stack. */
929
 
    rc_increment(pgs->device);
930
 
    *parts.ccolor = *pfrom->ccolor;
931
 
    *parts.dev_color = *pfrom->dev_color;
932
 
    if (reason == copy_for_gsave) {
933
 
        float *dfrom = pfrom->line_params.dash.pattern;
934
 
        float *dto = pgs->line_params.dash.pattern;
935
 
 
936
 
        GSTATE_ASSIGN_PARTS(pfrom, &parts);
937
 
        pgs->line_params.dash.pattern = dfrom;
938
 
        pfrom->line_params.dash.pattern = dto;
939
 
    } else {
940
 
        GSTATE_ASSIGN_PARTS(pgs, &parts);
941
 
    }
942
 
    cs_adjust_counts(pgs, 1);
943
 
    return pgs;
944
 
  fail:
945
 
    gs_free_object(mem, pgs->line_params.dash.pattern, cname);
946
 
    GSTATE_ASSIGN_PARTS(pgs, &parts);
947
 
    gstate_free_parts(pgs, mem, cname);
948
 
    gs_free_object(mem, pgs, cname);
949
 
    return 0;
950
 
}
951
 
 
952
 
 
953
 
/* Adjust reference counters for the whole clip stack */
954
 
/* accessible from the given point */
955
 
static void
956
 
clip_stack_rc_adjust(gx_clip_stack_t *cs, int delta, client_name_t cname)
957
 
{
958
 
    gx_clip_stack_t *p = cs;  
959
 
 
960
 
    while(p) {
961
 
        gx_clip_stack_t *q = p;
962
 
        p = p->next;  
963
 
        rc_adjust(q, delta, cname);
964
 
    }
965
 
}
966
 
 
967
 
/* Release the composite parts of a graphics state, */
968
 
/* but not the state itself. */
969
 
static void
970
 
gstate_free_contents(gs_state * pgs)
971
 
{
972
 
    gs_memory_t *mem = pgs->memory;
973
 
    const char *const cname = "gstate_free_contents";
974
 
 
975
 
    rc_decrement(pgs->device, cname);
976
 
    clip_stack_rc_adjust(pgs->clip_stack, -1, cname);
977
 
    rc_decrement(pgs->dfilter_stack, cname);
978
 
    cs_adjust_counts(pgs, -1);
979
 
    if (pgs->client_data != 0)
980
 
        (*pgs->client_procs.free) (pgs->client_data, mem);
981
 
    gs_free_object(mem, pgs->line_params.dash.pattern, cname);
982
 
    gstate_free_parts(pgs, mem, cname);
983
 
    gs_imager_state_release((gs_imager_state *)pgs);
984
 
}
985
 
 
986
 
/* Copy one gstate to another. */
987
 
static int
988
 
gstate_copy(gs_state * pto, const gs_state * pfrom,
989
 
            gs_state_copy_reason_t reason, client_name_t cname)
990
 
{
991
 
    gs_state_parts parts;
992
 
 
993
 
    GSTATE_ASSIGN_PARTS(&parts, pto);
994
 
    /* Copy the dash pattern if necessary. */
995
 
    if (pfrom->line_params.dash.pattern || pto->line_params.dash.pattern) {
996
 
        int code = gstate_copy_dash(pto, pfrom);
997
 
 
998
 
        if (code < 0)
999
 
            return code;
1000
 
    }
1001
 
    /*
1002
 
     * It's OK to decrement the counts before incrementing them,
1003
 
     * because anything that is going to survive has a count of
1004
 
     * at least 2 (pto and somewhere else) initially.
1005
 
     * Handle references from contents.
1006
 
     */
1007
 
    cs_adjust_counts(pto, -1);
1008
 
    gx_path_assign_preserve(pto->path, pfrom->path);
1009
 
    gx_cpath_assign_preserve(pto->clip_path, pfrom->clip_path);
1010
 
    /*
1011
 
     * effective_clip_shared will be copied, but we need to do the
1012
 
     * right thing with effective_clip_path.
1013
 
     */
1014
 
    if (pfrom->effective_clip_shared) {
1015
 
        /*
1016
 
         * pfrom->effective_clip_path is either pfrom->view_clip or
1017
 
         * pfrom->clip_path.
1018
 
         */
1019
 
        parts.effective_clip_path =
1020
 
            (pfrom->effective_clip_path == pfrom->view_clip ?
1021
 
             pto->view_clip : parts.clip_path);
1022
 
    } else
1023
 
        gx_cpath_assign_preserve(pto->effective_clip_path,
1024
 
                                 pfrom->effective_clip_path);
1025
 
    *parts.ccolor = *pfrom->ccolor;
1026
 
    *parts.dev_color = *pfrom->dev_color;
1027
 
    /* Handle references from gstate object. */
1028
 
    rc_pre_assign(pto->device, pfrom->device, cname);
1029
 
    rc_pre_assign(pto->dfilter_stack, pfrom->dfilter_stack, cname);
1030
 
    if (pto->clip_stack != pfrom->clip_stack) {
1031
 
        clip_stack_rc_adjust(pfrom->clip_stack, 1, cname);
1032
 
        clip_stack_rc_adjust(pto->clip_stack, -1, cname);
1033
 
    }
1034
 
    {
1035
 
        struct gx_pattern_cache_s *pcache = pto->pattern_cache;
1036
 
        void *pdata = pto->client_data;
1037
 
        gs_memory_t *mem = pto->memory;
1038
 
        gs_state *saved = pto->saved;
1039
 
        float *pattern = pto->line_params.dash.pattern;
1040
 
 
1041
 
        gs_imager_state_pre_assign((gs_imager_state *)pto,
1042
 
                                   (const gs_imager_state *)pfrom);
1043
 
        *pto = *pfrom;
1044
 
        pto->client_data = pdata;
1045
 
        pto->memory = mem;
1046
 
        pto->saved = saved;
1047
 
        pto->line_params.dash.pattern = pattern;
1048
 
        if (pto->pattern_cache == 0)
1049
 
            pto->pattern_cache = pcache;
1050
 
        if (pfrom->client_data != 0) {
1051
 
            /* We need to break 'const' here. */
1052
 
            gstate_copy_client_data((gs_state *) pfrom, pdata,
1053
 
                                    pfrom->client_data, reason);
1054
 
        }
1055
 
    }
1056
 
    GSTATE_ASSIGN_PARTS(pto, &parts);
1057
 
    cs_adjust_counts(pto, 1);
1058
 
    pto->show_gstate =
1059
 
        (pfrom->show_gstate == pfrom ? pto : 0);
1060
 
    return 0;
1061
 
}
1062
 
 
1063
 
/* Accessories. */
1064
 
gs_id gx_get_clip_path_id(gs_state *pgs)
1065
 
{
1066
 
    return pgs->clip_path->id;
1067
 
}