• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.misc.textTools import safeEval
4from . import DefaultTable
5import operator
6import struct
7
8
9class table_V_O_R_G_(DefaultTable.DefaultTable):
10
11	""" This table is structured so that you can treat it like a dictionary keyed by glyph name.
12	ttFont['VORG'][<glyphName>] will return the vertical origin for any glyph
13	ttFont['VORG'][<glyphName>] = <value> will set the vertical origin for any glyph.
14	"""
15
16	def decompile(self, data, ttFont):
17		self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
18		self.majorVersion, self.minorVersion, self.defaultVertOriginY, self.numVertOriginYMetrics = struct.unpack(">HHhH", data[:8])
19		assert (self.majorVersion <= 1), "Major version of VORG table is higher than I know how to handle"
20		data = data[8:]
21		vids = []
22		gids = []
23		pos = 0
24		for i in range(self.numVertOriginYMetrics):
25			gid, vOrigin = struct.unpack(">Hh", data[pos:pos+4])
26			pos += 4
27			gids.append(gid)
28			vids.append(vOrigin)
29
30		self.VOriginRecords = vOrig = {}
31		glyphOrder = ttFont.getGlyphOrder()
32		try:
33			names = [glyphOrder[gid] for gid in gids]
34		except IndexError:
35			getGlyphName = self.getGlyphName
36			names = map(getGlyphName, gids)
37
38		for name, vid in zip(names, vids):
39			vOrig[name] = vid
40
41	def compile(self, ttFont):
42		vorgs = list(self.VOriginRecords.values())
43		names = list(self.VOriginRecords.keys())
44		nameMap = ttFont.getReverseGlyphMap()
45		try:
46			gids = [nameMap[name] for name in names]
47		except KeyError:
48			nameMap = ttFont.getReverseGlyphMap(rebuild=True)
49			gids = [nameMap[name] for name in names]
50		vOriginTable = list(zip(gids, vorgs))
51		self.numVertOriginYMetrics = len(vorgs)
52		vOriginTable.sort() # must be in ascending GID order
53		dataList = [struct.pack(">Hh", rec[0], rec[1]) for rec in vOriginTable]
54		header = struct.pack(">HHhH", self.majorVersion, self.minorVersion, self.defaultVertOriginY, self.numVertOriginYMetrics)
55		dataList.insert(0, header)
56		data = bytesjoin(dataList)
57		return data
58
59	def toXML(self, writer, ttFont):
60		writer.simpletag("majorVersion", value=self.majorVersion)
61		writer.newline()
62		writer.simpletag("minorVersion", value=self.minorVersion)
63		writer.newline()
64		writer.simpletag("defaultVertOriginY", value=self.defaultVertOriginY)
65		writer.newline()
66		writer.simpletag("numVertOriginYMetrics", value=self.numVertOriginYMetrics)
67		writer.newline()
68		vOriginTable = []
69		glyphNames = self.VOriginRecords.keys()
70		for glyphName in glyphNames:
71			try:
72				gid = ttFont.getGlyphID(glyphName)
73			except:
74				assert 0, "VORG table contains a glyph name not in ttFont.getGlyphNames(): " + str(glyphName)
75			vOriginTable.append([gid, glyphName, self.VOriginRecords[glyphName]])
76		vOriginTable.sort()
77		for entry in vOriginTable:
78			vOriginRec = VOriginRecord(entry[1], entry[2])
79			vOriginRec.toXML(writer, ttFont)
80
81	def fromXML(self, name, attrs, content, ttFont):
82		if not hasattr(self, "VOriginRecords"):
83			self.VOriginRecords = {}
84		self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
85		if name == "VOriginRecord":
86			vOriginRec = VOriginRecord()
87			for element in content:
88				if isinstance(element, basestring):
89					continue
90				name, attrs, content = element
91				vOriginRec.fromXML(name, attrs, content, ttFont)
92			self.VOriginRecords[vOriginRec.glyphName] = vOriginRec.vOrigin
93		elif "value" in attrs:
94			setattr(self, name, safeEval(attrs["value"]))
95
96	def __getitem__(self, glyphSelector):
97		if isinstance(glyphSelector, int):
98			# its a gid, convert to glyph name
99			glyphSelector = self.getGlyphName(glyphSelector)
100
101		if glyphSelector not in self.VOriginRecords:
102			return self.defaultVertOriginY
103
104		return self.VOriginRecords[glyphSelector]
105
106	def __setitem__(self, glyphSelector, value):
107		if isinstance(glyphSelector, int):
108			# its a gid, convert to glyph name
109			glyphSelector = self.getGlyphName(glyphSelector)
110
111		if  value != self.defaultVertOriginY:
112			self.VOriginRecords[glyphSelector] = value
113		elif glyphSelector in self.VOriginRecords:
114			del self.VOriginRecords[glyphSelector]
115
116	def __delitem__(self, glyphSelector):
117		del self.VOriginRecords[glyphSelector]
118
119class VOriginRecord(object):
120
121	def __init__(self, name=None, vOrigin=None):
122		self.glyphName = name
123		self.vOrigin = vOrigin
124
125	def toXML(self, writer, ttFont):
126		writer.begintag("VOriginRecord")
127		writer.newline()
128		writer.simpletag("glyphName", value=self.glyphName)
129		writer.newline()
130		writer.simpletag("vOrigin", value=self.vOrigin)
131		writer.newline()
132		writer.endtag("VOriginRecord")
133		writer.newline()
134
135	def fromXML(self, name, attrs, content, ttFont):
136		value = attrs["value"]
137		if name == "glyphName":
138			setattr(self, name, value)
139		else:
140			setattr(self, name, safeEval(value))
141