Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backends/sdl2_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const
case Alignment::Left:
Message_rect.x = r.top.x + margin;
Message_rect.y = r.top.y + margin;
Message_rect.w = font->mSize;
Message_rect.h = font->mSize;
Message_rect.w = font->mSize*static_cast<int>(strlen(string));
Message_rect.h = font->mSize + margin*2;
Comment on lines +236 to +237
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify current drawText sizing logic around SDL_ttf surface creation and rect sizing.
rg -n -C3 'TTF_RenderText_Solid|Message_rect\.w|Message_rect\.h|surfaceMessage->(w|h|clip_rect\.w|clip_rect\.h)' src/backends/sdl2_renderer.cpp

Repository: kimkulling/tiny_ui

Length of output: 1360


Use the actual rendered surface dimensions for left-aligned text bounds.

Lines 236–237 estimate text dimensions using font->mSize * strlen(string) and font->mSize + margin*2, which is inaccurate for proportional fonts and causes potential text clipping or misalignment. The rendered surface from TTF_RenderText_Solid() (line 216) is already available—use its actual dimensions instead.

Proposed diff
         case Alignment::Left:
             Message_rect.x = r.top.x + margin;
             Message_rect.y = r.top.y + margin;
-            Message_rect.w = font->mSize*static_cast<int>(strlen(string));
-            Message_rect.h = font->mSize + margin*2;
+            Message_rect.w = surfaceMessage->w;
+            Message_rect.h = surfaceMessage->h;
             break;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/backends/sdl2_renderer.cpp` around lines 236 - 237, Replace the estimated
width/height that use font->mSize and strlen with the actual dimensions of the
SDL surface returned by TTF_RenderText_Solid(): set Message_rect.w =
textSurface->w (or surface->w) and Message_rect.h = textSurface->h (add margin
if you still need padding) so left-aligned bounds use the real rendered pixel
size; locate the TTF_RenderText_Solid() call and the Message_rect assignments to
update them accordingly.

break;
case Alignment::Center:
Message_rect.x = r.top.x + 2 * margin + surfaceMessage->clip_rect.w / 2;
Expand Down
2 changes: 1 addition & 1 deletion src/tinyui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static Style DefaultStyle {
Color4{ 100, 100, 100, 0 },
Color4{ 200, 200, 200, 0 },
8,
{ "Arial.ttf", 35, nullptr }
{ "Arial.ttf", 12, nullptr }
};

static constexpr char const *SeverityToken[] = {
Expand Down
Loading