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