blob: 6a7a63b7ac09df33b53444a927a712ec3b020b84 [file] [log] [blame]
Behdad Esfahbod32c10ee2013-11-27 17:46:17 -05001from __future__ import print_function, division
Behdad Esfahbod30e691e2013-11-27 17:27:45 -05002from fontTools.misc.py23 import *
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -05003from .DefaultTable import DefaultTable
jvrd4d15132002-05-11 00:59:27 +00004import struct
jvrd4d15132002-05-11 00:59:27 +00005
Behdad Esfahbode388db52013-11-28 14:26:58 -05006class OverflowErrorRecord(object):
jvr823f8cd2006-10-21 14:12:38 +00007 def __init__(self, overflowTuple):
8 self.tableType = overflowTuple[0]
9 self.LookupListIndex = overflowTuple[1]
10 self.SubTableIndex = overflowTuple[2]
11 self.itemName = overflowTuple[3]
12 self.itemIndex = overflowTuple[4]
13
14 def __repr__(self):
15 return str((self.tableType, "LookupIndex:", self.LookupListIndex, "SubTableIndex:", self.SubTableIndex, "ItemName:", self.itemName, "ItemIndex:", self.itemIndex))
16
17class OTLOffsetOverflowError(Exception):
18 def __init__(self, overflowErrorRecord):
19 self.value = overflowErrorRecord
20
21 def __str__(self):
22 return repr(self.value)
23
jvrd4d15132002-05-11 00:59:27 +000024
25class BaseTTXConverter(DefaultTable):
26
jvr3a6aa232003-09-02 19:23:13 +000027 """Generic base class for TTX table converters. It functions as an
28 adapter between the TTX (ttLib actually) table model and the model
29 we use for OpenType tables, which is necessarily subtly different.
30 """
jvr64b5c802002-05-11 10:21:36 +000031
jvrd4d15132002-05-11 00:59:27 +000032 def decompile(self, data, font):
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -050033 from . import otTables
Behdad Esfahbod0585b642013-11-22 16:20:59 -050034 cachingStats = None if True else {}
Behdad Esfahbode388db52013-11-28 14:26:58 -050035 class GlobalState(object):
Behdad Esfahbod79f73442013-11-26 17:07:37 -050036 def __init__(self, tableType, cachingStats):
37 self.tableType = tableType
38 self.cachingStats = cachingStats
39 globalState = GlobalState(tableType=self.tableTag,
40 cachingStats=cachingStats)
41 reader = OTTableReader(data, globalState)
jvrd4d15132002-05-11 00:59:27 +000042 tableClass = getattr(otTables, self.tableTag)
43 self.table = tableClass()
44 self.table.decompile(reader, font)
Behdad Esfahbod0585b642013-11-22 16:20:59 -050045 if cachingStats:
Behdad Esfahbodac1b4352013-11-27 04:15:34 -050046 stats = sorted([(v, k) for k, v in cachingStats.items()])
jvrcfadfd02002-07-22 22:13:57 +000047 stats.reverse()
Behdad Esfahbod3ec6a252013-11-27 04:57:33 -050048 print("cachingsstats for ", self.tableTag)
jvrcfadfd02002-07-22 22:13:57 +000049 for v, k in stats:
50 if v < 2:
51 break
Behdad Esfahbod3ec6a252013-11-27 04:57:33 -050052 print(v, k)
53 print("---", len(stats))
jvrd4d15132002-05-11 00:59:27 +000054
55 def compile(self, font):
jvr823f8cd2006-10-21 14:12:38 +000056 """ Create a top-level OTFWriter for the GPOS/GSUB table.
57 Call the compile method for the the table
jvr1c734522008-03-09 20:13:16 +000058 for each 'converter' record in the table converter list
59 call converter's write method for each item in the value.
jvr823f8cd2006-10-21 14:12:38 +000060 - For simple items, the write method adds a string to the
61 writer's self.items list.
jvr1c734522008-03-09 20:13:16 +000062 - For Struct/Table/Subtable items, it add first adds new writer to the
jvr823f8cd2006-10-21 14:12:38 +000063 to the writer's self.items, then calls the item's compile method.
64 This creates a tree of writers, rooted at the GUSB/GPOS writer, with
65 each writer representing a table, and the writer.items list containing
66 the child data strings and writers.
jvr1c734522008-03-09 20:13:16 +000067 call the getAllData method
jvr823f8cd2006-10-21 14:12:38 +000068 call _doneWriting, which removes duplicates
jvr1c734522008-03-09 20:13:16 +000069 call _gatherTables. This traverses the tables, adding unique occurences to a flat list of tables
70 Traverse the flat list of tables, calling getDataLength on each to update their position
71 Traverse the flat list of tables again, calling getData each get the data in the table, now that
72 pos's and offset are known.
jvr823f8cd2006-10-21 14:12:38 +000073
74 If a lookup subtable overflows an offset, we have to start all over.
75 """
Behdad Esfahbode388db52013-11-28 14:26:58 -050076 class GlobalState(object):
Behdad Esfahbod79f73442013-11-26 17:07:37 -050077 def __init__(self, tableType):
78 self.tableType = tableType
79 globalState = GlobalState(tableType=self.tableTag)
80 writer = OTTableWriter(globalState)
jvr823f8cd2006-10-21 14:12:38 +000081 writer.parent = None
jvrd4d15132002-05-11 00:59:27 +000082 self.table.compile(writer, font)
jvrcfadfd02002-07-22 22:13:57 +000083 return writer.getAllData()
jvr823f8cd2006-10-21 14:12:38 +000084
jvrd4d15132002-05-11 00:59:27 +000085 def toXML(self, writer, font):
86 self.table.toXML2(writer, font)
87
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -050088 def fromXML(self, name, attrs, content, font):
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -050089 from . import otTables
jvrd4d15132002-05-11 00:59:27 +000090 if not hasattr(self, "table"):
91 tableClass = getattr(otTables, self.tableTag)
92 self.table = tableClass()
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -050093 self.table.fromXML(name, attrs, content, font)
jvrd4d15132002-05-11 00:59:27 +000094
95
Behdad Esfahbod3879cf92013-11-22 19:23:35 -050096class OTTableReader(object):
Behdad Esfahbod79817042013-11-24 16:59:42 -050097
jvr64b5c802002-05-11 10:21:36 +000098 """Helper class to retrieve data from an OpenType table."""
Behdad Esfahbod3879cf92013-11-22 19:23:35 -050099
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500100 __slots__ = ('data', 'offset', 'pos', 'globalState', 'localState')
Behdad Esfahbod79817042013-11-24 16:59:42 -0500101
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500102 def __init__(self, data, globalState={}, localState=None, offset=0):
jvrd4d15132002-05-11 00:59:27 +0000103 self.data = data
104 self.offset = offset
105 self.pos = offset
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500106 self.globalState = globalState
107 self.localState = localState
Behdad Esfahbod79817042013-11-24 16:59:42 -0500108
Behdad Esfahbod0fac7fe2013-11-24 18:04:29 -0500109 def getSubReader(self, offset):
jvrd4d15132002-05-11 00:59:27 +0000110 offset = self.offset + offset
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500111 cachingStats = self.globalState.cachingStats
112 if cachingStats is not None:
113 cachingStats[offset] = cachingStats.get(offset, 0) + 1
114 return self.__class__(self.data, self.globalState, self.localState, offset)
Behdad Esfahbod79817042013-11-24 16:59:42 -0500115
jvrd4d15132002-05-11 00:59:27 +0000116 def readUShort(self):
117 pos = self.pos
118 newpos = pos + 2
jvre69caf82002-05-13 18:08:19 +0000119 value, = struct.unpack(">H", self.data[pos:newpos])
jvrd4d15132002-05-11 00:59:27 +0000120 self.pos = newpos
121 return value
Behdad Esfahbod79817042013-11-24 16:59:42 -0500122
jvrd4d15132002-05-11 00:59:27 +0000123 def readShort(self):
124 pos = self.pos
125 newpos = pos + 2
jvre69caf82002-05-13 18:08:19 +0000126 value, = struct.unpack(">h", self.data[pos:newpos])
jvrd4d15132002-05-11 00:59:27 +0000127 self.pos = newpos
128 return value
Behdad Esfahbod79817042013-11-24 16:59:42 -0500129
jvrd4d15132002-05-11 00:59:27 +0000130 def readLong(self):
131 pos = self.pos
132 newpos = pos + 4
jvre69caf82002-05-13 18:08:19 +0000133 value, = struct.unpack(">l", self.data[pos:newpos])
jvrd4d15132002-05-11 00:59:27 +0000134 self.pos = newpos
135 return value
Behdad Esfahbod79817042013-11-24 16:59:42 -0500136
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500137 def readUInt24(self):
138 pos = self.pos
139 newpos = pos + 3
Behdad Esfahbodc0a9d692013-11-28 06:38:07 -0500140 value, = struct.unpack(">l", b'\0'+self.data[pos:newpos])
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500141 self.pos = newpos
142 return value
143
jvr823f8cd2006-10-21 14:12:38 +0000144 def readULong(self):
145 pos = self.pos
146 newpos = pos + 4
147 value, = struct.unpack(">L", self.data[pos:newpos])
148 self.pos = newpos
149 return value
150
jvrd4d15132002-05-11 00:59:27 +0000151 def readTag(self):
152 pos = self.pos
153 newpos = pos + 4
Behdad Esfahbod960280b2013-11-27 18:16:43 -0500154 value = Tag(self.data[pos:newpos])
jvrd4d15132002-05-11 00:59:27 +0000155 assert len(value) == 4
156 self.pos = newpos
157 return value
Behdad Esfahbod79817042013-11-24 16:59:42 -0500158
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500159 def __setitem__(self, name, value):
160 state = self.localState.copy() if self.localState else dict()
161 state[name] = value
162 self.localState = state
Behdad Esfahbod79817042013-11-24 16:59:42 -0500163
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500164 def __getitem__(self, name):
165 return self.localState[name]
Behdad Esfahbod79817042013-11-24 16:59:42 -0500166
jvrd4d15132002-05-11 00:59:27 +0000167
Behdad Esfahbod3879cf92013-11-22 19:23:35 -0500168class OTTableWriter(object):
jvrd4d15132002-05-11 00:59:27 +0000169
jvr64b5c802002-05-11 10:21:36 +0000170 """Helper class to gather and assemble data for OpenType tables."""
171
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500172 def __init__(self, globalState, localState=None):
jvrd4d15132002-05-11 00:59:27 +0000173 self.items = []
jvrcfadfd02002-07-22 22:13:57 +0000174 self.pos = None
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500175 self.globalState = globalState
176 self.localState = localState
Behdad Esfahbod79817042013-11-24 16:59:42 -0500177
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500178 def __setitem__(self, name, value):
179 state = self.localState.copy() if self.localState else dict()
180 state[name] = value
181 self.localState = state
Behdad Esfahbod79817042013-11-24 16:59:42 -0500182
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500183 def __getitem__(self, name):
184 return self.localState[name]
Behdad Esfahbod79817042013-11-24 16:59:42 -0500185
jvr4105ca02002-07-23 08:43:03 +0000186 # assembler interface
187
188 def getAllData(self):
189 """Assemble all data, including all subtables."""
190 self._doneWriting()
jvr823f8cd2006-10-21 14:12:38 +0000191 tables, extTables = self._gatherTables()
jvr4105ca02002-07-23 08:43:03 +0000192 tables.reverse()
jvr823f8cd2006-10-21 14:12:38 +0000193 extTables.reverse()
jvr4105ca02002-07-23 08:43:03 +0000194 # Gather all data in two passes: the absolute positions of all
195 # subtable are needed before the actual data can be assembled.
196 pos = 0
197 for table in tables:
198 table.pos = pos
199 pos = pos + table.getDataLength()
jvr823f8cd2006-10-21 14:12:38 +0000200
201 for table in extTables:
202 table.pos = pos
203 pos = pos + table.getDataLength()
204
205
jvr4105ca02002-07-23 08:43:03 +0000206 data = []
207 for table in tables:
208 tableData = table.getData()
209 data.append(tableData)
jvr823f8cd2006-10-21 14:12:38 +0000210
211 for table in extTables:
212 tableData = table.getData()
213 data.append(tableData)
214
Behdad Esfahbod821572c2013-11-27 21:09:03 -0500215 return bytesjoin(data)
jvr4105ca02002-07-23 08:43:03 +0000216
217 def getDataLength(self):
218 """Return the length of this table in bytes, without subtables."""
219 l = 0
220 for item in self.items:
221 if hasattr(item, "getData") or hasattr(item, "getCountData"):
Behdad Esfahbode0c2e8e2013-11-25 05:32:17 -0500222 if item.longOffset:
jvr823f8cd2006-10-21 14:12:38 +0000223 l = l + 4 # sizeof(ULong)
224 else:
225 l = l + 2 # sizeof(UShort)
jvr4105ca02002-07-23 08:43:03 +0000226 else:
227 l = l + len(item)
228 return l
229
230 def getData(self):
231 """Assemble the data for this writer/table, without subtables."""
232 items = list(self.items) # make a shallow copy
jvr823f8cd2006-10-21 14:12:38 +0000233 pos = self.pos
234 numItems = len(items)
235 for i in range(numItems):
jvr4105ca02002-07-23 08:43:03 +0000236 item = items[i]
jvr823f8cd2006-10-21 14:12:38 +0000237
jvr4105ca02002-07-23 08:43:03 +0000238 if hasattr(item, "getData"):
Behdad Esfahbode0c2e8e2013-11-25 05:32:17 -0500239 if item.longOffset:
jvr823f8cd2006-10-21 14:12:38 +0000240 items[i] = packULong(item.pos - pos)
241 else:
242 try:
243 items[i] = packUShort(item.pos - pos)
Behdad Esfahbod9e482332013-12-17 05:46:51 -0500244 except struct.error:
jvr823f8cd2006-10-21 14:12:38 +0000245 # provide data to fix overflow problem.
Behdad Esfahbod58acba22013-11-24 20:08:05 -0500246 # If the overflow is to a lookup, or from a lookup to a subtable,
Behdad Esfahbodab0ca1b2013-12-17 06:19:15 -0500247 # just report the current item. Otherwise...
248 if self.name not in [ 'LookupList', 'Lookup']:
jvr823f8cd2006-10-21 14:12:38 +0000249 # overflow is within a subTable. Life is more complicated.
250 # If we split the sub-table just before the current item, we may still suffer overflow.
251 # This is because duplicate table merging is done only within an Extension subTable tree;
252 # when we split the subtable in two, some items may no longer be duplicates.
253 # Get worst case by adding up all the item lengths, depth first traversal.
254 # and then report the first item that overflows a short.
255 def getDeepItemLength(table):
256 if hasattr(table, "getDataLength"):
257 length = 0
258 for item in table.items:
259 length = length + getDeepItemLength(item)
260 else:
261 length = len(table)
262 return length
263
264 length = self.getDataLength()
265 if hasattr(self, "sortCoverageLast") and item.name == "Coverage":
266 # Coverage is first in the item list, but last in the table list,
267 # The original overflow is really in the item list. Skip the Coverage
268 # table in the following test.
269 items = items[i+1:]
270
271 for j in range(len(items)):
272 item = items[j]
273 length = length + getDeepItemLength(item)
274 if length > 65535:
275 break
276 overflowErrorRecord = self.getOverflowErrorRecord(item)
277
278
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500279 raise OTLOffsetOverflowError(overflowErrorRecord)
jvr823f8cd2006-10-21 14:12:38 +0000280
Behdad Esfahbod821572c2013-11-27 21:09:03 -0500281 return bytesjoin(items)
jvrd4d15132002-05-11 00:59:27 +0000282
jvrcfadfd02002-07-22 22:13:57 +0000283 def __hash__(self):
284 # only works after self._doneWriting() has been called
285 return hash(self.items)
286
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500287 def __ne__(self, other):
288 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500289 def __eq__(self, other):
290 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500291 return NotImplemented
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500292 return self.items == other.items
jvrcfadfd02002-07-22 22:13:57 +0000293
jvrcfadfd02002-07-22 22:13:57 +0000294 def _doneWriting(self, internedTables=None):
jvr823f8cd2006-10-21 14:12:38 +0000295 # Convert CountData references to data string items
296 # collapse duplicate table references to a unique entry
297 # "tables" are OTTableWriter objects.
298
299 # For Extension Lookup types, we can
300 # eliminate duplicates only within the tree under the Extension Lookup,
301 # as offsets may exceed 64K even between Extension LookupTable subtables.
jvrcfadfd02002-07-22 22:13:57 +0000302 if internedTables is None:
303 internedTables = {}
304 items = self.items
Behdad Esfahbod97dea0a2013-11-27 03:34:48 -0500305 iRange = list(range(len(items)))
jvr823f8cd2006-10-21 14:12:38 +0000306
307 if hasattr(self, "Extension"):
308 newTree = 1
309 else:
310 newTree = 0
311 for i in iRange:
jvrcfadfd02002-07-22 22:13:57 +0000312 item = items[i]
313 if hasattr(item, "getCountData"):
314 items[i] = item.getCountData()
315 elif hasattr(item, "getData"):
jvr823f8cd2006-10-21 14:12:38 +0000316 if newTree:
317 item._doneWriting()
jvrcfadfd02002-07-22 22:13:57 +0000318 else:
jvr823f8cd2006-10-21 14:12:38 +0000319 item._doneWriting(internedTables)
Behdad Esfahboddea08f22013-12-17 06:04:28 -0500320 internedItem = internedTables.get(item)
321 if internedItem:
322 items[i] = item = internedItem
jvr823f8cd2006-10-21 14:12:38 +0000323 else:
324 internedTables[item] = item
jvrcfadfd02002-07-22 22:13:57 +0000325 self.items = tuple(items)
326
jvr823f8cd2006-10-21 14:12:38 +0000327 def _gatherTables(self, tables=None, extTables=None, done=None):
328 # Convert table references in self.items tree to a flat
329 # list of tables in depth-first traversal order.
330 # "tables" are OTTableWriter objects.
331 # We do the traversal in reverse order at each level, in order to
332 # resolve duplicate references to be the last reference in the list of tables.
333 # For extension lookups, duplicate references can be merged only within the
334 # writer tree under the extension lookup.
335 if tables is None: # init call for first time.
jvrcfadfd02002-07-22 22:13:57 +0000336 tables = []
jvr823f8cd2006-10-21 14:12:38 +0000337 extTables = []
jvrcfadfd02002-07-22 22:13:57 +0000338 done = {}
jvr823f8cd2006-10-21 14:12:38 +0000339
340 done[self] = 1
341
342 numItems = len(self.items)
Behdad Esfahbod97dea0a2013-11-27 03:34:48 -0500343 iRange = list(range(numItems))
jvr823f8cd2006-10-21 14:12:38 +0000344 iRange.reverse()
345
346 if hasattr(self, "Extension"):
347 appendExtensions = 1
348 else:
349 appendExtensions = 0
350
351 # add Coverage table if it is sorted last.
352 sortCoverageLast = 0
353 if hasattr(self, "sortCoverageLast"):
354 # Find coverage table
355 for i in range(numItems):
356 item = self.items[i]
357 if hasattr(item, "name") and (item.name == "Coverage"):
358 sortCoverageLast = 1
359 break
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500360 if item not in done:
jvr823f8cd2006-10-21 14:12:38 +0000361 item._gatherTables(tables, extTables, done)
362 else:
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500363 # We're a new parent of item
364 pass
jvr823f8cd2006-10-21 14:12:38 +0000365
jvr823f8cd2006-10-21 14:12:38 +0000366 for i in iRange:
367 item = self.items[i]
jvrcfadfd02002-07-22 22:13:57 +0000368 if not hasattr(item, "getData"):
369 continue
jvr823f8cd2006-10-21 14:12:38 +0000370
371 if sortCoverageLast and (i==1) and item.name == 'Coverage':
372 # we've already 'gathered' it above
373 continue
374
375 if appendExtensions:
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500376 assert extTables is not None, "Program or XML editing error. Extension subtables cannot contain extensions subtables"
jvr823f8cd2006-10-21 14:12:38 +0000377 newDone = {}
378 item._gatherTables(extTables, None, newDone)
379
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500380 elif item not in done:
jvr823f8cd2006-10-21 14:12:38 +0000381 item._gatherTables(tables, extTables, done)
382 else:
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500383 # We're a new parent of item
384 pass
jvr823f8cd2006-10-21 14:12:38 +0000385
386
jvrcfadfd02002-07-22 22:13:57 +0000387 tables.append(self)
jvr823f8cd2006-10-21 14:12:38 +0000388 return tables, extTables
jvrcfadfd02002-07-22 22:13:57 +0000389
jvr4105ca02002-07-23 08:43:03 +0000390 # interface for gathering data, as used by table.compile()
jvrcfadfd02002-07-22 22:13:57 +0000391
jvr4105ca02002-07-23 08:43:03 +0000392 def getSubWriter(self):
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500393 subwriter = self.__class__(self.globalState, self.localState)
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500394 subwriter.parent = self # because some subtables have idential values, we discard
395 # the duplicates under the getAllData method. Hence some
396 # subtable writers can have more than one parent writer.
397 # But we just care about first one right now.
jvr823f8cd2006-10-21 14:12:38 +0000398 return subwriter
jvrd4d15132002-05-11 00:59:27 +0000399
400 def writeUShort(self, value):
401 assert 0 <= value < 0x10000
402 self.items.append(struct.pack(">H", value))
403
404 def writeShort(self, value):
405 self.items.append(struct.pack(">h", value))
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500406
407 def writeUInt24(self, value):
408 assert 0 <= value < 0x1000000
Behdad Esfahbodc0a9d692013-11-28 06:38:07 -0500409 b = struct.pack(">L", value)
410 self.items.append(b[1:])
jvrd4d15132002-05-11 00:59:27 +0000411
412 def writeLong(self, value):
413 self.items.append(struct.pack(">l", value))
414
jvr823f8cd2006-10-21 14:12:38 +0000415 def writeULong(self, value):
416 self.items.append(struct.pack(">L", value))
417
jvrd4d15132002-05-11 00:59:27 +0000418 def writeTag(self, tag):
Behdad Esfahbod960280b2013-11-27 18:16:43 -0500419 tag = Tag(tag).tobytes()
jvrd4d15132002-05-11 00:59:27 +0000420 assert len(tag) == 4
421 self.items.append(tag)
422
423 def writeSubTable(self, subWriter):
424 self.items.append(subWriter)
425
426 def writeCountReference(self, table, name):
Behdad Esfahbod79817042013-11-24 16:59:42 -0500427 ref = CountReference(table, name)
428 self.items.append(ref)
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500429 return ref
jvrd4d15132002-05-11 00:59:27 +0000430
431 def writeStruct(self, format, values):
Behdad Esfahbod66214cb2013-11-27 02:18:18 -0500432 data = struct.pack(*(format,) + values)
jvrd4d15132002-05-11 00:59:27 +0000433 self.items.append(data)
434
jvr823f8cd2006-10-21 14:12:38 +0000435 def writeData(self, data):
436 self.items.append(data)
jvrd4d15132002-05-11 00:59:27 +0000437
jvr823f8cd2006-10-21 14:12:38 +0000438 def getOverflowErrorRecord(self, item):
439 LookupListIndex = SubTableIndex = itemName = itemIndex = None
440 if self.name == 'LookupList':
441 LookupListIndex = item.repeatIndex
442 elif self.name == 'Lookup':
443 LookupListIndex = self.repeatIndex
444 SubTableIndex = item.repeatIndex
445 else:
446 itemName = item.name
447 if hasattr(item, 'repeatIndex'):
448 itemIndex = item.repeatIndex
449 if self.name == 'SubTable':
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500450 LookupListIndex = self.parent.repeatIndex
jvr823f8cd2006-10-21 14:12:38 +0000451 SubTableIndex = self.repeatIndex
452 elif self.name == 'ExtSubTable':
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500453 LookupListIndex = self.parent.parent.repeatIndex
454 SubTableIndex = self.parent.repeatIndex
jvr823f8cd2006-10-21 14:12:38 +0000455 else: # who knows how far below the SubTable level we are! Climb back up to the nearest subtable.
Behdad Esfahbod9e482332013-12-17 05:46:51 -0500456 itemName = ".".join([self.name, item.name])
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500457 p1 = self.parent
jvr823f8cd2006-10-21 14:12:38 +0000458 while p1 and p1.name not in ['ExtSubTable', 'SubTable']:
Behdad Esfahbod9e482332013-12-17 05:46:51 -0500459 itemName = ".".join([p1.name, item.name])
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500460 p1 = p1.parent
jvr823f8cd2006-10-21 14:12:38 +0000461 if p1:
462 if p1.name == 'ExtSubTable':
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500463 LookupListIndex = p1.parent.parent.repeatIndex
464 SubTableIndex = p1.parent.repeatIndex
jvr823f8cd2006-10-21 14:12:38 +0000465 else:
Behdad Esfahbodee6340f2013-12-17 06:31:37 -0500466 LookupListIndex = p1.parent.repeatIndex
Behdad Esfahbod9e482332013-12-17 05:46:51 -0500467 SubTableIndex = p1.repeatIndex
jvr823f8cd2006-10-21 14:12:38 +0000468
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500469 return OverflowErrorRecord( (self.globalState.tableType, LookupListIndex, SubTableIndex, itemName, itemIndex) )
jvr823f8cd2006-10-21 14:12:38 +0000470
jvrd4d15132002-05-11 00:59:27 +0000471
Behdad Esfahbode388db52013-11-28 14:26:58 -0500472class CountReference(object):
jvrcfadfd02002-07-22 22:13:57 +0000473 """A reference to a Count value, not a count of references."""
jvrd4d15132002-05-11 00:59:27 +0000474 def __init__(self, table, name):
475 self.table = table
476 self.name = name
Behdad Esfahbod79817042013-11-24 16:59:42 -0500477 def setValue(self, value):
478 table = self.table
479 name = self.name
480 if table[name] is None:
481 table[name] = value
482 else:
Behdad Esfahbod1f0eed82013-11-26 18:41:53 -0500483 assert table[name] == value, (name, table[name], value)
jvrcfadfd02002-07-22 22:13:57 +0000484 def getCountData(self):
jvrd4d15132002-05-11 00:59:27 +0000485 return packUShort(self.table[self.name])
486
487
jvr64b5c802002-05-11 10:21:36 +0000488def packUShort(value):
jvr64b5c802002-05-11 10:21:36 +0000489 return struct.pack(">H", value)
jvrd4d15132002-05-11 00:59:27 +0000490
491
jvr823f8cd2006-10-21 14:12:38 +0000492def packULong(value):
jvrce47e0d2008-03-09 20:48:45 +0000493 assert 0 <= value < 0x100000000, value
jvr823f8cd2006-10-21 14:12:38 +0000494 return struct.pack(">L", value)
495
496
Behdad Esfahbod5988cc32013-11-19 17:20:54 -0500497class BaseTable(object):
jvr823f8cd2006-10-21 14:12:38 +0000498
jvr823f8cd2006-10-21 14:12:38 +0000499 def __getattr__(self, attr):
Behdad Esfahbodf6502632013-12-17 05:59:05 -0500500 reader = self.__dict__.get("reader")
Behdad Esfahboddafdb292013-12-17 00:58:02 -0500501 if reader:
502 del self.reader
503 font = self.font
504 del self.font
505 self.decompile(reader, font)
506 return getattr(self, attr)
507
508 raise AttributeError(attr)
jvr823f8cd2006-10-21 14:12:38 +0000509
510
jvr64b5c802002-05-11 10:21:36 +0000511 """Generic base class for all OpenType (sub)tables."""
512
jvrd4d15132002-05-11 00:59:27 +0000513 def getConverters(self):
514 return self.converters
515
516 def getConverterByName(self, name):
517 return self.convertersByName[name]
518
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500519 def decompile(self, reader, font):
jvrf7ef96c2002-09-10 19:26:38 +0000520 self.readFormat(reader)
jvrd4d15132002-05-11 00:59:27 +0000521 table = {}
522 self.__rawTable = table # for debugging
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500523 converters = self.getConverters()
524 for conv in converters:
jvrd4d15132002-05-11 00:59:27 +0000525 if conv.name == "SubTable":
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500526 conv = conv.getConverter(reader.globalState.tableType,
jvrd4d15132002-05-11 00:59:27 +0000527 table["LookupType"])
jvr823f8cd2006-10-21 14:12:38 +0000528 if conv.name == "ExtSubTable":
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500529 conv = conv.getConverter(reader.globalState.tableType,
jvr823f8cd2006-10-21 14:12:38 +0000530 table["ExtensionLookupType"])
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500531 if conv.name == "FeatureParams":
532 conv = conv.getConverter(reader["FeatureTag"])
jvrd4d15132002-05-11 00:59:27 +0000533 if conv.repeat:
534 l = []
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500535 if conv.repeat in table:
536 countValue = table[conv.repeat]
537 else:
538 # conv.repeat is a propagated count
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500539 countValue = reader[conv.repeat]
Behdad Esfahbod6b6e9fa2013-11-24 22:11:41 -0500540 for i in range(countValue + conv.aux):
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500541 l.append(conv.read(reader, font, table))
jvrd4d15132002-05-11 00:59:27 +0000542 table[conv.name] = l
543 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500544 if conv.aux and not eval(conv.aux, None, table):
545 continue
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500546 table[conv.name] = conv.read(reader, font, table)
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500547 if conv.isPropagated:
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500548 reader[conv.name] = table[conv.name]
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500549
jvrd4d15132002-05-11 00:59:27 +0000550 self.postRead(table, font)
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500551
jvrd4d15132002-05-11 00:59:27 +0000552 del self.__rawTable # succeeded, get rid of debugging info
jvr823f8cd2006-10-21 14:12:38 +0000553
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500554 def ensureDecompiled(self):
Behdad Esfahbodf6502632013-12-17 05:59:05 -0500555 reader = self.__dict__.get("reader")
Behdad Esfahboddafdb292013-12-17 00:58:02 -0500556 if reader:
557 del self.reader
558 font = self.font
559 del self.font
560 self.decompile(reader, font)
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500561
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500562 def compile(self, writer, font):
Behdad Esfahbod3ac9e632013-11-26 19:42:55 -0500563 self.ensureDecompiled()
jvrd4d15132002-05-11 00:59:27 +0000564 table = self.preWrite(font)
jvr823f8cd2006-10-21 14:12:38 +0000565
566 if hasattr(self, 'sortCoverageLast'):
567 writer.sortCoverageLast = 1
568
Behdad Esfahbod5fec22b2013-12-17 02:42:18 -0500569 if hasattr(self.__class__, 'LookupType'):
570 writer['LookupType'].setValue(self.__class__.LookupType)
571
jvrf7ef96c2002-09-10 19:26:38 +0000572 self.writeFormat(writer)
jvrd4d15132002-05-11 00:59:27 +0000573 for conv in self.getConverters():
574 value = table.get(conv.name)
575 if conv.repeat:
576 if value is None:
jvr64b5c802002-05-11 10:21:36 +0000577 value = []
Behdad Esfahbod6b6e9fa2013-11-24 22:11:41 -0500578 countValue = len(value) - conv.aux
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500579 if conv.repeat in table:
Behdad Esfahbod6bfee2c2013-12-09 00:28:58 -0500580 CountReference(table, conv.repeat).setValue(countValue)
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500581 else:
582 # conv.repeat is a propagated count
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500583 writer[conv.repeat].setValue(countValue)
jvr823f8cd2006-10-21 14:12:38 +0000584 for i in range(len(value)):
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500585 conv.write(writer, font, table, value[i], i)
jvrd4d15132002-05-11 00:59:27 +0000586 elif conv.isCount:
587 # Special-case Count values.
588 # Assumption: a Count field will *always* precede
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500589 # the actual array(s).
jvrd4d15132002-05-11 00:59:27 +0000590 # We need a default value, as it may be set later by a nested
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500591 # table. We will later store it here.
jvrd4d15132002-05-11 00:59:27 +0000592 # We add a reference: by the time the data is assembled
593 # the Count value will be filled in.
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500594 ref = writer.writeCountReference(table, conv.name)
Behdad Esfahbod6bfee2c2013-12-09 00:28:58 -0500595 table[conv.name] = None
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500596 if conv.isPropagated:
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500597 writer[conv.name] = ref
Behdad Esfahbod5fec22b2013-12-17 02:42:18 -0500598 elif conv.isLookupType:
599 ref = writer.writeCountReference(table, conv.name)
600 table[conv.name] = None
601 writer['LookupType'] = ref
jvrd4d15132002-05-11 00:59:27 +0000602 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500603 if conv.aux and not eval(conv.aux, None, table):
604 continue
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500605 conv.write(writer, font, table, value)
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500606 if conv.isPropagated:
607 writer[conv.name] = value
jvrd4d15132002-05-11 00:59:27 +0000608
jvrf7ef96c2002-09-10 19:26:38 +0000609 def readFormat(self, reader):
610 pass
611
612 def writeFormat(self, writer):
613 pass
614
jvrd4d15132002-05-11 00:59:27 +0000615 def postRead(self, table, font):
616 self.__dict__.update(table)
617
618 def preWrite(self, font):
619 return self.__dict__.copy()
620
Behdad Esfahbodd76fa682013-12-09 00:39:25 -0500621 def toXML(self, xmlWriter, font, attrs=None, name=None):
622 tableName = name if name else self.__class__.__name__
jvrd4d15132002-05-11 00:59:27 +0000623 if attrs is None:
624 attrs = []
625 if hasattr(self, "Format"):
jvr64b5c802002-05-11 10:21:36 +0000626 attrs = attrs + [("Format", self.Format)]
jvrd4d15132002-05-11 00:59:27 +0000627 xmlWriter.begintag(tableName, attrs)
628 xmlWriter.newline()
629 self.toXML2(xmlWriter, font)
630 xmlWriter.endtag(tableName)
631 xmlWriter.newline()
632
633 def toXML2(self, xmlWriter, font):
634 # Simpler variant of toXML, *only* for the top level tables (like GPOS, GSUB).
635 # This is because in TTX our parent writes our main tag, and in otBase.py we
636 # do it ourselves. I think I'm getting schizophrenic...
637 for conv in self.getConverters():
jvr64b5c802002-05-11 10:21:36 +0000638 if conv.repeat:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500639 value = getattr(self, conv.name)
jvrd4d15132002-05-11 00:59:27 +0000640 for i in range(len(value)):
641 item = value[i]
jvr64b5c802002-05-11 10:21:36 +0000642 conv.xmlWrite(xmlWriter, font, item, conv.name,
643 [("index", i)])
644 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500645 if conv.aux and not eval(conv.aux, None, vars(self)):
646 continue
647 value = getattr(self, conv.name)
jvr64b5c802002-05-11 10:21:36 +0000648 conv.xmlWrite(xmlWriter, font, value, conv.name, [])
jvrd4d15132002-05-11 00:59:27 +0000649
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500650 def fromXML(self, name, attrs, content, font):
jvrd4d15132002-05-11 00:59:27 +0000651 try:
652 conv = self.getConverterByName(name)
653 except KeyError:
jvrd4d15132002-05-11 00:59:27 +0000654 raise # XXX on KeyError, raise nice error
655 value = conv.xmlRead(attrs, content, font)
jvrd4d15132002-05-11 00:59:27 +0000656 if conv.repeat:
jvr52966bb2002-09-12 16:45:48 +0000657 seq = getattr(self, conv.name, None)
658 if seq is None:
jvrd4d15132002-05-11 00:59:27 +0000659 seq = []
jvr64b5c802002-05-11 10:21:36 +0000660 setattr(self, conv.name, seq)
jvrd4d15132002-05-11 00:59:27 +0000661 seq.append(value)
662 else:
jvr64b5c802002-05-11 10:21:36 +0000663 setattr(self, conv.name, value)
jvrd4d15132002-05-11 00:59:27 +0000664
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500665 def __ne__(self, other):
666 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500667 def __eq__(self, other):
668 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500669 return NotImplemented
Behdad Esfahbod96b321c2013-08-17 11:11:22 -0400670
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500671 self.ensureDecompiled()
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500672 other.ensureDecompiled()
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500673
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500674 return self.__dict__ == other.__dict__
jvrd4d15132002-05-11 00:59:27 +0000675
676
677class FormatSwitchingBaseTable(BaseTable):
678
jvrcfadfd02002-07-22 22:13:57 +0000679 """Minor specialization of BaseTable, for tables that have multiple
jvr64b5c802002-05-11 10:21:36 +0000680 formats, eg. CoverageFormat1 vs. CoverageFormat2."""
681
jvrd4d15132002-05-11 00:59:27 +0000682 def getConverters(self):
683 return self.converters[self.Format]
684
685 def getConverterByName(self, name):
686 return self.convertersByName[self.Format][name]
687
jvrf7ef96c2002-09-10 19:26:38 +0000688 def readFormat(self, reader):
jvrd4d15132002-05-11 00:59:27 +0000689 self.Format = reader.readUShort()
Behdad Esfahbod180ace62013-11-27 02:40:30 -0500690 assert self.Format != 0, (self, reader.pos, len(reader.data))
jvrd4d15132002-05-11 00:59:27 +0000691
jvrf7ef96c2002-09-10 19:26:38 +0000692 def writeFormat(self, writer):
jvrd4d15132002-05-11 00:59:27 +0000693 writer.writeUShort(self.Format)
jvrd4d15132002-05-11 00:59:27 +0000694
Behdad Esfahbodd76fa682013-12-09 00:39:25 -0500695 def toXML(self, xmlWriter, font, attrs=None, name=None):
696 BaseTable.toXML(self, xmlWriter, font, attrs, name=self.__class__.__name__)
697
jvrd4d15132002-05-11 00:59:27 +0000698
jvr64b5c802002-05-11 10:21:36 +0000699#
700# Support for ValueRecords
701#
702# This data type is so different from all other OpenType data types that
703# it requires quite a bit of code for itself. It even has special support
704# in OTTableReader and OTTableWriter...
705#
706
jvrd4d15132002-05-11 00:59:27 +0000707valueRecordFormat = [
708# Mask Name isDevice signed
709 (0x0001, "XPlacement", 0, 1),
710 (0x0002, "YPlacement", 0, 1),
711 (0x0004, "XAdvance", 0, 1),
712 (0x0008, "YAdvance", 0, 1),
713 (0x0010, "XPlaDevice", 1, 0),
714 (0x0020, "YPlaDevice", 1, 0),
715 (0x0040, "XAdvDevice", 1, 0),
716 (0x0080, "YAdvDevice", 1, 0),
717# reserved:
718 (0x0100, "Reserved1", 0, 0),
719 (0x0200, "Reserved2", 0, 0),
720 (0x0400, "Reserved3", 0, 0),
721 (0x0800, "Reserved4", 0, 0),
722 (0x1000, "Reserved5", 0, 0),
723 (0x2000, "Reserved6", 0, 0),
724 (0x4000, "Reserved7", 0, 0),
725 (0x8000, "Reserved8", 0, 0),
726]
727
728def _buildDict():
729 d = {}
730 for mask, name, isDevice, signed in valueRecordFormat:
731 d[name] = mask, isDevice, signed
732 return d
733
734valueRecordFormatDict = _buildDict()
735
736
Behdad Esfahbode388db52013-11-28 14:26:58 -0500737class ValueRecordFactory(object):
jvrd4d15132002-05-11 00:59:27 +0000738
jvr64b5c802002-05-11 10:21:36 +0000739 """Given a format code, this object convert ValueRecords."""
Behdad Esfahbodd01c44a2013-11-22 15:21:41 -0500740
Behdad Esfahbod601bb942013-11-23 20:20:39 -0500741 def __init__(self, valueFormat):
jvrd4d15132002-05-11 00:59:27 +0000742 format = []
743 for mask, name, isDevice, signed in valueRecordFormat:
744 if valueFormat & mask:
745 format.append((name, isDevice, signed))
746 self.format = format
747
748 def readValueRecord(self, reader, font):
749 format = self.format
750 if not format:
751 return None
752 valueRecord = ValueRecord()
753 for name, isDevice, signed in format:
754 if signed:
755 value = reader.readShort()
756 else:
757 value = reader.readUShort()
758 if isDevice:
759 if value:
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -0500760 from . import otTables
jvrd4d15132002-05-11 00:59:27 +0000761 subReader = reader.getSubReader(value)
762 value = getattr(otTables, name)()
763 value.decompile(subReader, font)
764 else:
765 value = None
766 setattr(valueRecord, name, value)
767 return valueRecord
768
769 def writeValueRecord(self, writer, font, valueRecord):
770 for name, isDevice, signed in self.format:
771 value = getattr(valueRecord, name, 0)
772 if isDevice:
773 if value:
774 subWriter = writer.getSubWriter()
775 writer.writeSubTable(subWriter)
776 value.compile(subWriter, font)
777 else:
778 writer.writeUShort(0)
779 elif signed:
780 writer.writeShort(value)
781 else:
782 writer.writeUShort(value)
783
784
Behdad Esfahbode388db52013-11-28 14:26:58 -0500785class ValueRecord(object):
jvrd4d15132002-05-11 00:59:27 +0000786
787 # see ValueRecordFactory
788
789 def getFormat(self):
790 format = 0
791 for name in self.__dict__.keys():
792 format = format | valueRecordFormatDict[name][0]
793 return format
794
795 def toXML(self, xmlWriter, font, valueName, attrs=None):
796 if attrs is None:
797 simpleItems = []
798 else:
799 simpleItems = list(attrs)
800 for mask, name, isDevice, format in valueRecordFormat[:4]: # "simple" values
801 if hasattr(self, name):
802 simpleItems.append((name, getattr(self, name)))
803 deviceItems = []
804 for mask, name, isDevice, format in valueRecordFormat[4:8]: # device records
805 if hasattr(self, name):
806 device = getattr(self, name)
807 if device is not None:
808 deviceItems.append((name, device))
809 if deviceItems:
810 xmlWriter.begintag(valueName, simpleItems)
811 xmlWriter.newline()
812 for name, deviceRecord in deviceItems:
813 if deviceRecord is not None:
814 deviceRecord.toXML(xmlWriter, font)
815 xmlWriter.endtag(valueName)
816 xmlWriter.newline()
817 else:
818 xmlWriter.simpletag(valueName, simpleItems)
819 xmlWriter.newline()
820
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500821 def fromXML(self, name, attrs, content, font):
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -0500822 from . import otTables
jvrd4d15132002-05-11 00:59:27 +0000823 for k, v in attrs.items():
824 setattr(self, k, int(v))
825 for element in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -0500826 if not isinstance(element, tuple):
jvrd4d15132002-05-11 00:59:27 +0000827 continue
828 name, attrs, content = element
829 value = getattr(otTables, name)()
830 for elem2 in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -0500831 if not isinstance(elem2, tuple):
jvrd4d15132002-05-11 00:59:27 +0000832 continue
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500833 name2, attrs2, content2 = elem2
834 value.fromXML(name2, attrs2, content2, font)
jvrd4d15132002-05-11 00:59:27 +0000835 setattr(self, name, value)
836
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500837 def __ne__(self, other):
838 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500839 def __eq__(self, other):
840 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500841 return NotImplemented
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500842 return self.__dict__ == other.__dict__