1 /* Copyright (C) 2001-2012 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.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Painting procedures for Ghostscript library */
18 #include "math_.h"		/* for fabs */
19 #include "gx.h"
20 #include "gpcheck.h"
21 #include "gserrors.h"
22 #include "gsropt.h"		/* for gxpaint.h */
23 #include "gxfixed.h"
24 #include "gxmatrix.h"		/* for gs_state */
25 #include "gspaint.h"
26 #include "gspath.h"
27 #include "gzpath.h"
28 #include "gxpaint.h"
29 #include "gzstate.h"
30 #include "gxdevice.h"
31 #include "gxdevmem.h"
32 #include "gzcpath.h"
33 #include "gxhldevc.h"
34 #include "gsutil.h"
35 #include "gxdevsop.h"
36 
37 /* Define the nominal size for alpha buffers. */
38 #define abuf_nominal_SMALL 500
39 #define abuf_nominal_LARGE 2000
40 #if arch_small_memory
41 #  define abuf_nominal abuf_nominal_SMALL
42 #else
43 #ifdef DEBUG
44 #  define abuf_nominal\
45      (gs_debug_c('.') ? abuf_nominal_SMALL : abuf_nominal_LARGE)
46 #else
47 #  define abuf_nominal abuf_nominal_LARGE
48 #endif
49 #endif
50 
51 /* Erase the page */
52 int
gs_erasepage(gs_state * pgs)53 gs_erasepage(gs_state * pgs)
54 {
55     /*
56      * We can't just fill with device white; we must take the
57      * transfer function into account.
58      */
59     int code;
60 
61     if ((code = gs_gsave(pgs)) < 0)
62         return code;
63     if ((code = gs_setgray(pgs, 1.0)) >= 0) {
64         /* Fill the page directly, ignoring clipping. */
65         code = gs_fillpage(pgs);
66     }
67     gs_grestore(pgs);
68     return code;
69 }
70 
71 /* Fill the page with the current color. */
72 int
gs_fillpage(gs_state * pgs)73 gs_fillpage(gs_state * pgs)
74 {
75     gx_device *dev = gs_currentdevice(pgs);
76     int code;
77 
78     /* If we get here without a valid get_color_mapping_procs, fail */
79     if (dev_proc(dev, get_color_mapping_procs) == NULL ||
80         dev_proc(dev, get_color_mapping_procs) == gx_error_get_color_mapping_procs) {
81         emprintf1(dev->memory,
82                   "\n   *** Error: No get_color_mapping_procs for device: %s\n",
83                   dev->dname);
84         return_error(gs_error_Fatal);
85     }
86     /* Processing a fill object operation */
87     dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_PATH_TAG);
88 
89     code = gx_set_dev_color(pgs);
90     if (code != 0)
91         return code;
92 
93     code = (*dev_proc(dev, fillpage))(dev, (gs_imager_state *)pgs,
94                                       gs_currentdevicecolor_inline(pgs));
95     if (code < 0)
96         return code;
97     return (*dev_proc(dev, sync_output)) (dev);
98 }
99 /*
100  * Set up an alpha buffer for a stroke or fill operation.  Return 0
101  * if no buffer could be allocated, 1 if a buffer was installed,
102  * or the usual negative error code.
103  *
104  * The fill/stroke code sets up a clipping device if needed; however,
105  * since we scale up all the path coordinates, we either need to scale up
106  * the clipping region, or do clipping after, rather than before,
107  * alpha buffering.  Either of these is a little inconvenient, but
108  * the former is less inconvenient.
109  */
110 static int
scale_paths(gs_state * pgs,int log2_scale_x,int log2_scale_y,bool do_path)111 scale_paths(gs_state * pgs, int log2_scale_x, int log2_scale_y, bool do_path)
112 {
113     /*
114      * Because of clip and clippath, any of path, clip_path, and view_clip
115      * may be aliases for each other.  The only reliable way to detect
116      * this is by comparing the segments pointers.  Note that we must
117      * scale the non-segment parts of the paths even if the segments are
118      * aliased.
119      */
120     const gx_path_segments *seg_clip =
121         (pgs->clip_path->path_valid ? pgs->clip_path->path.segments : 0);
122     const gx_clip_rect_list *list_clip = pgs->clip_path->rect_list;
123     const gx_path_segments *seg_view_clip;
124     const gx_clip_rect_list *list_view_clip;
125     const gx_path_segments *seg_effective_clip =
126         (pgs->effective_clip_path->path_valid ?
127          pgs->effective_clip_path->path.segments : 0);
128     const gx_clip_rect_list *list_effective_clip =
129         pgs->effective_clip_path->rect_list;
130 
131     gx_cpath_scale_exp2_shared(pgs->clip_path, log2_scale_x, log2_scale_y,
132                                false, false);
133     if (pgs->view_clip != 0 && pgs->view_clip != pgs->clip_path) {
134         seg_view_clip =
135             (pgs->view_clip->path_valid ? pgs->view_clip->path.segments : 0);
136         list_view_clip = pgs->view_clip->rect_list;
137         gx_cpath_scale_exp2_shared(pgs->view_clip, log2_scale_x, log2_scale_y,
138                                    list_view_clip == list_clip,
139                                    seg_view_clip && seg_view_clip == seg_clip);
140     } else
141         seg_view_clip = 0, list_view_clip = 0;
142     if (pgs->effective_clip_path != pgs->clip_path &&
143         pgs->effective_clip_path != pgs->view_clip
144         )
145         gx_cpath_scale_exp2_shared(pgs->effective_clip_path, log2_scale_x,
146                                    log2_scale_y,
147                                    list_effective_clip == list_clip ||
148                                    list_effective_clip == list_view_clip,
149                                    seg_effective_clip &&
150                                    (seg_effective_clip == seg_clip ||
151                                     seg_effective_clip == seg_view_clip));
152     if (do_path) {
153         const gx_path_segments *seg_path = pgs->path->segments;
154 
155         gx_path_scale_exp2_shared(pgs->path, log2_scale_x, log2_scale_y,
156                                   seg_path == seg_clip ||
157                                   seg_path == seg_view_clip ||
158                                   seg_path == seg_effective_clip);
159     }
160     return 0;
161 }
162 static void
scale_dash_pattern(gs_state * pgs,floatp scale)163 scale_dash_pattern(gs_state * pgs, floatp scale)
164 {
165     int i;
166 
167     for (i = 0; i < pgs->line_params.dash.pattern_size; ++i)
168         pgs->line_params.dash.pattern[i] *= scale;
169     pgs->line_params.dash.offset *= scale;
170     pgs->line_params.dash.pattern_length *= scale;
171     pgs->line_params.dash.init_dist_left *= scale;
172     if (pgs->line_params.dot_length_absolute)
173         pgs->line_params.dot_length *= scale;
174 }
175 static int
alpha_buffer_init(gs_state * pgs,fixed extra_x,fixed extra_y,int alpha_bits,bool devn)176 alpha_buffer_init(gs_state * pgs, fixed extra_x, fixed extra_y, int alpha_bits,
177                   bool devn)
178 {
179     gx_device *dev = gs_currentdevice_inline(pgs);
180     int log2_alpha_bits = ilog2(alpha_bits);
181     gs_fixed_rect bbox;
182     gs_int_rect ibox;
183     uint width, raster, band_space;
184     uint height;
185     gs_log2_scale_point log2_scale;
186     gs_memory_t *mem;
187     gx_device_memory *mdev;
188 
189     log2_scale.x = log2_scale.y = log2_alpha_bits;
190     gx_path_bbox(pgs->path, &bbox);
191     ibox.p.x = fixed2int(bbox.p.x - extra_x) - 1;
192     ibox.p.y = fixed2int(bbox.p.y - extra_y) - 1;
193     ibox.q.x = fixed2int_ceiling(bbox.q.x + extra_x) + 1;
194     ibox.q.y = fixed2int_ceiling(bbox.q.y + extra_y) + 1;
195     width = (ibox.q.x - ibox.p.x) << log2_scale.x;
196     raster = bitmap_raster(width);
197     band_space = raster << log2_scale.y;
198     height = (abuf_nominal / band_space) << log2_scale.y;
199     if (height == 0)
200         height = 1 << log2_scale.y;
201     mem = pgs->memory;
202     mdev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
203                            "alpha_buffer_init");
204     if (mdev == 0)
205         return 0;		/* if no room, don't buffer */
206     /* We may have to update the marking parameters if we have a pdf14 device
207        as our target.  Need to do while dev is still active in pgs */
208     if (dev_proc(dev, dev_spec_op)(dev, gxdso_is_pdf14_device, NULL, 0)) {
209         gs_update_trans_marking_params(pgs);
210     }
211     gs_make_mem_abuf_device(mdev, mem, dev, &log2_scale,
212                             alpha_bits, ibox.p.x << log2_scale.x, devn);
213     mdev->width = width;
214     mdev->height = height;
215     mdev->bitmap_memory = mem;
216     if ((*dev_proc(mdev, open_device)) ((gx_device *) mdev) < 0) {
217         /* No room for bits, punt. */
218         gs_free_object(mem, mdev, "alpha_buffer_init");
219         return 0;
220     }
221     gx_set_device_only(pgs, (gx_device *) mdev);
222     scale_paths(pgs, log2_scale.x, log2_scale.y, true);
223     return 1;
224 }
225 
226 /* Release an alpha buffer. */
227 static int
alpha_buffer_release(gs_state * pgs,bool newpath)228 alpha_buffer_release(gs_state * pgs, bool newpath)
229 {
230     gx_device_memory *mdev =
231         (gx_device_memory *) gs_currentdevice_inline(pgs);
232     int code = (*dev_proc(mdev, close_device)) ((gx_device *) mdev);
233 
234     if (code >= 0)
235         scale_paths(pgs, -mdev->log2_scale.x, -mdev->log2_scale.y,
236                 !(newpath && !gx_path_is_shared(pgs->path)));
237     /* Reference counting will free mdev. */
238     gx_set_device_only(pgs, mdev->target);
239     return code;
240 }
241 
do_fill(gs_state * pgs,int rule)242 static int do_fill(gs_state *pgs, int rule)
243 {
244     int code, abits, acode, rcode = 0;
245     bool devn;
246 
247     /* Here we need to distinguish text from vectors to compute the object tag.
248        Actually we need to know whether this function is called to rasterize a character,
249        or to rasterize a vector graphics to the output device.
250        Currently we assume it works for the bitrgbtags device only,
251        which is a low level device with a 4-component color model.
252        We use the fact that with printers a character is usually being rendered
253        to a 1bpp cache device rather than to the output device.
254        Therefore we hackly look whether the target device
255        "has a color" : either it's a multicomponent color model,
256        or it is not gray (such as a yellow separation).
257 
258        This check has several limitations :
259        1. It doesn't work with -dNOCACHE.
260        2. It doesn't work with large characters,
261           which cannot fit into a cache cell and thus they
262           render directly to the output device.
263        3. It doesn't work for TextAlphaBits=2 or 4.
264           We don't care of this case because
265           text antialiasing usually usn't applied to printers.
266        4. It doesn't work for things like with "(xyz) true charpath stroke".
267           That's unfortunate, we'd like to improve someday.
268        5. It doesn't work for high level devices when a Type 3 character is being constructed.
269           This case is not important for low level devices
270           (which a printer is), because low level device doesn't accept
271           Type 3 charproc streams immediately.
272        6. It doesn't work properly while an insiding testing,
273           which sets gs_hit_device, which is uncolored.
274      */
275     if (gx_device_has_color(gs_currentdevice(pgs))) {
276         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_PATH_TAG);
277     }
278     else {
279         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_TEXT_TAG);
280     }
281     code = gx_set_dev_color(pgs);
282     if (code != 0)
283         return code;
284     code = gs_state_color_load(pgs);
285     if (code < 0)
286         return code;
287     abits = 0;
288     {
289         gx_device_color *col = gs_currentdevicecolor_inline(pgs);
290         devn = color_is_devn(col);
291         if (color_is_pure(col) || devn)
292             abits = alpha_buffer_bits(pgs);
293     }
294     if (abits > 1) {
295         acode = alpha_buffer_init(pgs, pgs->fill_adjust.x,
296                                   pgs->fill_adjust.y, abits, devn);
297         if (acode < 0)
298             return acode;
299     } else
300         acode = 0;
301     code = gx_fill_path(pgs->path, gs_currentdevicecolor_inline(pgs), pgs, rule,
302                         pgs->fill_adjust.x, pgs->fill_adjust.y);
303     if (acode > 0)
304         rcode = alpha_buffer_release(pgs, code >= 0);
305     if (code >= 0 && rcode < 0)
306         code = rcode;
307 
308     return code;
309 }
310 
311 /* Fill the current path using a specified rule. */
312 static int
fill_with_rule(gs_state * pgs,int rule)313 fill_with_rule(gs_state * pgs, int rule)
314 {
315     int code;
316 
317     /* If we're inside a charpath, just merge the current path */
318     /* into the parent's path. */
319     if (pgs->in_charpath)
320         code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
321                                      pgs->in_charpath);
322             /* If we're rendering a glyph cached, the show machinery decides
323              * whether to actually image it on the output or not, but uncached
324              * will render directly to the output, so for text rendering
325              * mode 3, we have to short circuit it here, but keep the
326              * current point
327              */
328     else if (gs_is_null_device(pgs->device)
329             || (pgs->show_gstate && pgs->text_rendering_mode == 3
330             && pgs->in_cachedevice == CACHE_DEVICE_NOT_CACHING)) {
331         /* Handle separately to prevent gs_state_color_load - bug 688308. */
332         gs_newpath(pgs);
333         code = 0;
334     } else {
335         code = do_fill(pgs, rule);
336         if (code >= 0)
337             gs_newpath(pgs);
338     }
339     return code;
340 }
341 /* Fill using the winding number rule */
342 int
gs_fill(gs_state * pgs)343 gs_fill(gs_state * pgs)
344 {
345     pgs->device->sgr.stroke_stored = false;
346     return fill_with_rule(pgs, gx_rule_winding_number);
347 }
348 /* Fill using the even/odd rule */
349 int
gs_eofill(gs_state * pgs)350 gs_eofill(gs_state * pgs)
351 {
352     pgs->device->sgr.stroke_stored = false;
353     return fill_with_rule(pgs, gx_rule_even_odd);
354 }
355 
356 static int
do_stroke(gs_state * pgs)357 do_stroke(gs_state * pgs)
358 {
359     int code, abits, acode, rcode = 0;
360     bool devn;
361 
362     /* to distinguish text from vectors we hackly look at the
363        target device 1 bit per component is a cache and this is
364        text else it is a path */
365     if (gx_device_has_color(gs_currentdevice(pgs)))
366         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_PATH_TAG);
367     else
368         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_TEXT_TAG);
369 
370     /* Here we need to distinguish text from vectors to compute the object tag.
371        Actually we need to know whether this function is called to rasterize a character,
372        or to rasterize a vector graphics to the output device.
373        Currently we assume it works for the bitrgbtags device only,
374        which is a low level device with a 4-component color model.
375        We use the fact that with printers a character is usually being rendered
376        to a 1bpp cache device rather than to the output device.
377        Therefore we hackly look whether the target device
378        "has a color" : either it's a multicomponent color model,
379        or it is not gray (such as a yellow separation).
380 
381        This check has several limitations :
382        1. It doesn't work with -dNOCACHE.
383        2. It doesn't work with large characters,
384           which cannot fit into a cache cell and thus they
385           render directly to the output device.
386        3. It doesn't work for TextAlphaBits=2 or 4.
387           We don't care of this case because
388           text antialiasing usually usn't applied to printers.
389        4. It doesn't work for things like with "(xyz) true charpath stroke".
390           That's unfortunate, we'd like to improve someday.
391        5. It doesn't work for high level devices when a Type 3 character is being constructed.
392           This case is not important for low level devices
393           (which a printer is), because low level device doesn't accept
394           Type 3 charproc streams immediately.
395      */
396     if (gx_device_has_color(gs_currentdevice(pgs))) {
397         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_PATH_TAG);
398     }
399     else {
400         dev_proc(pgs->device, set_graphics_type_tag)(pgs->device, GS_TEXT_TAG);
401     }
402     code = gx_set_dev_color(pgs);
403     if (code != 0)
404         return code;
405     code = gs_state_color_load(pgs);
406     if (code < 0)
407         return code;
408     abits = 0;
409     {
410         gx_device_color *col = gs_currentdevicecolor_inline(pgs);
411         devn = color_is_devn(col);
412         if (color_is_pure(col) || devn)
413             abits = alpha_buffer_bits(pgs);
414     }
415     if (abits > 1) {
416         /*
417          * Expand the bounding box by the line width.
418          * This is expensive to compute, so we only do it
419          * if we know we're going to buffer.
420          */
421         float xxyy = fabs(pgs->ctm.xx) + fabs(pgs->ctm.yy);
422         float xyyx = fabs(pgs->ctm.xy) + fabs(pgs->ctm.yx);
423         float scale = (float)(1 << (abits / 2));
424         float orig_width = gs_currentlinewidth(pgs);
425         float new_width = orig_width * scale;
426         fixed extra_adjust =
427                 float2fixed(max(xxyy, xyyx) * new_width / 2);
428         float orig_flatness = gs_currentflat(pgs);
429         gx_path spath;
430 
431         /* Scale up the line width, dash pattern, and flatness. */
432         if (extra_adjust < fixed_1)
433             extra_adjust = fixed_1;
434         acode = alpha_buffer_init(pgs,
435                                   pgs->fill_adjust.x + extra_adjust,
436                                   pgs->fill_adjust.y + extra_adjust,
437                                   abits, devn);
438         if (acode < 0)
439             return acode;
440         gs_setlinewidth(pgs, new_width);
441         scale_dash_pattern(pgs, scale);
442         gs_setflat(pgs, orig_flatness * scale);
443         /*
444          * The alpha-buffer device requires that we fill the
445          * entire path as a single unit.
446          */
447         gx_path_init_local(&spath, pgs->memory);
448         code = gx_stroke_add(pgs->path, &spath, pgs, false);
449         gs_setlinewidth(pgs, orig_width);
450         scale_dash_pattern(pgs, 1.0 / scale);
451         if (code >= 0)
452             code = gx_fill_path(&spath, gs_currentdevicecolor_inline(pgs), pgs,
453                                 gx_rule_winding_number,
454                                 pgs->fill_adjust.x,
455                                 pgs->fill_adjust.y);
456         gs_setflat(pgs, orig_flatness);
457         gx_path_free(&spath, "gs_stroke");
458         if (acode > 0)
459             rcode = alpha_buffer_release(pgs, code >= 0);
460     } else
461         code = gx_stroke_fill(pgs->path, pgs);
462     if (code >= 0 && rcode < 0)
463         code = rcode;
464     return code;
465 }
466 
467 /* Stroke the current path */
468 int
gs_stroke(gs_state * pgs)469 gs_stroke(gs_state * pgs)
470 {
471     int code;
472 
473     /*
474      * If we're inside a charpath, just merge the current path
475      * into the parent's path.
476      */
477     if (pgs->in_charpath) {
478         if (pgs->in_charpath == cpm_true_charpath) {
479             /*
480              * A stroke inside a true charpath should do the
481              * equivalent of strokepath.
482              */
483             code = gs_strokepath(pgs);
484             if (code < 0)
485                 return code;
486         }
487         code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
488                                      pgs->in_charpath);
489     }
490     if (gs_is_null_device(pgs->device)) {
491         /* Handle separately to prevent gs_state_color_load. */
492         gs_newpath(pgs);
493         code = 0;
494     } else {
495         code = do_stroke(pgs);
496         if (code >= 0)
497             gs_newpath(pgs);
498     }
499     return code;
500 }
501 
502 /* Compute the stroked outline of the current path */
503 static int
gs_strokepath_aux(gs_state * pgs,bool traditional)504 gs_strokepath_aux(gs_state * pgs, bool traditional)
505 {
506     gx_path spath;
507     int code;
508 
509     gx_path_init_local(&spath, pgs->path->memory);
510     code = gx_stroke_add(pgs->path, &spath, pgs, traditional);
511     if (code < 0) {
512         gx_path_free(&spath, "gs_strokepath");
513         return code;
514     }
515     pgs->device->sgr.stroke_stored = false;
516     code = gx_path_assign_free(pgs->path, &spath);
517     if (code < 0)
518         return code;
519     /* NB: needs testing with PCL */
520     if (gx_path_is_void(pgs->path))
521         pgs->current_point_valid = false;
522     else
523         gx_setcurrentpoint(pgs, fixed2float(spath.position.x), fixed2float(spath.position.y));
524     return 0;
525 
526 }
527 
528 int
gs_strokepath(gs_state * pgs)529 gs_strokepath(gs_state * pgs)
530 {
531     return gs_strokepath_aux(pgs, true);
532 }
533 
534 int
gs_strokepath2(gs_state * pgs)535 gs_strokepath2(gs_state * pgs)
536 {
537     return gs_strokepath_aux(pgs, false);
538 }
539