def add_ligature(font, codes): if 'GSUB' not in font: ligature_subst = otTables.LigatureSubst() ligature_subst.ligatures = {} lookup = otTables.Lookup() lookup.LookupType = 4 lookup.LookupFlag = 0 lookup.SubTableCount = 1 lookup.SubTable = [ligature_subst] font['GSUB'] = add_emoji_gsub.create_simple_gsub([lookup]) else: lookup = font['GSUB'].table.LookupList.Lookup[0] assert lookup.LookupType == 4 assert lookup.LookupFlag == 0 ligatures = lookup.SubTable[0].ligatures lig = otTables.Ligature() lig.CompCount = len(codes) lig.Component = [glyph_name([code]) for code in codes[1:]] lig.LigGlyph = glyph_name(codes) first = "%04X" % int(codes[0], 16) try: ligatures[first].append(lig) except KeyError: ligatures[first] = [lig]
def create_lookup(table, font, flag=0): """Create a Lookup based on mapping table.""" cmap = font_data.get_cmap(font) ligatures = {} for output, (ch1, ch2) in table.iteritems(): output = cmap[output] ch1 = get_glyph_name_or_create(ch1, font) ch2 = get_glyph_name_or_create(ch2, font) ligature = otTables.Ligature() ligature.CompCount = 2 ligature.Component = [ch2] ligature.LigGlyph = output try: ligatures[ch1].append(ligature) except KeyError: ligatures[ch1] = [ligature] ligature_subst = otTables.LigatureSubst() ligature_subst.ligatures = ligatures lookup = otTables.Lookup() lookup.LookupType = 4 lookup.LookupFlag = flag lookup.SubTableCount = 1 lookup.SubTable = [ligature_subst] return lookup
def test_preWrite_format1(self): table = otTables.LigatureSubst() table.ligatures = { "c": self.makeLigatures("ct"), "f": self.makeLigatures("ffi ff fi") } rawTable = table.preWrite(self.font) self.assertEqual(table.Format, 1) self.assertEqual(rawTable["Coverage"].glyphs, ["c", "f"]) [c, f] = rawTable["LigatureSet"] self.assertIsInstance(c, otTables.LigatureSet) self.assertIsInstance(f, otTables.LigatureSet) [ct] = c.Ligature self.assertIsInstance(ct, otTables.Ligature) self.assertEqual(ct.LigGlyph, "c_t") self.assertEqual(ct.Component, ["c", "t"]) [ffi, ff, fi] = f.Ligature self.assertIsInstance(ffi, otTables.Ligature) self.assertEqual(ffi.LigGlyph, "f_f_i") self.assertEqual(ffi.Component, ["f", "f", "i"]) self.assertIsInstance(ff, otTables.Ligature) self.assertEqual(ff.LigGlyph, "f_f") self.assertEqual(ff.Component, ["f", "f"]) self.assertIsInstance(fi, otTables.Ligature) self.assertEqual(fi.LigGlyph, "f_i") self.assertEqual(fi.Component, ["f", "i"])
def get_gsub_ligature_lookup(font): """If the font does not have a GSUB table, create one with a ligature substitution lookup. If it does, ensure the first lookup is a properly initialized ligature substitution lookup. Return the lookup.""" # The template might include more lookups after lookup 0, if it has a # GSUB table. if 'GSUB' not in font: ligature_subst = otTables.LigatureSubst() ligature_subst.ligatures = {} lookup = otTables.Lookup() lookup.LookupType = 4 lookup.LookupFlag = 0 lookup.SubTableCount = 1 lookup.SubTable = [ligature_subst] font['GSUB'] = add_emoji_gsub.create_simple_gsub([lookup]) else: lookup = font['GSUB'].table.LookupList.Lookup[0] assert lookup.LookupFlag == 0 # importXML doesn't fully init GSUB structures, so help it out st = lookup.SubTable[0] if not hasattr(lookup, 'LookupType'): assert st.LookupType == 4 setattr(lookup, 'LookupType', 4) if not hasattr(st, 'ligatures'): setattr(st, 'ligatures', {}) return lookup
def add_ligature(font, seq, name): if 'GSUB' not in font: ligature_subst = otTables.LigatureSubst() ligature_subst.ligatures = {} lookup = otTables.Lookup() lookup.LookupType = 4 lookup.LookupFlag = 0 lookup.SubTableCount = 1 lookup.SubTable = [ligature_subst] font['GSUB'] = add_emoji_gsub.create_simple_gsub([lookup]) else: lookup = font['GSUB'].table.LookupList.Lookup[0] assert lookup.LookupType == 4 assert lookup.LookupFlag == 0 ligatures = lookup.SubTable[0].ligatures lig = otTables.Ligature() lig.CompCount = len(seq) lig.Component = seq[1:] lig.LigGlyph = name first = seq[0] try: ligatures[first].append(lig) except KeyError: ligatures[first] = [lig]
def build(self): subtable = otTables.LigatureSubst() subtable.Format = 1 subtable.ligatures = {} for components in sorted(self.ligatures.keys(), key=self.make_key): lig = otTables.Ligature() lig.Component = components[1:] lig.LigGlyph = self.ligatures[components] subtable.ligatures.setdefault(components[0], []).append(lig) return self.buildLookup_([subtable])
def test_fromXML(self): table = otTables.LigatureSubst() for name, attrs, content in parseXML( '<LigatureSet glyph="f">' ' <Ligature components="f,f,i" glyph="f_f_i"/>' ' <Ligature components="f,f" glyph="f_f"/>' '</LigatureSet>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(set(table.ligatures.keys()), {"f"}) [ffi, ff] = table.ligatures["f"] self.assertEqual(ffi.LigGlyph, "f_f_i") self.assertEqual(ffi.Component, ["f", "f", "i"]) self.assertEqual(ff.LigGlyph, "f_f") self.assertEqual(ff.Component, ["f", "f"])
def test_toXML2(self): writer = XMLWriter(StringIO()) table = otTables.LigatureSubst() table.ligatures = { "c": self.makeLigatures("ct"), "f": self.makeLigatures("ffi ff fi") } table.toXML2(writer, self.font) self.assertEqual(writer.file.getvalue().splitlines()[1:], [ '<LigatureSet glyph="c">', ' <Ligature components="c,t" glyph="c_t"/>', '</LigatureSet>', '<LigatureSet glyph="f">', ' <Ligature components="f,f,i" glyph="f_f_i"/>', ' <Ligature components="f,f" glyph="f_f"/>', ' <Ligature components="f,i" glyph="f_i"/>', '</LigatureSet>' ])
def buildLigatureSubstSubtable(mapping): if not mapping: return None self = ot.LigatureSubst() # The following single line can replace the rest of this function # with fontTools >= 3.1: # self.ligatures = dict(mapping) self.ligatures = {} for components in sorted(mapping.keys(), key=_getLigatureKey): ligature = ot.Ligature() ligature.Component = components[1:] ligature.CompCount = len(ligature.Component) + 1 ligature.LigGlyph = mapping[components] firstGlyph = components[0] self.ligatures.setdefault(firstGlyph, []).append(ligature) return self
def build(self): lookup = otTables.Lookup() lookup.SubTable = [] st = otTables.LigatureSubst() st.Format = 1 st.ligatures = {} for components in sorted(self.ligatures.keys(), key=self.make_key): lig = otTables.Ligature() lig.Component = components lig.LigGlyph = self.ligatures[components] st.ligatures.setdefault(components[0], []).append(lig) lookup.SubTable.append(st) lookup.LookupFlag = self.lookup_flag lookup.LookupType = self.lookup_type lookup.SubTableCount = len(lookup.SubTable) return lookup
def test_postRead_format1(self): table = otTables.LigatureSubst() table.Format = 1 ligs_c = otTables.LigatureSet() ligs_c.Ligature = self.makeLigatures("ct") ligs_f = otTables.LigatureSet() ligs_f.Ligature = self.makeLigatures("ffi ff fi") rawTable = { "Coverage": makeCoverage(["c", "f"]), "LigatureSet": [ligs_c, ligs_f] } table.postRead(rawTable, self.font) self.assertEqual(set(table.ligatures.keys()), {"c", "f"}) self.assertEqual(len(table.ligatures["c"]), 1) self.assertEqual(table.ligatures["c"][0].LigGlyph, "c_t") self.assertEqual(table.ligatures["c"][0].Component, ["c", "t"]) self.assertEqual(len(table.ligatures["f"]), 3) self.assertEqual(table.ligatures["f"][0].LigGlyph, "f_f_i") self.assertEqual(table.ligatures["f"][0].Component, ["f", "f", "i"]) self.assertEqual(table.ligatures["f"][1].LigGlyph, "f_f") self.assertEqual(table.ligatures["f"][1].Component, ["f", "f"]) self.assertEqual(table.ligatures["f"][2].LigGlyph, "f_i") self.assertEqual(table.ligatures["f"][2].Component, ["f", "i"])
def init_gsub(self): """Call this if you are going to add ligatures to the font. Creates a GSUB table if there isn't one already.""" if hasattr(self, 'ligatures'): return font = self.font if 'GSUB' not in font: ligature_subst = otTables.LigatureSubst() ligature_subst.ligatures = {} lookup = otTables.Lookup() lookup.LookupType = 4 lookup.LookupFlag = 0 lookup.SubTableCount = 1 lookup.SubTable = [ligature_subst] font['GSUB'] = add_emoji_gsub.create_simple_gsub([lookup]) else: lookup = font['GSUB'].table.LookupList.Lookup[0] assert lookup.LookupType == 4 assert lookup.LookupFlag == 0 self.ligatures = lookup.SubTable[0].ligatures
def test_postRead_formatUnknown(self): table = otTables.LigatureSubst() table.Format = 987 rawTable = {"Coverage": makeCoverage(["f"])} self.assertRaises(AssertionError, table.postRead, rawTable, self.font)