Skip to content

Fix rendersize for fonts#25

Merged
kimkulling merged 1 commit intomainfrom
bugfix/fix_font_rendersize
Mar 18, 2026
Merged

Fix rendersize for fonts#25
kimkulling merged 1 commit intomainfrom
bugfix/fix_font_rendersize

Conversation

@kimkulling
Copy link
Copy Markdown
Owner

@kimkulling kimkulling commented Mar 18, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Fixed text rendering dimensions to properly account for text content and margins.
    • Adjusted default font size for improved layout consistency.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 18, 2026

Walkthrough

The pull request adjusts text rendering calculations in the SDL2 backend and modifies the default font size configuration. The left-aligned text rectangle width now factors in string length multiplied by font size, and the height includes margin padding. The default font size is reduced from 35 to 12.

Changes

Cohort / File(s) Summary
Text Rectangle Sizing
src/backends/sdl2_renderer.cpp
Left-aligned text rectangle width now calculated as string length multiplied by font size (instead of font size alone); height adjusted to font size plus double margin.
Default Font Configuration
src/tinyui.cpp
Default font size reduced from 35 to 12 in DefaultStyle initialization.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • Move the font instances #23: Modifies text rendering in src/backends/sdl2_renderer.cpp with font member refactoring related to the text rectangle sizing adjustments in this PR.
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix rendersize for fonts' directly aligns with the main changes: correcting text rectangle sizing calculations in the SDL2 renderer and adjusting the default font size.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/fix_font_rendersize
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 18, 2026

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/tinyui.cpp (1)

41-41: Replace the hardcoded default font size with a named constexpr.

Line 41 uses a magic compile-time literal. Extract it to a named constant so default sizing is centralized and safer to tune.

Proposed diff
+static constexpr uint32_t kDefaultFontSize = 12;
+
 static Style DefaultStyle {
@@
-    { "Arial.ttf", 12, nullptr }
+    { "Arial.ttf", kDefaultFontSize, nullptr }
 };

As per coding guidelines, "Use constexpr for compile-time constants and static constexpr for class-level constants".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/tinyui.cpp` at line 41, The literal font size 12 is a magic number —
define a compile-time constant (e.g. constexpr int DEFAULT_FONT_SIZE = 12; or if
this is inside a class use static constexpr int DEFAULT_FONT_SIZE = 12;) and
replace the numeric literal in the initializer { "Arial.ttf", 12, nullptr } with
that named constant (DEFAULT_FONT_SIZE) so the default is centralized and
editable via the symbol used by the initializer.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/backends/sdl2_renderer.cpp`:
- Around line 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.

---

Nitpick comments:
In `@src/tinyui.cpp`:
- Line 41: The literal font size 12 is a magic number — define a compile-time
constant (e.g. constexpr int DEFAULT_FONT_SIZE = 12; or if this is inside a
class use static constexpr int DEFAULT_FONT_SIZE = 12;) and replace the numeric
literal in the initializer { "Arial.ttf", 12, nullptr } with that named constant
(DEFAULT_FONT_SIZE) so the default is centralized and editable via the symbol
used by the initializer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0a2650c5-23de-46fc-9d99-2229d45eff5a

📥 Commits

Reviewing files that changed from the base of the PR and between ef209e6 and 92a7c4c.

📒 Files selected for processing (2)
  • src/backends/sdl2_renderer.cpp
  • src/tinyui.cpp

Comment on lines +236 to +237
Message_rect.w = font->mSize*static_cast<int>(strlen(string));
Message_rect.h = font->mSize + margin*2;
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.

@kimkulling kimkulling merged commit 607a766 into main Mar 18, 2026
4 checks passed
@kimkulling kimkulling deleted the bugfix/fix_font_rendersize branch March 18, 2026 11:45
@kimkulling kimkulling restored the bugfix/fix_font_rendersize branch March 18, 2026 11:53
@kimkulling kimkulling deleted the bugfix/fix_font_rendersize branch March 18, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant