Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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