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 31class BitmapGlyphMetrics(object): 32 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("unknown name '%s' being ignored in %s.", name, self.__class__.__name__) 53 54 55class BigGlyphMetrics(BitmapGlyphMetrics): 56 binaryFormat = bigGlyphMetricsFormat 57 58class SmallGlyphMetrics(BitmapGlyphMetrics): 59 binaryFormat = smallGlyphMetricsFormat 60