added manual implementation for the Coverage subtable to get rid of GlyphID dependencies
git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@341 4cde692c-a291-49d1-8350-778aa11640f8
This commit is contained in:
parent
52966bb144
commit
d41386e7f7
@ -11,8 +11,85 @@ from otBase import BaseTable, FormatSwitchingBaseTable
|
||||
class LookupOrder(BaseTable):
|
||||
"""Dummy class; this table isn't defined, but is used, and is always NULL."""
|
||||
|
||||
|
||||
class FeatureParams(BaseTable):
|
||||
"""Dummy class; this table isn't defined, but is used, and is always NULL."""
|
||||
# XXX The above is no longer true; the 'size' feature uses FeatureParams now.
|
||||
|
||||
|
||||
class Coverage(FormatSwitchingBaseTable):
|
||||
|
||||
# manual implementation to get rid of glyphID dependencies
|
||||
|
||||
def postRead(self, table, font):
|
||||
if self.Format == 1:
|
||||
self.glyphs = table["GlyphArray"]
|
||||
elif self.Format == 2:
|
||||
glyphs = self.glyphs = []
|
||||
ranges = table["RangeRecord"]
|
||||
for r in ranges:
|
||||
assert r.StartCoverageIndex == len(glyphs), \
|
||||
(r.StartCoverageIndex, len(glyphs))
|
||||
start = r.Start
|
||||
end = r.End
|
||||
startID = font.getGlyphID(start)
|
||||
endID = font.getGlyphID(end)
|
||||
glyphs.append(start)
|
||||
for glyphID in range(startID + 1, endID):
|
||||
glyphs.append(font.getGlyphName(glyphID))
|
||||
if start != end:
|
||||
glyphs.append(end)
|
||||
else:
|
||||
assert 0
|
||||
|
||||
def preWrite(self, font):
|
||||
format = 1
|
||||
table = {"GlyphArray": self.glyphs}
|
||||
if self.glyphs:
|
||||
glyphIDs = []
|
||||
for glyphName in self.glyphs:
|
||||
glyphIDs.append(font.getGlyphID(glyphName))
|
||||
|
||||
last = glyphIDs[0]
|
||||
ranges = [[last]]
|
||||
for glyphID in glyphIDs[1:]:
|
||||
if glyphID == last + 1:
|
||||
pass
|
||||
else:
|
||||
ranges[-1].append(last)
|
||||
ranges.append([glyphID])
|
||||
last = glyphID
|
||||
ranges[-1].append(last)
|
||||
|
||||
if len(ranges) * 3 < len(self.glyphs): # 3 words vs. 1 word
|
||||
# Format 2 is more compact
|
||||
index = 0
|
||||
for i in range(len(ranges)):
|
||||
start, end = ranges[i]
|
||||
r = RangeRecord()
|
||||
r.Start = font.getGlyphName(start)
|
||||
r.End = font.getGlyphName(end)
|
||||
r.StartCoverageIndex = index
|
||||
ranges[i] = r
|
||||
index = index + end - start + 1
|
||||
format = 2
|
||||
table = {"RangeRecord": ranges}
|
||||
#else:
|
||||
# fallthrough; Format 1 is more compact
|
||||
self.Format = format
|
||||
return table
|
||||
|
||||
def toXML2(self, xmlWriter, font):
|
||||
for glyphName in self.glyphs:
|
||||
xmlWriter.simpletag("Glyph", value=glyphName)
|
||||
xmlWriter.newline()
|
||||
|
||||
def fromXML(self, (name, attrs, content), font):
|
||||
glyphs = getattr(self, "glyphs", None)
|
||||
if glyphs is None:
|
||||
glyphs = []
|
||||
self.glyphs = glyphs
|
||||
glyphs.append(attrs["value"])
|
||||
|
||||
|
||||
#
|
||||
|
Loading…
x
Reference in New Issue
Block a user