gs_cidfn.ps 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. % Copyright (C) 1995, 2000 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_cidfn.ps,v 1.27 2003/05/23 09:57:34 igor Exp $
  16. % ProcSet for implementing CIDFont and CIDMap resources.
  17. % When this is run, systemdict is still writable.
  18. % ---------------- Defining CIDFont resources ---------------- %
  19. % Define a CIDFont resource. This is the defineresource implementation for
  20. % the CIDFont resource category.
  21. /.checkfonttype { % <cidfont> <fonttype> .checkfonttype <cidfont> <new?>
  22. 1 index /FID known {
  23. 1 index /FontType get ne {
  24. /definefont cvx /invalidfont signalerror
  25. } if false
  26. } {
  27. 1 index /FontType 3 -1 roll put true
  28. } ifelse
  29. } bind def
  30. /.cidfonttypes where { pop } { /.cidfonttypes 6 dict def } ifelse
  31. .cidfonttypes
  32. 30 dict begin
  33. % The key in .cidfonttypes is the CIDFontType value;
  34. % the value is a procedure that takes a font name and the CIDFont dictionary
  35. % and replaces the latter with a real font.
  36. % ------ CIDFontType 0 (FontType 9) ------ %
  37. % We add the following entries to the CIDFont dictionary, in addition to
  38. % the ones documented by Adobe:
  39. % SubrCache - dictionary for caching Subr arrays
  40. % For CIDFonts where we read the data from disk incrementally:
  41. % GlyphData is 0 (arbitrary)
  42. % DataSource - a ReusableStreamDecode filter for the data
  43. % We also add a FID entry, and possibly a Subrs entry, to each element of
  44. % FDArray.
  45. dup 0 {
  46. 9 .checkfonttype {
  47. /CIDInit /ProcSet findresource begin
  48. .completefont9
  49. end
  50. } if
  51. 1 index exch .buildfont9 exch pop
  52. } put % Don't bind it here, because gs_fapi.ps redefines .buildfont9
  53. % Add entries to a new CIDFontType 0 font per documentation (FontMatrix)
  54. % or for .buildfont9 (FDArray.Private.Subrs).
  55. /.completefont9 { % <cidfont0> .completefont9 <cidfont0>
  56. currentglobal 3 1 roll dup gcheck setglobal
  57. dup /FontMatrix known not {
  58. dup /FontMatrix [0.001 0 0 0.001 0 0] put
  59. dup /FDArray get {
  60. currentglobal exch dup gcheck setglobal
  61. dup /FontMatrix get [1000 0 0 1000 0 0] matrix concatmatrix
  62. /FontMatrix exch put
  63. setglobal
  64. } forall
  65. } if
  66. dup /FDArray get {
  67. % Read the Subrs if necessary.
  68. dup /Private get dup /Subrs known not {
  69. dup /SubrCount .knownget {
  70. % Stack: font Private SubrCount
  71. currentglobal 3 1 roll 1 index gcheck setglobal
  72. array 1 index /Subrs 3 -1 roll put
  73. % Stack: font global Private
  74. 2 index begin begin .loadsubrs end end
  75. setglobal
  76. } {
  77. pop
  78. } ifelse readonly pop
  79. } {
  80. pop pop
  81. } ifelse
  82. } forall
  83. 3 -1 roll setglobal
  84. } bind def
  85. % Read some Subrs for the current Type 1 subfont.
  86. % The subfont's Private dict is currentdict; the CIDFont itself is the
  87. % next dictionary on the stack.
  88. /.readsubrs { % <Subrs> <start> .readsubrs <Subrs>
  89. 1 SubrCount 1 sub {
  90. dup SDBytes mul SubrMapOffset add
  91. dup SDBytes .readint exch SDBytes add SDBytes .readint
  92. 1 index sub string ReadString 2 index 3 1 roll put
  93. } for
  94. } bind def
  95. % Ensure that all the Subrs for the current Type 1 subfont are loaded.
  96. % The subfont's Private dict is currentdict; the CIDFont itself is the
  97. % next dictionary on the stack.
  98. /.loadsubrs {
  99. Subrs length 0 ne {
  100. SubrCache SubrMapOffset .knownget {
  101. % We've already loaded some Subrs at this offset.
  102. % Make sure we've got as many as we need.
  103. dup length SubrCount lt {
  104. % We need to load more.
  105. SubrCount array exch 1 index copy length .readsubrs
  106. SubrCache SubrMapOffset 2 index put
  107. } if
  108. } {
  109. % We haven't loaded any Subrs at this offset yet.
  110. SubrCount array 0 .readsubrs
  111. SubrCache SubrMapOffset 2 index put
  112. } ifelse
  113. Subrs copy pop
  114. } if
  115. } bind def
  116. % ------ CIDFontType 1 (FontType 10) ------ %
  117. dup 1 {
  118. 10 .checkfonttype pop
  119. 1 index exch .buildfont10 exch pop
  120. } put % Don't bind it here because gs_fapi.ps redefines .buildfont10
  121. % ------ CIDFontType 2 (FontType 11) ------ %
  122. dup 2 {
  123. 11 .checkfonttype pop
  124. 1 index exch .buildfont11 exch pop
  125. } put % Don't bind it here because gs_fapi.ps redefines .buildfont11
  126. pop % .cidfonttypes
  127. % ---------------- Reading CIDFontType 0 files ---------------- %
  128. /StartData { % <(Binary)|(Hex)> <datalength> StartData -
  129. % (currentdict is CID font dict)
  130. % If we're loading a resource file and the data format is
  131. % binary, we can just save a pointer to the data and load it
  132. % incrementally.
  133. mark {
  134. % Previous versions of this code made provisions for
  135. % reading hex-encoded data incrementally. Since hex data
  136. % doesn't seem to be used in practice, we no longer bother.
  137. 2 index (Binary) ne { stop } if
  138. currentfile .currentresourcefile ne { stop } if
  139. % Hack: the pdfwrite driver relies on finalization to write
  140. % out fonts. However, the font may be finalized after the
  141. % resource file, in which case the resource file will be
  142. % closed. So if the current output device is pdfwrite,
  143. % don't use incremental loading.
  144. currentdevice .devicename /pdfwrite eq { stop } if
  145. currentfile fileposition
  146. } .internalstopped {
  147. % File is not positionable, or uses hex data.
  148. % Load the data now.
  149. cleartomark exch (Hex) eq
  150. { { currentfile exch readhexstring pop } }
  151. { { currentfile exch readstring pop } }
  152. ifelse exch
  153. % Stack: readproc length
  154. dup 65400 le {
  155. % readstring with a 0-length string causes a rangecheck,
  156. % but a data length of 0 is allowed.
  157. string dup () ne { 1 index exec } if
  158. } {
  159. mark 3 1 roll {
  160. % Stack: mark str ... readproc length
  161. dup 0 eq { pop exit } if
  162. dup 65400 .min dup string 3 index exec
  163. % Stack: mark str ... readproc length newstrlen newstr
  164. 4 1 roll sub
  165. } loop
  166. counttomark 1 add 1 roll ]
  167. } ifelse
  168. /GlyphData exch def
  169. % If we were reading hex data, skip past the >.
  170. 2 get { readhexstring } 0 get eq {
  171. currentfile 0 (>) .subfiledecode dup flushfile closefile
  172. } if
  173. } {
  174. % File is positionable and binary, just save a pointer.
  175. % Stack: (Binary) length -mark- pos
  176. /GlyphData 0 def
  177. exch pop 3 -1 roll pop exch
  178. % Stack: pos length
  179. /DataSource currentfile 2 index () .subfiledecode true .reusablestream def
  180. currentfile 3 1 roll add setfileposition
  181. } ifelse
  182. /SubrCache 10 dict def
  183. CIDFontName currentdict /CIDFont defineresource pop
  184. end % CID font dict
  185. end % resource category dict
  186. } bind def
  187. % Some Adobe fonts include the line
  188. % /Setup /cid_Setup load def
  189. % This is apparently included only to prevent proper, conforming PostScript
  190. % interpreters (as opposed to ATM or a special Adobe font loader) from
  191. % loading the font, since Setup is not referenced anywhere else in the file.
  192. /cid_Setup { } def
  193. currentdict end
  194. % ---------------- Rendering ---------------- %
  195. % ------ Generic ------ %
  196. % Read a string at a given offset in a "file" (binary file or
  197. % GlyphData in RAM).
  198. /ReadString { % <pos> <string> ReadString <string>
  199. GlyphData 0 eq {
  200. % Read from the file.
  201. DataSource 3 -1 roll setfileposition
  202. DataSource exch readstring pop
  203. } {
  204. % Read from a string or an array of strings.
  205. GlyphData .stringsreadstring
  206. } ifelse
  207. } bind def
  208. /.stringsreadstring % <pos> <string> <strings> .stringsreadstring
  209. % <vmstring>
  210. { dup type /stringtype eq
  211. { 3 1 roll length getinterval
  212. }
  213. { { % Stack: pos string glyphdata
  214. dup 0 get length dup 4 index gt { exit } if
  215. 4 -1 roll exch sub 3 1 roll
  216. dup length 1 sub 1 exch getinterval
  217. }
  218. loop
  219. % Stack: pos string glyphdata glyphdata[0]length
  220. % We know no request can span more than 2 strings.
  221. 3 index 3 index length add 1 index le
  222. { % Request fits in a single string: just return a substring.
  223. pop 0 get 3 1 roll length getinterval
  224. }
  225. { % Request spans 2 strings. Copy the first part.
  226. 1 index 0 get 4 index 3 -1 roll 1 index sub getinterval
  227. 2 index copy
  228. % Copy the second part.
  229. % Stack: pos str glyphdata str1
  230. length exch 1 get 0 3 index length
  231. 3 index sub getinterval 2 index 3 1 roll putinterval
  232. exch pop
  233. }
  234. ifelse
  235. }
  236. ifelse
  237. } bind def
  238. % Interpret a byte string as a (big-endian) integer.
  239. /.cvbsi % <bytes> .cvbsi <int>
  240. { 0 exch { exch 8 bitshift add } forall
  241. } bind def
  242. % Read an integer from binary data.
  243. /.readint % <pos> <nbytes> .readint <int>
  244. { string ReadString .cvbsi
  245. } bind def
  246. % ------ CIDFontType 0 ------ %
  247. /.readglyphdata {
  248. currentfont exch .type9mapcid
  249. FDArray exch get exch
  250. } bind def
  251. % BuildGlyph procedure for CIDFontType 0.
  252. % The name %Type9BuildGlyph is known to the interpreter.
  253. /.cid0buildstring 10 string def
  254. (%Type9BuildGlyph) cvn { % <cidfont> <cid> %Type9BuildGlyph -
  255. .currentglobal 3 1 roll 1 index gcheck .setglobal
  256. 1 index begin
  257. dup .readglyphdata dup null eq {
  258. % Substitute CID 0. **** WRONG ****
  259. pop pop 0 .readglyphdata
  260. } if
  261. % Stack: cidfont cid subfont charstring
  262. dup null eq { pop pop pop pop } { %**** WRONG ****
  263. 4 -1 roll pop
  264. 3 1 roll exch dup 4 -1 roll 0 0 moveto
  265. 3 index /FontType get 2 eq { .type2execchar } { .type1execchar } ifelse
  266. } ifelse %**** WRONG ****
  267. end
  268. .setglobal
  269. } bind def
  270. % ------ CIDFontType 2 ------ %
  271. % BuildGlyph procedure for CIDFontType 2.
  272. % The name %Type11BuildGlyph is known to the interpreter.
  273. (%Type11BuildGlyph) cvn { % <cidfont> <cid> %Type11BuildGlyph -
  274. % We must be prepared for out-of-range CIDs.
  275. 2 copy { .type11mapcid } .internalstopped {
  276. pop /CharStrings get /.notdef get
  277. } if
  278. % Stack: cidfont cid glyphindex
  279. 1 index exch .type42execchar
  280. } bind def
  281. % ---------------- Define resources ---------------- %
  282. languagelevel exch 2 .setlanguagelevel
  283. % Define the CIDInit ProcSet resource.
  284. % The ProcSet dictionary is still on the stack.
  285. % We might have loaded CMap support already. However, Adobe's
  286. % protected font downloader defines a CIDInit ProcSet that will be
  287. % loaded from the filesystem later, so we must check specifically
  288. % for the ProcSet being defined in VM.
  289. /CIDInit /ProcSet 2 copy resourcestatus { pop 0 eq } { false } ifelse {
  290. pop pop findresource dup length 4 index length add dict .copydict
  291. 4 -1 roll exch .copydict
  292. } {
  293. 3 -1 roll
  294. } ifelse exch defineresource pop
  295. % Define the CIDFont resource category.
  296. % We break out .buildcidfont because it appears that at least for
  297. % Type 32 (CIDFontType 4) fonts, the font can be registered in the Font
  298. % category with only a CIDFontType and no FontType.
  299. /.buildcidfont { % <name> <fontdict> .buildcidfont
  300. % <name> <cidfont>
  301. systemdict /ProvideUnicode .knownget not { false } if {
  302. /FontEmulationProcs /ProcSet findresource
  303. /ProvideUnicodeDecoding get exec
  304. } if
  305. dup /CIDFontType get //.cidfonttypes exch get exec
  306. } odef
  307. /CIDFont /Generic /Category findresource dup length dict .copydict
  308. dup /InstanceType /dicttype put
  309. dup /DefineResource {
  310. .buildcidfont
  311. /Generic /Category findresource /DefineResource get exec
  312. } put
  313. % CIDFonts may be defined in CFF OpenType files.
  314. % Check for this here.
  315. /.loadcidfontresource {
  316. dup .ResourceFile {
  317. {.loadfont} .execasresource
  318. } {
  319. dup /undefinedresource signalerror
  320. } ifelse
  321. } bind def
  322. dup /.LoadResource {
  323. currentglobal {
  324. .loadcidfontresource
  325. } {
  326. true setglobal {.loadcidfontresource} stopped false setglobal {stop} if
  327. } ifelse
  328. } bind put
  329. /Category defineresource pop
  330. % Add the new FontType resources.
  331. 9 1 11 { dup /FontType defineresource pop } for
  332. % Add the new FMapType resource.
  333. 9 dup /FMapType defineresource pop
  334. % Define the CIDMap resource category.
  335. % These aren't documented, but it's clear what they are for:
  336. % to give names to CIDMaps for CIDFontType 2 fonts.
  337. /CIDMap /Generic /Category findresource dup length dict .copydict
  338. dup /.CheckResource {
  339. % Allow a string, an array of strings, or (as of Adobe release 3011)
  340. % a dictionary.
  341. dup type dup dup /stringtype eq exch /dicttype eq or {
  342. pop true
  343. } {
  344. dup /arraytype eq exch /packedarraytype eq or {
  345. true exch { type /stringtype eq and } forall
  346. } {
  347. false
  348. } ifelse
  349. } ifelse
  350. } bind put
  351. /Category defineresource pop
  352. .setlanguagelevel
  353. %% Replace 1 (gs_ciddc.ps)
  354. (gs_ciddc.ps) runlibfile