fix word wrap meausrement

This commit is contained in:
2025-11-06 13:15:38 -06:00
parent 768ad399de
commit 721ac3bb93

View File

@@ -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;
}
c = wordStart;
} else {
// Character-based wrapping or word too long to fit
maxWidth = fmaxf(maxWidth, lineWidth);
lineWidth = 0;
height += lineHeight + f->linegap;
wordStart = c + 1;
wordStart = c;
wordWidth = 0;
if (!breakAtWord) {
lineWidth += charWidth;
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;