gs_diskf.ps 7.6 KB

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