aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/ttLib/tables/V_D_M_X_.py
blob: abca3bf1d438422adad57f64674d80a83b2f113e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from . import DefaultTable
from fontTools.misc import sstruct
from fontTools.misc.textTools import safeEval
import struct

VDMX_HeaderFmt = """
	>                 # big endian
	version:     H    # Version number (0 or 1)
	numRecs:     H    # Number of VDMX groups present
	numRatios:   H    # Number of aspect ratio groupings
"""
# the VMDX header is followed by an array of RatRange[numRatios] (i.e. aspect
# ratio ranges);
VDMX_RatRangeFmt = """
	>                 # big endian
	bCharSet:    B    # Character set
	xRatio:      B    # Value to use for x-Ratio
	yStartRatio: B    # Starting y-Ratio value
	yEndRatio:   B    # Ending y-Ratio value
"""
# followed by an array of offset[numRatios] from start of VDMX table to the
# VDMX Group for this ratio range (offsets will be re-calculated on compile);
# followed by an array of Group[numRecs] records;
VDMX_GroupFmt = """
	>                 # big endian
	recs:        H    # Number of height records in this group
	startsz:     B    # Starting yPelHeight
	endsz:       B    # Ending yPelHeight
"""
# followed by an array of vTable[recs] records.
VDMX_vTableFmt = """
	>                 # big endian
	yPelHeight:  H    # yPelHeight to which values apply
	yMax:        h    # Maximum value (in pels) for this yPelHeight
	yMin:        h    # Minimum value (in pels) for this yPelHeight
"""


class table_V_D_M_X_(DefaultTable.DefaultTable):

	def decompile(self, data, ttFont):
		pos = 0  # track current position from to start of VDMX table
		dummy, data = sstruct.unpack2(VDMX_HeaderFmt, data, self)
		pos += sstruct.calcsize(VDMX_HeaderFmt)
		self.ratRanges = []
		for i in range(self.numRatios):
			ratio, data = sstruct.unpack2(VDMX_RatRangeFmt, data)
			pos += sstruct.calcsize(VDMX_RatRangeFmt)
			# the mapping between a ratio and a group is defined further below
			ratio['groupIndex'] = None
			self.ratRanges.append(ratio)
		lenOffset = struct.calcsize('>H')
		_offsets = []  # temporarily store offsets to groups
		for i in range(self.numRatios):
			offset = struct.unpack('>H', data[0:lenOffset])[0]
			data = data[lenOffset:]
			pos += lenOffset
			_offsets.append(offset)
		self.groups = []
		for groupIndex in range(self.numRecs):
			# the offset to this group from beginning of the VDMX table
			currOffset = pos
			group, data = sstruct.unpack2(VDMX_GroupFmt, data)
			# the group lenght and bounding sizes are re-calculated on compile
			recs = group.pop('recs')
			startsz = group.pop('startsz')
			endsz = group.pop('endsz')
			pos += sstruct.calcsize(VDMX_GroupFmt)
			for j in range(recs):
				vTable, data = sstruct.unpack2(VDMX_vTableFmt, data)
				vTableLength = sstruct.calcsize(VDMX_vTableFmt)
				pos += vTableLength
				# group is a dict of (yMax, yMin) tuples keyed by yPelHeight
				group[vTable['yPelHeight']] = (vTable['yMax'], vTable['yMin'])
			# make sure startsz and endsz match the calculated values
			minSize = min(group.keys())
			maxSize = max(group.keys())
			assert startsz == minSize, \
				"startsz (%s) must equal min yPelHeight (%s): group %d" % \
				(group.startsz, minSize, groupIndex)
			assert endsz == maxSize, \
				"endsz (%s) must equal max yPelHeight (%s): group %d" % \
				(group.endsz, maxSize, groupIndex)
			self.groups.append(group)
			# match the defined offsets with the current group's offset
			for offsetIndex, offsetValue in enumerate(_offsets):
				# when numRecs < numRatios there can more than one ratio range
				# sharing the same VDMX group
				if currOffset == offsetValue:
					# map the group with the ratio range thas has the same
					# index as the offset to that group (it took me a while..)
					self.ratRanges[offsetIndex]['groupIndex'] = groupIndex
		# check that all ratio ranges have a group
		for i in range(self.numRatios):
			ratio = self.ratRanges[i]
			if ratio['groupIndex'] is None:
				from fontTools import ttLib
				raise ttLib.TTLibError(
					"no group defined for ratRange %d" % i)

	def _getOffsets(self):
		"""
		Calculate offsets to VDMX_Group records.
		For each ratRange return a list of offset values from the beginning of
		the VDMX table to a VDMX_Group.
		"""
		lenHeader = sstruct.calcsize(VDMX_HeaderFmt)
		lenRatRange = sstruct.calcsize(VDMX_RatRangeFmt)
		lenOffset = struct.calcsize('>H')
		lenGroupHeader = sstruct.calcsize(VDMX_GroupFmt)
		lenVTable = sstruct.calcsize(VDMX_vTableFmt)
		# offset to the first group
		pos = lenHeader + self.numRatios*lenRatRange + self.numRatios*lenOffset
		groupOffsets = []
		for group in self.groups:
			groupOffsets.append(pos)
			lenGroup = lenGroupHeader + len(group) * lenVTable
			pos += lenGroup  # offset to next group
		offsets = []
		for ratio in self.ratRanges:
			groupIndex = ratio['groupIndex']
			offsets.append(groupOffsets[groupIndex])
		return offsets

	def compile(self, ttFont):
		if not(self.version == 0 or self.version == 1):
			from fontTools import ttLib
			raise ttLib.TTLibError(
				"unknown format for VDMX table: version %s" % self.version)
		data = sstruct.pack(VDMX_HeaderFmt, self)
		for ratio in self.ratRanges:
			data += sstruct.pack(VDMX_RatRangeFmt, ratio)
		# recalculate offsets to VDMX groups
		for offset in self._getOffsets():
			data += struct.pack('>H', offset)
		for group in self.groups:
			recs = len(group)
			startsz = min(group.keys())
			endsz = max(group.keys())
			gHeader = {'recs': recs, 'startsz': startsz, 'endsz': endsz}
			data += sstruct.pack(VDMX_GroupFmt, gHeader)
			for yPelHeight, (yMax, yMin) in sorted(group.items()):
				vTable = {'yPelHeight': yPelHeight, 'yMax': yMax, 'yMin': yMin}
				data += sstruct.pack(VDMX_vTableFmt, vTable)
		return data

	def toXML(self, writer, ttFont):
		writer.simpletag("version", value=self.version)
		writer.newline()
		writer.begintag("ratRanges")
		writer.newline()
		for ratio in self.ratRanges:
			groupIndex = ratio['groupIndex']
			writer.simpletag(
				"ratRange",
				bCharSet=ratio['bCharSet'],
				xRatio=ratio['xRatio'],
				yStartRatio=ratio['yStartRatio'],
				yEndRatio=ratio['yEndRatio'],
				groupIndex=groupIndex
				)
			writer.newline()
		writer.endtag("ratRanges")
		writer.newline()
		writer.begintag("groups")
		writer.newline()
		for groupIndex in range(self.numRecs):
			group = self.groups[groupIndex]
			recs = len(group)
			startsz = min(group.keys())
			endsz = max(group.keys())
			writer.begintag("group", index=groupIndex)
			writer.newline()
			writer.comment("recs=%d, startsz=%d, endsz=%d" %
							(recs, startsz, endsz))
			writer.newline()
			for yPelHeight, (yMax, yMin) in sorted(group.items()):
				writer.simpletag(
					"record",
					[('yPelHeight', yPelHeight), ('yMax', yMax), ('yMin', yMin)])
				writer.newline()
			writer.endtag("group")
			writer.newline()
		writer.endtag("groups")
		writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		if name == "version":
			self.version = safeEval(attrs["value"])
		elif name == "ratRanges":
			if not hasattr(self, "ratRanges"):
				self.ratRanges = []
			for element in content:
				if not isinstance(element, tuple):
					continue
				name, attrs, content = element
				if name == "ratRange":
					if not hasattr(self, "numRatios"):
						self.numRatios = 1
					else:
						self.numRatios += 1
					ratio = {
						"bCharSet": safeEval(attrs["bCharSet"]),
						"xRatio": safeEval(attrs["xRatio"]),
						"yStartRatio": safeEval(attrs["yStartRatio"]),
						"yEndRatio": safeEval(attrs["yEndRatio"]),
						"groupIndex": safeEval(attrs["groupIndex"])
						}
					self.ratRanges.append(ratio)
		elif name == "groups":
			if not hasattr(self, "groups"):
				self.groups = []
			for element in content:
				if not isinstance(element, tuple):
					continue
				name, attrs, content = element
				if name == "group":
					if not hasattr(self, "numRecs"):
						self.numRecs = 1
					else:
						self.numRecs += 1
					group = {}
					for element in content:
						if not isinstance(element, tuple):
							continue
						name, attrs, content = element
						if name == "record":
							yPelHeight = safeEval(attrs["yPelHeight"])
							yMax = safeEval(attrs["yMax"])
							yMin = safeEval(attrs["yMin"])
							group[yPelHeight] = (yMax, yMin)
					self.groups.append(group)