Beispiel #1
0
 def test_preWrite_format2(self):
     table = otTables.SingleSubst()
     table.mapping = {"A": "c", "B": "b", "C": "a"}
     rawTable = table.preWrite(self.font)
     self.assertEqual(table.Format, 2)
     self.assertEqual(rawTable["Coverage"].glyphs, ["A", "B", "C"])
     self.assertEqual(rawTable["Substitute"], ["c", "b", "a"])
Beispiel #2
0
 def test_fromXML(self):
     table = otTables.SingleSubst()
     for name, attrs, content in parseXML('<Substitution in="A" out="a"/>'
                                          '<Substitution in="B" out="b"/>'
                                          '<Substitution in="C" out="c"/>'):
         table.fromXML(name, attrs, content, self.font)
     self.assertEqual(table.mapping, {"A": "a", "B": "b", "C": "c"})
Beispiel #3
0
 def test_preWrite_emptyMapping(self):
     table = otTables.SingleSubst()
     table.mapping = {}
     rawTable = table.preWrite(self.font)
     self.assertEqual(table.Format, 2)
     self.assertEqual(rawTable["Coverage"].glyphs, [])
     self.assertEqual(rawTable["Substitute"], [])
Beispiel #4
0
def merge(self, m, tables):

    assert len(tables) == len(m.duplicateGlyphsPerFont)
    for i, (table, dups) in enumerate(zip(tables, m.duplicateGlyphsPerFont)):
        if not dups: continue
        assert (table is not None and table is not NotImplemented
                ), "Have duplicates to resolve for font %d but no GSUB: %s" % (
                    i + 1, dups)
        synthFeature = None
        synthLookup = None
        for script in table.table.ScriptList.ScriptRecord:
            if script.ScriptTag == 'DFLT': continue  # XXX
            for langsys in [script.Script.DefaultLangSys] + [
                    l.LangSys for l in script.Script.LangSysRecord
            ]:
                if langsys is None: continue  # XXX Create!
                feature = [
                    v for v in langsys.FeatureIndex if v.FeatureTag == 'locl'
                ]
                assert len(feature) <= 1
                if feature:
                    feature = feature[0]
                else:
                    if not synthFeature:
                        synthFeature = otTables.FeatureRecord()
                        synthFeature.FeatureTag = 'locl'
                        f = synthFeature.Feature = otTables.Feature()
                        f.FeatureParams = None
                        f.LookupCount = 0
                        f.LookupListIndex = []
                        langsys.FeatureIndex.append(synthFeature)
                        langsys.FeatureIndex.sort(key=lambda v: v.FeatureTag)
                        table.table.FeatureList.FeatureRecord.append(
                            synthFeature)
                        table.table.FeatureList.FeatureCount += 1
                    feature = synthFeature

                if not synthLookup:
                    subtable = otTables.SingleSubst()
                    subtable.mapping = dups
                    synthLookup = otTables.Lookup()
                    synthLookup.LookupFlag = 0
                    synthLookup.LookupType = 1
                    synthLookup.SubTableCount = 1
                    synthLookup.SubTable = [subtable]
                    if table.table.LookupList is None:
                        # mtiLib uses None as default value for LookupList,
                        # while feaLib points to an empty array with count 0
                        # TODO: make them do the same
                        table.table.LookupList = otTables.LookupList()
                        table.table.LookupList.Lookup = []
                        table.table.LookupList.LookupCount = 0
                    table.table.LookupList.Lookup.append(synthLookup)
                    table.table.LookupList.LookupCount += 1

                feature.Feature.LookupListIndex[:0] = [synthLookup]
                feature.Feature.LookupCount += 1

    DefaultTable.merge(self, m, tables)
    return self
Beispiel #5
0
 def test_preWrite_format1(self):
     table = otTables.SingleSubst()
     table.mapping = {"A": "a", "B": "b", "C": "c"}
     rawTable = table.preWrite(self.font)
     self.assertEqual(table.Format, 1)
     self.assertEqual(rawTable["Coverage"].glyphs, ["A", "B", "C"])
     self.assertEqual(rawTable["DeltaGlyphID"], 5)
Beispiel #6
0
def merge(self, m, tables):

    assert len(tables) == len(m.duplicateGlyphsPerFont)
    for i, (table, dups) in enumerate(zip(tables, m.duplicateGlyphsPerFont)):
        if not dups: continue
        assert (
            table is not None and table is not NotImplemented
        ), "Have duplicates to resolve for font %d but no GSUB" % (i + 1)
        lookupMap = dict((id(v), v) for v in table.table.LookupList.Lookup)
        featureMap = dict(
            (id(v), v) for v in table.table.FeatureList.FeatureRecord)
        synthFeature = None
        synthLookup = None
        for script in table.table.ScriptList.ScriptRecord:
            if script.ScriptTag == 'DFLT': continue  # XXX
            for langsys in [script.Script.DefaultLangSys] + [
                    l.LangSys for l in script.Script.LangSysRecord
            ]:
                feature = [
                    featureMap[v] for v in langsys.FeatureIndex
                    if featureMap[v].FeatureTag == 'locl'
                ]
                assert len(feature) <= 1
                if feature:
                    feature = feature[0]
                else:
                    if not synthFeature:
                        synthFeature = otTables.FeatureRecord()
                        synthFeature.FeatureTag = 'locl'
                        f = synthFeature.Feature = otTables.Feature()
                        f.FeatureParams = None
                        f.LookupCount = 0
                        f.LookupListIndex = []
                        langsys.FeatureIndex.append(id(synthFeature))
                        featureMap[id(synthFeature)] = synthFeature
                        langsys.FeatureIndex.sort(
                            key=lambda v: featureMap[v].FeatureTag)
                        table.table.FeatureList.FeatureRecord.append(
                            synthFeature)
                        table.table.FeatureList.FeatureCount += 1
                    feature = synthFeature

                if not synthLookup:
                    subtable = otTables.SingleSubst()
                    subtable.mapping = dups
                    synthLookup = otTables.Lookup()
                    synthLookup.LookupFlag = 0
                    synthLookup.LookupType = 1
                    synthLookup.SubTableCount = 1
                    synthLookup.SubTable = [subtable]
                    table.table.LookupList.Lookup.append(synthLookup)
                    table.table.LookupList.LookupCount += 1

                feature.Feature.LookupListIndex[:0] = [id(synthLookup)]
                feature.Feature.LookupCount += 1

    DefaultTable.merge(self, m, tables)
    return self
Beispiel #7
0
 def test_postRead_format1(self):
     table = otTables.SingleSubst()
     table.Format = 1
     rawTable = {
         "Coverage": makeCoverage(["A", "B", "C"]),
         "DeltaGlyphID": 5
     }
     table.postRead(rawTable, self.font)
     self.assertEqual(table.mapping, {"A": "a", "B": "b", "C": "c"})
Beispiel #8
0
def merge(self, m, tables):

	assert len(tables) == len(m.duplicateGlyphsPerFont)
	for i,(table,dups) in enumerate(zip(tables, m.duplicateGlyphsPerFont)):
		if not dups: continue
		if table is None or table is NotImplemented:
			# Checks whether the dups are equivalent or not.
			# Discard gid if its shape is not equal to that of oldgid.
			for oldgid, gid in dups.items():
				if not isGlyphSame(m.fonts, oldgid, gid):
					oldgname, oldidx = getGlyphNameAndFontIndex(oldgid)
					gname, idx = getGlyphNameAndFontIndex(gid)
					log.warn("%s:<%s> is dropped and replaced by %s:<%s>" % (m.fontfiles[idx], gname, m.fontfiles[oldidx], oldgname))
			continue
		lookupMap = {id(v):v for v in table.table.LookupList.Lookup}
		featureMap = {id(v):v for v in table.table.FeatureList.FeatureRecord}
		synthFeature = None
		synthLookup = None
		for script in table.table.ScriptList.ScriptRecord:
			if script.ScriptTag == 'DFLT': continue # XXX
			for langsys in [script.Script.DefaultLangSys] + [l.LangSys for l in script.Script.LangSysRecord]:
				if langsys is None: continue # XXX Create!
				feature = [featureMap[v] for v in langsys.FeatureIndex if featureMap[v].FeatureTag == 'locl']
				assert len(feature) <= 1
				if feature:
					feature = feature[0]
				else:
					if not synthFeature:
						synthFeature = otTables.FeatureRecord()
						synthFeature.FeatureTag = 'locl'
						f = synthFeature.Feature = otTables.Feature()
						f.FeatureParams = None
						f.LookupCount = 0
						f.LookupListIndex = []
						langsys.FeatureIndex.append(id(synthFeature))
						featureMap[id(synthFeature)] = synthFeature
						langsys.FeatureIndex.sort(key=lambda v: featureMap[v].FeatureTag)
						table.table.FeatureList.FeatureRecord.append(synthFeature)
						table.table.FeatureList.FeatureCount += 1
					feature = synthFeature

				if not synthLookup:
					subtable = otTables.SingleSubst()
					subtable.mapping = dups
					synthLookup = otTables.Lookup()
					synthLookup.LookupFlag = 0
					synthLookup.LookupType = 1
					synthLookup.SubTableCount = 1
					synthLookup.SubTable = [subtable]
					table.table.LookupList.Lookup.append(synthLookup)
					table.table.LookupList.LookupCount += 1

				feature.Feature.LookupListIndex[:0] = [id(synthLookup)]
				feature.Feature.LookupCount += 1

	DefaultTable.merge(self, m, tables)
	return self
Beispiel #9
0
 def test_toXML2(self):
     writer = XMLWriter(StringIO())
     table = otTables.SingleSubst()
     table.mapping = {"A": "a", "B": "b", "C": "c"}
     table.toXML2(writer, self.font)
     self.assertEqual(writer.file.getvalue().splitlines()[1:], [
         '<Substitution in="A" out="a"/>',
         '<Substitution in="B" out="b"/>',
         '<Substitution in="C" out="c"/>',
     ])
Beispiel #10
0
 def test_postRead_format2(self):
     table = otTables.SingleSubst()
     table.Format = 2
     rawTable = {
         "Coverage": makeCoverage(["A", "B", "C"]),
         "GlyphCount": 3,
         "Substitute": ["c", "b", "a"]
     }
     table.postRead(rawTable, self.font)
     self.assertEqual(table.mapping, {"A": "c", "B": "b", "C": "a"})
Beispiel #11
0
 def build(self):
     lookup = otTables.Lookup()
     lookup.SubTable = []
     st = otTables.SingleSubst()
     st.mapping = self.mapping
     lookup.SubTable.append(st)
     lookup.LookupFlag = self.lookup_flag
     lookup.LookupType = self.lookup_type
     lookup.SubTableCount = len(lookup.SubTable)
     return lookup
Beispiel #12
0
 def build(self):
     subtable = otTables.SingleSubst()
     subtable.mapping = self.mapping
     return self.buildLookup_([subtable])
Beispiel #13
0
 def test_postRead_formatUnknown(self):
     table = otTables.SingleSubst()
     table.Format = 987
     rawTable = {"Coverage": makeCoverage(["A", "B", "C"])}
     self.assertRaises(AssertionError, table.postRead, rawTable, self.font)
Beispiel #14
0
def buildSingleSubstSubtable(mapping):
    if not mapping:
        return None
    self = ot.SingleSubst()
    self.mapping = dict(mapping)
    return self