blob: 5b225c71385b772887752aba1be183ab302e769a [file] [log] [blame]
Behdad Esfahbod45d2f382013-09-18 20:47:53 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -08003# Google Author(s): Behdad Esfahbod, Roozbeh Pournader
Behdad Esfahbod45d2f382013-09-18 20:47:53 -04004
5"""Font merger.
6"""
7
Behdad Esfahbod1ae29592014-01-14 15:07:50 +08008from __future__ import print_function, division, absolute_import
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -05009from fontTools.misc.py23 import *
10from fontTools import ttLib, cffLib
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080011from fontTools.ttLib.tables import otTables, _h_e_a_d
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -050012from fontTools.ttLib.tables.DefaultTable import DefaultTable
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -050013from functools import reduce
Behdad Esfahbod45d2f382013-09-18 20:47:53 -040014import sys
Behdad Esfahbodf2d59822013-09-19 16:16:39 -040015import time
Behdad Esfahbod49028b32013-12-18 17:34:17 -050016import operator
Behdad Esfahbod45d2f382013-09-18 20:47:53 -040017
Behdad Esfahbod45d2f382013-09-18 20:47:53 -040018
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -050019def _add_method(*clazzes, **kwargs):
Behdad Esfahbodc855f3a2013-09-19 20:09:23 -040020 """Returns a decorator function that adds a new method to one or
21 more classes."""
Behdad Esfahbodc68c0ff2013-12-19 14:19:23 -050022 allowDefault = kwargs.get('allowDefaultTable', False)
Behdad Esfahbodc855f3a2013-09-19 20:09:23 -040023 def wrapper(method):
24 for clazz in clazzes:
Behdad Esfahbod35e3c722013-12-20 21:34:09 -050025 assert allowDefault or clazz != DefaultTable, 'Oops, table class not found.'
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -050026 assert method.__name__ not in clazz.__dict__, \
Behdad Esfahbodc855f3a2013-09-19 20:09:23 -040027 "Oops, class '%s' has method '%s'." % (clazz.__name__,
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -050028 method.__name__)
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -050029 setattr(clazz, method.__name__, method)
Behdad Esfahbodc855f3a2013-09-19 20:09:23 -040030 return None
31 return wrapper
Behdad Esfahbod45d2f382013-09-18 20:47:53 -040032
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -080033# General utility functions for merging values from different fonts
Behdad Esfahbod92fd5662013-12-19 04:56:50 -050034
Behdad Esfahbod49028b32013-12-18 17:34:17 -050035def equal(lst):
Behdad Esfahbod477dad12014-03-28 13:52:48 -070036 lst = list(lst)
Behdad Esfahbod49028b32013-12-18 17:34:17 -050037 t = iter(lst)
38 first = next(t)
Behdad Esfahbod477dad12014-03-28 13:52:48 -070039 assert all(item == first for item in t), "Expected all items to be equal: %s" % lst
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080040 return first
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -080041
42def first(lst):
Behdad Esfahbod49028b32013-12-18 17:34:17 -050043 return next(iter(lst))
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -080044
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080045def recalculate(lst):
Behdad Esfahbod92fd5662013-12-19 04:56:50 -050046 return NotImplemented
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080047
48def current_time(lst):
Behdad Esfahbod49028b32013-12-18 17:34:17 -050049 return int(time.time() - _h_e_a_d.mac_epoch_diff)
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080050
Roozbeh Pournader642eaf12013-12-21 01:04:18 -080051def bitwise_and(lst):
52 return reduce(operator.and_, lst)
53
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080054def bitwise_or(lst):
Behdad Esfahbod49028b32013-12-18 17:34:17 -050055 return reduce(operator.or_, lst)
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -080056
Behdad Esfahbod92fd5662013-12-19 04:56:50 -050057def avg_int(lst):
58 lst = list(lst)
59 return sum(lst) // len(lst)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -040060
Behdad Esfahbod0d5fcf42014-03-28 14:37:32 -070061def onlyExisting(func):
Behdad Esfahbod92fd5662013-12-19 04:56:50 -050062 """Returns a filter func that when called with a list,
63 only calls func on the non-NotImplemented items of the list,
64 and only so if there's at least one item remaining.
65 Otherwise returns NotImplemented."""
66
67 def wrapper(lst):
68 items = [item for item in lst if item is not NotImplemented]
69 return func(items) if items else NotImplemented
70
71 return wrapper
72
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -050073def sumLists(lst):
74 l = []
75 for item in lst:
76 l.extend(item)
77 return l
78
79def sumDicts(lst):
80 d = {}
81 for item in lst:
82 d.update(item)
83 return d
84
Behdad Esfahbod12dd5472013-12-19 05:58:06 -050085def mergeObjects(lst):
Behdad Esfahbod08845072014-03-28 15:02:40 -070086 lst = [item for item in lst if item is not NotImplemented]
Behdad Esfahbod12dd5472013-12-19 05:58:06 -050087 if not lst:
Behdad Esfahbod08845072014-03-28 15:02:40 -070088 return NotImplemented
89 lst = [item for item in lst if item is not None]
90 if not lst:
91 return None
Behdad Esfahbod12dd5472013-12-19 05:58:06 -050092
93 clazz = lst[0].__class__
94 assert all(type(item) == clazz for item in lst), lst
Behdad Esfahbod08845072014-03-28 15:02:40 -070095
Behdad Esfahbod12dd5472013-12-19 05:58:06 -050096 logic = clazz.mergeMap
97 returnTable = clazz()
Behdad Esfahbod82c54632014-03-28 14:41:53 -070098 returnDict = {}
Behdad Esfahbod12dd5472013-12-19 05:58:06 -050099
100 allKeys = set.union(set(), *(vars(table).keys() for table in lst))
101 for key in allKeys:
102 try:
103 mergeLogic = logic[key]
104 except KeyError:
105 try:
106 mergeLogic = logic['*']
107 except KeyError:
108 raise Exception("Don't know how to merge key %s of class %s" %
109 (key, clazz.__name__))
110 if mergeLogic is NotImplemented:
111 continue
112 value = mergeLogic(getattr(table, key, NotImplemented) for table in lst)
113 if value is not NotImplemented:
Behdad Esfahbod82c54632014-03-28 14:41:53 -0700114 returnDict[key] = value
115
116 returnTable.__dict__ = returnDict
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500117
118 return returnTable
119
Behdad Esfahbod201a6812014-03-28 14:58:12 -0700120def mergeBits(bitmap):
121
122 def wrapper(lst):
123 lst = list(lst)
124 returnValue = 0
125 for bitNumber in range(bitmap['size']):
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800126 try:
Behdad Esfahbod201a6812014-03-28 14:58:12 -0700127 mergeLogic = bitmap[bitNumber]
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800128 except KeyError:
Behdad Esfahbod201a6812014-03-28 14:58:12 -0700129 try:
130 mergeLogic = bitmap['*']
131 except KeyError:
132 raise Exception("Don't know how to merge bit %s" % bitNumber)
133 shiftedBit = 1 << bitNumber
134 mergedValue = mergeLogic(bool(item & shiftedBit) for item in lst)
135 returnValue |= mergedValue << bitNumber
136 return returnValue
137
138 return wrapper
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800139
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500140
141@_add_method(DefaultTable, allowDefaultTable=True)
Behdad Esfahbod3b36f552013-12-19 04:45:17 -0500142def merge(self, m, tables):
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500143 if not hasattr(self, 'mergeMap'):
144 m.log("Don't know how to merge '%s'." % self.tableTag)
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500145 return NotImplemented
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500146
Behdad Esfahbod27c71f92014-01-27 21:01:45 -0500147 logic = self.mergeMap
148
149 if isinstance(logic, dict):
150 return m.mergeObjects(self, self.mergeMap, tables)
151 else:
152 return logic(tables)
153
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500154
155ttLib.getTableClass('maxp').mergeMap = {
156 '*': max,
157 'tableTag': equal,
158 'tableVersion': equal,
159 'numGlyphs': sum,
Behdad Esfahbod27c71f92014-01-27 21:01:45 -0500160 'maxStorage': first,
161 'maxFunctionDefs': first,
162 'maxInstructionDefs': first,
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400163 # TODO When we correctly merge hinting data, update these values:
164 # maxFunctionDefs, maxInstructionDefs, maxSizeOfInstructions
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500165}
Behdad Esfahbod65f19d82013-09-18 21:02:41 -0400166
Behdad Esfahbodb8039e22014-03-28 13:54:37 -0700167headFlagsMergeBitMap = {
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800168 'size': 16,
169 '*': bitwise_or,
170 1: bitwise_and, # Baseline at y = 0
171 2: bitwise_and, # lsb at x = 0
172 3: bitwise_and, # Force ppem to integer values. FIXME?
173 5: bitwise_and, # Font is vertical
174 6: lambda bit: 0, # Always set to zero
175 11: bitwise_and, # Font data is 'lossless'
176 13: bitwise_and, # Optimized for ClearType
177 14: bitwise_and, # Last resort font. FIXME? equal or first may be better
178 15: lambda bit: 0, # Always set to zero
179}
180
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500181ttLib.getTableClass('head').mergeMap = {
182 'tableTag': equal,
183 'tableVersion': max,
184 'fontRevision': max,
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500185 'checkSumAdjustment': lambda lst: 0, # We need *something* here
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500186 'magicNumber': equal,
Behdad Esfahbod201a6812014-03-28 14:58:12 -0700187 'flags': mergeBits(headFlagsMergeBitMap),
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500188 'unitsPerEm': equal,
189 'created': current_time,
190 'modified': current_time,
191 'xMin': min,
192 'yMin': min,
193 'xMax': max,
194 'yMax': max,
195 'macStyle': first,
196 'lowestRecPPEM': max,
197 'fontDirectionHint': lambda lst: 2,
198 'indexToLocFormat': recalculate,
199 'glyphDataFormat': equal,
200}
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400201
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500202ttLib.getTableClass('hhea').mergeMap = {
203 '*': equal,
204 'tableTag': equal,
205 'tableVersion': max,
206 'ascent': max,
207 'descent': min,
208 'lineGap': max,
209 'advanceWidthMax': max,
210 'minLeftSideBearing': min,
211 'minRightSideBearing': min,
212 'xMaxExtent': max,
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800213 'caretSlopeRise': first,
214 'caretSlopeRun': first,
215 'caretOffset': first,
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500216 'numberOfHMetrics': recalculate,
217}
Behdad Esfahbod65f19d82013-09-18 21:02:41 -0400218
Behdad Esfahbodb8039e22014-03-28 13:54:37 -0700219os2FsTypeMergeBitMap = {
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800220 'size': 16,
221 '*': lambda bit: 0,
222 1: bitwise_or, # no embedding permitted
223 2: bitwise_and, # allow previewing and printing documents
224 3: bitwise_and, # allow editing documents
225 8: bitwise_or, # no subsetting permitted
226 9: bitwise_or, # no embedding of outlines permitted
227}
228
229def mergeOs2FsType(lst):
230 lst = list(lst)
231 if all(item == 0 for item in lst):
232 return 0
233
234 # Compute least restrictive logic for each fsType value
235 for i in range(len(lst)):
236 # unset bit 1 (no embedding permitted) if either bit 2 or 3 is set
237 if lst[i] & 0x000C:
238 lst[i] &= ~0x0002
239 # set bit 2 (allow previewing) if bit 3 is set (allow editing)
240 elif lst[i] & 0x0008:
241 lst[i] |= 0x0004
242 # set bits 2 and 3 if everything is allowed
243 elif lst[i] == 0:
244 lst[i] = 0x000C
245
Behdad Esfahbod201a6812014-03-28 14:58:12 -0700246 fsType = mergeBits(os2FsTypeMergeBitMap)(lst)
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800247 # unset bits 2 and 3 if bit 1 is set (some font is "no embedding")
248 if fsType & 0x0002:
249 fsType &= ~0x000C
250 return fsType
251
252
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500253ttLib.getTableClass('OS/2').mergeMap = {
254 '*': first,
255 'tableTag': equal,
256 'version': max,
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500257 'xAvgCharWidth': avg_int, # Apparently fontTools doesn't recalc this
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800258 'fsType': mergeOs2FsType, # Will be overwritten
259 'panose': first, # FIXME: should really be the first Latin font
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500260 'ulUnicodeRange1': bitwise_or,
261 'ulUnicodeRange2': bitwise_or,
262 'ulUnicodeRange3': bitwise_or,
263 'ulUnicodeRange4': bitwise_or,
264 'fsFirstCharIndex': min,
265 'fsLastCharIndex': max,
266 'sTypoAscender': max,
267 'sTypoDescender': min,
268 'sTypoLineGap': max,
269 'usWinAscent': max,
270 'usWinDescent': max,
Behdad Esfahbod0e235be2014-03-28 14:56:27 -0700271 # Version 2,3,4
Behdad Esfahbod77654212014-03-28 14:48:09 -0700272 'ulCodePageRange1': onlyExisting(bitwise_or),
273 'ulCodePageRange2': onlyExisting(bitwise_or),
274 'usMaxContex': onlyExisting(max),
Behdad Esfahboddb2410a2013-12-19 03:30:29 -0500275 # TODO version 5
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500276}
Behdad Esfahbod71294de2013-09-19 19:43:17 -0400277
Roozbeh Pournader642eaf12013-12-21 01:04:18 -0800278@_add_method(ttLib.getTableClass('OS/2'))
279def merge(self, m, tables):
280 DefaultTable.merge(self, m, tables)
281 if self.version < 2:
282 # bits 8 and 9 are reserved and should be set to zero
283 self.fsType &= ~0x0300
284 if self.version >= 3:
285 # Only one of bits 1, 2, and 3 may be set. We already take
286 # care of bit 1 implications in mergeOs2FsType. So unset
287 # bit 2 if bit 3 is already set.
288 if self.fsType & 0x0008:
289 self.fsType &= ~0x0004
290 return self
291
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500292ttLib.getTableClass('post').mergeMap = {
293 '*': first,
294 'tableTag': equal,
295 'formatType': max,
296 'isFixedPitch': min,
297 'minMemType42': max,
298 'maxMemType42': lambda lst: 0,
299 'minMemType1': max,
300 'maxMemType1': lambda lst: 0,
Behdad Esfahbod0d5fcf42014-03-28 14:37:32 -0700301 'mapping': onlyExisting(sumDicts),
Behdad Esfahbodc68c0ff2013-12-19 14:19:23 -0500302 'extraNames': lambda lst: [],
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500303}
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400304
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500305ttLib.getTableClass('vmtx').mergeMap = ttLib.getTableClass('hmtx').mergeMap = {
306 'tableTag': equal,
307 'metrics': sumDicts,
308}
Behdad Esfahbod65f19d82013-09-18 21:02:41 -0400309
Roozbeh Pournader7a272142013-12-19 15:46:05 -0800310ttLib.getTableClass('gasp').mergeMap = {
311 'tableTag': equal,
312 'version': max,
313 'gaspRange': first, # FIXME? Appears irreconcilable
314}
315
316ttLib.getTableClass('name').mergeMap = {
317 'tableTag': equal,
318 'names': first, # FIXME? Does mixing name records make sense?
319}
320
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500321ttLib.getTableClass('loca').mergeMap = {
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500322 '*': recalculate,
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500323 'tableTag': equal,
324}
325
326ttLib.getTableClass('glyf').mergeMap = {
327 'tableTag': equal,
328 'glyphs': sumDicts,
329 'glyphOrder': sumLists,
330}
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400331
Behdad Esfahbodbe4ecc72013-09-19 20:37:01 -0400332@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod3b36f552013-12-19 04:45:17 -0500333def merge(self, m, tables):
Behdad Esfahbod27c71f92014-01-27 21:01:45 -0500334 for i,table in enumerate(tables):
Behdad Esfahbod43650332013-09-20 16:33:33 -0400335 for g in table.glyphs.values():
Behdad Esfahbod27c71f92014-01-27 21:01:45 -0500336 if i:
337 # Drop hints for all but first font, since
338 # we don't map functions / CVT values.
339 g.removeHinting()
Behdad Esfahbod43650332013-09-20 16:33:33 -0400340 # Expand composite glyphs to load their
341 # composite glyph names.
342 if g.isComposite():
343 g.expand(table)
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500344 return DefaultTable.merge(self, m, tables)
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400345
Behdad Esfahbod27c71f92014-01-27 21:01:45 -0500346ttLib.getTableClass('prep').mergeMap = lambda self, lst: first(lst)
347ttLib.getTableClass('fpgm').mergeMap = lambda self, lst: first(lst)
348ttLib.getTableClass('cvt ').mergeMap = lambda self, lst: first(lst)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400349
Behdad Esfahbodbe4ecc72013-09-19 20:37:01 -0400350@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod3b36f552013-12-19 04:45:17 -0500351def merge(self, m, tables):
Behdad Esfahbod71294de2013-09-19 19:43:17 -0400352 # TODO Handle format=14.
Behdad Esfahbod3b36f552013-12-19 04:45:17 -0500353 cmapTables = [t for table in tables for t in table.tables
Behdad Esfahbodf480c7c2014-03-12 12:18:47 -0700354 if t.isUnicode()]
Behdad Esfahbod71294de2013-09-19 19:43:17 -0400355 # TODO Better handle format-4 and format-12 coexisting in same font.
356 # TODO Insert both a format-4 and format-12 if needed.
Behdad Esfahbodbe4ecc72013-09-19 20:37:01 -0400357 module = ttLib.getTableModule('cmap')
Behdad Esfahbod71294de2013-09-19 19:43:17 -0400358 assert all(t.format in [4, 12] for t in cmapTables)
359 format = max(t.format for t in cmapTables)
360 cmapTable = module.cmap_classes[format](format)
361 cmapTable.cmap = {}
362 cmapTable.platformID = 3
363 cmapTable.platEncID = max(t.platEncID for t in cmapTables)
364 cmapTable.language = 0
365 for table in cmapTables:
366 # TODO handle duplicates.
367 cmapTable.cmap.update(table.cmap)
368 self.tableVersion = 0
369 self.tables = [cmapTable]
370 self.numSubTables = len(self.tables)
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500371 return self
Behdad Esfahbod71294de2013-09-19 19:43:17 -0400372
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400373
Behdad Esfahbod26429342013-12-19 11:53:47 -0500374otTables.ScriptList.mergeMap = {
375 'ScriptCount': sum,
Behdad Esfahbod972af5a2013-12-31 18:16:36 +0800376 'ScriptRecord': lambda lst: sorted(sumLists(lst), key=lambda s: s.ScriptTag),
Behdad Esfahbod26429342013-12-19 11:53:47 -0500377}
378
379otTables.FeatureList.mergeMap = {
380 'FeatureCount': sum,
381 'FeatureRecord': sumLists,
382}
383
384otTables.LookupList.mergeMap = {
385 'LookupCount': sum,
386 'Lookup': sumLists,
387}
388
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500389otTables.Coverage.mergeMap = {
390 'glyphs': sumLists,
391}
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400392
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500393otTables.ClassDef.mergeMap = {
394 'classDefs': sumDicts,
395}
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400396
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500397otTables.LigCaretList.mergeMap = {
398 'Coverage': mergeObjects,
399 'LigGlyphCount': sum,
400 'LigGlyph': sumLists,
401}
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400402
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500403otTables.AttachList.mergeMap = {
404 'Coverage': mergeObjects,
405 'GlyphCount': sum,
406 'AttachPoint': sumLists,
407}
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400408
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500409# XXX Renumber MarkFilterSets of lookups
410otTables.MarkGlyphSetsDef.mergeMap = {
411 'MarkSetTableFormat': equal,
412 'MarkSetCount': sum,
413 'Coverage': sumLists,
414}
Behdad Esfahbodc14ab482013-09-19 21:22:54 -0400415
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500416otTables.GDEF.mergeMap = {
417 '*': mergeObjects,
418 'Version': max,
419}
420
Behdad Esfahbod26429342013-12-19 11:53:47 -0500421otTables.GSUB.mergeMap = otTables.GPOS.mergeMap = {
422 '*': mergeObjects,
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500423 'Version': max,
Behdad Esfahbod26429342013-12-19 11:53:47 -0500424}
425
426ttLib.getTableClass('GDEF').mergeMap = \
427ttLib.getTableClass('GSUB').mergeMap = \
428ttLib.getTableClass('GPOS').mergeMap = \
429ttLib.getTableClass('BASE').mergeMap = \
430ttLib.getTableClass('JSTF').mergeMap = \
431ttLib.getTableClass('MATH').mergeMap = \
432{
Behdad Esfahbod0d5fcf42014-03-28 14:37:32 -0700433 'tableTag': onlyExisting(equal), # XXX clean me up
Behdad Esfahbod12dd5472013-12-19 05:58:06 -0500434 'table': mergeObjects,
435}
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400436
Behdad Esfahbod26429342013-12-19 11:53:47 -0500437
Behdad Esfahbod50803312014-02-10 18:14:37 -0500438@_add_method(otTables.SingleSubst,
439 otTables.MultipleSubst,
440 otTables.AlternateSubst,
441 otTables.LigatureSubst,
442 otTables.ReverseChainSingleSubst,
443 otTables.SinglePos,
444 otTables.PairPos,
445 otTables.CursivePos,
446 otTables.MarkBasePos,
447 otTables.MarkLigPos,
448 otTables.MarkMarkPos)
449def mapLookups(self, lookupMap):
450 pass
451
452# Copied and trimmed down from subset.py
453@_add_method(otTables.ContextSubst,
454 otTables.ChainContextSubst,
455 otTables.ContextPos,
456 otTables.ChainContextPos)
457def __classify_context(self):
458
459 class ContextHelper(object):
460 def __init__(self, klass, Format):
461 if klass.__name__.endswith('Subst'):
462 Typ = 'Sub'
463 Type = 'Subst'
464 else:
465 Typ = 'Pos'
466 Type = 'Pos'
467 if klass.__name__.startswith('Chain'):
468 Chain = 'Chain'
469 else:
470 Chain = ''
471 ChainTyp = Chain+Typ
472
473 self.Typ = Typ
474 self.Type = Type
475 self.Chain = Chain
476 self.ChainTyp = ChainTyp
477
478 self.LookupRecord = Type+'LookupRecord'
479
480 if Format == 1:
481 self.Rule = ChainTyp+'Rule'
482 self.RuleSet = ChainTyp+'RuleSet'
483 elif Format == 2:
484 self.Rule = ChainTyp+'ClassRule'
485 self.RuleSet = ChainTyp+'ClassSet'
486
487 if self.Format not in [1, 2, 3]:
488 return None # Don't shoot the messenger; let it go
489 if not hasattr(self.__class__, "__ContextHelpers"):
490 self.__class__.__ContextHelpers = {}
491 if self.Format not in self.__class__.__ContextHelpers:
492 helper = ContextHelper(self.__class__, self.Format)
493 self.__class__.__ContextHelpers[self.Format] = helper
494 return self.__class__.__ContextHelpers[self.Format]
495
496
497@_add_method(otTables.ContextSubst,
498 otTables.ChainContextSubst,
499 otTables.ContextPos,
500 otTables.ChainContextPos)
501def mapLookups(self, lookupMap):
502 c = self.__classify_context()
503
504 if self.Format in [1, 2]:
505 for rs in getattr(self, c.RuleSet):
506 if not rs: continue
507 for r in getattr(rs, c.Rule):
508 if not r: continue
509 for ll in getattr(r, c.LookupRecord):
510 if not ll: continue
511 ll.LookupListIndex = lookupMap[ll.LookupListIndex]
512 elif self.Format == 3:
513 for ll in getattr(self, c.LookupRecord):
514 if not ll: continue
515 ll.LookupListIndex = lookupMap[ll.LookupListIndex]
516 else:
517 assert 0, "unknown format: %s" % self.Format
518
519@_add_method(otTables.Lookup)
520def mapLookups(self, lookupMap):
521 for st in self.SubTable:
522 if not st: continue
523 st.mapLookups(lookupMap)
524
525@_add_method(otTables.LookupList)
526def mapLookups(self, lookupMap):
527 for l in self.Lookup:
528 if not l: continue
529 l.mapLookups(lookupMap)
530
Behdad Esfahbod26429342013-12-19 11:53:47 -0500531@_add_method(otTables.Feature)
532def mapLookups(self, lookupMap):
533 self.LookupListIndex = [lookupMap[i] for i in self.LookupListIndex]
534
535@_add_method(otTables.FeatureList)
536def mapLookups(self, lookupMap):
537 for f in self.FeatureRecord:
538 if not f or not f.Feature: continue
539 f.Feature.mapLookups(lookupMap)
540
541@_add_method(otTables.DefaultLangSys,
542 otTables.LangSys)
543def mapFeatures(self, featureMap):
544 self.FeatureIndex = [featureMap[i] for i in self.FeatureIndex]
545 if self.ReqFeatureIndex != 65535:
546 self.ReqFeatureIndex = featureMap[self.ReqFeatureIndex]
547
548@_add_method(otTables.Script)
549def mapFeatures(self, featureMap):
550 if self.DefaultLangSys:
551 self.DefaultLangSys.mapFeatures(featureMap)
552 for l in self.LangSysRecord:
553 if not l or not l.LangSys: continue
554 l.LangSys.mapFeatures(featureMap)
555
556@_add_method(otTables.ScriptList)
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500557def mapFeatures(self, featureMap):
Behdad Esfahbod26429342013-12-19 11:53:47 -0500558 for s in self.ScriptRecord:
559 if not s or not s.Script: continue
560 s.Script.mapFeatures(featureMap)
561
562
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400563class Options(object):
564
565 class UnknownOptionError(Exception):
566 pass
567
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400568 def __init__(self, **kwargs):
569
570 self.set(**kwargs)
571
572 def set(self, **kwargs):
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500573 for k,v in kwargs.items():
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400574 if not hasattr(self, k):
575 raise self.UnknownOptionError("Unknown option '%s'" % k)
576 setattr(self, k, v)
577
578 def parse_opts(self, argv, ignore_unknown=False):
579 ret = []
580 opts = {}
581 for a in argv:
582 orig_a = a
583 if not a.startswith('--'):
584 ret.append(a)
585 continue
586 a = a[2:]
587 i = a.find('=')
588 op = '='
589 if i == -1:
590 if a.startswith("no-"):
591 k = a[3:]
592 v = False
593 else:
594 k = a
595 v = True
596 else:
597 k = a[:i]
598 if k[-1] in "-+":
599 op = k[-1]+'=' # Ops is '-=' or '+=' now.
600 k = k[:-1]
601 v = a[i+1:]
602 k = k.replace('-', '_')
603 if not hasattr(self, k):
604 if ignore_unknown == True or k in ignore_unknown:
605 ret.append(orig_a)
606 continue
607 else:
608 raise self.UnknownOptionError("Unknown option '%s'" % a)
609
610 ov = getattr(self, k)
611 if isinstance(ov, bool):
612 v = bool(v)
613 elif isinstance(ov, int):
614 v = int(v)
615 elif isinstance(ov, list):
616 vv = v.split(',')
617 if vv == ['']:
618 vv = []
619 vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
620 if op == '=':
621 v = vv
622 elif op == '+=':
623 v = ov
624 v.extend(vv)
625 elif op == '-=':
626 v = ov
627 for x in vv:
628 if x in v:
629 v.remove(x)
630 else:
631 assert 0
632
633 opts[k] = v
634 self.set(**opts)
635
636 return ret
637
638
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500639class Merger(object):
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400640
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400641 def __init__(self, options=None, log=None):
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400642
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400643 if not log:
644 log = Logger()
645 if not options:
646 options = Options()
647
648 self.options = options
649 self.log = log
650
651 def merge(self, fontfiles):
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400652
653 mega = ttLib.TTFont()
654
655 #
656 # Settle on a mega glyph order.
657 #
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400658 fonts = [ttLib.TTFont(fontfile) for fontfile in fontfiles]
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400659 glyphOrders = [font.getGlyphOrder() for font in fonts]
660 megaGlyphOrder = self._mergeGlyphOrders(glyphOrders)
661 # Reload fonts and set new glyph names on them.
662 # TODO Is it necessary to reload font? I think it is. At least
663 # it's safer, in case tables were loaded to provide glyph names.
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400664 fonts = [ttLib.TTFont(fontfile) for fontfile in fontfiles]
Behdad Esfahbod3235a042013-09-19 20:57:33 -0400665 for font,glyphOrder in zip(fonts, glyphOrders):
666 font.setGlyphOrder(glyphOrder)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400667 mega.setGlyphOrder(megaGlyphOrder)
668
Behdad Esfahbod26429342013-12-19 11:53:47 -0500669 for font in fonts:
670 self._preMerge(font)
671
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500672 allTags = reduce(set.union, (list(font.keys()) for font in fonts), set())
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400673 allTags.remove('GlyphOrder')
674 for tag in allTags:
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400675
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400676 clazz = ttLib.getTableClass(tag)
677
Behdad Esfahbod26429342013-12-19 11:53:47 -0500678 tables = [font.get(tag, NotImplemented) for font in fonts]
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500679 table = clazz(tag).merge(self, tables)
680 if table is not NotImplemented and table is not False:
Behdad Esfahbod65f19d82013-09-18 21:02:41 -0400681 mega[tag] = table
Behdad Esfahbodb640f742013-09-19 20:12:56 -0400682 self.log("Merged '%s'." % tag)
Behdad Esfahbod65f19d82013-09-18 21:02:41 -0400683 else:
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500684 self.log("Dropped '%s'." % tag)
Behdad Esfahbodb640f742013-09-19 20:12:56 -0400685 self.log.lapse("merge '%s'" % tag)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400686
Behdad Esfahbod26429342013-12-19 11:53:47 -0500687 self._postMerge(mega)
688
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400689 return mega
690
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400691 def _mergeGlyphOrders(self, glyphOrders):
Behdad Esfahbodc2e27fd2013-09-20 16:25:48 -0400692 """Modifies passed-in glyphOrders to reflect new glyph names.
693 Returns glyphOrder for the merged font."""
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400694 # Simply append font index to the glyph name for now.
Behdad Esfahbodc2e27fd2013-09-20 16:25:48 -0400695 # TODO Even this simplistic numbering can result in conflicts.
696 # But then again, we have to improve this soon anyway.
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400697 mega = []
698 for n,glyphOrder in enumerate(glyphOrders):
699 for i,glyphName in enumerate(glyphOrder):
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500700 glyphName += "#" + repr(n)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400701 glyphOrder[i] = glyphName
702 mega.append(glyphName)
703 return mega
704
Behdad Esfahbod6baf26e2013-12-19 04:47:34 -0500705 def mergeObjects(self, returnTable, logic, tables):
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500706 # Right now we don't use self at all. Will use in the future
707 # for options and logging.
708
709 if logic is NotImplemented:
710 return NotImplemented
711
712 allKeys = set.union(set(), *(vars(table).keys() for table in tables if table is not NotImplemented))
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -0800713 for key in allKeys:
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -0800714 try:
Behdad Esfahbod6baf26e2013-12-19 04:47:34 -0500715 mergeLogic = logic[key]
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -0800716 except KeyError:
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500717 try:
Behdad Esfahbod6baf26e2013-12-19 04:47:34 -0500718 mergeLogic = logic['*']
Behdad Esfahbod9e6adb62013-12-19 04:20:26 -0500719 except KeyError:
Behdad Esfahbod6baf26e2013-12-19 04:47:34 -0500720 raise Exception("Don't know how to merge key %s of class %s" %
721 (key, returnTable.__class__.__name__))
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500722 if mergeLogic is NotImplemented:
Roozbeh Pournadere219c6c2013-12-18 12:15:46 -0800723 continue
Behdad Esfahbod92fd5662013-12-19 04:56:50 -0500724 value = mergeLogic(getattr(table, key, NotImplemented) for table in tables)
725 if value is not NotImplemented:
726 setattr(returnTable, key, value)
727
728 return returnTable
Roozbeh Pournader47bee9c2013-12-18 00:45:12 -0800729
Behdad Esfahbod26429342013-12-19 11:53:47 -0500730 def _preMerge(self, font):
731
Behdad Esfahbod26429342013-12-19 11:53:47 -0500732 GDEF = font.get('GDEF')
733 GSUB = font.get('GSUB')
734 GPOS = font.get('GPOS')
735
736 for t in [GSUB, GPOS]:
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500737 if not t: continue
Behdad Esfahbod26429342013-12-19 11:53:47 -0500738
Behdad Esfahbod50803312014-02-10 18:14:37 -0500739 if t.table.LookupList:
Behdad Esfahbodb76d6ff2013-12-20 20:24:27 -0500740 lookupMap = {i:id(v) for i,v in enumerate(t.table.LookupList.Lookup)}
Behdad Esfahbod50803312014-02-10 18:14:37 -0500741 t.table.LookupList.mapLookups(lookupMap)
742 if t.table.FeatureList:
743 # XXX Handle present FeatureList but absent LookupList
744 t.table.FeatureList.mapLookups(lookupMap)
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500745
746 if t.table.FeatureList and t.table.ScriptList:
Behdad Esfahbodb76d6ff2013-12-20 20:24:27 -0500747 featureMap = {i:id(v) for i,v in enumerate(t.table.FeatureList.FeatureRecord)}
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500748 t.table.ScriptList.mapFeatures(featureMap)
749
750 # TODO GDEF/Lookup MarkFilteringSets
Behdad Esfahbod26429342013-12-19 11:53:47 -0500751 # TODO FeatureParams nameIDs
752
753 def _postMerge(self, font):
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500754
755 GDEF = font.get('GDEF')
756 GSUB = font.get('GSUB')
757 GPOS = font.get('GPOS')
758
759 for t in [GSUB, GPOS]:
760 if not t: continue
761
Behdad Esfahbod50803312014-02-10 18:14:37 -0500762 if t.table.LookupList:
Behdad Esfahbodb76d6ff2013-12-20 20:24:27 -0500763 lookupMap = {id(v):i for i,v in enumerate(t.table.LookupList.Lookup)}
Behdad Esfahbod50803312014-02-10 18:14:37 -0500764 t.table.LookupList.mapLookups(lookupMap)
765 if t.table.FeatureList:
766 # XXX Handle present FeatureList but absent LookupList
767 t.table.FeatureList.mapLookups(lookupMap)
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500768
769 if t.table.FeatureList and t.table.ScriptList:
Behdad Esfahbod50803312014-02-10 18:14:37 -0500770 # XXX Handle present ScriptList but absent FeatureList
Behdad Esfahbodb76d6ff2013-12-20 20:24:27 -0500771 featureMap = {id(v):i for i,v in enumerate(t.table.FeatureList.FeatureRecord)}
Behdad Esfahbod398770d2013-12-19 15:30:24 -0500772 t.table.ScriptList.mapFeatures(featureMap)
773
774 # TODO GDEF/Lookup MarkFilteringSets
775 # TODO FeatureParams nameIDs
Behdad Esfahbod26429342013-12-19 11:53:47 -0500776
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400777
778class Logger(object):
779
780 def __init__(self, verbose=False, xml=False, timing=False):
781 self.verbose = verbose
782 self.xml = xml
783 self.timing = timing
784 self.last_time = self.start_time = time.time()
785
786 def parse_opts(self, argv):
787 argv = argv[:]
788 for v in ['verbose', 'xml', 'timing']:
789 if "--"+v in argv:
790 setattr(self, v, True)
791 argv.remove("--"+v)
792 return argv
793
794 def __call__(self, *things):
795 if not self.verbose:
796 return
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500797 print(' '.join(str(x) for x in things))
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400798
799 def lapse(self, *things):
800 if not self.timing:
801 return
802 new_time = time.time()
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500803 print("Took %0.3fs to %s" %(new_time - self.last_time,
804 ' '.join(str(x) for x in things)))
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400805 self.last_time = new_time
806
807 def font(self, font, file=sys.stdout):
808 if not self.xml:
809 return
810 from fontTools.misc import xmlWriter
811 writer = xmlWriter.XMLWriter(file)
812 font.disassembleInstructions = False # Work around ttLib bug
813 for tag in font.keys():
814 writer.begintag(tag)
815 writer.newline()
816 font[tag].toXML(writer, font)
817 writer.endtag(tag)
818 writer.newline()
819
820
821__all__ = [
822 'Options',
823 'Merger',
824 'Logger',
825 'main'
826]
827
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400828def main(args):
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400829
830 log = Logger()
831 args = log.parse_opts(args)
832
833 options = Options()
834 args = options.parse_opts(args)
835
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400836 if len(args) < 1:
Behdad Esfahbodf63e80e2013-12-18 17:14:26 -0500837 print("usage: pyftmerge font...", file=sys.stderr)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400838 sys.exit(1)
Behdad Esfahbodf2d59822013-09-19 16:16:39 -0400839
840 merger = Merger(options=options, log=log)
841 font = merger.merge(args)
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400842 outfile = 'merged.ttf'
843 font.save(outfile)
Behdad Esfahbodb640f742013-09-19 20:12:56 -0400844 log.lapse("compile and save font")
845
846 log.last_time = log.start_time
847 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbod45d2f382013-09-18 20:47:53 -0400848
849if __name__ == "__main__":
850 main(sys.argv[1:])