gsdps1.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (C) 1991, 1992, 1994, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gsdps1.c,v 1.10 2003/09/15 10:04:01 igor Exp $ */
  14. /* Display PostScript graphics additions for Ghostscript library */
  15. #include "math_.h"
  16. #include "gx.h"
  17. #include "gserrors.h"
  18. #include "gsmatrix.h" /* for gscoord.h */
  19. #include "gscoord.h"
  20. #include "gspaint.h"
  21. #include "gxdevice.h"
  22. #include "gxfixed.h"
  23. #include "gxmatrix.h"
  24. #include "gxhldevc.h"
  25. #include "gspath.h"
  26. #include "gspath2.h" /* defines interface */
  27. #include "gzpath.h"
  28. #include "gzcpath.h"
  29. #include "gzstate.h"
  30. /*
  31. * Define how much rounding slop setbbox should leave,
  32. * in device coordinates. Because of rounding in transforming
  33. * path coordinates to fixed point, the minimum realistic value is:
  34. *
  35. * #define box_rounding_slop_fixed (fixed_epsilon)
  36. *
  37. * But even this isn't enough to compensate for cumulative rounding error
  38. * in rmoveto or rcurveto. Instead, we somewhat arbitrarily use:
  39. */
  40. #define box_rounding_slop_fixed (fixed_epsilon * 3)
  41. /* ------ Graphics state ------ */
  42. /* Set the bounding box for the current path. */
  43. int
  44. gs_setbbox(gs_state * pgs, floatp llx, floatp lly, floatp urx, floatp ury)
  45. {
  46. gs_rect ubox, dbox;
  47. gs_fixed_rect obox, bbox;
  48. gx_path *ppath = pgs->path;
  49. int code;
  50. if (llx > urx || lly > ury)
  51. return_error(gs_error_rangecheck);
  52. /* Transform box to device coordinates. */
  53. ubox.p.x = llx;
  54. ubox.p.y = lly;
  55. ubox.q.x = urx;
  56. ubox.q.y = ury;
  57. if ((code = gs_bbox_transform(&ubox, &ctm_only(pgs), &dbox)) < 0)
  58. return code;
  59. /* Round the corners in opposite directions. */
  60. /* Because we can't predict the magnitude of the dbox values, */
  61. /* we add/subtract the slop after fixing. */
  62. if (dbox.p.x < fixed2float(min_fixed + box_rounding_slop_fixed) ||
  63. dbox.p.y < fixed2float(min_fixed + box_rounding_slop_fixed) ||
  64. dbox.q.x >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon) ||
  65. dbox.q.y >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon)
  66. )
  67. return_error(gs_error_limitcheck);
  68. bbox.p.x =
  69. (fixed) floor(dbox.p.x * fixed_scale) - box_rounding_slop_fixed;
  70. bbox.p.y =
  71. (fixed) floor(dbox.p.y * fixed_scale) - box_rounding_slop_fixed;
  72. bbox.q.x =
  73. (fixed) ceil(dbox.q.x * fixed_scale) + box_rounding_slop_fixed;
  74. bbox.q.y =
  75. (fixed) ceil(dbox.q.y * fixed_scale) + box_rounding_slop_fixed;
  76. if (gx_path_bbox(ppath, &obox) >= 0) { /* Take the union of the bboxes. */
  77. ppath->bbox.p.x = min(obox.p.x, bbox.p.x);
  78. ppath->bbox.p.y = min(obox.p.y, bbox.p.y);
  79. ppath->bbox.q.x = max(obox.q.x, bbox.q.x);
  80. ppath->bbox.q.y = max(obox.q.y, bbox.q.y);
  81. } else { /* empty path *//* Just set the bbox. */
  82. ppath->bbox = bbox;
  83. }
  84. ppath->bbox_set = 1;
  85. return 0;
  86. }
  87. /* ------ Rectangles ------ */
  88. /* Append a list of rectangles to a path. */
  89. int
  90. gs_rectappend(gs_state * pgs, const gs_rect * pr, uint count)
  91. {
  92. for (; count != 0; count--, pr++) {
  93. floatp px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
  94. int code;
  95. /* Ensure counter-clockwise drawing. */
  96. if ((qx >= px) != (qy >= py))
  97. qx = px, px = pr->q.x; /* swap x values */
  98. if ((code = gs_moveto(pgs, px, py)) < 0 ||
  99. (code = gs_lineto(pgs, qx, py)) < 0 ||
  100. (code = gs_lineto(pgs, qx, qy)) < 0 ||
  101. (code = gs_lineto(pgs, px, qy)) < 0 ||
  102. (code = gs_closepath(pgs)) < 0
  103. )
  104. return code;
  105. }
  106. return 0;
  107. }
  108. /* Clip to a list of rectangles. */
  109. int
  110. gs_rectclip(gs_state * pgs, const gs_rect * pr, uint count)
  111. {
  112. int code;
  113. gx_path save;
  114. gx_path_init_local(&save, pgs->memory);
  115. gx_path_assign_preserve(&save, pgs->path);
  116. gs_newpath(pgs);
  117. if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
  118. (code = gs_clip(pgs)) < 0
  119. ) {
  120. gx_path_assign_free(pgs->path, &save);
  121. return code;
  122. }
  123. gx_path_free(&save, "gs_rectclip");
  124. gs_newpath(pgs);
  125. return 0;
  126. }
  127. /* Fill a list of rectangles. */
  128. /* We take the trouble to do this efficiently in the simple cases. */
  129. int
  130. gs_rectfill(gs_state * pgs, const gs_rect * pr, uint count)
  131. {
  132. const gs_rect *rlist = pr;
  133. gx_clip_path *pcpath;
  134. uint rcount = count;
  135. int code;
  136. gx_device * pdev = pgs->device;
  137. gx_device_color *pdc = pgs->dev_color;
  138. const gs_imager_state *pis = (const gs_imager_state *)pgs;
  139. bool hl_color_available = gx_hld_is_hl_color_available(pis, pdc);
  140. gs_fixed_rect empty = {{0, 0}, {0, 0}};
  141. bool hl_color = (hl_color_available &&
  142. dev_proc(pdev, fill_rectangle_hl_color)(pdev,
  143. &empty, pis, pdc, NULL) == 0);
  144. gx_set_dev_color(pgs);
  145. if ((is_fzero2(pgs->ctm.xy, pgs->ctm.yx) ||
  146. is_fzero2(pgs->ctm.xx, pgs->ctm.yy)) &&
  147. gx_effective_clip_path(pgs, &pcpath) >= 0 &&
  148. clip_list_is_rectangle(gx_cpath_list(pcpath)) &&
  149. (hl_color ||
  150. pdc->type == gx_dc_type_pure ||
  151. pdc->type == gx_dc_type_ht_binary ||
  152. pdc->type == gx_dc_type_ht_colored
  153. /* DeviceN todo: add wts case */) &&
  154. gs_state_color_load(pgs) >= 0 &&
  155. (*dev_proc(pdev, get_alpha_bits)) (pdev, go_graphics)
  156. <= 1 &&
  157. (!pgs->overprint || !pgs->effective_overprint_mode)
  158. ) {
  159. uint i;
  160. gs_fixed_rect clip_rect;
  161. gx_cpath_inner_box(pcpath, &clip_rect);
  162. for (i = 0; i < count; ++i) {
  163. gs_fixed_point p, q;
  164. gs_fixed_rect draw_rect;
  165. if (gs_point_transform2fixed(&pgs->ctm, pr[i].p.x, pr[i].p.y, &p) < 0 ||
  166. gs_point_transform2fixed(&pgs->ctm, pr[i].q.x, pr[i].q.y, &q) < 0
  167. ) { /* Switch to the slow algorithm. */
  168. goto slow;
  169. }
  170. draw_rect.p.x = min(p.x, q.x);
  171. draw_rect.p.y = min(p.y, q.y);
  172. draw_rect.q.x = max(p.x, q.x);
  173. draw_rect.q.y = max(p.y, q.y);
  174. if (hl_color) {
  175. rect_intersect(draw_rect, clip_rect);
  176. if (draw_rect.p.x < draw_rect.q.x &&
  177. draw_rect.p.y < draw_rect.q.y) {
  178. code = dev_proc(pdev, fill_rectangle_hl_color)(pdev,
  179. &draw_rect, pis, pdc, pcpath);
  180. if (code < 0)
  181. return code;
  182. }
  183. } else {
  184. int x, y, w, h;
  185. draw_rect.p.x -= pgs->fill_adjust.x;
  186. draw_rect.p.y -= pgs->fill_adjust.x;
  187. draw_rect.q.x += pgs->fill_adjust.x;
  188. draw_rect.q.y += pgs->fill_adjust.x;
  189. rect_intersect(draw_rect, clip_rect);
  190. x = fixed2int_pixround(draw_rect.p.x);
  191. y = fixed2int_pixround(draw_rect.p.y);
  192. w = fixed2int_pixround(draw_rect.q.x) - x;
  193. h = fixed2int_pixround(draw_rect.q.y) - y;
  194. if (w > 0 && h > 0)
  195. if (gx_fill_rectangle(x, y, w, h, pdc, pgs) < 0)
  196. goto slow;
  197. }
  198. }
  199. return 0;
  200. slow:rlist = pr + i;
  201. rcount = count - i;
  202. } {
  203. bool do_save = !gx_path_is_null(pgs->path);
  204. if (do_save) {
  205. if ((code = gs_gsave(pgs)) < 0)
  206. return code;
  207. gs_newpath(pgs);
  208. }
  209. if ((code = gs_rectappend(pgs, rlist, rcount)) < 0 ||
  210. (code = gs_fill(pgs)) < 0
  211. )
  212. DO_NOTHING;
  213. if (do_save)
  214. gs_grestore(pgs);
  215. else if (code < 0)
  216. gs_newpath(pgs);
  217. }
  218. return code;
  219. }
  220. /* Stroke a list of rectangles. */
  221. /* (We could do this a lot more efficiently.) */
  222. int
  223. gs_rectstroke(gs_state * pgs, const gs_rect * pr, uint count,
  224. const gs_matrix * pmat)
  225. {
  226. bool do_save = pmat != NULL || !gx_path_is_null(pgs->path);
  227. int code;
  228. if (do_save) {
  229. if ((code = gs_gsave(pgs)) < 0)
  230. return code;
  231. gs_newpath(pgs);
  232. }
  233. if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
  234. (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
  235. (code = gs_stroke(pgs)) < 0
  236. )
  237. DO_NOTHING;
  238. if (do_save)
  239. gs_grestore(pgs);
  240. else if (code < 0)
  241. gs_newpath(pgs);
  242. return code;
  243. }