woff2.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. from io import BytesIO
  2. import sys
  3. import array
  4. import struct
  5. from collections import OrderedDict
  6. from fontTools.misc import sstruct
  7. from fontTools.misc.arrayTools import calcIntBounds
  8. from fontTools.misc.textTools import Tag, bytechr, byteord, bytesjoin, pad
  9. from fontTools.ttLib import (
  10. TTFont,
  11. TTLibError,
  12. getTableModule,
  13. getTableClass,
  14. getSearchRange,
  15. )
  16. from fontTools.ttLib.sfnt import (
  17. SFNTReader,
  18. SFNTWriter,
  19. DirectoryEntry,
  20. WOFFFlavorData,
  21. sfntDirectoryFormat,
  22. sfntDirectorySize,
  23. SFNTDirectoryEntry,
  24. sfntDirectoryEntrySize,
  25. calcChecksum,
  26. )
  27. from fontTools.ttLib.tables import ttProgram, _g_l_y_f
  28. import logging
  29. log = logging.getLogger("fontTools.ttLib.woff2")
  30. haveBrotli = False
  31. try:
  32. try:
  33. import brotlicffi as brotli
  34. except ImportError:
  35. import brotli
  36. haveBrotli = True
  37. except ImportError:
  38. pass
  39. class WOFF2Reader(SFNTReader):
  40. flavor = "woff2"
  41. def __init__(self, file, checkChecksums=0, fontNumber=-1):
  42. if not haveBrotli:
  43. log.error(
  44. "The WOFF2 decoder requires the Brotli Python extension, available at: "
  45. "https://github.com/google/brotli"
  46. )
  47. raise ImportError("No module named brotli")
  48. self.file = file
  49. signature = Tag(self.file.read(4))
  50. if signature != b"wOF2":
  51. raise TTLibError("Not a WOFF2 font (bad signature)")
  52. self.file.seek(0)
  53. self.DirectoryEntry = WOFF2DirectoryEntry
  54. data = self.file.read(woff2DirectorySize)
  55. if len(data) != woff2DirectorySize:
  56. raise TTLibError("Not a WOFF2 font (not enough data)")
  57. sstruct.unpack(woff2DirectoryFormat, data, self)
  58. self.tables = OrderedDict()
  59. offset = 0
  60. for i in range(self.numTables):
  61. entry = self.DirectoryEntry()
  62. entry.fromFile(self.file)
  63. tag = Tag(entry.tag)
  64. self.tables[tag] = entry
  65. entry.offset = offset
  66. offset += entry.length
  67. totalUncompressedSize = offset
  68. compressedData = self.file.read(self.totalCompressedSize)
  69. decompressedData = brotli.decompress(compressedData)
  70. if len(decompressedData) != totalUncompressedSize:
  71. raise TTLibError(
  72. "unexpected size for decompressed font data: expected %d, found %d"
  73. % (totalUncompressedSize, len(decompressedData))
  74. )
  75. self.transformBuffer = BytesIO(decompressedData)
  76. self.file.seek(0, 2)
  77. if self.length != self.file.tell():
  78. raise TTLibError("reported 'length' doesn't match the actual file size")
  79. self.flavorData = WOFF2FlavorData(self)
  80. # make empty TTFont to store data while reconstructing tables
  81. self.ttFont = TTFont(recalcBBoxes=False, recalcTimestamp=False)
  82. def __getitem__(self, tag):
  83. """Fetch the raw table data. Reconstruct transformed tables."""
  84. entry = self.tables[Tag(tag)]
  85. if not hasattr(entry, "data"):
  86. if entry.transformed:
  87. entry.data = self.reconstructTable(tag)
  88. else:
  89. entry.data = entry.loadData(self.transformBuffer)
  90. return entry.data
  91. def reconstructTable(self, tag):
  92. """Reconstruct table named 'tag' from transformed data."""
  93. entry = self.tables[Tag(tag)]
  94. rawData = entry.loadData(self.transformBuffer)
  95. if tag == "glyf":
  96. # no need to pad glyph data when reconstructing
  97. padding = self.padding if hasattr(self, "padding") else None
  98. data = self._reconstructGlyf(rawData, padding)
  99. elif tag == "loca":
  100. data = self._reconstructLoca()
  101. elif tag == "hmtx":
  102. data = self._reconstructHmtx(rawData)
  103. else:
  104. raise TTLibError("transform for table '%s' is unknown" % tag)
  105. return data
  106. def _reconstructGlyf(self, data, padding=None):
  107. """Return recostructed glyf table data, and set the corresponding loca's
  108. locations. Optionally pad glyph offsets to the specified number of bytes.
  109. """
  110. self.ttFont["loca"] = WOFF2LocaTable()
  111. glyfTable = self.ttFont["glyf"] = WOFF2GlyfTable()
  112. glyfTable.reconstruct(data, self.ttFont)
  113. if padding:
  114. glyfTable.padding = padding
  115. data = glyfTable.compile(self.ttFont)
  116. return data
  117. def _reconstructLoca(self):
  118. """Return reconstructed loca table data."""
  119. if "loca" not in self.ttFont:
  120. # make sure glyf is reconstructed first
  121. self.tables["glyf"].data = self.reconstructTable("glyf")
  122. locaTable = self.ttFont["loca"]
  123. data = locaTable.compile(self.ttFont)
  124. if len(data) != self.tables["loca"].origLength:
  125. raise TTLibError(
  126. "reconstructed 'loca' table doesn't match original size: "
  127. "expected %d, found %d" % (self.tables["loca"].origLength, len(data))
  128. )
  129. return data
  130. def _reconstructHmtx(self, data):
  131. """Return reconstructed hmtx table data."""
  132. # Before reconstructing 'hmtx' table we need to parse other tables:
  133. # 'glyf' is required for reconstructing the sidebearings from the glyphs'
  134. # bounding box; 'hhea' is needed for the numberOfHMetrics field.
  135. if "glyf" in self.flavorData.transformedTables:
  136. # transformed 'glyf' table is self-contained, thus 'loca' not needed
  137. tableDependencies = ("maxp", "hhea", "glyf")
  138. else:
  139. # decompiling untransformed 'glyf' requires 'loca', which requires 'head'
  140. tableDependencies = ("maxp", "head", "hhea", "loca", "glyf")
  141. for tag in tableDependencies:
  142. self._decompileTable(tag)
  143. hmtxTable = self.ttFont["hmtx"] = WOFF2HmtxTable()
  144. hmtxTable.reconstruct(data, self.ttFont)
  145. data = hmtxTable.compile(self.ttFont)
  146. return data
  147. def _decompileTable(self, tag):
  148. """Decompile table data and store it inside self.ttFont."""
  149. data = self[tag]
  150. if self.ttFont.isLoaded(tag):
  151. return self.ttFont[tag]
  152. tableClass = getTableClass(tag)
  153. table = tableClass(tag)
  154. self.ttFont.tables[tag] = table
  155. table.decompile(data, self.ttFont)
  156. class WOFF2Writer(SFNTWriter):
  157. flavor = "woff2"
  158. def __init__(
  159. self,
  160. file,
  161. numTables,
  162. sfntVersion="\000\001\000\000",
  163. flavor=None,
  164. flavorData=None,
  165. ):
  166. if not haveBrotli:
  167. log.error(
  168. "The WOFF2 encoder requires the Brotli Python extension, available at: "
  169. "https://github.com/google/brotli"
  170. )
  171. raise ImportError("No module named brotli")
  172. self.file = file
  173. self.numTables = numTables
  174. self.sfntVersion = Tag(sfntVersion)
  175. self.flavorData = WOFF2FlavorData(data=flavorData)
  176. self.directoryFormat = woff2DirectoryFormat
  177. self.directorySize = woff2DirectorySize
  178. self.DirectoryEntry = WOFF2DirectoryEntry
  179. self.signature = Tag("wOF2")
  180. self.nextTableOffset = 0
  181. self.transformBuffer = BytesIO()
  182. self.tables = OrderedDict()
  183. # make empty TTFont to store data while normalising and transforming tables
  184. self.ttFont = TTFont(recalcBBoxes=False, recalcTimestamp=False)
  185. def __setitem__(self, tag, data):
  186. """Associate new entry named 'tag' with raw table data."""
  187. if tag in self.tables:
  188. raise TTLibError("cannot rewrite '%s' table" % tag)
  189. if tag == "DSIG":
  190. # always drop DSIG table, since the encoding process can invalidate it
  191. self.numTables -= 1
  192. return
  193. entry = self.DirectoryEntry()
  194. entry.tag = Tag(tag)
  195. entry.flags = getKnownTagIndex(entry.tag)
  196. # WOFF2 table data are written to disk only on close(), after all tags
  197. # have been specified
  198. entry.data = data
  199. self.tables[tag] = entry
  200. def close(self):
  201. """All tags must have been specified. Now write the table data and directory."""
  202. if len(self.tables) != self.numTables:
  203. raise TTLibError(
  204. "wrong number of tables; expected %d, found %d"
  205. % (self.numTables, len(self.tables))
  206. )
  207. if self.sfntVersion in ("\x00\x01\x00\x00", "true"):
  208. isTrueType = True
  209. elif self.sfntVersion == "OTTO":
  210. isTrueType = False
  211. else:
  212. raise TTLibError("Not a TrueType or OpenType font (bad sfntVersion)")
  213. # The WOFF2 spec no longer requires the glyph offsets to be 4-byte aligned.
  214. # However, the reference WOFF2 implementation still fails to reconstruct
  215. # 'unpadded' glyf tables, therefore we need to 'normalise' them.
  216. # See:
  217. # https://github.com/khaledhosny/ots/issues/60
  218. # https://github.com/google/woff2/issues/15
  219. if (
  220. isTrueType
  221. and "glyf" in self.flavorData.transformedTables
  222. and "glyf" in self.tables
  223. ):
  224. self._normaliseGlyfAndLoca(padding=4)
  225. self._setHeadTransformFlag()
  226. # To pass the legacy OpenType Sanitiser currently included in browsers,
  227. # we must sort the table directory and data alphabetically by tag.
  228. # See:
  229. # https://github.com/google/woff2/pull/3
  230. # https://lists.w3.org/Archives/Public/public-webfonts-wg/2015Mar/0000.html
  231. #
  232. # 2023: We rely on this in _transformTables where we expect that
  233. # "loca" comes after "glyf" table.
  234. self.tables = OrderedDict(sorted(self.tables.items()))
  235. self.totalSfntSize = self._calcSFNTChecksumsLengthsAndOffsets()
  236. fontData = self._transformTables()
  237. compressedFont = brotli.compress(fontData, mode=brotli.MODE_FONT)
  238. self.totalCompressedSize = len(compressedFont)
  239. self.length = self._calcTotalSize()
  240. self.majorVersion, self.minorVersion = self._getVersion()
  241. self.reserved = 0
  242. directory = self._packTableDirectory()
  243. self.file.seek(0)
  244. self.file.write(pad(directory + compressedFont, size=4))
  245. self._writeFlavorData()
  246. def _normaliseGlyfAndLoca(self, padding=4):
  247. """Recompile glyf and loca tables, aligning glyph offsets to multiples of
  248. 'padding' size. Update the head table's 'indexToLocFormat' accordingly while
  249. compiling loca.
  250. """
  251. if self.sfntVersion == "OTTO":
  252. return
  253. for tag in ("maxp", "head", "loca", "glyf", "fvar"):
  254. if tag in self.tables:
  255. self._decompileTable(tag)
  256. self.ttFont["glyf"].padding = padding
  257. for tag in ("glyf", "loca"):
  258. self._compileTable(tag)
  259. def _setHeadTransformFlag(self):
  260. """Set bit 11 of 'head' table flags to indicate that the font has undergone
  261. a lossless modifying transform. Re-compile head table data."""
  262. self._decompileTable("head")
  263. self.ttFont["head"].flags |= 1 << 11
  264. self._compileTable("head")
  265. def _decompileTable(self, tag):
  266. """Fetch table data, decompile it, and store it inside self.ttFont."""
  267. tag = Tag(tag)
  268. if tag not in self.tables:
  269. raise TTLibError("missing required table: %s" % tag)
  270. if self.ttFont.isLoaded(tag):
  271. return
  272. data = self.tables[tag].data
  273. if tag == "loca":
  274. tableClass = WOFF2LocaTable
  275. elif tag == "glyf":
  276. tableClass = WOFF2GlyfTable
  277. elif tag == "hmtx":
  278. tableClass = WOFF2HmtxTable
  279. else:
  280. tableClass = getTableClass(tag)
  281. table = tableClass(tag)
  282. self.ttFont.tables[tag] = table
  283. table.decompile(data, self.ttFont)
  284. def _compileTable(self, tag):
  285. """Compile table and store it in its 'data' attribute."""
  286. self.tables[tag].data = self.ttFont[tag].compile(self.ttFont)
  287. def _calcSFNTChecksumsLengthsAndOffsets(self):
  288. """Compute the 'original' SFNT checksums, lengths and offsets for checksum
  289. adjustment calculation. Return the total size of the uncompressed font.
  290. """
  291. offset = sfntDirectorySize + sfntDirectoryEntrySize * len(self.tables)
  292. for tag, entry in self.tables.items():
  293. data = entry.data
  294. entry.origOffset = offset
  295. entry.origLength = len(data)
  296. if tag == "head":
  297. entry.checkSum = calcChecksum(data[:8] + b"\0\0\0\0" + data[12:])
  298. else:
  299. entry.checkSum = calcChecksum(data)
  300. offset += (entry.origLength + 3) & ~3
  301. return offset
  302. def _transformTables(self):
  303. """Return transformed font data."""
  304. transformedTables = self.flavorData.transformedTables
  305. for tag, entry in self.tables.items():
  306. data = None
  307. if tag in transformedTables:
  308. data = self.transformTable(tag)
  309. if data is not None:
  310. entry.transformed = True
  311. if data is None:
  312. if tag == "glyf":
  313. # Currently we always sort table tags so
  314. # 'loca' comes after 'glyf'.
  315. transformedTables.discard("loca")
  316. # pass-through the table data without transformation
  317. data = entry.data
  318. entry.transformed = False
  319. entry.offset = self.nextTableOffset
  320. entry.saveData(self.transformBuffer, data)
  321. self.nextTableOffset += entry.length
  322. self.writeMasterChecksum()
  323. fontData = self.transformBuffer.getvalue()
  324. return fontData
  325. def transformTable(self, tag):
  326. """Return transformed table data, or None if some pre-conditions aren't
  327. met -- in which case, the non-transformed table data will be used.
  328. """
  329. if tag == "loca":
  330. data = b""
  331. elif tag == "glyf":
  332. for tag in ("maxp", "head", "loca", "glyf"):
  333. self._decompileTable(tag)
  334. glyfTable = self.ttFont["glyf"]
  335. data = glyfTable.transform(self.ttFont)
  336. elif tag == "hmtx":
  337. if "glyf" not in self.tables:
  338. return
  339. for tag in ("maxp", "head", "hhea", "loca", "glyf", "hmtx"):
  340. self._decompileTable(tag)
  341. hmtxTable = self.ttFont["hmtx"]
  342. data = hmtxTable.transform(self.ttFont) # can be None
  343. else:
  344. raise TTLibError("Transform for table '%s' is unknown" % tag)
  345. return data
  346. def _calcMasterChecksum(self):
  347. """Calculate checkSumAdjustment."""
  348. tags = list(self.tables.keys())
  349. checksums = []
  350. for i in range(len(tags)):
  351. checksums.append(self.tables[tags[i]].checkSum)
  352. # Create a SFNT directory for checksum calculation purposes
  353. self.searchRange, self.entrySelector, self.rangeShift = getSearchRange(
  354. self.numTables, 16
  355. )
  356. directory = sstruct.pack(sfntDirectoryFormat, self)
  357. tables = sorted(self.tables.items())
  358. for tag, entry in tables:
  359. sfntEntry = SFNTDirectoryEntry()
  360. sfntEntry.tag = entry.tag
  361. sfntEntry.checkSum = entry.checkSum
  362. sfntEntry.offset = entry.origOffset
  363. sfntEntry.length = entry.origLength
  364. directory = directory + sfntEntry.toString()
  365. directory_end = sfntDirectorySize + len(self.tables) * sfntDirectoryEntrySize
  366. assert directory_end == len(directory)
  367. checksums.append(calcChecksum(directory))
  368. checksum = sum(checksums) & 0xFFFFFFFF
  369. # BiboAfba!
  370. checksumadjustment = (0xB1B0AFBA - checksum) & 0xFFFFFFFF
  371. return checksumadjustment
  372. def writeMasterChecksum(self):
  373. """Write checkSumAdjustment to the transformBuffer."""
  374. checksumadjustment = self._calcMasterChecksum()
  375. self.transformBuffer.seek(self.tables["head"].offset + 8)
  376. self.transformBuffer.write(struct.pack(">L", checksumadjustment))
  377. def _calcTotalSize(self):
  378. """Calculate total size of WOFF2 font, including any meta- and/or private data."""
  379. offset = self.directorySize
  380. for entry in self.tables.values():
  381. offset += len(entry.toString())
  382. offset += self.totalCompressedSize
  383. offset = (offset + 3) & ~3
  384. offset = self._calcFlavorDataOffsetsAndSize(offset)
  385. return offset
  386. def _calcFlavorDataOffsetsAndSize(self, start):
  387. """Calculate offsets and lengths for any meta- and/or private data."""
  388. offset = start
  389. data = self.flavorData
  390. if data.metaData:
  391. self.metaOrigLength = len(data.metaData)
  392. self.metaOffset = offset
  393. self.compressedMetaData = brotli.compress(
  394. data.metaData, mode=brotli.MODE_TEXT
  395. )
  396. self.metaLength = len(self.compressedMetaData)
  397. offset += self.metaLength
  398. else:
  399. self.metaOffset = self.metaLength = self.metaOrigLength = 0
  400. self.compressedMetaData = b""
  401. if data.privData:
  402. # make sure private data is padded to 4-byte boundary
  403. offset = (offset + 3) & ~3
  404. self.privOffset = offset
  405. self.privLength = len(data.privData)
  406. offset += self.privLength
  407. else:
  408. self.privOffset = self.privLength = 0
  409. return offset
  410. def _getVersion(self):
  411. """Return the WOFF2 font's (majorVersion, minorVersion) tuple."""
  412. data = self.flavorData
  413. if data.majorVersion is not None and data.minorVersion is not None:
  414. return data.majorVersion, data.minorVersion
  415. else:
  416. # if None, return 'fontRevision' from 'head' table
  417. if "head" in self.tables:
  418. return struct.unpack(">HH", self.tables["head"].data[4:8])
  419. else:
  420. return 0, 0
  421. def _packTableDirectory(self):
  422. """Return WOFF2 table directory data."""
  423. directory = sstruct.pack(self.directoryFormat, self)
  424. for entry in self.tables.values():
  425. directory = directory + entry.toString()
  426. return directory
  427. def _writeFlavorData(self):
  428. """Write metadata and/or private data using appropiate padding."""
  429. compressedMetaData = self.compressedMetaData
  430. privData = self.flavorData.privData
  431. if compressedMetaData and privData:
  432. compressedMetaData = pad(compressedMetaData, size=4)
  433. if compressedMetaData:
  434. self.file.seek(self.metaOffset)
  435. assert self.file.tell() == self.metaOffset
  436. self.file.write(compressedMetaData)
  437. if privData:
  438. self.file.seek(self.privOffset)
  439. assert self.file.tell() == self.privOffset
  440. self.file.write(privData)
  441. def reordersTables(self):
  442. return True
  443. # -- woff2 directory helpers and cruft
  444. woff2DirectoryFormat = """
  445. > # big endian
  446. signature: 4s # "wOF2"
  447. sfntVersion: 4s
  448. length: L # total woff2 file size
  449. numTables: H # number of tables
  450. reserved: H # set to 0
  451. totalSfntSize: L # uncompressed size
  452. totalCompressedSize: L # compressed size
  453. majorVersion: H # major version of WOFF file
  454. minorVersion: H # minor version of WOFF file
  455. metaOffset: L # offset to metadata block
  456. metaLength: L # length of compressed metadata
  457. metaOrigLength: L # length of uncompressed metadata
  458. privOffset: L # offset to private data block
  459. privLength: L # length of private data block
  460. """
  461. woff2DirectorySize = sstruct.calcsize(woff2DirectoryFormat)
  462. woff2KnownTags = (
  463. "cmap",
  464. "head",
  465. "hhea",
  466. "hmtx",
  467. "maxp",
  468. "name",
  469. "OS/2",
  470. "post",
  471. "cvt ",
  472. "fpgm",
  473. "glyf",
  474. "loca",
  475. "prep",
  476. "CFF ",
  477. "VORG",
  478. "EBDT",
  479. "EBLC",
  480. "gasp",
  481. "hdmx",
  482. "kern",
  483. "LTSH",
  484. "PCLT",
  485. "VDMX",
  486. "vhea",
  487. "vmtx",
  488. "BASE",
  489. "GDEF",
  490. "GPOS",
  491. "GSUB",
  492. "EBSC",
  493. "JSTF",
  494. "MATH",
  495. "CBDT",
  496. "CBLC",
  497. "COLR",
  498. "CPAL",
  499. "SVG ",
  500. "sbix",
  501. "acnt",
  502. "avar",
  503. "bdat",
  504. "bloc",
  505. "bsln",
  506. "cvar",
  507. "fdsc",
  508. "feat",
  509. "fmtx",
  510. "fvar",
  511. "gvar",
  512. "hsty",
  513. "just",
  514. "lcar",
  515. "mort",
  516. "morx",
  517. "opbd",
  518. "prop",
  519. "trak",
  520. "Zapf",
  521. "Silf",
  522. "Glat",
  523. "Gloc",
  524. "Feat",
  525. "Sill",
  526. )
  527. woff2FlagsFormat = """
  528. > # big endian
  529. flags: B # table type and flags
  530. """
  531. woff2FlagsSize = sstruct.calcsize(woff2FlagsFormat)
  532. woff2UnknownTagFormat = """
  533. > # big endian
  534. tag: 4s # 4-byte tag (optional)
  535. """
  536. woff2UnknownTagSize = sstruct.calcsize(woff2UnknownTagFormat)
  537. woff2UnknownTagIndex = 0x3F
  538. woff2Base128MaxSize = 5
  539. woff2DirectoryEntryMaxSize = (
  540. woff2FlagsSize + woff2UnknownTagSize + 2 * woff2Base128MaxSize
  541. )
  542. woff2TransformedTableTags = ("glyf", "loca")
  543. woff2GlyfTableFormat = """
  544. > # big endian
  545. version: H # = 0x0000
  546. optionFlags: H # Bit 0: we have overlapSimpleBitmap[], Bits 1-15: reserved
  547. numGlyphs: H # Number of glyphs
  548. indexFormat: H # Offset format for loca table
  549. nContourStreamSize: L # Size of nContour stream
  550. nPointsStreamSize: L # Size of nPoints stream
  551. flagStreamSize: L # Size of flag stream
  552. glyphStreamSize: L # Size of glyph stream
  553. compositeStreamSize: L # Size of composite stream
  554. bboxStreamSize: L # Comnined size of bboxBitmap and bboxStream
  555. instructionStreamSize: L # Size of instruction stream
  556. """
  557. woff2GlyfTableFormatSize = sstruct.calcsize(woff2GlyfTableFormat)
  558. bboxFormat = """
  559. > # big endian
  560. xMin: h
  561. yMin: h
  562. xMax: h
  563. yMax: h
  564. """
  565. woff2OverlapSimpleBitmapFlag = 0x0001
  566. def getKnownTagIndex(tag):
  567. """Return index of 'tag' in woff2KnownTags list. Return 63 if not found."""
  568. for i in range(len(woff2KnownTags)):
  569. if tag == woff2KnownTags[i]:
  570. return i
  571. return woff2UnknownTagIndex
  572. class WOFF2DirectoryEntry(DirectoryEntry):
  573. def fromFile(self, file):
  574. pos = file.tell()
  575. data = file.read(woff2DirectoryEntryMaxSize)
  576. left = self.fromString(data)
  577. consumed = len(data) - len(left)
  578. file.seek(pos + consumed)
  579. def fromString(self, data):
  580. if len(data) < 1:
  581. raise TTLibError("can't read table 'flags': not enough data")
  582. dummy, data = sstruct.unpack2(woff2FlagsFormat, data, self)
  583. if self.flags & 0x3F == 0x3F:
  584. # if bits [0..5] of the flags byte == 63, read a 4-byte arbitrary tag value
  585. if len(data) < woff2UnknownTagSize:
  586. raise TTLibError("can't read table 'tag': not enough data")
  587. dummy, data = sstruct.unpack2(woff2UnknownTagFormat, data, self)
  588. else:
  589. # otherwise, tag is derived from a fixed 'Known Tags' table
  590. self.tag = woff2KnownTags[self.flags & 0x3F]
  591. self.tag = Tag(self.tag)
  592. self.origLength, data = unpackBase128(data)
  593. self.length = self.origLength
  594. if self.transformed:
  595. self.length, data = unpackBase128(data)
  596. if self.tag == "loca" and self.length != 0:
  597. raise TTLibError("the transformLength of the 'loca' table must be 0")
  598. # return left over data
  599. return data
  600. def toString(self):
  601. data = bytechr(self.flags)
  602. if (self.flags & 0x3F) == 0x3F:
  603. data += struct.pack(">4s", self.tag.tobytes())
  604. data += packBase128(self.origLength)
  605. if self.transformed:
  606. data += packBase128(self.length)
  607. return data
  608. @property
  609. def transformVersion(self):
  610. """Return bits 6-7 of table entry's flags, which indicate the preprocessing
  611. transformation version number (between 0 and 3).
  612. """
  613. return self.flags >> 6
  614. @transformVersion.setter
  615. def transformVersion(self, value):
  616. assert 0 <= value <= 3
  617. self.flags |= value << 6
  618. @property
  619. def transformed(self):
  620. """Return True if the table has any transformation, else return False."""
  621. # For all tables in a font, except for 'glyf' and 'loca', the transformation
  622. # version 0 indicates the null transform (where the original table data is
  623. # passed directly to the Brotli compressor). For 'glyf' and 'loca' tables,
  624. # transformation version 3 indicates the null transform
  625. if self.tag in {"glyf", "loca"}:
  626. return self.transformVersion != 3
  627. else:
  628. return self.transformVersion != 0
  629. @transformed.setter
  630. def transformed(self, booleanValue):
  631. # here we assume that a non-null transform means version 0 for 'glyf' and
  632. # 'loca' and 1 for every other table (e.g. hmtx); but that may change as
  633. # new transformation formats are introduced in the future (if ever).
  634. if self.tag in {"glyf", "loca"}:
  635. self.transformVersion = 3 if not booleanValue else 0
  636. else:
  637. self.transformVersion = int(booleanValue)
  638. class WOFF2LocaTable(getTableClass("loca")):
  639. """Same as parent class. The only difference is that it attempts to preserve
  640. the 'indexFormat' as encoded in the WOFF2 glyf table.
  641. """
  642. def __init__(self, tag=None):
  643. self.tableTag = Tag(tag or "loca")
  644. def compile(self, ttFont):
  645. try:
  646. max_location = max(self.locations)
  647. except AttributeError:
  648. self.set([])
  649. max_location = 0
  650. if "glyf" in ttFont and hasattr(ttFont["glyf"], "indexFormat"):
  651. # copile loca using the indexFormat specified in the WOFF2 glyf table
  652. indexFormat = ttFont["glyf"].indexFormat
  653. if indexFormat == 0:
  654. if max_location >= 0x20000:
  655. raise TTLibError("indexFormat is 0 but local offsets > 0x20000")
  656. if not all(l % 2 == 0 for l in self.locations):
  657. raise TTLibError(
  658. "indexFormat is 0 but local offsets not multiples of 2"
  659. )
  660. locations = array.array("H")
  661. for i in range(len(self.locations)):
  662. locations.append(self.locations[i] // 2)
  663. else:
  664. locations = array.array("I", self.locations)
  665. if sys.byteorder != "big":
  666. locations.byteswap()
  667. data = locations.tobytes()
  668. else:
  669. # use the most compact indexFormat given the current glyph offsets
  670. data = super(WOFF2LocaTable, self).compile(ttFont)
  671. return data
  672. class WOFF2GlyfTable(getTableClass("glyf")):
  673. """Decoder/Encoder for WOFF2 'glyf' table transform."""
  674. subStreams = (
  675. "nContourStream",
  676. "nPointsStream",
  677. "flagStream",
  678. "glyphStream",
  679. "compositeStream",
  680. "bboxStream",
  681. "instructionStream",
  682. )
  683. def __init__(self, tag=None):
  684. self.tableTag = Tag(tag or "glyf")
  685. def reconstruct(self, data, ttFont):
  686. """Decompile transformed 'glyf' data."""
  687. inputDataSize = len(data)
  688. if inputDataSize < woff2GlyfTableFormatSize:
  689. raise TTLibError("not enough 'glyf' data")
  690. dummy, data = sstruct.unpack2(woff2GlyfTableFormat, data, self)
  691. offset = woff2GlyfTableFormatSize
  692. for stream in self.subStreams:
  693. size = getattr(self, stream + "Size")
  694. setattr(self, stream, data[:size])
  695. data = data[size:]
  696. offset += size
  697. hasOverlapSimpleBitmap = self.optionFlags & woff2OverlapSimpleBitmapFlag
  698. self.overlapSimpleBitmap = None
  699. if hasOverlapSimpleBitmap:
  700. overlapSimpleBitmapSize = (self.numGlyphs + 7) >> 3
  701. self.overlapSimpleBitmap = array.array("B", data[:overlapSimpleBitmapSize])
  702. offset += overlapSimpleBitmapSize
  703. if offset != inputDataSize:
  704. raise TTLibError(
  705. "incorrect size of transformed 'glyf' table: expected %d, received %d bytes"
  706. % (offset, inputDataSize)
  707. )
  708. bboxBitmapSize = ((self.numGlyphs + 31) >> 5) << 2
  709. bboxBitmap = self.bboxStream[:bboxBitmapSize]
  710. self.bboxBitmap = array.array("B", bboxBitmap)
  711. self.bboxStream = self.bboxStream[bboxBitmapSize:]
  712. self.nContourStream = array.array("h", self.nContourStream)
  713. if sys.byteorder != "big":
  714. self.nContourStream.byteswap()
  715. assert len(self.nContourStream) == self.numGlyphs
  716. if "head" in ttFont:
  717. ttFont["head"].indexToLocFormat = self.indexFormat
  718. try:
  719. self.glyphOrder = ttFont.getGlyphOrder()
  720. except:
  721. self.glyphOrder = None
  722. if self.glyphOrder is None:
  723. self.glyphOrder = [".notdef"]
  724. self.glyphOrder.extend(["glyph%.5d" % i for i in range(1, self.numGlyphs)])
  725. else:
  726. if len(self.glyphOrder) != self.numGlyphs:
  727. raise TTLibError(
  728. "incorrect glyphOrder: expected %d glyphs, found %d"
  729. % (len(self.glyphOrder), self.numGlyphs)
  730. )
  731. glyphs = self.glyphs = {}
  732. for glyphID, glyphName in enumerate(self.glyphOrder):
  733. glyph = self._decodeGlyph(glyphID)
  734. glyphs[glyphName] = glyph
  735. def transform(self, ttFont):
  736. """Return transformed 'glyf' data"""
  737. self.numGlyphs = len(self.glyphs)
  738. assert len(self.glyphOrder) == self.numGlyphs
  739. if "maxp" in ttFont:
  740. ttFont["maxp"].numGlyphs = self.numGlyphs
  741. self.indexFormat = ttFont["head"].indexToLocFormat
  742. for stream in self.subStreams:
  743. setattr(self, stream, b"")
  744. bboxBitmapSize = ((self.numGlyphs + 31) >> 5) << 2
  745. self.bboxBitmap = array.array("B", [0] * bboxBitmapSize)
  746. self.overlapSimpleBitmap = array.array("B", [0] * ((self.numGlyphs + 7) >> 3))
  747. for glyphID in range(self.numGlyphs):
  748. try:
  749. self._encodeGlyph(glyphID)
  750. except NotImplementedError:
  751. return None
  752. hasOverlapSimpleBitmap = any(self.overlapSimpleBitmap)
  753. self.bboxStream = self.bboxBitmap.tobytes() + self.bboxStream
  754. for stream in self.subStreams:
  755. setattr(self, stream + "Size", len(getattr(self, stream)))
  756. self.version = 0
  757. self.optionFlags = 0
  758. if hasOverlapSimpleBitmap:
  759. self.optionFlags |= woff2OverlapSimpleBitmapFlag
  760. data = sstruct.pack(woff2GlyfTableFormat, self)
  761. data += bytesjoin([getattr(self, s) for s in self.subStreams])
  762. if hasOverlapSimpleBitmap:
  763. data += self.overlapSimpleBitmap.tobytes()
  764. return data
  765. def _decodeGlyph(self, glyphID):
  766. glyph = getTableModule("glyf").Glyph()
  767. glyph.numberOfContours = self.nContourStream[glyphID]
  768. if glyph.numberOfContours == 0:
  769. return glyph
  770. elif glyph.isComposite():
  771. self._decodeComponents(glyph)
  772. else:
  773. self._decodeCoordinates(glyph)
  774. self._decodeOverlapSimpleFlag(glyph, glyphID)
  775. self._decodeBBox(glyphID, glyph)
  776. return glyph
  777. def _decodeComponents(self, glyph):
  778. data = self.compositeStream
  779. glyph.components = []
  780. more = 1
  781. haveInstructions = 0
  782. while more:
  783. component = getTableModule("glyf").GlyphComponent()
  784. more, haveInstr, data = component.decompile(data, self)
  785. haveInstructions = haveInstructions | haveInstr
  786. glyph.components.append(component)
  787. self.compositeStream = data
  788. if haveInstructions:
  789. self._decodeInstructions(glyph)
  790. def _decodeCoordinates(self, glyph):
  791. data = self.nPointsStream
  792. endPtsOfContours = []
  793. endPoint = -1
  794. for i in range(glyph.numberOfContours):
  795. ptsOfContour, data = unpack255UShort(data)
  796. endPoint += ptsOfContour
  797. endPtsOfContours.append(endPoint)
  798. glyph.endPtsOfContours = endPtsOfContours
  799. self.nPointsStream = data
  800. self._decodeTriplets(glyph)
  801. self._decodeInstructions(glyph)
  802. def _decodeOverlapSimpleFlag(self, glyph, glyphID):
  803. if self.overlapSimpleBitmap is None or glyph.numberOfContours <= 0:
  804. return
  805. byte = glyphID >> 3
  806. bit = glyphID & 7
  807. if self.overlapSimpleBitmap[byte] & (0x80 >> bit):
  808. glyph.flags[0] |= _g_l_y_f.flagOverlapSimple
  809. def _decodeInstructions(self, glyph):
  810. glyphStream = self.glyphStream
  811. instructionStream = self.instructionStream
  812. instructionLength, glyphStream = unpack255UShort(glyphStream)
  813. glyph.program = ttProgram.Program()
  814. glyph.program.fromBytecode(instructionStream[:instructionLength])
  815. self.glyphStream = glyphStream
  816. self.instructionStream = instructionStream[instructionLength:]
  817. def _decodeBBox(self, glyphID, glyph):
  818. haveBBox = bool(self.bboxBitmap[glyphID >> 3] & (0x80 >> (glyphID & 7)))
  819. if glyph.isComposite() and not haveBBox:
  820. raise TTLibError("no bbox values for composite glyph %d" % glyphID)
  821. if haveBBox:
  822. dummy, self.bboxStream = sstruct.unpack2(bboxFormat, self.bboxStream, glyph)
  823. else:
  824. glyph.recalcBounds(self)
  825. def _decodeTriplets(self, glyph):
  826. def withSign(flag, baseval):
  827. assert 0 <= baseval and baseval < 65536, "integer overflow"
  828. return baseval if flag & 1 else -baseval
  829. nPoints = glyph.endPtsOfContours[-1] + 1
  830. flagSize = nPoints
  831. if flagSize > len(self.flagStream):
  832. raise TTLibError("not enough 'flagStream' data")
  833. flagsData = self.flagStream[:flagSize]
  834. self.flagStream = self.flagStream[flagSize:]
  835. flags = array.array("B", flagsData)
  836. triplets = array.array("B", self.glyphStream)
  837. nTriplets = len(triplets)
  838. assert nPoints <= nTriplets
  839. x = 0
  840. y = 0
  841. glyph.coordinates = getTableModule("glyf").GlyphCoordinates.zeros(nPoints)
  842. glyph.flags = array.array("B")
  843. tripletIndex = 0
  844. for i in range(nPoints):
  845. flag = flags[i]
  846. onCurve = not bool(flag >> 7)
  847. flag &= 0x7F
  848. if flag < 84:
  849. nBytes = 1
  850. elif flag < 120:
  851. nBytes = 2
  852. elif flag < 124:
  853. nBytes = 3
  854. else:
  855. nBytes = 4
  856. assert (tripletIndex + nBytes) <= nTriplets
  857. if flag < 10:
  858. dx = 0
  859. dy = withSign(flag, ((flag & 14) << 7) + triplets[tripletIndex])
  860. elif flag < 20:
  861. dx = withSign(flag, (((flag - 10) & 14) << 7) + triplets[tripletIndex])
  862. dy = 0
  863. elif flag < 84:
  864. b0 = flag - 20
  865. b1 = triplets[tripletIndex]
  866. dx = withSign(flag, 1 + (b0 & 0x30) + (b1 >> 4))
  867. dy = withSign(flag >> 1, 1 + ((b0 & 0x0C) << 2) + (b1 & 0x0F))
  868. elif flag < 120:
  869. b0 = flag - 84
  870. dx = withSign(flag, 1 + ((b0 // 12) << 8) + triplets[tripletIndex])
  871. dy = withSign(
  872. flag >> 1, 1 + (((b0 % 12) >> 2) << 8) + triplets[tripletIndex + 1]
  873. )
  874. elif flag < 124:
  875. b2 = triplets[tripletIndex + 1]
  876. dx = withSign(flag, (triplets[tripletIndex] << 4) + (b2 >> 4))
  877. dy = withSign(
  878. flag >> 1, ((b2 & 0x0F) << 8) + triplets[tripletIndex + 2]
  879. )
  880. else:
  881. dx = withSign(
  882. flag, (triplets[tripletIndex] << 8) + triplets[tripletIndex + 1]
  883. )
  884. dy = withSign(
  885. flag >> 1,
  886. (triplets[tripletIndex + 2] << 8) + triplets[tripletIndex + 3],
  887. )
  888. tripletIndex += nBytes
  889. x += dx
  890. y += dy
  891. glyph.coordinates[i] = (x, y)
  892. glyph.flags.append(int(onCurve))
  893. bytesConsumed = tripletIndex
  894. self.glyphStream = self.glyphStream[bytesConsumed:]
  895. def _encodeGlyph(self, glyphID):
  896. glyphName = self.getGlyphName(glyphID)
  897. glyph = self[glyphName]
  898. self.nContourStream += struct.pack(">h", glyph.numberOfContours)
  899. if glyph.numberOfContours == 0:
  900. return
  901. elif glyph.isComposite():
  902. self._encodeComponents(glyph)
  903. elif glyph.isVarComposite():
  904. raise NotImplementedError
  905. else:
  906. self._encodeCoordinates(glyph)
  907. self._encodeOverlapSimpleFlag(glyph, glyphID)
  908. self._encodeBBox(glyphID, glyph)
  909. def _encodeComponents(self, glyph):
  910. lastcomponent = len(glyph.components) - 1
  911. more = 1
  912. haveInstructions = 0
  913. for i in range(len(glyph.components)):
  914. if i == lastcomponent:
  915. haveInstructions = hasattr(glyph, "program")
  916. more = 0
  917. component = glyph.components[i]
  918. self.compositeStream += component.compile(more, haveInstructions, self)
  919. if haveInstructions:
  920. self._encodeInstructions(glyph)
  921. def _encodeCoordinates(self, glyph):
  922. lastEndPoint = -1
  923. if _g_l_y_f.flagCubic in glyph.flags:
  924. raise NotImplementedError
  925. for endPoint in glyph.endPtsOfContours:
  926. ptsOfContour = endPoint - lastEndPoint
  927. self.nPointsStream += pack255UShort(ptsOfContour)
  928. lastEndPoint = endPoint
  929. self._encodeTriplets(glyph)
  930. self._encodeInstructions(glyph)
  931. def _encodeOverlapSimpleFlag(self, glyph, glyphID):
  932. if glyph.numberOfContours <= 0:
  933. return
  934. if glyph.flags[0] & _g_l_y_f.flagOverlapSimple:
  935. byte = glyphID >> 3
  936. bit = glyphID & 7
  937. self.overlapSimpleBitmap[byte] |= 0x80 >> bit
  938. def _encodeInstructions(self, glyph):
  939. instructions = glyph.program.getBytecode()
  940. self.glyphStream += pack255UShort(len(instructions))
  941. self.instructionStream += instructions
  942. def _encodeBBox(self, glyphID, glyph):
  943. assert glyph.numberOfContours != 0, "empty glyph has no bbox"
  944. if not glyph.isComposite():
  945. # for simple glyphs, compare the encoded bounding box info with the calculated
  946. # values, and if they match omit the bounding box info
  947. currentBBox = glyph.xMin, glyph.yMin, glyph.xMax, glyph.yMax
  948. calculatedBBox = calcIntBounds(glyph.coordinates)
  949. if currentBBox == calculatedBBox:
  950. return
  951. self.bboxBitmap[glyphID >> 3] |= 0x80 >> (glyphID & 7)
  952. self.bboxStream += sstruct.pack(bboxFormat, glyph)
  953. def _encodeTriplets(self, glyph):
  954. assert len(glyph.coordinates) == len(glyph.flags)
  955. coordinates = glyph.coordinates.copy()
  956. coordinates.absoluteToRelative()
  957. flags = array.array("B")
  958. triplets = array.array("B")
  959. for i in range(len(coordinates)):
  960. onCurve = glyph.flags[i] & _g_l_y_f.flagOnCurve
  961. x, y = coordinates[i]
  962. absX = abs(x)
  963. absY = abs(y)
  964. onCurveBit = 0 if onCurve else 128
  965. xSignBit = 0 if (x < 0) else 1
  966. ySignBit = 0 if (y < 0) else 1
  967. xySignBits = xSignBit + 2 * ySignBit
  968. if x == 0 and absY < 1280:
  969. flags.append(onCurveBit + ((absY & 0xF00) >> 7) + ySignBit)
  970. triplets.append(absY & 0xFF)
  971. elif y == 0 and absX < 1280:
  972. flags.append(onCurveBit + 10 + ((absX & 0xF00) >> 7) + xSignBit)
  973. triplets.append(absX & 0xFF)
  974. elif absX < 65 and absY < 65:
  975. flags.append(
  976. onCurveBit
  977. + 20
  978. + ((absX - 1) & 0x30)
  979. + (((absY - 1) & 0x30) >> 2)
  980. + xySignBits
  981. )
  982. triplets.append((((absX - 1) & 0xF) << 4) | ((absY - 1) & 0xF))
  983. elif absX < 769 and absY < 769:
  984. flags.append(
  985. onCurveBit
  986. + 84
  987. + 12 * (((absX - 1) & 0x300) >> 8)
  988. + (((absY - 1) & 0x300) >> 6)
  989. + xySignBits
  990. )
  991. triplets.append((absX - 1) & 0xFF)
  992. triplets.append((absY - 1) & 0xFF)
  993. elif absX < 4096 and absY < 4096:
  994. flags.append(onCurveBit + 120 + xySignBits)
  995. triplets.append(absX >> 4)
  996. triplets.append(((absX & 0xF) << 4) | (absY >> 8))
  997. triplets.append(absY & 0xFF)
  998. else:
  999. flags.append(onCurveBit + 124 + xySignBits)
  1000. triplets.append(absX >> 8)
  1001. triplets.append(absX & 0xFF)
  1002. triplets.append(absY >> 8)
  1003. triplets.append(absY & 0xFF)
  1004. self.flagStream += flags.tobytes()
  1005. self.glyphStream += triplets.tobytes()
  1006. class WOFF2HmtxTable(getTableClass("hmtx")):
  1007. def __init__(self, tag=None):
  1008. self.tableTag = Tag(tag or "hmtx")
  1009. def reconstruct(self, data, ttFont):
  1010. (flags,) = struct.unpack(">B", data[:1])
  1011. data = data[1:]
  1012. if flags & 0b11111100 != 0:
  1013. raise TTLibError("Bits 2-7 of '%s' flags are reserved" % self.tableTag)
  1014. # When bit 0 is _not_ set, the lsb[] array is present
  1015. hasLsbArray = flags & 1 == 0
  1016. # When bit 1 is _not_ set, the leftSideBearing[] array is present
  1017. hasLeftSideBearingArray = flags & 2 == 0
  1018. if hasLsbArray and hasLeftSideBearingArray:
  1019. raise TTLibError(
  1020. "either bits 0 or 1 (or both) must set in transformed '%s' flags"
  1021. % self.tableTag
  1022. )
  1023. glyfTable = ttFont["glyf"]
  1024. headerTable = ttFont["hhea"]
  1025. glyphOrder = glyfTable.glyphOrder
  1026. numGlyphs = len(glyphOrder)
  1027. numberOfHMetrics = min(int(headerTable.numberOfHMetrics), numGlyphs)
  1028. assert len(data) >= 2 * numberOfHMetrics
  1029. advanceWidthArray = array.array("H", data[: 2 * numberOfHMetrics])
  1030. if sys.byteorder != "big":
  1031. advanceWidthArray.byteswap()
  1032. data = data[2 * numberOfHMetrics :]
  1033. if hasLsbArray:
  1034. assert len(data) >= 2 * numberOfHMetrics
  1035. lsbArray = array.array("h", data[: 2 * numberOfHMetrics])
  1036. if sys.byteorder != "big":
  1037. lsbArray.byteswap()
  1038. data = data[2 * numberOfHMetrics :]
  1039. else:
  1040. # compute (proportional) glyphs' lsb from their xMin
  1041. lsbArray = array.array("h")
  1042. for i, glyphName in enumerate(glyphOrder):
  1043. if i >= numberOfHMetrics:
  1044. break
  1045. glyph = glyfTable[glyphName]
  1046. xMin = getattr(glyph, "xMin", 0)
  1047. lsbArray.append(xMin)
  1048. numberOfSideBearings = numGlyphs - numberOfHMetrics
  1049. if hasLeftSideBearingArray:
  1050. assert len(data) >= 2 * numberOfSideBearings
  1051. leftSideBearingArray = array.array("h", data[: 2 * numberOfSideBearings])
  1052. if sys.byteorder != "big":
  1053. leftSideBearingArray.byteswap()
  1054. data = data[2 * numberOfSideBearings :]
  1055. else:
  1056. # compute (monospaced) glyphs' leftSideBearing from their xMin
  1057. leftSideBearingArray = array.array("h")
  1058. for i, glyphName in enumerate(glyphOrder):
  1059. if i < numberOfHMetrics:
  1060. continue
  1061. glyph = glyfTable[glyphName]
  1062. xMin = getattr(glyph, "xMin", 0)
  1063. leftSideBearingArray.append(xMin)
  1064. if data:
  1065. raise TTLibError("too much '%s' table data" % self.tableTag)
  1066. self.metrics = {}
  1067. for i in range(numberOfHMetrics):
  1068. glyphName = glyphOrder[i]
  1069. advanceWidth, lsb = advanceWidthArray[i], lsbArray[i]
  1070. self.metrics[glyphName] = (advanceWidth, lsb)
  1071. lastAdvance = advanceWidthArray[-1]
  1072. for i in range(numberOfSideBearings):
  1073. glyphName = glyphOrder[i + numberOfHMetrics]
  1074. self.metrics[glyphName] = (lastAdvance, leftSideBearingArray[i])
  1075. def transform(self, ttFont):
  1076. glyphOrder = ttFont.getGlyphOrder()
  1077. glyf = ttFont["glyf"]
  1078. hhea = ttFont["hhea"]
  1079. numberOfHMetrics = hhea.numberOfHMetrics
  1080. # check if any of the proportional glyphs has left sidebearings that
  1081. # differ from their xMin bounding box values.
  1082. hasLsbArray = False
  1083. for i in range(numberOfHMetrics):
  1084. glyphName = glyphOrder[i]
  1085. lsb = self.metrics[glyphName][1]
  1086. if lsb != getattr(glyf[glyphName], "xMin", 0):
  1087. hasLsbArray = True
  1088. break
  1089. # do the same for the monospaced glyphs (if any) at the end of hmtx table
  1090. hasLeftSideBearingArray = False
  1091. for i in range(numberOfHMetrics, len(glyphOrder)):
  1092. glyphName = glyphOrder[i]
  1093. lsb = self.metrics[glyphName][1]
  1094. if lsb != getattr(glyf[glyphName], "xMin", 0):
  1095. hasLeftSideBearingArray = True
  1096. break
  1097. # if we need to encode both sidebearings arrays, then no transformation is
  1098. # applicable, and we must use the untransformed hmtx data
  1099. if hasLsbArray and hasLeftSideBearingArray:
  1100. return
  1101. # set bit 0 and 1 when the respective arrays are _not_ present
  1102. flags = 0
  1103. if not hasLsbArray:
  1104. flags |= 1 << 0
  1105. if not hasLeftSideBearingArray:
  1106. flags |= 1 << 1
  1107. data = struct.pack(">B", flags)
  1108. advanceWidthArray = array.array(
  1109. "H",
  1110. [
  1111. self.metrics[glyphName][0]
  1112. for i, glyphName in enumerate(glyphOrder)
  1113. if i < numberOfHMetrics
  1114. ],
  1115. )
  1116. if sys.byteorder != "big":
  1117. advanceWidthArray.byteswap()
  1118. data += advanceWidthArray.tobytes()
  1119. if hasLsbArray:
  1120. lsbArray = array.array(
  1121. "h",
  1122. [
  1123. self.metrics[glyphName][1]
  1124. for i, glyphName in enumerate(glyphOrder)
  1125. if i < numberOfHMetrics
  1126. ],
  1127. )
  1128. if sys.byteorder != "big":
  1129. lsbArray.byteswap()
  1130. data += lsbArray.tobytes()
  1131. if hasLeftSideBearingArray:
  1132. leftSideBearingArray = array.array(
  1133. "h",
  1134. [
  1135. self.metrics[glyphOrder[i]][1]
  1136. for i in range(numberOfHMetrics, len(glyphOrder))
  1137. ],
  1138. )
  1139. if sys.byteorder != "big":
  1140. leftSideBearingArray.byteswap()
  1141. data += leftSideBearingArray.tobytes()
  1142. return data
  1143. class WOFF2FlavorData(WOFFFlavorData):
  1144. Flavor = "woff2"
  1145. def __init__(self, reader=None, data=None, transformedTables=None):
  1146. """Data class that holds the WOFF2 header major/minor version, any
  1147. metadata or private data (as bytes strings), and the set of
  1148. table tags that have transformations applied (if reader is not None),
  1149. or will have once the WOFF2 font is compiled.
  1150. Args:
  1151. reader: an SFNTReader (or subclass) object to read flavor data from.
  1152. data: another WOFFFlavorData object to initialise data from.
  1153. transformedTables: set of strings containing table tags to be transformed.
  1154. Raises:
  1155. ImportError if the brotli module is not installed.
  1156. NOTE: The 'reader' argument, on the one hand, and the 'data' and
  1157. 'transformedTables' arguments, on the other hand, are mutually exclusive.
  1158. """
  1159. if not haveBrotli:
  1160. raise ImportError("No module named brotli")
  1161. if reader is not None:
  1162. if data is not None:
  1163. raise TypeError("'reader' and 'data' arguments are mutually exclusive")
  1164. if transformedTables is not None:
  1165. raise TypeError(
  1166. "'reader' and 'transformedTables' arguments are mutually exclusive"
  1167. )
  1168. if transformedTables is not None and (
  1169. "glyf" in transformedTables
  1170. and "loca" not in transformedTables
  1171. or "loca" in transformedTables
  1172. and "glyf" not in transformedTables
  1173. ):
  1174. raise ValueError("'glyf' and 'loca' must be transformed (or not) together")
  1175. super(WOFF2FlavorData, self).__init__(reader=reader)
  1176. if reader:
  1177. transformedTables = [
  1178. tag for tag, entry in reader.tables.items() if entry.transformed
  1179. ]
  1180. elif data:
  1181. self.majorVersion = data.majorVersion
  1182. self.majorVersion = data.minorVersion
  1183. self.metaData = data.metaData
  1184. self.privData = data.privData
  1185. if transformedTables is None and hasattr(data, "transformedTables"):
  1186. transformedTables = data.transformedTables
  1187. if transformedTables is None:
  1188. transformedTables = woff2TransformedTableTags
  1189. self.transformedTables = set(transformedTables)
  1190. def _decompress(self, rawData):
  1191. return brotli.decompress(rawData)
  1192. def unpackBase128(data):
  1193. r"""Read one to five bytes from UIntBase128-encoded input string, and return
  1194. a tuple containing the decoded integer plus any leftover data.
  1195. >>> unpackBase128(b'\x3f\x00\x00') == (63, b"\x00\x00")
  1196. True
  1197. >>> unpackBase128(b'\x8f\xff\xff\xff\x7f')[0] == 4294967295
  1198. True
  1199. >>> unpackBase128(b'\x80\x80\x3f') # doctest: +IGNORE_EXCEPTION_DETAIL
  1200. Traceback (most recent call last):
  1201. File "<stdin>", line 1, in ?
  1202. TTLibError: UIntBase128 value must not start with leading zeros
  1203. >>> unpackBase128(b'\x8f\xff\xff\xff\xff\x7f')[0] # doctest: +IGNORE_EXCEPTION_DETAIL
  1204. Traceback (most recent call last):
  1205. File "<stdin>", line 1, in ?
  1206. TTLibError: UIntBase128-encoded sequence is longer than 5 bytes
  1207. >>> unpackBase128(b'\x90\x80\x80\x80\x00')[0] # doctest: +IGNORE_EXCEPTION_DETAIL
  1208. Traceback (most recent call last):
  1209. File "<stdin>", line 1, in ?
  1210. TTLibError: UIntBase128 value exceeds 2**32-1
  1211. """
  1212. if len(data) == 0:
  1213. raise TTLibError("not enough data to unpack UIntBase128")
  1214. result = 0
  1215. if byteord(data[0]) == 0x80:
  1216. # font must be rejected if UIntBase128 value starts with 0x80
  1217. raise TTLibError("UIntBase128 value must not start with leading zeros")
  1218. for i in range(woff2Base128MaxSize):
  1219. if len(data) == 0:
  1220. raise TTLibError("not enough data to unpack UIntBase128")
  1221. code = byteord(data[0])
  1222. data = data[1:]
  1223. # if any of the top seven bits are set then we're about to overflow
  1224. if result & 0xFE000000:
  1225. raise TTLibError("UIntBase128 value exceeds 2**32-1")
  1226. # set current value = old value times 128 bitwise-or (byte bitwise-and 127)
  1227. result = (result << 7) | (code & 0x7F)
  1228. # repeat until the most significant bit of byte is false
  1229. if (code & 0x80) == 0:
  1230. # return result plus left over data
  1231. return result, data
  1232. # make sure not to exceed the size bound
  1233. raise TTLibError("UIntBase128-encoded sequence is longer than 5 bytes")
  1234. def base128Size(n):
  1235. """Return the length in bytes of a UIntBase128-encoded sequence with value n.
  1236. >>> base128Size(0)
  1237. 1
  1238. >>> base128Size(24567)
  1239. 3
  1240. >>> base128Size(2**32-1)
  1241. 5
  1242. """
  1243. assert n >= 0
  1244. size = 1
  1245. while n >= 128:
  1246. size += 1
  1247. n >>= 7
  1248. return size
  1249. def packBase128(n):
  1250. r"""Encode unsigned integer in range 0 to 2**32-1 (inclusive) to a string of
  1251. bytes using UIntBase128 variable-length encoding. Produce the shortest possible
  1252. encoding.
  1253. >>> packBase128(63) == b"\x3f"
  1254. True
  1255. >>> packBase128(2**32-1) == b'\x8f\xff\xff\xff\x7f'
  1256. True
  1257. """
  1258. if n < 0 or n >= 2**32:
  1259. raise TTLibError("UIntBase128 format requires 0 <= integer <= 2**32-1")
  1260. data = b""
  1261. size = base128Size(n)
  1262. for i in range(size):
  1263. b = (n >> (7 * (size - i - 1))) & 0x7F
  1264. if i < size - 1:
  1265. b |= 0x80
  1266. data += struct.pack("B", b)
  1267. return data
  1268. def unpack255UShort(data):
  1269. """Read one to three bytes from 255UInt16-encoded input string, and return a
  1270. tuple containing the decoded integer plus any leftover data.
  1271. >>> unpack255UShort(bytechr(252))[0]
  1272. 252
  1273. Note that some numbers (e.g. 506) can have multiple encodings:
  1274. >>> unpack255UShort(struct.pack("BB", 254, 0))[0]
  1275. 506
  1276. >>> unpack255UShort(struct.pack("BB", 255, 253))[0]
  1277. 506
  1278. >>> unpack255UShort(struct.pack("BBB", 253, 1, 250))[0]
  1279. 506
  1280. """
  1281. code = byteord(data[:1])
  1282. data = data[1:]
  1283. if code == 253:
  1284. # read two more bytes as an unsigned short
  1285. if len(data) < 2:
  1286. raise TTLibError("not enough data to unpack 255UInt16")
  1287. (result,) = struct.unpack(">H", data[:2])
  1288. data = data[2:]
  1289. elif code == 254:
  1290. # read another byte, plus 253 * 2
  1291. if len(data) == 0:
  1292. raise TTLibError("not enough data to unpack 255UInt16")
  1293. result = byteord(data[:1])
  1294. result += 506
  1295. data = data[1:]
  1296. elif code == 255:
  1297. # read another byte, plus 253
  1298. if len(data) == 0:
  1299. raise TTLibError("not enough data to unpack 255UInt16")
  1300. result = byteord(data[:1])
  1301. result += 253
  1302. data = data[1:]
  1303. else:
  1304. # leave as is if lower than 253
  1305. result = code
  1306. # return result plus left over data
  1307. return result, data
  1308. def pack255UShort(value):
  1309. r"""Encode unsigned integer in range 0 to 65535 (inclusive) to a bytestring
  1310. using 255UInt16 variable-length encoding.
  1311. >>> pack255UShort(252) == b'\xfc'
  1312. True
  1313. >>> pack255UShort(506) == b'\xfe\x00'
  1314. True
  1315. >>> pack255UShort(762) == b'\xfd\x02\xfa'
  1316. True
  1317. """
  1318. if value < 0 or value > 0xFFFF:
  1319. raise TTLibError("255UInt16 format requires 0 <= integer <= 65535")
  1320. if value < 253:
  1321. return struct.pack(">B", value)
  1322. elif value < 506:
  1323. return struct.pack(">BB", 255, value - 253)
  1324. elif value < 762:
  1325. return struct.pack(">BB", 254, value - 506)
  1326. else:
  1327. return struct.pack(">BH", 253, value)
  1328. def compress(input_file, output_file, transform_tables=None):
  1329. """Compress OpenType font to WOFF2.
  1330. Args:
  1331. input_file: a file path, file or file-like object (open in binary mode)
  1332. containing an OpenType font (either CFF- or TrueType-flavored).
  1333. output_file: a file path, file or file-like object where to save the
  1334. compressed WOFF2 font.
  1335. transform_tables: Optional[Iterable[str]]: a set of table tags for which
  1336. to enable preprocessing transformations. By default, only 'glyf'
  1337. and 'loca' tables are transformed. An empty set means disable all
  1338. transformations.
  1339. """
  1340. log.info("Processing %s => %s" % (input_file, output_file))
  1341. font = TTFont(input_file, recalcBBoxes=False, recalcTimestamp=False)
  1342. font.flavor = "woff2"
  1343. if transform_tables is not None:
  1344. font.flavorData = WOFF2FlavorData(
  1345. data=font.flavorData, transformedTables=transform_tables
  1346. )
  1347. font.save(output_file, reorderTables=False)
  1348. def decompress(input_file, output_file):
  1349. """Decompress WOFF2 font to OpenType font.
  1350. Args:
  1351. input_file: a file path, file or file-like object (open in binary mode)
  1352. containing a compressed WOFF2 font.
  1353. output_file: a file path, file or file-like object where to save the
  1354. decompressed OpenType font.
  1355. """
  1356. log.info("Processing %s => %s" % (input_file, output_file))
  1357. font = TTFont(input_file, recalcBBoxes=False, recalcTimestamp=False)
  1358. font.flavor = None
  1359. font.flavorData = None
  1360. font.save(output_file, reorderTables=True)
  1361. def main(args=None):
  1362. """Compress and decompress WOFF2 fonts"""
  1363. import argparse
  1364. from fontTools import configLogger
  1365. from fontTools.ttx import makeOutputFileName
  1366. class _HelpAction(argparse._HelpAction):
  1367. def __call__(self, parser, namespace, values, option_string=None):
  1368. subparsers_actions = [
  1369. action
  1370. for action in parser._actions
  1371. if isinstance(action, argparse._SubParsersAction)
  1372. ]
  1373. for subparsers_action in subparsers_actions:
  1374. for choice, subparser in subparsers_action.choices.items():
  1375. print(subparser.format_help())
  1376. parser.exit()
  1377. class _NoGlyfTransformAction(argparse.Action):
  1378. def __call__(self, parser, namespace, values, option_string=None):
  1379. namespace.transform_tables.difference_update({"glyf", "loca"})
  1380. class _HmtxTransformAction(argparse.Action):
  1381. def __call__(self, parser, namespace, values, option_string=None):
  1382. namespace.transform_tables.add("hmtx")
  1383. parser = argparse.ArgumentParser(
  1384. prog="fonttools ttLib.woff2", description=main.__doc__, add_help=False
  1385. )
  1386. parser.add_argument(
  1387. "-h", "--help", action=_HelpAction, help="show this help message and exit"
  1388. )
  1389. parser_group = parser.add_subparsers(title="sub-commands")
  1390. parser_compress = parser_group.add_parser(
  1391. "compress", description="Compress a TTF or OTF font to WOFF2"
  1392. )
  1393. parser_decompress = parser_group.add_parser(
  1394. "decompress", description="Decompress a WOFF2 font to OTF"
  1395. )
  1396. for subparser in (parser_compress, parser_decompress):
  1397. group = subparser.add_mutually_exclusive_group(required=False)
  1398. group.add_argument(
  1399. "-v",
  1400. "--verbose",
  1401. action="store_true",
  1402. help="print more messages to console",
  1403. )
  1404. group.add_argument(
  1405. "-q",
  1406. "--quiet",
  1407. action="store_true",
  1408. help="do not print messages to console",
  1409. )
  1410. parser_compress.add_argument(
  1411. "input_file",
  1412. metavar="INPUT",
  1413. help="the input OpenType font (.ttf or .otf)",
  1414. )
  1415. parser_decompress.add_argument(
  1416. "input_file",
  1417. metavar="INPUT",
  1418. help="the input WOFF2 font",
  1419. )
  1420. parser_compress.add_argument(
  1421. "-o",
  1422. "--output-file",
  1423. metavar="OUTPUT",
  1424. help="the output WOFF2 font",
  1425. )
  1426. parser_decompress.add_argument(
  1427. "-o",
  1428. "--output-file",
  1429. metavar="OUTPUT",
  1430. help="the output OpenType font",
  1431. )
  1432. transform_group = parser_compress.add_argument_group()
  1433. transform_group.add_argument(
  1434. "--no-glyf-transform",
  1435. dest="transform_tables",
  1436. nargs=0,
  1437. action=_NoGlyfTransformAction,
  1438. help="Do not transform glyf (and loca) tables",
  1439. )
  1440. transform_group.add_argument(
  1441. "--hmtx-transform",
  1442. dest="transform_tables",
  1443. nargs=0,
  1444. action=_HmtxTransformAction,
  1445. help="Enable optional transformation for 'hmtx' table",
  1446. )
  1447. parser_compress.set_defaults(
  1448. subcommand=compress,
  1449. transform_tables={"glyf", "loca"},
  1450. )
  1451. parser_decompress.set_defaults(subcommand=decompress)
  1452. options = vars(parser.parse_args(args))
  1453. subcommand = options.pop("subcommand", None)
  1454. if not subcommand:
  1455. parser.print_help()
  1456. return
  1457. quiet = options.pop("quiet")
  1458. verbose = options.pop("verbose")
  1459. configLogger(
  1460. level=("ERROR" if quiet else "DEBUG" if verbose else "INFO"),
  1461. )
  1462. if not options["output_file"]:
  1463. if subcommand is compress:
  1464. extension = ".woff2"
  1465. elif subcommand is decompress:
  1466. # choose .ttf/.otf file extension depending on sfntVersion
  1467. with open(options["input_file"], "rb") as f:
  1468. f.seek(4) # skip 'wOF2' signature
  1469. sfntVersion = f.read(4)
  1470. assert len(sfntVersion) == 4, "not enough data"
  1471. extension = ".otf" if sfntVersion == b"OTTO" else ".ttf"
  1472. else:
  1473. raise AssertionError(subcommand)
  1474. options["output_file"] = makeOutputFileName(
  1475. options["input_file"], outputDir=None, extension=extension
  1476. )
  1477. try:
  1478. subcommand(**options)
  1479. except TTLibError as e:
  1480. parser.error(e)
  1481. if __name__ == "__main__":
  1482. sys.exit(main())