| 17 | |
| 18 | |
| 19 | def freeze_feature(font, moving_rules, config): |
| 20 | feature_list = font["GSUB"].table.FeatureList |
| 21 | feature_record = feature_list.FeatureRecord |
| 22 | feature_dict = { |
| 23 | feature.FeatureTag: (i, feature.Feature) |
| 24 | for i, feature in enumerate(feature_record) |
| 25 | if feature.FeatureTag != "calt" |
| 26 | } |
| 27 | |
| 28 | calt_features = [ |
| 29 | feature.Feature for feature in feature_record if feature.FeatureTag == "calt" |
| 30 | ] |
| 31 | |
| 32 | enable_calt = config.get("calt") == "1" |
| 33 | if not enable_calt: |
| 34 | for calt_feature in calt_features: |
| 35 | calt_feature.LookupListIndex.clear() |
| 36 | calt_feature.LookupCount = 0 |
| 37 | |
| 38 | indices_to_remove = [] |
| 39 | for tag, status in config.items(): |
| 40 | if tag not in feature_dict or status == "0": |
| 41 | continue |
| 42 | |
| 43 | index, target_feature = feature_dict[tag] |
| 44 | if status == "-1": |
| 45 | indices_to_remove.append(index) |
| 46 | continue |
| 47 | |
| 48 | if tag in moving_rules and enable_calt: |
| 49 | for calt_feature in calt_features: |
| 50 | calt_feature.LookupListIndex.extend(target_feature.LookupListIndex) |
| 51 | else: |
| 52 | glyph_dict = font["glyf"].glyphs |
| 53 | hmtx_dict = font["hmtx"].metrics |
| 54 | for lookup_index in target_feature.LookupListIndex: |
| 55 | mapping = getattr( |
| 56 | font["GSUB"].table.LookupList.Lookup[lookup_index].SubTable[0], |
| 57 | "mapping", |
| 58 | None, |
| 59 | ) |
| 60 | if not mapping: |
| 61 | continue |
| 62 | for old_key, new_key in mapping.items(): |
| 63 | if ( |
| 64 | old_key in glyph_dict |
| 65 | and old_key in hmtx_dict |
| 66 | and new_key in glyph_dict |
| 67 | and new_key in hmtx_dict |
| 68 | ): |
| 69 | glyph_dict[old_key] = glyph_dict[new_key] |
| 70 | hmtx_dict[old_key] = hmtx_dict[new_key] |
| 71 | |
| 72 | # Remove features's lookup list index in reverse order to maintain correct indices |
| 73 | for index in sorted(indices_to_remove, reverse=True): |
| 74 | feature_list.FeatureRecord[index].Feature.LookupCount = 0 |
| 75 | feature_list.FeatureRecord[index].Feature.LookupListIndex = [] |
| 76 | |