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 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 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 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 _add_ligature(self, glyphstr): lig = otTables.Ligature() lig.CompCount = len(glyphstr) lig.Component = [self.glyph_name(ch) for ch in glyphstr[1:]] lig.LigGlyph = self.glyph_name(glyphstr) first = self.glyph_name(glyphstr[0]) try: self.ligatures[first].append(lig) except KeyError: self.ligatures[first] = [lig]
def parseLigature(self, lines, font, _lookupMap=None): self.ligatures = {} for line in lines: assert len(line) >= 2, line line = makeGlyphs(line) # The following single line can replace the rest of this function with fontTools >= 3.1 #self.ligatures[tuple(line[1:])] = line[0] 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 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 add_ligature(lookup, cmap, seq, name): # The sequences consist of codepoints, but the entries in the ligature table # are glyph names. Aliasing can give single codepoints names based on # sequences (e.g. 'guardsman' with 'male guardsman') so we map the # codepoints through the cmap to get the glyph names. glyph_names = [cmap[cp] for cp in seq] lig = otTables.Ligature() lig.CompCount = len(seq) lig.Component = glyph_names[1:] lig.LigGlyph = name ligatures = lookup.SubTable[0].ligatures first_name = glyph_names[0] try: ligatures[first_name].append(lig) except KeyError: ligatures[first_name] = [lig]
def makeLigature(self, s): """'ffi' --> Ligature(LigGlyph='f_f_i', Component=['f', 'f', 'i'])""" lig = otTables.Ligature() lig.Component = list(s) lig.LigGlyph = "_".join(lig.Component) return lig