blob: 57d896ed14648bf8437a1a886bae62560410311e [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 Esfahbod8413c102013-09-17 16:59:39 -04003from fontTools.misc import sstruct
Matt Fontainec33b0a22013-08-19 14:13:05 -04004from fontTools.misc.textTools import safeEval
5
6
7bigGlyphMetricsFormat = """
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
19smallGlyphMetricsFormat = """
20 > # big endian
21 height: B
22 width: B
23 BearingX: b
24 BearingY: b
25 Advance: B
26"""
27
28class 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 Esfahbod3a9fd302013-11-27 03:19:32 -050039 def fromXML(self, name, attrs, content, ttFont):
Matt Fontainec33b0a22013-08-19 14:13:05 -040040 metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1])
41 for element in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -050042 if not isinstance(element, tuple):
Matt Fontainec33b0a22013-08-19 14:13:05 -040043 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 Esfahbod3ec6a252013-11-27 04:57:33 -050049 print("Warning: unknown name '%s' being ignored in %s." % name, self.__class__.__name__)
Matt Fontainec33b0a22013-08-19 14:13:05 -040050
51
52class BigGlyphMetrics(BitmapGlyphMetrics):
53 binaryFormat = bigGlyphMetricsFormat
54
55class SmallGlyphMetrics(BitmapGlyphMetrics):
56 binaryFormat = smallGlyphMetricsFormat