[mtiLib] Port to otlLib.builder for supported lookups

This commit is contained in:
Behdad Esfahbod 2016-01-13 17:54:42 +00:00
parent 5c34acb94d
commit aeb5ecbfd4

View File

@ -10,6 +10,7 @@ from __future__ import print_function, division, absolute_import
from fontTools import ttLib from fontTools import ttLib
from fontTools.ttLib.tables import otTables as ot from fontTools.ttLib.tables import otTables as ot
from fontTools.ttLib.tables.otBase import ValueRecord, valueRecordFormatDict from fontTools.ttLib.tables.otBase import ValueRecord, valueRecordFormatDict
from fontTools.otlLib import builder
from contextlib import contextmanager from contextlib import contextmanager
class MtiLibError(Exception): pass class MtiLibError(Exception): pass
@ -153,39 +154,35 @@ def parseLookupFlags(lines):
filterset = int(line[1]) filterset = int(line[1])
return flags, filterset return flags, filterset
def parseSingleSubst(self, lines, font, _lookupMap=None): def parseSingleSubst(lines, font, _lookupMap=None):
self.mapping = {} mapping = {}
for line in lines: for line in lines:
assert len(line) == 2, line assert len(line) == 2, line
line = makeGlyphs(line) line = makeGlyphs(line)
self.mapping[line[0]] = line[1] mapping[line[0]] = line[1]
return builder.buildSingleSubst(mapping)
def parseMultiple(self, lines, font, _lookupMap=None): def parseMultiple(lines, font, _lookupMap=None):
self.mapping = {} mapping = {}
for line in lines: for line in lines:
line = makeGlyphs(line) line = makeGlyphs(line)
self.mapping[line[0]] = line[1:] mapping[line[0]] = line[1:]
return builder.buildMultipleSubst(mapping)
def parseAlternate(self, lines, font, _lookupMap=None): def parseAlternate(lines, font, _lookupMap=None):
self.alternates = {} mapping = {}
for line in lines: for line in lines:
line = makeGlyphs(line) line = makeGlyphs(line)
self.alternates[line[0]] = line[1:] mapping[line[0]] = line[1:]
return builder.buildAlternateSubst(mapping)
def parseLigature(self, lines, font, _lookupMap=None): def parseLigature(lines, font, _lookupMap=None):
self.ligatures = {} mapping = {}
for line in lines: for line in lines:
assert len(line) >= 2, line assert len(line) >= 2, line
line = makeGlyphs(line) line = makeGlyphs(line)
# The following single line can replace the rest of this function with fontTools >= 3.1 mapping[tuple(line[1:])] = line[0]
#self.ligatures[tuple(line[1:])] = line[0] return builder.buildLigatureSubst(mapping)
ligGlyph, firstGlyph = line[:2]
otherComponents = line[2:]
ligature = ot.Ligature()
ligature.Component = otherComponents
ligature.CompCount = len(ligature.Component) + 1
ligature.LigGlyph = ligGlyph
self.ligatures.setdefault(firstGlyph, []).append(ligature)
def parseSinglePos(self, lines, font, _lookupMap=None): def parseSinglePos(self, lines, font, _lookupMap=None):
values = {} values = {}
@ -764,10 +761,10 @@ def parseLookup(lines, tableTag, font, lookupMap=None):
lookup.MarkFilteringSet = filterset lookup.MarkFilteringSet = filterset
lookup.LookupType, parseLookupSubTable = { lookup.LookupType, parseLookupSubTable = {
'GSUB': { 'GSUB': {
'single': (1, parseSingleSubst), 'single': (0, parseSingleSubst),
'multiple': (2, parseMultiple), 'multiple': (0, parseMultiple),
'alternate': (3, parseAlternate), 'alternate': (0, parseAlternate),
'ligature': (4, parseLigature), 'ligature': (0, parseLigature),
'context': (5, parseContextSubst), 'context': (5, parseContextSubst),
'chained': (6, parseChainedSubst), 'chained': (6, parseChainedSubst),
'reversechained':(8, parseReverseChainedSubst), 'reversechained':(8, parseReverseChainedSubst),
@ -790,8 +787,11 @@ def parseLookup(lines, tableTag, font, lookupMap=None):
while lines.peek(): while lines.peek():
with lines.until(('% subtable', 'subtable end')): with lines.until(('% subtable', 'subtable end')):
while lines.peek(): while lines.peek():
subtable = ot.lookupTypes[tableTag][lookup.LookupType]() if lookup.LookupType is 0:
parseLookupSubTable(subtable, lines, font, lookupMap) subtable = parseLookupSubTable(lines, font, lookupMap)
else:
subtable = ot.lookupTypes[tableTag][lookup.LookupType]()
parseLookupSubTable(subtable, lines, font, lookupMap)
subtables.append(subtable) subtables.append(subtable)
if lines.peek() and lines.peek()[0] in ('% subtable', 'subtable end'): if lines.peek() and lines.peek()[0] in ('% subtable', 'subtable end'):
next(lines) next(lines)