| 1 |
% Copyright (C) 2001-2023 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 |
% Loader for CFF (compressed) fonts, including OpenType CFFs. |
| 17 |
% The following are not implemented yet: |
| 18 |
% Deleted entries in the Name Index |
| 19 |
% Embedded PostScript |
| 20 |
% Multiple Master fonts |
| 21 |
% Chameleon fonts |
| 22 |
% Synthetic fonts |
| 23 |
|
| 24 |
% ---------------- Font loading machinery ---------------- % |
| 25 |
|
| 26 |
% Augment the FONTPATH machinery so it recognizes OpenType CFF font sets. |
| 27 |
|
| 28 |
/.scanfontheaders where { |
| 29 |
/.scanfontheaders [ |
| 30 |
/.scanfontheaders .systemvar aload pop (OTTO*) |
| 31 |
] put |
| 32 |
} if |
| 33 |
|
| 34 |
% Load a font file that might be an OpenType CFF font set. |
| 35 |
|
| 36 |
% <file> .init_otto_font_file <file> |
| 37 |
/.init_otto_font_file { |
| 38 |
/FontSetInit /ProcSet findresource begin |
| 39 |
2 dict begin |
| 40 |
/f exch def /cff //null def |
| 41 |
card32 pop card16 6 { next pop } repeat dup { |
| 42 |
% Stack: numtables tablesleft |
| 43 |
dup 0 eq { |
| 44 |
pop pop /.loadottofontfile cvx /invalidfont signalerror |
| 45 |
} if |
| 46 |
f 4 string readstring pop (CFF ) eq { sub exit } if |
| 47 |
f 12 string readstring pop pop 1 sub % skip to next table |
| 48 |
} loop |
| 49 |
% Stack: tablesread |
| 50 |
card32 pop card32 card32 |
| 51 |
% Stack: tablesread start length |
| 52 |
exch 3 -1 roll 1 add 16 mul 12 add sub |
| 53 |
f exch subfilefilter flushfile % skip to start |
| 54 |
f exch subfilefilter end |
| 55 |
} .forcebind odef |
| 56 |
|
| 57 |
% <file> .init_otto_font_file <file> |
| 58 |
/.init_wOFF_otto_font_file { |
| 59 |
/FontSetInit /ProcSet findresource begin |
| 60 |
2 dict begin |
| 61 |
/f exch def /cff //null def |
| 62 |
3 {card32 pop} repeat % (wOFF), (OTTO) and file length |
| 63 |
|
| 64 |
card16 |
| 65 |
30 { next pop } repeat |
| 66 |
dup |
| 67 |
{ |
| 68 |
% Stack: numtables tablesleft |
| 69 |
dup 0 eq { |
| 70 |
pop pop /.loadottofontfile cvx /invalidfont signalerror |
| 71 |
} if |
| 72 |
f 4 string readstring pop (CFF ) eq { sub exit } if |
| 73 |
f 12 string readstring pop pop 1 sub % skip to next table |
| 74 |
} loop |
| 75 |
% Stack: tablesread |
| 76 |
4 { card32 } repeat |
| 77 |
% Stack: tablesread start complen len checksum |
| 78 |
pop |
| 79 |
% Stack: tablesread start complen len |
| 80 |
4 -2 roll exch |
| 81 |
% Stack: complen len start tablesread |
| 82 |
1 add 20 mul 44 add sub |
| 83 |
% Stack: complen len offset |
| 84 |
f exch subfilefilter flushfile % skip to start |
| 85 |
% Stack: complen len |
| 86 |
% the table can legally be uncompressed: complen == len |
| 87 |
1 index 1 index eq |
| 88 |
{ exch pop f exch subfilefilter} |
| 89 |
{ pop f exch subfilefilter /FlateDecode filter} ifelse |
| 90 |
end |
| 91 |
} .internalbind def |
| 92 |
|
| 93 |
% <file> .loadfontfile - |
| 94 |
/.loadnonottofontfile /.loadfontfile load def |
| 95 |
/.loadfontfile { |
| 96 |
dup (12345678) .peekstring pop (wOFFOTTO) eq |
| 97 |
{ |
| 98 |
//.init_wOFF_otto_font_file exec |
| 99 |
//true //false |
| 100 |
}{//true} ifelse |
| 101 |
|
| 102 |
{ |
| 103 |
dup 4 string .peekstring pop (OTTO) eq |
| 104 |
{ |
| 105 |
% If this is a font at all, it's an OpenType CFF font set. |
| 106 |
.init_otto_font_file //true |
| 107 |
} |
| 108 |
{ //false } ifelse |
| 109 |
} if |
| 110 |
|
| 111 |
{ % Use a random FontSet resource name. ****** WRONG ****** |
| 112 |
realtime rand xor =string cvs exch //false //false |
| 113 |
ReadData pop |
| 114 |
} { |
| 115 |
% Not a TrueType font. |
| 116 |
.loadnonottofontfile |
| 117 |
} ifelse |
| 118 |
} .internalbind def |
| 119 |
|
| 120 |
currentdict /.init_wOFF_otto_font_file .forceundef |
| 121 |
|
| 122 |
20 dict begin |
| 123 |
% ------ Utilities ------ % |
| 124 |
|
| 125 |
/subfilefilter { % <file> <length> subfilefilter <filter> |
| 126 |
% SubFileDecode interprets a length of 0 as infinite. |
| 127 |
dup 0 le { pop pop () 0 } if () /SubFileDecode filter |
| 128 |
} .internalbind def |
| 129 |
|
| 130 |
|
| 131 |
/advance { % <n> advance - |
| 132 |
f cff eq { pos add /pos exch store } { pop } ifelse |
| 133 |
} .internalbind def |
| 134 |
/next { % - next <byte> |
| 135 |
f read { |
| 136 |
1 advance |
| 137 |
CFFDEBUG { ( ) print dup = } if |
| 138 |
} { |
| 139 |
0 |
| 140 |
CFFDEBUG { ( Out of range access, assuming 0) = } if |
| 141 |
/pdfformaterror where { |
| 142 |
pop |
| 143 |
( **** Warning: Out of range access to a CFF table, assuming 0.\n) |
| 144 |
pdfformaterror |
| 145 |
} if |
| 146 |
} ifelse |
| 147 |
} .internalbind def |
| 148 |
/next2 { % - next2 <byte1> <byte2> |
| 149 |
f read { |
| 150 |
f read { |
| 151 |
2 advance |
| 152 |
CFFDEBUG { ( ) print 1 index =only (,) print dup = } if |
| 153 |
} { |
| 154 |
1 advance |
| 155 |
CFFDEBUG { ( ) print dup = } if |
| 156 |
} ifelse |
| 157 |
} if |
| 158 |
} .internalbind def |
| 159 |
/nextstring { % <length> nextstring <string> |
| 160 |
dup 0 eq { |
| 161 |
pop () |
| 162 |
} { |
| 163 |
string f exch readstring pop dup length advance |
| 164 |
CFFDEBUG { ( ) print dup //== exec } if |
| 165 |
} ifelse |
| 166 |
} .internalbind def |
| 167 |
|
| 168 |
/card8 % - card8 <card8> |
| 169 |
/next load |
| 170 |
def |
| 171 |
/card16 { % - card16 <card16> |
| 172 |
next2 exch 8 bitshift add |
| 173 |
} .internalbind def |
| 174 |
/card32 { % - card32 <card32> |
| 175 |
card16 16 bitshift card16 add |
| 176 |
} .internalbind def |
| 177 |
/offsetprocs [ |
| 178 |
/card8 load |
| 179 |
/card16 load |
| 180 |
{ card8 16 bitshift card16 add } .internalbind |
| 181 |
/card32 load |
| 182 |
] readonly def |
| 183 |
/offsetproc { % <offsize> offsetproc <proc> |
| 184 |
1 sub //offsetprocs exch get |
| 185 |
} .internalbind def |
| 186 |
/offset { % <offsize> offset <offset> |
| 187 |
offsetproc exec |
| 188 |
} .internalbind def |
| 189 |
/sid % - <sid> sid |
| 190 |
/card16 load |
| 191 |
def |
| 192 |
|
| 193 |
% ------ Main program ------ % |
| 194 |
|
| 195 |
% We need to pass the file as a parameter for the sake of the PDF |
| 196 |
% interpreter. Also for the sake of PDF, a flag forces the font |
| 197 |
% to be defined as <resname> instead of the name embedded in the data. |
| 198 |
% This is needed for subsetted fonts; it is valid if the CFF |
| 199 |
% contains only a single font. |
| 200 |
% Finally, PDF interpreter may request creation of CIDFont out of an |
| 201 |
% ordinary CFF font. |
| 202 |
/StartData { % <resname> <nbytes> StartData - |
| 203 |
currentfile exch subfilefilter //false //false ReadData pop |
| 204 |
} .internalbind def |
| 205 |
/ReadData { % <resname> <file> <forceresname> <forcecid> ReadData <fontset> |
| 206 |
% Initialize. |
| 207 |
|
| 208 |
30 dict begin |
| 209 |
/forcecidfont exch def |
| 210 |
/forceresname exch def |
| 211 |
/cff exch def |
| 212 |
/pos 0 def |
| 213 |
/resname exch cvlit def |
| 214 |
/DEBUG CFFDEBUG def % bring the binding closer |
| 215 |
/StringCache 1 dict def % Private DICT may be reused. |
| 216 |
|
| 217 |
forcecidfont |
| 218 |
[ { cff 1024 string readstring not { exit } if } loop ] |
| 219 |
.parsecff /fonts exch def |
| 220 |
|
| 221 |
resname |
| 222 |
mark fonts { |
| 223 |
forceresname { exch pop resname exch } if |
| 224 |
dup /CIDFontType known { % This is a CIDFont. |
| 225 |
dup /CIDFontName 3 index put |
| 226 |
1 index exch /CIDFont defineresource |
| 227 |
} { % This is a font. |
| 228 |
dup /FontName 3 index put |
| 229 |
dup /FontType 2 put |
| 230 |
1 index exch |
| 231 |
definefont |
| 232 |
} ifelse |
| 233 |
} forall .dicttomark |
| 234 |
|
| 235 |
end % temporary dict |
| 236 |
end % FontSetInit ProcSet |
| 237 |
/FontSet defineresource |
| 238 |
|
| 239 |
} .internalbind def |
| 240 |
|
| 241 |
% ---------------- Resource category definition ---------------- % |
| 242 |
|
| 243 |
currentdict end readonly |
| 244 |
|
| 245 |
languagelevel exch 2 .setlanguagelevel |
| 246 |
|
| 247 |
/FontSet /Generic /Category findresource dup length dict .copydict |
| 248 |
/Category defineresource pop |
| 249 |
|
| 250 |
/FontSetInit exch /ProcSet defineresource pop |
| 251 |
|
| 252 |
.setlanguagelevel |