Remove Drawer2D_MeasureText and use Drawer2D_TextWidth/Height instead

This commit is contained in:
UnknownShadow200 2020-04-29 20:04:36 +10:00
parent f2c107b79e
commit 8ce30ea785
5 changed files with 23 additions and 33 deletions

View File

@ -594,15 +594,6 @@ int Drawer2D_FontHeight(const struct FontDesc* font, cc_bool useShadow) {
return height; 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) { void Drawer2D_DrawClippedText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y, int maxWidth) {
char strBuffer[512]; char strBuffer[512];
struct DrawTextArgs part; struct DrawTextArgs part;

View File

@ -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. */ /* Draws text using the given font at the given coordinates. */
CC_API void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y); CC_API void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y);
/* Returns how wide the given text would be when drawn. */ /* 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. */ /* Returns how tall the given text would be when drawn. */
/* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */ /* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */
int Drawer2D_TextHeight(struct DrawTextArgs* args); CC_API 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);
/* Similar to Drawer2D_DrawText, but trims the text with trailing ".." if wider than maxWidth. */ /* 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); 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. */ /* Returns the line height for drawing any character in the font. */

View File

@ -119,8 +119,8 @@ static void LButton_Draw(void* widget) {
int xOffset, yOffset; int xOffset, yOffset;
if (w->hidden) return; if (w->hidden) return;
xOffset = w->width - w->_textSize.Width; xOffset = w->width - w->_textWidth;
yOffset = w->height - w->_textSize.Height; yOffset = w->height - w->_textHeight;
DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true); DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true);
LButton_DrawBackground(w); LButton_DrawBackground(w);
@ -160,7 +160,8 @@ void LButton_SetConst(struct LButton* w, const char* text) {
struct DrawTextArgs args; struct DrawTextArgs args;
w->text = String_FromReadonly(text); w->text = String_FromReadonly(text);
DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true); 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; struct LInput* w = (struct LInput*)widget;
String text; char textBuffer[STRING_SIZE]; String text; char textBuffer[STRING_SIZE];
struct DrawTextArgs args; struct DrawTextArgs args;
Size2D size; int textWidth;
String_InitArray(text, textBuffer); String_InitArray(text, textBuffer);
LInput_GetText(w, &text); LInput_GetText(w, &text);
DrawTextArgs_Make(&args, &text, &Launcher_TextFont, false); DrawTextArgs_Make(&args, &text, &Launcher_TextFont, false);
size = Drawer2D_MeasureText(&args); textWidth = Drawer2D_TextWidth(&args);
w->width = max(w->minWidth, size.Width + 20); w->width = max(w->minWidth, textWidth + 20);
w->_textHeight = size.Height; w->_textHeight = Drawer2D_TextHeight(&args);
LInput_DrawOuterBorder(w); LInput_DrawOuterBorder(w);
LInput_DrawInnerBorder(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_) { void LInput_SetText(struct LInput* w, const String* text_) {
String text; char textBuffer[STRING_SIZE]; String text; char textBuffer[STRING_SIZE];
struct DrawTextArgs args; struct DrawTextArgs args;
Size2D size; int textWidth;
String_Copy(&w->text, text_); String_Copy(&w->text, text_);
String_InitArray(text, textBuffer); String_InitArray(text, textBuffer);
LInput_GetText(w, &text); LInput_GetText(w, &text);
DrawTextArgs_Make(&args, &text, &Launcher_TextFont, true); DrawTextArgs_Make(&args, &text, &Launcher_TextFont, true);
size = Drawer2D_MeasureText(&args); textWidth = Drawer2D_TextWidth(&args);
w->width = max(w->minWidth, size.Width + 20); w->width = max(w->minWidth, textWidth + 20);
w->_textHeight = size.Height; w->_textHeight = Drawer2D_TextHeight(&args);
} }
static CC_NOINLINE cc_bool LInput_AppendRaw(struct LInput* w, char c) { 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) { void LLabel_SetText(struct LLabel* w, const String* text) {
struct DrawTextArgs args; struct DrawTextArgs args;
Size2D size;
String_Copy(&w->text, text); String_Copy(&w->text, text);
DrawTextArgs_Make(&args, &w->text, w->font, true); DrawTextArgs_Make(&args, &w->text, w->font, true);
size = Drawer2D_MeasureText(&args); w->width = Drawer2D_TextWidth(&args);
w->width = size.Width; w->height = size.Height; w->height = Drawer2D_TextHeight(&args);
LWidget_CalcPosition(w); LWidget_CalcPosition(w);
} }

View File

@ -54,7 +54,7 @@ void LWidget_Redraw(void* widget);
struct LButton { struct LButton {
LWidget_Layout LWidget_Layout
String text; 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_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); CC_NOINLINE void LButton_SetConst(struct LButton* w, const char* text);

View File

@ -2651,21 +2651,23 @@ static void SpecialInputWidget_DrawTitles(struct SpecialInputWidget* w, Bitmap*
static Size2D SpecialInputWidget_MeasureContent(struct SpecialInputWidget* w, struct SpecialInputTab* tab) { static Size2D SpecialInputWidget_MeasureContent(struct SpecialInputWidget* w, struct SpecialInputTab* tab) {
struct DrawTextArgs args; struct DrawTextArgs args;
int textWidth, textHeight;
Size2D size; Size2D size;
int i, rows; int i, rows;
int maxWidth = 0; int maxWidth = 0;
DrawTextArgs_MakeEmpty(&args, w->font, false); DrawTextArgs_MakeEmpty(&args, w->font, false);
args.text.length = tab->charsPerItem; args.text.length = tab->charsPerItem;
textHeight = Drawer2D_TextHeight(&args);
for (i = 0; i < tab->contents.length; i += tab->charsPerItem) { for (i = 0; i < tab->contents.length; i += tab->charsPerItem) {
args.text.buffer = &tab->contents.buffer[i]; args.text.buffer = &tab->contents.buffer[i];
size = Drawer2D_MeasureText(&args); textWidth = Drawer2D_TextWidth(&args);
maxWidth = max(maxWidth, size.Width); maxWidth = max(maxWidth, textWidth);
} }
w->elementWidth = maxWidth + SPECIAL_CONTENT_SPACING; w->elementWidth = maxWidth + SPECIAL_CONTENT_SPACING;
w->elementHeight = size.Height + SPECIAL_CONTENT_SPACING; w->elementHeight = textHeight + SPECIAL_CONTENT_SPACING;
rows = Math_CeilDiv(tab->contents.length / tab->charsPerItem, tab->itemsPerRow); rows = Math_CeilDiv(tab->contents.length / tab->charsPerItem, tab->itemsPerRow);
size.Width = w->elementWidth * tab->itemsPerRow; size.Width = w->elementWidth * tab->itemsPerRow;