libsdl-org / SDL_ttf

Support for TrueType (.ttf) font files with Simple Directmedia Layer.
zlib License
402 stars 131 forks source link

Bug: Text drawn with a renderer text engine sometimes becomes distorted #419

Closed JBetz closed 1 month ago

JBetz commented 1 month ago

I haven't find a reliable way to reproduce this yet, but basically, text sometimes becomes distorted when there are multiple text objects drawn to the same window using the same text engine.

With SDL_RENDER_DRIVER set to gpu, the text in the editor is clear but the text in the grid items has some seemingly random artifacts, most notable between the t and e glyph inside Paste:

image

With SDL_RENDER_DRIVER set to software, the text in the grid items is noticeably clearer:

image

Note however that in both cases the rectangle is slightly narrow, which makes me think that this isn't an issue with the GPU renderer but something in the TTF + renderer interface. Or perhaps wrong usage of the text engine on my side.

JBetz commented 1 month ago

No issue drawing multiple text objects created by the same text engine with different colors: image

slouken commented 1 month ago

The blurriness and artifacts should be fixed in https://github.com/libsdl-org/SDL_ttf/commit/3adac8ac7ca3be6a5f73a23d930c9f7449f99189. I'm still not sure what's causing the text to be truncated though.

slouken commented 1 month ago

Are you setting a clip rectangle or anything? I'm not sure why it would work in the editor, but not in your button.

JBetz commented 1 month ago

I am, and indeed that's probably the issue.

My rectangles have float coordinates when then get truncated to integers to create a SDL_Rect when clipping. For static text, the origin is determined by a layout engine which returns floats, and then the extent is calculated with TTF_GetTextSubStringsForRange which returns ints.

I'll need to play with it some more because even if I round x and y down and width and height up for the clip rectangle, it still doesn't quite fit.

FWIW, I noticed that if I round the x and y parameters passed to TTF_DrawRendererText (i.e., convert from float to int), the artifacts go away:

image

I haven't tested your fix yet but will soon.

slouken commented 1 month ago

Yeah, you shouldn't clip the text. Try removing that and grabbing the latest fix, it should work correctly now.

JBetz commented 1 month ago

Blur and artifacts are gone, but now some of the glyphs have bits missing. Oddly, the e in the north east slot looks fine while all the others don't.

image

JBetz commented 1 month ago

If I truncate the x and y parameters passed to TTF_DrawRendererText, it looks perfect:

image

slouken commented 1 month ago

Yeah, since the text is pixel perfect, it should be drawn on pixel boundaries. We don't mess with the coordinates passed in, because there might be scaling applied, but you want it aligned with the output.

JBetz commented 1 month ago

That makes sense. Thanks!