From 626b35a9938a75a025eb7f945fbba1c2eb4eb6f8 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 28 Jul 2022 20:05:08 +1000 Subject: [PATCH] Change Drawer2D_UNSAFE_NextPart to return color code instead of color --- src/Drawer2D.c | 31 ++++++++++++++++++------------- src/Drawer2D.h | 2 +- src/interop_ios.m | 31 +++++++++++++++++-------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/Drawer2D.c b/src/Drawer2D.c index b3c00ec20..f66b9024a 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -316,16 +316,18 @@ cc_bool Drawer2D_ValidColorCodeAt(const cc_string* text, int i) { return BitmapCol_A(Drawer2D_GetColor(text->buffer[i])) != 0; } -cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, BitmapCol* color) { - BitmapCol c; +cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, char* colorCode) { + BitmapCol color; + char cur; int i; /* check if current part starts with a colour code */ if (left->length >= 2 && left->buffer[0] == '&') { - c = Drawer2D_GetColor(left->buffer[1]); + cur = left->buffer[1]; + color = Drawer2D_GetColor(cur); - if (BitmapCol_A(c)) { - *color = c; + if (BitmapCol_A(color)) { + *colorCode = cur; left->buffer += 2; left->length -= 2; } @@ -347,9 +349,9 @@ cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, BitmapCol* co cc_bool Drawer2D_IsEmptyText(const cc_string* text) { cc_string left = *text, part; - BitmapCol color; + char colorCode; - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { if (part.length) return false; } @@ -358,9 +360,9 @@ cc_bool Drawer2D_IsEmptyText(const cc_string* text) { void Drawer2D_WithoutColors(cc_string* str, const cc_string* src) { cc_string left = *src, part; - BitmapCol color; + char colorCode; - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { String_AppendString(str, &part); } @@ -1287,10 +1289,10 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) { struct FontDesc* font = args->font; cc_string left = args->text, part; double width = 0; - BitmapCol color; + char colorCode; interop_SetFont(font->handle, font->size, font->flags); - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { char buffer[NATIVE_STR_LEN]; int len = Platform_EncodeUtf8(buffer, &part); @@ -1302,7 +1304,8 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) { static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) { struct FontDesc* font = args->font; cc_string left = args->text, part; - BitmapCol color = Drawer2D.Colors['f']; + BitmapCol color; + char colorCode = 'f'; double xOffset = 0; char hexBuffer[7]; cc_string hex; @@ -1311,10 +1314,12 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int y += (args->font->height - args->font->size) / 2; interop_SetFont(font->handle, font->size, font->flags); - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { char buffer[NATIVE_STR_LEN]; int len = Platform_EncodeUtf8(buffer, &part); + + color = Drawer2D_GetColor(colorCode); if (shadow) color = GetShadowColor(color); String_InitArray(hex, hexBuffer); diff --git a/src/Drawer2D.h b/src/Drawer2D.h index 6e442f16f..7f16f0bfe 100644 --- a/src/Drawer2D.h +++ b/src/Drawer2D.h @@ -91,7 +91,7 @@ void Drawer2D_WithoutColors(cc_string* str, const cc_string* src); char Drawer2D_LastColor(const cc_string* text, int start); /* Returns whether the color code is f, F or \0 */ cc_bool Drawer2D_IsWhiteColor(char c); -cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, BitmapCol* color); +cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, char* colorCode); /* Allocates a new instance of the default font using the given size and flags */ /* Uses Font_MakeBitmapped or SysFont_MakeDefault depending on Drawer2D_BitmappedText */ diff --git a/src/interop_ios.m b/src/interop_ios.m index 16b0715ee..3a987c44a 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -250,13 +250,14 @@ static NSString* ToNSString(const cc_string* text) { } static NSMutableAttributedString* ToAttributedString(const cc_string* text) { - cc_string left = *text, part; - BitmapCol color = Drawer2D.Colors['f']; + cc_string left = *text, part; + char colorCode = 'f'; NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init]; - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { - NSString* bit = ToNSString(&part); + BitmapCol color = Drawer2D_GetColor(colorCode); + NSString* bit = ToNSString(&part); NSDictionary* attrs = @{ //NSFontAttributeName : font, @@ -766,14 +767,15 @@ void interop_SysFontFree(void* handle) { } int interop_SysTextWidth(struct DrawTextArgs* args) { - UIFont* font = (__bridge UIFont*)args->font->handle; - cc_string left = args->text, part; - BitmapCol color = Drawer2D.Colors['f']; + UIFont* font = (__bridge UIFont*)args->font->handle; + cc_string left = args->text, part; + char colorCode = 'f'; NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init]; - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { - NSString* bit = ToNSString(&part); + BitmapCol color = Drawer2D_GetColor(colorCode); + NSString* bit = ToNSString(&part); NSDictionary* attrs = @{ NSFontAttributeName : font, @@ -792,14 +794,15 @@ int interop_SysTextWidth(struct DrawTextArgs* args) { } void interop_SysTextDraw(struct DrawTextArgs* args, struct Context2D* ctx, int x, int y, cc_bool shadow) { - UIFont* font = (__bridge UIFont*)args->font->handle; - cc_string left = args->text, part; - BitmapCol color = Drawer2D.Colors['f']; + UIFont* font = (__bridge UIFont*)args->font->handle; + cc_string left = args->text, part; + char colorCode = 'f'; NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init]; - while (Drawer2D_UNSAFE_NextPart(&left, &part, &color)) + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) { - NSString* bit = ToNSString(&part); + BitmapCol color = Drawer2D_GetColor(colorCode); + NSString* bit = ToNSString(&part); NSDictionary* attrs = @{ NSFontAttributeName : font,