blob: 620d3a1e8b2638a9231796c56083eae83f8f5dc2 [file] [log] [blame]
Matt Fontainec33b0a22013-08-19 14:13:05 -04001# Since bitmap glyph metrics are shared between EBLC and EBDT
2# this class gets its own python file.
Behdad Esfahbod32c10ee2013-11-27 17:46:17 -05003from __future__ import print_function, division
Behdad Esfahbod30e691e2013-11-27 17:27:45 -05004from fontTools.misc.py23 import *
Behdad Esfahbod8413c102013-09-17 16:59:39 -04005from fontTools.misc import sstruct
Matt Fontainec33b0a22013-08-19 14:13:05 -04006from fontTools.misc.textTools import safeEval
7
8
9bigGlyphMetricsFormat = """
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
21smallGlyphMetricsFormat = """
22 > # big endian
23 height: B
24 width: B
25 BearingX: b
26 BearingY: b
27 Advance: B
28"""
29
30class 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 Esfahbod3a9fd302013-11-27 03:19:32 -050041 def fromXML(self, name, attrs, content, ttFont):
Matt Fontainec33b0a22013-08-19 14:13:05 -040042 metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1])
43 for element in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -050044 if not isinstance(element, tuple):
Matt Fontainec33b0a22013-08-19 14:13:05 -040045 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 Esfahbod3ec6a252013-11-27 04:57:33 -050051 print("Warning: unknown name '%s' being ignored in %s." % name, self.__class__.__name__)
Matt Fontainec33b0a22013-08-19 14:13:05 -040052
53
54class BigGlyphMetrics(BitmapGlyphMetrics):
55 binaryFormat = bigGlyphMetricsFormat
56
57class SmallGlyphMetrics(BitmapGlyphMetrics):
58 binaryFormat = smallGlyphMetricsFormat