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