Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 1 | """fontTools.t1Lib.py -- Tools for PostScript Type 1 fonts |
| 2 | |
| 3 | Functions for reading and writing raw Type 1 data: |
| 4 | |
| 5 | read(path) |
| 6 | reads any Type 1 font file, returns the raw data and a type indicator: |
| 7 | 'LWFN', 'PFB' or 'OTHER', depending on the format of the file pointed |
| 8 | to by 'path'. |
| 9 | Raises an error when the file does not contain valid Type 1 data. |
| 10 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 11 | write(path, data, kind='OTHER', dohex=0) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 12 | writes raw Type 1 data to the file pointed to by 'path'. |
| 13 | 'kind' can be one of 'LWFN', 'PFB' or 'OTHER'; it defaults to 'OTHER'. |
| 14 | 'dohex' is a flag which determines whether the eexec encrypted |
| 15 | part should be written as hexadecimal or binary, but only if kind |
| 16 | is 'LWFN' or 'PFB'. |
| 17 | """ |
| 18 | |
| 19 | __author__ = "jvr" |
| 20 | __version__ = "1.0b2" |
| 21 | DEBUG = 0 |
| 22 | |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 23 | from fontTools.misc import eexec |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 24 | import string |
| 25 | import re |
| 26 | import os |
| 27 | |
| 28 | if os.name == 'mac': |
| 29 | import Res |
| 30 | import macfs |
| 31 | |
| 32 | error = 't1Lib.error' |
| 33 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 34 | |
| 35 | class T1Font: |
| 36 | |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 37 | """Type 1 font class. |
| 38 | |
| 39 | Uses a minimal interpeter that supports just about enough PS to parse |
| 40 | Type 1 fonts. |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 41 | """ |
| 42 | |
| 43 | def __init__(self, path=None): |
| 44 | if path is not None: |
| 45 | self.data, type = read(path) |
| 46 | else: |
| 47 | pass # XXX |
| 48 | |
| 49 | def saveAs(self, path, type): |
| 50 | self.write(path, self.getData(), type) |
| 51 | |
| 52 | def getData(self): |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 53 | # XXX Todo: if the data has been converted to Python object, |
| 54 | # recreate the PS stream |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 55 | return self.data |
| 56 | |
| 57 | def __getitem__(self, key): |
| 58 | if not hasattr(self, "font"): |
| 59 | self.parse() |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 60 | return self.font[key] |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 61 | |
| 62 | def parse(self): |
Just | 528614e | 2000-01-16 22:14:02 +0000 | [diff] [blame] | 63 | from fontTools.misc import psLib |
| 64 | from fontTools.misc import psCharStrings |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 65 | self.font = psLib.suckfont(self.data) |
| 66 | charStrings = self.font["CharStrings"] |
| 67 | lenIV = self.font["Private"].get("lenIV", 4) |
| 68 | assert lenIV >= 0 |
| 69 | for glyphName, charString in charStrings.items(): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 70 | charString, R = eexec.decrypt(charString, 4330) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 71 | charStrings[glyphName] = psCharStrings.T1CharString(charString[lenIV:]) |
| 72 | subrs = self.font["Private"]["Subrs"] |
| 73 | for i in range(len(subrs)): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 74 | charString, R = eexec.decrypt(subrs[i], 4330) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 75 | subrs[i] = psCharStrings.T1CharString(charString[lenIV:]) |
| 76 | del self.data |
| 77 | |
| 78 | |
| 79 | |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 80 | # low level T1 data read and write functions |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 81 | |
| 82 | def read(path): |
| 83 | """reads any Type 1 font file, returns raw data""" |
| 84 | normpath = string.lower(path) |
| 85 | if os.name == 'mac': |
| 86 | fss = macfs.FSSpec(path) |
| 87 | creator, type = fss.GetCreatorType() |
| 88 | if type == 'LWFN': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 89 | return readLWFN(path), 'LWFN' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 90 | if normpath[-4:] == '.pfb': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 91 | return readPFB(path), 'PFB' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 92 | else: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 93 | return readOther(path), 'OTHER' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 94 | |
| 95 | def write(path, data, kind='OTHER', dohex=0): |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 96 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 97 | kind = string.upper(kind) |
| 98 | try: |
| 99 | os.remove(path) |
| 100 | except os.error: |
| 101 | pass |
| 102 | err = 1 |
| 103 | try: |
| 104 | if kind == 'LWFN': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 105 | writeLWFN(path, data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 106 | elif kind == 'PFB': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 107 | writePFB(path, data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 108 | else: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 109 | writeOther(path, data, dohex) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 110 | err = 0 |
| 111 | finally: |
| 112 | if err and not DEBUG: |
| 113 | try: |
| 114 | os.remove(path) |
| 115 | except os.error: |
| 116 | pass |
| 117 | |
| 118 | |
| 119 | # -- internal -- |
| 120 | |
| 121 | LWFNCHUNKSIZE = 2000 |
| 122 | HEXLINELENGTH = 80 |
| 123 | |
| 124 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 125 | def readLWFN(path): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 126 | """reads an LWFN font file, returns raw data""" |
| 127 | resref = Res.OpenResFile(path) |
| 128 | try: |
| 129 | Res.UseResFile(resref) |
| 130 | n = Res.Count1Resources('POST') |
| 131 | data = [] |
| 132 | for i in range(501, 501 + n): |
| 133 | res = Res.Get1Resource('POST', i) |
| 134 | code = ord(res.data[0]) |
| 135 | if ord(res.data[1]) <> 0: |
| 136 | raise error, 'corrupt LWFN file' |
| 137 | if code in [1, 2]: |
| 138 | data.append(res.data[2:]) |
| 139 | elif code in [3, 5]: |
| 140 | break |
| 141 | elif code == 4: |
| 142 | f = open(path, "rb") |
| 143 | data.append(f.read()) |
| 144 | f.close() |
| 145 | elif code == 0: |
| 146 | pass # comment, ignore |
| 147 | else: |
| 148 | raise error, 'bad chunk code: ' + `code` |
| 149 | finally: |
| 150 | Res.CloseResFile(resref) |
| 151 | data = string.join(data, '') |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 152 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 153 | return data |
| 154 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 155 | def readPFB(path, onlyHeader=0): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 156 | """reads a PFB font file, returns raw data""" |
| 157 | f = open(path, "rb") |
| 158 | data = [] |
| 159 | while 1: |
| 160 | if f.read(1) <> chr(128): |
| 161 | raise error, 'corrupt PFB file' |
| 162 | code = ord(f.read(1)) |
| 163 | if code in [1, 2]: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 164 | chunklen = stringToLong(f.read(4)) |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 165 | chunk = f.read(chunklen) |
| 166 | assert len(chunk) == chunklen |
| 167 | data.append(chunk) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 168 | elif code == 3: |
| 169 | break |
| 170 | else: |
| 171 | raise error, 'bad chunk code: ' + `code` |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 172 | if onlyHeader: |
| 173 | break |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 174 | f.close() |
| 175 | data = string.join(data, '') |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 176 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 177 | return data |
| 178 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 179 | def readOther(path): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 180 | """reads any (font) file, returns raw data""" |
| 181 | f = open(path, "rb") |
| 182 | data = f.read() |
| 183 | f.close() |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 184 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 185 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 186 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 187 | data = [] |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 188 | for isEncrypted, chunk in chunks: |
| 189 | if isEncrypted and isHex(chunk[:4]): |
| 190 | data.append(deHexString(chunk)) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 191 | else: |
| 192 | data.append(chunk) |
| 193 | return string.join(data, '') |
| 194 | |
| 195 | # file writing tools |
| 196 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 197 | def writeLWFN(path, data): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 198 | Res.CreateResFile(path) |
| 199 | fss = macfs.FSSpec(path) |
| 200 | fss.SetCreatorType('just', 'LWFN') |
| 201 | resref = Res.OpenResFile(path) |
| 202 | try: |
| 203 | Res.UseResFile(resref) |
| 204 | resID = 501 |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 205 | chunks = findEncryptedChunks(data) |
| 206 | for isEncrypted, chunk in chunks: |
| 207 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 208 | code = 2 |
| 209 | else: |
| 210 | code = 1 |
| 211 | while chunk: |
| 212 | res = Res.Resource(chr(code) + '\0' + chunk[:LWFNCHUNKSIZE - 2]) |
| 213 | res.AddResource('POST', resID, '') |
| 214 | chunk = chunk[LWFNCHUNKSIZE - 2:] |
| 215 | resID = resID + 1 |
| 216 | res = Res.Resource(chr(5) + '\0') |
| 217 | res.AddResource('POST', resID, '') |
| 218 | finally: |
| 219 | Res.CloseResFile(resref) |
| 220 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 221 | def writePFB(path, data): |
| 222 | chunks = findEncryptedChunks(data) |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 223 | f = open(path, "wb") |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 224 | try: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 225 | for isEncrypted, chunk in chunks: |
| 226 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 227 | code = 2 |
| 228 | else: |
| 229 | code = 1 |
| 230 | f.write(chr(128) + chr(code)) |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 231 | f.write(longToString(len(chunk))) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 232 | f.write(chunk) |
| 233 | f.write(chr(128) + chr(3)) |
| 234 | finally: |
| 235 | f.close() |
| 236 | if os.name == 'mac': |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 237 | fss = macfs.FSSpec(path) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 238 | fss.SetCreatorType('mdos', 'BINA') |
| 239 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 240 | def writeOther(path, data, dohex = 0): |
| 241 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 242 | f = open(path, "wb") |
| 243 | try: |
| 244 | hexlinelen = HEXLINELENGTH / 2 |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 245 | for isEncrypted, chunk in chunks: |
| 246 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 247 | code = 2 |
| 248 | else: |
| 249 | code = 1 |
| 250 | if code == 2 and dohex: |
| 251 | while chunk: |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 252 | f.write(eexec.hexString(chunk[:hexlinelen])) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 253 | f.write('\r') |
| 254 | chunk = chunk[hexlinelen:] |
| 255 | else: |
| 256 | f.write(chunk) |
| 257 | finally: |
| 258 | f.close() |
| 259 | if os.name == 'mac': |
| 260 | fss = macfs.FSSpec(path) |
| 261 | fss.SetCreatorType('R*ch', 'TEXT') # BBEdit text file |
| 262 | |
| 263 | |
| 264 | # decryption tools |
| 265 | |
| 266 | EEXECBEGIN = "currentfile eexec" |
| 267 | EEXECEND = '0' * 64 |
| 268 | EEXECINTERNALEND = "currentfile closefile" |
| 269 | EEXECBEGINMARKER = "%-- eexec start\r" |
| 270 | EEXECENDMARKER = "%-- eexec end\r" |
| 271 | |
| 272 | _ishexRE = re.compile('[0-9A-Fa-f]*$') |
| 273 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 274 | def isHex(text): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 275 | return _ishexRE.match(text) is not None |
| 276 | |
| 277 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 278 | def decryptType1(data): |
| 279 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 280 | data = [] |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 281 | for isEncrypted, chunk in chunks: |
| 282 | if isEncrypted: |
| 283 | if isHex(chunk[:4]): |
| 284 | chunk = deHexString(chunk) |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 285 | decrypted, R = eexec.decrypt(chunk, 55665) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 286 | decrypted = decrypted[4:] |
| 287 | if decrypted[-len(EEXECINTERNALEND)-1:-1] <> EEXECINTERNALEND \ |
| 288 | and decrypted[-len(EEXECINTERNALEND)-2:-2] <> EEXECINTERNALEND: |
| 289 | raise error, "invalid end of eexec part" |
| 290 | decrypted = decrypted[:-len(EEXECINTERNALEND)-2] + '\r' |
| 291 | data.append(EEXECBEGINMARKER + decrypted + EEXECENDMARKER) |
| 292 | else: |
| 293 | if chunk[-len(EEXECBEGIN)-1:-1] == EEXECBEGIN: |
| 294 | data.append(chunk[:-len(EEXECBEGIN)-1]) |
| 295 | else: |
| 296 | data.append(chunk) |
| 297 | return string.join(data, '') |
| 298 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 299 | def findEncryptedChunks(data): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 300 | chunks = [] |
| 301 | while 1: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 302 | eBegin = string.find(data, EEXECBEGIN) |
| 303 | if eBegin < 0: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 304 | break |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 305 | eEnd = string.find(data, EEXECEND, eBegin) |
| 306 | if eEnd < 0: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 307 | raise error, "can't find end of eexec part" |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 308 | chunks.append((0, data[:eBegin + len(EEXECBEGIN) + 1])) |
| 309 | chunks.append((1, data[eBegin + len(EEXECBEGIN) + 1:eEnd])) |
| 310 | data = data[eEnd:] |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 311 | chunks.append((0, data)) |
| 312 | return chunks |
| 313 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 314 | def deHexString(hexstring): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 315 | return eexec.deHexString(string.join(string.split(hexstring), "")) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 316 | |
| 317 | |
| 318 | # Type 1 assertion |
| 319 | |
| 320 | _fontType1RE = re.compile(r"/FontType\s+1\s+def") |
| 321 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 322 | def assertType1(data): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 323 | for head in ['%!PS-AdobeFont', '%!FontType1-1.0']: |
| 324 | if data[:len(head)] == head: |
| 325 | break |
| 326 | else: |
| 327 | raise error, "not a PostScript font" |
| 328 | if not _fontType1RE.search(data): |
| 329 | raise error, "not a Type 1 font" |
| 330 | if string.find(data, "currentfile eexec") < 0: |
| 331 | raise error, "not an encrypted Type 1 font" |
| 332 | # XXX what else? |
| 333 | return data |
| 334 | |
| 335 | |
| 336 | # pfb helpers |
| 337 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 338 | def longToString(long): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 339 | str = "" |
| 340 | for i in range(4): |
| 341 | str = str + chr((long & (0xff << (i * 8))) >> i * 8) |
| 342 | return str |
| 343 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 344 | def stringToLong(str): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 345 | if len(str) <> 4: |
| 346 | raise ValueError, 'string must be 4 bytes long' |
| 347 | long = 0 |
| 348 | for i in range(4): |
| 349 | long = long + (ord(str[i]) << (i * 8)) |
| 350 | return long |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame^] | 351 | |