Home | History | Annotate | Download | only in tables
      1 from __future__ import print_function, division, absolute_import
      2 from fontTools.misc.py23 import *
      3 from fontTools.misc import sstruct
      4 from fontTools.misc.textTools import safeEval
      5 from . import DefaultTable
      6 
      7 vheaFormat = """
      8 		>	# big endian
      9 		tableVersion:			16.16F
     10 		ascent:					h
     11 		descent:				h
     12 		lineGap:				h
     13 		advanceHeightMax:		H
     14 		minTopSideBearing:		h
     15 		minBottomSideBearing:	h
     16 		yMaxExtent:				h
     17 		caretSlopeRise:			h
     18 		caretSlopeRun:			h
     19 		reserved0:				h
     20 		reserved1:				h
     21 		reserved2:				h
     22 		reserved3:				h
     23 		reserved4:				h
     24 		metricDataFormat:		h
     25 		numberOfVMetrics:		H
     26 """
     27 
     28 class table__v_h_e_a(DefaultTable.DefaultTable):
     29 	
     30 	dependencies = ['vmtx', 'glyf']
     31 	
     32 	def decompile(self, data, ttFont):
     33 		sstruct.unpack(vheaFormat, data, self)
     34 	
     35 	def compile(self, ttFont):
     36 		self.recalc(ttFont)
     37 		return sstruct.pack(vheaFormat, self)
     38 	
     39 	def recalc(self, ttFont):
     40 		vtmxTable = ttFont['vmtx']
     41 		if 'glyf' in ttFont:
     42 			if not ttFont.isLoaded('glyf'):
     43 				return
     44 			glyfTable = ttFont['glyf']
     45 			advanceHeightMax = -100000    # arbitrary big negative number
     46 			minTopSideBearing = 100000    # arbitrary big number
     47 			minBottomSideBearing = 100000 # arbitrary big number
     48 			yMaxExtent = -100000          # arbitrary big negative number
     49 			
     50 			for name in ttFont.getGlyphOrder():
     51 				height, tsb = vtmxTable[name]
     52 				g = glyfTable[name]
     53 				if g.numberOfContours <= 0:
     54 					continue
     55 				advanceHeightMax = max(advanceHeightMax, height)
     56 				minTopSideBearing = min(minTopSideBearing, tsb)
     57 				rsb = height - tsb - (g.yMax - g.yMin)
     58 				minBottomSideBearing = min(minBottomSideBearing, rsb)
     59 				extent = tsb + (g.yMax - g.yMin)
     60 				yMaxExtent = max(yMaxExtent, extent)
     61 			self.advanceHeightMax = advanceHeightMax
     62 			self.minTopSideBearing = minTopSideBearing
     63 			self.minBottomSideBearing = minBottomSideBearing
     64 			self.yMaxExtent = yMaxExtent
     65 		else:
     66 			# XXX CFF recalc...
     67 			pass
     68 	
     69 	def toXML(self, writer, ttFont):
     70 		formatstring, names, fixes = sstruct.getformat(vheaFormat)
     71 		for name in names:
     72 			value = getattr(self, name)
     73 			writer.simpletag(name, value=value)
     74 			writer.newline()
     75 	
     76 	def fromXML(self, name, attrs, content, ttFont):
     77 		setattr(self, name, safeEval(attrs["value"]))
     78 
     79