[Core] Fix handling of letter spacing

This commit is contained in:
Nic Barker 2025-06-05 10:38:53 +10:00
parent 80659eda04
commit 87575cb7c3
3 changed files with 11 additions and 6 deletions

6
clay.h
View file

@ -1674,7 +1674,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height); measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
measured->minWidth = CLAY__MAX(dimensions.width, measured->minWidth); 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->measuredWordsStartIndex = tempWord.next;
measured->unwrappedDimensions.width = measuredWidth; measured->unwrappedDimensions.width = measuredWidth;
@ -2522,13 +2522,13 @@ void Clay__CalculateFinalLayout(void) {
lineLengthChars = 0; lineLengthChars = 0;
lineStartOffset = measuredWord->startOffset; lineStartOffset = measuredWord->startOffset;
} else { } else {
lineWidth += measuredWord->width; lineWidth += measuredWord->width + textConfig->letterSpacing;
lineLengthChars += measuredWord->length; lineLengthChars += measuredWord->length;
wordIndex = measuredWord->next; wordIndex = measuredWord->next;
} }
} }
if (lineLengthChars > 0) { 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++; textElementData->wrappedLines.length++;
} }
containerElement->dimensions.height = lineHeight * (float)textElementData->wrappedLines.length; containerElement->dimensions.height = lineHeight * (float)textElementData->wrappedLines.length;

View file

@ -11,7 +11,7 @@ Texture2D profilePicture;
#define RAYLIB_VECTOR2_TO_CLAY_VECTOR2(vector) (Clay_Vector2) { .x = vector.x, .y = vector.y } #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_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) { void HandleHeaderButtonInteraction(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData) {
if (pointerData.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { if (pointerData.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {

View file

@ -87,6 +87,8 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex
float maxTextWidth = 0.0f; float maxTextWidth = 0.0f;
float lineTextWidth = 0; float lineTextWidth = 0;
int maxLineCharCount = 0;
int lineCharCount = 0;
float textHeight = config->fontSize; float textHeight = config->fontSize;
Font* fonts = (Font*)userData; 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; 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') { if (text.chars[i] == '\n') {
maxTextWidth = fmax(maxTextWidth, lineTextWidth); maxTextWidth = fmax(maxTextWidth, lineTextWidth);
maxLineCharCount = CLAY__MAX(maxLineCharCount, lineCharCount);
lineTextWidth = 0; lineTextWidth = 0;
lineCharCount = 0;
continue; continue;
} }
int index = text.chars[i] - 32; 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); maxTextWidth = fmax(maxTextWidth, lineTextWidth);
maxLineCharCount = CLAY__MAX(maxLineCharCount, lineCharCount);
textSize.width = maxTextWidth * scaleFactor; textSize.width = maxTextWidth * scaleFactor + (lineCharCount * config->letterSpacing);
textSize.height = textHeight; textSize.height = textHeight;
return textSize; return textSize;