zishu's blog

zishu's blog

一个热爱生活的博主。https://zishu.me

An elegant font compression solution --- fonttools

Sharing a python library called fonttools, which is very convenient for processing font files.

Download#

First, you need to download the python environment on your computer, which is self-explanatory.

Then, enter the following command in the local command line:

pip install fonttools

Font Library#

  1. Create a new folder locally and name it 'fonts', you can choose any name. Put the fonts you want to compress and organize in this folder, such as 'OPPOSans.ttf'.
  2. Then create a file named 'word.txt'. Now we need to find a commonly used font library. Here, I will share one on GitHub https://github.com/zburu/cdn/blob/main/font/ 汉字常用字体.txt. Copy the content of commonly used fonts into the 'word.txt' file.

Run#

pyftsubset OPPOSans.ttf --text=$(cat word.txt) --no-hinting

Let's take a look at the command. 'OPPOSans.ttf' is the font file, 'word.txt' contains the commonly used fonts that we want to extract.

You can name them according to your own requirements, but they must correspond to each other.

Simply enter the command in the command line. After it finishes running, you can see the font files with the word 'subset' in the folder. The file size is greatly reduced, and you can use them directly.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.