From fec606a8c3d1161bde35855c1a00b250e2d41307 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 30 Jun 2022 16:17:31 -0700 Subject: [PATCH] change textbounds to take a packer instead of batch --- include/Wellspring.h | 2 +- src/Wellspring.c | 60 +++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/include/Wellspring.h b/include/Wellspring.h index 56f0523..e198334 100644 --- a/include/Wellspring.h +++ b/include/Wellspring.h @@ -150,7 +150,7 @@ WELLSPRINGAPI void Wellspring_StartTextBatch( ); WELLSPRINGAPI uint8_t Wellspring_TextBounds( - Wellspring_TextBatch *textBatch, + Wellspring_Packer* packer, float x, float y, Wellspring_HorizontalAlignment horizontalAlignment, diff --git a/src/Wellspring.c b/src/Wellspring.c index bfff7b8..b3eff8c 100644 --- a/src/Wellspring.c +++ b/src/Wellspring.c @@ -384,8 +384,8 @@ static inline uint32_t IsWhitespace(uint32_t codepoint) } } -uint8_t Wellspring_TextBounds( - Wellspring_TextBatch* textBatch, +static uint8_t Wellspring_Internal_TextBounds( + Packer* packer, float x, float y, Wellspring_HorizontalAlignment horizontalAlignment, @@ -394,8 +394,6 @@ uint8_t Wellspring_TextBounds( uint32_t strLengthInBytes, Wellspring_Rectangle *pRectangle ) { - Batch* batch = (Batch*)textBatch; - Packer* myPacker = batch->currentPacker; uint32_t decodeState = 0; uint32_t codepoint; int32_t glyphIndex; @@ -412,7 +410,7 @@ uint8_t Wellspring_TextBounds( float startX = x; float advance = 0; - y += Wellspring_INTERNAL_GetVerticalAlignOffset(myPacker->font, verticalAlignment, myPacker->scale); + y += Wellspring_INTERNAL_GetVerticalAlignOffset(packer->font, verticalAlignment, packer->scale); for (i = 0; i < strLengthInBytes; i += 1) { @@ -430,24 +428,24 @@ uint8_t Wellspring_TextBounds( if (IsWhitespace(codepoint)) { int32_t ws_adv, ws_bearing; - stbtt_GetCodepointHMetrics(&myPacker->font->fontInfo, codepoint, &ws_adv, &ws_bearing); - x += myPacker->scale * ws_adv; - maxX += myPacker->scale * ws_adv; + stbtt_GetCodepointHMetrics(&packer->font->fontInfo, codepoint, &ws_adv, &ws_bearing); + x += packer->scale * ws_adv; + maxX += packer->scale * ws_adv; continue; } rangeData = NULL; /* Find the packed char data */ - for (j = 0; j < myPacker->rangeCount; j += 1) + for (j = 0; j < packer->rangeCount; j += 1) { if ( - codepoint >= myPacker->ranges[j].firstCodepoint && - codepoint < myPacker->ranges[j].firstCodepoint + myPacker->ranges[j].charCount + codepoint >= packer->ranges[j].firstCodepoint && + codepoint < packer->ranges[j].firstCodepoint + packer->ranges[j].charCount ) { - rangeData = myPacker->ranges[j].data; - rangeIndex = codepoint - myPacker->ranges[j].firstCodepoint; - rangeFontSize = myPacker->ranges[j].fontSize; + rangeData = packer->ranges[j].data; + rangeIndex = codepoint - packer->ranges[j].firstCodepoint; + rangeFontSize = packer->ranges[j].fontSize; break; } } @@ -458,17 +456,17 @@ uint8_t Wellspring_TextBounds( return 0; } - glyphIndex = stbtt_FindGlyphIndex(&myPacker->font->fontInfo, codepoint); + glyphIndex = stbtt_FindGlyphIndex(&packer->font->fontInfo, codepoint); if (i > 0) { - x += myPacker->scale * stbtt_GetGlyphKernAdvance(&myPacker->font->fontInfo, previousGlyphIndex, glyphIndex); + x += packer->scale * stbtt_GetGlyphKernAdvance(&packer->font->fontInfo, previousGlyphIndex, glyphIndex); } stbtt_GetPackedQuad( rangeData, - myPacker->width, - myPacker->height, + packer->width, + packer->height, rangeIndex, &x, &y, @@ -505,6 +503,28 @@ uint8_t Wellspring_TextBounds( return 1; } +uint8_t Wellspring_TextBounds( + Wellspring_Packer* packer, + float x, + float y, + Wellspring_HorizontalAlignment horizontalAlignment, + Wellspring_VerticalAlignment verticalAlignment, + const uint8_t* strBytes, + uint32_t strLengthInBytes, + Wellspring_Rectangle* pRectangle +) { + return Wellspring_Internal_TextBounds( + (Packer*) packer, + x, + y, + horizontalAlignment, + verticalAlignment, + strBytes, + strLengthInBytes, + pRectangle + ); +} + uint8_t Wellspring_Draw( Wellspring_TextBatch *textBatch, float x, @@ -536,7 +556,7 @@ uint8_t Wellspring_Draw( /* FIXME: If we horizontally align, we have to decode and process glyphs twice, very inefficient. */ if (horizontalAlignment == WELLSPRING_HORIZONTALALIGNMENT_RIGHT) { - if (!Wellspring_TextBounds(textBatch, x, y, horizontalAlignment, verticalAlignment, strBytes, strLengthInBytes, &bounds)) + if (!Wellspring_Internal_TextBounds(myPacker, x, y, horizontalAlignment, verticalAlignment, strBytes, strLengthInBytes, &bounds)) { /* Something went wrong while calculating bounds. */ return 0; @@ -546,7 +566,7 @@ uint8_t Wellspring_Draw( } else if (horizontalAlignment == WELLSPRING_HORIZONTALALIGNMENT_CENTER) { - if (!Wellspring_TextBounds(textBatch, x, y, horizontalAlignment, verticalAlignment, strBytes, strLengthInBytes, &bounds)) + if (!Wellspring_Internal_TextBounds(myPacker, x, y, horizontalAlignment, verticalAlignment, strBytes, strLengthInBytes, &bounds)) { /* Something went wrong while calculating bounds. */ return 0;