gs_patrn.ps 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. % Copyright (C) 2001, 2002 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: gs_patrn.ps,v 1.2 2002/11/13 20:23:10 alexcher Exp $
  16. % Pattern color space method dictionary.
  17. % verify that Pattern color spaces are supported
  18. /.setpatternspace where
  19. { pop }
  20. { currentfile closefile }
  21. ifelse
  22. .currentglobal true .setglobal
  23. .cspace_util begin
  24. %
  25. % <name1 | array1> get_pattern_base_cspace <null | name2 | array2>
  26. %
  27. % If the Pattern color space has a base color space, push that base
  28. % color space onto the stack. Otherwise, push a null object.
  29. %
  30. /get_pattern_base_cspace
  31. {
  32. dup type /nametype eq
  33. { pop //null }
  34. {
  35. dup length 1 gt
  36. { 1 get }
  37. { pop //null }
  38. ifelse
  39. }
  40. ifelse
  41. }
  42. bind def
  43. %
  44. % <dict> has_base_color <bool>
  45. %
  46. % Determine if a Pattern "color" includes a base color. This is the case
  47. % if the pattern dictionary has PatternType 1 and PaintType 2.
  48. %
  49. /has_base_color
  50. {
  51. dup //null eq
  52. { pop //false }
  53. {
  54. dup /PatternType get 1 eq
  55. { /PaintType get 2 eq }
  56. { pop //false }
  57. ifelse
  58. }
  59. ifelse
  60. }
  61. bind def
  62. %
  63. % <c1> ... <cn> <pattern_dict> <pattern_cspace>
  64. % get_pattern_base_color
  65. % <c1> ... <cn> <base_cspace> true
  66. % or
  67. % <?c1?> ... <?cn?> <dict> <pattern>
  68. % get_pattern_base_color
  69. % false
  70. %
  71. % If a pattern dictionary has a base color, set up that base color and
  72. % color space, and push true. Otherwise, just push false. It is assumed
  73. % that if the pattern includes a base color, the Pattern color space
  74. % has a base color space.
  75. %
  76. /get_pattern_base_color
  77. {
  78. exch //has_base_color exec
  79. { 1 get //true }
  80. { pop //false }
  81. ifelse
  82. }
  83. bind def
  84. colorspacedict
  85. /Pattern
  86. mark
  87. /cs_potential_indexed_base false
  88. /cs_potential_pattern_base false
  89. /cs_potential_alternate false
  90. /cs_potential_icc_alternate false
  91. %
  92. % We employ the same convention for describing the number of
  93. % components in a Pattern color space as is used by the graphic
  94. % library. For pattern spaces with no underlying color space,
  95. % the result is -1. If a Pattern space has an underlying color
  96. % space with n components, the result is -(n + 1).
  97. %
  98. /cs_get_ncomps
  99. {
  100. //get_pattern_base_cspace exec dup //null eq
  101. { pop 0 }
  102. //.cs_get_ncomps
  103. ifelse
  104. 1 add neg
  105. }
  106. bind
  107. % there is no "range" for a Pattern color space
  108. /cs_get_range { {} cvlit } bind
  109. /cs_get_default_color { pop //null } bind
  110. /cs_get_currentgray
  111. {
  112. //get_pattern_base_color exec
  113. //.cs_get_currentgray
  114. { 0 }
  115. ifelse
  116. }
  117. bind
  118. /cs_get_currentrgb
  119. {
  120. //get_pattern_base_color exec
  121. //.cs_get_currentrgb
  122. { 0 0 0 }
  123. ifelse
  124. }
  125. bind
  126. /cs_get_currentcmyk
  127. {
  128. //get_pattern_base_color exec
  129. //.cs_get_currentcmyk
  130. { 0 0 0 1.0 }
  131. ifelse
  132. }
  133. bind
  134. /cs_validate
  135. {
  136. dup //get_pattern_base_cspace exec dup //null eq
  137. { pop }
  138. {
  139. //.cs_validate exec //.cs_potential_pattern_base exec not
  140. //setcspace_rangecheck
  141. if
  142. }
  143. ifelse
  144. }
  145. bind
  146. % substitute the base space if appropriate
  147. /cs_substitute
  148. {
  149. dup //get_pattern_base_cspace exec dup //null eq
  150. { pop dup }
  151. {
  152. //.cs_substitute exec 2 copy eq
  153. { pop pop dup }
  154. {
  155. % retain only the new alternate space
  156. exch pop
  157. % build all new structures in local VM
  158. .currentglobal 3 1 roll //false .setglobal
  159. % construct a new array and insert the new base color space
  160. 1 index dup length array copy dup 1 4 -1 roll put
  161. % restore VM mode
  162. 3 -1 roll .setglobal
  163. }
  164. ifelse
  165. }
  166. ifelse
  167. }
  168. bind
  169. /cs_prepare {}
  170. %
  171. % Install the current color space.
  172. %
  173. % The current Ghostscript color space implementation requires that
  174. % color spaces that provide a base or alternative color space set
  175. % that base/alternative color space to be the current color space
  176. % before attempting to set the original color space.
  177. %
  178. % In principle, the only errors that are possible for .setpatternspace
  179. % (given that setcolorspace itself is legal) are limitcheck and/or
  180. % VMerror. The Ghostscript implementation allows a few others, so
  181. % we go through the full code to restore the current color space in
  182. % the event of an error.
  183. %
  184. /cs_install
  185. {
  186. dup //get_pattern_base_cspace exec dup //null eq
  187. {
  188. pop
  189. dup type /nametype eq
  190. { pop { /Pattern } cvlit }
  191. if
  192. .setpatternspace
  193. }
  194. {
  195. % save the current color space
  196. currentcolorspace
  197. % set the base color space as the current color space
  198. exch //forcesetcolorspace
  199. % set the pattern color space; restore the earlier space on error
  200. mark 2 index
  201. { .setpatternspace }
  202. stopped
  203. { cleartomark setcolorspace stop }
  204. { pop pop pop }
  205. ifelse
  206. }
  207. ifelse
  208. }
  209. bind
  210. %
  211. % Pattern dictionaries generated by makepattern will include an
  212. % Implementation entry whose value is an internal data structure.
  213. % Such structures are given executable type names that match their
  214. % internal structure names. The names used for pattern
  215. % implementations are gs_pattern1_instance_t and
  216. % gs_pattern2_instance_t. It is unfortunate to have to expose such
  217. % internal names at this level, but not easily avoided.
  218. %
  219. /cs_prepare_color
  220. {
  221. % verify that the topmost operand is a pattern dictionary
  222. 1 index dup type /dicttype ne
  223. {
  224. //null ne
  225. //setcspace_typecheck
  226. if
  227. pop
  228. }
  229. {
  230. dup /Implementation .knownget
  231. {
  232. type dup dup
  233. /gs_pattern1_instance_t ne exch /gs_pattern2_instance_t ne and
  234. exch xcheck not
  235. or
  236. //setcspace_typecheck
  237. if
  238. }
  239. //setcspace_typecheck
  240. ifelse
  241. % check if base color space operands are present
  242. dup /PatternType get 1 eq
  243. {
  244. /PaintType get 2 eq
  245. {
  246. % verify that a base color space exists
  247. //get_pattern_base_cspace exec dup //null eq
  248. //setcspace_rangecheck
  249. if
  250. exch 1 index //.cs_get_ncomps exec
  251. exch 1 index 3 add 1 roll
  252. //check_num_stack exec
  253. //.cs_get_ncomps exec 1 add -1 roll
  254. }
  255. { pop }
  256. ifelse
  257. }
  258. { pop pop }
  259. ifelse
  260. }
  261. ifelse
  262. }
  263. bind
  264. /cs_complete_setcolor //pop_1
  265. .dicttomark
  266. put
  267. end % .cspace_util
  268. .setglobal