Skip to content

Fix GuiGetTextWidth() to measure multi-line text correctly#540

Open
shyraptor wants to merge 1 commit intoraysan5:masterfrom
shyraptor:fix/guigettextwidth-multiline
Open

Fix GuiGetTextWidth() to measure multi-line text correctly#540
shyraptor wants to merge 1 commit intoraysan5:masterfrom
shyraptor:fix/guigettextwidth-multiline

Conversation

@shyraptor
Copy link
Copy Markdown

Resolves #415.

GuiGetTextWidth() was stopping at the first '\n', so auto-sized widgets (GuiLabel, GuiMessageBox, ...) clipped multi-line text.

Fix

  • New static GetLineWidth() helper — single-line measurement, identical to the old GuiGetTextWidth() body except for the icon corrections below.
  • GuiGetTextWidth() becomes a wrapper that iterates '\n'-separated lines and returns max(GetLineWidth(line)).
  • GuiDrawText() now calls GetLineWidth() directly for per-line text positioning, preserving its existing alignment behavior byte-for-byte (the icon is already stripped by GetTextIcon() before measurement, so GetLineWidth() takes no icon path there).

Incidental corrections in GetLineWidth()

Both corrections bring the icon-marker handling in line with the authoritative GetTextIcon():

  1. Off-by-one in icon skiptext += (pos + 1) instead of text += textIconOffset. The old code set textIconOffset to the index of the closing # and then advanced by that, leaving the closing # in the measured string. This over-measured every icon-prefixed label by ~1 glyph.
  2. Digit-only icon detection — the marker is recognised only as # + 1..3 digits + #. The old detector accepted any character between the hashes, so e.g. "#abc#Text" was misclassified as an icon and the label width was computed incorrectly.

MRE (from #415)

Both examples in the issue body now render with the border/message box fully enclosing the multi-line text.

Resolves raysan5#415.

GuiGetTextWidth() stopped at the first '\n', so auto-sized widgets
(GuiLabel, GuiMessageBox, ...) clipped multi-line text.

Factor the existing single-line measurement into a new static
GetLineWidth() helper and turn GuiGetTextWidth() into a wrapper that
iterates '\n'-separated lines and returns the maximum line width.

GuiDrawText() now calls GetLineWidth() directly for per-line text
positioning, so its existing per-line alignment behavior is preserved
byte-for-byte (the icon is already stripped by GetTextIcon() before
measurement, so GetLineWidth() takes no icon path there).

Incidental corrections in GetLineWidth() vs. the old GuiGetTextWidth()
body, to bring it into agreement with GetTextIcon():

- The icon-marker skip now uses text += (pos + 1) to land past the
  closing '#', matching GetTextIcon(). Previously it was
  text += textIconOffset where textIconOffset was the index OF the
  closing '#', leaving that '#' in the measured string and
  over-measuring icon-prefixed labels by ~1 glyph.

- The icon marker is recognised only as '#' + 1..3 digits + '#',
  matching GetTextIcon(). The old detector accepted any character
  between the hashes, so e.g. "#abc#Text" was misclassified as an
  icon and the label width was computed incorrectly.
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.

GetTextWidth() returns incorrect value for multi-line strings

1 participant