1 % Copyright (C) 1995, 2000 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
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.
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.
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.
20 % ---------------- Defining CIDFont resources ---------------- %
22 % Define a CIDFont resource. This is the defineresource implementation for
23 % the CIDFont resource category.
25 /.checkfonttype { % <cidfont> <fonttype> .checkfonttype <cidfont> <new?>
27 1 index /FontType get ne {
28 /definefont cvx /invalidfont signalerror
31 1 index /FontType 3 -1 roll put true
35 /.cidfonttypes where { pop } { /.cidfonttypes 6 dict def } ifelse
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.
44 % ------ CIDFontType 0 (FontType 9) ------ %
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
57 /CIDInit /ProcSet findresource begin
61 1 index exch .buildfont9 exch pop
62 } put % Don't bind it here, because gs_fapi.ps redefines .buildfont9
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
71 currentglobal exch dup gcheck setglobal
72 dup /FontMatrix get [1000 0 0 1000 0 0] matrix concatmatrix
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
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>
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
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.
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
122 % We haven't loaded any Subrs at this offset yet.
123 SubrCount array 0 .readsubrs
124 SubrCache SubrMapOffset 2 index put
130 % ------ CIDFontType 1 (FontType 10) ------ %
133 10 .checkfonttype pop
134 1 index exch .buildfont10 exch pop
135 } put % Don't bind it here because gs_fapi.ps redefines .buildfont10
137 % ------ CIDFontType 2 (FontType 11) ------ %
140 11 .checkfonttype pop
141 1 index exch .buildfont11 exch pop
142 } put % Don't bind it here because gs_fapi.ps redefines .buildfont11
146 % ---------------- Reading CIDFontType 0 files ---------------- %
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
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
167 % File is not positionable, or uses hex data.
169 cleartomark exch (Hex) eq
170 { { currentfile exch readhexstring pop } }
171 { { currentfile exch readstring pop } }
173 % Stack: readproc length
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
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
186 counttomark 1 add 1 roll ]
189 % If we were reading hex data, skip past the >.
190 2 get { readhexstring } 0 get eq {
191 currentfile 0 (>) .subfiledecode dup flushfile closefile
194 % File is positionable and binary, just save a pointer.
195 % Stack: (Binary) length -mark- pos
197 exch pop 3 -1 roll pop exch
199 /DataSource currentfile 2 index () .subfiledecode true .reusablestream def
200 currentfile 3 1 roll add setfileposition
202 /SubrCache 10 dict def
203 CIDFontName currentdict /CIDFont defineresource pop
205 end % resource category dict
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.
217 % ---------------- Rendering ---------------- %
219 % ------ Generic ------ %
221 % Read a string at a given offset in a "file" (binary file or
223 /ReadString { % <pos> <string> ReadString <string>
225 % Read from the file.
226 DataSource 3 -1 roll setfileposition
227 DataSource exch readstring pop
229 % Read from a string or an array of strings.
230 GlyphData .stringsreadstring
233 /.stringsreadstring % <pos> <string> <strings> .stringsreadstring
235 { dup type /stringtype eq
236 { 3 1 roll length getinterval
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
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
250 { % Request spans 2 strings. Copy the first part.
251 1 index 0 get 4 index 3 -1 roll 1 index sub getinterval
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
264 % Interpret a byte string as a (big-endian) integer.
265 /.cvbsi % <bytes> .cvbsi <int>
266 { 0 exch { exch 8 bitshift add } forall
269 % Read an integer from binary data.
270 /.readint % <pos> <nbytes> .readint <int>
271 { string ReadString .cvbsi
274 % ------ CIDFontType 0 ------ %
277 currentfont exch .type9mapcid
278 FDArray exch get exch
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
287 dup .readglyphdata dup null eq {
288 % Substitute CID 0. **** WRONG ****
289 pop pop 0 .readglyphdata
291 % Stack: cidfont cid subfont charstring
292 dup null eq { pop pop pop pop } { %**** WRONG ****
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 ****
301 % ------ CIDFontType 2 ------ %
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
310 % Stack: cidfont cid glyphindex
311 1 index exch .type42execchar
314 % ---------------- Define resources ---------------- %
316 languagelevel exch 2 .setlanguagelevel
318 % Define the CIDInit ProcSet resource.
319 % The ProcSet dictionary is still on the stack.
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
330 } ifelse exch defineresource pop
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
338 systemdict /ProvideUnicode .knownget not { false } if {
339 /FontEmulationProcs /ProcSet findresource
340 /ProvideUnicodeDecoding get exec
342 dup /CIDFontType get //.cidfonttypes exch get exec
345 /CIDFont /Generic /Category findresource dup length dict .copydict
346 dup /InstanceType /dicttype put
347 dup /DefineResource {
349 /Generic /Category findresource /DefineResource get exec
351 % CIDFonts may be defined in CFF OpenType files.
352 % Check for this here.
353 /.loadcidfontresource {
355 {.loadfont} .execasresource
357 dup /undefinedresource signalerror
364 true setglobal {.loadcidfontresource} stopped false setglobal {stop} if
368 /Category defineresource pop
370 % Add the new FontType resources.
372 9 1 11 { dup /FontType defineresource pop } for
374 % Add the new FMapType resource.
376 9 dup /FMapType defineresource pop
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.
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)
386 dup type dup dup /stringtype eq exch /dicttype eq or {
389 dup /arraytype eq exch /packedarraytype eq or {
390 true exch { type /stringtype eq and } forall
396 /Category defineresource pop
400 %% Replace 1 (gs_ciddc.ps)
401 (gs_ciddc.ps) runlibfile