BitmapGlyphMetrics.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Since bitmap glyph metrics are shared between EBLC and EBDT
  2. # this class gets its own python file.
  3. from fontTools.misc import sstruct
  4. from fontTools.misc.textTools import safeEval
  5. import logging
  6. log = logging.getLogger(__name__)
  7. bigGlyphMetricsFormat = """
  8. > # big endian
  9. height: B
  10. width: B
  11. horiBearingX: b
  12. horiBearingY: b
  13. horiAdvance: B
  14. vertBearingX: b
  15. vertBearingY: b
  16. vertAdvance: B
  17. """
  18. smallGlyphMetricsFormat = """
  19. > # big endian
  20. height: B
  21. width: B
  22. BearingX: b
  23. BearingY: b
  24. Advance: B
  25. """
  26. class BitmapGlyphMetrics(object):
  27. def toXML(self, writer, ttFont):
  28. writer.begintag(self.__class__.__name__)
  29. writer.newline()
  30. for metricName in sstruct.getformat(self.__class__.binaryFormat)[1]:
  31. writer.simpletag(metricName, value=getattr(self, metricName))
  32. writer.newline()
  33. writer.endtag(self.__class__.__name__)
  34. writer.newline()
  35. def fromXML(self, name, attrs, content, ttFont):
  36. metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1])
  37. for element in content:
  38. if not isinstance(element, tuple):
  39. continue
  40. name, attrs, content = element
  41. # Make sure this is a metric that is needed by GlyphMetrics.
  42. if name in metricNames:
  43. vars(self)[name] = safeEval(attrs["value"])
  44. else:
  45. log.warning(
  46. "unknown name '%s' being ignored in %s.",
  47. name,
  48. self.__class__.__name__,
  49. )
  50. class BigGlyphMetrics(BitmapGlyphMetrics):
  51. binaryFormat = bigGlyphMetricsFormat
  52. class SmallGlyphMetrics(BitmapGlyphMetrics):
  53. binaryFormat = smallGlyphMetricsFormat