gsptype2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Copyright (C) 1999 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: gsptype2.c,v 1.19 2005/05/25 15:57:58 igor Exp $ */
  14. /* PatternType 2 implementation */
  15. #include "gx.h"
  16. #include "gserrors.h"
  17. #include "gscspace.h"
  18. #include "gsshade.h"
  19. #include "gsmatrix.h" /* for gspcolor.h */
  20. #include "gsstate.h" /* for set/currentfilladjust */
  21. #include "gxcolor2.h"
  22. #include "gxdcolor.h"
  23. #include "gsptype2.h"
  24. #include "gxpcolor.h"
  25. #include "gxstate.h" /* for gs_state_memory */
  26. #include "gzpath.h"
  27. #include "gzstate.h"
  28. /* GC descriptors */
  29. private_st_pattern2_template();
  30. private_st_pattern2_instance();
  31. /* GC procedures */
  32. private ENUM_PTRS_BEGIN(pattern2_instance_enum_ptrs) {
  33. if (index < st_pattern2_template_max_ptrs) {
  34. gs_ptr_type_t ptype =
  35. ENUM_SUPER_ELT(gs_pattern2_instance_t, st_pattern2_template,
  36. template, 0);
  37. if (ptype)
  38. return ptype;
  39. return ENUM_OBJ(NULL); /* don't stop early */
  40. }
  41. ENUM_PREFIX(st_pattern_instance, st_pattern2_template_max_ptrs);
  42. }
  43. ENUM_PTRS_END
  44. private RELOC_PTRS_BEGIN(pattern2_instance_reloc_ptrs) {
  45. RELOC_PREFIX(st_pattern_instance);
  46. RELOC_SUPER(gs_pattern2_instance_t, st_pattern2_template, template);
  47. } RELOC_PTRS_END
  48. /* Define a PatternType 2 pattern. */
  49. private pattern_proc_uses_base_space(gs_pattern2_uses_base_space);
  50. private pattern_proc_make_pattern(gs_pattern2_make_pattern);
  51. private pattern_proc_get_pattern(gs_pattern2_get_pattern);
  52. private pattern_proc_remap_color(gs_pattern2_remap_color);
  53. private pattern_proc_set_color(gs_pattern2_set_color);
  54. private const gs_pattern_type_t gs_pattern2_type = {
  55. 2, {
  56. gs_pattern2_uses_base_space, gs_pattern2_make_pattern,
  57. gs_pattern2_get_pattern, gs_pattern2_remap_color,
  58. gs_pattern2_set_color,
  59. }
  60. };
  61. /* Initialize a PatternType 2 pattern. */
  62. void
  63. gs_pattern2_init(gs_pattern2_template_t * ppat)
  64. {
  65. gs_pattern_common_init((gs_pattern_template_t *)ppat, &gs_pattern2_type);
  66. }
  67. /* Test whether a PatternType 2 pattern uses a base space. */
  68. private bool
  69. gs_pattern2_uses_base_space(const gs_pattern_template_t *ptemp)
  70. {
  71. return false;
  72. }
  73. /* Make an instance of a PatternType 2 pattern. */
  74. private int
  75. gs_pattern2_make_pattern(gs_client_color * pcc,
  76. const gs_pattern_template_t * pcp,
  77. const gs_matrix * pmat, gs_state * pgs,
  78. gs_memory_t * mem)
  79. {
  80. const gs_pattern2_template_t *ptemp =
  81. (const gs_pattern2_template_t *)pcp;
  82. int code = gs_make_pattern_common(pcc, pcp, pmat, pgs, mem,
  83. &st_pattern2_instance);
  84. gs_pattern2_instance_t *pinst;
  85. if (code < 0)
  86. return code;
  87. pinst = (gs_pattern2_instance_t *)pcc->pattern;
  88. pinst->template = *ptemp;
  89. pinst->shfill = false;
  90. return 0;
  91. }
  92. /* Get the template of a PatternType 2 pattern instance. */
  93. private const gs_pattern_template_t *
  94. gs_pattern2_get_pattern(const gs_pattern_instance_t *pinst)
  95. {
  96. return (const gs_pattern_template_t *)
  97. &((const gs_pattern2_instance_t *)pinst)->template;
  98. }
  99. /* Set the 'shfill' flag to a PatternType 2 pattern instance. */
  100. int
  101. gs_pattern2_set_shfill(gs_client_color * pcc)
  102. {
  103. gs_pattern2_instance_t *pinst;
  104. if (pcc->pattern->type != &gs_pattern2_type)
  105. return_error(gs_error_unregistered); /* Must not happen. */
  106. pinst = (gs_pattern2_instance_t *)pcc->pattern;
  107. pinst->shfill = true;
  108. return 0;
  109. }
  110. /* ---------------- Rendering ---------------- */
  111. /* GC descriptor */
  112. gs_private_st_ptrs_add0(st_dc_pattern2, gx_device_color, "dc_pattern2",
  113. dc_pattern2_enum_ptrs, dc_pattern2_reloc_ptrs,
  114. st_client_color, ccolor);
  115. private dev_color_proc_get_dev_halftone(gx_dc_pattern2_get_dev_halftone);
  116. private dev_color_proc_load(gx_dc_pattern2_load);
  117. private dev_color_proc_fill_rectangle(gx_dc_pattern2_fill_rectangle);
  118. private dev_color_proc_equal(gx_dc_pattern2_equal);
  119. private dev_color_proc_save_dc(gx_dc_pattern2_save_dc);
  120. /*
  121. * Define the PatternType 2 Pattern device color type. This is public only
  122. * for testing when writing PDF or PostScript.
  123. */
  124. const gx_device_color_type_t gx_dc_pattern2 = {
  125. &st_dc_pattern2,
  126. gx_dc_pattern2_save_dc, gx_dc_pattern2_get_dev_halftone,
  127. gx_dc_ht_get_phase,
  128. gx_dc_pattern2_load, gx_dc_pattern2_fill_rectangle,
  129. gx_dc_default_fill_masked, gx_dc_pattern2_equal,
  130. gx_dc_pattern_write, gx_dc_pattern_read,
  131. gx_dc_pattern_get_nonzero_comps
  132. };
  133. /* Check device color for Pattern Type 2. */
  134. bool
  135. gx_dc_is_pattern2_color(const gx_device_color *pdevc)
  136. {
  137. return pdevc->type == &gx_dc_pattern2;
  138. }
  139. /*
  140. * The device halftone used by a PatternType 2 patter is that current in
  141. * the graphic state at the time of the makepattern call.
  142. */
  143. private const gx_device_halftone *
  144. gx_dc_pattern2_get_dev_halftone(const gx_device_color * pdevc)
  145. {
  146. return ((gs_pattern2_instance_t *)pdevc->ccolor.pattern)->saved->dev_ht;
  147. }
  148. /* Load a PatternType 2 color into the cache. (No effect.) */
  149. private int
  150. gx_dc_pattern2_load(gx_device_color *pdevc, const gs_imager_state *ignore_pis,
  151. gx_device *ignore_dev, gs_color_select_t ignore_select)
  152. {
  153. return 0;
  154. }
  155. /* Remap a PatternType 2 color. */
  156. private int
  157. gs_pattern2_remap_color(const gs_client_color * pc, const gs_color_space * pcs,
  158. gx_device_color * pdc, const gs_imager_state * pis,
  159. gx_device * dev, gs_color_select_t select)
  160. {
  161. /* We don't do any actual color mapping now. */
  162. pdc->type = &gx_dc_pattern2;
  163. pdc->ccolor = *pc;
  164. pdc->ccolor_valid = true;
  165. return 0;
  166. }
  167. /*
  168. * Perform actions required at set_color time. Since PatternType 2
  169. * patterns specify a color space, we must update the overprint
  170. * information as required by that color space. We temporarily disable
  171. * overprint_mode, as it is never applicable when using shading patterns.
  172. */
  173. private int
  174. gs_pattern2_set_color(const gs_client_color * pcc, gs_state * pgs)
  175. {
  176. gs_pattern2_instance_t * pinst = (gs_pattern2_instance_t *)pcc->pattern;
  177. gs_color_space * pcs = pinst->template.Shading->params.ColorSpace;
  178. int code, save_overprint_mode = pgs->overprint_mode;
  179. pgs->overprint_mode = 0;
  180. code = pcs->type->set_overprint(pcs, pgs);
  181. pgs->overprint_mode = save_overprint_mode;
  182. return code;
  183. }
  184. /* Fill path or rect, and with a PatternType 2 color. */
  185. int
  186. gx_dc_pattern2_fill_path(const gx_device_color * pdevc,
  187. gx_path * ppath, gs_fixed_rect * rect,
  188. gx_device * dev)
  189. {
  190. gs_pattern2_instance_t *pinst =
  191. (gs_pattern2_instance_t *)pdevc->ccolor.pattern;
  192. return gs_shading_fill_path_adjusted(pinst->template.Shading, ppath, rect, dev,
  193. (gs_imager_state *)pinst->saved, !pinst->shfill);
  194. }
  195. /* Fill a rectangle with a PatternType 2 color. */
  196. private int
  197. gx_dc_pattern2_fill_rectangle(const gx_device_color * pdevc, int x, int y,
  198. int w, int h, gx_device * dev,
  199. gs_logical_operation_t lop,
  200. const gx_rop_source_t * source)
  201. {
  202. gs_fixed_rect rect;
  203. rect.p.x = int2fixed(x);
  204. rect.p.y = int2fixed(y);
  205. rect.q.x = int2fixed(x + w);
  206. rect.q.y = int2fixed(y + h);
  207. return gx_dc_pattern2_fill_path(pdevc, NULL, &rect, dev);
  208. }
  209. /* Compare two PatternType 2 colors for equality. */
  210. private bool
  211. gx_dc_pattern2_equal(const gx_device_color * pdevc1,
  212. const gx_device_color * pdevc2)
  213. {
  214. return pdevc2->type == pdevc1->type &&
  215. pdevc1->ccolor.pattern == pdevc2->ccolor.pattern;
  216. }
  217. /*
  218. * Currently patterns cannot be passed through the command list,
  219. * however vector devices need to save a color for comparing
  220. * it with another color, which appears later.
  221. * We provide a minimal support, which is necessary
  222. * for the current implementation of pdfwrite.
  223. * It is not sufficient for restoring the pattern from the saved color.
  224. */
  225. private void
  226. gx_dc_pattern2_save_dc(
  227. const gx_device_color * pdevc,
  228. gx_device_color_saved * psdc )
  229. {
  230. gs_pattern2_instance_t * pinst = (gs_pattern2_instance_t *)pdevc->ccolor.pattern;
  231. psdc->type = pdevc->type;
  232. psdc->colors.pattern2.id = pinst->pattern_id;
  233. }
  234. /* Transform a shading bounding box into device space. */
  235. /* This is just a bridge to an old code. */
  236. int
  237. gx_dc_pattern2_shade_bbox_transform2fixed(const gs_rect * rect, const gs_imager_state * pis,
  238. gs_fixed_rect * rfixed)
  239. {
  240. gs_rect dev_rect;
  241. int code = gs_bbox_transform(rect, &ctm_only(pis), &dev_rect);
  242. if (code >= 0) {
  243. rfixed->p.x = float2fixed(dev_rect.p.x);
  244. rfixed->p.y = float2fixed(dev_rect.p.y);
  245. rfixed->q.x = float2fixed(dev_rect.q.x);
  246. rfixed->q.y = float2fixed(dev_rect.q.y);
  247. }
  248. return code;
  249. }
  250. /* Get a shading bbox. Returns 1 on success. */
  251. int
  252. gx_dc_pattern2_get_bbox(const gx_device_color * pdevc, gs_fixed_rect *bbox)
  253. {
  254. gs_pattern2_instance_t *pinst =
  255. (gs_pattern2_instance_t *)pdevc->ccolor.pattern;
  256. int code;
  257. if (!pinst->template.Shading->params.have_BBox)
  258. return 0;
  259. code = gx_dc_pattern2_shade_bbox_transform2fixed(
  260. &pinst->template.Shading->params.BBox, (gs_imager_state *)pinst->saved, bbox);
  261. if (code < 0)
  262. return code;
  263. return 1;
  264. }
  265. /* Check device color for a possibly self-overlapping shading. */
  266. bool
  267. gx_dc_pattern2_can_overlap(const gx_device_color *pdevc)
  268. {
  269. gs_pattern2_instance_t * pinst;
  270. if (pdevc->type != &gx_dc_pattern2)
  271. return false;
  272. pinst = (gs_pattern2_instance_t *)pdevc->ccolor.pattern;
  273. switch (pinst->template.Shading->head.type) {
  274. case 3: case 6: case 7:
  275. return true;
  276. default:
  277. return false;
  278. }
  279. }
  280. /* Check whether a pattern color has a background. */
  281. bool gx_dc_pattern2_has_background(const gx_device_color *pdevc)
  282. {
  283. gs_pattern2_instance_t * pinst;
  284. const gs_shading_t *Shading;
  285. if (pdevc->type != &gx_dc_pattern2)
  286. return false;
  287. pinst = (gs_pattern2_instance_t *)pdevc->ccolor.pattern;
  288. Shading = pinst->template.Shading;
  289. return !pinst->shfill && Shading->params.Background != NULL;
  290. }