blob: 9cf466797c3377b86a07fc82d683711cdebdce39 [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.
3import sstruct
4from types import TupleType
5from fontTools.misc.textTools import safeEval
6
7
8bigGlyphMetricsFormat = """
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
20smallGlyphMetricsFormat = """
21 > # big endian
22 height: B
23 width: B
24 BearingX: b
25 BearingY: b
26 Advance: B
27"""
28
29class 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
53class BigGlyphMetrics(BitmapGlyphMetrics):
54 binaryFormat = bigGlyphMetricsFormat
55
56class SmallGlyphMetrics(BitmapGlyphMetrics):
57 binaryFormat = smallGlyphMetricsFormat