|
#!/usr/bin/env python3 |
|
import sys |
|
import os |
|
import re |
|
import fontforge |
|
from fontTools.ttLib import TTFont |
|
|
|
|
|
OS2_WEIGHT_CLASS_MAP = { |
|
100: "Thin", |
|
200: "ExtraLight", |
|
300: "Light", |
|
400: "Regular", |
|
500: "Medium", |
|
600: "SemiBold", |
|
700: "Bold", |
|
800: "ExtraBold", |
|
900: "Black", |
|
} |
|
|
|
def extract_string_from_tuple(tup): |
|
if not tup: |
|
return None |
|
s = tup[-1] |
|
if isinstance(s, bytes): |
|
try: |
|
return s.decode('utf-8') |
|
except Exception: |
|
try: |
|
return s.decode('latin-1') |
|
except Exception: |
|
return repr(s) |
|
return str(s) |
|
|
|
|
|
def tuple_has_nameid(tup, target): |
|
for el in tup: |
|
if isinstance(el, int) and el == target: |
|
return True |
|
return False |
|
|
|
def get_weight_name(font): |
|
return OS2_WEIGHT_CLASS_MAP.get(font.os2_weight, 'Normal') |
|
|
|
def convert(path): |
|
print('Converting', path) |
|
f = fontforge.open(path) |
|
if f.cidweight == "Light": |
|
f.os2_weight = 300 |
|
|
|
# attrs = [a for a in dir(f) |
|
# if not a.startswith('__') |
|
# and not callable(getattr(f, a, None))] |
|
# for a in attrs: |
|
# print(f"{a}: {getattr(f, a)}") |
|
|
|
f.cidcopyright = "Apple Inc." |
|
f.cidweight = get_weight_name(f) |
|
f.cidfontname = f.cidfamilyname.replace(" ", "") + "-" + f.cidweight |
|
f.cidfullname = f.cidfontname |
|
f.fontname = f.cidfontname |
|
f.fullname = f.cidfontname |
|
f.familyname = f.cidfamilyname |
|
|
|
new_names = [ |
|
(1033, 0, f.cidcopyright), |
|
(1033, 3, f.cidfontname), |
|
(1033, 5, "21.0d1e1"), |
|
(1033, 6, f.cidfontname), |
|
(1033, 7, f.familyname + " is a trademark of Apple Inc."), |
|
(1033, 8, "Apple Inc."), |
|
(1033, 12, "https://www.apple.com/"), |
|
(2052, 3, f.cidfontname.replace("SC", "-简体中文").replace("PingFang", "苹方").replace("Thin", "纤细").replace("ExtraLight", "特细").replace("Light", "细体").replace("Regular", "常规").replace("Medium", "中等").replace("SemiBold", "中粗").replace("Bold", "粗体").replace("ExtraBold", "特粗").replace("Black", "黑体")), |
|
(2052, 7, f.familyname.replace(" SC", "").replace("PingFang", "苹方") + "是 Apple Inc. 的商标"), |
|
] |
|
f.sfnt_names = new_names |
|
|
|
out = os.path.splitext(path)[0] + '.ttf' |
|
try: |
|
f.generate(out) |
|
print(' Generated', out) |
|
except Exception as e: |
|
print(' Failed to generate:', e) |
|
finally: |
|
f.close() |
|
|
|
|
|
if __name__ == '__main__': |
|
if len(sys.argv) < 2: |
|
print('Usage: convert_fonts.py file.otf') |
|
sys.exit(1) |
|
convert(sys.argv[1]) |
Also See: refinec/PingFangSC#1 (comment)