From 8ce30ea7858d8f2ae4f2e24d5922e8eb0a320f31 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 29 Apr 2020 20:04:36 +1000 Subject: [PATCH] Remove Drawer2D_MeasureText and use Drawer2D_TextWidth/Height instead --- src/Drawer2D.c | 9 --------- src/Drawer2D.h | 7 ++----- src/LWidgets.c | 28 ++++++++++++++-------------- src/LWidgets.h | 2 +- src/Widgets.c | 10 ++++++---- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/Drawer2D.c b/src/Drawer2D.c index e89080099..7d84e8f53 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -594,15 +594,6 @@ int Drawer2D_FontHeight(const struct FontDesc* font, cc_bool useShadow) { return height; } -Size2D Drawer2D_MeasureText(struct DrawTextArgs* args) { - Size2D size; - size.Width = Drawer2D_TextWidth(args); - size.Height = Drawer2D_TextHeight(args); - - if (!size.Width) size.Height = 0; - return size; -} - void Drawer2D_DrawClippedText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y, int maxWidth) { char strBuffer[512]; struct DrawTextArgs part; diff --git a/src/Drawer2D.h b/src/Drawer2D.h index fc18d6d52..beabb08f3 100644 --- a/src/Drawer2D.h +++ b/src/Drawer2D.h @@ -59,13 +59,10 @@ void Drawer2D_Underline(Bitmap* bmp, int x, int y, int width, int height, Bitmap /* Draws text using the given font at the given coordinates. */ CC_API void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y); /* Returns how wide the given text would be when drawn. */ -int Drawer2D_TextWidth(struct DrawTextArgs* args); +CC_API int Drawer2D_TextWidth(struct DrawTextArgs* args); /* Returns how tall the given text would be when drawn. */ /* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */ -int Drawer2D_TextHeight(struct DrawTextArgs* args); -/* Returns size the given text would be when drawn. */ -/* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */ -CC_API Size2D Drawer2D_MeasureText(struct DrawTextArgs* args); +CC_API int Drawer2D_TextHeight(struct DrawTextArgs* args); /* Similar to Drawer2D_DrawText, but trims the text with trailing ".." if wider than maxWidth. */ void Drawer2D_DrawClippedText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y, int maxWidth); /* Returns the line height for drawing any character in the font. */ diff --git a/src/LWidgets.c b/src/LWidgets.c index 4e257435f..a49695964 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -119,8 +119,8 @@ static void LButton_Draw(void* widget) { int xOffset, yOffset; if (w->hidden) return; - xOffset = w->width - w->_textSize.Width; - yOffset = w->height - w->_textSize.Height; + xOffset = w->width - w->_textWidth; + yOffset = w->height - w->_textHeight; DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true); LButton_DrawBackground(w); @@ -160,7 +160,8 @@ void LButton_SetConst(struct LButton* w, const char* text) { struct DrawTextArgs args; w->text = String_FromReadonly(text); DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true); - w->_textSize = Drawer2D_MeasureText(&args); + w->_textWidth = Drawer2D_TextWidth(&args); + w->_textHeight = Drawer2D_TextHeight(&args); } @@ -255,15 +256,15 @@ static void LInput_Draw(void* widget) { struct LInput* w = (struct LInput*)widget; String text; char textBuffer[STRING_SIZE]; struct DrawTextArgs args; - Size2D size; + int textWidth; String_InitArray(text, textBuffer); LInput_GetText(w, &text); DrawTextArgs_Make(&args, &text, &Launcher_TextFont, false); - size = Drawer2D_MeasureText(&args); - w->width = max(w->minWidth, size.Width + 20); - w->_textHeight = size.Height; + textWidth = Drawer2D_TextWidth(&args); + w->width = max(w->minWidth, textWidth + 20); + w->_textHeight = Drawer2D_TextHeight(&args); LInput_DrawOuterBorder(w); LInput_DrawInnerBorder(w); @@ -459,16 +460,16 @@ void LInput_Init(struct LScreen* s, struct LInput* w, int width, const char* hin void LInput_SetText(struct LInput* w, const String* text_) { String text; char textBuffer[STRING_SIZE]; struct DrawTextArgs args; - Size2D size; + int textWidth; String_Copy(&w->text, text_); String_InitArray(text, textBuffer); LInput_GetText(w, &text); DrawTextArgs_Make(&args, &text, &Launcher_TextFont, true); - size = Drawer2D_MeasureText(&args); - w->width = max(w->minWidth, size.Width + 20); - w->_textHeight = size.Height; + textWidth = Drawer2D_TextWidth(&args); + w->width = max(w->minWidth, textWidth + 20); + w->_textHeight = Drawer2D_TextHeight(&args); } static CC_NOINLINE cc_bool LInput_AppendRaw(struct LInput* w, char c) { @@ -565,12 +566,11 @@ void LLabel_Init(struct LScreen* s, struct LLabel* w, const char* text) { void LLabel_SetText(struct LLabel* w, const String* text) { struct DrawTextArgs args; - Size2D size; String_Copy(&w->text, text); DrawTextArgs_Make(&args, &w->text, w->font, true); - size = Drawer2D_MeasureText(&args); - w->width = size.Width; w->height = size.Height; + w->width = Drawer2D_TextWidth(&args); + w->height = Drawer2D_TextHeight(&args); LWidget_CalcPosition(w); } diff --git a/src/LWidgets.h b/src/LWidgets.h index 29a542785..3cd490a89 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -54,7 +54,7 @@ void LWidget_Redraw(void* widget); struct LButton { LWidget_Layout String text; - Size2D _textSize; + int _textWidth, _textHeight; }; CC_NOINLINE void LButton_Init(struct LScreen* s, struct LButton* w, int width, int height, const char* text); CC_NOINLINE void LButton_SetConst(struct LButton* w, const char* text); diff --git a/src/Widgets.c b/src/Widgets.c index 78f17a017..a7ee85851 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -2651,21 +2651,23 @@ static void SpecialInputWidget_DrawTitles(struct SpecialInputWidget* w, Bitmap* static Size2D SpecialInputWidget_MeasureContent(struct SpecialInputWidget* w, struct SpecialInputTab* tab) { struct DrawTextArgs args; + int textWidth, textHeight; Size2D size; int i, rows; int maxWidth = 0; DrawTextArgs_MakeEmpty(&args, w->font, false); args.text.length = tab->charsPerItem; + textHeight = Drawer2D_TextHeight(&args); for (i = 0; i < tab->contents.length; i += tab->charsPerItem) { args.text.buffer = &tab->contents.buffer[i]; - size = Drawer2D_MeasureText(&args); - maxWidth = max(maxWidth, size.Width); + textWidth = Drawer2D_TextWidth(&args); + maxWidth = max(maxWidth, textWidth); } - w->elementWidth = maxWidth + SPECIAL_CONTENT_SPACING; - w->elementHeight = size.Height + SPECIAL_CONTENT_SPACING; + w->elementWidth = maxWidth + SPECIAL_CONTENT_SPACING; + w->elementHeight = textHeight + SPECIAL_CONTENT_SPACING; rows = Math_CeilDiv(tab->contents.length / tab->charsPerItem, tab->itemsPerRow); size.Width = w->elementWidth * tab->itemsPerRow;