星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gs_cidfn.ps 13KB

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