Files
linuxcncWebWASM/scripts/subset_rtcp_beginner_fonts.py

29 lines
1.4 KiB
Python

"""Embed only the authored Unicode codepoints, avoiding CJK radical aliases."""
import html
import shutil
from pathlib import Path
from fontTools import subset
from fontTools.ttLib import TTFont
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'docs/teaching/rtcp-beginners'
characters=set(html.unescape((OUT/'handbook.html').read_text()))
for style in ['Regular','Bold']:
font=TTFont(f'/usr/share/fonts/opentype/noto/NotoSansCJK-{style}.ttc',fontNumber=2)
options=subset.Options()
options.name_IDs=['*'];options.name_legacy=True;options.name_languages=['*']
tool=subset.Subsetter(options=options);tool.populate(unicodes={ord(c) for c in characters});tool.subset(font)
for record in font['name'].names:
replacement={1:'RtcpHandbookCJK',2:style,3:f'RtcpHandbookCJK-{style}',4:f'RtcpHandbookCJK {style}',6:f'RtcpHandbookCJK-{style}'}.get(record.nameID)
if replacement:
record.string=replacement.encode(record.getEncoding())
if 'CFF ' in font:
cff=font['CFF '].cff
cff.fontNames=[f'RtcpHandbookCJK-{style}']
cff.topDictIndex[0].FamilyName='RtcpHandbookCJK'
cff.topDictIndex[0].FullName=f'RtcpHandbookCJK {style}'
font.save(OUT/f'assets/handbook-{style.lower()}.otf')
font.close()
shutil.copyfile('/usr/share/doc/fonts-noto-cjk/copyright',OUT/'assets/font-license.txt')
print('Embedded SC fonts subset to',len(characters),'authored characters')