1# Since bitmap glyph metrics are shared between EBLC and EBDT
2# this class gets its own python file.
3from __future__ import print_function, division, absolute_import
4from fontTools.misc.py23 import *
5from fontTools.misc import sstruct
6from 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(object):
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
41	def fromXML(self, name, attrs, content, ttFont):
42		metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1])
43		for element in content:
44			if not isinstance(element, tuple):
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:
51				print("Warning: unknown name '%s' being ignored in %s." % name, self.__class__.__name__)
52
53
54class BigGlyphMetrics(BitmapGlyphMetrics):
55	binaryFormat = bigGlyphMetricsFormat
56
57class SmallGlyphMetrics(BitmapGlyphMetrics):
58	binaryFormat = smallGlyphMetricsFormat
59