__init__.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. """fontTools.t1Lib.py -- Tools for PostScript Type 1 fonts.
  2. Functions for reading and writing raw Type 1 data:
  3. read(path)
  4. reads any Type 1 font file, returns the raw data and a type indicator:
  5. 'LWFN', 'PFB' or 'OTHER', depending on the format of the file pointed
  6. to by 'path'.
  7. Raises an error when the file does not contain valid Type 1 data.
  8. write(path, data, kind='OTHER', dohex=False)
  9. writes raw Type 1 data to the file pointed to by 'path'.
  10. 'kind' can be one of 'LWFN', 'PFB' or 'OTHER'; it defaults to 'OTHER'.
  11. 'dohex' is a flag which determines whether the eexec encrypted
  12. part should be written as hexadecimal or binary, but only if kind
  13. is 'OTHER'.
  14. """
  15. import fontTools
  16. from fontTools.misc import eexec
  17. from fontTools.misc.macCreatorType import getMacCreatorAndType
  18. from fontTools.misc.textTools import bytechr, byteord, bytesjoin, tobytes
  19. from fontTools.misc.psOperators import (
  20. _type1_pre_eexec_order,
  21. _type1_fontinfo_order,
  22. _type1_post_eexec_order,
  23. )
  24. from fontTools.encodings.StandardEncoding import StandardEncoding
  25. import os
  26. import re
  27. __author__ = "jvr"
  28. __version__ = "1.0b3"
  29. DEBUG = 0
  30. try:
  31. try:
  32. from Carbon import Res
  33. except ImportError:
  34. import Res # MacPython < 2.2
  35. except ImportError:
  36. haveMacSupport = 0
  37. else:
  38. haveMacSupport = 1
  39. class T1Error(Exception):
  40. pass
  41. class T1Font(object):
  42. """Type 1 font class.
  43. Uses a minimal interpeter that supports just about enough PS to parse
  44. Type 1 fonts.
  45. """
  46. def __init__(self, path, encoding="ascii", kind=None):
  47. if kind is None:
  48. self.data, _ = read(path)
  49. elif kind == "LWFN":
  50. self.data = readLWFN(path)
  51. elif kind == "PFB":
  52. self.data = readPFB(path)
  53. elif kind == "OTHER":
  54. self.data = readOther(path)
  55. else:
  56. raise ValueError(kind)
  57. self.encoding = encoding
  58. def saveAs(self, path, type, dohex=False):
  59. write(path, self.getData(), type, dohex)
  60. def getData(self):
  61. if not hasattr(self, "data"):
  62. self.data = self.createData()
  63. return self.data
  64. def getGlyphSet(self):
  65. """Return a generic GlyphSet, which is a dict-like object
  66. mapping glyph names to glyph objects. The returned glyph objects
  67. have a .draw() method that supports the Pen protocol, and will
  68. have an attribute named 'width', but only *after* the .draw() method
  69. has been called.
  70. In the case of Type 1, the GlyphSet is simply the CharStrings dict.
  71. """
  72. return self["CharStrings"]
  73. def __getitem__(self, key):
  74. if not hasattr(self, "font"):
  75. self.parse()
  76. return self.font[key]
  77. def parse(self):
  78. from fontTools.misc import psLib
  79. from fontTools.misc import psCharStrings
  80. self.font = psLib.suckfont(self.data, self.encoding)
  81. charStrings = self.font["CharStrings"]
  82. lenIV = self.font["Private"].get("lenIV", 4)
  83. assert lenIV >= 0
  84. subrs = self.font["Private"]["Subrs"]
  85. for glyphName, charString in charStrings.items():
  86. charString, R = eexec.decrypt(charString, 4330)
  87. charStrings[glyphName] = psCharStrings.T1CharString(
  88. charString[lenIV:], subrs=subrs
  89. )
  90. for i in range(len(subrs)):
  91. charString, R = eexec.decrypt(subrs[i], 4330)
  92. subrs[i] = psCharStrings.T1CharString(charString[lenIV:], subrs=subrs)
  93. del self.data
  94. def createData(self):
  95. sf = self.font
  96. eexec_began = False
  97. eexec_dict = {}
  98. lines = []
  99. lines.extend(
  100. [
  101. self._tobytes(f"%!FontType1-1.1: {sf['FontName']}"),
  102. self._tobytes(f"%t1Font: ({fontTools.version})"),
  103. self._tobytes(f"%%BeginResource: font {sf['FontName']}"),
  104. ]
  105. )
  106. # follow t1write.c:writeRegNameKeyedFont
  107. size = 3 # Headroom for new key addition
  108. size += 1 # FontMatrix is always counted
  109. size += 1 + 1 # Private, CharStings
  110. for key in font_dictionary_keys:
  111. size += int(key in sf)
  112. lines.append(self._tobytes(f"{size} dict dup begin"))
  113. for key, value in sf.items():
  114. if eexec_began:
  115. eexec_dict[key] = value
  116. continue
  117. if key == "FontInfo":
  118. fi = sf["FontInfo"]
  119. # follow t1write.c:writeFontInfoDict
  120. size = 3 # Headroom for new key addition
  121. for subkey in FontInfo_dictionary_keys:
  122. size += int(subkey in fi)
  123. lines.append(self._tobytes(f"/FontInfo {size} dict dup begin"))
  124. for subkey, subvalue in fi.items():
  125. lines.extend(self._make_lines(subkey, subvalue))
  126. lines.append(b"end def")
  127. elif key in _type1_post_eexec_order: # usually 'Private'
  128. eexec_dict[key] = value
  129. eexec_began = True
  130. else:
  131. lines.extend(self._make_lines(key, value))
  132. lines.append(b"end")
  133. eexec_portion = self.encode_eexec(eexec_dict)
  134. lines.append(bytesjoin([b"currentfile eexec ", eexec_portion]))
  135. for _ in range(8):
  136. lines.append(self._tobytes("0" * 64))
  137. lines.extend([b"cleartomark", b"%%EndResource", b"%%EOF"])
  138. data = bytesjoin(lines, "\n")
  139. return data
  140. def encode_eexec(self, eexec_dict):
  141. lines = []
  142. # '-|', '|-', '|'
  143. RD_key, ND_key, NP_key = None, None, None
  144. lenIV = 4
  145. subrs = std_subrs
  146. # Ensure we look at Private first, because we need RD_key, ND_key, NP_key and lenIV
  147. sortedItems = sorted(eexec_dict.items(), key=lambda item: item[0] != "Private")
  148. for key, value in sortedItems:
  149. if key == "Private":
  150. pr = eexec_dict["Private"]
  151. # follow t1write.c:writePrivateDict
  152. size = 3 # for RD, ND, NP
  153. for subkey in Private_dictionary_keys:
  154. size += int(subkey in pr)
  155. lines.append(b"dup /Private")
  156. lines.append(self._tobytes(f"{size} dict dup begin"))
  157. for subkey, subvalue in pr.items():
  158. if not RD_key and subvalue == RD_value:
  159. RD_key = subkey
  160. elif not ND_key and subvalue in ND_values:
  161. ND_key = subkey
  162. elif not NP_key and subvalue in PD_values:
  163. NP_key = subkey
  164. if subkey == "lenIV":
  165. lenIV = subvalue
  166. if subkey == "OtherSubrs":
  167. # XXX: assert that no flex hint is used
  168. lines.append(self._tobytes(hintothers))
  169. elif subkey == "Subrs":
  170. for subr_bin in subvalue:
  171. subr_bin.compile()
  172. subrs = [subr_bin.bytecode for subr_bin in subvalue]
  173. lines.append(f"/Subrs {len(subrs)} array".encode("ascii"))
  174. for i, subr_bin in enumerate(subrs):
  175. encrypted_subr, R = eexec.encrypt(
  176. bytesjoin([char_IV[:lenIV], subr_bin]), 4330
  177. )
  178. lines.append(
  179. bytesjoin(
  180. [
  181. self._tobytes(
  182. f"dup {i} {len(encrypted_subr)} {RD_key} "
  183. ),
  184. encrypted_subr,
  185. self._tobytes(f" {NP_key}"),
  186. ]
  187. )
  188. )
  189. lines.append(b"def")
  190. lines.append(b"put")
  191. else:
  192. lines.extend(self._make_lines(subkey, subvalue))
  193. elif key == "CharStrings":
  194. lines.append(b"dup /CharStrings")
  195. lines.append(
  196. self._tobytes(f"{len(eexec_dict['CharStrings'])} dict dup begin")
  197. )
  198. for glyph_name, char_bin in eexec_dict["CharStrings"].items():
  199. char_bin.compile()
  200. encrypted_char, R = eexec.encrypt(
  201. bytesjoin([char_IV[:lenIV], char_bin.bytecode]), 4330
  202. )
  203. lines.append(
  204. bytesjoin(
  205. [
  206. self._tobytes(
  207. f"/{glyph_name} {len(encrypted_char)} {RD_key} "
  208. ),
  209. encrypted_char,
  210. self._tobytes(f" {ND_key}"),
  211. ]
  212. )
  213. )
  214. lines.append(b"end put")
  215. else:
  216. lines.extend(self._make_lines(key, value))
  217. lines.extend(
  218. [
  219. b"end",
  220. b"dup /FontName get exch definefont pop",
  221. b"mark",
  222. b"currentfile closefile\n",
  223. ]
  224. )
  225. eexec_portion = bytesjoin(lines, "\n")
  226. encrypted_eexec, R = eexec.encrypt(bytesjoin([eexec_IV, eexec_portion]), 55665)
  227. return encrypted_eexec
  228. def _make_lines(self, key, value):
  229. if key == "FontName":
  230. return [self._tobytes(f"/{key} /{value} def")]
  231. if key in ["isFixedPitch", "ForceBold", "RndStemUp"]:
  232. return [self._tobytes(f"/{key} {'true' if value else 'false'} def")]
  233. elif key == "Encoding":
  234. if value == StandardEncoding:
  235. return [self._tobytes(f"/{key} StandardEncoding def")]
  236. else:
  237. # follow fontTools.misc.psOperators._type1_Encoding_repr
  238. lines = []
  239. lines.append(b"/Encoding 256 array")
  240. lines.append(b"0 1 255 {1 index exch /.notdef put} for")
  241. for i in range(256):
  242. name = value[i]
  243. if name != ".notdef":
  244. lines.append(self._tobytes(f"dup {i} /{name} put"))
  245. lines.append(b"def")
  246. return lines
  247. if isinstance(value, str):
  248. return [self._tobytes(f"/{key} ({value}) def")]
  249. elif isinstance(value, bool):
  250. return [self._tobytes(f"/{key} {'true' if value else 'false'} def")]
  251. elif isinstance(value, list):
  252. return [self._tobytes(f"/{key} [{' '.join(str(v) for v in value)}] def")]
  253. elif isinstance(value, tuple):
  254. return [self._tobytes(f"/{key} {{{' '.join(str(v) for v in value)}}} def")]
  255. else:
  256. return [self._tobytes(f"/{key} {value} def")]
  257. def _tobytes(self, s, errors="strict"):
  258. return tobytes(s, self.encoding, errors)
  259. # low level T1 data read and write functions
  260. def read(path, onlyHeader=False):
  261. """reads any Type 1 font file, returns raw data"""
  262. _, ext = os.path.splitext(path)
  263. ext = ext.lower()
  264. creator, typ = getMacCreatorAndType(path)
  265. if typ == "LWFN":
  266. return readLWFN(path, onlyHeader), "LWFN"
  267. if ext == ".pfb":
  268. return readPFB(path, onlyHeader), "PFB"
  269. else:
  270. return readOther(path), "OTHER"
  271. def write(path, data, kind="OTHER", dohex=False):
  272. assertType1(data)
  273. kind = kind.upper()
  274. try:
  275. os.remove(path)
  276. except os.error:
  277. pass
  278. err = 1
  279. try:
  280. if kind == "LWFN":
  281. writeLWFN(path, data)
  282. elif kind == "PFB":
  283. writePFB(path, data)
  284. else:
  285. writeOther(path, data, dohex)
  286. err = 0
  287. finally:
  288. if err and not DEBUG:
  289. try:
  290. os.remove(path)
  291. except os.error:
  292. pass
  293. # -- internal --
  294. LWFNCHUNKSIZE = 2000
  295. HEXLINELENGTH = 80
  296. def readLWFN(path, onlyHeader=False):
  297. """reads an LWFN font file, returns raw data"""
  298. from fontTools.misc.macRes import ResourceReader
  299. reader = ResourceReader(path)
  300. try:
  301. data = []
  302. for res in reader.get("POST", []):
  303. code = byteord(res.data[0])
  304. if byteord(res.data[1]) != 0:
  305. raise T1Error("corrupt LWFN file")
  306. if code in [1, 2]:
  307. if onlyHeader and code == 2:
  308. break
  309. data.append(res.data[2:])
  310. elif code in [3, 5]:
  311. break
  312. elif code == 4:
  313. with open(path, "rb") as f:
  314. data.append(f.read())
  315. elif code == 0:
  316. pass # comment, ignore
  317. else:
  318. raise T1Error("bad chunk code: " + repr(code))
  319. finally:
  320. reader.close()
  321. data = bytesjoin(data)
  322. assertType1(data)
  323. return data
  324. def readPFB(path, onlyHeader=False):
  325. """reads a PFB font file, returns raw data"""
  326. data = []
  327. with open(path, "rb") as f:
  328. while True:
  329. if f.read(1) != bytechr(128):
  330. raise T1Error("corrupt PFB file")
  331. code = byteord(f.read(1))
  332. if code in [1, 2]:
  333. chunklen = stringToLong(f.read(4))
  334. chunk = f.read(chunklen)
  335. assert len(chunk) == chunklen
  336. data.append(chunk)
  337. elif code == 3:
  338. break
  339. else:
  340. raise T1Error("bad chunk code: " + repr(code))
  341. if onlyHeader:
  342. break
  343. data = bytesjoin(data)
  344. assertType1(data)
  345. return data
  346. def readOther(path):
  347. """reads any (font) file, returns raw data"""
  348. with open(path, "rb") as f:
  349. data = f.read()
  350. assertType1(data)
  351. chunks = findEncryptedChunks(data)
  352. data = []
  353. for isEncrypted, chunk in chunks:
  354. if isEncrypted and isHex(chunk[:4]):
  355. data.append(deHexString(chunk))
  356. else:
  357. data.append(chunk)
  358. return bytesjoin(data)
  359. # file writing tools
  360. def writeLWFN(path, data):
  361. # Res.FSpCreateResFile was deprecated in OS X 10.5
  362. Res.FSpCreateResFile(path, "just", "LWFN", 0)
  363. resRef = Res.FSOpenResFile(path, 2) # write-only
  364. try:
  365. Res.UseResFile(resRef)
  366. resID = 501
  367. chunks = findEncryptedChunks(data)
  368. for isEncrypted, chunk in chunks:
  369. if isEncrypted:
  370. code = 2
  371. else:
  372. code = 1
  373. while chunk:
  374. res = Res.Resource(bytechr(code) + "\0" + chunk[: LWFNCHUNKSIZE - 2])
  375. res.AddResource("POST", resID, "")
  376. chunk = chunk[LWFNCHUNKSIZE - 2 :]
  377. resID = resID + 1
  378. res = Res.Resource(bytechr(5) + "\0")
  379. res.AddResource("POST", resID, "")
  380. finally:
  381. Res.CloseResFile(resRef)
  382. def writePFB(path, data):
  383. chunks = findEncryptedChunks(data)
  384. with open(path, "wb") as f:
  385. for isEncrypted, chunk in chunks:
  386. if isEncrypted:
  387. code = 2
  388. else:
  389. code = 1
  390. f.write(bytechr(128) + bytechr(code))
  391. f.write(longToString(len(chunk)))
  392. f.write(chunk)
  393. f.write(bytechr(128) + bytechr(3))
  394. def writeOther(path, data, dohex=False):
  395. chunks = findEncryptedChunks(data)
  396. with open(path, "wb") as f:
  397. hexlinelen = HEXLINELENGTH // 2
  398. for isEncrypted, chunk in chunks:
  399. if isEncrypted:
  400. code = 2
  401. else:
  402. code = 1
  403. if code == 2 and dohex:
  404. while chunk:
  405. f.write(eexec.hexString(chunk[:hexlinelen]))
  406. f.write(b"\r")
  407. chunk = chunk[hexlinelen:]
  408. else:
  409. f.write(chunk)
  410. # decryption tools
  411. EEXECBEGIN = b"currentfile eexec"
  412. # The spec allows for 512 ASCII zeros interrupted by arbitrary whitespace to
  413. # follow eexec
  414. EEXECEND = re.compile(b"(0[ \t\r\n]*){512}", flags=re.M)
  415. EEXECINTERNALEND = b"currentfile closefile"
  416. EEXECBEGINMARKER = b"%-- eexec start\r"
  417. EEXECENDMARKER = b"%-- eexec end\r"
  418. _ishexRE = re.compile(b"[0-9A-Fa-f]*$")
  419. def isHex(text):
  420. return _ishexRE.match(text) is not None
  421. def decryptType1(data):
  422. chunks = findEncryptedChunks(data)
  423. data = []
  424. for isEncrypted, chunk in chunks:
  425. if isEncrypted:
  426. if isHex(chunk[:4]):
  427. chunk = deHexString(chunk)
  428. decrypted, R = eexec.decrypt(chunk, 55665)
  429. decrypted = decrypted[4:]
  430. if (
  431. decrypted[-len(EEXECINTERNALEND) - 1 : -1] != EEXECINTERNALEND
  432. and decrypted[-len(EEXECINTERNALEND) - 2 : -2] != EEXECINTERNALEND
  433. ):
  434. raise T1Error("invalid end of eexec part")
  435. decrypted = decrypted[: -len(EEXECINTERNALEND) - 2] + b"\r"
  436. data.append(EEXECBEGINMARKER + decrypted + EEXECENDMARKER)
  437. else:
  438. if chunk[-len(EEXECBEGIN) - 1 : -1] == EEXECBEGIN:
  439. data.append(chunk[: -len(EEXECBEGIN) - 1])
  440. else:
  441. data.append(chunk)
  442. return bytesjoin(data)
  443. def findEncryptedChunks(data):
  444. chunks = []
  445. while True:
  446. eBegin = data.find(EEXECBEGIN)
  447. if eBegin < 0:
  448. break
  449. eBegin = eBegin + len(EEXECBEGIN) + 1
  450. endMatch = EEXECEND.search(data, eBegin)
  451. if endMatch is None:
  452. raise T1Error("can't find end of eexec part")
  453. eEnd = endMatch.start()
  454. cypherText = data[eBegin : eEnd + 2]
  455. if isHex(cypherText[:4]):
  456. cypherText = deHexString(cypherText)
  457. plainText, R = eexec.decrypt(cypherText, 55665)
  458. eEndLocal = plainText.find(EEXECINTERNALEND)
  459. if eEndLocal < 0:
  460. raise T1Error("can't find end of eexec part")
  461. chunks.append((0, data[:eBegin]))
  462. chunks.append((1, cypherText[: eEndLocal + len(EEXECINTERNALEND) + 1]))
  463. data = data[eEnd:]
  464. chunks.append((0, data))
  465. return chunks
  466. def deHexString(hexstring):
  467. return eexec.deHexString(bytesjoin(hexstring.split()))
  468. # Type 1 assertion
  469. _fontType1RE = re.compile(rb"/FontType\s+1\s+def")
  470. def assertType1(data):
  471. for head in [b"%!PS-AdobeFont", b"%!FontType1"]:
  472. if data[: len(head)] == head:
  473. break
  474. else:
  475. raise T1Error("not a PostScript font")
  476. if not _fontType1RE.search(data):
  477. raise T1Error("not a Type 1 font")
  478. if data.find(b"currentfile eexec") < 0:
  479. raise T1Error("not an encrypted Type 1 font")
  480. # XXX what else?
  481. return data
  482. # pfb helpers
  483. def longToString(long):
  484. s = b""
  485. for i in range(4):
  486. s += bytechr((long & (0xFF << (i * 8))) >> i * 8)
  487. return s
  488. def stringToLong(s):
  489. if len(s) != 4:
  490. raise ValueError("string must be 4 bytes long")
  491. l = 0
  492. for i in range(4):
  493. l += byteord(s[i]) << (i * 8)
  494. return l
  495. # PS stream helpers
  496. font_dictionary_keys = list(_type1_pre_eexec_order)
  497. # t1write.c:writeRegNameKeyedFont
  498. # always counts following keys
  499. font_dictionary_keys.remove("FontMatrix")
  500. FontInfo_dictionary_keys = list(_type1_fontinfo_order)
  501. # extend because AFDKO tx may use following keys
  502. FontInfo_dictionary_keys.extend(
  503. [
  504. "FSType",
  505. "Copyright",
  506. ]
  507. )
  508. Private_dictionary_keys = [
  509. # We don't know what names will be actually used.
  510. # "RD",
  511. # "ND",
  512. # "NP",
  513. "Subrs",
  514. "OtherSubrs",
  515. "UniqueID",
  516. "BlueValues",
  517. "OtherBlues",
  518. "FamilyBlues",
  519. "FamilyOtherBlues",
  520. "BlueScale",
  521. "BlueShift",
  522. "BlueFuzz",
  523. "StdHW",
  524. "StdVW",
  525. "StemSnapH",
  526. "StemSnapV",
  527. "ForceBold",
  528. "LanguageGroup",
  529. "password",
  530. "lenIV",
  531. "MinFeature",
  532. "RndStemUp",
  533. ]
  534. # t1write_hintothers.h
  535. hintothers = """/OtherSubrs[{}{}{}{systemdict/internaldict known not{pop 3}{1183615869
  536. systemdict/internaldict get exec dup/startlock known{/startlock get exec}{dup
  537. /strtlck known{/strtlck get exec}{pop 3}ifelse}ifelse}ifelse}executeonly]def"""
  538. # t1write.c:saveStdSubrs
  539. std_subrs = [
  540. # 3 0 callother pop pop setcurrentpoint return
  541. b"\x8e\x8b\x0c\x10\x0c\x11\x0c\x11\x0c\x21\x0b",
  542. # 0 1 callother return
  543. b"\x8b\x8c\x0c\x10\x0b",
  544. # 0 2 callother return
  545. b"\x8b\x8d\x0c\x10\x0b",
  546. # return
  547. b"\x0b",
  548. # 3 1 3 callother pop callsubr return
  549. b"\x8e\x8c\x8e\x0c\x10\x0c\x11\x0a\x0b",
  550. ]
  551. # follow t1write.c:writeRegNameKeyedFont
  552. eexec_IV = b"cccc"
  553. char_IV = b"\x0c\x0c\x0c\x0c"
  554. RD_value = ("string", "currentfile", "exch", "readstring", "pop")
  555. ND_values = [("def",), ("noaccess", "def")]
  556. PD_values = [("put",), ("noaccess", "put")]