gsub.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import struct
  2. from lxml.etree import Element, ElementTree, fromstring
  3. from data import VFixed
  4. from tables.common.otlScript import ScriptList, ScriptRecord, Script
  5. from tables.common.otlFeature import FeatureList, FeatureRecord, Feature
  6. from tables.common.otlLookup import LookupList, LookupType4
  7. from transform.bytes import outputTableBytes
  8. class GSUB:
  9. def __init__(self, glyphs):
  10. # we're using version 1.0 here, which doesn't have a
  11. # featureVariations table, because we don't need it.
  12. self.majorVersion = 1
  13. self.minorVersion = 0
  14. self.scriptList = ScriptList() # non-editable ScriptList
  15. self.featureList = FeatureList() # non-editable FeatureList
  16. self.lookupList = LookupList(glyphs) # non-editable LookupList
  17. def toTTX(self):
  18. gsub = Element("GSUB")
  19. gsub.append(Element("Version", {"value": VFixed(f"{self.majorVersion}.{self.minorVersion}").toHexStr() })) # TTX wants the version in this format.
  20. gsub.append(self.scriptList.toTTX())
  21. gsub.append(self.featureList.toTTX())
  22. gsub.append(self.lookupList.toTTX())
  23. return gsub
  24. def toBytes(self):
  25. # TODO: make toBytes functions for OTL scripts, features and lookups.
  26. # TODO: learn how to generate offsets for OTL scripts, features and lookups.
  27. # - generate the scriptList, featureList, and lookup list, generate offsets.
  28. gsub = struct.pack( ">HH"
  29. , self.majorVersion # UInt16
  30. , self.minorVersion # UInt16
  31. # offset to scriptList # Offset16
  32. # offset to featureList # Offset16
  33. # offset to lookupList # Offset16
  34. )
  35. # - dump the lists immediately after this information
  36. # - return the table.
  37. return outputTableBytes(gsub)