Optimizing the number of font glyphs for web use
Required packages apt install fontforge и pip install fonttools.
Let’s decide on the necessary glyphs: we need Latin, Cyrillic, special characters, including some Greek symbols (ΣΩαβγμ), Greek analogues that are similar in style but have different codes (∆∑µ), and a few more that can be used somewhere in the text or as elements to replace graphics (♪♫♬).
Why are there multiple variants of similar glyphs with different codes?
Because the µ (mu U+00b5) symbol and the Greek letter μ (mu U+03bc) in extended fonts can have their own glyph with different shapes, or, conversely, a common glyph, where similar characters in shape refer to the same glyph. Or, for example, the Greek Σ (Sigma U+03a3) is not the same as the symbol ∑ (summation U+2211).
But perhaps these symbols look the same on your screen.
Example with prepared character sets in unicode
nano make_menu.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
nano ff-conv.sh
This site uses the Latin alphabet, but not with all symbols, such as diacritics, and also Cyrillic and some special characters.
Symbolic representation of Unicode characters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Generating fonts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Original font PTSansNarrowRegular_original.ttf 230K
Some fonts may use a single glyph for certain character combinations,
for Latin, these are often combinations of fi fl ff ft.
Often, web font generation services remove these glyphs.
If you have this problem, you can solve it by simply adding a CSS rule:
letter-spacing:-0.1pt;
font-glyph-fl-fi-ft.png
font-fl-fi-ft-errore.png
Example with manual code selection
Open the .ttf file in Fontforge and begin selecting and copying the desired glyph numeric codes into the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
pyftsubset PTSansNarrow-Regular.ttf --output-file=PTSans-sub.ttf --text-file=glyph-symbols
Universal CSS style for font and fallback with size adjustments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
And a little more
Count the number of glyphs in the original file and the one we made ourselves.
1 2 3 4 5 6 7 8 9 10 11 | |
For .svg, this can be done, for example, like this:
cat PTSansNarrow.svg | grep "glyph-name" | wc -l
723
cat PTSansNarrow_subset.svg | grep "glyph-name" | wc -l
292
It should be noted here that the resulting fonts may contain fewer glyphs than the characters for selection, because a specific font may not have glyphs for some characters. The script contains an extended set of characters (codes) for selection from different fonts, but the main thing is that it does not contain unnecessary characters. However, it would still be more correct to open a specific font in the editor and select the required glyphs from the available ones.
If the font is intended to be used only for text, which sometimes does not even require additional characters to alphanumeric other than the period and comma, then the number of glyphs can be reduced even further.
1 2 3 4 5 6 7 8 9 | |