@remeus's answer inspired me, but I don't want to use the pyftsubset.exefontTools.subset directly; I want to use the script since I can easily add something to it.
CODE
import fontTools.subset
from pathlib import Path
import os
SOURCE_FILE = Path('YOUR.ttf') # ttf, otf, ...
OUT_FORMAT = 'woff' # otf, woff, woff2, ...
TEXT_FILE = Path('YOUR.txt')
def main():
output_file = Path('temp') / Path(SOURCE_FILE.stem + '.' + OUT_FORMAT)
output_file.parent.mkdir(parents=True, exist_ok=True)
args = [SOURCE_FILE,
f"--output-file={output_file}",
# f"--text=中英文 abcdefg",
f"--text-file={TEXT_FILE}", # 中英文 abcdefg
# "--unicodes=U+0020-007E",
f"--flavor={OUT_FORMAT}",
# 👇 https://github.com/fonttools/fonttools/blob/1df4f1e/Lib/fontTools/subset/__init__.py#L332-L337
"--verbose=true", # https://github.com/fonttools/fonttools/blob/1df4f1e/Lib/fontTools/subset/__init__.py#L3015
# "--xml=true",
"--timing=true",
]
fontTools.subset.main([str(_) for _ in args])
os.startfile(output_file.parent)
if __name__ == "__main__":
main()
Explain
Here are three ways to specify the subset you want
--text
: Write directly
--text-file
: Write the text in the file (It doesn't matter if you repeat it, the code will automatically remove it for you) (I recommend this way.)
example:
my.txt (utf-8)
0123456
abcdefg
ABCDEFG
中英文
--unicodes
: Use unicode range
📙 If you need to convert to woff2, you will have to install brotli first, using pip install brotlicffi.
resason:
Other issue
Below is indirect issues. You can directly skip this paragraph if you don't want to see it.
Here is my experience, I want to use the font on the browser, but I get an error as below
OTS parsing error: Web font size more than 30MB
my font-size
So, If I change the format to Woff or Woff2 may solve the problem.
Still, you can use fontTools again
TTFont(Path()).save(Path())
It cost too much time when your file size is large.
package
uni2ascii -a P letters-in-unicode.txt > letters-in-ascii.txt
– Alyose