gs_diskf.ps 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. % Copyright (C) 1996 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_diskf.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
  16. % Support for converting Type 1 fonts without eexec encryption to
  17. % Type 4 fonts that load individual character outlines on demand.
  18. % If DISKFONTS is true, we load individual CharStrings as they are needed.
  19. % (This is intended primarily for machines with very small memories.)
  20. % Initially, the character definition is the file position of the definition;
  21. % this gets replaced with the actual CharString.
  22. % Note that if we are loading characters lazily, CharStrings is writable.
  23. % _Cstring must be long enough to hold the longest CharString for
  24. % a character defined using seac. This is lenIV + 4 * 5 (for the operands
  25. % of sbw, assuming div is not used) + 2 (for sbw) + 3 * 5 (for the operands
  26. % of seac other than the character codes) + 2 * 2 (for the character codes)
  27. % + 2 (for seac), i.e., lenIV + 43.
  28. /_Cstring 60 string def
  29. % When we initially load the font, we call
  30. % <index|charname> <length> <readproc> cskip_C
  31. % to skip over each character definition and return the file position instead.
  32. % This substitutes for the procedure
  33. % <index|charname> <length> string currentfile exch read[hex]string pop
  34. % [encrypt]
  35. % What we actually store in the CharString is fileposition * 1000 + length,
  36. % negated if the string is stored in binary form.
  37. /cskip_C
  38. { exch dup 1000 ge 3 index type /nametype ne or
  39. { % This is a Subrs string, or the string is so long we can't represent
  40. % its length. Load it now.
  41. exch exec
  42. }
  43. { % Record the position and length, and skip the string.
  44. dup currentfile fileposition 1000 mul add
  45. 2 index 3 get /readstring cvx eq { neg } if
  46. 3 1 roll
  47. dup _Cstring length idiv
  48. { currentfile _Cstring 3 index 3 get exec pop pop
  49. } repeat
  50. _Cstring length mod _Cstring exch 0 exch getinterval
  51. currentfile exch 3 -1 roll 3 get exec pop pop
  52. }
  53. ifelse
  54. } bind def
  55. % Load a CharString from the file. The font is the top entry
  56. % on the dictionary stack.
  57. /load_C % <charname> <fileposandlength> load_C -
  58. { dup abs 1000 idiv FontFile exch setfileposition
  59. CharStrings 3 1 roll
  60. .currentglobal CharStrings .gcheck .setglobal exch
  61. dup 0 lt
  62. { neg 1000 mod string FontFile exch readstring }
  63. { 1000 mod string FontFile exch readhexstring }
  64. ifelse pop
  65. exch .setglobal
  66. % If the CharStrings aren't encrypted on the file, encrypt now.
  67. Private /-| get 0 get
  68. dup type /nametype ne
  69. { dup length 5 sub 5 exch getinterval exec }
  70. { pop }
  71. ifelse dup 4 1 roll put
  72. % If the character is defined with seac, load its components now.
  73. mark exch seac_C
  74. counttomark
  75. { StandardEncoding exch get dup CharStrings exch get
  76. dup type /integertype eq { load_C } { pop pop } ifelse
  77. } repeat
  78. pop % the mark
  79. } bind def
  80. /seac_C % <charstring> seac_C <achar> <bchar> ..or nothing..
  81. { dup length _Cstring length le
  82. { 4330 exch _Cstring .type1decrypt exch pop
  83. dup dup length 2 sub 2 getinterval <0c06> eq % seac
  84. { dup length
  85. Private /lenIV known { Private /lenIV get } { 4 } ifelse
  86. exch 1 index sub getinterval
  87. % Parse the string just enough to extract the seac information.
  88. % We assume that the only possible operators are hsbw, sbw, and seac,
  89. % and that there are no 5-byte numbers.
  90. mark 0 3 -1 roll
  91. { exch
  92. { { dup 32 lt
  93. { pop 0 }
  94. { dup 247 lt
  95. { 139 sub 0 }
  96. { dup 251 lt
  97. { 247 sub 256 mul 108 add 1 1 }
  98. { 251 sub -256 mul -108 add -1 1 }
  99. ifelse
  100. }
  101. ifelse
  102. }
  103. ifelse
  104. } % 0
  105. { mul add 0 } % 1
  106. }
  107. exch get exec
  108. }
  109. forall pop
  110. counttomark 1 add 2 roll cleartomark % pop all but achar bchar
  111. }
  112. { pop % not seac
  113. }
  114. ifelse
  115. }
  116. { pop % punt
  117. }
  118. ifelse
  119. } bind def
  120. % Define replacement procedures for loading fonts.
  121. % If DISKFONTS is true and the body of the font is not encrypted with eexec:
  122. % - Prevent the CharStrings from being made read-only.
  123. % - Substitute a different CharString-reading procedure.
  124. % (eexec disables this because the implicit 'systemdict begin' hides
  125. % the redefinitions that make the scheme work.)
  126. % We assume that:
  127. % - The magic procedures (-|, -!, |-, and |) are defined with
  128. % executeonly or readonly;
  129. % - The contents of the reading procedures are as defined in bdftops.ps;
  130. % - The font includes the code
  131. % <font> /CharStrings <CharStrings> readonly put
  132. /.loadfontdict 6 dict def mark
  133. /begin % push this dict after systemdict
  134. { dup begin
  135. //systemdict eq { //.loadfontdict begin } if
  136. } bind
  137. /end % match begin
  138. { currentdict end
  139. //.loadfontdict eq currentdict //systemdict eq and { end } if
  140. } bind
  141. /dict % leave room for FontFile, BuildChar, BuildGlyph
  142. { 3 add dict
  143. } bind
  144. /executeonly % for reading procedures
  145. { readonly
  146. }
  147. /noaccess % for Subrs strings and Private dictionary
  148. { readonly
  149. }
  150. /readonly % for procedures and CharStrings dictionary
  151. { % We want to take the following non-standard actions here:
  152. % - If the operand is the CharStrings dictionary, do nothing;
  153. % - If the operand is a number (a file position replacing the
  154. % actual CharString), do nothing;
  155. % - If the operand is either of the reading procedures (-| or -!),
  156. % substitute a different one.
  157. dup type /dicttype eq % CharStrings or Private
  158. count 2 gt and
  159. { 1 index /CharStrings ne { readonly } if }
  160. { dup type /arraytype eq % procedure or data array
  161. { dup length 5 ge 1 index xcheck and
  162. { dup 0 get /string eq
  163. 1 index 1 get /currentfile eq and
  164. 1 index 2 get /exch eq and
  165. 1 index 3 get dup /readstring eq exch /readhexstring eq or and
  166. 1 index 4 get /pop eq and
  167. { /cskip_C cvx 2 packedarray cvx
  168. }
  169. { readonly
  170. }
  171. ifelse
  172. }
  173. { readonly
  174. }
  175. ifelse
  176. }
  177. { dup type /stringtype eq % must be a Subr string
  178. { readonly }
  179. if
  180. }
  181. ifelse
  182. }
  183. ifelse
  184. } bind
  185. /definefont % to insert BuildChar/Glyph and change FontType
  186. { dup /FontType get 1 eq
  187. { dup /FontType 4 put
  188. dup /BuildChar /build_C load put
  189. dup /BuildGlyph /build_C load put
  190. }
  191. if definefont
  192. } bind
  193. counttomark 2 idiv { .loadfontdict 3 1 roll put } repeat pop
  194. .loadfontdict readonly pop
  195. % Define the BuildChar and BuildGlyph procedures for modified fonts.
  196. % A single procedure serves for both.
  197. /build_C % <font> <code|name> build_C -
  198. { 1 index begin
  199. dup dup type /integertype eq { Encoding exch get } if
  200. % Stack: font code|name name
  201. dup CharStrings exch .knownget not
  202. { 2 copy eq { exch pop /.notdef exch } if
  203. QUIET not
  204. { (Substituting .notdef for ) print = flush }
  205. { pop }
  206. ifelse
  207. /.notdef CharStrings /.notdef get
  208. } if
  209. % Stack: font code|name name charstring
  210. dup type /integertype eq
  211. { load_C end build_C }
  212. { end .type1execchar }
  213. ifelse
  214. } bind def