2013-09-18 20:47:53 -04:00
|
|
|
# Copyright 2013 Google, Inc. All Rights Reserved.
|
|
|
|
#
|
2013-12-18 00:45:12 -08:00
|
|
|
# Google Author(s): Behdad Esfahbod, Roozbeh Pournader
|
2013-09-18 20:47:53 -04:00
|
|
|
|
|
|
|
"""Font merger.
|
|
|
|
"""
|
|
|
|
|
2013-12-18 17:14:26 -05:00
|
|
|
from __future__ import print_function, division
|
|
|
|
from fontTools.misc.py23 import *
|
|
|
|
from fontTools import ttLib, cffLib
|
2013-12-18 12:15:46 -08:00
|
|
|
from fontTools.ttLib.tables import otTables, _h_e_a_d
|
2013-12-19 04:20:26 -05:00
|
|
|
from fontTools.ttLib.tables.DefaultTable import DefaultTable
|
2013-12-18 17:14:26 -05:00
|
|
|
from functools import reduce
|
2013-09-18 20:47:53 -04:00
|
|
|
import sys
|
2013-09-19 16:16:39 -04:00
|
|
|
import time
|
2013-12-18 17:34:17 -05:00
|
|
|
import operator
|
2013-09-18 20:47:53 -04:00
|
|
|
|
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
def _add_method(*clazzes, **kwargs):
|
2013-09-19 20:09:23 -04:00
|
|
|
"""Returns a decorator function that adds a new method to one or
|
|
|
|
more classes."""
|
2013-12-19 14:19:23 -05:00
|
|
|
allowDefault = kwargs.get('allowDefaultTable', False)
|
2013-09-19 20:09:23 -04:00
|
|
|
def wrapper(method):
|
|
|
|
for clazz in clazzes:
|
2013-12-19 14:19:23 -05:00
|
|
|
if not allowDefault:
|
2013-12-19 04:20:26 -05:00
|
|
|
assert clazz != DefaultTable, 'Oops, table class not found.'
|
|
|
|
assert method.__name__ not in clazz.__dict__, \
|
2013-09-19 20:09:23 -04:00
|
|
|
"Oops, class '%s' has method '%s'." % (clazz.__name__,
|
2013-12-18 17:14:26 -05:00
|
|
|
method.__name__)
|
2013-12-19 04:20:26 -05:00
|
|
|
setattr(clazz, method.__name__, method)
|
2013-09-19 20:09:23 -04:00
|
|
|
return None
|
|
|
|
return wrapper
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-12-18 00:45:12 -08:00
|
|
|
# General utility functions for merging values from different fonts
|
2013-12-19 04:56:50 -05:00
|
|
|
|
2013-12-18 17:34:17 -05:00
|
|
|
def equal(lst):
|
|
|
|
t = iter(lst)
|
|
|
|
first = next(t)
|
|
|
|
assert all(item == first for item in t)
|
2013-12-18 12:15:46 -08:00
|
|
|
return first
|
2013-12-18 00:45:12 -08:00
|
|
|
|
|
|
|
def first(lst):
|
2013-12-18 17:34:17 -05:00
|
|
|
return next(iter(lst))
|
2013-12-18 00:45:12 -08:00
|
|
|
|
2013-12-18 12:15:46 -08:00
|
|
|
def recalculate(lst):
|
2013-12-19 04:56:50 -05:00
|
|
|
return NotImplemented
|
2013-12-18 12:15:46 -08:00
|
|
|
|
|
|
|
def current_time(lst):
|
2013-12-18 17:34:17 -05:00
|
|
|
return int(time.time() - _h_e_a_d.mac_epoch_diff)
|
2013-12-18 12:15:46 -08:00
|
|
|
|
|
|
|
def bitwise_or(lst):
|
2013-12-18 17:34:17 -05:00
|
|
|
return reduce(operator.or_, lst)
|
2013-12-18 12:15:46 -08:00
|
|
|
|
2013-12-19 04:56:50 -05:00
|
|
|
def avg_int(lst):
|
|
|
|
lst = list(lst)
|
|
|
|
return sum(lst) // len(lst)
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-12-19 04:56:50 -05:00
|
|
|
def nonnone(func):
|
2013-12-19 04:20:26 -05:00
|
|
|
"""Returns a filter func that when called with a list,
|
|
|
|
only calls func on the non-None items of the list, and
|
|
|
|
only so if there's at least one non-None item in the
|
2013-12-19 04:56:50 -05:00
|
|
|
list. Otherwise returns None."""
|
2013-09-18 21:02:41 -04:00
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
def wrapper(lst):
|
|
|
|
items = [item for item in lst if item is not None]
|
|
|
|
return func(items) if items else None
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2013-12-19 04:56:50 -05:00
|
|
|
def implemented(func):
|
|
|
|
"""Returns a filter func that when called with a list,
|
|
|
|
only calls func on the non-NotImplemented items of the list,
|
|
|
|
and only so if there's at least one item remaining.
|
|
|
|
Otherwise returns NotImplemented."""
|
|
|
|
|
|
|
|
def wrapper(lst):
|
|
|
|
items = [item for item in lst if item is not NotImplemented]
|
|
|
|
return func(items) if items else NotImplemented
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
def sumLists(lst):
|
|
|
|
l = []
|
|
|
|
for item in lst:
|
|
|
|
l.extend(item)
|
|
|
|
return l
|
|
|
|
|
|
|
|
def sumDicts(lst):
|
|
|
|
d = {}
|
|
|
|
for item in lst:
|
|
|
|
d.update(item)
|
|
|
|
return d
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-12-19 05:58:06 -05:00
|
|
|
def mergeObjects(lst):
|
|
|
|
lst = [item for item in lst if item is not None and item is not NotImplemented]
|
|
|
|
if not lst:
|
|
|
|
return None # Not all can be NotImplemented
|
|
|
|
|
|
|
|
clazz = lst[0].__class__
|
|
|
|
assert all(type(item) == clazz for item in lst), lst
|
|
|
|
logic = clazz.mergeMap
|
|
|
|
returnTable = clazz()
|
|
|
|
|
|
|
|
allKeys = set.union(set(), *(vars(table).keys() for table in lst))
|
|
|
|
for key in allKeys:
|
|
|
|
try:
|
|
|
|
mergeLogic = logic[key]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
mergeLogic = logic['*']
|
|
|
|
except KeyError:
|
|
|
|
raise Exception("Don't know how to merge key %s of class %s" %
|
|
|
|
(key, clazz.__name__))
|
|
|
|
if mergeLogic is NotImplemented:
|
|
|
|
continue
|
|
|
|
value = mergeLogic(getattr(table, key, NotImplemented) for table in lst)
|
|
|
|
if value is not NotImplemented:
|
|
|
|
setattr(returnTable, key, value)
|
|
|
|
|
|
|
|
return returnTable
|
|
|
|
|
2013-09-18 21:02:41 -04:00
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
@_add_method(DefaultTable, allowDefaultTable=True)
|
2013-12-19 04:45:17 -05:00
|
|
|
def merge(self, m, tables):
|
2013-12-19 04:20:26 -05:00
|
|
|
if not hasattr(self, 'mergeMap'):
|
|
|
|
m.log("Don't know how to merge '%s'." % self.tableTag)
|
2013-12-19 04:56:50 -05:00
|
|
|
return NotImplemented
|
2013-12-19 04:20:26 -05:00
|
|
|
|
2013-12-19 04:56:50 -05:00
|
|
|
return m.mergeObjects(self, self.mergeMap, tables)
|
2013-09-19 19:43:17 -04:00
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
ttLib.getTableClass('maxp').mergeMap = {
|
|
|
|
'*': max,
|
|
|
|
'tableTag': equal,
|
|
|
|
'tableVersion': equal,
|
|
|
|
'numGlyphs': sum,
|
|
|
|
'maxStorage': max, # FIXME: may need to be changed to sum
|
|
|
|
'maxFunctionDefs': sum,
|
|
|
|
'maxInstructionDefs': sum,
|
|
|
|
# TODO When we correctly merge hinting data, update these values:
|
|
|
|
# maxFunctionDefs, maxInstructionDefs, maxSizeOfInstructions
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('head').mergeMap = {
|
|
|
|
'tableTag': equal,
|
|
|
|
'tableVersion': max,
|
|
|
|
'fontRevision': max,
|
2013-12-19 04:56:50 -05:00
|
|
|
'checkSumAdjustment': lambda lst: 0, # We need *something* here
|
2013-12-19 04:20:26 -05:00
|
|
|
'magicNumber': equal,
|
|
|
|
'flags': first, # FIXME: replace with bit-sensitive code
|
|
|
|
'unitsPerEm': equal,
|
|
|
|
'created': current_time,
|
|
|
|
'modified': current_time,
|
|
|
|
'xMin': min,
|
|
|
|
'yMin': min,
|
|
|
|
'xMax': max,
|
|
|
|
'yMax': max,
|
|
|
|
'macStyle': first,
|
|
|
|
'lowestRecPPEM': max,
|
|
|
|
'fontDirectionHint': lambda lst: 2,
|
|
|
|
'indexToLocFormat': recalculate,
|
|
|
|
'glyphDataFormat': equal,
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('hhea').mergeMap = {
|
|
|
|
'*': equal,
|
|
|
|
'tableTag': equal,
|
|
|
|
'tableVersion': max,
|
|
|
|
'ascent': max,
|
|
|
|
'descent': min,
|
|
|
|
'lineGap': max,
|
|
|
|
'advanceWidthMax': max,
|
|
|
|
'minLeftSideBearing': min,
|
|
|
|
'minRightSideBearing': min,
|
|
|
|
'xMaxExtent': max,
|
|
|
|
'caretSlopeRise': first, # FIXME
|
|
|
|
'caretSlopeRun': first, # FIXME
|
|
|
|
'caretOffset': first, # FIXME
|
|
|
|
'numberOfHMetrics': recalculate,
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('OS/2').mergeMap = {
|
|
|
|
'*': first,
|
|
|
|
'tableTag': equal,
|
|
|
|
'version': max,
|
2013-12-19 04:56:50 -05:00
|
|
|
'xAvgCharWidth': avg_int, # Apparently fontTools doesn't recalc this
|
2013-12-19 04:20:26 -05:00
|
|
|
'fsType': first, # FIXME
|
|
|
|
'panose': first, # FIXME?
|
|
|
|
'ulUnicodeRange1': bitwise_or,
|
|
|
|
'ulUnicodeRange2': bitwise_or,
|
|
|
|
'ulUnicodeRange3': bitwise_or,
|
|
|
|
'ulUnicodeRange4': bitwise_or,
|
|
|
|
'fsFirstCharIndex': min,
|
|
|
|
'fsLastCharIndex': max,
|
|
|
|
'sTypoAscender': max,
|
|
|
|
'sTypoDescender': min,
|
|
|
|
'sTypoLineGap': max,
|
|
|
|
'usWinAscent': max,
|
|
|
|
'usWinDescent': max,
|
|
|
|
'ulCodePageRange1': bitwise_or,
|
|
|
|
'ulCodePageRange2': bitwise_or,
|
|
|
|
'usMaxContex': max,
|
|
|
|
# TODO version 5
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('post').mergeMap = {
|
|
|
|
'*': first,
|
|
|
|
'tableTag': equal,
|
|
|
|
'formatType': max,
|
|
|
|
'isFixedPitch': min,
|
|
|
|
'minMemType42': max,
|
|
|
|
'maxMemType42': lambda lst: 0,
|
|
|
|
'minMemType1': max,
|
|
|
|
'maxMemType1': lambda lst: 0,
|
2013-12-19 04:56:50 -05:00
|
|
|
'mapping': implemented(sumDicts),
|
2013-12-19 14:19:23 -05:00
|
|
|
'extraNames': lambda lst: [],
|
2013-12-19 04:20:26 -05:00
|
|
|
}
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
ttLib.getTableClass('vmtx').mergeMap = ttLib.getTableClass('hmtx').mergeMap = {
|
|
|
|
'tableTag': equal,
|
|
|
|
'metrics': sumDicts,
|
|
|
|
}
|
2013-09-18 21:02:41 -04:00
|
|
|
|
2013-12-19 15:46:05 -08:00
|
|
|
ttLib.getTableClass('gasp').mergeMap = {
|
|
|
|
'tableTag': equal,
|
|
|
|
'version': max,
|
|
|
|
'gaspRange': first, # FIXME? Appears irreconcilable
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('name').mergeMap = {
|
|
|
|
'tableTag': equal,
|
|
|
|
'names': first, # FIXME? Does mixing name records make sense?
|
|
|
|
}
|
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
ttLib.getTableClass('loca').mergeMap = {
|
2013-12-19 04:56:50 -05:00
|
|
|
'*': recalculate,
|
2013-12-19 04:20:26 -05:00
|
|
|
'tableTag': equal,
|
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('glyf').mergeMap = {
|
|
|
|
'tableTag': equal,
|
|
|
|
'glyphs': sumDicts,
|
|
|
|
'glyphOrder': sumLists,
|
|
|
|
}
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-09-19 20:37:01 -04:00
|
|
|
@_add_method(ttLib.getTableClass('glyf'))
|
2013-12-19 04:45:17 -05:00
|
|
|
def merge(self, m, tables):
|
|
|
|
for table in tables:
|
2013-09-20 16:33:33 -04:00
|
|
|
for g in table.glyphs.values():
|
|
|
|
# Drop hints for now, since we don't remap
|
|
|
|
# functions / CVT values.
|
|
|
|
g.removeHinting()
|
|
|
|
# Expand composite glyphs to load their
|
|
|
|
# composite glyph names.
|
|
|
|
if g.isComposite():
|
|
|
|
g.expand(table)
|
2013-12-19 04:56:50 -05:00
|
|
|
return DefaultTable.merge(self, m, tables)
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-12-19 04:56:50 -05:00
|
|
|
ttLib.getTableClass('prep').mergeMap = NotImplemented
|
|
|
|
ttLib.getTableClass('fpgm').mergeMap = NotImplemented
|
|
|
|
ttLib.getTableClass('cvt ').mergeMap = NotImplemented
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-09-19 20:37:01 -04:00
|
|
|
@_add_method(ttLib.getTableClass('cmap'))
|
2013-12-19 04:45:17 -05:00
|
|
|
def merge(self, m, tables):
|
2013-09-19 19:43:17 -04:00
|
|
|
# TODO Handle format=14.
|
2013-12-19 04:45:17 -05:00
|
|
|
cmapTables = [t for table in tables for t in table.tables
|
2013-09-19 19:43:17 -04:00
|
|
|
if t.platformID == 3 and t.platEncID in [1, 10]]
|
|
|
|
# TODO Better handle format-4 and format-12 coexisting in same font.
|
|
|
|
# TODO Insert both a format-4 and format-12 if needed.
|
2013-09-19 20:37:01 -04:00
|
|
|
module = ttLib.getTableModule('cmap')
|
2013-09-19 19:43:17 -04:00
|
|
|
assert all(t.format in [4, 12] for t in cmapTables)
|
|
|
|
format = max(t.format for t in cmapTables)
|
|
|
|
cmapTable = module.cmap_classes[format](format)
|
|
|
|
cmapTable.cmap = {}
|
|
|
|
cmapTable.platformID = 3
|
|
|
|
cmapTable.platEncID = max(t.platEncID for t in cmapTables)
|
|
|
|
cmapTable.language = 0
|
|
|
|
for table in cmapTables:
|
|
|
|
# TODO handle duplicates.
|
|
|
|
cmapTable.cmap.update(table.cmap)
|
|
|
|
self.tableVersion = 0
|
|
|
|
self.tables = [cmapTable]
|
|
|
|
self.numSubTables = len(self.tables)
|
2013-12-19 04:56:50 -05:00
|
|
|
return self
|
2013-09-19 19:43:17 -04:00
|
|
|
|
2013-09-19 21:22:54 -04:00
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
otTables.ScriptList.mergeMap = {
|
|
|
|
'ScriptCount': sum,
|
|
|
|
'ScriptRecord': sumLists,
|
|
|
|
}
|
|
|
|
|
|
|
|
otTables.FeatureList.mergeMap = {
|
|
|
|
'FeatureCount': sum,
|
|
|
|
'FeatureRecord': sumLists,
|
|
|
|
}
|
|
|
|
|
|
|
|
otTables.LookupList.mergeMap = {
|
|
|
|
'LookupCount': sum,
|
|
|
|
'Lookup': sumLists,
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:58:06 -05:00
|
|
|
otTables.Coverage.mergeMap = {
|
|
|
|
'glyphs': sumLists,
|
|
|
|
}
|
|
|
|
|
|
|
|
otTables.ClassDef.mergeMap = {
|
|
|
|
'classDefs': sumDicts,
|
|
|
|
}
|
|
|
|
|
|
|
|
otTables.LigCaretList.mergeMap = {
|
|
|
|
'Coverage': mergeObjects,
|
|
|
|
'LigGlyphCount': sum,
|
|
|
|
'LigGlyph': sumLists,
|
|
|
|
}
|
2013-09-19 21:22:54 -04:00
|
|
|
|
2013-12-19 05:58:06 -05:00
|
|
|
otTables.AttachList.mergeMap = {
|
|
|
|
'Coverage': mergeObjects,
|
|
|
|
'GlyphCount': sum,
|
|
|
|
'AttachPoint': sumLists,
|
|
|
|
}
|
|
|
|
|
|
|
|
# XXX Renumber MarkFilterSets of lookups
|
|
|
|
otTables.MarkGlyphSetsDef.mergeMap = {
|
|
|
|
'MarkSetTableFormat': equal,
|
|
|
|
'MarkSetCount': sum,
|
|
|
|
'Coverage': sumLists,
|
|
|
|
}
|
|
|
|
|
|
|
|
otTables.GDEF.mergeMap = {
|
|
|
|
'*': mergeObjects,
|
|
|
|
'Version': max,
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
otTables.GSUB.mergeMap = otTables.GPOS.mergeMap = {
|
|
|
|
'*': mergeObjects,
|
2013-12-19 05:58:06 -05:00
|
|
|
'Version': max,
|
2013-12-19 11:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ttLib.getTableClass('GDEF').mergeMap = \
|
|
|
|
ttLib.getTableClass('GSUB').mergeMap = \
|
|
|
|
ttLib.getTableClass('GPOS').mergeMap = \
|
|
|
|
ttLib.getTableClass('BASE').mergeMap = \
|
|
|
|
ttLib.getTableClass('JSTF').mergeMap = \
|
|
|
|
ttLib.getTableClass('MATH').mergeMap = \
|
|
|
|
{
|
|
|
|
'tableTag': equal,
|
2013-12-19 05:58:06 -05:00
|
|
|
'table': mergeObjects,
|
|
|
|
}
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
|
|
|
|
@_add_method(otTables.Feature)
|
|
|
|
def mapLookups(self, lookupMap):
|
|
|
|
self.LookupListIndex = [lookupMap[i] for i in self.LookupListIndex]
|
|
|
|
|
|
|
|
@_add_method(otTables.FeatureList)
|
|
|
|
def mapLookups(self, lookupMap):
|
|
|
|
for f in self.FeatureRecord:
|
|
|
|
if not f or not f.Feature: continue
|
|
|
|
f.Feature.mapLookups(lookupMap)
|
|
|
|
|
|
|
|
@_add_method(otTables.DefaultLangSys,
|
|
|
|
otTables.LangSys)
|
|
|
|
def mapFeatures(self, featureMap):
|
|
|
|
self.FeatureIndex = [featureMap[i] for i in self.FeatureIndex]
|
|
|
|
if self.ReqFeatureIndex != 65535:
|
|
|
|
self.ReqFeatureIndex = featureMap[self.ReqFeatureIndex]
|
|
|
|
|
|
|
|
@_add_method(otTables.Script)
|
|
|
|
def mapFeatures(self, featureMap):
|
|
|
|
if self.DefaultLangSys:
|
|
|
|
self.DefaultLangSys.mapFeatures(featureMap)
|
|
|
|
for l in self.LangSysRecord:
|
|
|
|
if not l or not l.LangSys: continue
|
|
|
|
l.LangSys.mapFeatures(featureMap)
|
|
|
|
|
|
|
|
@_add_method(otTables.ScriptList)
|
2013-12-19 15:30:24 -05:00
|
|
|
def mapFeatures(self, featureMap):
|
2013-12-19 11:53:47 -05:00
|
|
|
for s in self.ScriptRecord:
|
|
|
|
if not s or not s.Script: continue
|
|
|
|
s.Script.mapFeatures(featureMap)
|
|
|
|
|
|
|
|
|
2013-09-19 16:16:39 -04:00
|
|
|
class Options(object):
|
|
|
|
|
|
|
|
class UnknownOptionError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
|
|
self.set(**kwargs)
|
|
|
|
|
|
|
|
def set(self, **kwargs):
|
2013-12-18 17:14:26 -05:00
|
|
|
for k,v in kwargs.items():
|
2013-09-19 16:16:39 -04:00
|
|
|
if not hasattr(self, k):
|
|
|
|
raise self.UnknownOptionError("Unknown option '%s'" % k)
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
def parse_opts(self, argv, ignore_unknown=False):
|
|
|
|
ret = []
|
|
|
|
opts = {}
|
|
|
|
for a in argv:
|
|
|
|
orig_a = a
|
|
|
|
if not a.startswith('--'):
|
|
|
|
ret.append(a)
|
|
|
|
continue
|
|
|
|
a = a[2:]
|
|
|
|
i = a.find('=')
|
|
|
|
op = '='
|
|
|
|
if i == -1:
|
|
|
|
if a.startswith("no-"):
|
|
|
|
k = a[3:]
|
|
|
|
v = False
|
|
|
|
else:
|
|
|
|
k = a
|
|
|
|
v = True
|
|
|
|
else:
|
|
|
|
k = a[:i]
|
|
|
|
if k[-1] in "-+":
|
|
|
|
op = k[-1]+'=' # Ops is '-=' or '+=' now.
|
|
|
|
k = k[:-1]
|
|
|
|
v = a[i+1:]
|
|
|
|
k = k.replace('-', '_')
|
|
|
|
if not hasattr(self, k):
|
|
|
|
if ignore_unknown == True or k in ignore_unknown:
|
|
|
|
ret.append(orig_a)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise self.UnknownOptionError("Unknown option '%s'" % a)
|
|
|
|
|
|
|
|
ov = getattr(self, k)
|
|
|
|
if isinstance(ov, bool):
|
|
|
|
v = bool(v)
|
|
|
|
elif isinstance(ov, int):
|
|
|
|
v = int(v)
|
|
|
|
elif isinstance(ov, list):
|
|
|
|
vv = v.split(',')
|
|
|
|
if vv == ['']:
|
|
|
|
vv = []
|
|
|
|
vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
|
|
|
|
if op == '=':
|
|
|
|
v = vv
|
|
|
|
elif op == '+=':
|
|
|
|
v = ov
|
|
|
|
v.extend(vv)
|
|
|
|
elif op == '-=':
|
|
|
|
v = ov
|
|
|
|
for x in vv:
|
|
|
|
if x in v:
|
|
|
|
v.remove(x)
|
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
opts[k] = v
|
|
|
|
self.set(**opts)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2013-12-19 04:20:26 -05:00
|
|
|
class Merger(object):
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-09-19 16:16:39 -04:00
|
|
|
def __init__(self, options=None, log=None):
|
|
|
|
|
|
|
|
if not log:
|
|
|
|
log = Logger()
|
|
|
|
if not options:
|
|
|
|
options = Options()
|
|
|
|
|
|
|
|
self.options = options
|
|
|
|
self.log = log
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-09-19 16:16:39 -04:00
|
|
|
def merge(self, fontfiles):
|
2013-09-18 20:47:53 -04:00
|
|
|
|
|
|
|
mega = ttLib.TTFont()
|
|
|
|
|
|
|
|
#
|
|
|
|
# Settle on a mega glyph order.
|
|
|
|
#
|
2013-09-19 16:16:39 -04:00
|
|
|
fonts = [ttLib.TTFont(fontfile) for fontfile in fontfiles]
|
2013-09-18 20:47:53 -04:00
|
|
|
glyphOrders = [font.getGlyphOrder() for font in fonts]
|
|
|
|
megaGlyphOrder = self._mergeGlyphOrders(glyphOrders)
|
|
|
|
# Reload fonts and set new glyph names on them.
|
|
|
|
# TODO Is it necessary to reload font? I think it is. At least
|
|
|
|
# it's safer, in case tables were loaded to provide glyph names.
|
2013-09-19 16:16:39 -04:00
|
|
|
fonts = [ttLib.TTFont(fontfile) for fontfile in fontfiles]
|
2013-09-19 20:57:33 -04:00
|
|
|
for font,glyphOrder in zip(fonts, glyphOrders):
|
|
|
|
font.setGlyphOrder(glyphOrder)
|
2013-09-18 20:47:53 -04:00
|
|
|
mega.setGlyphOrder(megaGlyphOrder)
|
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
for font in fonts:
|
|
|
|
self._preMerge(font)
|
|
|
|
|
2013-12-18 17:14:26 -05:00
|
|
|
allTags = reduce(set.union, (list(font.keys()) for font in fonts), set())
|
2013-09-18 20:47:53 -04:00
|
|
|
allTags.remove('GlyphOrder')
|
|
|
|
for tag in allTags:
|
2013-09-19 16:16:39 -04:00
|
|
|
|
2013-09-18 20:47:53 -04:00
|
|
|
clazz = ttLib.getTableClass(tag)
|
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
tables = [font.get(tag, NotImplemented) for font in fonts]
|
2013-12-19 04:56:50 -05:00
|
|
|
table = clazz(tag).merge(self, tables)
|
|
|
|
if table is not NotImplemented and table is not False:
|
2013-09-18 21:02:41 -04:00
|
|
|
mega[tag] = table
|
2013-09-19 20:12:56 -04:00
|
|
|
self.log("Merged '%s'." % tag)
|
2013-09-18 21:02:41 -04:00
|
|
|
else:
|
2013-12-19 04:20:26 -05:00
|
|
|
self.log("Dropped '%s'." % tag)
|
2013-09-19 20:12:56 -04:00
|
|
|
self.log.lapse("merge '%s'" % tag)
|
2013-09-18 20:47:53 -04:00
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
self._postMerge(mega)
|
|
|
|
|
2013-09-18 20:47:53 -04:00
|
|
|
return mega
|
|
|
|
|
|
|
|
def _mergeGlyphOrders(self, glyphOrders):
|
2013-09-20 16:25:48 -04:00
|
|
|
"""Modifies passed-in glyphOrders to reflect new glyph names.
|
|
|
|
Returns glyphOrder for the merged font."""
|
2013-09-18 20:47:53 -04:00
|
|
|
# Simply append font index to the glyph name for now.
|
2013-09-20 16:25:48 -04:00
|
|
|
# TODO Even this simplistic numbering can result in conflicts.
|
|
|
|
# But then again, we have to improve this soon anyway.
|
2013-09-18 20:47:53 -04:00
|
|
|
mega = []
|
|
|
|
for n,glyphOrder in enumerate(glyphOrders):
|
|
|
|
for i,glyphName in enumerate(glyphOrder):
|
2013-12-18 17:14:26 -05:00
|
|
|
glyphName += "#" + repr(n)
|
2013-09-18 20:47:53 -04:00
|
|
|
glyphOrder[i] = glyphName
|
|
|
|
mega.append(glyphName)
|
|
|
|
return mega
|
|
|
|
|
2013-12-19 04:47:34 -05:00
|
|
|
def mergeObjects(self, returnTable, logic, tables):
|
2013-12-19 04:56:50 -05:00
|
|
|
# Right now we don't use self at all. Will use in the future
|
|
|
|
# for options and logging.
|
|
|
|
|
|
|
|
if logic is NotImplemented:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
allKeys = set.union(set(), *(vars(table).keys() for table in tables if table is not NotImplemented))
|
2013-12-18 00:45:12 -08:00
|
|
|
for key in allKeys:
|
2013-12-18 12:15:46 -08:00
|
|
|
try:
|
2013-12-19 04:47:34 -05:00
|
|
|
mergeLogic = logic[key]
|
2013-12-18 12:15:46 -08:00
|
|
|
except KeyError:
|
2013-12-19 04:20:26 -05:00
|
|
|
try:
|
2013-12-19 04:47:34 -05:00
|
|
|
mergeLogic = logic['*']
|
2013-12-19 04:20:26 -05:00
|
|
|
except KeyError:
|
2013-12-19 04:47:34 -05:00
|
|
|
raise Exception("Don't know how to merge key %s of class %s" %
|
|
|
|
(key, returnTable.__class__.__name__))
|
2013-12-19 04:56:50 -05:00
|
|
|
if mergeLogic is NotImplemented:
|
2013-12-18 12:15:46 -08:00
|
|
|
continue
|
2013-12-19 04:56:50 -05:00
|
|
|
value = mergeLogic(getattr(table, key, NotImplemented) for table in tables)
|
|
|
|
if value is not NotImplemented:
|
|
|
|
setattr(returnTable, key, value)
|
|
|
|
|
|
|
|
return returnTable
|
2013-12-18 00:45:12 -08:00
|
|
|
|
2013-12-19 11:53:47 -05:00
|
|
|
def _preMerge(self, font):
|
|
|
|
|
|
|
|
GDEF = font.get('GDEF')
|
|
|
|
GSUB = font.get('GSUB')
|
|
|
|
GPOS = font.get('GPOS')
|
|
|
|
|
|
|
|
for t in [GSUB, GPOS]:
|
2013-12-19 15:30:24 -05:00
|
|
|
if not t: continue
|
|
|
|
|
|
|
|
if t.table.LookupList and t.table.FeatureList:
|
2013-12-20 20:24:27 -05:00
|
|
|
lookupMap = {i:id(v) for i,v in enumerate(t.table.LookupList.Lookup)}
|
2013-12-19 15:30:24 -05:00
|
|
|
t.table.FeatureList.mapLookups(lookupMap)
|
2013-12-19 11:53:47 -05:00
|
|
|
|
2013-12-19 15:30:24 -05:00
|
|
|
if t.table.FeatureList and t.table.ScriptList:
|
2013-12-20 20:24:27 -05:00
|
|
|
featureMap = {i:id(v) for i,v in enumerate(t.table.FeatureList.FeatureRecord)}
|
2013-12-19 15:30:24 -05:00
|
|
|
t.table.ScriptList.mapFeatures(featureMap)
|
|
|
|
|
|
|
|
# TODO GDEF/Lookup MarkFilteringSets
|
2013-12-19 11:53:47 -05:00
|
|
|
# TODO FeatureParams nameIDs
|
|
|
|
|
|
|
|
def _postMerge(self, font):
|
2013-12-19 15:30:24 -05:00
|
|
|
|
|
|
|
GDEF = font.get('GDEF')
|
|
|
|
GSUB = font.get('GSUB')
|
|
|
|
GPOS = font.get('GPOS')
|
|
|
|
|
|
|
|
for t in [GSUB, GPOS]:
|
|
|
|
if not t: continue
|
|
|
|
|
|
|
|
if t.table.LookupList and t.table.FeatureList:
|
2013-12-20 20:24:27 -05:00
|
|
|
lookupMap = {id(v):i for i,v in enumerate(t.table.LookupList.Lookup)}
|
2013-12-19 15:30:24 -05:00
|
|
|
t.table.FeatureList.mapLookups(lookupMap)
|
|
|
|
|
|
|
|
if t.table.FeatureList and t.table.ScriptList:
|
2013-12-20 20:24:27 -05:00
|
|
|
featureMap = {id(v):i for i,v in enumerate(t.table.FeatureList.FeatureRecord)}
|
2013-12-19 15:30:24 -05:00
|
|
|
t.table.ScriptList.mapFeatures(featureMap)
|
|
|
|
|
|
|
|
# TODO GDEF/Lookup MarkFilteringSets
|
|
|
|
# TODO FeatureParams nameIDs
|
2013-12-19 11:53:47 -05:00
|
|
|
|
2013-09-19 16:16:39 -04:00
|
|
|
|
|
|
|
class Logger(object):
|
|
|
|
|
|
|
|
def __init__(self, verbose=False, xml=False, timing=False):
|
|
|
|
self.verbose = verbose
|
|
|
|
self.xml = xml
|
|
|
|
self.timing = timing
|
|
|
|
self.last_time = self.start_time = time.time()
|
|
|
|
|
|
|
|
def parse_opts(self, argv):
|
|
|
|
argv = argv[:]
|
|
|
|
for v in ['verbose', 'xml', 'timing']:
|
|
|
|
if "--"+v in argv:
|
|
|
|
setattr(self, v, True)
|
|
|
|
argv.remove("--"+v)
|
|
|
|
return argv
|
|
|
|
|
|
|
|
def __call__(self, *things):
|
|
|
|
if not self.verbose:
|
|
|
|
return
|
2013-12-18 17:14:26 -05:00
|
|
|
print(' '.join(str(x) for x in things))
|
2013-09-19 16:16:39 -04:00
|
|
|
|
|
|
|
def lapse(self, *things):
|
|
|
|
if not self.timing:
|
|
|
|
return
|
|
|
|
new_time = time.time()
|
2013-12-18 17:14:26 -05:00
|
|
|
print("Took %0.3fs to %s" %(new_time - self.last_time,
|
|
|
|
' '.join(str(x) for x in things)))
|
2013-09-19 16:16:39 -04:00
|
|
|
self.last_time = new_time
|
|
|
|
|
|
|
|
def font(self, font, file=sys.stdout):
|
|
|
|
if not self.xml:
|
|
|
|
return
|
|
|
|
from fontTools.misc import xmlWriter
|
|
|
|
writer = xmlWriter.XMLWriter(file)
|
|
|
|
font.disassembleInstructions = False # Work around ttLib bug
|
|
|
|
for tag in font.keys():
|
|
|
|
writer.begintag(tag)
|
|
|
|
writer.newline()
|
|
|
|
font[tag].toXML(writer, font)
|
|
|
|
writer.endtag(tag)
|
|
|
|
writer.newline()
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'Options',
|
|
|
|
'Merger',
|
|
|
|
'Logger',
|
|
|
|
'main'
|
|
|
|
]
|
|
|
|
|
2013-09-18 20:47:53 -04:00
|
|
|
def main(args):
|
2013-09-19 16:16:39 -04:00
|
|
|
|
|
|
|
log = Logger()
|
|
|
|
args = log.parse_opts(args)
|
|
|
|
|
|
|
|
options = Options()
|
|
|
|
args = options.parse_opts(args)
|
|
|
|
|
2013-09-18 20:47:53 -04:00
|
|
|
if len(args) < 1:
|
2013-12-18 17:14:26 -05:00
|
|
|
print("usage: pyftmerge font...", file=sys.stderr)
|
2013-09-18 20:47:53 -04:00
|
|
|
sys.exit(1)
|
2013-09-19 16:16:39 -04:00
|
|
|
|
|
|
|
merger = Merger(options=options, log=log)
|
|
|
|
font = merger.merge(args)
|
2013-09-18 20:47:53 -04:00
|
|
|
outfile = 'merged.ttf'
|
|
|
|
font.save(outfile)
|
2013-09-19 20:12:56 -04:00
|
|
|
log.lapse("compile and save font")
|
|
|
|
|
|
|
|
log.last_time = log.start_time
|
|
|
|
log.lapse("make one with everything(TOTAL TIME)")
|
2013-09-18 20:47:53 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|