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