xref: /aosp_15_r20/external/fonttools/Lib/fontTools/ttLib/tables/BitmapGlyphMetrics.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1# Since bitmap glyph metrics are shared between EBLC and EBDT
2# this class gets its own python file.
3from fontTools.misc import sstruct
4from fontTools.misc.textTools import safeEval
5import logging
6
7
8log = logging.getLogger(__name__)
9
10bigGlyphMetricsFormat = """
11  > # big endian
12  height:       B
13  width:        B
14  horiBearingX: b
15  horiBearingY: b
16  horiAdvance:  B
17  vertBearingX: b
18  vertBearingY: b
19  vertAdvance:  B
20"""
21
22smallGlyphMetricsFormat = """
23  > # big endian
24  height:   B
25  width:    B
26  BearingX: b
27  BearingY: b
28  Advance:  B
29"""
30
31
32class BitmapGlyphMetrics(object):
33    def toXML(self, writer, ttFont):
34        writer.begintag(self.__class__.__name__)
35        writer.newline()
36        for metricName in sstruct.getformat(self.__class__.binaryFormat)[1]:
37            writer.simpletag(metricName, value=getattr(self, metricName))
38            writer.newline()
39        writer.endtag(self.__class__.__name__)
40        writer.newline()
41
42    def fromXML(self, name, attrs, content, ttFont):
43        metricNames = set(sstruct.getformat(self.__class__.binaryFormat)[1])
44        for element in content:
45            if not isinstance(element, tuple):
46                continue
47            name, attrs, content = element
48            # Make sure this is a metric that is needed by GlyphMetrics.
49            if name in metricNames:
50                vars(self)[name] = safeEval(attrs["value"])
51            else:
52                log.warning(
53                    "unknown name '%s' being ignored in %s.",
54                    name,
55                    self.__class__.__name__,
56                )
57
58
59class BigGlyphMetrics(BitmapGlyphMetrics):
60    binaryFormat = bigGlyphMetricsFormat
61
62
63class SmallGlyphMetrics(BitmapGlyphMetrics):
64    binaryFormat = smallGlyphMetricsFormat
65