Matt Fontaine | c33b0a2 | 2013-08-19 14:13:05 -0400 | [diff] [blame] | 1 | # Since bitmap glyph metrics are shared between EBLC and EBDT |
| 2 | # this class gets its own python file. |
| 3 | import sstruct |
| 4 | from types import TupleType |
| 5 | from fontTools.misc.textTools import safeEval |
| 6 | |
| 7 | |
| 8 | bigGlyphMetricsFormat = """ |
| 9 | > # big endian |
| 10 | height: B |
| 11 | width: B |
| 12 | horiBearingX: b |
| 13 | horiBearingY: b |
| 14 | horiAdvance: B |
| 15 | vertBearingX: b |
| 16 | vertBearingY: b |
| 17 | vertAdvance: B |
| 18 | """ |
| 19 | |
| 20 | smallGlyphMetricsFormat = """ |
| 21 | > # big endian |
| 22 | height: B |
| 23 | width: B |
| 24 | BearingX: b |
| 25 | BearingY: b |
| 26 | Advance: B |
| 27 | """ |
| 28 | |
| 29 | class BitmapGlyphMetrics: |
| 30 | |
| 31 | def toXML(self, writer, ttFont): |
| 32 | writer.begintag(self.__class__.__name__) |
| 33 | writer.newline() |
| 34 | for metricName in sstruct.getformat(self.__class__.binaryFormat)[1]: |
| 35 | writer.simpletag(metricName, value=getattr(self, metricName)) |
| 36 | writer.newline() |
| 37 | writer.endtag(self.__class__.__name__) |
| 38 | writer.newline() |
| 39 | |
| 40 | def fromXML(self, (name, attrs, content), ttFont): |
| 41 | metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1]) |
| 42 | for element in content: |
| 43 | if type(element) != TupleType: |
| 44 | continue |
| 45 | name, attrs, content = element |
| 46 | # Make sure this is a metric that is needed by GlyphMetrics. |
| 47 | if name in metricNames: |
| 48 | vars(self)[name] = safeEval(attrs['value']) |
| 49 | else: |
| 50 | print "Warning: unknown name '%s' being ignored in %s." % name, self.__class__.__name__ |
| 51 | |
| 52 | |
| 53 | class BigGlyphMetrics(BitmapGlyphMetrics): |
| 54 | binaryFormat = bigGlyphMetricsFormat |
| 55 | |
| 56 | class SmallGlyphMetrics(BitmapGlyphMetrics): |
| 57 | binaryFormat = smallGlyphMetricsFormat |