2015-09-04 15:06:11 +02:00
|
|
|
from __future__ import print_function, division, absolute_import
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from fontTools.feaLib.error import FeatureLibError
|
|
|
|
from fontTools.feaLib.parser import Parser
|
2016-01-14 17:15:52 +01:00
|
|
|
from fontTools.otlLib import builder as otl
|
2015-09-09 16:57:29 +02:00
|
|
|
from fontTools.ttLib import getTableClass
|
2015-12-04 11:16:43 +01:00
|
|
|
from fontTools.ttLib.tables import otBase, otTables
|
2016-01-07 10:31:13 +01:00
|
|
|
import itertools
|
2015-09-04 15:06:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def addOpenTypeFeatures(featurefile_path, font):
|
|
|
|
builder = Builder(featurefile_path, font)
|
|
|
|
builder.build()
|
|
|
|
|
|
|
|
|
|
|
|
class Builder(object):
|
|
|
|
def __init__(self, featurefile_path, font):
|
|
|
|
self.featurefile_path = featurefile_path
|
|
|
|
self.font = font
|
2016-01-14 17:10:45 +01:00
|
|
|
self.glyphMap = font.getReverseGlyphMap()
|
2015-09-04 22:29:06 +02:00
|
|
|
self.default_language_systems_ = set()
|
|
|
|
self.script_ = None
|
2015-12-10 19:17:11 +01:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2015-09-04 22:29:06 +02:00
|
|
|
self.language_systems = set()
|
2015-09-07 13:33:44 +02:00
|
|
|
self.named_lookups_ = {}
|
2015-09-07 11:14:03 +02:00
|
|
|
self.cur_lookup_ = None
|
2015-09-07 13:33:44 +02:00
|
|
|
self.cur_lookup_name_ = None
|
2015-09-07 17:22:37 +02:00
|
|
|
self.cur_feature_name_ = None
|
2015-09-07 11:14:03 +02:00
|
|
|
self.lookups_ = []
|
2015-09-08 15:55:54 +02:00
|
|
|
self.features_ = {} # ('latn', 'DEU ', 'smcp') --> [LookupBuilder*]
|
2016-01-11 16:00:52 +01:00
|
|
|
self.parseTree = None
|
|
|
|
self.required_features_ = {} # ('latn', 'DEU ') --> 'scmp'
|
|
|
|
# for feature 'aalt'
|
|
|
|
self.aalt_features_ = [] # [(location, featureName)*], for 'aalt'
|
|
|
|
self.aalt_location_ = None
|
2016-02-03 17:38:38 +01:00
|
|
|
self.aalt_alternates_ = {}
|
2016-01-11 18:01:47 +01:00
|
|
|
# for table 'head'
|
|
|
|
self.fontRevision_ = None # 2.71
|
2016-03-14 23:44:25 +04:00
|
|
|
# for table 'name'
|
|
|
|
self.names_ = []
|
2016-01-11 16:00:52 +01:00
|
|
|
# for table 'GDEF'
|
2016-01-08 08:32:47 +01:00
|
|
|
self.attachPoints_ = {} # "a" --> {3, 7}
|
2016-01-20 09:49:09 +01:00
|
|
|
self.ligCaretCoords_ = {} # "f_f_i" --> {300, 600}
|
|
|
|
self.ligCaretPoints_ = {} # "f_f_i" --> {3, 7}
|
2016-01-08 19:06:52 +01:00
|
|
|
self.glyphClassDefs_ = {} # "fi" --> (2, (file, line, column))
|
2015-12-10 19:17:11 +01:00
|
|
|
self.markAttach_ = {} # "acute" --> (4, (file, line, column))
|
|
|
|
self.markAttachClassID_ = {} # frozenset({"acute", "grave"}) --> 4
|
|
|
|
self.markFilterSets_ = {} # frozenset({"acute", "grave"}) --> 4
|
2015-09-04 15:06:11 +02:00
|
|
|
|
|
|
|
def build(self):
|
2015-12-08 17:04:21 +01:00
|
|
|
self.parseTree = Parser(self.featurefile_path).parse()
|
|
|
|
self.parseTree.build(self)
|
2016-01-11 16:00:52 +01:00
|
|
|
self.build_feature_aalt_()
|
2016-01-11 18:01:47 +01:00
|
|
|
self.build_head()
|
2016-03-14 23:44:25 +04:00
|
|
|
self.build_name()
|
2015-09-09 16:57:29 +02:00
|
|
|
for tag in ('GPOS', 'GSUB'):
|
2015-12-07 21:37:42 +01:00
|
|
|
table = self.makeTable(tag)
|
|
|
|
if (table.ScriptList.ScriptCount > 0 or
|
|
|
|
table.FeatureList.FeatureCount > 0 or
|
|
|
|
table.LookupList.LookupCount > 0):
|
|
|
|
fontTable = self.font[tag] = getTableClass(tag)()
|
|
|
|
fontTable.table = table
|
2015-12-07 22:49:20 +01:00
|
|
|
elif tag in self.font:
|
2015-12-07 21:37:42 +01:00
|
|
|
del self.font[tag]
|
2016-01-08 17:38:49 +01:00
|
|
|
gdef = self.buildGDEF()
|
2015-12-08 17:04:21 +01:00
|
|
|
if gdef:
|
|
|
|
self.font["GDEF"] = gdef
|
|
|
|
elif "GDEF" in self.font:
|
|
|
|
del self.font["GDEF"]
|
2015-09-07 11:14:03 +02:00
|
|
|
|
2016-01-06 16:15:26 +01:00
|
|
|
def get_chained_lookup_(self, location, builder_class):
|
|
|
|
result = builder_class(self.font, location)
|
|
|
|
result.lookupflag = self.lookupflag_
|
|
|
|
result.markFilterSet = self.lookupflag_markFilterSet_
|
|
|
|
self.lookups_.append(result)
|
|
|
|
return result
|
|
|
|
|
2016-01-07 08:57:34 +01:00
|
|
|
def add_lookup_to_feature_(self, lookup, feature_name):
|
|
|
|
for script, lang in self.language_systems:
|
|
|
|
key = (script, lang, feature_name)
|
|
|
|
self.features_.setdefault(key, []).append(lookup)
|
|
|
|
|
2015-09-07 11:14:03 +02:00
|
|
|
def get_lookup_(self, location, builder_class):
|
2015-09-07 13:33:44 +02:00
|
|
|
if (self.cur_lookup_ and
|
2015-12-10 19:17:11 +01:00
|
|
|
type(self.cur_lookup_) == builder_class and
|
|
|
|
self.cur_lookup_.lookupflag == self.lookupflag_ and
|
|
|
|
self.cur_lookup_.markFilterSet ==
|
|
|
|
self.lookupflag_markFilterSet_):
|
2015-09-07 11:14:03 +02:00
|
|
|
return self.cur_lookup_
|
2015-09-07 16:27:12 +02:00
|
|
|
if self.cur_lookup_name_ and self.cur_lookup_:
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Within a named lookup block, all rules must be of "
|
|
|
|
"the same lookup type and flag", location)
|
2015-12-10 19:17:11 +01:00
|
|
|
self.cur_lookup_ = builder_class(self.font, location)
|
|
|
|
self.cur_lookup_.lookupflag = self.lookupflag_
|
|
|
|
self.cur_lookup_.markFilterSet = self.lookupflag_markFilterSet_
|
2015-09-07 11:14:03 +02:00
|
|
|
self.lookups_.append(self.cur_lookup_)
|
2015-09-07 13:33:44 +02:00
|
|
|
if self.cur_lookup_name_:
|
2015-09-07 17:22:37 +02:00
|
|
|
# We are starting a lookup rule inside a named lookup block.
|
2015-09-07 13:33:44 +02:00
|
|
|
self.named_lookups_[self.cur_lookup_name_] = self.cur_lookup_
|
2015-09-28 16:49:17 +02:00
|
|
|
if self.cur_feature_name_:
|
|
|
|
# We are starting a lookup rule inside a feature. This includes
|
|
|
|
# lookup rules inside named lookups inside features.
|
2016-01-07 08:57:34 +01:00
|
|
|
self.add_lookup_to_feature_(self.cur_lookup_,
|
|
|
|
self.cur_feature_name_)
|
2015-09-07 11:14:03 +02:00
|
|
|
return self.cur_lookup_
|
2015-09-04 15:06:11 +02:00
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def build_feature_aalt_(self):
|
2016-02-03 17:38:38 +01:00
|
|
|
if not self.aalt_features_ and not self.aalt_alternates_:
|
2016-01-11 16:00:52 +01:00
|
|
|
return
|
2016-02-03 17:38:38 +01:00
|
|
|
alternates = {g: set(a) for g, a in self.aalt_alternates_.items()}
|
2016-01-11 16:00:52 +01:00
|
|
|
for location, name in self.aalt_features_ + [(None, "aalt")]:
|
|
|
|
feature = [(script, lang, feature, lookups)
|
|
|
|
for (script, lang, feature), lookups
|
|
|
|
in self.features_.items()
|
|
|
|
if feature == name]
|
2016-01-11 18:12:23 +01:00
|
|
|
# "aalt" does not have to specify its own lookups, but it might.
|
|
|
|
if not feature and name != "aalt":
|
2016-01-11 16:00:52 +01:00
|
|
|
raise FeatureLibError("Feature %s has not been defined" % name,
|
|
|
|
location)
|
|
|
|
for script, lang, feature, lookups in feature:
|
|
|
|
for lookup in lookups:
|
|
|
|
for glyph, alts in lookup.getAlternateGlyphs().items():
|
|
|
|
alternates.setdefault(glyph, set()).update(alts)
|
|
|
|
single = {glyph: list(repl)[0] for glyph, repl in alternates.items()
|
|
|
|
if len(repl) == 1}
|
|
|
|
multi = {glyph: sorted(repl, key=self.font.getGlyphID)
|
|
|
|
for glyph, repl in alternates.items()
|
|
|
|
if len(repl) > 1}
|
|
|
|
if not single and not multi:
|
|
|
|
return
|
|
|
|
self.features_ = {(script, lang, feature): lookups
|
|
|
|
for (script, lang, feature), lookups
|
|
|
|
in self.features_.items()
|
|
|
|
if feature != "aalt"}
|
|
|
|
old_lookups = self.lookups_
|
|
|
|
self.lookups_ = []
|
|
|
|
self.start_feature(self.aalt_location_, "aalt")
|
|
|
|
if single:
|
2016-02-03 17:38:38 +01:00
|
|
|
single_lookup = self.get_lookup_(location, SingleSubstBuilder)
|
|
|
|
single_lookup.mapping = single
|
|
|
|
if multi:
|
|
|
|
multi_lookup = self.get_lookup_(location, AlternateSubstBuilder)
|
|
|
|
multi_lookup.alternates = multi
|
2016-01-11 16:00:52 +01:00
|
|
|
self.end_feature()
|
|
|
|
self.lookups_.extend(old_lookups)
|
|
|
|
|
2016-01-11 18:01:47 +01:00
|
|
|
def build_head(self):
|
|
|
|
if not self.fontRevision_:
|
|
|
|
return
|
|
|
|
table = self.font.get("head")
|
|
|
|
if not table: # this only happens for unit tests
|
|
|
|
table = self.font["head"] = getTableClass("head")()
|
|
|
|
table.decompile(b"\0" * 54, self.font)
|
|
|
|
table.tableVersion = 1.0
|
2016-01-11 19:36:19 +01:00
|
|
|
table.created = table.modified = 3406620153 # 2011-12-13 11:22:33
|
2016-01-11 18:01:47 +01:00
|
|
|
table.fontRevision = self.fontRevision_
|
|
|
|
|
2016-03-14 23:44:25 +04:00
|
|
|
def build_name(self):
|
|
|
|
if not self.names_:
|
|
|
|
return
|
|
|
|
table = self.font.get("name")
|
|
|
|
if not table: # this only happens for unit tests
|
|
|
|
table = self.font["name"] = getTableClass("name")()
|
|
|
|
for name in self.names_:
|
|
|
|
nameID, platformID, platEncID, langID, string = name
|
|
|
|
table.setName(string, nameID, platformID, platEncID, langID)
|
|
|
|
|
2016-01-08 17:38:49 +01:00
|
|
|
def buildGDEF(self):
|
2015-12-08 17:04:21 +01:00
|
|
|
gdef = otTables.GDEF()
|
2016-01-08 17:38:49 +01:00
|
|
|
gdef.GlyphClassDef = self.buildGDEFGlyphClassDef_()
|
2016-01-19 23:25:47 +01:00
|
|
|
gdef.AttachList = \
|
2016-01-19 22:38:23 +01:00
|
|
|
otl.buildAttachList(self.attachPoints_, self.glyphMap)
|
2016-01-20 09:49:09 +01:00
|
|
|
gdef.LigCaretList = \
|
|
|
|
otl.buildLigCaretList(self.ligCaretCoords_, self.ligCaretPoints_,
|
|
|
|
self.glyphMap)
|
2016-01-08 17:38:49 +01:00
|
|
|
gdef.MarkAttachClassDef = self.buildGDEFMarkAttachClassDef_()
|
|
|
|
gdef.MarkGlyphSetsDef = self.buildGDEFMarkGlyphSetsDef_()
|
|
|
|
gdef.Version = 0x00010002 if gdef.MarkGlyphSetsDef else 1.0
|
|
|
|
if any((gdef.GlyphClassDef, gdef.AttachList, gdef.LigCaretList,
|
|
|
|
gdef.MarkAttachClassDef, gdef.MarkGlyphSetsDef)):
|
|
|
|
result = getTableClass("GDEF")()
|
|
|
|
result.table = gdef
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def buildGDEFGlyphClassDef_(self):
|
2015-12-08 22:28:02 +01:00
|
|
|
inferredGlyphClass = {}
|
|
|
|
for lookup in self.lookups_:
|
|
|
|
inferredGlyphClass.update(lookup.inferGlyphClasses())
|
2015-12-08 17:04:21 +01:00
|
|
|
for markClass in self.parseTree.markClasses.values():
|
2015-12-12 12:54:23 +01:00
|
|
|
for markClassDef in markClass.definitions:
|
|
|
|
for glyph in markClassDef.glyphSet():
|
|
|
|
inferredGlyphClass[glyph] = 3
|
2016-01-08 19:06:52 +01:00
|
|
|
if self.glyphClassDefs_:
|
|
|
|
classes = {g: c for (g, (c, _)) in self.glyphClassDefs_.items()}
|
|
|
|
else:
|
|
|
|
classes = inferredGlyphClass
|
|
|
|
if classes:
|
2016-01-08 17:38:49 +01:00
|
|
|
result = otTables.GlyphClassDef()
|
2016-01-08 19:06:52 +01:00
|
|
|
result.classDefs = classes
|
2016-01-08 08:32:47 +01:00
|
|
|
return result
|
|
|
|
else:
|
2015-12-08 17:04:21 +01:00
|
|
|
return None
|
2016-01-08 08:32:47 +01:00
|
|
|
|
2016-01-08 17:38:49 +01:00
|
|
|
def buildGDEFMarkAttachClassDef_(self):
|
|
|
|
classDefs = {g: c for g, (c, _) in self.markAttach_.items()}
|
|
|
|
if not classDefs:
|
|
|
|
return None
|
|
|
|
result = otTables.MarkAttachClassDef()
|
|
|
|
result.classDefs = classDefs
|
|
|
|
return result
|
|
|
|
|
|
|
|
def buildGDEFMarkGlyphSetsDef_(self):
|
2016-01-20 11:28:33 +01:00
|
|
|
sets = [None] * len(self.markFilterSets_)
|
|
|
|
for glyphs, id in self.markFilterSets_.items():
|
|
|
|
sets[id] = glyphs
|
|
|
|
return otl.buildMarkGlyphSetsDef(sets, self.glyphMap)
|
2016-01-08 17:38:49 +01:00
|
|
|
|
2016-01-26 12:09:52 +01:00
|
|
|
def buildLookups_(self, tag):
|
|
|
|
assert tag in ('GPOS', 'GSUB'), tag
|
2015-09-07 17:22:37 +02:00
|
|
|
for lookup in self.lookups_:
|
|
|
|
lookup.lookup_index = None
|
2016-01-26 12:09:52 +01:00
|
|
|
lookups = []
|
|
|
|
for i, lookup in enumerate(self.lookups_):
|
|
|
|
if lookup.table != tag:
|
2015-09-07 11:14:03 +02:00
|
|
|
continue
|
2016-01-26 12:09:52 +01:00
|
|
|
# TODO: https://github.com/behdad/fonttools/issues/448
|
2015-09-07 11:14:03 +02:00
|
|
|
# If multiple lookup builders would build equivalent lookups,
|
|
|
|
# emit them only once. This is quadratic in the number of lookups,
|
|
|
|
# but the checks are cheap. If performance ever becomes an issue,
|
|
|
|
# we could hash the lookup content and only compare those with
|
|
|
|
# the same hash value.
|
2016-01-26 12:09:52 +01:00
|
|
|
equivalent = None
|
|
|
|
for other in self.lookups_[:i]:
|
|
|
|
if lookup.equals(other):
|
|
|
|
equivalent = other
|
|
|
|
if equivalent is not None:
|
|
|
|
lookup.lookup_index = equivalent.lookup_index
|
2015-09-07 11:14:03 +02:00
|
|
|
continue
|
2016-01-26 12:09:52 +01:00
|
|
|
lookup.lookup_index = len(lookups)
|
|
|
|
lookups.append(lookup)
|
|
|
|
return [l.build() for l in lookups]
|
|
|
|
|
|
|
|
def makeTable(self, tag):
|
|
|
|
table = getattr(otTables, tag, None)()
|
|
|
|
table.Version = 1.0
|
|
|
|
table.ScriptList = otTables.ScriptList()
|
|
|
|
table.ScriptList.ScriptRecord = []
|
|
|
|
table.FeatureList = otTables.FeatureList()
|
|
|
|
table.FeatureList.FeatureRecord = []
|
|
|
|
table.LookupList = otTables.LookupList()
|
|
|
|
table.LookupList.Lookup = self.buildLookups_(tag)
|
2015-09-07 17:22:37 +02:00
|
|
|
|
|
|
|
# Build a table for mapping (tag, lookup_indices) to feature_index.
|
|
|
|
# For example, ('liga', (2,3,7)) --> 23.
|
|
|
|
feature_indices = {}
|
2015-09-08 15:55:54 +02:00
|
|
|
required_feature_indices = {} # ('latn', 'DEU') --> 23
|
|
|
|
scripts = {} # 'latn' --> {'DEU': [23, 24]} for feature #23,24
|
2015-09-07 17:22:37 +02:00
|
|
|
for key, lookups in sorted(self.features_.items()):
|
|
|
|
script, lang, feature_tag = key
|
|
|
|
# l.lookup_index will be None when a lookup is not needed
|
|
|
|
# for the table under construction. For example, substitution
|
|
|
|
# rules will have no lookup_index while building GPOS tables.
|
|
|
|
lookup_indices = tuple([l.lookup_index for l in lookups
|
|
|
|
if l.lookup_index is not None])
|
|
|
|
if len(lookup_indices) == 0:
|
|
|
|
continue
|
2015-09-07 21:34:10 +02:00
|
|
|
|
2015-09-07 17:22:37 +02:00
|
|
|
feature_key = (feature_tag, lookup_indices)
|
|
|
|
feature_index = feature_indices.get(feature_key)
|
|
|
|
if feature_index is None:
|
|
|
|
feature_index = len(table.FeatureList.FeatureRecord)
|
|
|
|
frec = otTables.FeatureRecord()
|
|
|
|
frec.FeatureTag = feature_tag
|
|
|
|
frec.Feature = otTables.Feature()
|
|
|
|
frec.Feature.FeatureParams = None
|
|
|
|
frec.Feature.LookupListIndex = lookup_indices
|
|
|
|
frec.Feature.LookupCount = len(lookup_indices)
|
|
|
|
table.FeatureList.FeatureRecord.append(frec)
|
|
|
|
feature_indices[feature_key] = feature_index
|
2015-09-07 21:34:10 +02:00
|
|
|
scripts.setdefault(script, {}).setdefault(lang, []).append(
|
|
|
|
feature_index)
|
2015-09-08 15:55:54 +02:00
|
|
|
if self.required_features_.get((script, lang)) == feature_tag:
|
|
|
|
required_feature_indices[(script, lang)] = feature_index
|
2015-09-07 21:34:10 +02:00
|
|
|
|
|
|
|
# Build ScriptList.
|
|
|
|
for script, lang_features in sorted(scripts.items()):
|
|
|
|
srec = otTables.ScriptRecord()
|
|
|
|
srec.ScriptTag = script
|
|
|
|
srec.Script = otTables.Script()
|
|
|
|
srec.Script.DefaultLangSys = None
|
|
|
|
srec.Script.LangSysRecord = []
|
|
|
|
for lang, feature_indices in sorted(lang_features.items()):
|
2015-09-07 22:03:50 +02:00
|
|
|
langrec = otTables.LangSysRecord()
|
|
|
|
langrec.LangSys = otTables.LangSys()
|
|
|
|
langrec.LangSys.LookupOrder = None
|
2015-09-08 15:55:54 +02:00
|
|
|
|
|
|
|
req_feature_index = \
|
|
|
|
required_feature_indices.get((script, lang))
|
|
|
|
if req_feature_index is None:
|
|
|
|
langrec.LangSys.ReqFeatureIndex = 0xFFFF
|
|
|
|
else:
|
|
|
|
langrec.LangSys.ReqFeatureIndex = req_feature_index
|
|
|
|
|
|
|
|
langrec.LangSys.FeatureIndex = [i for i in feature_indices
|
|
|
|
if i != req_feature_index]
|
|
|
|
langrec.LangSys.FeatureCount = \
|
|
|
|
len(langrec.LangSys.FeatureIndex)
|
|
|
|
|
2015-09-07 21:34:10 +02:00
|
|
|
if lang == "dflt":
|
2015-09-07 22:03:50 +02:00
|
|
|
srec.Script.DefaultLangSys = langrec.LangSys
|
2015-09-07 21:34:10 +02:00
|
|
|
else:
|
2015-09-07 22:03:50 +02:00
|
|
|
langrec.LangSysTag = lang
|
|
|
|
srec.Script.LangSysRecord.append(langrec)
|
2015-09-07 21:34:10 +02:00
|
|
|
srec.Script.LangSysCount = len(srec.Script.LangSysRecord)
|
|
|
|
table.ScriptList.ScriptRecord.append(srec)
|
2015-09-07 17:22:37 +02:00
|
|
|
|
|
|
|
table.ScriptList.ScriptCount = len(table.ScriptList.ScriptRecord)
|
|
|
|
table.FeatureList.FeatureCount = len(table.FeatureList.FeatureRecord)
|
2015-09-07 11:14:03 +02:00
|
|
|
table.LookupList.LookupCount = len(table.LookupList.Lookup)
|
2015-09-04 15:06:11 +02:00
|
|
|
return table
|
|
|
|
|
|
|
|
def add_language_system(self, location, script, language):
|
2015-09-04 22:29:06 +02:00
|
|
|
# OpenType Feature File Specification, section 4.b.i
|
|
|
|
if (script == "DFLT" and language == "dflt" and
|
|
|
|
self.default_language_systems_):
|
2015-09-04 15:06:11 +02:00
|
|
|
raise FeatureLibError(
|
|
|
|
'If "languagesystem DFLT dflt" is present, it must be '
|
|
|
|
'the first of the languagesystem statements', location)
|
2015-09-08 10:56:07 +02:00
|
|
|
if (script, language) in self.default_language_systems_:
|
|
|
|
raise FeatureLibError(
|
|
|
|
'"languagesystem %s %s" has already been specified' %
|
|
|
|
(script.strip(), language.strip()), location)
|
2015-09-04 22:29:06 +02:00
|
|
|
self.default_language_systems_.add((script, language))
|
|
|
|
|
|
|
|
def get_default_language_systems_(self):
|
|
|
|
# OpenType Feature File specification, 4.b.i. languagesystem:
|
|
|
|
# If no "languagesystem" statement is present, then the
|
|
|
|
# implementation must behave exactly as though the following
|
|
|
|
# statement were present at the beginning of the feature file:
|
|
|
|
# languagesystem DFLT dflt;
|
|
|
|
if self.default_language_systems_:
|
|
|
|
return frozenset(self.default_language_systems_)
|
|
|
|
else:
|
|
|
|
return frozenset({('DFLT', 'dflt')})
|
|
|
|
|
|
|
|
def start_feature(self, location, name):
|
|
|
|
self.language_systems = self.get_default_language_systems_()
|
2015-09-07 13:33:44 +02:00
|
|
|
self.cur_lookup_ = None
|
2015-09-07 17:22:37 +02:00
|
|
|
self.cur_feature_name_ = name
|
2016-03-13 00:16:27 +04:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2016-01-11 16:00:52 +01:00
|
|
|
if name == "aalt":
|
|
|
|
self.aalt_location_ = location
|
2015-09-04 22:29:06 +02:00
|
|
|
|
2015-09-07 11:14:03 +02:00
|
|
|
def end_feature(self):
|
2015-09-07 17:22:37 +02:00
|
|
|
assert self.cur_feature_name_ is not None
|
|
|
|
self.cur_feature_name_ = None
|
2015-09-07 11:14:03 +02:00
|
|
|
self.language_systems = None
|
|
|
|
self.cur_lookup_ = None
|
2016-03-13 00:16:27 +04:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2015-09-07 11:14:03 +02:00
|
|
|
|
2015-09-07 13:33:44 +02:00
|
|
|
def start_lookup_block(self, location, name):
|
|
|
|
if name in self.named_lookups_:
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Lookup "%s" has already been defined' % name, location)
|
2016-02-03 11:39:50 +01:00
|
|
|
if self.cur_feature_name_ == "aalt":
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Lookup blocks cannot be placed inside 'aalt' features; "
|
|
|
|
"move it out, and then refer to it with a lookup statement",
|
|
|
|
location)
|
2015-09-07 13:33:44 +02:00
|
|
|
self.cur_lookup_name_ = name
|
|
|
|
self.named_lookups_[name] = None
|
|
|
|
self.cur_lookup_ = None
|
2016-03-13 00:16:27 +04:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2015-09-07 13:33:44 +02:00
|
|
|
|
|
|
|
def end_lookup_block(self):
|
|
|
|
assert self.cur_lookup_name_ is not None
|
|
|
|
self.cur_lookup_name_ = None
|
|
|
|
self.cur_lookup_ = None
|
2016-03-13 00:16:27 +04:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2015-09-07 13:33:44 +02:00
|
|
|
|
2016-01-07 08:57:34 +01:00
|
|
|
def add_lookup_call(self, lookup_name):
|
|
|
|
assert lookup_name in self.named_lookups_, lookup_name
|
|
|
|
self.cur_lookup_ = None
|
|
|
|
lookup = self.named_lookups_[lookup_name]
|
|
|
|
self.add_lookup_to_feature_(lookup, self.cur_feature_name_)
|
|
|
|
|
2016-01-11 18:01:47 +01:00
|
|
|
def set_font_revision(self, location, revision):
|
|
|
|
self.fontRevision_ = revision
|
|
|
|
|
2015-09-08 15:55:54 +02:00
|
|
|
def set_language(self, location, language, include_default, required):
|
2015-09-07 21:34:10 +02:00
|
|
|
assert(len(language) == 4)
|
2015-09-08 12:18:03 +02:00
|
|
|
if self.cur_feature_name_ in ('aalt', 'size'):
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Language statements are not allowed "
|
|
|
|
"within \"feature %s\"" % self.cur_feature_name_, location)
|
2015-09-07 11:14:03 +02:00
|
|
|
self.cur_lookup_ = None
|
2015-09-04 22:29:06 +02:00
|
|
|
if include_default:
|
|
|
|
langsys = set(self.get_default_language_systems_())
|
|
|
|
else:
|
|
|
|
langsys = set()
|
|
|
|
langsys.add((self.script_, language))
|
|
|
|
self.language_systems = frozenset(langsys)
|
2015-09-08 15:55:54 +02:00
|
|
|
if required:
|
|
|
|
key = (self.script_, language)
|
|
|
|
if key in self.required_features_:
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Language %s (script %s) has already "
|
|
|
|
"specified feature %s as its required feature" % (
|
|
|
|
language.strip(), self.script_.strip(),
|
|
|
|
self.required_features_[key].strip()),
|
|
|
|
location)
|
|
|
|
self.required_features_[key] = self.cur_feature_name_
|
2015-09-04 22:29:06 +02:00
|
|
|
|
2015-12-10 19:17:11 +01:00
|
|
|
def getMarkAttachClass_(self, location, glyphs):
|
|
|
|
id = self.markAttachClassID_.get(glyphs)
|
|
|
|
if id is not None:
|
|
|
|
return id
|
|
|
|
id = len(self.markAttachClassID_) + 1
|
|
|
|
self.markAttachClassID_[glyphs] = id
|
|
|
|
for glyph in glyphs:
|
|
|
|
if glyph in self.markAttach_:
|
|
|
|
_, loc = self.markAttach_[glyph]
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Glyph %s already has been assigned "
|
|
|
|
"a MarkAttachmentType at %s:%d:%d" % (
|
|
|
|
glyph, loc[0], loc[1], loc[2]),
|
|
|
|
location)
|
|
|
|
self.markAttach_[glyph] = (id, location)
|
|
|
|
return id
|
|
|
|
|
|
|
|
def getMarkFilterSet_(self, location, glyphs):
|
|
|
|
id = self.markFilterSets_.get(glyphs)
|
|
|
|
if id is not None:
|
|
|
|
return id
|
|
|
|
id = len(self.markFilterSets_)
|
|
|
|
self.markFilterSets_[glyphs] = id
|
|
|
|
return id
|
|
|
|
|
|
|
|
def set_lookup_flag(self, location, value, markAttach, markFilter):
|
|
|
|
value = value & 0xFF
|
|
|
|
if markAttach:
|
|
|
|
markAttachClass = self.getMarkAttachClass_(location, markAttach)
|
|
|
|
value = value | (markAttachClass << 8)
|
|
|
|
if markFilter:
|
|
|
|
markFilterSet = self.getMarkFilterSet_(location, markFilter)
|
|
|
|
value = value | 0x10
|
|
|
|
self.lookupflag_markFilterSet_ = markFilterSet
|
|
|
|
else:
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
|
|
|
self.lookupflag_ = value
|
|
|
|
|
2015-09-04 22:29:06 +02:00
|
|
|
def set_script(self, location, script):
|
2015-09-08 12:18:03 +02:00
|
|
|
if self.cur_feature_name_ in ('aalt', 'size'):
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Script statements are not allowed "
|
|
|
|
"within \"feature %s\"" % self.cur_feature_name_, location)
|
2015-09-07 11:14:03 +02:00
|
|
|
self.cur_lookup_ = None
|
2015-09-04 22:29:06 +02:00
|
|
|
self.script_ = script
|
2015-12-10 19:17:11 +01:00
|
|
|
self.lookupflag_ = 0
|
|
|
|
self.lookupflag_markFilterSet_ = None
|
2015-09-08 15:55:54 +02:00
|
|
|
self.set_language(location, "dflt",
|
2016-02-04 10:31:33 +01:00
|
|
|
include_default=False, required=False)
|
2015-09-07 11:14:03 +02:00
|
|
|
|
2015-12-03 13:05:42 +01:00
|
|
|
def find_lookup_builders_(self, lookups):
|
|
|
|
"""Helper for building chain contextual substitutions
|
|
|
|
|
|
|
|
Given a list of lookup names, finds the LookupBuilder for each name.
|
|
|
|
If an input name is None, it gets mapped to a None LookupBuilder.
|
|
|
|
"""
|
2015-11-30 15:02:09 +01:00
|
|
|
lookup_builders = []
|
|
|
|
for lookup in lookups:
|
|
|
|
if lookup is not None:
|
|
|
|
lookup_builders.append(self.named_lookups_.get(lookup.name))
|
|
|
|
else:
|
|
|
|
lookup_builders.append(None)
|
2015-12-03 13:05:42 +01:00
|
|
|
return lookup_builders
|
|
|
|
|
2016-01-08 08:32:47 +01:00
|
|
|
def add_attach_points(self, location, glyphs, contourPoints):
|
|
|
|
for glyph in glyphs:
|
|
|
|
self.attachPoints_.setdefault(glyph, set()).update(contourPoints)
|
|
|
|
|
2015-12-09 22:56:24 +01:00
|
|
|
def add_chain_context_pos(self, location, prefix, glyphs, suffix, lookups):
|
2015-12-09 23:53:20 +01:00
|
|
|
lookup = self.get_lookup_(location, ChainContextPosBuilder)
|
|
|
|
lookup.rules.append((prefix, glyphs, suffix,
|
|
|
|
self.find_lookup_builders_(lookups)))
|
2015-12-09 22:56:24 +01:00
|
|
|
|
2015-12-09 13:58:05 +01:00
|
|
|
def add_chain_context_subst(self, location,
|
2015-12-09 22:56:24 +01:00
|
|
|
prefix, glyphs, suffix, lookups):
|
2015-11-30 15:02:09 +01:00
|
|
|
lookup = self.get_lookup_(location, ChainContextSubstBuilder)
|
2015-12-09 22:56:24 +01:00
|
|
|
lookup.substitutions.append((prefix, glyphs, suffix,
|
2015-12-03 13:05:42 +01:00
|
|
|
self.find_lookup_builders_(lookups)))
|
2015-11-30 15:02:09 +01:00
|
|
|
|
2016-01-07 11:32:54 +01:00
|
|
|
def add_alternate_subst(self, location,
|
|
|
|
prefix, glyph, suffix, replacement):
|
2016-02-03 17:38:38 +01:00
|
|
|
if self.cur_feature_name_ == "aalt":
|
|
|
|
alts = self.aalt_alternates_.setdefault(glyph, set())
|
|
|
|
alts.update(replacement)
|
|
|
|
return
|
2016-01-07 11:32:54 +01:00
|
|
|
if prefix or suffix:
|
|
|
|
chain = self.get_lookup_(location, ChainContextSubstBuilder)
|
2016-02-04 14:34:45 +01:00
|
|
|
lookup = self.get_chained_lookup_(location, AlternateSubstBuilder)
|
2016-01-07 11:32:54 +01:00
|
|
|
chain.substitutions.append((prefix, [glyph], suffix, [lookup]))
|
|
|
|
else:
|
|
|
|
lookup = self.get_lookup_(location, AlternateSubstBuilder)
|
2015-09-07 11:14:03 +02:00
|
|
|
if glyph in lookup.alternates:
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Already defined alternates for glyph "%s"' % glyph,
|
|
|
|
location)
|
2016-01-07 11:32:54 +01:00
|
|
|
lookup.alternates[glyph] = replacement
|
2015-09-07 11:14:03 +02:00
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def add_feature_reference(self, location, featureName):
|
|
|
|
if self.cur_feature_name_ != "aalt":
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Feature references are only allowed inside "feature aalt"',
|
|
|
|
location)
|
|
|
|
self.aalt_features_.append((location, featureName))
|
|
|
|
|
2016-01-07 10:31:13 +01:00
|
|
|
def add_ligature_subst(self, location,
|
2016-02-04 15:31:29 +01:00
|
|
|
prefix, glyphs, suffix, replacement, forceChain):
|
|
|
|
if prefix or suffix or forceChain:
|
2016-01-07 10:31:13 +01:00
|
|
|
chain = self.get_lookup_(location, ChainContextSubstBuilder)
|
2016-02-04 14:22:39 +01:00
|
|
|
lookup = self.get_chained_lookup_(location, LigatureSubstBuilder)
|
2016-01-07 10:31:13 +01:00
|
|
|
chain.substitutions.append((prefix, glyphs, suffix, [lookup]))
|
|
|
|
else:
|
|
|
|
lookup = self.get_lookup_(location, LigatureSubstBuilder)
|
|
|
|
|
|
|
|
# OpenType feature file syntax, section 5.d, "Ligature substitution":
|
|
|
|
# "Since the OpenType specification does not allow ligature
|
|
|
|
# substitutions to be specified on target sequences that contain
|
|
|
|
# glyph classes, the implementation software will enumerate
|
|
|
|
# all specific glyph sequences if glyph classes are detected"
|
|
|
|
for g in sorted(itertools.product(*glyphs)):
|
|
|
|
lookup.ligatures[g] = replacement
|
2015-09-07 16:10:13 +02:00
|
|
|
|
2016-01-06 17:33:34 +01:00
|
|
|
def add_multiple_subst(self, location,
|
|
|
|
prefix, glyph, suffix, replacements):
|
|
|
|
if prefix or suffix:
|
2016-02-04 14:46:22 +01:00
|
|
|
chain = self.get_lookup_(location, ChainContextSubstBuilder)
|
2016-01-06 17:33:34 +01:00
|
|
|
sub = self.get_chained_lookup_(location, MultipleSubstBuilder)
|
|
|
|
sub.mapping[glyph] = replacements
|
2016-02-04 14:46:22 +01:00
|
|
|
chain.substitutions.append((prefix, [{glyph}], suffix, [sub]))
|
2016-01-06 17:33:34 +01:00
|
|
|
return
|
2015-09-10 15:28:02 +02:00
|
|
|
lookup = self.get_lookup_(location, MultipleSubstBuilder)
|
|
|
|
if glyph in lookup.mapping:
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Already defined substitution for glyph "%s"' % glyph,
|
|
|
|
location)
|
|
|
|
lookup.mapping[glyph] = replacements
|
2015-09-08 12:05:44 +02:00
|
|
|
|
2015-12-09 13:58:05 +01:00
|
|
|
def add_reverse_chain_single_subst(self, location, old_prefix,
|
|
|
|
old_suffix, mapping):
|
2015-12-03 13:05:42 +01:00
|
|
|
lookup = self.get_lookup_(location, ReverseChainSingleSubstBuilder)
|
|
|
|
lookup.substitutions.append((old_prefix, old_suffix, mapping))
|
|
|
|
|
2016-02-04 16:45:05 +01:00
|
|
|
def add_single_subst(self, location, prefix, suffix, mapping, forceChain):
|
2016-02-03 17:38:38 +01:00
|
|
|
if self.cur_feature_name_ == "aalt":
|
|
|
|
for (from_glyph, to_glyph) in mapping.items():
|
|
|
|
alts = self.aalt_alternates_.setdefault(from_glyph, set())
|
|
|
|
alts.add(to_glyph)
|
|
|
|
return
|
2016-02-04 16:45:05 +01:00
|
|
|
if prefix or suffix or forceChain:
|
2016-02-05 15:12:07 +01:00
|
|
|
self.add_single_subst_chained_(location, prefix, suffix, mapping)
|
2016-01-06 16:15:26 +01:00
|
|
|
return
|
2015-09-08 10:33:07 +02:00
|
|
|
lookup = self.get_lookup_(location, SingleSubstBuilder)
|
|
|
|
for (from_glyph, to_glyph) in mapping.items():
|
|
|
|
if from_glyph in lookup.mapping:
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Already defined rule for replacing glyph "%s" by "%s"' %
|
|
|
|
(from_glyph, lookup.mapping[from_glyph]),
|
|
|
|
location)
|
|
|
|
lookup.mapping[from_glyph] = to_glyph
|
|
|
|
|
2016-02-05 15:12:07 +01:00
|
|
|
def find_chainable_SingleSubst_(self, chain, glyphs):
|
|
|
|
"""Helper for add_single_subst_chained_()"""
|
|
|
|
for _, _, _, substitutions in chain.substitutions:
|
|
|
|
for sub in substitutions:
|
|
|
|
if (isinstance(sub, SingleSubstBuilder) and
|
|
|
|
not any(g in glyphs for g in sub.mapping.keys())):
|
|
|
|
return sub
|
|
|
|
return None
|
|
|
|
|
|
|
|
def add_single_subst_chained_(self, location, prefix, suffix, mapping):
|
|
|
|
# https://github.com/behdad/fonttools/issues/512
|
|
|
|
chain = self.get_lookup_(location, ChainContextSubstBuilder)
|
|
|
|
sub = self.find_chainable_SingleSubst_(chain, set(mapping.keys()))
|
|
|
|
if sub is None:
|
|
|
|
sub = self.get_chained_lookup_(location, SingleSubstBuilder)
|
|
|
|
sub.mapping.update(mapping)
|
|
|
|
chain.substitutions.append((prefix, [mapping.keys()], suffix, [sub]))
|
|
|
|
|
2015-12-09 12:59:20 +01:00
|
|
|
def add_cursive_pos(self, location, glyphclass, entryAnchor, exitAnchor):
|
|
|
|
lookup = self.get_lookup_(location, CursivePosBuilder)
|
2015-12-07 23:56:08 +01:00
|
|
|
lookup.add_attachment(
|
|
|
|
location, glyphclass,
|
2016-01-14 13:08:26 +01:00
|
|
|
makeOpenTypeAnchor(entryAnchor),
|
|
|
|
makeOpenTypeAnchor(exitAnchor))
|
2015-12-07 23:56:08 +01:00
|
|
|
|
2015-12-09 16:51:15 +01:00
|
|
|
def add_marks_(self, location, lookupBuilder, marks):
|
|
|
|
"""Helper for add_mark_{base,liga,mark}_pos."""
|
|
|
|
for _, markClass in marks:
|
2015-12-12 12:54:23 +01:00
|
|
|
for markClassDef in markClass.definitions:
|
|
|
|
for mark in markClassDef.glyphs.glyphSet():
|
|
|
|
if mark not in lookupBuilder.marks:
|
2016-01-14 13:08:26 +01:00
|
|
|
otMarkAnchor = makeOpenTypeAnchor(markClassDef.anchor)
|
2015-12-12 12:54:23 +01:00
|
|
|
lookupBuilder.marks[mark] = (
|
|
|
|
markClass.name, otMarkAnchor)
|
2015-12-09 16:51:15 +01:00
|
|
|
|
2015-12-09 12:59:20 +01:00
|
|
|
def add_mark_base_pos(self, location, bases, marks):
|
|
|
|
builder = self.get_lookup_(location, MarkBasePosBuilder)
|
2015-12-09 16:51:15 +01:00
|
|
|
self.add_marks_(location, builder, marks)
|
2015-12-08 22:28:02 +01:00
|
|
|
for baseAnchor, markClass in marks:
|
2016-01-14 13:08:26 +01:00
|
|
|
otBaseAnchor = makeOpenTypeAnchor(baseAnchor)
|
2015-12-09 16:51:15 +01:00
|
|
|
for base in bases:
|
|
|
|
builder.bases.setdefault(base, {})[markClass.name] = (
|
|
|
|
otBaseAnchor)
|
|
|
|
|
|
|
|
def add_mark_lig_pos(self, location, ligatures, components):
|
|
|
|
builder = self.get_lookup_(location, MarkLigPosBuilder)
|
|
|
|
componentAnchors = []
|
|
|
|
for marks in components:
|
|
|
|
anchors = {}
|
|
|
|
self.add_marks_(location, builder, marks)
|
|
|
|
for ligAnchor, markClass in marks:
|
2016-01-14 13:08:26 +01:00
|
|
|
anchors[markClass.name] = makeOpenTypeAnchor(ligAnchor)
|
2015-12-09 16:51:15 +01:00
|
|
|
componentAnchors.append(anchors)
|
|
|
|
for glyph in ligatures:
|
|
|
|
builder.ligatures[glyph] = componentAnchors
|
2015-12-08 19:04:42 +01:00
|
|
|
|
2015-12-09 17:56:47 +01:00
|
|
|
def add_mark_mark_pos(self, location, baseMarks, marks):
|
|
|
|
builder = self.get_lookup_(location, MarkMarkPosBuilder)
|
|
|
|
self.add_marks_(location, builder, marks)
|
|
|
|
for baseAnchor, markClass in marks:
|
2016-01-14 13:08:26 +01:00
|
|
|
otBaseAnchor = makeOpenTypeAnchor(baseAnchor)
|
2015-12-09 17:56:47 +01:00
|
|
|
for baseMark in baseMarks:
|
|
|
|
builder.baseMarks.setdefault(baseMark, {})[markClass.name] = (
|
|
|
|
otBaseAnchor)
|
|
|
|
|
2015-12-21 16:06:59 +01:00
|
|
|
def add_class_pair_pos(self, location, glyphclass1, value1,
|
|
|
|
glyphclass2, value2):
|
2016-02-02 18:23:37 +01:00
|
|
|
lookup = self.get_lookup_(location, PairPosBuilder)
|
|
|
|
lookup.addClassPair(location, glyphclass1, value1, glyphclass2, value2)
|
2015-12-21 16:06:59 +01:00
|
|
|
|
|
|
|
def add_specific_pair_pos(self, location, glyph1, value1, glyph2, value2):
|
2016-02-02 18:23:37 +01:00
|
|
|
lookup = self.get_lookup_(location, PairPosBuilder)
|
|
|
|
lookup.addGlyphPair(location, glyph1, value1, glyph2, value2)
|
2015-12-07 17:18:18 +01:00
|
|
|
|
2016-02-09 08:58:18 +01:00
|
|
|
def add_single_pos(self, location, prefix, suffix, pos, forceChain):
|
|
|
|
if prefix or suffix or forceChain:
|
2016-02-09 15:38:18 +01:00
|
|
|
self.add_single_pos_chained_(location, prefix, suffix, pos)
|
|
|
|
else:
|
|
|
|
lookup = self.get_lookup_(location, SinglePosBuilder)
|
2016-01-25 16:27:18 +01:00
|
|
|
for glyphs, value in pos:
|
|
|
|
for glyph in glyphs:
|
2016-02-09 15:38:18 +01:00
|
|
|
lookup.add_pos(location, glyph, value)
|
|
|
|
|
|
|
|
def add_single_pos_chained_(self, location, prefix, suffix, pos):
|
|
|
|
chain = self.get_lookup_(location, ChainContextPosBuilder)
|
|
|
|
sub = self.get_chained_lookup_(location, SinglePosBuilder)
|
|
|
|
subs = []
|
2016-01-25 16:27:18 +01:00
|
|
|
for glyphs, value in pos:
|
2016-02-09 15:38:18 +01:00
|
|
|
if value is None:
|
|
|
|
subs.append(None)
|
|
|
|
continue
|
|
|
|
if not glyphs.isdisjoint(sub.mapping.keys()):
|
|
|
|
sub = self.get_chained_lookup_(location, SinglePosBuilder)
|
2016-01-25 16:27:18 +01:00
|
|
|
for glyph in glyphs:
|
2016-02-09 15:38:18 +01:00
|
|
|
sub.add_pos(location, glyph, value)
|
|
|
|
subs.append(sub)
|
|
|
|
assert len(pos) == len(subs), (pos, subs)
|
|
|
|
chain.rules.append(
|
|
|
|
(prefix, [g for g, v in pos], suffix, subs))
|
2015-12-04 11:16:43 +01:00
|
|
|
|
2016-01-08 19:06:52 +01:00
|
|
|
def setGlyphClass_(self, location, glyph, glyphClass):
|
|
|
|
oldClass, oldLocation = self.glyphClassDefs_.get(glyph, (None, None))
|
|
|
|
if oldClass and oldClass != glyphClass:
|
|
|
|
raise FeatureLibError(
|
|
|
|
"Glyph %s was assigned to a different class at %s:%s:%s" %
|
|
|
|
(glyph, oldLocation[0], oldLocation[1], oldLocation[2]),
|
|
|
|
location)
|
|
|
|
self.glyphClassDefs_[glyph] = (glyphClass, location)
|
|
|
|
|
|
|
|
def add_glyphClassDef(self, location, baseGlyphs, ligatureGlyphs,
|
|
|
|
markGlyphs, componentGlyphs):
|
|
|
|
for glyph in baseGlyphs:
|
|
|
|
self.setGlyphClass_(location, glyph, 1)
|
|
|
|
for glyph in ligatureGlyphs:
|
|
|
|
self.setGlyphClass_(location, glyph, 2)
|
|
|
|
for glyph in markGlyphs:
|
|
|
|
self.setGlyphClass_(location, glyph, 3)
|
|
|
|
for glyph in componentGlyphs:
|
|
|
|
self.setGlyphClass_(location, glyph, 4)
|
2016-01-08 17:11:29 +01:00
|
|
|
|
2016-01-07 17:22:31 +01:00
|
|
|
def add_ligatureCaretByIndex_(self, location, glyphs, carets):
|
|
|
|
for glyph in glyphs:
|
2016-01-20 09:49:09 +01:00
|
|
|
self.ligCaretPoints_.setdefault(glyph, set()).update(carets)
|
2016-01-07 17:22:31 +01:00
|
|
|
|
2016-01-07 16:39:35 +01:00
|
|
|
def add_ligatureCaretByPos_(self, location, glyphs, carets):
|
|
|
|
for glyph in glyphs:
|
2016-01-20 09:49:09 +01:00
|
|
|
self.ligCaretCoords_.setdefault(glyph, set()).update(carets)
|
2016-01-07 16:39:35 +01:00
|
|
|
|
2016-03-14 23:44:25 +04:00
|
|
|
def add_name_record(self, location, nameID, platformID, platEncID,
|
|
|
|
langID, string):
|
|
|
|
self.names_.append([nameID, platformID, platEncID, langID, string])
|
|
|
|
|
2015-12-04 11:16:43 +01:00
|
|
|
|
2016-01-14 13:08:26 +01:00
|
|
|
def makeOpenTypeAnchor(anchor):
|
2015-12-07 23:56:08 +01:00
|
|
|
"""ast.Anchor --> otTables.Anchor"""
|
|
|
|
if anchor is None:
|
|
|
|
return None
|
2016-01-14 13:08:26 +01:00
|
|
|
deviceX, deviceY = None, None
|
2015-12-07 23:56:08 +01:00
|
|
|
if anchor.xDeviceTable is not None:
|
2016-01-22 19:45:56 +01:00
|
|
|
deviceX = otl.buildDevice(dict(anchor.xDeviceTable))
|
2015-12-07 23:56:08 +01:00
|
|
|
if anchor.yDeviceTable is not None:
|
2016-01-22 19:45:56 +01:00
|
|
|
deviceY = otl.buildDevice(dict(anchor.yDeviceTable))
|
2016-01-14 17:15:52 +01:00
|
|
|
return otl.buildAnchor(anchor.x, anchor.y, anchor.contourpoint,
|
|
|
|
deviceX, deviceY)
|
2015-12-07 23:56:08 +01:00
|
|
|
|
|
|
|
|
2016-01-14 16:25:28 +01:00
|
|
|
_VALUEREC_ATTRS = {
|
|
|
|
name[0].lower() + name[1:]: (name, isDevice)
|
|
|
|
for _, name, isDevice, _ in otBase.valueRecordFormat
|
|
|
|
if not name.startswith("Reserved")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-04 11:16:43 +01:00
|
|
|
def makeOpenTypeValueRecord(v):
|
|
|
|
"""ast.ValueRecord --> (otBase.ValueRecord, int ValueFormat)"""
|
2015-12-07 21:26:58 +01:00
|
|
|
if v is None:
|
|
|
|
return None, 0
|
2015-12-04 15:49:04 +01:00
|
|
|
|
2016-01-14 16:25:28 +01:00
|
|
|
vr = {}
|
|
|
|
for astName, (otName, isDevice) in _VALUEREC_ATTRS.items():
|
|
|
|
val = getattr(v, astName, None)
|
|
|
|
if val:
|
2016-01-22 19:45:56 +01:00
|
|
|
vr[otName] = otl.buildDevice(dict(val)) if isDevice else val
|
2015-12-07 11:47:55 +01:00
|
|
|
|
2016-01-19 17:05:17 +01:00
|
|
|
valRec = otl.buildValue(vr)
|
|
|
|
return valRec, valRec.getFormat()
|
2015-12-04 11:16:43 +01:00
|
|
|
|
2015-09-07 11:14:03 +02:00
|
|
|
|
|
|
|
class LookupBuilder(object):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location, table, lookup_type):
|
2015-12-04 11:04:37 +01:00
|
|
|
self.font = font
|
2016-01-14 17:10:45 +01:00
|
|
|
self.glyphMap = font.getReverseGlyphMap()
|
2015-09-07 11:14:03 +02:00
|
|
|
self.location = location
|
|
|
|
self.table, self.lookup_type = table, lookup_type
|
2015-12-10 19:17:11 +01:00
|
|
|
self.lookupflag = 0
|
|
|
|
self.markFilterSet = None
|
2015-09-07 17:22:37 +02:00
|
|
|
self.lookup_index = None # assigned when making final tables
|
2015-09-07 11:14:03 +02:00
|
|
|
assert table in ('GPOS', 'GSUB')
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (isinstance(other, self.__class__) and
|
|
|
|
self.table == other.table and
|
2015-12-10 19:17:11 +01:00
|
|
|
self.lookupflag == other.lookupflag and
|
|
|
|
self.markFilterSet == other.markFilterSet)
|
2015-09-07 11:14:03 +02:00
|
|
|
|
2015-12-08 22:28:02 +01:00
|
|
|
def inferGlyphClasses(self):
|
|
|
|
"""Infers glyph glasses for the GDEF table, such as {"cedilla":3}."""
|
|
|
|
return {}
|
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def getAlternateGlyphs(self):
|
|
|
|
"""Helper for building 'aalt' features."""
|
|
|
|
return {}
|
|
|
|
|
2015-12-09 12:51:01 +01:00
|
|
|
def buildLookup_(self, subtables):
|
2016-01-20 19:24:23 +01:00
|
|
|
return otl.buildLookup(subtables, self.lookupflag, self.markFilterSet)
|
2015-12-09 12:51:01 +01:00
|
|
|
|
2015-12-09 16:51:15 +01:00
|
|
|
def buildMarkClasses_(self, marks):
|
|
|
|
"""{"cedilla": ("BOTTOM", ast.Anchor), ...} --> {"BOTTOM":0, "TOP":1}
|
|
|
|
|
|
|
|
Helper for MarkBasePostBuilder, MarkLigPosBuilder, and
|
|
|
|
MarkMarkPosBuilder. Seems to return the same numeric IDs
|
|
|
|
for mark classes as the AFDKO makeotf tool.
|
|
|
|
"""
|
|
|
|
ids = {}
|
|
|
|
for mark in sorted(marks.keys(), key=self.font.getGlyphID):
|
|
|
|
markClassName, _markAnchor = marks[mark]
|
|
|
|
if markClassName not in ids:
|
|
|
|
ids[markClassName] = len(ids)
|
|
|
|
return ids
|
|
|
|
|
2015-12-04 11:04:37 +01:00
|
|
|
def setBacktrackCoverage_(self, prefix, subtable):
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.BacktrackGlyphCount = len(prefix)
|
|
|
|
subtable.BacktrackCoverage = []
|
|
|
|
for p in reversed(prefix):
|
2016-01-14 17:15:52 +01:00
|
|
|
coverage = otl.buildCoverage(p, self.glyphMap)
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.BacktrackCoverage.append(coverage)
|
|
|
|
|
2015-12-04 11:04:37 +01:00
|
|
|
def setLookAheadCoverage_(self, suffix, subtable):
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.LookAheadGlyphCount = len(suffix)
|
|
|
|
subtable.LookAheadCoverage = []
|
|
|
|
for s in suffix:
|
2016-01-14 17:15:52 +01:00
|
|
|
coverage = otl.buildCoverage(s, self.glyphMap)
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.LookAheadCoverage.append(coverage)
|
|
|
|
|
2015-12-04 11:04:37 +01:00
|
|
|
def setInputCoverage_(self, glyphs, subtable):
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.InputGlyphCount = len(glyphs)
|
|
|
|
subtable.InputCoverage = []
|
|
|
|
for g in glyphs:
|
2016-01-14 17:15:52 +01:00
|
|
|
coverage = otl.buildCoverage(g, self.glyphMap)
|
2015-12-03 13:05:42 +01:00
|
|
|
subtable.InputCoverage.append(coverage)
|
|
|
|
|
2015-09-07 11:14:03 +02:00
|
|
|
|
|
|
|
class AlternateSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 3)
|
2015-09-07 11:14:03 +02:00
|
|
|
self.alternates = {}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.alternates == other.alternates)
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-22 19:32:45 +01:00
|
|
|
subtable = otl.buildAlternateSubstSubtable(self.alternates)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_([subtable])
|
2015-11-30 15:02:09 +01:00
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def getAlternateGlyphs(self):
|
|
|
|
return self.alternates
|
|
|
|
|
2015-11-30 15:02:09 +01:00
|
|
|
|
2015-12-09 23:53:20 +01:00
|
|
|
class ChainContextPosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 8)
|
2015-12-09 23:53:20 +01:00
|
|
|
self.rules = [] # (prefix, input, suffix, lookups)
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.rules == other.rules)
|
|
|
|
|
|
|
|
def build(self):
|
|
|
|
subtables = []
|
|
|
|
for (prefix, glyphs, suffix, lookups) in self.rules:
|
|
|
|
st = otTables.ChainContextPos()
|
|
|
|
subtables.append(st)
|
|
|
|
st.Format = 3
|
|
|
|
self.setBacktrackCoverage_(prefix, st)
|
|
|
|
self.setLookAheadCoverage_(suffix, st)
|
|
|
|
self.setInputCoverage_(glyphs, st)
|
|
|
|
|
|
|
|
st.PosCount = len([l for l in lookups if l is not None])
|
|
|
|
st.PosLookupRecord = []
|
|
|
|
for sequenceIndex, l in enumerate(lookups):
|
|
|
|
if l is not None:
|
|
|
|
rec = otTables.PosLookupRecord()
|
|
|
|
rec.SequenceIndex = sequenceIndex
|
|
|
|
rec.LookupListIndex = l.lookup_index
|
|
|
|
st.PosLookupRecord.append(rec)
|
|
|
|
return self.buildLookup_(subtables)
|
|
|
|
|
|
|
|
|
2015-11-30 15:02:09 +01:00
|
|
|
class ChainContextSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 6)
|
2015-11-30 15:02:09 +01:00
|
|
|
self.substitutions = [] # (prefix, input, suffix, lookups)
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.substitutions == other.substitutions)
|
|
|
|
|
|
|
|
def build(self):
|
2015-12-09 12:51:01 +01:00
|
|
|
subtables = []
|
2015-11-30 15:02:09 +01:00
|
|
|
for (prefix, input, suffix, lookups) in self.substitutions:
|
|
|
|
st = otTables.ChainContextSubst()
|
2015-12-09 12:51:01 +01:00
|
|
|
subtables.append(st)
|
2015-11-30 15:02:09 +01:00
|
|
|
st.Format = 3
|
2015-12-03 13:05:42 +01:00
|
|
|
self.setBacktrackCoverage_(prefix, st)
|
|
|
|
self.setLookAheadCoverage_(suffix, st)
|
|
|
|
self.setInputCoverage_(input, st)
|
2015-11-30 15:02:09 +01:00
|
|
|
|
|
|
|
st.SubstCount = len([l for l in lookups if l is not None])
|
|
|
|
st.SubstLookupRecord = []
|
|
|
|
for sequenceIndex, l in enumerate(lookups):
|
|
|
|
if l is not None:
|
|
|
|
rec = otTables.SubstLookupRecord()
|
|
|
|
rec.SequenceIndex = sequenceIndex
|
|
|
|
rec.LookupListIndex = l.lookup_index
|
|
|
|
st.SubstLookupRecord.append(rec)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_(subtables)
|
2015-09-07 16:10:13 +02:00
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def getAlternateGlyphs(self):
|
|
|
|
result = {}
|
|
|
|
for (_prefix, _input, _suffix, lookups) in self.substitutions:
|
|
|
|
for lookup in lookups:
|
|
|
|
alts = lookup.getAlternateGlyphs()
|
|
|
|
for glyph, replacements in alts.items():
|
|
|
|
result.setdefault(glyph, set()).update(replacements)
|
|
|
|
return result
|
|
|
|
|
2015-09-07 16:10:13 +02:00
|
|
|
|
|
|
|
class LigatureSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 4)
|
2015-09-07 16:10:13 +02:00
|
|
|
self.ligatures = {} # {('f','f','i'): 'f_f_i'}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.ligatures == other.ligatures)
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-22 19:32:45 +01:00
|
|
|
subtable = otl.buildLigatureSubstSubtable(self.ligatures)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_([subtable])
|
2015-09-08 10:33:07 +02:00
|
|
|
|
|
|
|
|
2015-09-10 15:28:02 +02:00
|
|
|
class MultipleSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 2)
|
2015-09-10 15:28:02 +02:00
|
|
|
self.mapping = {}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.mapping == other.mapping)
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-22 19:32:45 +01:00
|
|
|
subtable = otl.buildMultipleSubstSubtable(self.mapping)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_([subtable])
|
2015-09-10 15:28:02 +02:00
|
|
|
|
|
|
|
|
2015-12-09 12:59:20 +01:00
|
|
|
class CursivePosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 3)
|
2015-12-07 23:56:08 +01:00
|
|
|
self.attachments = {}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.attachments == other.attachments)
|
|
|
|
|
|
|
|
def add_attachment(self, location, glyphs, entryAnchor, exitAnchor):
|
|
|
|
for glyph in glyphs:
|
2016-01-14 17:54:47 +01:00
|
|
|
self.attachments[glyph] = (entryAnchor, exitAnchor)
|
2015-12-07 23:56:08 +01:00
|
|
|
|
|
|
|
def build(self):
|
2016-01-22 19:38:20 +01:00
|
|
|
st = otl.buildCursivePosSubtable(self.attachments, self.glyphMap)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_([st])
|
2015-12-07 23:56:08 +01:00
|
|
|
|
|
|
|
|
2015-12-09 12:59:20 +01:00
|
|
|
class MarkBasePosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 4)
|
2015-12-08 22:28:02 +01:00
|
|
|
self.marks = {} # glyphName -> (markClassName, anchor)
|
|
|
|
self.bases = {} # glyphName -> {markClassName: anchor}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.marks == other.marks and
|
|
|
|
self.bases == other.bases)
|
|
|
|
|
|
|
|
def inferGlyphClasses(self):
|
2015-12-09 16:51:15 +01:00
|
|
|
result = {glyph: 1 for glyph in self.bases}
|
|
|
|
result.update({glyph: 3 for glyph in self.marks})
|
2015-12-08 22:28:02 +01:00
|
|
|
return result
|
2015-12-08 19:04:42 +01:00
|
|
|
|
|
|
|
def build(self):
|
2016-01-21 16:23:55 +01:00
|
|
|
markClasses = self.buildMarkClasses_(self.marks)
|
|
|
|
marks = {mark: (markClasses[mc], anchor)
|
|
|
|
for mark, (mc, anchor) in self.marks.items()}
|
2016-01-21 17:23:36 +01:00
|
|
|
bases = {}
|
|
|
|
for glyph, anchors in self.bases.items():
|
|
|
|
bases[glyph] = {markClasses[mc]: anchor
|
|
|
|
for (mc, anchor) in anchors.items()}
|
2016-01-22 11:53:34 +01:00
|
|
|
subtables = otl.buildMarkBasePos(marks, bases, self.glyphMap)
|
|
|
|
return self.buildLookup_(subtables)
|
2015-12-08 22:28:02 +01:00
|
|
|
|
2015-12-09 16:51:15 +01:00
|
|
|
|
|
|
|
class MarkLigPosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 5)
|
2015-12-09 16:51:15 +01:00
|
|
|
self.marks = {} # glyphName -> (markClassName, anchor)
|
|
|
|
self.ligatures = {} # glyphName -> [{markClassName: anchor}, ...]
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.marks == other.marks and
|
|
|
|
self.ligatures == other.ligatures)
|
|
|
|
|
|
|
|
def inferGlyphClasses(self):
|
|
|
|
result = {glyph: 2 for glyph in self.ligatures}
|
|
|
|
result.update({glyph: 3 for glyph in self.marks})
|
|
|
|
return result
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-21 15:16:55 +01:00
|
|
|
markClasses = self.buildMarkClasses_(self.marks)
|
2016-01-21 16:23:55 +01:00
|
|
|
marks = {mark: (markClasses[mc], anchor)
|
|
|
|
for mark, (mc, anchor) in self.marks.items()}
|
2016-01-22 12:45:29 +01:00
|
|
|
ligs = {}
|
|
|
|
for lig, components in self.ligatures.items():
|
|
|
|
ligs[lig] = []
|
|
|
|
for c in components:
|
|
|
|
ligs[lig].append({markClasses[mc]: a for mc, a in c.items()})
|
2016-01-22 14:50:17 +01:00
|
|
|
subtables = otl.buildMarkLigPos(marks, ligs, self.glyphMap)
|
|
|
|
return self.buildLookup_(subtables)
|
2015-12-08 19:04:42 +01:00
|
|
|
|
|
|
|
|
2015-12-09 17:56:47 +01:00
|
|
|
class MarkMarkPosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 6)
|
2015-12-09 17:56:47 +01:00
|
|
|
self.marks = {} # glyphName -> (markClassName, anchor)
|
|
|
|
self.baseMarks = {} # glyphName -> {markClassName: anchor}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.marks == other.marks and
|
|
|
|
self.baseMarks == other.baseMarks)
|
|
|
|
|
|
|
|
def inferGlyphClasses(self):
|
|
|
|
result = {glyph: 3 for glyph in self.baseMarks}
|
|
|
|
result.update({glyph: 3 for glyph in self.marks})
|
|
|
|
return result
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-21 16:23:55 +01:00
|
|
|
markClasses = self.buildMarkClasses_(self.marks)
|
|
|
|
markClassList = sorted(markClasses.keys(), key=markClasses.get)
|
|
|
|
marks = {mark: (markClasses[mc], anchor)
|
|
|
|
for mark, (mc, anchor) in self.marks.items()}
|
|
|
|
|
2015-12-09 17:56:47 +01:00
|
|
|
st = otTables.MarkMarkPos()
|
|
|
|
st.Format = 1
|
|
|
|
st.ClassCount = len(markClasses)
|
2016-01-21 16:23:55 +01:00
|
|
|
st.Mark1Coverage = otl.buildCoverage(marks, self.glyphMap)
|
2016-01-14 17:15:52 +01:00
|
|
|
st.Mark2Coverage = otl.buildCoverage(self.baseMarks, self.glyphMap)
|
2016-01-21 16:23:55 +01:00
|
|
|
st.Mark1Array = otl.buildMarkArray(marks, self.glyphMap)
|
2015-12-09 17:56:47 +01:00
|
|
|
st.Mark2Array = otTables.Mark2Array()
|
|
|
|
st.Mark2Array.Mark2Count = len(st.Mark2Coverage.glyphs)
|
|
|
|
st.Mark2Array.Mark2Record = []
|
|
|
|
for base in st.Mark2Coverage.glyphs:
|
2016-01-21 13:05:36 +01:00
|
|
|
anchors = [self.baseMarks[base].get(mc) for mc in markClassList]
|
|
|
|
st.Mark2Array.Mark2Record.append(otl.buildMark2Record(anchors))
|
2015-12-09 17:56:47 +01:00
|
|
|
return self.buildLookup_([st])
|
|
|
|
|
|
|
|
|
2015-12-03 13:05:42 +01:00
|
|
|
class ReverseChainSingleSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 8)
|
2015-12-03 13:05:42 +01:00
|
|
|
self.substitutions = [] # (prefix, suffix, mapping)
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.substitutions == other.substitutions)
|
|
|
|
|
|
|
|
def build(self):
|
2015-12-09 12:51:01 +01:00
|
|
|
subtables = []
|
2015-12-03 13:05:42 +01:00
|
|
|
for prefix, suffix, mapping in self.substitutions:
|
|
|
|
st = otTables.ReverseChainSingleSubst()
|
|
|
|
st.Format = 1
|
|
|
|
self.setBacktrackCoverage_(prefix, st)
|
|
|
|
self.setLookAheadCoverage_(suffix, st)
|
2016-01-14 17:15:52 +01:00
|
|
|
st.Coverage = otl.buildCoverage(mapping.keys(), self.glyphMap)
|
2015-12-09 12:30:57 +01:00
|
|
|
st.GlyphCount = len(mapping)
|
|
|
|
st.Substitute = [mapping[g] for g in st.Coverage.glyphs]
|
2015-12-09 12:51:01 +01:00
|
|
|
subtables.append(st)
|
|
|
|
return self.buildLookup_(subtables)
|
2015-12-03 13:05:42 +01:00
|
|
|
|
|
|
|
|
2015-09-08 10:33:07 +02:00
|
|
|
class SingleSubstBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GSUB', 1)
|
2015-09-08 10:33:07 +02:00
|
|
|
self.mapping = {}
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.mapping == other.mapping)
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-22 19:32:45 +01:00
|
|
|
subtable = otl.buildSingleSubstSubtable(self.mapping)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_([subtable])
|
2015-12-04 11:16:43 +01:00
|
|
|
|
2016-01-11 16:00:52 +01:00
|
|
|
def getAlternateGlyphs(self):
|
|
|
|
return {glyph: set([repl]) for glyph, repl in self.mapping.items()}
|
|
|
|
|
2015-12-04 11:16:43 +01:00
|
|
|
|
2015-12-23 11:35:49 +01:00
|
|
|
class ClassPairPosSubtableBuilder(object):
|
|
|
|
def __init__(self, builder, valueFormat1, valueFormat2):
|
|
|
|
self.builder_ = builder
|
|
|
|
self.classDef1_, self.classDef2_ = None, None
|
|
|
|
self.coverage_ = set()
|
|
|
|
self.values_ = {} # (glyphclass1, glyphclass2) --> (value1, value2)
|
|
|
|
self.valueFormat1_, self.valueFormat2_ = valueFormat1, valueFormat2
|
|
|
|
self.forceSubtableBreak_ = False
|
|
|
|
self.subtables_ = []
|
|
|
|
|
|
|
|
def addPair(self, gc1, value1, gc2, value2):
|
|
|
|
mergeable = (not self.forceSubtableBreak_ and
|
|
|
|
self.classDef1_ is not None and
|
|
|
|
self.classDef1_.canAdd(gc1) and
|
|
|
|
self.classDef2_ is not None and
|
|
|
|
self.classDef2_.canAdd(gc2))
|
|
|
|
if not mergeable:
|
|
|
|
self.flush_()
|
2016-02-02 10:48:47 +01:00
|
|
|
self.classDef1_ = otl.ClassDefBuilder(useClass0=True)
|
|
|
|
self.classDef2_ = otl.ClassDefBuilder(useClass0=False)
|
2015-12-23 11:35:49 +01:00
|
|
|
self.coverage_ = set()
|
|
|
|
self.values_ = {}
|
|
|
|
self.classDef1_.add(gc1)
|
|
|
|
self.classDef2_.add(gc2)
|
|
|
|
self.coverage_.update(gc1)
|
|
|
|
self.values_[(gc1, gc2)] = (value1, value2)
|
|
|
|
|
|
|
|
def addSubtableBreak(self):
|
|
|
|
self.forceSubtableBreak_ = True
|
|
|
|
|
|
|
|
def subtables(self):
|
|
|
|
self.flush_()
|
|
|
|
return self.subtables_
|
|
|
|
|
|
|
|
def flush_(self):
|
|
|
|
if self.classDef1_ is None or self.classDef2_ is None:
|
|
|
|
return
|
2016-02-02 15:17:01 +01:00
|
|
|
st = otl.buildPairPosClassesSubtable(self.values_,
|
|
|
|
self.builder_.glyphMap)
|
2015-12-23 11:35:49 +01:00
|
|
|
self.subtables_.append(st)
|
|
|
|
|
|
|
|
|
2016-02-02 18:23:37 +01:00
|
|
|
class PairPosBuilder(LookupBuilder):
|
2015-12-23 11:35:49 +01:00
|
|
|
SUBTABLE_BREAK_ = "SUBTABLE_BREAK"
|
|
|
|
|
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 2)
|
2016-02-02 18:23:37 +01:00
|
|
|
self.pairs = [] # [(gc1, value1, gc2, value2)*]
|
|
|
|
self.glyphPairs = {} # (glyph1, glyph2) --> (value1, value2)
|
|
|
|
self.locations = {} # (gc1, gc2) --> (filepath, line, column)
|
2015-12-23 11:35:49 +01:00
|
|
|
|
2016-02-02 18:23:37 +01:00
|
|
|
def addClassPair(self, location, glyphclass1, value1, glyphclass2, value2):
|
|
|
|
self.pairs.append((glyphclass1, value1, glyphclass2, value2))
|
|
|
|
|
|
|
|
def addGlyphPair(self, location, glyph1, value1, glyph2, value2):
|
|
|
|
key = (glyph1, glyph2)
|
|
|
|
oldValue = self.glyphPairs.get(key, None)
|
|
|
|
if oldValue is not None:
|
|
|
|
otherLoc = self.locations[key]
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Already defined position for pair %s %s at %s:%d:%d'
|
|
|
|
% (glyph1, glyph2, otherLoc[0], otherLoc[1], otherLoc[2]),
|
|
|
|
location)
|
|
|
|
val1, _ = makeOpenTypeValueRecord(value1)
|
|
|
|
val2, _ = makeOpenTypeValueRecord(value2)
|
|
|
|
self.glyphPairs[key] = (val1, val2)
|
|
|
|
self.locations[key] = location
|
2015-12-23 11:35:49 +01:00
|
|
|
|
|
|
|
def add_subtable_break(self, location):
|
2016-02-02 18:23:37 +01:00
|
|
|
self.pairs.append((self.SUBTABLE_BREAK_, self.SUBTABLE_BREAK_,
|
2015-12-23 11:35:49 +01:00
|
|
|
self.SUBTABLE_BREAK_, self.SUBTABLE_BREAK_))
|
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
2016-02-02 18:23:37 +01:00
|
|
|
self.glyphPairs == other.glyphPairs and
|
2015-12-23 11:35:49 +01:00
|
|
|
self.pairs == other.pairs)
|
|
|
|
|
|
|
|
def build(self):
|
|
|
|
builders = {}
|
|
|
|
builder = None
|
2016-02-02 18:23:37 +01:00
|
|
|
for glyphclass1, value1, glyphclass2, value2 in self.pairs:
|
2015-12-23 11:35:49 +01:00
|
|
|
if glyphclass1 is self.SUBTABLE_BREAK_:
|
|
|
|
if builder is not None:
|
|
|
|
builder.addSubtableBreak()
|
|
|
|
continue
|
|
|
|
val1, valFormat1 = makeOpenTypeValueRecord(value1)
|
|
|
|
val2, valFormat2 = makeOpenTypeValueRecord(value2)
|
|
|
|
builder = builders.get((valFormat1, valFormat2))
|
|
|
|
if builder is None:
|
|
|
|
builder = ClassPairPosSubtableBuilder(
|
|
|
|
self, valFormat1, valFormat2)
|
|
|
|
builders[(valFormat1, valFormat2)] = builder
|
|
|
|
builder.addPair(glyphclass1, val1, glyphclass2, val2)
|
|
|
|
subtables = []
|
2016-02-02 18:23:37 +01:00
|
|
|
if self.glyphPairs:
|
|
|
|
subtables.extend(
|
|
|
|
otl.buildPairPosGlyphs(self.glyphPairs, self.glyphMap))
|
2015-12-23 11:35:49 +01:00
|
|
|
for key in sorted(builders.keys()):
|
|
|
|
subtables.extend(builders[key].subtables())
|
|
|
|
return self.buildLookup_(subtables)
|
|
|
|
|
|
|
|
|
2015-12-04 11:16:43 +01:00
|
|
|
class SinglePosBuilder(LookupBuilder):
|
2015-12-10 19:17:11 +01:00
|
|
|
def __init__(self, font, location):
|
|
|
|
LookupBuilder.__init__(self, font, location, 'GPOS', 1)
|
2016-01-19 16:03:29 +01:00
|
|
|
self.locations = {} # glyph -> (filename, line, column)
|
|
|
|
self.mapping = {} # glyph -> otTables.ValueRecord
|
|
|
|
|
|
|
|
def add_pos(self, location, glyph, valueRecord):
|
|
|
|
otValueRecord, _ = makeOpenTypeValueRecord(valueRecord)
|
|
|
|
curValue = self.mapping.get(glyph)
|
|
|
|
if curValue is not None and curValue != otValueRecord:
|
|
|
|
otherLoc = self.locations[glyph]
|
|
|
|
raise FeatureLibError(
|
|
|
|
'Already defined different position for glyph "%s" at %s:%d:%d'
|
|
|
|
% (glyph, otherLoc[0], otherLoc[1], otherLoc[2]),
|
|
|
|
location)
|
|
|
|
if otValueRecord:
|
|
|
|
self.mapping[glyph] = otValueRecord
|
|
|
|
self.locations[glyph] = location
|
2015-12-04 11:16:43 +01:00
|
|
|
|
|
|
|
def equals(self, other):
|
|
|
|
return (LookupBuilder.equals(self, other) and
|
|
|
|
self.mapping == other.mapping)
|
|
|
|
|
|
|
|
def build(self):
|
2016-01-19 16:03:29 +01:00
|
|
|
subtables = otl.buildSinglePos(self.mapping, self.glyphMap)
|
2015-12-09 12:51:01 +01:00
|
|
|
return self.buildLookup_(subtables)
|