<s> from fontTools . ttLib import TTFont <EOL> from nototools import summary <EOL> class HbInputGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , font , reverse_cmap = None ) : <EOL> self . font = font <EOL> self . reverse_cmap = reverse_cmap or build_reverse_cmap ( self . font ) <EOL> def all_inputs ( self ) : <EOL> """<STR_LIT>""" <EOL> inputs = [ ] <EOL> glyph_names = self . font . getGlyphOrder ( ) <EOL> for name in glyph_names : <EOL> inputs . append ( self . input_from_name ( name ) ) <EOL> return inputs <EOL> def input_from_name ( self , name , features = None ) : <EOL> """<STR_LIT>""" <EOL> if features is None : <EOL> features = [ ] <EOL> if name in self . reverse_cmap : <EOL> return features , unichr ( self . reverse_cmap [ name ] ) <EOL> if '<STR_LIT>' not in self . font : <EOL> return <EOL> gsub = self . font [ '<STR_LIT>' ] . table <EOL> for lookup_index , lookup in enumerate ( gsub . LookupList . Lookup ) : <EOL> for st in lookup . SubTable : <EOL> if lookup . LookupType == <NUM_LIT:1> : <EOL> for glyph , subst in st . mapping . items ( ) : <EOL> if subst == name : <EOL> return self . _input_with_context ( <EOL> gsub , glyph , lookup_index , features ) <EOL> elif lookup . LookupType == <NUM_LIT:4> : <EOL> for prefix , ligatures in st . ligatures . items ( ) : <EOL> for ligature in ligatures : <EOL> if ligature . LigGlyph == name : <EOL> glyphs = [ prefix ] + ligature . Component <EOL> return self . _sequence_from_glyph_names ( <EOL> glyphs , features ) <EOL> def _input_with_context ( self , gsub , glyph , lookup_index , features ) : <EOL> """<STR_LIT>""" <EOL> for feature in gsub . FeatureList . FeatureRecord : <EOL> if lookup_index in feature . Feature . LookupListIndex : <EOL> features . append ( feature . FeatureTag ) <EOL> return self . input_from_name ( glyph , features ) <EOL> for lookup in gsub . LookupList . Lookup : <EOL> for st in lookup . SubTable : <EOL> if lookup . LookupType != <NUM_LIT:6> : <EOL> continue <EOL> for sub_lookup in st . SubstLookupRecord : <EOL> if sub_lookup . LookupListIndex != lookup_index : <EOL> continue <EOL> if st . LookAheadCoverage : <EOL> glyphs = [ glyph , st . LookAheadCoverage [ <NUM_LIT:0> ] . glyphs [ <NUM_LIT:0> ] ] <EOL> elif st . BacktrackCoverage : <EOL> glyphs = [ st . BacktrackCoverage [ <NUM_LIT:0> ] . glyphs [ <NUM_LIT:0> ] , glyph ] <EOL> else : <EOL> continue <EOL> return self . _sequence_from_glyph_names ( glyphs , features ) <EOL> raise ValueError ( '<STR_LIT>' % lookup_index ) <EOL> def _sequence_from_glyph_names ( self , glyphs , features ) : <EOL> """<STR_LIT>""" <EOL> text = [ ] <EOL> for glyph in glyphs : <EOL> features , cur_text = self . input_from_name ( glyph , features ) <EOL> text . append ( cur_text ) <EOL> return features , '<STR_LIT>' . join ( text ) <EOL> def build_reverse_cmap ( font ) : <EOL> """<STR_LIT>""" <EOL> return { n : v for v , n in summary . get_largest_cmap ( font ) . items ( ) } </s>
