godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.92k stars 21.06k forks source link

Font Hinting (or positioning) seems slightly off. #90779

Open jitspoe opened 5 months ago

jitspoe commented 5 months ago

Tested versions

4.2.1 stable

System information

Windows 10, Vulkan forward +, Nvidia 3070

Issue description

When using a strongly hinted font, such as Tahoma, where the vertical lines should be pixel perfect, I've noticed that I can't seem to get pixel perfect hinted fonts in Godot.

Here are the import settings I'm using, which should be pixel perfect for a font at size 24: image

If you zoom in, you can see the vertical lines are not pixel perfect: image

Though the horizontal lines do seem correct, which makes me wonder if the font is actually importing properly, but being positioned off pixel horizontally when displayed in the controls.

For comparison, this is how the font looks in the ttf viewer in Windows: image

And zoomed in: image

Notice everything is pixel perfect here for the vertical and horizontal lines with font size 24.

Steps to reproduce

Use a font that has strong hinting, such as Tahoma. In the import settings, set hinting to "Normal". Might need to set subpixel positioning to "Disabled" (didn't seem to change anything, but I've heard it helps). Use the font in a theme and set it to one of the sizes that hints well, like 24 or 12.

Minimal reproduction project (MRP)

test_font_hint(add own tahoma.ttf).zip

I've included a test project, but you will have to supply your own copy of tahoma.ttf, as I don't think it's legal to redistribute it. Should be on any copy of Windows, though. I couldn't find another font that had hinting this strong to use as an example.

jitspoe commented 3 months ago

Here's a more egregious example using an actual pixel font:

image

Note how it looks pixel perfect for the first few characters then gets all blurry.

Here's the font I used, at size 11: NotJamOldStyle11.zip

jitspoe commented 3 months ago

Ok, it seems these settings in the font import fix the issue:

image

(Namely setting hinting to normal and subpixel positioning to disabled). I understand that not snapping might not be ideal for all fonts, but I would expect strongly hinted, especially pixel art fonts, to just render sharply. Wonder why they start getting off, and if we can just round the accumulated error (if that's what's causing it) so things just look good by default.

With the adjusted settings:

image

Edit: Just realized that the original issue still occurs with the Tahoma font, as that's the settings I used when I opened the bug. My memory is terrible. 😅

bruvzg commented 3 months ago

Note how it looks pixel perfect for the first few characters then gets all blurry.

That's fully expected if you enable subpixel positioning with "pixel" font (it's not a real bitmap pixel font, but vector font using straight lines), subpixel positioning will rasterize multiple variants of each glyph to get better spacing (and in case of "pixel" font, some of the lines won't align with pixel boundaries). Subpixel positioning should not be used with pixel fonts, it's for small size vector fonts.

Just realized that the original issue still occurs with the Tahoma font

I do not see anything wrong here. Font like Tahoma aren't intended to be pixel perfect and will always be smoothed out if you have antialiasing enabled. The purpose of hinting is better contrast, not pixel perfect alignment.

Calinou commented 3 months ago

Does this occur with font oversampling disabled in the project settings? See https://github.com/godotengine/godot/issues/86563.

jitspoe commented 3 months ago

I don't see any notable difference with oversampling disabled: image

Calinou commented 3 months ago

Note that MSDF import is having a hard time with Tahoma in general:

image

Even without MSDF, Oversampling set to 5 in the Import dock (and 500% zoom in the 2D editor) looks bad due to misaligned characters:

Normal hinting Light hinting No hinting
image image image

This is with the font size set to 12 in the theme. Forcing the autohinter or enabling subpixel positioning doesn't improve upon this.

Regarding the sharpness of the "i" glyph, forcing the autohinter helps on this particular issue, but it doesn't help on everything (example with antialiasing disabled):

No autohinter Force autohinter
image image

I've also tried enabling the 2D pixel snap project settings to no avail.