Debian Codegraph @

Resource/Init/gs_cidfn.ps

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