blob: cf4a66241d5366b729b9a40616d57d87accaa465 [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)
244 except AssertionError:
245 # 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,
jvr823f8cd2006-10-21 14:12:38 +0000247 # just report the current item.
248 if self.name in [ 'LookupList', 'Lookup']:
249 overflowErrorRecord = self.getOverflowErrorRecord(item)
250 else:
251 # overflow is within a subTable. Life is more complicated.
252 # If we split the sub-table just before the current item, we may still suffer overflow.
253 # This is because duplicate table merging is done only within an Extension subTable tree;
254 # when we split the subtable in two, some items may no longer be duplicates.
255 # Get worst case by adding up all the item lengths, depth first traversal.
256 # and then report the first item that overflows a short.
257 def getDeepItemLength(table):
258 if hasattr(table, "getDataLength"):
259 length = 0
260 for item in table.items:
261 length = length + getDeepItemLength(item)
262 else:
263 length = len(table)
264 return length
265
266 length = self.getDataLength()
267 if hasattr(self, "sortCoverageLast") and item.name == "Coverage":
268 # Coverage is first in the item list, but last in the table list,
269 # The original overflow is really in the item list. Skip the Coverage
270 # table in the following test.
271 items = items[i+1:]
272
273 for j in range(len(items)):
274 item = items[j]
275 length = length + getDeepItemLength(item)
276 if length > 65535:
277 break
278 overflowErrorRecord = self.getOverflowErrorRecord(item)
279
280
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500281 raise OTLOffsetOverflowError(overflowErrorRecord)
jvr823f8cd2006-10-21 14:12:38 +0000282
Behdad Esfahbod821572c2013-11-27 21:09:03 -0500283 return bytesjoin(items)
jvrd4d15132002-05-11 00:59:27 +0000284
jvrcfadfd02002-07-22 22:13:57 +0000285 def __hash__(self):
286 # only works after self._doneWriting() has been called
287 return hash(self.items)
288
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500289 def __ne__(self, other):
290 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500291 def __eq__(self, other):
292 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500293 return NotImplemented
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500294 return self.items == other.items
jvrcfadfd02002-07-22 22:13:57 +0000295
jvrcfadfd02002-07-22 22:13:57 +0000296 def _doneWriting(self, internedTables=None):
jvr823f8cd2006-10-21 14:12:38 +0000297 # Convert CountData references to data string items
298 # collapse duplicate table references to a unique entry
299 # "tables" are OTTableWriter objects.
300
301 # For Extension Lookup types, we can
302 # eliminate duplicates only within the tree under the Extension Lookup,
303 # as offsets may exceed 64K even between Extension LookupTable subtables.
jvrcfadfd02002-07-22 22:13:57 +0000304 if internedTables is None:
305 internedTables = {}
306 items = self.items
Behdad Esfahbod97dea0a2013-11-27 03:34:48 -0500307 iRange = list(range(len(items)))
jvr823f8cd2006-10-21 14:12:38 +0000308
309 if hasattr(self, "Extension"):
310 newTree = 1
311 else:
312 newTree = 0
313 for i in iRange:
jvrcfadfd02002-07-22 22:13:57 +0000314 item = items[i]
315 if hasattr(item, "getCountData"):
316 items[i] = item.getCountData()
317 elif hasattr(item, "getData"):
jvr823f8cd2006-10-21 14:12:38 +0000318 if newTree:
319 item._doneWriting()
jvrcfadfd02002-07-22 22:13:57 +0000320 else:
jvr823f8cd2006-10-21 14:12:38 +0000321 item._doneWriting(internedTables)
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500322 if item in internedTables:
jvr823f8cd2006-10-21 14:12:38 +0000323 items[i] = item = internedTables[item]
324 else:
325 internedTables[item] = item
jvrcfadfd02002-07-22 22:13:57 +0000326 self.items = tuple(items)
327
jvr823f8cd2006-10-21 14:12:38 +0000328 def _gatherTables(self, tables=None, extTables=None, done=None):
329 # Convert table references in self.items tree to a flat
330 # list of tables in depth-first traversal order.
331 # "tables" are OTTableWriter objects.
332 # We do the traversal in reverse order at each level, in order to
333 # resolve duplicate references to be the last reference in the list of tables.
334 # For extension lookups, duplicate references can be merged only within the
335 # writer tree under the extension lookup.
336 if tables is None: # init call for first time.
jvrcfadfd02002-07-22 22:13:57 +0000337 tables = []
jvr823f8cd2006-10-21 14:12:38 +0000338 extTables = []
jvrcfadfd02002-07-22 22:13:57 +0000339 done = {}
jvr823f8cd2006-10-21 14:12:38 +0000340
341 done[self] = 1
342
343 numItems = len(self.items)
Behdad Esfahbod97dea0a2013-11-27 03:34:48 -0500344 iRange = list(range(numItems))
jvr823f8cd2006-10-21 14:12:38 +0000345 iRange.reverse()
346
347 if hasattr(self, "Extension"):
348 appendExtensions = 1
349 else:
350 appendExtensions = 0
351
352 # add Coverage table if it is sorted last.
353 sortCoverageLast = 0
354 if hasattr(self, "sortCoverageLast"):
355 # Find coverage table
356 for i in range(numItems):
357 item = self.items[i]
358 if hasattr(item, "name") and (item.name == "Coverage"):
359 sortCoverageLast = 1
360 break
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500361 if item not in done:
jvr823f8cd2006-10-21 14:12:38 +0000362 item._gatherTables(tables, extTables, done)
363 else:
364 index = max(item.parent.keys())
365 item.parent[index + 1] = self
366
367 saveItem = None
368 for i in iRange:
369 item = self.items[i]
jvrcfadfd02002-07-22 22:13:57 +0000370 if not hasattr(item, "getData"):
371 continue
jvr823f8cd2006-10-21 14:12:38 +0000372
373 if sortCoverageLast and (i==1) and item.name == 'Coverage':
374 # we've already 'gathered' it above
375 continue
376
377 if appendExtensions:
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500378 assert extTables is not None, "Program or XML editing error. Extension subtables cannot contain extensions subtables"
jvr823f8cd2006-10-21 14:12:38 +0000379 newDone = {}
380 item._gatherTables(extTables, None, newDone)
381
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500382 elif item not in done:
jvr823f8cd2006-10-21 14:12:38 +0000383 item._gatherTables(tables, extTables, done)
384 else:
385 index = max(item.parent.keys())
386 item.parent[index + 1] = self
387
388
jvrcfadfd02002-07-22 22:13:57 +0000389 tables.append(self)
jvr823f8cd2006-10-21 14:12:38 +0000390 return tables, extTables
jvrcfadfd02002-07-22 22:13:57 +0000391
jvr4105ca02002-07-23 08:43:03 +0000392 # interface for gathering data, as used by table.compile()
jvrcfadfd02002-07-22 22:13:57 +0000393
jvr4105ca02002-07-23 08:43:03 +0000394 def getSubWriter(self):
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500395 subwriter = self.__class__(self.globalState, self.localState)
jvr823f8cd2006-10-21 14:12:38 +0000396 subwriter.parent = {0:self} # because some subtables have idential values, we discard
397 # the duplicates under the getAllData method. Hence some
398 # subtable writers can have more than one parent writer.
399 return subwriter
jvrd4d15132002-05-11 00:59:27 +0000400
401 def writeUShort(self, value):
402 assert 0 <= value < 0x10000
403 self.items.append(struct.pack(">H", value))
404
405 def writeShort(self, value):
406 self.items.append(struct.pack(">h", value))
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500407
408 def writeUInt24(self, value):
409 assert 0 <= value < 0x1000000
Behdad Esfahbodc0a9d692013-11-28 06:38:07 -0500410 b = struct.pack(">L", value)
411 self.items.append(b[1:])
jvrd4d15132002-05-11 00:59:27 +0000412
413 def writeLong(self, value):
414 self.items.append(struct.pack(">l", value))
415
jvr823f8cd2006-10-21 14:12:38 +0000416 def writeULong(self, value):
417 self.items.append(struct.pack(">L", value))
418
jvrd4d15132002-05-11 00:59:27 +0000419 def writeTag(self, tag):
Behdad Esfahbod960280b2013-11-27 18:16:43 -0500420 tag = Tag(tag).tobytes()
jvrd4d15132002-05-11 00:59:27 +0000421 assert len(tag) == 4
422 self.items.append(tag)
423
424 def writeSubTable(self, subWriter):
425 self.items.append(subWriter)
426
427 def writeCountReference(self, table, name):
Behdad Esfahbod79817042013-11-24 16:59:42 -0500428 ref = CountReference(table, name)
429 self.items.append(ref)
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500430 return ref
jvrd4d15132002-05-11 00:59:27 +0000431
432 def writeStruct(self, format, values):
Behdad Esfahbod66214cb2013-11-27 02:18:18 -0500433 data = struct.pack(*(format,) + values)
jvrd4d15132002-05-11 00:59:27 +0000434 self.items.append(data)
435
jvr823f8cd2006-10-21 14:12:38 +0000436 def writeData(self, data):
437 self.items.append(data)
jvrd4d15132002-05-11 00:59:27 +0000438
jvr823f8cd2006-10-21 14:12:38 +0000439 def getOverflowErrorRecord(self, item):
440 LookupListIndex = SubTableIndex = itemName = itemIndex = None
441 if self.name == 'LookupList':
442 LookupListIndex = item.repeatIndex
443 elif self.name == 'Lookup':
444 LookupListIndex = self.repeatIndex
445 SubTableIndex = item.repeatIndex
446 else:
447 itemName = item.name
448 if hasattr(item, 'repeatIndex'):
449 itemIndex = item.repeatIndex
450 if self.name == 'SubTable':
451 LookupListIndex = self.parent[0].repeatIndex
452 SubTableIndex = self.repeatIndex
453 elif self.name == 'ExtSubTable':
454 LookupListIndex = self.parent[0].parent[0].repeatIndex
455 SubTableIndex = self.parent[0].repeatIndex
456 else: # who knows how far below the SubTable level we are! Climb back up to the nearest subtable.
457 itemName = ".".join(self.name, item.name)
458 p1 = self.parent[0]
459 while p1 and p1.name not in ['ExtSubTable', 'SubTable']:
460 itemName = ".".join(p1.name, item.name)
461 p1 = p1.parent[0]
462 if p1:
463 if p1.name == 'ExtSubTable':
464 LookupListIndex = self.parent[0].parent[0].repeatIndex
465 SubTableIndex = self.parent[0].repeatIndex
466 else:
467 LookupListIndex = self.parent[0].repeatIndex
468 SubTableIndex = self.repeatIndex
469
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500470 return OverflowErrorRecord( (self.globalState.tableType, LookupListIndex, SubTableIndex, itemName, itemIndex) )
jvr823f8cd2006-10-21 14:12:38 +0000471
jvrd4d15132002-05-11 00:59:27 +0000472
Behdad Esfahbode388db52013-11-28 14:26:58 -0500473class CountReference(object):
jvrcfadfd02002-07-22 22:13:57 +0000474 """A reference to a Count value, not a count of references."""
jvrd4d15132002-05-11 00:59:27 +0000475 def __init__(self, table, name):
476 self.table = table
477 self.name = name
Behdad Esfahbod79817042013-11-24 16:59:42 -0500478 def setValue(self, value):
479 table = self.table
480 name = self.name
481 if table[name] is None:
482 table[name] = value
483 else:
Behdad Esfahbod1f0eed82013-11-26 18:41:53 -0500484 assert table[name] == value, (name, table[name], value)
jvrcfadfd02002-07-22 22:13:57 +0000485 def getCountData(self):
jvrd4d15132002-05-11 00:59:27 +0000486 return packUShort(self.table[self.name])
487
488
jvr64b5c802002-05-11 10:21:36 +0000489def packUShort(value):
jvrcfadfd02002-07-22 22:13:57 +0000490 assert 0 <= value < 0x10000, value
jvr64b5c802002-05-11 10:21:36 +0000491 return struct.pack(">H", value)
jvrd4d15132002-05-11 00:59:27 +0000492
493
jvr823f8cd2006-10-21 14:12:38 +0000494def packULong(value):
jvrce47e0d2008-03-09 20:48:45 +0000495 assert 0 <= value < 0x100000000, value
jvr823f8cd2006-10-21 14:12:38 +0000496 return struct.pack(">L", value)
497
498
Behdad Esfahbod5988cc32013-11-19 17:20:54 -0500499class BaseTable(object):
jvr823f8cd2006-10-21 14:12:38 +0000500 def __init__(self):
501 self.compileStatus = 0 # 0 means table was created
502 # 1 means the table.read() function was called by a table which is subject
503 # to delayed compilation
504 # 2 means that it was subject to delayed compilation, and
505 # has been decompiled
jvr823f8cd2006-10-21 14:12:38 +0000506
507 self.recurse = 0
jvrd4d15132002-05-11 00:59:27 +0000508
jvr823f8cd2006-10-21 14:12:38 +0000509 def __getattr__(self, attr):
510 # we get here only when the table does not have the attribute.
511 # This method ovveride exists so that we can try to de-compile
512 # a table which is subject to delayed decompilation, and then try
513 # to get the value again after decompilation.
514 self.recurse +=1
515 if self.recurse > 2:
516 # shouldn't ever get here - we should only get to two levels of recursion.
517 # this guards against self.decompile NOT setting compileStatus to other than 1.
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500518 raise AttributeError(attr)
jvr823f8cd2006-10-21 14:12:38 +0000519 if self.compileStatus == 1:
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500520 self.ensureDecompiled()
jvr823f8cd2006-10-21 14:12:38 +0000521 val = getattr(self, attr)
522 self.recurse -=1
523 return val
524
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500525 raise AttributeError(attr)
jvr823f8cd2006-10-21 14:12:38 +0000526
527
jvr64b5c802002-05-11 10:21:36 +0000528 """Generic base class for all OpenType (sub)tables."""
529
jvrd4d15132002-05-11 00:59:27 +0000530 def getConverters(self):
531 return self.converters
532
533 def getConverterByName(self, name):
534 return self.convertersByName[name]
535
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500536 def decompile(self, reader, font):
jvr823f8cd2006-10-21 14:12:38 +0000537 self.compileStatus = 2 # table has been decompiled.
jvrf7ef96c2002-09-10 19:26:38 +0000538 self.readFormat(reader)
jvrd4d15132002-05-11 00:59:27 +0000539 table = {}
540 self.__rawTable = table # for debugging
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500541 converters = self.getConverters()
542 for conv in converters:
jvrd4d15132002-05-11 00:59:27 +0000543 if conv.name == "SubTable":
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500544 conv = conv.getConverter(reader.globalState.tableType,
jvrd4d15132002-05-11 00:59:27 +0000545 table["LookupType"])
jvr823f8cd2006-10-21 14:12:38 +0000546 if conv.name == "ExtSubTable":
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500547 conv = conv.getConverter(reader.globalState.tableType,
jvr823f8cd2006-10-21 14:12:38 +0000548 table["ExtensionLookupType"])
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500549 if conv.name == "FeatureParams":
550 conv = conv.getConverter(reader["FeatureTag"])
jvrd4d15132002-05-11 00:59:27 +0000551 if conv.repeat:
552 l = []
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500553 if conv.repeat in table:
554 countValue = table[conv.repeat]
555 else:
556 # conv.repeat is a propagated count
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500557 countValue = reader[conv.repeat]
Behdad Esfahbod6b6e9fa2013-11-24 22:11:41 -0500558 for i in range(countValue + conv.aux):
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500559 l.append(conv.read(reader, font, table))
jvrd4d15132002-05-11 00:59:27 +0000560 table[conv.name] = l
561 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500562 if conv.aux and not eval(conv.aux, None, table):
563 continue
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500564 table[conv.name] = conv.read(reader, font, table)
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500565 if conv.isPropagated:
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500566 reader[conv.name] = table[conv.name]
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500567
jvrd4d15132002-05-11 00:59:27 +0000568 self.postRead(table, font)
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500569
jvrd4d15132002-05-11 00:59:27 +0000570 del self.__rawTable # succeeded, get rid of debugging info
jvr823f8cd2006-10-21 14:12:38 +0000571
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500572 def ensureDecompiled(self):
573 if self.compileStatus != 1:
574 return
Behdad Esfahbodf4e61ae2013-11-26 17:37:03 -0500575 self.decompile(self.reader, self.font)
576 del self.reader, self.font
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500577
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500578 def compile(self, writer, font):
Behdad Esfahbod3ac9e632013-11-26 19:42:55 -0500579 self.ensureDecompiled()
jvrd4d15132002-05-11 00:59:27 +0000580 table = self.preWrite(font)
jvr823f8cd2006-10-21 14:12:38 +0000581
582 if hasattr(self, 'sortCoverageLast'):
583 writer.sortCoverageLast = 1
584
jvrf7ef96c2002-09-10 19:26:38 +0000585 self.writeFormat(writer)
jvrd4d15132002-05-11 00:59:27 +0000586 for conv in self.getConverters():
587 value = table.get(conv.name)
588 if conv.repeat:
589 if value is None:
jvr64b5c802002-05-11 10:21:36 +0000590 value = []
Behdad Esfahbod6b6e9fa2013-11-24 22:11:41 -0500591 countValue = len(value) - conv.aux
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500592 if conv.repeat in table:
Behdad Esfahbod6bfee2c2013-12-09 00:28:58 -0500593 CountReference(table, conv.repeat).setValue(countValue)
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500594 else:
595 # conv.repeat is a propagated count
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500596 writer[conv.repeat].setValue(countValue)
jvr823f8cd2006-10-21 14:12:38 +0000597 for i in range(len(value)):
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500598 conv.write(writer, font, table, value[i], i)
jvrd4d15132002-05-11 00:59:27 +0000599 elif conv.isCount:
600 # Special-case Count values.
601 # Assumption: a Count field will *always* precede
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500602 # the actual array(s).
jvrd4d15132002-05-11 00:59:27 +0000603 # We need a default value, as it may be set later by a nested
Behdad Esfahbod41caf2d2013-11-22 19:12:14 -0500604 # table. We will later store it here.
jvrd4d15132002-05-11 00:59:27 +0000605 # We add a reference: by the time the data is assembled
606 # the Count value will be filled in.
Behdad Esfahbodee27eb82013-11-24 17:34:43 -0500607 ref = writer.writeCountReference(table, conv.name)
Behdad Esfahbod6bfee2c2013-12-09 00:28:58 -0500608 table[conv.name] = None
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500609 if conv.isPropagated:
Behdad Esfahbod79f73442013-11-26 17:07:37 -0500610 writer[conv.name] = ref
jvrd4d15132002-05-11 00:59:27 +0000611 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500612 if conv.aux and not eval(conv.aux, None, table):
613 continue
Behdad Esfahbod078b3632013-11-24 17:08:06 -0500614 conv.write(writer, font, table, value)
Behdad Esfahbod9e1bd2d2013-11-26 19:23:08 -0500615 if conv.isPropagated:
616 writer[conv.name] = value
jvrd4d15132002-05-11 00:59:27 +0000617
jvrf7ef96c2002-09-10 19:26:38 +0000618 def readFormat(self, reader):
619 pass
620
621 def writeFormat(self, writer):
622 pass
623
jvrd4d15132002-05-11 00:59:27 +0000624 def postRead(self, table, font):
625 self.__dict__.update(table)
626
627 def preWrite(self, font):
628 return self.__dict__.copy()
629
Behdad Esfahbodd76fa682013-12-09 00:39:25 -0500630 def toXML(self, xmlWriter, font, attrs=None, name=None):
631 tableName = name if name else self.__class__.__name__
jvrd4d15132002-05-11 00:59:27 +0000632 if attrs is None:
633 attrs = []
634 if hasattr(self, "Format"):
jvr64b5c802002-05-11 10:21:36 +0000635 attrs = attrs + [("Format", self.Format)]
jvrd4d15132002-05-11 00:59:27 +0000636 xmlWriter.begintag(tableName, attrs)
637 xmlWriter.newline()
638 self.toXML2(xmlWriter, font)
639 xmlWriter.endtag(tableName)
640 xmlWriter.newline()
641
642 def toXML2(self, xmlWriter, font):
643 # Simpler variant of toXML, *only* for the top level tables (like GPOS, GSUB).
644 # This is because in TTX our parent writes our main tag, and in otBase.py we
645 # do it ourselves. I think I'm getting schizophrenic...
646 for conv in self.getConverters():
jvr64b5c802002-05-11 10:21:36 +0000647 if conv.repeat:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500648 value = getattr(self, conv.name)
jvrd4d15132002-05-11 00:59:27 +0000649 for i in range(len(value)):
650 item = value[i]
jvr64b5c802002-05-11 10:21:36 +0000651 conv.xmlWrite(xmlWriter, font, item, conv.name,
652 [("index", i)])
653 else:
Behdad Esfahbod5b9cabc2013-11-25 04:01:56 -0500654 if conv.aux and not eval(conv.aux, None, vars(self)):
655 continue
656 value = getattr(self, conv.name)
jvr64b5c802002-05-11 10:21:36 +0000657 conv.xmlWrite(xmlWriter, font, value, conv.name, [])
jvrd4d15132002-05-11 00:59:27 +0000658
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500659 def fromXML(self, name, attrs, content, font):
jvrd4d15132002-05-11 00:59:27 +0000660 try:
661 conv = self.getConverterByName(name)
662 except KeyError:
jvrd4d15132002-05-11 00:59:27 +0000663 raise # XXX on KeyError, raise nice error
664 value = conv.xmlRead(attrs, content, font)
jvrd4d15132002-05-11 00:59:27 +0000665 if conv.repeat:
jvr52966bb2002-09-12 16:45:48 +0000666 seq = getattr(self, conv.name, None)
667 if seq is None:
jvrd4d15132002-05-11 00:59:27 +0000668 seq = []
jvr64b5c802002-05-11 10:21:36 +0000669 setattr(self, conv.name, seq)
jvrd4d15132002-05-11 00:59:27 +0000670 seq.append(value)
671 else:
jvr64b5c802002-05-11 10:21:36 +0000672 setattr(self, conv.name, value)
jvrd4d15132002-05-11 00:59:27 +0000673
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500674 def __ne__(self, other):
675 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500676 def __eq__(self, other):
677 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500678 return NotImplemented
Behdad Esfahbod96b321c2013-08-17 11:11:22 -0400679
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500680 self.ensureDecompiled()
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500681 other.ensureDecompiled()
Behdad Esfahbodf50d0df2013-11-20 18:38:46 -0500682
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500683 return self.__dict__ == other.__dict__
jvrd4d15132002-05-11 00:59:27 +0000684
685
686class FormatSwitchingBaseTable(BaseTable):
687
jvrcfadfd02002-07-22 22:13:57 +0000688 """Minor specialization of BaseTable, for tables that have multiple
jvr64b5c802002-05-11 10:21:36 +0000689 formats, eg. CoverageFormat1 vs. CoverageFormat2."""
690
jvrd4d15132002-05-11 00:59:27 +0000691 def getConverters(self):
692 return self.converters[self.Format]
693
694 def getConverterByName(self, name):
695 return self.convertersByName[self.Format][name]
696
jvrf7ef96c2002-09-10 19:26:38 +0000697 def readFormat(self, reader):
jvrd4d15132002-05-11 00:59:27 +0000698 self.Format = reader.readUShort()
Behdad Esfahbod180ace62013-11-27 02:40:30 -0500699 assert self.Format != 0, (self, reader.pos, len(reader.data))
jvrd4d15132002-05-11 00:59:27 +0000700
jvrf7ef96c2002-09-10 19:26:38 +0000701 def writeFormat(self, writer):
jvrd4d15132002-05-11 00:59:27 +0000702 writer.writeUShort(self.Format)
jvrd4d15132002-05-11 00:59:27 +0000703
Behdad Esfahbodd76fa682013-12-09 00:39:25 -0500704 def toXML(self, xmlWriter, font, attrs=None, name=None):
705 BaseTable.toXML(self, xmlWriter, font, attrs, name=self.__class__.__name__)
706
jvrd4d15132002-05-11 00:59:27 +0000707
jvr64b5c802002-05-11 10:21:36 +0000708#
709# Support for ValueRecords
710#
711# This data type is so different from all other OpenType data types that
712# it requires quite a bit of code for itself. It even has special support
713# in OTTableReader and OTTableWriter...
714#
715
jvrd4d15132002-05-11 00:59:27 +0000716valueRecordFormat = [
717# Mask Name isDevice signed
718 (0x0001, "XPlacement", 0, 1),
719 (0x0002, "YPlacement", 0, 1),
720 (0x0004, "XAdvance", 0, 1),
721 (0x0008, "YAdvance", 0, 1),
722 (0x0010, "XPlaDevice", 1, 0),
723 (0x0020, "YPlaDevice", 1, 0),
724 (0x0040, "XAdvDevice", 1, 0),
725 (0x0080, "YAdvDevice", 1, 0),
726# reserved:
727 (0x0100, "Reserved1", 0, 0),
728 (0x0200, "Reserved2", 0, 0),
729 (0x0400, "Reserved3", 0, 0),
730 (0x0800, "Reserved4", 0, 0),
731 (0x1000, "Reserved5", 0, 0),
732 (0x2000, "Reserved6", 0, 0),
733 (0x4000, "Reserved7", 0, 0),
734 (0x8000, "Reserved8", 0, 0),
735]
736
737def _buildDict():
738 d = {}
739 for mask, name, isDevice, signed in valueRecordFormat:
740 d[name] = mask, isDevice, signed
741 return d
742
743valueRecordFormatDict = _buildDict()
744
745
Behdad Esfahbode388db52013-11-28 14:26:58 -0500746class ValueRecordFactory(object):
jvrd4d15132002-05-11 00:59:27 +0000747
jvr64b5c802002-05-11 10:21:36 +0000748 """Given a format code, this object convert ValueRecords."""
Behdad Esfahbodd01c44a2013-11-22 15:21:41 -0500749
Behdad Esfahbod601bb942013-11-23 20:20:39 -0500750 def __init__(self, valueFormat):
jvrd4d15132002-05-11 00:59:27 +0000751 format = []
752 for mask, name, isDevice, signed in valueRecordFormat:
753 if valueFormat & mask:
754 format.append((name, isDevice, signed))
755 self.format = format
756
757 def readValueRecord(self, reader, font):
758 format = self.format
759 if not format:
760 return None
761 valueRecord = ValueRecord()
762 for name, isDevice, signed in format:
763 if signed:
764 value = reader.readShort()
765 else:
766 value = reader.readUShort()
767 if isDevice:
768 if value:
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -0500769 from . import otTables
jvrd4d15132002-05-11 00:59:27 +0000770 subReader = reader.getSubReader(value)
771 value = getattr(otTables, name)()
772 value.decompile(subReader, font)
773 else:
774 value = None
775 setattr(valueRecord, name, value)
776 return valueRecord
777
778 def writeValueRecord(self, writer, font, valueRecord):
779 for name, isDevice, signed in self.format:
780 value = getattr(valueRecord, name, 0)
781 if isDevice:
782 if value:
783 subWriter = writer.getSubWriter()
784 writer.writeSubTable(subWriter)
785 value.compile(subWriter, font)
786 else:
787 writer.writeUShort(0)
788 elif signed:
789 writer.writeShort(value)
790 else:
791 writer.writeUShort(value)
792
793
Behdad Esfahbode388db52013-11-28 14:26:58 -0500794class ValueRecord(object):
jvrd4d15132002-05-11 00:59:27 +0000795
796 # see ValueRecordFactory
797
798 def getFormat(self):
799 format = 0
800 for name in self.__dict__.keys():
801 format = format | valueRecordFormatDict[name][0]
802 return format
803
804 def toXML(self, xmlWriter, font, valueName, attrs=None):
805 if attrs is None:
806 simpleItems = []
807 else:
808 simpleItems = list(attrs)
809 for mask, name, isDevice, format in valueRecordFormat[:4]: # "simple" values
810 if hasattr(self, name):
811 simpleItems.append((name, getattr(self, name)))
812 deviceItems = []
813 for mask, name, isDevice, format in valueRecordFormat[4:8]: # device records
814 if hasattr(self, name):
815 device = getattr(self, name)
816 if device is not None:
817 deviceItems.append((name, device))
818 if deviceItems:
819 xmlWriter.begintag(valueName, simpleItems)
820 xmlWriter.newline()
821 for name, deviceRecord in deviceItems:
822 if deviceRecord is not None:
823 deviceRecord.toXML(xmlWriter, font)
824 xmlWriter.endtag(valueName)
825 xmlWriter.newline()
826 else:
827 xmlWriter.simpletag(valueName, simpleItems)
828 xmlWriter.newline()
829
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500830 def fromXML(self, name, attrs, content, font):
Behdad Esfahbod2b06aaa2013-11-27 02:34:11 -0500831 from . import otTables
jvrd4d15132002-05-11 00:59:27 +0000832 for k, v in attrs.items():
833 setattr(self, k, int(v))
834 for element in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -0500835 if not isinstance(element, tuple):
jvrd4d15132002-05-11 00:59:27 +0000836 continue
837 name, attrs, content = element
838 value = getattr(otTables, name)()
839 for elem2 in content:
Behdad Esfahbodb774f9f2013-11-27 05:17:37 -0500840 if not isinstance(elem2, tuple):
jvrd4d15132002-05-11 00:59:27 +0000841 continue
Behdad Esfahbod3a9fd302013-11-27 03:19:32 -0500842 name2, attrs2, content2 = elem2
843 value.fromXML(name2, attrs2, content2, font)
jvrd4d15132002-05-11 00:59:27 +0000844 setattr(self, name, value)
845
Behdad Esfahbod8ea64392013-12-06 22:25:48 -0500846 def __ne__(self, other):
847 return not self.__eq__(other)
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500848 def __eq__(self, other):
849 if type(self) != type(other):
Behdad Esfahbod273a9002013-12-07 03:40:44 -0500850 return NotImplemented
Behdad Esfahbodb7fd2e12013-11-27 18:58:45 -0500851 return self.__dict__ == other.__dict__