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