_l_o_c_a.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from . import DefaultTable
  2. import sys
  3. import array
  4. import logging
  5. log = logging.getLogger(__name__)
  6. class table__l_o_c_a(DefaultTable.DefaultTable):
  7. dependencies = ["glyf"]
  8. def decompile(self, data, ttFont):
  9. longFormat = ttFont["head"].indexToLocFormat
  10. if longFormat:
  11. format = "I"
  12. else:
  13. format = "H"
  14. locations = array.array(format)
  15. locations.frombytes(data)
  16. if sys.byteorder != "big":
  17. locations.byteswap()
  18. if not longFormat:
  19. l = array.array("I")
  20. for i in range(len(locations)):
  21. l.append(locations[i] * 2)
  22. locations = l
  23. if len(locations) < (ttFont["maxp"].numGlyphs + 1):
  24. log.warning(
  25. "corrupt 'loca' table, or wrong numGlyphs in 'maxp': %d %d",
  26. len(locations) - 1,
  27. ttFont["maxp"].numGlyphs,
  28. )
  29. self.locations = locations
  30. def compile(self, ttFont):
  31. try:
  32. max_location = max(self.locations)
  33. except AttributeError:
  34. self.set([])
  35. max_location = 0
  36. if max_location < 0x20000 and all(l % 2 == 0 for l in self.locations):
  37. locations = array.array("H")
  38. for i in range(len(self.locations)):
  39. locations.append(self.locations[i] // 2)
  40. ttFont["head"].indexToLocFormat = 0
  41. else:
  42. locations = array.array("I", self.locations)
  43. ttFont["head"].indexToLocFormat = 1
  44. if sys.byteorder != "big":
  45. locations.byteswap()
  46. return locations.tobytes()
  47. def set(self, locations):
  48. self.locations = array.array("I", locations)
  49. def toXML(self, writer, ttFont):
  50. writer.comment("The 'loca' table will be calculated by the compiler")
  51. writer.newline()
  52. def __getitem__(self, index):
  53. return self.locations[index]
  54. def __len__(self):
  55. return len(self.locations)