peterhinch / micropython-font-to-py

A Python 3 utility to convert fonts to Python source capable of being frozen as bytecode
MIT License
385 stars 69 forks source link

Don't generate bitmaps for characters that are not in the TTF font #23

Closed ironss closed 4 years ago

ironss commented 4 years ago

If the TTF font does not have a character in the character set, font-to-py generates a bitmap using the TTF fonts substitute character. This wastes space in the bitmap font, and means that two substitute characters might appear in the generated text: one for a character that is in the charset spec, and a different one for a character that is not in the generated font.

I feel it would be better to omit characters from the generated bitmap font if the TTF font does not have that character. However, this comes down to how a project manages which characters are needed for the various fonts it uses.

Commit https://github.com/ironss/micropython-font-to-py/commit/7752d3f44b8168a99c0c653553d5c96fa94c03da#diff-f4ac81b4c240921eba248479064aa376 fixes this.

Note that you really do have to have both checks that the character is in the TTF font. The first one is used to find the first and last character; the second one ensures that only characters in the TTF font are included in the bitmap font.

(I cannot help feeling that it might be possible to simplify this area of the code that generates a sorted list of character codes, but the current implementation works).

peterhinch commented 4 years ago

Thank you. I've now pushed this update.