MCPcopy Create free account
hub / github.com/coldtype/st2 / Strike

Class Strike

ST2/meshtable.py:147–271  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145
146
147class Strike(object):
148 def __init__(self, rawdata=None, ppem=0, resolution=72):
149 self.data = rawdata
150 self.ppem = ppem
151 self.resolution = resolution
152 self.glyphs = {}
153
154 def decompile(self, ttFont):
155 if self.data is None:
156 from fontTools import ttLib
157 raise ttLib.TTLibError
158 if len(self.data) < meshStrikeHeaderFormatSize:
159 from fontTools import ttLib
160 raise(ttLib.TTLibError, "Strike header too short: Expected %x, got %x.") \
161 % (meshStrikeHeaderFormatSize, len(self.data))
162
163 # read Strike header from raw data
164 sstruct.unpack(meshStrikeHeaderFormat, self.data[:meshStrikeHeaderFormatSize], self)
165
166 # calculate number of glyphs
167 firstGlyphDataOffset, = struct.unpack(">L", \
168 self.data[meshStrikeHeaderFormatSize:meshStrikeHeaderFormatSize + meshGlyphDataOffsetFormatSize])
169 self.numGlyphs = (firstGlyphDataOffset - meshStrikeHeaderFormatSize) // meshGlyphDataOffsetFormatSize - 1
170 # ^ -1 because there's one more offset than glyphs
171
172 # build offset list for single glyph data offsets
173 self.glyphDataOffsets = []
174 for i in range(self.numGlyphs + 1): # + 1 because there's one more offset than glyphs
175 start = i * meshGlyphDataOffsetFormatSize + meshStrikeHeaderFormatSize
176 current_offset, = struct.unpack(">L", self.data[start:start + meshGlyphDataOffsetFormatSize])
177 self.glyphDataOffsets.append(current_offset)
178
179 # iterate through offset list and slice raw data into glyph data records
180 for i in range(self.numGlyphs):
181 current_glyph = Glyph(rawdata=self.data[self.glyphDataOffsets[i]:self.glyphDataOffsets[i+1]], gid=i)
182 current_glyph.decompile(ttFont)
183 self.glyphs[current_glyph.glyphName] = current_glyph
184 del self.glyphDataOffsets
185 del self.numGlyphs
186 del self.data
187
188 def compile(self, ttFont):
189 self.glyphDataOffsets = b""
190 self.bitmapData = b""
191
192 glyphOrder = ttFont.getGlyphOrder()
193
194 # first glyph starts right after the header
195 currentGlyphDataOffset = meshStrikeHeaderFormatSize + meshGlyphDataOffsetFormatSize * (len(glyphOrder) + 1)
196 for glyphName in glyphOrder:
197 if glyphName in self.glyphs:
198 # we have glyph data for this glyph
199 current_glyph = self.glyphs[glyphName]
200 else:
201 # must add empty glyph data record for this glyph
202 current_glyph = Glyph(glyphName=glyphName)
203 current_glyph.compile(ttFont)
204 current_glyph.glyphDataOffset = currentGlyphDataOffset

Callers 2

decompileMethod · 0.85
fromXMLMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected