sbixGlyph.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from fontTools.misc import sstruct
  2. from fontTools.misc.textTools import readHex, safeEval
  3. import struct
  4. sbixGlyphHeaderFormat = """
  5. >
  6. originOffsetX: h # The x-value of the point in the glyph relative to its
  7. # lower-left corner which corresponds to the origin of
  8. # the glyph on the screen, that is the point on the
  9. # baseline at the left edge of the glyph.
  10. originOffsetY: h # The y-value of the point in the glyph relative to its
  11. # lower-left corner which corresponds to the origin of
  12. # the glyph on the screen, that is the point on the
  13. # baseline at the left edge of the glyph.
  14. graphicType: 4s # e.g. "png "
  15. """
  16. sbixGlyphHeaderFormatSize = sstruct.calcsize(sbixGlyphHeaderFormat)
  17. class Glyph(object):
  18. def __init__(
  19. self,
  20. glyphName=None,
  21. referenceGlyphName=None,
  22. originOffsetX=0,
  23. originOffsetY=0,
  24. graphicType=None,
  25. imageData=None,
  26. rawdata=None,
  27. gid=0,
  28. ):
  29. self.gid = gid
  30. self.glyphName = glyphName
  31. self.referenceGlyphName = referenceGlyphName
  32. self.originOffsetX = originOffsetX
  33. self.originOffsetY = originOffsetY
  34. self.rawdata = rawdata
  35. self.graphicType = graphicType
  36. self.imageData = imageData
  37. # fix self.graphicType if it is null terminated or too short
  38. if self.graphicType is not None:
  39. if self.graphicType[-1] == "\0":
  40. self.graphicType = self.graphicType[:-1]
  41. if len(self.graphicType) > 4:
  42. from fontTools import ttLib
  43. raise ttLib.TTLibError(
  44. "Glyph.graphicType must not be longer than 4 characters."
  45. )
  46. elif len(self.graphicType) < 4:
  47. # pad with spaces
  48. self.graphicType += " "[: (4 - len(self.graphicType))]
  49. def decompile(self, ttFont):
  50. self.glyphName = ttFont.getGlyphName(self.gid)
  51. if self.rawdata is None:
  52. from fontTools import ttLib
  53. raise ttLib.TTLibError("No table data to decompile")
  54. if len(self.rawdata) > 0:
  55. if len(self.rawdata) < sbixGlyphHeaderFormatSize:
  56. from fontTools import ttLib
  57. # print "Glyph %i header too short: Expected %x, got %x." % (self.gid, sbixGlyphHeaderFormatSize, len(self.rawdata))
  58. raise ttLib.TTLibError("Glyph header too short.")
  59. sstruct.unpack(
  60. sbixGlyphHeaderFormat, self.rawdata[:sbixGlyphHeaderFormatSize], self
  61. )
  62. if self.graphicType == "dupe":
  63. # this glyph is a reference to another glyph's image data
  64. (gid,) = struct.unpack(">H", self.rawdata[sbixGlyphHeaderFormatSize:])
  65. self.referenceGlyphName = ttFont.getGlyphName(gid)
  66. else:
  67. self.imageData = self.rawdata[sbixGlyphHeaderFormatSize:]
  68. self.referenceGlyphName = None
  69. # clean up
  70. del self.rawdata
  71. del self.gid
  72. def compile(self, ttFont):
  73. if self.glyphName is None:
  74. from fontTools import ttLib
  75. raise ttLib.TTLibError("Can't compile Glyph without glyph name")
  76. # TODO: if ttFont has no maxp, cmap etc., ignore glyph names and compile by index?
  77. # (needed if you just want to compile the sbix table on its own)
  78. self.gid = struct.pack(">H", ttFont.getGlyphID(self.glyphName))
  79. if self.graphicType is None:
  80. rawdata = b""
  81. else:
  82. rawdata = sstruct.pack(sbixGlyphHeaderFormat, self)
  83. if self.graphicType == "dupe":
  84. rawdata += struct.pack(">H", ttFont.getGlyphID(self.referenceGlyphName))
  85. else:
  86. assert self.imageData is not None
  87. rawdata += self.imageData
  88. self.rawdata = rawdata
  89. def toXML(self, xmlWriter, ttFont):
  90. if self.graphicType is None:
  91. # TODO: ignore empty glyphs?
  92. # a glyph data entry is required for each glyph,
  93. # but empty ones can be calculated at compile time
  94. xmlWriter.simpletag("glyph", name=self.glyphName)
  95. xmlWriter.newline()
  96. return
  97. xmlWriter.begintag(
  98. "glyph",
  99. graphicType=self.graphicType,
  100. name=self.glyphName,
  101. originOffsetX=self.originOffsetX,
  102. originOffsetY=self.originOffsetY,
  103. )
  104. xmlWriter.newline()
  105. if self.graphicType == "dupe":
  106. # graphicType == "dupe" is a reference to another glyph id.
  107. xmlWriter.simpletag("ref", glyphname=self.referenceGlyphName)
  108. else:
  109. xmlWriter.begintag("hexdata")
  110. xmlWriter.newline()
  111. xmlWriter.dumphex(self.imageData)
  112. xmlWriter.endtag("hexdata")
  113. xmlWriter.newline()
  114. xmlWriter.endtag("glyph")
  115. xmlWriter.newline()
  116. def fromXML(self, name, attrs, content, ttFont):
  117. if name == "ref":
  118. # glyph is a "dupe", i.e. a reference to another glyph's image data.
  119. # in this case imageData contains the glyph id of the reference glyph
  120. # get glyph id from glyphname
  121. glyphname = safeEval("'''" + attrs["glyphname"] + "'''")
  122. self.imageData = struct.pack(">H", ttFont.getGlyphID(glyphname))
  123. self.referenceGlyphName = glyphname
  124. elif name == "hexdata":
  125. self.imageData = readHex(content)
  126. else:
  127. from fontTools import ttLib
  128. raise ttLib.TTLibError("can't handle '%s' element" % name)