DefaultTable.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from fontTools.misc.textTools import Tag
  4. from fontTools.ttLib import getClassTag
  5. if TYPE_CHECKING:
  6. from typing import Any
  7. from fontTools.misc.xmlWriter import XMLWriter
  8. from fontTools.ttLib import TTFont
  9. class DefaultTable:
  10. dependencies: list[str] = []
  11. def __init__(self, tag: str | bytes | None = None) -> None:
  12. if tag is None:
  13. tag = getClassTag(self.__class__)
  14. self.tableTag = Tag(tag)
  15. def decompile(self, data: bytes, ttFont: TTFont) -> None:
  16. self.data = data
  17. def compile(self, ttFont: TTFont) -> bytes:
  18. return self.data
  19. def toXML(
  20. self, writer: XMLWriter, ttFont: TTFont, **kwargs: dict[str, Any]
  21. ) -> None:
  22. if hasattr(self, "ERROR"):
  23. writer.comment("An error occurred during the decompilation of this table")
  24. writer.newline()
  25. writer.comment(self.ERROR)
  26. writer.newline()
  27. writer.begintag("hexdata")
  28. writer.newline()
  29. writer.dumphex(self.compile(ttFont))
  30. writer.endtag("hexdata")
  31. writer.newline()
  32. def fromXML(
  33. self, name: str, attrs: dict[str, str], content: str, ttFont: TTFont
  34. ) -> None:
  35. from fontTools import ttLib
  36. from fontTools.misc.textTools import readHex
  37. if name != "hexdata":
  38. raise ttLib.TTLibError("can't handle '%s' element" % name)
  39. self.decompile(readHex(content), ttFont)
  40. def __repr__(self) -> str:
  41. return "<'%s' table at %x>" % (self.tableTag, id(self))
  42. def __eq__(self, other: Any) -> bool:
  43. if type(self) != type(other):
  44. return NotImplemented
  45. return self.__dict__ == other.__dict__
  46. def __ne__(self, other: Any) -> bool:
  47. result = self.__eq__(other)
  48. return result if result is NotImplemented else not result