otlScript.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from lxml.etree import Element
  2. from data import Tag
  3. # OTLScript
  4. # -----------------------------
  5. # Classes representing common OpenType Layout Script structures.
  6. # (https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#scripts-and-languages)
  7. class Script:
  8. """
  9. Class representing a placeholder Script table.
  10. Currently not editable atm - it's just designed to have the right data for forc's particular context.
  11. """
  12. def __init(self):
  13. self.whatever = 0
  14. def toTTX(self):
  15. script = Element("Script")
  16. # ReqFeatureIndex value 65535 tells OpenType we don't need any Features in particular.
  17. dfl = Element("DefaultLangSys")
  18. dfl.append(Element("ReqFeatureIndex", {"value": "65535" }))
  19. dfl.append(Element("FeatureIndex", {"index": "0", "value": "0" }))
  20. script.append(dfl)
  21. return script
  22. class ScriptRecord:
  23. """
  24. Class representing a placeholder ScriptRecord table.
  25. Currently not editable atm - it's just designed to have the right data for forc's particular context.
  26. """
  27. def __init__(self):
  28. self.scriptTag = Tag("DFLT") # script tag identifier.
  29. # DFLT means 'default', ie. 'no script in particular'
  30. self.script = Script() # placeholder script table.
  31. def toTTX(self, index):
  32. scriptRecord = Element("ScriptRecord", {"index": str(index) })
  33. scriptRecord.append(Element("ScriptTag", {"value": str(self.scriptTag) }))
  34. scriptRecord.append(self.script.toTTX())
  35. return scriptRecord
  36. def toBytes(self):
  37. # TODO: need to input the offset from ScriptList building.
  38. return struct.pack( '>4b'
  39. , self.scriptTag.toBytes() # Tag (4 bytes/UInt32)
  40. # TODO: Offset16 to script table from the beginning of scriptList
  41. )
  42. class ScriptList:
  43. """
  44. Class representing a ScriptList table.
  45. """
  46. def __init__(self):
  47. self.scriptRecords = [ScriptRecord()] # array of script tables.
  48. def toTTX(self):
  49. scriptList = Element("ScriptList")
  50. for index, sr in enumerate(self.scriptRecords):
  51. scriptList.append(sr.toTTX(index))
  52. return scriptList
  53. def toBytes(self):
  54. return struct.pack( '>H'
  55. , len(self.scriptRecords) # UInt16
  56. # TODO: insert the scriptRecords themselves.
  57. )