1 /* Copyright (C) 2001-2019 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,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
13 CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15
16
17 /* Miscellaneous graphics state operators for Ghostscript library */
18 #include "gx.h"
19 #include "memory_.h"
20 #include "gserrors.h"
21 #include "gsstruct.h"
22 #include "gsutil.h" /* for gs_next_ids */
23 #include "gzstate.h"
24 #include "gxcspace.h" /* here for gscolor2.h */
25 #include "gsalpha.h"
26 #include "gscolor2.h"
27 #include "gscoord.h" /* for gs_initmatrix */
28 #include "gscie.h"
29 #include "gxclipsr.h"
30 #include "gxcmap.h"
31 #include "gxdevice.h"
32 #include "gxpcache.h"
33 #include "gzht.h"
34 #include "gzline.h"
35 #include "gspath.h"
36 #include "gzpath.h"
37 #include "gzcpath.h"
38 #include "gsovrc.h"
39 #include "gxcolor2.h"
40 #include "gxpcolor.h"
41 #include "gsicc_manage.h"
42
43 /* Forward references */
44 static gs_gstate *gstate_alloc(gs_memory_t *, client_name_t,
45 const gs_gstate *);
46 static gs_gstate *gstate_clone(gs_gstate *, gs_memory_t *, client_name_t,
47 gs_gstate_copy_reason_t);
48 static void gstate_free_contents(gs_gstate *);
49 static int gstate_copy(gs_gstate *, const gs_gstate *,
50 gs_gstate_copy_reason_t, client_name_t);
51 static void clip_stack_rc_adjust(gx_clip_stack_t *cs, int delta, client_name_t cname);
52
53 /*
54 * Graphics state storage management is complicated. There are many
55 * different classes of storage associated with a graphics state:
56 *
57 * (1) The gstate object itself. This includes some objects physically
58 * embedded within the gstate object, but because of garbage collection
59 * requirements, there are no embedded objects that can be
60 * referenced by non-transient pointers. We assume that the gstate
61 * stack "owns" its gstates and that we can free the top gstate when
62 * doing a restore.
63 *
64 * (2) Objects that are referenced directly by the gstate and whose lifetime
65 * is independent of the gstate. These are garbage collected, not
66 * reference counted, so we don't need to do anything special with them
67 * when manipulating gstates. Currently this includes:
68 * font
69 *
70 * (3) Objects that are referenced directly by the gstate, may be shared
71 * among gstates, and should disappear when no gstates reference them.
72 * These fall into two groups:
73 *
74 * (3a) Objects that are logically connected to individual gstates.
75 * We use reference counting to manage these. Currently these are:
76 * halftone, dev_ht, cie_render, black_generation,
77 * undercolor_removal, set_transfer.*, cie_joint_caches,
78 * clip_stack, {opacity,shape}.mask
79 * effective_transfer.* may point to some of the same objects as
80 * set_transfer.*, but don't contribute to the reference count.
81 * Similarly, dev_color may point to the dev_ht object. For
82 * simplicity, we initialize all of these pointers to 0 and then
83 * allocate the object itself when needed.
84 *
85 * (3b) Objects whose lifetimes are associated with something else.
86 * Currently these are:
87 * pattern_cache, which is associated with the entire
88 * stack, is allocated when first needed, and currently
89 * is never freed;
90 * view_clip, which is associated with the current
91 * save level (effectively, with the gstate sub-stack
92 * back to the save) and is managed specially;
93 *
94 * (4) Objects that are referenced directly by exactly one gstate and that
95 * are not referenced (except transiently) from any other object.
96 * These fall into two groups:
97 *
98 * (4b) Objects allocated individually, for the given reason:
99 * line_params.dash.pattern (variable-length),
100 * color_space, path, clip_path, effective_clip.path,
101 * ccolor, dev_color
102 * (may be referenced from image enumerators or elsewhere)
103 *
104 * (4b) The "client data" for a gstate. For the interpreter, this is
105 * the refs associated with the gstate, such as the screen procedures.
106 * Client-supplied procedures manage client data.
107 *
108 * (5) Objects referenced indirectly from gstate objects of category (4),
109 * including objects that may also be referenced directly by the gstate.
110 * The individual routines that manipulate these are responsible
111 * for doing the right kind of reference counting or whatever.
112 * Currently:
113 * devices, path, clip_path, and (if different from both clip_path
114 * and view_clip) effective_clip.path require
115 * gx_path_assign/free, which uses a reference count;
116 * color_space and ccolor require cs_adjust_color/cspace_count
117 * or cs_adjust_counts, which use a reference count;
118 * dev_color has no references to storage that it owns.
119 * We count on garbage collection or restore to deallocate
120 * sub-objects of halftone.
121 *
122 * Note that when after a gsave, the existing gstate references the related
123 * objects that we allocate at the same time, and the newly allocated gstate
124 * references the old related objects. Similarly, during a grestore, we
125 * free the related objects referenced by the current gstate, but after the
126 * grestore, we free the saved gstate, not the current one. However, when
127 * we allocate gstates off-stack, the newly allocated gstate does reference
128 * the newly allocated component objects. Note also that setgstate /
129 * currentgstate may produce gstates in which different allocators own
130 * different sub-objects; this is OK, because restore guarantees that there
131 * won't be any dangling pointers (as long as we don't allow pointers from
132 * global gstates to local objects).
133 */
134
135 /*
136 * Define these elements of the graphics state that are allocated
137 * individually for each state, except for line_params.dash.pattern.
138 * Note that effective_clip_shared is not on the list.
139 */
140 typedef struct gs_gstate_parts_s {
141 gx_path *path;
142 gx_clip_path *clip_path;
143 gx_clip_path *effective_clip_path;
144 struct {
145 gs_client_color *ccolor;
146 gx_device_color *dev_color;
147 } color[2];
148 } gs_gstate_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[0].ccolor = (pfrom)->color[0].ccolor,\
154 (pto)->color[0].dev_color = (pfrom)->color[0].dev_color,\
155 (pto)->color[1].ccolor = (pfrom)->color[1].ccolor,\
156 (pto)->color[1].dev_color = (pfrom)->color[1].dev_color)
157
158 extern_st(st_gs_gstate); /* for gstate_alloc() */
159
160 /* Copy client data, using the copy_for procedure if available, */
161 /* the copy procedure otherwise. */
162 static int
gstate_copy_client_data(gs_gstate * pgs,void * dto,void * dfrom,gs_gstate_copy_reason_t reason)163 gstate_copy_client_data(gs_gstate * pgs, void *dto, void *dfrom,
164 gs_gstate_copy_reason_t reason)
165 {
166 return (pgs->client_procs.copy_for != 0 ?
167 (*pgs->client_procs.copy_for) (dto, dfrom, reason) :
168 (*pgs->client_procs.copy) (dto, dfrom));
169 }
170
171 /* ------ Operations on the entire graphics state ------ */
172
173 /*
174 * Allocate a path for the graphics state. We use stable memory because
175 * some PostScript files have Type 3 fonts whose BuildChar procedure
176 * uses the sequence save ... setcachedevice ... restore, and the path
177 * built between the setcachedevice and the restore must not be freed.
178 * If it weren't for this, we don't think stable memory would be needed.
179 */
180 static gs_memory_t *
gstate_path_memory(gs_memory_t * mem)181 gstate_path_memory(gs_memory_t *mem)
182 {
183 return gs_memory_stable(mem);
184 }
185
186 /* Allocate and initialize a graphics state. */
187 gs_gstate *
gs_gstate_alloc(gs_memory_t * mem)188 gs_gstate_alloc(gs_memory_t * mem)
189 {
190 gs_gstate *pgs = gstate_alloc(mem, "gs_gstate_alloc", NULL);
191 gs_memory_t *path_mem = gstate_path_memory(mem);
192 int code;
193
194 if (pgs == 0)
195 return 0;
196 GS_STATE_INIT_VALUES(pgs, 1.0);
197 /* Need to set up at least enough to make gs_gstate_free happy */
198 pgs->saved = 0;
199 pgs->clip_stack = NULL;
200 pgs->view_clip = NULL;
201 pgs->font = NULL;
202 pgs->root_font = NULL;
203 pgs->show_gstate = NULL;
204 pgs->device = NULL;
205
206 /*
207 * Just enough of the state is initialized at this point
208 * that it's OK to call gs_gstate_free if an allocation fails.
209 */
210
211 code = gs_gstate_initialize(pgs, mem);
212 if (code < 0)
213 goto fail;
214
215 /* Finish initializing the color rendering state. */
216
217 rc_alloc_struct_1(pgs->halftone, gs_halftone, &st_halftone, mem,
218 goto fail, "gs_gstate_alloc(halftone)");
219 pgs->halftone->type = ht_type_none;
220
221 /* Initialize other things not covered by initgraphics */
222
223 pgs->clip_stack = 0;
224 pgs->view_clip = gx_cpath_alloc(path_mem, "gs_gstate_alloc(view_clip)");
225 if (pgs->view_clip == NULL)
226 goto fail;
227 pgs->view_clip->rule = 0; /* no clipping */
228 pgs->effective_clip_id = pgs->clip_path->id;
229 pgs->effective_view_clip_id = gs_no_id;
230 pgs->in_cachedevice = 0;
231 pgs->device = 0; /* setting device adjusts refcts */
232 code = gs_nulldevice(pgs);
233 if (code < 0)
234 goto fail;
235 gs_setalpha(pgs, 1.0);
236 gs_setfillconstantalpha(pgs, 1.0);
237 gs_setstrokeconstantalpha(pgs, 1.0);
238 gs_setalphaisshape(pgs, false);
239 gs_settransfer(pgs, gs_identity_transfer);
240 gs_setflat(pgs, 1.0);
241 gs_setfilladjust(pgs, 0.3, 0.3);
242 gs_setlimitclamp(pgs, false);
243 gs_setstrokeadjust(pgs, true);
244 pgs->font = 0; /* Not right, but acceptable until the */
245 /* PostScript code does the first setfont. */
246 pgs->root_font = 0; /* ditto */
247 pgs->in_charpath = (gs_char_path_mode) 0;
248 pgs->show_gstate = 0;
249 pgs->level = 0;
250 if (gs_initgraphics(pgs) >= 0)
251 return pgs;
252 /* Something went very wrong. */
253 fail:
254 gs_gstate_free(pgs);
255 return 0;
256 }
257
258 /* Set the client data in a graphics state. */
259 /* This should only be done to a newly created state. */
260 void
gs_gstate_set_client(gs_gstate * pgs,void * pdata,const gs_gstate_client_procs * pprocs,bool client_has_pattern_streams)261 gs_gstate_set_client(gs_gstate * pgs, void *pdata,
262 const gs_gstate_client_procs * pprocs, bool client_has_pattern_streams)
263 {
264 pgs->client_data = pdata;
265 pgs->client_procs = *pprocs;
266 pgs->have_pattern_streams = client_has_pattern_streams;
267 }
268
269 /* Get the client data from a graphics state. */
270 #undef gs_gstate_client_data /* gzstate.h makes this a macro */
271 void *
gs_gstate_client_data(const gs_gstate * pgs)272 gs_gstate_client_data(const gs_gstate * pgs)
273 {
274 return pgs->client_data;
275 }
276
277 /* Free the chain of gstates.*/
278 int
gs_gstate_free_chain(gs_gstate * pgs)279 gs_gstate_free_chain(gs_gstate * pgs)
280 {
281 gs_gstate *saved = pgs, *tmp;
282
283 while(saved != 0) {
284 tmp = saved->saved;
285 gs_gstate_free(saved);
286 saved = tmp;
287 }
288 return 0;
289 }
290
291 /* Free a graphics state. */
292 int
gs_gstate_free(gs_gstate * pgs)293 gs_gstate_free(gs_gstate * pgs)
294 {
295 gstate_free_contents(pgs);
296 gs_free_object(pgs->memory, pgs, "gs_gstate_free");
297 return 0;
298 }
299
300 /* Save the graphics state. */
301 int
gs_gsave(gs_gstate * pgs)302 gs_gsave(gs_gstate * pgs)
303 {
304 gs_gstate *pnew = gstate_clone(pgs, pgs->memory, "gs_gsave",
305 copy_for_gsave);
306
307 if (pnew == 0)
308 return_error(gs_error_VMerror);
309 /* As of PLRM3, the interaction between gsave and the clip stack is
310 * now clear. gsave stores the clip stack into the saved graphics
311 * state, but then clears it in the current graphics state.
312 *
313 * Ordinarily, reference count rules would indicate an rc_decrement()
314 * on pgs->clip_stack, but gstate_clone() has an exception for
315 * the clip_stack field.
316 */
317 pgs->clip_stack = 0;
318 pgs->saved = pnew;
319 if (pgs->show_gstate == pgs)
320 pgs->show_gstate = pnew->show_gstate = pnew;
321 pgs->level++;
322 if_debug2m('g', pgs->memory, "[g]gsave -> 0x%lx, level = %d\n",
323 (ulong) pnew, pgs->level);
324 return 0;
325 }
326
327 /*
328 * Save the graphics state for a 'save'.
329 * We cut the stack below the new gstate, and return the old one.
330 * In addition to an ordinary gsave, we create a new view clip path.
331 */
332 int
gs_gsave_for_save(gs_gstate * pgs,gs_gstate ** psaved)333 gs_gsave_for_save(gs_gstate * pgs, gs_gstate ** psaved)
334 {
335 int code;
336 gx_clip_path *old_cpath = pgs->view_clip;
337 gx_clip_path *new_cpath;
338
339 if (old_cpath) {
340 new_cpath =
341 gx_cpath_alloc_shared(old_cpath, pgs->memory,
342 "gs_gsave_for_save(view_clip)");
343 if (new_cpath == 0)
344 return_error(gs_error_VMerror);
345 } else {
346 new_cpath = 0;
347 }
348 code = gs_gsave(pgs);
349 if (code < 0)
350 goto fail;
351 if (pgs->effective_clip_path == pgs->view_clip)
352 pgs->effective_clip_path = new_cpath;
353 pgs->view_clip = new_cpath;
354 /* Cut the stack so we can't grestore past here. */
355 *psaved = pgs->saved;
356 pgs->saved = 0;
357
358 code = gs_gsave(pgs);
359 if (code < 0) {
360 pgs->saved = *psaved;
361 *psaved = NULL;
362 gs_grestore(pgs);
363 return code;
364 }
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 */
gs_grestore_only(gs_gstate * pgs)374 gs_grestore_only(gs_gstate * pgs)
375 {
376 gs_gstate *saved = pgs->saved;
377 gs_gstate tmp_gstate;
378 void *pdata = pgs->client_data;
379 void *sdata;
380
381 if_debug2m('g', pgs->memory, "[g]grestore 0x%lx, level was %d\n",
382 (ulong) saved, pgs->level);
383 if (!saved)
384 return 1;
385 sdata = saved->client_data;
386 if (saved->pattern_cache == 0)
387 saved->pattern_cache = pgs->pattern_cache;
388 /* Swap back the client data pointers. */
389 pgs->client_data = sdata;
390 saved->client_data = pdata;
391 if (pdata != 0 && sdata != 0)
392 gstate_copy_client_data(pgs, pdata, sdata, copy_for_grestore);
393 gstate_free_contents(pgs);
394 tmp_gstate = *pgs; /* temp after contents freed (with pointers zeroed) */
395 *pgs = *saved;
396 if (pgs->show_gstate == saved)
397 pgs->show_gstate = pgs;
398 *saved = tmp_gstate; /* restore "freed" state (pointers zeroed after contents freed) */
399 gs_free_object(pgs->memory, saved, "gs_grestore");
400
401 return 0;
402 }
403
404 /* Restore the graphics state per PostScript semantics */
405 int
gs_grestore(gs_gstate * pgs)406 gs_grestore(gs_gstate * pgs)
407 {
408 int code;
409 if (!pgs->saved)
410 return gs_gsave(pgs); /* shouldn't ever happen */
411 code = gs_grestore_only(pgs);
412 if (code < 0)
413 return code;
414
415 /* Wraparound: make sure there are always >= 1 saves on stack */
416 if (pgs->saved)
417 return 0;
418 return gs_gsave(pgs);
419 }
420
421 /* Restore the graphics state for a 'restore', splicing the old stack */
422 /* back on. Note that we actually do a grestoreall + 2 grestores. */
423 int
gs_grestoreall_for_restore(gs_gstate * pgs,gs_gstate * saved)424 gs_grestoreall_for_restore(gs_gstate * pgs, gs_gstate * saved)
425 {
426 int code;
427
428 while (pgs->saved->saved) {
429 code = gs_grestore(pgs);
430 if (code < 0)
431 return code;
432 }
433 /* Make sure we don't leave dangling pointers in the caches. */
434 if (pgs->pattern_cache)
435 (*pgs->pattern_cache->free_all) (pgs->pattern_cache);
436 pgs->saved->saved = saved;
437 code = gs_grestore(pgs);
438 if (code < 0)
439 return code;
440 if (pgs->view_clip) {
441 gx_cpath_free(pgs->view_clip, "gs_grestoreall_for_restore");
442 pgs->view_clip = 0;
443 }
444 return gs_grestore(pgs);
445 }
446
447 /* Restore to the bottommost graphics state (at this save level). */
448 int
gs_grestoreall(gs_gstate * pgs)449 gs_grestoreall(gs_gstate * pgs)
450 {
451 if (!pgs->saved) /* shouldn't happen */
452 return gs_gsave(pgs);
453 while (pgs->saved->saved) {
454 int code = gs_grestore(pgs);
455
456 if (code < 0)
457 return code;
458 }
459 return gs_grestore(pgs);
460 }
461
462 /* Allocate and return a new graphics state. */
463 gs_gstate *
gs_gstate_copy(gs_gstate * pgs,gs_memory_t * mem)464 gs_gstate_copy(gs_gstate * pgs, gs_memory_t * mem)
465 {
466 gs_gstate *pnew;
467 /* Prevent 'capturing' the view clip path. */
468 gx_clip_path *view_clip = pgs->view_clip;
469
470 pgs->view_clip = 0;
471 pnew = gstate_clone(pgs, mem, "gs_gstate", copy_for_gstate);
472 if (pnew == 0)
473 return 0;
474 clip_stack_rc_adjust(pnew->clip_stack, 1, "gs_gstate_copy");
475 pgs->view_clip = view_clip;
476 pnew->saved = 0;
477 /*
478 * Prevent dangling references from the show_gstate pointer. If
479 * this context is its own show_gstate, set the pointer in the clone
480 * to point to the clone; otherwise, set the pointer in the clone to
481 * 0, and let gs_setgstate fix it up.
482 */
483 pnew->show_gstate =
484 (pgs->show_gstate == pgs ? pnew : 0);
485 return pnew;
486 }
487
488 /* Copy one previously allocated graphics state to another. */
489 int
gs_copygstate(gs_gstate * pto,const gs_gstate * pfrom)490 gs_copygstate(gs_gstate * pto, const gs_gstate * pfrom)
491 {
492 return gstate_copy(pto, pfrom, copy_for_copygstate, "gs_copygstate");
493 }
494
495 /* Copy the current graphics state to a previously allocated one. */
496 int
gs_currentgstate(gs_gstate * pto,const gs_gstate * pgs)497 gs_currentgstate(gs_gstate * pto, const gs_gstate * pgs)
498 {
499 int code =
500 gstate_copy(pto, pgs, copy_for_currentgstate, "gs_currentgstate");
501
502 if (code >= 0)
503 pto->view_clip = 0;
504 return code;
505 }
506
507 /* Restore the current graphics state from a previously allocated one. */
508 int
gs_setgstate(gs_gstate * pgs,const gs_gstate * pfrom)509 gs_setgstate(gs_gstate * pgs, const gs_gstate * pfrom)
510 {
511 /*
512 * The implementation is the same as currentgstate,
513 * except we must preserve the saved pointer, the level,
514 * the view clip, and possibly the show_gstate.
515 */
516 gs_gstate *saved_show = pgs->show_gstate;
517 int level = pgs->level;
518 gx_clip_path *view_clip = pgs->view_clip;
519 int code;
520
521 pgs->view_clip = 0; /* prevent refcount decrementing */
522 code = gstate_copy(pgs, pfrom, copy_for_setgstate, "gs_setgstate");
523 if (code < 0)
524 return code;
525 pgs->level = level;
526 pgs->view_clip = view_clip;
527 pgs->show_gstate =
528 (pgs->show_gstate == pfrom ? pgs : saved_show);
529 return 0;
530 }
531
532 /* Get the allocator pointer of a graphics state. */
533 /* This is provided only for the interpreter */
534 /* and for color space implementation. */
535 gs_memory_t *
gs_gstate_memory(const gs_gstate * pgs)536 gs_gstate_memory(const gs_gstate * pgs)
537 {
538 return pgs->memory;
539 }
540
541 /* Get the saved pointer of the graphics state. */
542 /* This is provided only for Level 2 grestore. */
543 gs_gstate *
gs_gstate_saved(const gs_gstate * pgs)544 gs_gstate_saved(const gs_gstate * pgs)
545 {
546 return pgs->saved;
547 }
548
549 /* Swap the saved pointer of the graphics state. */
550 /* This is provided only for save/restore. */
551 gs_gstate *
gs_gstate_swap_saved(gs_gstate * pgs,gs_gstate * new_saved)552 gs_gstate_swap_saved(gs_gstate * pgs, gs_gstate * new_saved)
553 {
554 gs_gstate *saved = pgs->saved;
555
556 pgs->saved = new_saved;
557 return saved;
558 }
559
560 /* Swap the memory pointer of the graphics state. */
561 /* This is provided only for the interpreter. */
562 gs_memory_t *
gs_gstate_swap_memory(gs_gstate * pgs,gs_memory_t * mem)563 gs_gstate_swap_memory(gs_gstate * pgs, gs_memory_t * mem)
564 {
565 gs_memory_t *memory = pgs->memory;
566
567 pgs->memory = mem;
568 return memory;
569 }
570
571 /* ------ Operations on components ------ */
572
573 /*
574 * Push an overprint compositor onto the current device. Note that if
575 * the current device already is an overprint compositor, the
576 * create_compositor will update its parameters but not create a new
577 * compositor device.
578 */
579 int
gs_gstate_update_overprint(gs_gstate * pgs,const gs_overprint_params_t * pparams)580 gs_gstate_update_overprint(gs_gstate * pgs, const gs_overprint_params_t * pparams)
581 {
582 gs_composite_t * pct = 0;
583 int code;
584 gx_device * dev = pgs->device;
585 gx_device * ovptdev;
586
587 code = gs_create_overprint(&pct, pparams, pgs->memory);
588 if (code >= 0) {
589 code = dev_proc(dev, create_compositor)( dev,
590 &ovptdev,
591 pct,
592 pgs,
593 pgs->memory,
594 NULL);
595 if (code >= 0 || code == gs_error_handled){
596 if (ovptdev != dev)
597 gx_set_device_only(pgs, ovptdev);
598 code = 0;
599 }
600 }
601 if (pct != 0)
602 gs_free_object(pgs->memory, pct, "gs_gstate_update_overprint");
603
604 /* the following hack handles devices that don't support compositors */
605 if (code == gs_error_unknownerror && !pparams->retain_any_comps)
606 code = 0;
607 return code;
608 }
609
610 /*
611 * Reset the overprint mode for the current color space and color. This
612 * routine should be called whenever the current device (i.e.: color
613 * model), overprint, overprint mode, color space, or color are modified.
614 *
615 * The need reason this routine must be called for changes in the current
616 * color and must consider the current color involves the Pattern color
617 * space. In that space, the "color" (pattern) can determine if the base
618 * color space is used (PatternType 1 with PaintType 2), or may provide
619 * is own color space (PatternType 1 with PaintType 1, PatternType 2).
620 *
621 * The most general situation (PatternType 1 with PaintType 1) cannot be
622 * handled properly due to limitations of the pattern cache mechanism,
623 * so in this case overprint is effectively disable by making all color
624 * components "drawn".
625 */
626 int
gs_do_set_overprint(gs_gstate * pgs)627 gs_do_set_overprint(gs_gstate * pgs)
628 {
629 const gs_color_space * pcs = gs_currentcolorspace_inline(pgs);
630 const gs_client_color * pcc = gs_currentcolor_inline(pgs);
631 int code = 0;
632
633 if (cs_num_components(pcs) < 0 && pcc->pattern != 0)
634 code = pcc->pattern->type->procs.set_color(pcc, pgs);
635 else {
636 gx_device* dev = pgs->device;
637 cmm_dev_profile_t* dev_profile;
638
639 dev_proc(dev, get_profile)(dev, &dev_profile);
640 if (!dev_profile->sim_overprint || dev_profile->device_profile[0]->data_cs != gsCMYK)
641 return code;
642
643 /* The spaces that do not allow opm (e.g. ones that are not ICC or DeviceCMYK)
644 will blow away any true setting later. But we have to be prepared
645 in case this is a CMYK ICC space for example. Hence we set effective mode
646 to mode here (Bug 698721)*/
647 pgs->color[0].effective_opm = pgs->overprint_mode;
648
649 if_debug2m(gs_debug_flag_overprint, pgs->memory,
650 "[overprint] gs_do_set_overprint. Preset effective mode. pgs->color[0].effective_opm = %d pgs->color[1].effective_opm = %d\n",
651 pgs->color[0].effective_opm, pgs->color[1].effective_opm);
652
653 pcs->type->set_overprint(pcs, pgs);
654 }
655 return code;
656 }
657
658 /* setoverprint (non-stroke case) interpreter code
659 ensures that this is called when appropriate. This
660 should only be coming when we are doing PS files.
661 As they don't have separate stroke and fill overprint
662 controls */
663 void
gs_setoverprint(gs_gstate * pgs,bool ovp)664 gs_setoverprint(gs_gstate * pgs, bool ovp)
665 {
666 pgs->overprint = ovp;
667 pgs->stroke_overprint = ovp;
668 }
669
670 /* currentoverprint */
671 bool
gs_currentoverprint(const gs_gstate * pgs)672 gs_currentoverprint(const gs_gstate * pgs)
673 {
674 return pgs->overprint;
675 }
676
677 /* setstrokeoverprint */
678 void
gs_setstrokeoverprint(gs_gstate * pgs,bool ovp)679 gs_setstrokeoverprint(gs_gstate * pgs, bool ovp)
680 {
681 pgs->stroke_overprint = ovp;
682 }
683
684 /* currentstrokeoverprint */
685 bool
gs_currentstrokeoverprint(const gs_gstate * pgs)686 gs_currentstrokeoverprint(const gs_gstate * pgs)
687 {
688 return pgs->stroke_overprint;
689 }
690
691 /* setstrokeoverprint */
692 void
gs_setfilloverprint(gs_gstate * pgs,bool ovp)693 gs_setfilloverprint(gs_gstate * pgs, bool ovp)
694 {
695 pgs->overprint = ovp;
696 }
697
698 /* currentstrokeoverprint */
699 bool
gs_currentfilloverprint(const gs_gstate * pgs)700 gs_currentfilloverprint(const gs_gstate * pgs)
701 {
702 return pgs->overprint;
703 }
704
705 /* setoverprintmode */
706 int
gs_setoverprintmode(gs_gstate * pgs,int mode)707 gs_setoverprintmode(gs_gstate * pgs, int mode)
708 {
709 if (mode < 0 || mode > 1)
710 return_error(gs_error_rangecheck);
711 pgs->overprint_mode = mode;
712
713 return 0;
714 }
715
716 /* currentoverprintmode */
717 int
gs_currentoverprintmode(const gs_gstate * pgs)718 gs_currentoverprintmode(const gs_gstate * pgs)
719 {
720 return pgs->overprint_mode;
721 }
722
723 void
gs_setcpsimode(gs_memory_t * mem,bool mode)724 gs_setcpsimode(gs_memory_t *mem, bool mode)
725 {
726 gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(mem);
727
728 libctx->core->CPSI_mode = mode;
729 }
730
731 /* currentcpsimode */
732 bool
gs_currentcpsimode(const gs_memory_t * mem)733 gs_currentcpsimode(const gs_memory_t * mem)
734 {
735 gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(mem);
736
737 return libctx->core->CPSI_mode;
738 }
739
740 /* The edgebuffer based scanconverter can only cope with values of 0
741 * or 0.5 (i.e. 'center of pixel' or 'any part of pixel'). These
742 * are the only values required for correct behaviour according to
743 * the PDF and PS specs. Therefore, if we are using the edgebuffer
744 * based scan converter, force these values. */
745 static void
sanitize_fill_adjust(gs_gstate * pgs)746 sanitize_fill_adjust(gs_gstate * pgs)
747 {
748 int scanconverter = gs_getscanconverter(pgs->memory);
749 if (scanconverter >= GS_SCANCONVERTER_EDGEBUFFER || (GS_SCANCONVERTER_DEFAULT_IS_EDGEBUFFER && scanconverter == GS_SCANCONVERTER_DEFAULT)) {
750 fixed adjust = (pgs->fill_adjust.x >= float2fixed(0.25) || pgs->fill_adjust.y >= float2fixed(0.25) ? fixed_half : 0);
751 pgs->fill_adjust.x = adjust;
752 pgs->fill_adjust.y = adjust;
753 }
754 }
755
756 void
gs_setscanconverter(gs_gstate * gs,int converter)757 gs_setscanconverter(gs_gstate * gs, int converter)
758 {
759 gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(gs->memory);
760
761 libctx->core->scanconverter = converter;
762
763 sanitize_fill_adjust(gs);
764 }
765
766 /* getscanconverter */
767 int
gs_getscanconverter(const gs_memory_t * mem)768 gs_getscanconverter(const gs_memory_t * mem)
769 {
770 gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(mem);
771
772 return libctx->core->scanconverter;
773 }
774
775 /* setrenderingintent
776 *
777 * Use ICC numbers from Table 18 (section 6.1.11) rather than the PDF order
778 * to reduce re-coding and confusion.
779 * Perceptual 0
780 * Relative Colorimetric 1
781 * Saturation 2
782 * AbsoluteColorimetric 3
783 */
784 int
gs_setrenderingintent(gs_gstate * pgs,int ri)785 gs_setrenderingintent(gs_gstate *pgs, int ri) {
786 if (ri < 0 || ri > 3)
787 return_error(gs_error_rangecheck);
788 pgs->renderingintent = ri;
789 return 0;
790 }
791
792 /* currentrenderingintent */
793 int
gs_currentrenderingintent(const gs_gstate * pgs)794 gs_currentrenderingintent(const gs_gstate * pgs)
795 {
796 return pgs->renderingintent;
797 }
798
799 int
gs_setblackptcomp(gs_gstate * pgs,bool bkpt)800 gs_setblackptcomp(gs_gstate *pgs, bool bkpt) {
801 pgs->blackptcomp = bkpt;
802 return 0;
803 }
804
805 /* currentrenderingintent */
806 bool
gs_currentblackptcomp(const gs_gstate * pgs)807 gs_currentblackptcomp(const gs_gstate * pgs)
808 {
809 return pgs->blackptcomp;
810 }
811
812 /*
813 * Reset most of the graphics state.
814 */
815 int
gs_initgraphics(gs_gstate * pgs)816 gs_initgraphics(gs_gstate * pgs)
817 {
818 int code;
819 const gs_gstate gstate_initial = {
820 gs_gstate_initial(1.0)
821 };
822
823 gs_initmatrix(pgs);
824 if ((code = gs_newpath(pgs)) < 0 ||
825 (code = gs_initclip(pgs)) < 0 ||
826 (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
827 (code = gs_setlinestartcap(pgs, gstate_initial.line_params.start_cap)) < 0 ||
828 (code = gs_setlineendcap(pgs, gstate_initial.line_params.end_cap)) < 0 ||
829 (code = gs_setlinedashcap(pgs, gstate_initial.line_params.dash_cap)) < 0 ||
830 (code = gs_setlinejoin(pgs, gstate_initial.line_params.join)) < 0 ||
831 (code = gs_setcurvejoin(pgs, gstate_initial.line_params.curve_join)) < 0 ||
832 (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
833 (gs_setdashadapt(pgs, false),
834 (code = gs_setdotlength(pgs, 0.0, false))) < 0 ||
835 (code = gs_setdotorientation(pgs)) < 0 ||
836 (code = gs_setmiterlimit(pgs, gstate_initial.line_params.miter_limit)) < 0
837 )
838 return code;
839 gs_init_rop(pgs);
840 /* Initialize things so that gx_remap_color won't crash. */
841 if (pgs->icc_manager->default_gray == 0x00) {
842 gs_color_space *pcs1, *pcs2;
843
844 pcs1 = gs_cspace_new_DeviceGray(pgs->memory);
845 if (pcs1 == NULL)
846 return_error(gs_error_unknownerror);
847
848 if (pgs->color[0].color_space != NULL) {
849 gs_setcolorspace(pgs, pcs1);
850 rc_decrement_cs(pcs1, "gs_initgraphics");
851 } else {
852 pgs->color[0].color_space = pcs1;
853 gs_setcolorspace(pgs, pcs1);
854 }
855 code = gx_set_dev_color(pgs);
856 if (code < 0)
857 return code;
858
859 gs_swapcolors_quick(pgs); /* To color 1 */
860
861 pcs2 = gs_cspace_new_DeviceGray(pgs->memory);
862 if (pcs2 == NULL)
863 return_error(gs_error_unknownerror);
864
865 if (pgs->color[0].color_space != NULL) {
866 gs_setcolorspace(pgs, pcs2);
867 rc_decrement_cs(pcs2, "gs_initgraphics");
868 } else {
869 pgs->color[0].color_space = pcs2;
870 gs_setcolorspace(pgs, pcs2);
871 }
872 code = gx_set_dev_color(pgs);
873
874 gs_swapcolors_quick(pgs); /* To color 0 */
875
876 if (code < 0)
877 return code;
878
879 } else {
880 gs_color_space *pcs1, *pcs2;
881
882 pcs1 = gs_cspace_new_ICC(pgs->memory, pgs, 1);
883 if (pcs1 == NULL)
884 return_error(gs_error_unknownerror);
885
886 if (pgs->color[0].color_space != NULL) {
887 gs_setcolorspace(pgs, pcs1);
888 rc_decrement_cs(pcs1, "gs_initgraphics");
889 } else {
890 pgs->color[0].color_space = pcs1;
891 gs_setcolorspace(pgs, pcs1);
892 }
893 code = gx_set_dev_color(pgs);
894 if (code < 0)
895 return code;
896
897 gs_swapcolors_quick(pgs); /* To color 1 */
898 pcs2 = gs_cspace_new_ICC(pgs->memory, pgs, 1);
899 if (pcs2 == NULL)
900 return_error(gs_error_unknownerror);
901
902 if (pgs->color[0].color_space != NULL) {
903 gs_setcolorspace(pgs, pcs2);
904 rc_decrement_cs(pcs2, "gs_initgraphics");
905 } else {
906 pgs->color[0].color_space = pcs2;
907 gs_setcolorspace(pgs, pcs2);
908 }
909 code = gx_set_dev_color(pgs);
910
911 gs_swapcolors_quick(pgs); /* To color 0 */
912
913 if (code < 0)
914 return code;
915 }
916 pgs->in_cachedevice = 0;
917
918 return 0;
919 }
920
921 /* setfilladjust */
922 int
gs_setfilladjust(gs_gstate * pgs,double adjust_x,double adjust_y)923 gs_setfilladjust(gs_gstate * pgs, double adjust_x, double adjust_y)
924 {
925 #define CLAMP_TO_HALF(v)\
926 ((v) <= 0 ? fixed_0 : (v) >= 0.5 ? fixed_half : float2fixed(v));
927
928 pgs->fill_adjust.x = CLAMP_TO_HALF(adjust_x);
929 pgs->fill_adjust.y = CLAMP_TO_HALF(adjust_y);
930
931 sanitize_fill_adjust(pgs);
932
933 return 0;
934 #undef CLAMP_TO_HALF
935 }
936
937 /* currentfilladjust */
938 int
gs_currentfilladjust(const gs_gstate * pgs,gs_point * adjust)939 gs_currentfilladjust(const gs_gstate * pgs, gs_point * adjust)
940 {
941 adjust->x = fixed2float(pgs->fill_adjust.x);
942 adjust->y = fixed2float(pgs->fill_adjust.y);
943 return 0;
944 }
945
946 /* setlimitclamp */
947 void
gs_setlimitclamp(gs_gstate * pgs,bool clamp)948 gs_setlimitclamp(gs_gstate * pgs, bool clamp)
949 {
950 pgs->clamp_coordinates = clamp;
951 }
952
953 /* currentlimitclamp */
954 bool
gs_currentlimitclamp(const gs_gstate * pgs)955 gs_currentlimitclamp(const gs_gstate * pgs)
956 {
957 return pgs->clamp_coordinates;
958 }
959
960 /* settextrenderingmode */
961 void
gs_settextrenderingmode(gs_gstate * pgs,uint trm)962 gs_settextrenderingmode(gs_gstate * pgs, uint trm)
963 {
964 pgs->text_rendering_mode = trm;
965 }
966
967 /* currenttextrenderingmode */
968 uint
gs_currenttextrenderingmode(const gs_gstate * pgs)969 gs_currenttextrenderingmode(const gs_gstate * pgs)
970 {
971 return pgs->text_rendering_mode;
972 }
973
974 double
gs_currenttextspacing(const gs_gstate * pgs)975 gs_currenttextspacing(const gs_gstate *pgs)
976 {
977 return pgs->textspacing;
978 }
979
980 int
gs_settextspacing(gs_gstate * pgs,double Tc)981 gs_settextspacing(gs_gstate *pgs, double Tc)
982 {
983 pgs->textspacing = (float)Tc;
984 return 0;
985 }
986
987 double
gs_currenttextleading(const gs_gstate * pgs)988 gs_currenttextleading(const gs_gstate *pgs)
989 {
990 return pgs->textleading;
991 }
992
993 int
gs_settextleading(gs_gstate * pgs,double TL)994 gs_settextleading(gs_gstate *pgs, double TL)
995 {
996 pgs->textleading = (float)TL;
997 return 0;
998 }
999
1000 double
gs_currenttextrise(const gs_gstate * pgs)1001 gs_currenttextrise(const gs_gstate *pgs)
1002 {
1003 return pgs->textrise;
1004 }
1005
1006 int
gs_settextrise(gs_gstate * pgs,double Ts)1007 gs_settextrise(gs_gstate *pgs, double Ts)
1008 {
1009 pgs->textrise = (float)Ts;
1010 return 0;
1011 }
1012
1013 double
gs_currentwordspacing(const gs_gstate * pgs)1014 gs_currentwordspacing(const gs_gstate *pgs)
1015 {
1016 return pgs->wordspacing;
1017 }
1018
1019 int
gs_setwordspacing(gs_gstate * pgs,double Tw)1020 gs_setwordspacing(gs_gstate *pgs, double Tw)
1021 {
1022 pgs->wordspacing = (float)Tw;
1023 return 0;
1024 }
1025
1026 int
gs_settexthscaling(gs_gstate * pgs,double Tz)1027 gs_settexthscaling(gs_gstate *pgs, double Tz)
1028 {
1029 pgs->texthscaling = (float)Tz;
1030 return 0;
1031 }
1032
1033 double
gs_currenttexthscaling(const gs_gstate * pgs)1034 gs_currenttexthscaling(const gs_gstate *pgs)
1035 {
1036 return pgs->texthscaling;
1037 }
1038
1039 int
gs_setPDFfontsize(gs_gstate * pgs,double Tf)1040 gs_setPDFfontsize(gs_gstate *pgs, double Tf)
1041 {
1042 pgs->PDFfontsize = (float)Tf;
1043 return 0;
1044 }
1045
1046 double
gs_currentPDFfontsize(const gs_gstate * pgs)1047 gs_currentPDFfontsize(const gs_gstate *pgs)
1048 {
1049 return pgs->PDFfontsize;
1050 }
1051
1052 int
gs_settextlinematrix(gs_gstate * pgs,gs_matrix * m)1053 gs_settextlinematrix(gs_gstate *pgs, gs_matrix *m)
1054 {
1055 pgs->textlinematrix.xx = m->xx;
1056 pgs->textlinematrix.xy = m->xy;
1057 pgs->textlinematrix.yx = m->yx;
1058 pgs->textlinematrix.yy = m->yy;
1059 pgs->textlinematrix.tx = m->tx;
1060 pgs->textlinematrix.ty = m->ty;
1061 return 0;
1062 }
1063 int
gs_gettextlinematrix(gs_gstate * pgs,gs_matrix * m)1064 gs_gettextlinematrix(gs_gstate *pgs, gs_matrix *m)
1065 {
1066 m->xx = pgs->textlinematrix.xx;
1067 m->xy = pgs->textlinematrix.xy;
1068 m->yx = pgs->textlinematrix.yx;
1069 m->yy = pgs->textlinematrix.yy;
1070 m->tx = pgs->textlinematrix.tx;
1071 m->ty = pgs->textlinematrix.ty;
1072 return 0;
1073 }
1074
1075 int
gs_settextmatrix(gs_gstate * pgs,gs_matrix * m)1076 gs_settextmatrix(gs_gstate *pgs, gs_matrix *m)
1077 {
1078 pgs->textmatrix.xx = m->xx;
1079 pgs->textmatrix.xy = m->xy;
1080 pgs->textmatrix.yx = m->yx;
1081 pgs->textmatrix.yy = m->yy;
1082 pgs->textmatrix.tx = m->tx;
1083 pgs->textmatrix.ty = m->ty;
1084 return 0;
1085 }
1086 int
gs_gettextmatrix(gs_gstate * pgs,gs_matrix * m)1087 gs_gettextmatrix(gs_gstate *pgs, gs_matrix *m)
1088 {
1089 m->xx = pgs->textmatrix.xx;
1090 m->xy = pgs->textmatrix.xy;
1091 m->yx = pgs->textmatrix.yx;
1092 m->yy = pgs->textmatrix.yy;
1093 m->tx = pgs->textmatrix.tx;
1094 m->ty = pgs->textmatrix.ty;
1095 return 0;
1096 }
1097
1098
1099 /* sethpglpathmode */
1100 void
gs_sethpglpathmode(gs_gstate * pgs,bool path)1101 gs_sethpglpathmode(gs_gstate * pgs, bool path)
1102 {
1103 pgs->hpgl_path_mode = path;
1104 }
1105
1106 /* currenthpglpathmode */
1107 bool
gs_currenthpglpathmode(const gs_gstate * pgs)1108 gs_currenthpglpathmode(const gs_gstate * pgs)
1109 {
1110 return pgs->hpgl_path_mode;
1111 }
1112
1113 /* ------ Internal routines ------ */
1114
1115 /* Free the privately allocated parts of a gstate. */
1116 static void
gstate_free_parts(gs_gstate * parts,gs_memory_t * mem,client_name_t cname)1117 gstate_free_parts(gs_gstate * parts, gs_memory_t * mem, client_name_t cname)
1118 {
1119 gs_free_object(mem, parts->color[1].dev_color, cname);
1120 gs_free_object(mem, parts->color[1].ccolor, cname);
1121 gs_free_object(mem, parts->color[0].dev_color, cname);
1122 gs_free_object(mem, parts->color[0].ccolor, cname);
1123 parts->color[1].dev_color = 0;
1124 parts->color[1].ccolor = 0;
1125 parts->color[0].dev_color = 0;
1126 parts->color[0].ccolor = 0;
1127 if (!parts->effective_clip_shared && parts->effective_clip_path) {
1128 gx_cpath_free(parts->effective_clip_path, cname);
1129 parts->effective_clip_path = 0;
1130 }
1131 gx_cpath_free(parts->clip_path, cname);
1132 parts->clip_path = 0;
1133 if (parts->path) {
1134 gx_path_free(parts->path, cname);
1135 parts->path = 0;
1136 }
1137 }
1138
1139 /* Allocate the privately allocated parts of a gstate. */
1140 static int
gstate_alloc_parts(gs_gstate * parts,const gs_gstate * shared,gs_memory_t * mem,client_name_t cname)1141 gstate_alloc_parts(gs_gstate * parts, const gs_gstate * shared,
1142 gs_memory_t * mem, client_name_t cname)
1143 {
1144 gs_memory_t *path_mem = gstate_path_memory(mem);
1145
1146 parts->path =
1147 (shared ?
1148 gx_path_alloc_shared(shared->path, path_mem,
1149 "gstate_alloc_parts(path)") :
1150 gx_path_alloc(path_mem, "gstate_alloc_parts(path)"));
1151 parts->clip_path =
1152 (shared ?
1153 gx_cpath_alloc_shared(shared->clip_path, mem,
1154 "gstate_alloc_parts(clip_path)") :
1155 gx_cpath_alloc(mem, "gstate_alloc_parts(clip_path)"));
1156 if (!shared || shared->effective_clip_shared) {
1157 parts->effective_clip_path = parts->clip_path;
1158 parts->effective_clip_shared = true;
1159 } else {
1160 parts->effective_clip_path =
1161 gx_cpath_alloc_shared(shared->effective_clip_path, mem,
1162 "gstate_alloc_parts(effective_clip_path)");
1163 parts->effective_clip_shared = false;
1164 }
1165 parts->color[0].color_space = NULL;
1166 parts->color[1].color_space = NULL;
1167 parts->color[0].ccolor =
1168 gs_alloc_struct(mem, gs_client_color, &st_client_color, cname);
1169 parts->color[1].ccolor =
1170 gs_alloc_struct(mem, gs_client_color, &st_client_color, cname);
1171 parts->color[0].dev_color =
1172 gs_alloc_struct(mem, gx_device_color, &st_device_color, cname);
1173 parts->color[1].dev_color =
1174 gs_alloc_struct(mem, gx_device_color, &st_device_color, cname);
1175 if (parts->path == 0 || parts->clip_path == 0 ||
1176 parts->effective_clip_path == 0 ||
1177 parts->color[0].ccolor == 0 || parts->color[0].dev_color == 0 ||
1178 parts->color[1].ccolor == 0 || parts->color[1].dev_color == 0
1179 ) {
1180 gstate_free_parts(parts, mem, cname);
1181 return_error(gs_error_VMerror);
1182 }
1183 return 0;
1184 }
1185
1186 /*
1187 * Allocate a gstate and its contents.
1188 * If pfrom is not NULL, the path, clip_path, and (if distinct from both
1189 * clip_path and view_clip) effective_clip_path share the segments of
1190 * pfrom's corresponding path(s).
1191 */
1192 static gs_gstate *
gstate_alloc(gs_memory_t * mem,client_name_t cname,const gs_gstate * pfrom)1193 gstate_alloc(gs_memory_t * mem, client_name_t cname, const gs_gstate * pfrom)
1194 {
1195 gs_gstate *pgs =
1196 gs_alloc_struct(mem, gs_gstate, &st_gs_gstate, cname);
1197
1198 if (pgs == 0)
1199 return 0;
1200 memset(pgs, 0x00, sizeof(gs_gstate));
1201 if (gstate_alloc_parts(pgs, pfrom, mem, cname) < 0) {
1202 gs_free_object(mem, pgs, cname);
1203 return 0;
1204 }
1205 pgs->memory = mem;
1206 return pgs;
1207 }
1208
1209 /* Copy the dash pattern from one gstate to another. */
1210 static int
gstate_copy_dash(gs_memory_t * mem,gx_dash_params * dash,const gs_gstate * pfrom)1211 gstate_copy_dash(gs_memory_t *mem, gx_dash_params *dash , const gs_gstate * pfrom)
1212 {
1213 return gx_set_dash(dash, pfrom->line_params.dash.pattern,
1214 pfrom->line_params.dash.pattern_size,
1215 pfrom->line_params.dash.offset, mem);
1216 }
1217
1218 /* Clone an existing graphics state. */
1219 /* Return 0 if the allocation fails. */
1220 /* If reason is for_gsave, the clone refers to the old contents, */
1221 /* and we switch the old state to refer to the new contents. */
1222 static gs_gstate *
gstate_clone(gs_gstate * pfrom,gs_memory_t * mem,client_name_t cname,gs_gstate_copy_reason_t reason)1223 gstate_clone(gs_gstate * pfrom, gs_memory_t * mem, client_name_t cname,
1224 gs_gstate_copy_reason_t reason)
1225 {
1226 gs_gstate *pgs = gstate_alloc(mem, cname, pfrom);
1227 gs_gstate_parts parts;
1228 void *pdata = NULL;
1229 gx_dash_params dash;
1230
1231 if (pgs == NULL)
1232 return 0;
1233 GSTATE_ASSIGN_PARTS(&parts, pgs);
1234 if (pfrom->client_data != NULL) {
1235 pdata = (*pfrom->client_procs.alloc) (mem);
1236
1237 if (pdata == NULL ||
1238 gstate_copy_client_data(pfrom, pdata, pfrom->client_data, reason) < 0
1239 )
1240 goto fail;
1241 }
1242 /* Copy the dash and dash pattern if necessary. */
1243 dash = gs_currentlineparams_inline(pfrom)->dash;
1244 if (pfrom->line_params.dash.pattern) {
1245 int code;
1246
1247 dash.pattern = NULL; /* Ensures a fresh allocation */
1248 code = gstate_copy_dash(mem, &dash, pfrom);
1249 if (code < 0)
1250 goto fail;
1251 }
1252 *pgs = *pfrom;
1253 pgs->client_data = pdata;
1254 gs_currentlineparams_inline(pgs)->dash = dash;
1255 pgs->memory = mem;
1256
1257 gs_gstate_copied(pgs);
1258 /* Don't do anything to clip_stack. */
1259
1260 rc_increment(pgs->device);
1261 *parts.color[0].ccolor = *pfrom->color[0].ccolor;
1262 *parts.color[0].dev_color = *pfrom->color[0].dev_color;
1263 *parts.color[1].ccolor = *pfrom->color[1].ccolor;
1264 *parts.color[1].dev_color = *pfrom->color[1].dev_color;
1265 if (reason == copy_for_gsave) {
1266 float *dfrom = pfrom->line_params.dash.pattern;
1267 float *dto = pgs->line_params.dash.pattern;
1268
1269 GSTATE_ASSIGN_PARTS(pfrom, &parts);
1270 pgs->line_params.dash.pattern = dfrom;
1271 pfrom->line_params.dash.pattern = dto;
1272 } else {
1273 GSTATE_ASSIGN_PARTS(pgs, &parts);
1274 }
1275 gs_swapcolors_quick(pgs);
1276 cs_adjust_counts_icc(pgs, 1);
1277 gs_swapcolors_quick(pgs);
1278 cs_adjust_counts_icc(pgs, 1);
1279 return pgs;
1280 fail:
1281 if (pdata != NULL)
1282 (*pfrom->client_procs.free) (pdata, mem);
1283 memset(pgs->color, 0, 2*sizeof(gs_gstate_color));
1284 gs_free_object(mem, pgs->line_params.dash.pattern, cname);
1285 GSTATE_ASSIGN_PARTS(pgs, &parts);
1286 gstate_free_parts(pgs, mem, cname);
1287 gs_free_object(mem, pgs, cname);
1288 return 0;
1289 }
1290
1291 /* Adjust reference counters for the whole clip stack */
1292 /* accessible from the given point */
1293 static void
clip_stack_rc_adjust(gx_clip_stack_t * cs,int delta,client_name_t cname)1294 clip_stack_rc_adjust(gx_clip_stack_t *cs, int delta, client_name_t cname)
1295 {
1296 gx_clip_stack_t *p = cs;
1297
1298 while(p) {
1299 gx_clip_stack_t *q = p;
1300 p = p->next;
1301 rc_adjust(q, delta, cname);
1302 }
1303 }
1304
1305 /*
1306 * Finalization for graphics states. This is where we handle RC for those
1307 * elements.
1308 */
1309 void
gs_gstate_finalize(const gs_memory_t * cmem,void * vptr)1310 gs_gstate_finalize(const gs_memory_t *cmem,void *vptr)
1311 {
1312 gs_gstate *pgs = (gs_gstate *)vptr;
1313 (void)cmem; /* unused */
1314
1315 if (cmem == NULL)
1316 return; /* place for breakpoint */
1317 gstate_free_contents(pgs);
1318 }
1319
1320 /* Release the composite parts of a graphics state, */
1321 /* but not the state itself. */
1322 static void
gstate_free_contents(gs_gstate * pgs)1323 gstate_free_contents(gs_gstate * pgs)
1324 {
1325 gs_memory_t *mem = pgs->memory;
1326 const char *const cname = "gstate_free_contents";
1327
1328 rc_decrement(pgs->device, cname);
1329 pgs->device = 0;
1330 clip_stack_rc_adjust(pgs->clip_stack, -1, cname);
1331 pgs->clip_stack = 0;
1332 if (pgs->view_clip != NULL && pgs->level == 0) {
1333 gx_cpath_free(pgs->view_clip, cname);
1334 pgs->view_clip = NULL;
1335 }
1336 gs_swapcolors_quick(pgs);
1337 cs_adjust_counts_icc(pgs, -1);
1338 gs_swapcolors_quick(pgs);
1339 cs_adjust_counts_icc(pgs, -1);
1340 pgs->color[0].color_space = 0;
1341 pgs->color[1].color_space = 0;
1342 if (pgs->client_data != 0)
1343 (*pgs->client_procs.free) (pgs->client_data, mem);
1344 pgs->client_data = 0;
1345 gs_free_object(mem, pgs->line_params.dash.pattern, cname);
1346 pgs->line_params.dash.pattern = 0;
1347 gstate_free_parts(pgs, mem, cname); /* this also clears pointers to freed elements */
1348 gs_gstate_release(pgs);
1349 }
1350
1351 /* Copy one gstate to another. */
1352 static int
gstate_copy(gs_gstate * pto,const gs_gstate * pfrom,gs_gstate_copy_reason_t reason,client_name_t cname)1353 gstate_copy(gs_gstate * pto, const gs_gstate * pfrom,
1354 gs_gstate_copy_reason_t reason, client_name_t cname)
1355 {
1356 gs_gstate_parts parts;
1357
1358 GSTATE_ASSIGN_PARTS(&parts, pto);
1359 /* Copy the dash pattern if necessary. */
1360 if (pfrom->line_params.dash.pattern || pto->line_params.dash.pattern) {
1361 int code = gstate_copy_dash(pto->memory,
1362 &(gs_currentlineparams_inline(pto)->dash), pfrom);
1363
1364 if (code < 0)
1365 return code;
1366 }
1367 /*
1368 * It's OK to decrement the counts before incrementing them,
1369 * because anything that is going to survive has a count of
1370 * at least 2 (pto and somewhere else) initially.
1371 * Handle references from contents.
1372 */
1373 cs_adjust_counts_icc(pto, -1);
1374 gs_swapcolors_quick(pto);
1375 cs_adjust_counts_icc(pto, -1);
1376 gs_swapcolors_quick(pto);
1377 gx_path_assign_preserve(pto->path, pfrom->path);
1378 gx_cpath_assign_preserve(pto->clip_path, pfrom->clip_path);
1379 /*
1380 * effective_clip_shared will be copied, but we need to do the
1381 * right thing with effective_clip_path.
1382 */
1383 if (pfrom->effective_clip_shared) {
1384 /*
1385 * pfrom->effective_clip_path is either pfrom->view_clip or
1386 * pfrom->clip_path.
1387 */
1388 parts.effective_clip_path =
1389 (pfrom->effective_clip_path == pfrom->view_clip ?
1390 pto->view_clip : parts.clip_path);
1391 } else
1392 gx_cpath_assign_preserve(pto->effective_clip_path,
1393 pfrom->effective_clip_path);
1394 *parts.color[0].ccolor = *pfrom->color[0].ccolor;
1395 *parts.color[0].dev_color = *pfrom->color[0].dev_color;
1396 *parts.color[1].ccolor = *pfrom->color[1].ccolor;
1397 *parts.color[1].dev_color = *pfrom->color[1].dev_color;
1398 /* Handle references from gstate object. */
1399 rc_pre_assign(pto->device, pfrom->device, cname);
1400 if (pto->clip_stack != pfrom->clip_stack) {
1401 clip_stack_rc_adjust(pfrom->clip_stack, 1, cname);
1402 clip_stack_rc_adjust(pto->clip_stack, -1, cname);
1403 }
1404 {
1405 struct gx_pattern_cache_s *pcache = pto->pattern_cache;
1406 void *pdata = pto->client_data;
1407 gs_memory_t *mem = pto->memory;
1408 gs_gstate *saved = pto->saved;
1409 float *pattern = pto->line_params.dash.pattern;
1410
1411 gs_gstate_pre_assign(pto, (const gs_gstate *)pfrom);
1412 *pto = *pfrom;
1413 pto->client_data = pdata;
1414 pto->memory = mem;
1415 pto->saved = saved;
1416 pto->line_params.dash.pattern = pattern;
1417 if (pto->pattern_cache == 0)
1418 pto->pattern_cache = pcache;
1419 if (pfrom->client_data != 0) {
1420 /* We need to break 'const' here. */
1421 gstate_copy_client_data((gs_gstate *) pfrom, pdata,
1422 pfrom->client_data, reason);
1423 }
1424 }
1425 GSTATE_ASSIGN_PARTS(pto, &parts);
1426 cs_adjust_counts_icc(pto, 1);
1427 gs_swapcolors_quick(pto);
1428 cs_adjust_counts_icc(pto, 1);
1429 gs_swapcolors_quick(pto);
1430 pto->show_gstate =
1431 (pfrom->show_gstate == pfrom ? pto : 0);
1432 return 0;
1433 }
1434
1435 /* Accessories. */
gx_get_clip_path_id(gs_gstate * pgs)1436 gs_id gx_get_clip_path_id(gs_gstate *pgs)
1437 {
1438 return pgs->clip_path->id;
1439 }
1440
gs_swapcolors_quick(const gs_gstate * cpgs)1441 void gs_swapcolors_quick(const gs_gstate *cpgs)
1442 {
1443 union {
1444 const gs_gstate *cpgs;
1445 gs_gstate *pgs;
1446 } const_breaker;
1447 gs_gstate *pgs;
1448 struct gx_cie_joint_caches_s *tmp_cie;
1449 gs_devicen_color_map tmp_ccm;
1450 gs_client_color *tmp_cc;
1451 int tmp;
1452 gx_device_color *tmp_dc;
1453 gs_color_space *tmp_cs;
1454
1455 /* Break const just once, neatly, here rather than
1456 * hackily in every caller. */
1457 const_breaker.cpgs = cpgs;
1458 pgs = const_breaker.pgs;
1459
1460 tmp_cc = pgs->color[0].ccolor;
1461 pgs->color[0].ccolor = pgs->color[1].ccolor;
1462 pgs->color[1].ccolor = tmp_cc;
1463
1464 tmp_dc = pgs->color[0].dev_color;
1465 pgs->color[0].dev_color = pgs->color[1].dev_color;
1466 pgs->color[1].dev_color = tmp_dc;
1467
1468 tmp_cs = pgs->color[0].color_space;
1469 pgs->color[0].color_space = pgs->color[1].color_space;
1470 pgs->color[1].color_space = tmp_cs;
1471
1472 /* Overprint and effective_op vary with stroke/fill and cs */
1473 tmp = pgs->color[0].effective_opm;
1474 pgs->color[0].effective_opm = pgs->color[1].effective_opm;
1475 pgs->color[1].effective_opm = tmp;
1476
1477 /* Swap the bits of the gs_gstate that depend on the current color */
1478 tmp_cie = pgs->cie_joint_caches;
1479 pgs->cie_joint_caches = pgs->cie_joint_caches_alt;
1480 pgs->cie_joint_caches_alt = tmp_cie;
1481
1482 tmp_ccm = pgs->color_component_map;
1483 pgs->color_component_map = pgs->color_component_map_alt;
1484 pgs->color_component_map_alt = tmp_ccm;
1485
1486 pgs->is_fill_color = !(pgs->is_fill_color); /* used by overprint for fill_stroke */
1487 }
1488