1
0

gs_ll3.ps 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. % Copyright (C) 1997, 2000 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: gs_ll3.ps,v 1.10 2001/07/12 05:47:07 lpd Exp $
  18. % Initialization file for PostScript LanguageLevel 3 functions.
  19. % This file must be loaded after gs_lev2.ps and gs_res.ps.
  20. % These definitions go into ll3dict or various ProcSets.
  21. % NOTE: the interpreter creates ll3dict.
  22. ll3dict begin
  23. % We need LanguageLevel 2 or higher in order to have setuserparams and
  24. % defineresource.
  25. languagelevel dup 2 max .setlanguagelevel
  26. % ------ Idiom recognition ------ %
  27. /IdiomRecognition false .definepsuserparam
  28. % Modify `bind' to apply idiom recognition afterwards.
  29. /.bindscratch 128 string def
  30. % Do the right thing if NOBIND or DELAYBIND is in effect.
  31. % Note also that since this definition of `bind' may get bound in,
  32. % it has to function properly even at lower language levels,
  33. % where IdiomRecognition may not be defined.
  34. /bind load /.bind load ne
  35. /bind { % <proc> bind <proc'>
  36. //.bind currentuserparams /IdiomRecognition
  37. .knownget not { false } if {
  38. (*) {
  39. /IdiomSet findresource
  40. false exch {
  41. % Stack: proc false dummykey [template substitute]
  42. exch pop dup 1 get exch 0 get
  43. % Stack: proc false substitute template
  44. 3 index .eqproc {
  45. 2 index gcheck 1 index gcheck not and {
  46. pop
  47. } {
  48. 3 -1 roll pop exch not exit
  49. } ifelse
  50. } {
  51. pop
  52. } ifelse
  53. } forall { exit } if
  54. } //.bindscratch /IdiomSet resourceforall
  55. } if
  56. } odef
  57. { /.bind /bind load def
  58. /bind { } def
  59. } if
  60. currentdict /.bindscratch .undef
  61. % ------ HalftoneTypes 6, 10, 16 and HalftoneMode ------ %
  62. % This code depends on an internal HalftoneType 7 with the following keys:
  63. % Width, Height, Width2, Height2, TransferFunction:
  64. % as for HalftoneType 16.
  65. % Thresholds: a string or bytestring holding the thresholds,
  66. % (Width x Height + Width2 x Height2) x BitsPerSample / 8 bytes,
  67. % as for HalftoneType 16 except that the samples may be either
  68. % 8 or 16 bits wide.
  69. % BitsPerSample: 8 or 16.
  70. % Note that this HalftoneType never appears in halftone dictionaries given
  71. % to sethalftone, only as a component in those given to .sethalftone5,
  72. % so its numeric value can be chosen ad lib as long as it differs from the
  73. % other values that are legal in component dictionaries for .sethalftone5
  74. % (currently only 1 and 3).
  75. /.makehalftone7 { % <dict> <dict> <source> <Width> <Height>
  76. % (<Width2> <Height2> | null) <BPS> .makehalftone7
  77. % <setdict> <dict5> { .sethalftone5 }
  78. 8 dict begin
  79. /HalftoneType 7 def
  80. /BitsPerSample exch def
  81. dup null eq {
  82. pop 0
  83. } {
  84. /Height2 1 index def /Width2 2 index def mul
  85. } ifelse 3 1 roll
  86. /Height 1 index def
  87. /Width 2 index def
  88. mul add BitsPerSample 8 idiv mul .bigstring
  89. % Stack: dict dict source str
  90. dup type /stringtype eq { readstring } { .readbytestring } ifelse
  91. not { /sethalftone load /rangecheck signalerror exit } if
  92. readonly /Thresholds exch def
  93. /TransferFunction .knownget { /TransferFunction exch def } if
  94. % If the original Thresholds was a file, replace it with
  95. % a new one.
  96. dup /Thresholds get type /filetype eq {
  97. dup /Thresholds [ Thresholds ] cvx 0 () .subfiledecode put
  98. } if
  99. mark /HalftoneType 5 /Default currentdict end .dicttomark
  100. { .sethalftone5 }
  101. } bind def
  102. /.bigstring { % <size> .bigstring <string|bytestring>
  103. dup 65400 gt { .bytestring } { string } ifelse
  104. } bind def
  105. /.readbytestring { % <source> <bytestring> .readbytestring
  106. % <bytestring> <filled>
  107. % Note that since bytestrings don't implement getinterval,
  108. % if filled is false, there is no way to tell how much
  109. % was read.
  110. true exch 0 1 2 index length 1 sub {
  111. % Stack: source true str index
  112. 3 index read not { pop exch not exch exit } if
  113. 3 copy put pop pop
  114. } for 3 -1 roll pop exch
  115. } bind def
  116. /.sethalftone6 { % <dict> <dict> .sethalftone6 <setdict> <dict5>
  117. % { .sethalftone5 }
  118. % Keys: Width, Height, Thresholds, T'Function
  119. dup /Thresholds get
  120. 1 index /Width get 2 index /Height get
  121. null 8 .makehalftone7
  122. } bind def
  123. /.sethalftone10 { % <dict> <dict> .sethalftone10 <setdict> <dict5>
  124. % { .sethalftone5 }
  125. % Keys: Xsquare, Ysquare, Thresholds, T'Function
  126. % Note that this is the only one of these three HalftoneTypes
  127. % that allows either a file or a string for Thresholds.
  128. dup /Thresholds get dup type /stringtype eq { 0 () .subfiledecode } if
  129. 1 index /Xsquare get dup 3 index /Ysquare get dup
  130. 8 .makehalftone7
  131. } bind def
  132. /.sethalftone16 { % <dict> <dict> .sethalftone16 <setdict> <dict5>
  133. % { .sethalftone5 }
  134. % Keys: Width, Height, Width2, Height2,
  135. % Thresholds, T'Function
  136. dup /Thresholds get
  137. 1 index /Width get 2 index /Height get
  138. 3 index /Width2 .knownget { % 2-rectangle case
  139. 4 index /Height2 get
  140. } { % 1-rectangle case
  141. null
  142. } ifelse 16 .makehalftone7
  143. } bind def
  144. .halftonetypes begin
  145. 6 /.sethalftone6 load def
  146. 10 /.sethalftone10 load def
  147. 16 /.sethalftone16 load def
  148. end
  149. % Redefine the halftone-setting operators to honor HalftoneMode.
  150. /setcolorscreen {
  151. /HalftoneMode getuserparam 0 eq {
  152. //setcolorscreen
  153. } {
  154. 12 { pop } repeat .getdefaulthalftone
  155. { //sethalftone }
  156. { .setdefaulthalftone }
  157. ifelse
  158. } ifelse
  159. } odef
  160. /setscreen {
  161. /HalftoneMode getuserparam 0 eq {
  162. //setscreen
  163. } {
  164. pop pop pop .getdefaulthalftone
  165. { //sethalftone }
  166. { .setdefaulthalftone }
  167. ifelse
  168. } ifelse
  169. } odef
  170. /sethalftone {
  171. /HalftoneMode getuserparam 0 eq {
  172. //sethalftone
  173. } {
  174. pop .getdefaulthalftone
  175. { //sethalftone }
  176. { .setdefaulthalftone }
  177. ifelse
  178. } ifelse
  179. } odef
  180. % ------ ImageTypes 3 and 4 (masked images) ------ %
  181. .imagetypes
  182. dup 3 /.image3 load put
  183. dup 4 /.image4 load put
  184. % We also detect ImageType 103 here: it isn't worth making a separate file
  185. % just for this.
  186. /.image3x where { pop dup 103 /.image3x load put } if
  187. pop
  188. % ------ Functions ------ %
  189. % Define the FunctionType resource category.
  190. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  191. /InstanceType /integertype def
  192. /FunctionType currentdict end /Category defineresource pop
  193. {0 2 3} { dup /FunctionType defineresource pop } forall
  194. % ------ Smooth shading ------ %
  195. % Define the ShadingType resource category.
  196. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  197. /InstanceType /integertype def
  198. /ShadingType currentdict end /Category defineresource pop
  199. systemdict /.shadingtypes mark % not ll3dict
  200. 1 /.buildshading1 load
  201. 2 /.buildshading2 load
  202. 3 /.buildshading3 load
  203. 4 /.buildshading4 load
  204. 5 /.buildshading5 load
  205. 6 /.buildshading6 load
  206. 7 /.buildshading7 load
  207. .dicttomark put
  208. systemdict /.reuseparamdict mark
  209. /Intent 2
  210. /AsyncRead false
  211. /CloseSource true
  212. .dicttomark readonly put
  213. /.buildshading { % <shadingdict> .buildshading <shading>
  214. % Unfortunately, we always need to make the DataSource reusable,
  215. % because if clipping is involved, even shfill may need to read
  216. % the source data multiple times. If it weren't for this,
  217. % we would only need to create a reusable stream if the ultimate
  218. % source of the data is a procedure (since the library can't
  219. % suspend shading to do a procedure callout).
  220. dup /DataSource .knownget {
  221. dup type /filetype eq {
  222. //.reuseparamdict /ReusableStreamDecode filter
  223. .currentglobal 1 index gcheck .setglobal
  224. % Stack: shdict rsdfile saveglobal
  225. 2 index dup length dict copy exch .setglobal
  226. dup /DataSource 4 -1 roll put exch pop
  227. } {
  228. pop
  229. } ifelse
  230. } if
  231. % The .buildshading operators use the current color space
  232. % for ColorSpace.
  233. dup /ShadingType get //.shadingtypes exch get
  234. 1 index /ColorSpace get setcolorspace exec
  235. } bind def
  236. systemdict /.reuseparamdict undef
  237. /.buildpattern2 { % <template> <matrix> .buildpattern2
  238. % <template> <pattern>
  239. % We want to build the pattern without doing gsave/grestore,
  240. % since we want it to load the CIE caches.
  241. 1 index /Shading get
  242. mark currentcolor currentcolorspace
  243. counttomark 4 add -3 roll mark 4 1 roll
  244. % Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
  245. { .buildshading } stopped {
  246. cleartomark setcolorspace setcolor pop stop
  247. } if
  248. .buildshadingpattern
  249. 3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
  250. } bind def
  251. .patterntypes
  252. 2 /.buildpattern2 load put
  253. /shfill { % <shadingdict> shfill -
  254. % Currently, .shfill requires that the color space
  255. % in the pattern be the current color space.
  256. dup gsave { .buildshading .shfill } stopped grestore { stop } if
  257. pop
  258. } odef
  259. % Establish an initial smoothness value that matches Adobe RIPs.
  260. 0.02 setsmoothness
  261. % ------ UseCIEColor ------ %
  262. % The library maintains and detects the UseCIEColor device parameter,
  263. % but it doesn't have access to the resource dictionaries. We also
  264. % want color space substitution to work in systems without a PostScript
  265. % interpreter. Therefore, we eagerly inform the library of changes in
  266. % the (effective) ColorSpace category that might affect the operation of
  267. % UseCIEColor. We must notice the following events:
  268. % 1) defineresource and undefineresource of the Default ColorSpaces.
  269. % 2) restore.
  270. % 3) Changes in current VM, which cause a different set of resources
  271. % to become visible.
  272. % #1 is rare. #2 is handled in C code. If checking the ColorSpace
  273. % category on #3 turns out to be expensive, we can cache more information
  274. % about whether these operations actually affect UseCIEColor.
  275. % This operator implements color space substitution in the library:
  276. % <index> <bool> .setsubstitutecolorspace -
  277. % <bool> = true means substitute the current color space for the one given
  278. % by <index>; <bool> = false means stop substituting. Substitution is not
  279. % affected by grestore/setgstate, but it is affected by restore.
  280. % ColorSpace defineresource and undefineresource for the Default keys
  281. % call .definedefaultcs and .undefinedefaultcs. See gs_res.ps.
  282. /.useciecolorkeydict mark
  283. /UseCIEColor null
  284. .dicttomark readonly def
  285. /.definedefaultcs { % <index> <value> .definedefaultcs -
  286. currentcolorspace
  287. % Temporarily disable color substitution, in case the substitute
  288. % color space is or mentions a color space that is currently
  289. % being substituted.
  290. currentdevice //.useciecolorkeydict .getdeviceparams exch pop exch pop
  291. mark 5 -2 roll
  292. % Stack: cspace UseCIEColor mark index value
  293. { setcolorspace true .setsubstitutecolorspace }
  294. stopped counttomark 1 add 1 roll cleartomark
  295. % Stack: cspace UseCIEColor stopped?
  296. 3 1 roll
  297. currentdevice null true mark /UseCIEColor 6 -1 roll .putdeviceparams pop pop
  298. setcolorspace { stop } if
  299. } bind def
  300. currentdict /.useciecolorkeydict undef
  301. /.undefinedefaultcs { % <index> .undefinedefaultcs -
  302. false .setsubstitutecolorspace
  303. } bind def
  304. /.setdefaultcs { % <index> <value|null> .setdefaultcs -
  305. dup null eq { pop .undefinedefaultcs } { .definedefaultcs } ifelse
  306. } bind def
  307. /.getdefaultcs { % <key> .getdefaultcs <value|null>
  308. .GetInstance { 0 get } { null } ifelse
  309. } bind def
  310. /.setglobal { % <bool> .setglobal -
  311. dup .currentglobal ne {
  312. % If there are no local definitions of the Default keys,
  313. % changing VM can't affect the definition of any resource.
  314. /.localcsdefaults .uservar {
  315. % We only want to change substitutions for color spaces
  316. % whose definitions are actually changing.
  317. /ColorSpace /Category findresource begin
  318. /DefaultGray .getdefaultcs
  319. /DefaultRGB .getdefaultcs
  320. /DefaultCMYK .getdefaultcs
  321. end
  322. 3 index .setglobal
  323. /ColorSpace /Category findresource begin
  324. /DefaultGray .getdefaultcs
  325. /DefaultRGB .getdefaultcs
  326. /DefaultCMYK .getdefaultcs
  327. end
  328. % Stack: bool oldgray oldrgb oldcmyk
  329. % newgray newrgb newcmyk
  330. dup 4 index ne { 2 exch .setdefaultcs } { pop } ifelse
  331. dup 4 index ne { 1 exch .setdefaultcs } { pop } ifelse
  332. dup 4 index ne { 0 exch .setdefaultcs } { pop } ifelse
  333. pop pop pop pop
  334. } {
  335. .setglobal
  336. } ifelse
  337. } {
  338. .setglobal
  339. } ifelse
  340. } .bind odef % bind in .setglobal
  341. % Define default device color spaces to allow substitution
  342. % on UseCIEColor == true :
  343. /DefaultGray
  344. [ /CIEBasedA
  345. 3 dict begin
  346. /WhitePoint [ 0.9505 1.0 1.089 ] def
  347. /DecodeLMN [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
  348. /MatrixLMN [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  349. currentdict end
  350. ]
  351. /ColorSpace defineresource pop
  352. /DefaultRGB
  353. [ /CIEBasedABC
  354. 3 dict begin
  355. /WhitePoint [ 0.9505 1.0 1.089 ] def
  356. /DecodeLMN [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
  357. /MatrixLMN [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  358. currentdict end
  359. ]
  360. /ColorSpace defineresource pop
  361. /DefaultCMYK
  362. [ /CIEBasedDEFG
  363. 4 dict begin
  364. /WhitePoint [ 0.9505 1.0 1.089 ] def
  365. /Table [ 2 2 2 2
  366. % Trivial table :
  367. % [
  368. % [ < FFFFFF 000000 FFFF00 000000>
  369. % < FF00FF 000000 FF0000 000000> ]
  370. % [ < 00FFFF 000000 00FF00 000000>
  371. % < 0000FF 000000 000000 000000> ]
  372. % ]
  373. % AcrobatReader-compatible table :
  374. [
  375. [ < FFFFFF 000000 FFFF00 000000>
  376. < EA007F 000000 FC000F 000000> ]
  377. [ < 00A0C8 000000 008436 000000>
  378. < 00007C 000000 000000 000000> ]
  379. ]
  380. ] def
  381. /DecodeLMN [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
  382. /MatrixLMN [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  383. currentdict end
  384. ]
  385. /ColorSpace defineresource pop
  386. % ------ DeviceN color space ------ %
  387. % This isn't quite right, because the ColorSpaceFamily resource will exist
  388. % even with languagelevel < 3, but it's close enough.
  389. /.setdevicenspace where {
  390. pop colorspacedict /DeviceN {
  391. dup 2 get setcolorspace dup
  392. dup 1 get length % # of components
  393. .converttinttransform .setdevicenspace
  394. } bind put
  395. } if
  396. % ------ Miscellaneous ------ %
  397. % Define additional user and system parameters.
  398. /HalftoneMode 0 .definepsuserparam
  399. /MaxSuperScreen 1016 .definepsuserparam
  400. pssystemparams begin % read-only, so use .forcedef
  401. /MaxDisplayAndSourceList 160000 .forcedef
  402. end
  403. % Define the IdiomSet resource category.
  404. { /IdiomSet } {
  405. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  406. /InstanceType /dicttype def
  407. currentdict end /Category defineresource pop
  408. } forall
  409. /languagelevel 3 def
  410. % When running in LanguageLevel 3 mode, this interpreter is supposed to be
  411. % compatible with Adobe version 3010.
  412. /version (3010) readonly def
  413. .setlanguagelevel
  414. end % ll3dict