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. |
Behdad Esfahbod | 8413c10 | 2013-09-17 16:59:39 -0400 | [diff] [blame] | 3 | from fontTools.misc import sstruct |
Matt Fontaine | c33b0a2 | 2013-08-19 14:13:05 -0400 | [diff] [blame] | 4 | from fontTools.misc.textTools import safeEval |
| 5 | |
| 6 | |
| 7 | bigGlyphMetricsFormat = """ |
| 8 | > # big endian |
| 9 | height: B |
| 10 | width: B |
| 11 | horiBearingX: b |
| 12 | horiBearingY: b |
| 13 | horiAdvance: B |
| 14 | vertBearingX: b |
| 15 | vertBearingY: b |
| 16 | vertAdvance: B |
| 17 | """ |
| 18 | |
| 19 | smallGlyphMetricsFormat = """ |
| 20 | > # big endian |
| 21 | height: B |
| 22 | width: B |
| 23 | BearingX: b |
| 24 | BearingY: b |
| 25 | Advance: B |
| 26 | """ |
| 27 | |
| 28 | class BitmapGlyphMetrics: |
| 29 | |
| 30 | def toXML(self, writer, ttFont): |
| 31 | writer.begintag(self.__class__.__name__) |
| 32 | writer.newline() |
| 33 | for metricName in sstruct.getformat(self.__class__.binaryFormat)[1]: |
| 34 | writer.simpletag(metricName, value=getattr(self, metricName)) |
| 35 | writer.newline() |
| 36 | writer.endtag(self.__class__.__name__) |
| 37 | writer.newline() |
| 38 | |
Behdad Esfahbod | 3a9fd30 | 2013-11-27 03:19:32 -0500 | [diff] [blame] | 39 | def fromXML(self, name, attrs, content, ttFont): |
Matt Fontaine | c33b0a2 | 2013-08-19 14:13:05 -0400 | [diff] [blame] | 40 | metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1]) |
| 41 | for element in content: |
Behdad Esfahbod | b774f9f | 2013-11-27 05:17:37 -0500 | [diff] [blame^] | 42 | if not isinstance(element, tuple): |
Matt Fontaine | c33b0a2 | 2013-08-19 14:13:05 -0400 | [diff] [blame] | 43 | continue |
| 44 | name, attrs, content = element |
| 45 | # Make sure this is a metric that is needed by GlyphMetrics. |
| 46 | if name in metricNames: |
| 47 | vars(self)[name] = safeEval(attrs['value']) |
| 48 | else: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 49 | print("Warning: unknown name '%s' being ignored in %s." % name, self.__class__.__name__) |
Matt Fontaine | c33b0a2 | 2013-08-19 14:13:05 -0400 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class BigGlyphMetrics(BitmapGlyphMetrics): |
| 53 | binaryFormat = bigGlyphMetricsFormat |
| 54 | |
| 55 | class SmallGlyphMetrics(BitmapGlyphMetrics): |
| 56 | binaryFormat = smallGlyphMetricsFormat |