From 721ac3bb93e099447dbdfea5595435c55bf5d770 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 6 Nov 2025 13:15:38 -0600 Subject: [PATCH] fix word wrap meausrement --- source/font.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/source/font.c b/source/font.c index 78aae578..13cafc89 100644 --- a/source/font.c +++ b/source/font.c @@ -173,34 +173,40 @@ HMM_Vec2 measure_text(const char *text, font *f, float letterSpacing, float wrap float wordWidth = 0; float spaceWidth = f->Characters[' '].advance + letterSpacing; - for (const char *c = text; *c != '\0'; c++) { + for (const char *c = text; *c != '\0'; ) { if (*c == '\n') { maxWidth = fmaxf(maxWidth, lineWidth); lineWidth = 0; height += lineHeight + f->linegap; wordStart = c + 1; wordWidth = 0; + c++; continue; } float charWidth = f->Characters[(unsigned char)*c].advance + letterSpacing; if (wrap > 0 && lineWidth + charWidth > wrap) { - if (breakAtWord && *c != ' ' && wordWidth > 0) { + if (breakAtWord && *c != ' ' && wordWidth > 0 && wordWidth <= wrap) { + // Backtrack to word start only if the word would fit on a new line lineWidth -= wordWidth; - c = wordStart - 1; - } - - maxWidth = fmaxf(maxWidth, lineWidth); - lineWidth = 0; - height += lineHeight + f->linegap; - wordStart = c + 1; - wordWidth = 0; - - if (!breakAtWord) { - lineWidth += charWidth; - continue; + c = wordStart; + } else { + // Character-based wrapping or word too long to fit + maxWidth = fmaxf(maxWidth, lineWidth); + lineWidth = 0; + height += lineHeight + f->linegap; + wordStart = c; + wordWidth = 0; + + if (!breakAtWord || *c == ' ') { + c++; + continue; + } + // For word-based wrapping with long words, don't advance c here - let it be processed in this line } + } else { + c++; } lineWidth += charWidth;