| 1 |
from babelfont.BaseObject import I18NDictionary |
| 2 |
import re |
| 3 |
|
| 4 |
from babelfont.Master import CORE_METRICS |
| 5 |
|
| 6 |
|
| 7 |
def _stash(item, glyphs, include=None, exclude=None): |
| 8 |
if include: |
| 9 |
keys = include |
| 10 |
else: |
| 11 |
keys = glyphs.keys() |
| 12 |
if exclude: |
| 13 |
keys = [k for k in keys if k not in exclude] |
| 14 |
|
| 15 |
for key in keys: |
| 16 |
if glyphs.get(key): |
| 17 |
if "com.glyphsapp" not in item._formatspecific: |
| 18 |
item._formatspecific["com.glyphsapp"] = {} |
| 19 |
item._formatspecific["com.glyphsapp"][key] = glyphs.get(key) |
| 20 |
|
| 21 |
|
| 22 |
def _g(item, key, default=None, pop=False): |
| 23 |
if "com.glyphsapp" in item._formatspecific: |
| 24 |
if pop: |
| 25 |
return item._formatspecific["com.glyphsapp"].pop(key, default) |
| 26 |
return item._formatspecific["com.glyphsapp"].get(key, default) |
| 27 |
return default |
| 28 |
|
| 29 |
|
| 30 |
def _stashed_cp(thing, name): |
| 31 |
return _custom_parameter(thing._formatspecific.get("com.glyphsapp", []), name) |
| 32 |
|
| 33 |
|
| 34 |
def _custom_parameter(thing, name): |
| 35 |
for param in thing.get("customParameters", []): |
| 36 |
if param["name"] == name: |
| 37 |
return param["value"] |
| 38 |
return None |
| 39 |
|
| 40 |
|
| 41 |
def _moveformatspecific(item): |
| 42 |
rv = {} |
| 43 |
if "com.glyphsapp" in item._formatspecific: |
| 44 |
rv = {**item._formatspecific.get("com.glyphsapp", {})} |
| 45 |
return rv |
| 46 |
|
| 47 |
|
| 48 |
def _copyattrs(src, dst, attrs, convertor=lambda x: x): |
| 49 |
for a in attrs: |
| 50 |
v = getattr(src, a) |
| 51 |
if isinstance(v, I18NDictionary): |
| 52 |
v = v.get_default() |
| 53 |
if v: |
| 54 |
dst[a] = convertor(v) |
| 55 |
|
| 56 |
|
| 57 |
# These are things which are core metrics both for us and glyphs; other |
| 58 |
# metrics which we believe to be metrics are stored by Glyphs as custom |
| 59 |
# parameters |
| 60 |
shared_core_metrics = ["xHeight", "capHeight", "ascender", "descender", "italicAngle"] |
| 61 |
custom_parameter_metrics = [x for x in CORE_METRICS if x not in shared_core_metrics] |
| 62 |
|
| 63 |
_rename_metrics = { |
| 64 |
"x-height": "xHeight", |
| 65 |
"cap height": "capHeight", |
| 66 |
"italic angle": "italicAngle", |
| 67 |
} |
| 68 |
_reverse_rename_metrics = {v: k for k, v in _rename_metrics.items()} |
| 69 |
|
| 70 |
|
| 71 |
def _glyphs_metrics_to_ours(k): |
| 72 |
return _rename_metrics.get(k, k) |
| 73 |
|
| 74 |
|
| 75 |
def _our_metrics_to_glyphs(k): |
| 76 |
return _reverse_rename_metrics.get(k, k) |
| 77 |
|
| 78 |
|
| 79 |
def _metrics_name_to_dict(k): |
| 80 |
m = re.match(r"(.*) \[filter (.*)\]$", k) |
| 81 |
if m: |
| 82 |
return {"type": _our_metrics_to_glyphs(m[1]), "filter": m[2]} |
| 83 |
if k in _reverse_rename_metrics: |
| 84 |
return {"type": _reverse_rename_metrics[k]} |
| 85 |
return {"type": k} |
| 86 |
|
| 87 |
|
| 88 |
def _metrics_dict_to_name(k): |
| 89 |
name = _glyphs_metrics_to_ours(k.get("type")) |
| 90 |
if "filter" in k: |
| 91 |
name += " [filter " + k.get("filter") + "]" |
| 92 |
return name |
| 93 |
|
| 94 |
|
| 95 |
def glyphs_i18ndict(i): |
| 96 |
return [{"language": k, "value": v} for k, v in i.items()] |
| 97 |
|
| 98 |
|
| 99 |
def to_bitfield(flags): |
| 100 |
# Convert an integer flag to an array of bit indices |
| 101 |
return [i for i in range(8) if flags & (1 << i)] |
| 102 |
|
| 103 |
|
| 104 |
# Taken from glyphsLib |
| 105 |
LANGUAGE_MAPPING = { |
| 106 |
"dflt": None, |
| 107 |
"AFK": 0x0436, |
| 108 |
"ARA": 0x0C01, |
| 109 |
"ASM": 0x044D, |
| 110 |
"AZE": 0x042C, |
| 111 |
"BEL": 0x0423, |
| 112 |
"BEN": 0x0845, |
| 113 |
"BGR": 0x0402, |
| 114 |
"BRE": 0x047E, |
| 115 |
"CAT": 0x0403, |
| 116 |
"CSY": 0x0405, |
| 117 |
"DAN": 0x0406, |
| 118 |
"DEU": 0x0407, |
| 119 |
"ELL": 0x0408, |
| 120 |
"ENG": 0x0409, |
| 121 |
"ESP": 0x0C0A, |
| 122 |
"ETI": 0x0425, |
| 123 |
"EUQ": 0x042D, |
| 124 |
"FIN": 0x040B, |
| 125 |
"FLE": 0x0813, |
| 126 |
"FOS": 0x0438, |
| 127 |
"FRA": 0x040C, |
| 128 |
"FRI": 0x0462, |
| 129 |
"GRN": 0x046F, |
| 130 |
"GUJ": 0x0447, |
| 131 |
"HAU": 0x0468, |
| 132 |
"HIN": 0x0439, |
| 133 |
"HRV": 0x041A, |
| 134 |
"HUN": 0x040E, |
| 135 |
"HVE": 0x042B, |
| 136 |
"IRI": 0x083C, |
| 137 |
"ISL": 0x040F, |
| 138 |
"ITA": 0x0410, |
| 139 |
"ITA": 0x0410, |
| 140 |
"IWR": 0x040D, |
| 141 |
"JPN": 0x0411, |
| 142 |
"KAN": 0x044B, |
| 143 |
"KAT": 0x0437, |
| 144 |
"KAZ": 0x043F, |
| 145 |
"KHM": 0x0453, |
| 146 |
"KOK": 0x0457, |
| 147 |
"LAO": 0x0454, |
| 148 |
"LSB": 0x082E, |
| 149 |
"LTH": 0x0427, |
| 150 |
"LVI": 0x0426, |
| 151 |
"MAR": 0x044E, |
| 152 |
"MKD": 0x042F, |
| 153 |
"MLR": 0x044C, |
| 154 |
"MLY": 0x043E, |
| 155 |
"MNG": 0x0352, |
| 156 |
"MTS": 0x043A, |
| 157 |
"NEP": 0x0461, |
| 158 |
"NLD": 0x0413, |
| 159 |
"NOB": 0x0414, |
| 160 |
"ORI": 0x0448, |
| 161 |
"PAN": 0x0446, |
| 162 |
"PAS": 0x0463, |
| 163 |
"PLK": 0x0415, |
| 164 |
"PTG": 0x0816, |
| 165 |
"PTG-BR": 0x0416, |
| 166 |
"RMS": 0x0417, |
| 167 |
"ROM": 0x0418, |
| 168 |
"RUS": 0x0419, |
| 169 |
"SAN": 0x044F, |
| 170 |
"SKY": 0x041B, |
| 171 |
"SLV": 0x0424, |
| 172 |
"SQI": 0x041C, |
| 173 |
"SRB": 0x081A, |
| 174 |
"SVE": 0x041D, |
| 175 |
"TAM": 0x0449, |
| 176 |
"TAT": 0x0444, |
| 177 |
"TEL": 0x044A, |
| 178 |
"THA": 0x041E, |
| 179 |
"TIB": 0x0451, |
| 180 |
"TRK": 0x041F, |
| 181 |
"UKR": 0x0422, |
| 182 |
"URD": 0x0420, |
| 183 |
"USB": 0x042E, |
| 184 |
"UYG": 0x0480, |
| 185 |
"UZB": 0x0443, |
| 186 |
"VIT": 0x042A, |
| 187 |
"WEL": 0x0452, |
| 188 |
"ZHH": 0x0C04, |
| 189 |
"ZHS": 0x0804, |
| 190 |
"ZHT": 0x0404, |
| 191 |
} |
| 192 |
|
| 193 |
REVERSE_LANGUAGE_MAPPING = {v: k for v, k in LANGUAGE_MAPPING.items()} |
| 194 |
|
| 195 |
|
| 196 |
def _to_name_langID(language): |
| 197 |
if language not in LANGUAGE_MAPPING: |
| 198 |
raise ValueError(f"Unknown name language: {language}") |
| 199 |
return LANGUAGE_MAPPING[language] |
| 200 |
|
| 201 |
|
| 202 |
def _to_glyphs_language(langID): |
| 203 |
if langID not in REVERSE_LANGUAGE_MAPPING: |
| 204 |
raise ValueError(f"Unknown name langID: {langID}") |
| 205 |
return REVERSE_LANGUAGE_MAPPING[langID] |
| 206 |
|
| 207 |
|
| 208 |
def labels_to_feature(labels): |
| 209 |
feature_names = [] |
| 210 |
for label in labels: |
| 211 |
langID = _to_name_langID(label["language"]) |
| 212 |
name = label["value"] |
| 213 |
if name == "": |
| 214 |
continue |
| 215 |
name = name.replace("\\", r"\005c").replace('"', r"\0022") |
| 216 |
if langID is None: |
| 217 |
feature_names.append(f' name "{name}";') |
| 218 |
else: |
| 219 |
feature_names.append(f' name 3 1 0x{langID:X} "{name}";') |
| 220 |
if feature_names: |
| 221 |
feature_names.insert(0, "featureNames {") |
| 222 |
feature_names.append("};") |
| 223 |
return feature_names |