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