From 51082d2f1edcadf50d55a085610e3a5dc162a2f7 Mon Sep 17 00:00:00 2001 From: Nic Barker Date: Sat, 5 Oct 2024 20:57:52 +1300 Subject: [PATCH] Change lineSpacing text config attribute to lineHeight (#37) --- README.md | 8 +++--- clay.h | 26 ++++++++++++------- .../raylib-sidebar-scrolling-container/main.c | 2 +- .../web/canvas2d/clay-canvas2d-renderer.html | 2 +- renderers/web/html/clay-html-renderer.html | 2 +- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3bd9737c..a1e52b77 100644 --- a/README.md +++ b/README.md @@ -968,7 +968,7 @@ Clay_TextElementConfig { uint16_t fontId; uint16_t fontSize; uint16_t letterSpacing; - uint16_t lineSpacing; + uint16_t lineHeight; Clay_TextElementConfigWrapMode wrapMode { CLAY_TEXT_WRAP_WORDS (default), CLAY_TEXT_WRAP_NEWLINES, @@ -1024,11 +1024,11 @@ Font size is generally thought of as `x pixels tall`, but interpretation is left --- -**`.lineSpacing`** +**`.lineHeight`** -`CLAY_TEXT_CONFIG(.lineSpacing = 1)` +`CLAY_TEXT_CONFIG(.lineHeight = 20)` -`.lineSpacing` results in **vertical** white space between lines of text (from both `\n` characters and text wrapping) and will affect layout of parents and siblings. +`.lineHeight` - when non zero - forcibly sets the `height` of each wrapped line of text to `.lineheight` pixels tall. Will affect the layout of both parents and siblings. A value of `0` will use the measured height of the font. --- diff --git a/clay.h b/clay.h index 152bb52f..15535974 100644 --- a/clay.h +++ b/clay.h @@ -252,7 +252,7 @@ typedef struct uint16_t fontId; uint16_t fontSize; uint16_t letterSpacing; - uint16_t lineSpacing; + uint16_t lineHeight; Clay_TextElementConfigWrapMode wrapMode; #ifdef CLAY_EXTEND_CONFIG_TEXT CLAY_EXTEND_CONFIG_TEXT @@ -1965,17 +1965,18 @@ void Clay__CalculateFinalLayout() { // Clone the style config to prevent pollution of other elements that share this config containerElement->layoutConfig = Clay__LayoutConfigArray_Add(&Clay__layoutConfigs, *containerElement->layoutConfig); containerElement->layoutConfig->layoutDirection = CLAY_TOP_TO_BOTTOM; - containerElement->layoutConfig->childGap = textConfig->lineSpacing; containerElement->children = CLAY__INIT(Clay__LayoutElementChildren) { // Note: this overwrites the text property .elements = &Clay__layoutElementChildren.internalArray[Clay__layoutElementChildren.length], .length = 0, }; // Short circuit all wrap calculations if wrap mode is none if (textConfig->wrapMode == CLAY_TEXT_WRAP_NONE || (containerElement->dimensions.width == textElementData->preferredDimensions.width && textConfig->wrapMode != CLAY_TEXT_WRAP_NEWLINES)) { + float lineHeight = textConfig->lineHeight != 0 ? textConfig->lineHeight : textElementData->preferredDimensions.height; Clay_LayoutElementArray_Add(&Clay__layoutElements, CLAY__INIT(Clay_LayoutElement) { .text = text, - .dimensions = textElementData->preferredDimensions, - .layoutConfig = &CLAY_LAYOUT_DEFAULT, + .dimensions = { textElementData->preferredDimensions.width, lineHeight }, + .minDimensions = textElementData->preferredDimensions, + .layoutConfig = CLAY_LAYOUT(.sizing = { .height = CLAY_SIZING_FIXED(lineHeight) }), .elementConfig = { .textElementConfig = containerElement->elementConfig.textElementConfig }, .id = Clay__RehashWithNumber(containerElement->id, containerElement->children.length), .elementType = CLAY__LAYOUT_ELEMENT_TYPE_TEXT, @@ -2028,15 +2029,17 @@ void Clay__CalculateFinalLayout() { wordStartIndex = lineStartIndex; wordEndIndex = lineStartIndex; } + float lineHeight = textConfig->lineHeight != 0 ? textConfig->lineHeight : lineDimensions.height; Clay_LayoutElementArray_Add(&Clay__layoutElements, CLAY__INIT(Clay_LayoutElement) { .text = stringToRender, - .dimensions = { lineDimensions.width, lineDimensions.height }, - .layoutConfig = &CLAY_LAYOUT_DEFAULT, + .dimensions = { lineDimensions.width, lineHeight }, + .minDimensions = { lineDimensions.width, lineDimensions.height }, + .layoutConfig = CLAY_LAYOUT(.sizing = { .height = CLAY_SIZING_FIXED(lineHeight) }), .elementConfig = { .textElementConfig = containerElement->elementConfig.textElementConfig }, .id = Clay__RehashWithNumber(containerElement->id, containerElement->children.length), .elementType = CLAY__LAYOUT_ELEMENT_TYPE_TEXT, }); - containerElement->dimensions.height += lineDimensions.height + (float)(containerElement->children.length > 0 ? textConfig->lineSpacing : 0); + containerElement->dimensions.height += lineHeight; containerElement->children.length++; lineDimensions = CLAY__INIT(Clay_Dimensions) {}; Clay__int32_tArray_Add(&Clay__layoutElementChildren, (int32_t)Clay__layoutElements.length - 1); @@ -2259,6 +2262,9 @@ void Clay__CalculateFinalLayout() { } case CLAY_RENDER_COMMAND_TYPE_TEXT: { renderCommand.text = currentElement->text; + if (currentElement->minDimensions.height != currentElement->dimensions.height) { + renderCommand.boundingBox.y += (currentElement->dimensions.height - currentElement->minDimensions.height) / 2; + } break; } case CLAY_RENDER_COMMAND_TYPE_BORDER: { // We render borders on close because they need to render above children @@ -2903,9 +2909,9 @@ void Clay__RenderDebugView() { // .fontId CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontTitle", 2), CLAY_STRING("Font ID"), infoTitleConfig); CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontBody", 2), Clay__IntToString(textConfig->fontId), infoTextConfig); - // .lineSpacing - CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontTitle", 3), CLAY_STRING("Line Spacing"), infoTitleConfig); - CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontBody", 3), Clay__IntToString(textConfig->lineSpacing), infoTextConfig); + // .lineHeight + CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontTitle", 3), CLAY_STRING("Line Height"), infoTitleConfig); + CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontBody", 3), textConfig->lineHeight == 0 ? CLAY_STRING("Auto") : Clay__IntToString(textConfig->lineHeight), infoTextConfig); // .letterSpacing CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontTitle", 4), CLAY_STRING("Letter Spacing"), infoTitleConfig); CLAY_TEXT(CLAY_IDI("Clay__DebugViewElementInfoRectangleFontBody", 4), Clay__IntToString(textConfig->letterSpacing), infoTextConfig); diff --git a/examples/raylib-sidebar-scrolling-container/main.c b/examples/raylib-sidebar-scrolling-container/main.c index b2df1fed..a36d7d05 100644 --- a/examples/raylib-sidebar-scrolling-container/main.c +++ b/examples/raylib-sidebar-scrolling-container/main.c @@ -73,7 +73,7 @@ Clay_RenderCommandArray CreateLayout() { CLAY_TEXT(CLAY_ID("BodyText2"), CLAY_STRING("Faucibus purus in massa tempor nec. Nec ullamcorper sit amet risus nullam eget felis eget nunc. Diam vulputate ut pharetra sit amet aliquam id diam. Lacus suspendisse faucibus interdum posuere lorem. A diam sollicitudin tempor id. Amet massa vitae tortor condimentum lacinia. Aliquet nibh praesent tristique magna."), - CLAY_TEXT_CONFIG(.fontSize = 24, .lineSpacing = 20, .textColor = {0,0,0,255})); + CLAY_TEXT_CONFIG(.fontSize = 24, .lineHeight = 60, .textColor = {0,0,0,255})); CLAY_TEXT(CLAY_ID("BodyText3"), CLAY_STRING("Suspendisse in est ante in nibh. Amet venenatis urna cursus eget nunc scelerisque viverra. Elementum sagittis vitae et leo duis ut diam quam nulla. Enim nulla aliquet porttitor lacus. Pellentesque habitant morbi tristique senectus et. Facilisi nullam vehicula ipsum a arcu cursus vitae.\nSem fringilla ut morbi tincidunt. Euismod quis viverra nibh cras pulvinar mattis nunc sed. Velit sed ullamcorper morbi tincidunt ornare massa. Varius quam quisque id diam vel quam. Nulla pellentesque dignissim enim sit amet venenatis. Enim lobortis scelerisque fermentum dui faucibus in. Pretium viverra suspendisse potenti nullam ac tortor vitae. Lectus vestibulum mattis ullamcorper velit sed. Eget mauris pharetra et ultrices neque ornare aenean euismod elementum. Habitant morbi tristique senectus et. Integer vitae justo eget magna fermentum iaculis eu. Semper quis lectus nulla at volutpat diam. Enim praesent elementum facilisis leo. Massa vitae tortor condimentum lacinia quis vel."), diff --git a/renderers/web/canvas2d/clay-canvas2d-renderer.html b/renderers/web/canvas2d/clay-canvas2d-renderer.html index f29fd777..79071211 100644 --- a/renderers/web/canvas2d/clay-canvas2d-renderer.html +++ b/renderers/web/canvas2d/clay-canvas2d-renderer.html @@ -110,7 +110,7 @@ { name: 'fontId', type: 'uint16_t' }, { name: 'fontSize', type: 'uint16_t' }, { name: 'letterSpacing', type: 'uint16_t' }, - { name: 'lineSpacing', type: 'uint16_t' }, + { name: 'lineHeight', type: 'uint16_t' }, { name: 'wrapMode', type: 'uint32_t' }, { name: 'disablePointerEvents', type: 'uint8_t' } ] diff --git a/renderers/web/html/clay-html-renderer.html b/renderers/web/html/clay-html-renderer.html index 87dda5d7..e853772a 100644 --- a/renderers/web/html/clay-html-renderer.html +++ b/renderers/web/html/clay-html-renderer.html @@ -128,7 +128,7 @@ { name: 'fontId', type: 'uint16_t' }, { name: 'fontSize', type: 'uint16_t' }, { name: 'letterSpacing', type: 'uint16_t' }, - { name: 'lineSpacing', type: 'uint16_t' }, + { name: 'lineHeight', type: 'uint16_t' }, { name: 'disablePointerEvents', type: 'uint8_t' } ] };