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