sbixStrike.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from fontTools.misc import sstruct
  2. from fontTools.misc.textTools import safeEval
  3. from .sbixGlyph import Glyph
  4. import struct
  5. sbixStrikeHeaderFormat = """
  6. >
  7. ppem: H # The PPEM for which this strike was designed (e.g., 9,
  8. # 12, 24)
  9. resolution: H # The screen resolution (in dpi) for which this strike
  10. # was designed (e.g., 72)
  11. """
  12. sbixGlyphDataOffsetFormat = """
  13. >
  14. glyphDataOffset: L # Offset from the beginning of the strike data record
  15. # to data for the individual glyph
  16. """
  17. sbixStrikeHeaderFormatSize = sstruct.calcsize(sbixStrikeHeaderFormat)
  18. sbixGlyphDataOffsetFormatSize = sstruct.calcsize(sbixGlyphDataOffsetFormat)
  19. class Strike(object):
  20. def __init__(self, rawdata=None, ppem=0, resolution=72):
  21. self.data = rawdata
  22. self.ppem = ppem
  23. self.resolution = resolution
  24. self.glyphs = {}
  25. def decompile(self, ttFont):
  26. if self.data is None:
  27. from fontTools import ttLib
  28. raise ttLib.TTLibError
  29. if len(self.data) < sbixStrikeHeaderFormatSize:
  30. from fontTools import ttLib
  31. raise (
  32. ttLib.TTLibError,
  33. "Strike header too short: Expected %x, got %x.",
  34. ) % (sbixStrikeHeaderFormatSize, len(self.data))
  35. # read Strike header from raw data
  36. sstruct.unpack(
  37. sbixStrikeHeaderFormat, self.data[:sbixStrikeHeaderFormatSize], self
  38. )
  39. # calculate number of glyphs
  40. (firstGlyphDataOffset,) = struct.unpack(
  41. ">L",
  42. self.data[
  43. sbixStrikeHeaderFormatSize : sbixStrikeHeaderFormatSize
  44. + sbixGlyphDataOffsetFormatSize
  45. ],
  46. )
  47. self.numGlyphs = (
  48. firstGlyphDataOffset - sbixStrikeHeaderFormatSize
  49. ) // sbixGlyphDataOffsetFormatSize - 1
  50. # ^ -1 because there's one more offset than glyphs
  51. # build offset list for single glyph data offsets
  52. self.glyphDataOffsets = []
  53. for i in range(
  54. self.numGlyphs + 1
  55. ): # + 1 because there's one more offset than glyphs
  56. start = i * sbixGlyphDataOffsetFormatSize + sbixStrikeHeaderFormatSize
  57. (current_offset,) = struct.unpack(
  58. ">L", self.data[start : start + sbixGlyphDataOffsetFormatSize]
  59. )
  60. self.glyphDataOffsets.append(current_offset)
  61. # iterate through offset list and slice raw data into glyph data records
  62. for i in range(self.numGlyphs):
  63. current_glyph = Glyph(
  64. rawdata=self.data[
  65. self.glyphDataOffsets[i] : self.glyphDataOffsets[i + 1]
  66. ],
  67. gid=i,
  68. )
  69. current_glyph.decompile(ttFont)
  70. self.glyphs[current_glyph.glyphName] = current_glyph
  71. del self.glyphDataOffsets
  72. del self.numGlyphs
  73. del self.data
  74. def compile(self, ttFont):
  75. self.glyphDataOffsets = b""
  76. self.bitmapData = b""
  77. glyphOrder = ttFont.getGlyphOrder()
  78. # first glyph starts right after the header
  79. currentGlyphDataOffset = (
  80. sbixStrikeHeaderFormatSize
  81. + sbixGlyphDataOffsetFormatSize * (len(glyphOrder) + 1)
  82. )
  83. for glyphName in glyphOrder:
  84. if glyphName in self.glyphs:
  85. # we have glyph data for this glyph
  86. current_glyph = self.glyphs[glyphName]
  87. else:
  88. # must add empty glyph data record for this glyph
  89. current_glyph = Glyph(glyphName=glyphName)
  90. current_glyph.compile(ttFont)
  91. current_glyph.glyphDataOffset = currentGlyphDataOffset
  92. self.bitmapData += current_glyph.rawdata
  93. currentGlyphDataOffset += len(current_glyph.rawdata)
  94. self.glyphDataOffsets += sstruct.pack(
  95. sbixGlyphDataOffsetFormat, current_glyph
  96. )
  97. # add last "offset", really the end address of the last glyph data record
  98. dummy = Glyph()
  99. dummy.glyphDataOffset = currentGlyphDataOffset
  100. self.glyphDataOffsets += sstruct.pack(sbixGlyphDataOffsetFormat, dummy)
  101. # pack header
  102. self.data = sstruct.pack(sbixStrikeHeaderFormat, self)
  103. # add offsets and image data after header
  104. self.data += self.glyphDataOffsets + self.bitmapData
  105. def toXML(self, xmlWriter, ttFont):
  106. xmlWriter.begintag("strike")
  107. xmlWriter.newline()
  108. xmlWriter.simpletag("ppem", value=self.ppem)
  109. xmlWriter.newline()
  110. xmlWriter.simpletag("resolution", value=self.resolution)
  111. xmlWriter.newline()
  112. glyphOrder = ttFont.getGlyphOrder()
  113. for i in range(len(glyphOrder)):
  114. if glyphOrder[i] in self.glyphs:
  115. self.glyphs[glyphOrder[i]].toXML(xmlWriter, ttFont)
  116. # TODO: what if there are more glyph data records than (glyf table) glyphs?
  117. xmlWriter.endtag("strike")
  118. xmlWriter.newline()
  119. def fromXML(self, name, attrs, content, ttFont):
  120. if name in ["ppem", "resolution"]:
  121. setattr(self, name, safeEval(attrs["value"]))
  122. elif name == "glyph":
  123. if "graphicType" in attrs:
  124. myFormat = safeEval("'''" + attrs["graphicType"] + "'''")
  125. else:
  126. myFormat = None
  127. if "glyphname" in attrs:
  128. myGlyphName = safeEval("'''" + attrs["glyphname"] + "'''")
  129. elif "name" in attrs:
  130. myGlyphName = safeEval("'''" + attrs["name"] + "'''")
  131. else:
  132. from fontTools import ttLib
  133. raise ttLib.TTLibError("Glyph must have a glyph name.")
  134. if "originOffsetX" in attrs:
  135. myOffsetX = safeEval(attrs["originOffsetX"])
  136. else:
  137. myOffsetX = 0
  138. if "originOffsetY" in attrs:
  139. myOffsetY = safeEval(attrs["originOffsetY"])
  140. else:
  141. myOffsetY = 0
  142. current_glyph = Glyph(
  143. glyphName=myGlyphName,
  144. graphicType=myFormat,
  145. originOffsetX=myOffsetX,
  146. originOffsetY=myOffsetY,
  147. )
  148. for element in content:
  149. if isinstance(element, tuple):
  150. name, attrs, content = element
  151. current_glyph.fromXML(name, attrs, content, ttFont)
  152. current_glyph.compile(ttFont)
  153. self.glyphs[current_glyph.glyphName] = current_glyph
  154. else:
  155. from fontTools import ttLib
  156. raise ttLib.TTLibError("can't handle '%s' element" % name)