"""Cuts the three CJK interface fonts out of Noto Sans CJK Bold (SIL OFL 1.1, Adobe/Google). python Tools/loc/make_cjk_fonts.py Writes Assets/Fonts/GcCjkSC-Bold.otf, GcCjkJP-Bold.otf, GcCjkKR-Bold.otf: each face keeps Latin, the language's common characters (GB2312 for Chinese, JIS X 0208 for Japanese, every Hangul syllable plus KS X 1001 for Korean) and every character used in the translation tables, so the build carries a few MB instead of 3 x 16 MB. The subsets are renamed ("GC CJK SC" etc.), since the OFL wants modified fonts under another name. """ import io, os, sys from fontTools.ttLib import TTCollection from fontTools import subset HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) OUT = os.path.join(ROOT, 'Assets', 'Fonts') TABLES = os.path.join(ROOT, 'Assets', 'Resources', 'Loc') def codec_chars(codec, lead, trail): out = set() for a in lead: for b in trail: try: out.add(bytes([a, b]).decode(codec)) except UnicodeDecodeError: pass return out def table_chars(code): p = os.path.join(TABLES, code + '.txt') return set(io.open(p, encoding='utf-8-sig').read()) if os.path.exists(p) else set() BASE = set(chr(c) for c in range(0x20, 0x7F)) | set(chr(c) for c in range(0xA0, 0x180)) \ | set('‘’“”…·—–•×→←↑↓★☆●○■□▲△%$():;!?、。,.・「」『』【】〈〉《》~ー〜') SETS = { # Simplified Chinese: all of GB2312 (6763 hanzi and symbols) 'SC': ('Noto Sans CJK SC', codec_chars('gb2312', range(0xA1, 0xF8), range(0xA1, 0xFF)), 'zh-Hans'), # Japanese: JIS X 0208 (kana and 6355 kanji) 'JP': ('Noto Sans CJK JP', codec_chars('euc_jp', range(0xA1, 0xF5), range(0xA1, 0xFF)), 'ja'), # Korean: every Hangul syllable, the jamo, and KS X 1001 'KR': ('Noto Sans CJK KR', set(chr(c) for c in range(0xAC00, 0xD7A4)) | set(chr(c) for c in range(0x3130, 0x3190)) | codec_chars('euc_kr', range(0xA1, 0xFE), range(0xA1, 0xFF)), 'ko'), } def main(src): coll = TTCollection(src) for tag, (face, chars, code) in SETS.items(): font = next(f for f in coll.fonts if f['name'].getDebugName(4) == face + ' Bold') want = BASE | chars | table_chars(code) opts = subset.Options() opts.layout_features = ['*'] opts.name_IDs = ['*'] opts.name_languages = ['*'] opts.notdef_outline = True opts.drop_tables += ['DSIG'] s = subset.Subsetter(opts) s.populate(unicodes=[ord(c) for c in want if c.strip() or c == ' ']) s.subset(font) family = 'GC CJK ' + tag name = font['name'] for rec in list(name.names): if rec.nameID in (1, 16): rec.string = family elif rec.nameID == 4: rec.string = family + ' Bold' elif rec.nameID in (6, 20): rec.string = 'GcCjk' + tag + '-Bold' elif rec.nameID == 3: rec.string = '1.0;GcCjk' + tag + '-Bold' elif rec.nameID == 17: rec.string = 'Bold' if 'CFF ' in font: cff = font['CFF '].cff cff.fontNames = ['GcCjk' + tag + '-Bold'] top = cff.topDictIndex[0] for k in ('FullName', 'FamilyName'): if hasattr(top, k): setattr(top, k, family) out = os.path.join(OUT, f'GcCjk{tag}-Bold.otf') font.save(out) print(f'{out}: {len(font.getBestCmap())} chars, {os.path.getsize(out) / 1e6:.1f} MB') if __name__ == '__main__': main(sys.argv[1])