peterhinch / micropython-font-to-py

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

Variable character width extended ASCII vs non-extended ASCII #5

Closed stroobandt closed 5 years ago

stroobandt commented 6 years ago

Dear Peter,

I am experiencing considerable variation in character width when creating fonts with 255 versus 128 ASCII characters. See the example below. Is there a way to overcome this?

Also, which ASCII extension code page are you using? This is not documented. Does it depend on my PC settings?

$ ./font_to_py.py -x font.ttf 12 font12.py
Writing Python font file.
Height set in 2 passes. Actual height 11 pixels.
Max character width 11 pixels.
font12.py written successfully.

$ ./font_to_py.py -x -l 255 font.ttf 12 font12x.py
Writing Python font file.
Height set in 2 passes. Actual height 12 pixels.
Max character width 9 pixels.
font12x.py written successfully.

It does not seem quite logical to me that when more glyphs are present, the slimmer the font gets.

stroobandt commented 6 years ago

I have been digging a little deeper… It seems that only OTF fonts allow their extended ASCII glyphs to be converted successfully.

Conversely, TTF fonts are not showing extended ASCII glyphs properly even though -l 255 was specified with the command. The ? character is shown instead.

peterhinch commented 6 years ago

Thank you for that information. This may be a limitation of the freetype library. I've updated the docs and amended the code merely to output a warning (in the hope that freetype is subsequently fixed).

Regarding the observation about width it is counter-intuitive but I think it's correct. The algorithm aims to provide a set of glyphs with the specified maximum height. The entire font is scaled to ensure that no character exceeds the specified height. If there are characters beyond ordinal value 126 which are taller than those below 126, all glyphs will be so scaled. Consequently the height and width of all glyphs below 126 will be reduced.