1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
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.
14
/* $Id: gsstate.c 8407 2007-11-27 18:11:10Z leonardo $ */
15
/* Miscellaneous graphics state operators for Ghostscript library */
20
#include "gsutil.h" /* for gs_next_ids */
22
#include "gxcspace.h" /* here for gscolor2.h */
25
#include "gscoord.h" /* for gs_initmatrix */
40
/* Forward references */
41
static gs_state *gstate_alloc(gs_memory_t *, client_name_t,
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);
51
* Graphics state storage management is complicated. There are many
52
* different classes of storage associated with a graphics state:
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
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:
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:
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.
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
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.
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:
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,
101
* (may be referenced from image enumerators or elsewhere)
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.
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.
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.
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).
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.
139
typedef struct gs_state_parts_s {
141
gx_clip_path *clip_path;
142
gx_clip_path *effective_clip_path;
143
gs_client_color *ccolor;
144
gx_device_color *dev_color;
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)
153
extern_st(st_imager_state);
154
public_st_gs_state();
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);
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);
167
static RELOC_PTRS_WITH(gs_state_reloc_ptrs, gs_state *gsvptr)
169
RELOC_PREFIX(st_imager_state);
171
#define r1(i,elt) RELOC_PTR(gs_state,elt);
174
gsvptr->device = gx_device_reloc_ptr(gsvptr->device, gcst);
175
RELOC_PTR(gs_state, dfilter_stack);
180
/* Copy client data, using the copy_for procedure if available, */
181
/* the copy procedure otherwise. */
183
gstate_copy_client_data(gs_state * pgs, void *dto, void *dfrom,
184
gs_state_copy_reason_t reason)
186
return (pgs->client_procs.copy_for != 0 ?
187
(*pgs->client_procs.copy_for) (dto, dfrom, reason) :
188
(*pgs->client_procs.copy) (dto, dfrom));
191
/* ------ Operations on the entire graphics state ------ */
193
/* Define the initial value of the graphics state. */
194
static const gs_imager_state gstate_initial = {
195
gs_imager_state_initial(1.0)
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.
206
gstate_path_memory(gs_memory_t *mem)
208
return gs_memory_stable(mem);
211
/* Allocate and initialize a graphics state. */
213
gs_state_alloc(gs_memory_t * mem)
215
gs_state *pgs = gstate_alloc(mem, "gs_state_alloc", NULL);
221
*(gs_imager_state *)pgs = gstate_initial;
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.
228
code = gs_imager_state_initialize((gs_imager_state *) pgs, mem);
232
/* Finish initializing the color rendering state. */
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;
238
/* Initialize other things not covered by initgraphics */
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)");
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 */
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;
267
pgs->dfilter_stack = 0;
268
pgs->transparency_group_stack = 0;
269
if (gs_initgraphics(pgs) >= 0)
271
/* Something went very wrong. */
277
/* Set the client data in a graphics state. */
278
/* This should only be done to a newly created state. */
280
gs_state_set_client(gs_state * pgs, void *pdata,
281
const gs_state_client_procs * pprocs, bool client_has_pattern_streams)
283
pgs->client_data = pdata;
284
pgs->client_procs = *pprocs;
285
pgs->have_pattern_streams = client_has_pattern_streams;
288
/* Get the client data from a graphics state. */
289
#undef gs_state_client_data /* gzstate.h makes this a macro */
291
gs_state_client_data(const gs_state * pgs)
293
return pgs->client_data;
296
/* Free the chain of gstates.*/
298
gs_state_free_chain(gs_state * pgs)
300
gs_state *saved = pgs, *tmp;
304
gs_state_free(saved);
310
/* Free a graphics state. */
312
gs_state_free(gs_state * pgs)
314
gstate_free_contents(pgs);
315
gs_free_object(pgs->memory, pgs, "gs_state_free");
319
/* Save the graphics state. */
321
gs_gsave(gs_state * pgs)
323
gs_state *pnew = gstate_clone(pgs, pgs->memory, "gs_gsave",
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.
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.
337
rc_increment(pnew->dfilter_stack);
339
if (pgs->show_gstate == pgs)
340
pgs->show_gstate = pnew->show_gstate = pnew;
342
if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
343
(ulong) pnew, pgs->level);
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.
353
gs_gsave_for_save(gs_state * pgs, gs_state ** psaved)
356
gx_clip_path *old_cpath = pgs->view_clip;
357
gx_clip_path *new_cpath;
361
gx_cpath_alloc_shared(old_cpath, pgs->memory,
362
"gs_gsave_for_save(view_clip)");
364
return_error(gs_error_VMerror);
368
code = gs_gsave(pgs);
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;
380
gx_cpath_free(new_cpath, "gs_gsave_for_save(view_clip)");
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)
388
gs_state *saved = pgs->saved;
389
void *pdata = pgs->client_data;
391
gs_transparency_state_t *tstack = pgs->transparency_stack;
392
bool prior_overprint = pgs->overprint;
394
if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
395
(ulong) saved, pgs->level);
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);
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");
413
/* update the overprint compositor, if necessary */
414
if (prior_overprint != pgs->overprint)
415
return gs_do_set_overprint(pgs);
420
/* Restore the graphics state per PostScript semantics */
422
gs_grestore(gs_state * pgs)
426
return gs_gsave(pgs); /* shouldn't ever happen */
427
code = gs_grestore_only(pgs);
431
/* Wraparound: make sure there are always >= 1 saves on stack */
434
return gs_gsave(pgs);
437
/* Restore the graphics state for a 'restore', splicing the old stack */
438
/* back on. Note that we actually do a grestoreall + 2 grestores. */
440
gs_grestoreall_for_restore(gs_state * pgs, gs_state * saved)
444
while (pgs->saved->saved) {
445
code = gs_grestore(pgs);
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);
456
if (pgs->view_clip) {
457
gx_cpath_free(pgs->view_clip, "gs_grestoreall_for_restore");
460
return gs_grestore(pgs);
464
/* Restore to the bottommost graphics state (at this save level). */
466
gs_grestoreall(gs_state * pgs)
468
if (!pgs->saved) /* shouldn't happen */
469
return gs_gsave(pgs);
470
while (pgs->saved->saved) {
471
int code = gs_grestore(pgs);
476
return gs_grestore(pgs);
479
/* Allocate and return a new graphics state. */
481
gs_gstate(gs_state * pgs)
483
return gs_state_copy(pgs, pgs->memory);
486
gs_state_copy(gs_state * pgs, gs_memory_t * mem)
489
/* Prevent 'capturing' the view clip path. */
490
gx_clip_path *view_clip = pgs->view_clip;
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;
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.
507
(pgs->show_gstate == pgs ? pnew : 0);
511
/* Copy one previously allocated graphics state to another. */
513
gs_copygstate(gs_state * pto, const gs_state * pfrom)
515
return gstate_copy(pto, pfrom, copy_for_copygstate, "gs_copygstate");
518
/* Copy the current graphics state to a previously allocated one. */
520
gs_currentgstate(gs_state * pto, const gs_state * pgs)
523
gstate_copy(pto, pgs, copy_for_currentgstate, "gs_currentgstate");
530
/* Restore the current graphics state from a previously allocated one. */
532
gs_setgstate(gs_state * pgs, const gs_state * pfrom)
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.
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;
545
pgs->view_clip = 0; /* prevent refcount decrementing */
546
code = gstate_copy(pgs, pfrom, copy_for_setgstate, "gs_setgstate");
550
pgs->view_clip = view_clip;
552
(pgs->show_gstate == pfrom ? pgs : saved_show);
553
pgs->transparency_stack = tstack;
555
/* update the overprint compositor */
556
return gs_do_set_overprint(pgs);
559
/* Get the allocator pointer of a graphics state. */
560
/* This is provided only for the interpreter */
561
/* and for color space implementation. */
563
gs_state_memory(const gs_state * pgs)
568
/* Get the saved pointer of the graphics state. */
569
/* This is provided only for Level 2 grestore. */
571
gs_state_saved(const gs_state * pgs)
576
/* Swap the saved pointer of the graphics state. */
577
/* This is provided only for save/restore. */
579
gs_state_swap_saved(gs_state * pgs, gs_state * new_saved)
581
gs_state *saved = pgs->saved;
583
pgs->saved = new_saved;
587
/* Swap the memory pointer of the graphics state. */
588
/* This is provided only for the interpreter. */
590
gs_state_swap_memory(gs_state * pgs, gs_memory_t * mem)
592
gs_memory_t *memory = pgs->memory;
598
/* ------ Operations on components ------ */
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
607
gs_state_update_overprint(gs_state * pgs, const gs_overprint_params_t * pparams)
609
gs_composite_t * pct = 0;
610
gs_imager_state * pis = (gs_imager_state *)pgs;
612
gx_device * dev = pgs->device;
615
if ( (code = gs_create_overprint(&pct, pparams, pgs->memory)) >= 0 &&
616
(code = dev_proc(dev, create_compositor)( dev,
620
pgs->memory )) >= 0 ) {
622
gx_set_device_only(pgs, ovptdev);
625
gs_free_object(pgs->memory, pct, "gs_state_update_overprint");
627
/* the following hack handles devices that don't support compositors */
628
if (code == gs_error_unknownerror && !pparams->retain_any_comps)
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.
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).
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".
650
gs_do_set_overprint(gs_state * pgs)
652
const gs_color_space * pcs = pgs->color_space;
653
const gs_client_color * pcc = pgs->ccolor;
656
if (cs_num_components(pcs) < 0 && pcc->pattern != 0)
657
code = pcc->pattern->type->procs.set_color(pcc, pgs);
659
pcs->type->set_overprint(pcs, pgs);
665
gs_setoverprint(gs_state * pgs, bool ovp)
667
bool prior_ovp = pgs->overprint;
669
pgs->overprint = ovp;
670
if (prior_ovp != ovp)
671
(void)gs_do_set_overprint(pgs);
674
/* currentoverprint */
676
gs_currentoverprint(const gs_state * pgs)
678
return pgs->overprint;
681
/* setoverprintmode */
683
gs_setoverprintmode(gs_state * pgs, int mode)
685
int prior_mode = pgs->effective_overprint_mode;
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);
696
/* currentoverprintmode */
698
gs_currentoverprintmode(const gs_state * pgs)
700
return pgs->overprint_mode;
703
/* setrenderingintent
705
* Use ICC numbers from Table 18 (section 6.1.11) rather than the PDF order
706
* to reduce re-coding and confusion.
708
* Relative Colorimetric 1
710
* AbsoluteColorimetric 3
713
gs_setrenderingintent(gs_state *pgs, int ri) {
714
if (ri < 0 || ri > 3)
715
return_error(gs_error_rangecheck);
716
pgs->renderingintent = ri;
720
/* currentrenderingintent */
722
gs_currentrenderingintent(const gs_state * pgs)
724
return pgs->renderingintent;
728
* Reset most of the graphics state.
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.
736
gs_initgraphics(gs_state * 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
760
gs_setfilladjust(gs_state * pgs, floatp adjust_x, floatp adjust_y)
762
#define CLAMP_TO_HALF(v)\
763
((v) <= 0 ? fixed_0 : (v) >= 0.5 ? fixed_half : float2fixed(v));
765
pgs->fill_adjust.x = CLAMP_TO_HALF(adjust_x);
766
pgs->fill_adjust.y = CLAMP_TO_HALF(adjust_y);
771
/* currentfilladjust */
773
gs_currentfilladjust(const gs_state * pgs, gs_point * adjust)
775
adjust->x = fixed2float(pgs->fill_adjust.x);
776
adjust->y = fixed2float(pgs->fill_adjust.y);
782
gs_setlimitclamp(gs_state * pgs, bool clamp)
784
pgs->clamp_coordinates = clamp;
787
/* currentlimitclamp */
789
gs_currentlimitclamp(const gs_state * pgs)
791
return pgs->clamp_coordinates;
794
/* settextrenderingmode */
796
gs_settextrenderingmode(gs_state * pgs, uint trm)
798
pgs->text_rendering_mode = trm;
801
/* currenttextrenderingmode */
803
gs_currenttextrenderingmode(const gs_state * pgs)
805
return pgs->text_rendering_mode;
808
/* ------ Internal routines ------ */
810
/* Free the privately allocated parts of a gstate. */
812
gstate_free_parts(const gs_state * parts, gs_memory_t * mem, client_name_t cname)
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);
822
/* Allocate the privately allocated parts of a gstate. */
824
gstate_alloc_parts(gs_state * parts, const gs_state * shared,
825
gs_memory_t * mem, client_name_t cname)
827
gs_memory_t *path_mem = gstate_path_memory(mem);
831
gx_path_alloc_shared(shared->path, path_mem,
832
"gstate_alloc_parts(path)") :
833
gx_path_alloc(path_mem, "gstate_alloc_parts(path)"));
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;
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;
848
parts->color_space = NULL;
850
gs_alloc_struct(mem, gs_client_color, &st_client_color, cname);
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
857
gstate_free_parts(parts, mem, cname);
858
return_error(gs_error_VMerror);
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).
870
gstate_alloc(gs_memory_t * mem, client_name_t cname, const gs_state * pfrom)
873
gs_alloc_struct(mem, gs_state, &st_gs_state, cname);
877
if (gstate_alloc_parts(pgs, pfrom, mem, cname) < 0) {
878
gs_free_object(mem, pgs, cname);
885
/* Copy the dash pattern from one gstate to another. */
887
gstate_copy_dash(gs_state * pto, const gs_state * pfrom)
889
return gs_setdash(pto, pfrom->line_params.dash.pattern,
890
pfrom->line_params.dash.pattern_size,
891
pfrom->line_params.dash.offset);
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. */
899
gstate_clone(gs_state * pfrom, gs_memory_t * mem, client_name_t cname,
900
gs_state_copy_reason_t reason)
902
gs_state *pgs = gstate_alloc(mem, cname, pfrom);
903
gs_state_parts parts;
907
GSTATE_ASSIGN_PARTS(&parts, pgs);
909
pgs->transparency_stack = 0;
910
/* Copy the dash pattern if necessary. */
911
if (pgs->line_params.dash.pattern) {
914
pgs->line_params.dash.pattern = 0; /* force allocation */
915
code = gstate_copy_dash(pgs, pfrom);
919
if (pgs->client_data != 0) {
920
void *pdata = pgs->client_data = (*pgs->client_procs.alloc) (mem);
923
gstate_copy_client_data(pgs, pdata, pfrom->client_data, reason) < 0
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;
936
GSTATE_ASSIGN_PARTS(pfrom, &parts);
937
pgs->line_params.dash.pattern = dfrom;
938
pfrom->line_params.dash.pattern = dto;
940
GSTATE_ASSIGN_PARTS(pgs, &parts);
942
cs_adjust_counts(pgs, 1);
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);
953
/* Adjust reference counters for the whole clip stack */
954
/* accessible from the given point */
956
clip_stack_rc_adjust(gx_clip_stack_t *cs, int delta, client_name_t cname)
958
gx_clip_stack_t *p = cs;
961
gx_clip_stack_t *q = p;
963
rc_adjust(q, delta, cname);
967
/* Release the composite parts of a graphics state, */
968
/* but not the state itself. */
970
gstate_free_contents(gs_state * pgs)
972
gs_memory_t *mem = pgs->memory;
973
const char *const cname = "gstate_free_contents";
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);
986
/* Copy one gstate to another. */
988
gstate_copy(gs_state * pto, const gs_state * pfrom,
989
gs_state_copy_reason_t reason, client_name_t cname)
991
gs_state_parts parts;
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);
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.
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);
1011
* effective_clip_shared will be copied, but we need to do the
1012
* right thing with effective_clip_path.
1014
if (pfrom->effective_clip_shared) {
1016
* pfrom->effective_clip_path is either pfrom->view_clip or
1019
parts.effective_clip_path =
1020
(pfrom->effective_clip_path == pfrom->view_clip ?
1021
pto->view_clip : parts.clip_path);
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);
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;
1041
gs_imager_state_pre_assign((gs_imager_state *)pto,
1042
(const gs_imager_state *)pfrom);
1044
pto->client_data = pdata;
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);
1056
GSTATE_ASSIGN_PARTS(pto, &parts);
1057
cs_adjust_counts(pto, 1);
1059
(pfrom->show_gstate == pfrom ? pto : 0);
1064
gs_id gx_get_clip_path_id(gs_state *pgs)
1066
return pgs->clip_path->id;