From a1dfa2b77a0ac7cf2e7cdca25e024e31de436b1a Mon Sep 17 00:00:00 2001 From: jvr Date: Thu, 12 Sep 2002 20:51:09 +0000 Subject: [PATCH] added manual implementation of ClassDef to get nicer XML output as well as to get rid of GlyphID dependencies git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@353 4cde692c-a291-49d1-8350-778aa11640f8 --- Lib/fontTools/ttLib/tables/otTables.py | 69 ++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/otTables.py b/Lib/fontTools/ttLib/tables/otTables.py index 3b8aac933..7b574eec1 100644 --- a/Lib/fontTools/ttLib/tables/otTables.py +++ b/Lib/fontTools/ttLib/tables/otTables.py @@ -54,9 +54,7 @@ class Coverage(FormatSwitchingBaseTable): last = glyphIDs[0] ranges = [[last]] for glyphID in glyphIDs[1:]: - if glyphID == last + 1: - pass - else: + if glyphID != last + 1: ranges[-1].append(last) ranges.append([glyphID]) last = glyphID @@ -164,6 +162,71 @@ class SingleSubst(FormatSwitchingBaseTable): mapping[attrs["in"]] = attrs["out"] +class ClassDef(FormatSwitchingBaseTable): + + def postRead(self, rawTable, font): + classDefs = {} + if self.Format == 1: + start = rawTable["StartGlyph"] + glyphID = font.getGlyphID(start) + for cls in rawTable["ClassValueArray"]: + classDefs[cls] = font.getGlyphName(glyphID) + glyphID = glyphID + 1 + elif self.Format == 2: + records = rawTable["ClassRangeRecord"] + for rec in records: + start = rec.Start + end = rec.End + cls = rec.Class + classDefs[start] = cls + for glyphID in range(font.getGlyphID(start) + 1, + font.getGlyphID(end)): + classDefs[font.getGlyphName(glyphID)] = cls + classDefs[end] = cls + else: + assert 0, "unknown format: %s" % self.Format + self.classDefs = classDefs + + def preWrite(self, font): + items = self.classDefs.items() + for i in range(len(items)): + glyphName, cls = items[i] + items[i] = font.getGlyphID(glyphName), glyphName, cls + items.sort() + last, lastName, lastCls = items[0] + rec = ClassRangeRecord() + rec.Start = lastName + rec.Class = lastCls + ranges = [rec] + for glyphID, glyphName, cls in items[1:]: + if glyphID != last + 1 or cls != lastCls: + rec.End = lastName + rec = ClassRangeRecord() + rec.Start = glyphName + rec.Class = cls + ranges.append(rec) + last = glyphID + lastName = glyphName + lastCls = cls + rec.End = lastName + self.Format = 2 # currently no support for Format 1 + return {"ClassRangeRecord": ranges} + + def toXML2(self, xmlWriter, font): + items = self.classDefs.items() + items.sort() + for glyphName, cls in items: + xmlWriter.simpletag("ClassDef", [("glyph", glyphName), ("class", cls)]) + xmlWriter.newline() + + def fromXML(self, (name, attrs, content), font): + classDefs = getattr(self, "classDefs", None) + if classDefs is None: + classDefs = {} + self.classDefs = classDefs + classDefs[attrs["glyph"]] = int(attrs["class"]) + + # # For each subtable format there is a class. However, we don't really distinguish # between "field name" and "format name": often these are the same. Yet there's