From 87575cb7c3d1e14544e041f5eb43c9c6aa590343 Mon Sep 17 00:00:00 2001 From: Nic Barker Date: Thu, 5 Jun 2025 10:38:53 +1000 Subject: [PATCH] [Core] Fix handling of letter spacing --- clay.h | 6 +++--- examples/raylib-sidebar-scrolling-container/main.c | 2 +- renderers/raylib/clay_renderer_raylib.c | 9 +++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/clay.h b/clay.h index 07ae4f3..1deec93 100644 --- a/clay.h +++ b/clay.h @@ -1674,7 +1674,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text measuredHeight = CLAY__MAX(measuredHeight, dimensions.height); measured->minWidth = CLAY__MAX(dimensions.width, measured->minWidth); } - measuredWidth = CLAY__MAX(lineWidth, measuredWidth); + measuredWidth = CLAY__MAX(lineWidth, measuredWidth) - config->letterSpacing; measured->measuredWordsStartIndex = tempWord.next; measured->unwrappedDimensions.width = measuredWidth; @@ -2522,13 +2522,13 @@ void Clay__CalculateFinalLayout(void) { lineLengthChars = 0; lineStartOffset = measuredWord->startOffset; } else { - lineWidth += measuredWord->width; + lineWidth += measuredWord->width + textConfig->letterSpacing; lineLengthChars += measuredWord->length; wordIndex = measuredWord->next; } } if (lineLengthChars > 0) { - Clay__WrappedTextLineArray_Add(&context->wrappedTextLines, CLAY__INIT(Clay__WrappedTextLine) { { lineWidth, lineHeight }, {.length = lineLengthChars, .chars = &textElementData->text.chars[lineStartOffset] } }); + Clay__WrappedTextLineArray_Add(&context->wrappedTextLines, CLAY__INIT(Clay__WrappedTextLine) { { lineWidth - textConfig->letterSpacing, lineHeight }, {.length = lineLengthChars, .chars = &textElementData->text.chars[lineStartOffset] } }); textElementData->wrappedLines.length++; } containerElement->dimensions.height = lineHeight * (float)textElementData->wrappedLines.length; diff --git a/examples/raylib-sidebar-scrolling-container/main.c b/examples/raylib-sidebar-scrolling-container/main.c index 7d8be80..97c7042 100644 --- a/examples/raylib-sidebar-scrolling-container/main.c +++ b/examples/raylib-sidebar-scrolling-container/main.c @@ -11,7 +11,7 @@ Texture2D profilePicture; #define RAYLIB_VECTOR2_TO_CLAY_VECTOR2(vector) (Clay_Vector2) { .x = vector.x, .y = vector.y } Clay_String profileText = CLAY_STRING_CONST("Profile Page one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen"); -Clay_TextElementConfig headerTextConfig = { .fontId = 1, .fontSize = 16, .textColor = {0,0,0,255} }; +Clay_TextElementConfig headerTextConfig = { .fontId = 1, .letterSpacing = 5, .fontSize = 16, .textColor = {0,0,0,255} }; void HandleHeaderButtonInteraction(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData) { if (pointerData.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index 2cd9af0..4e3b2d3 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -87,6 +87,8 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex float maxTextWidth = 0.0f; float lineTextWidth = 0; + int maxLineCharCount = 0; + int lineCharCount = 0; float textHeight = config->fontSize; Font* fonts = (Font*)userData; @@ -99,11 +101,13 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex float scaleFactor = config->fontSize/(float)fontToUse.baseSize; - for (int i = 0; i < text.length; ++i) + for (int i = 0; i < text.length; ++i, lineCharCount++) { if (text.chars[i] == '\n') { maxTextWidth = fmax(maxTextWidth, lineTextWidth); + maxLineCharCount = CLAY__MAX(maxLineCharCount, lineCharCount); lineTextWidth = 0; + lineCharCount = 0; continue; } int index = text.chars[i] - 32; @@ -112,8 +116,9 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex } maxTextWidth = fmax(maxTextWidth, lineTextWidth); + maxLineCharCount = CLAY__MAX(maxLineCharCount, lineCharCount); - textSize.width = maxTextWidth * scaleFactor; + textSize.width = maxTextWidth * scaleFactor + (lineCharCount * config->letterSpacing); textSize.height = textHeight; return textSize; -- 2.39.5