From ce5611c91ca038d226e0941b1dd99ebb09274fa8 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 8 Nov 2020 00:21:53 +1100 Subject: [PATCH] Cleanup Http/Drawer2D a little bit more --- src/Drawer2D.c | 61 ++++++++++++++++++++++++++------------------------ src/Drawer2D.h | 5 ++++- src/Http.c | 50 ++++++++++++++++++++--------------------- src/Http.h | 8 +------ 4 files changed, 61 insertions(+), 63 deletions(-) diff --git a/src/Drawer2D.c b/src/Drawer2D.c index 1a60801d6..5cd071a55 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -59,36 +59,14 @@ void Drawer2D_SetDefaultFont(const cc_string* fontName) { static int Drawer2D_AdjHeight(int point) { return Math_CeilDiv(point * 3, 2); } void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags) { - cc_string* font; - cc_result res; - int i; + if (!Drawer2D_BitmappedText) { Font_MakeDefault(desc, size, flags); return; } - if (Drawer2D_BitmappedText) { - /* TODO: Scale X and Y independently */ - size = Display_ScaleY(size); - desc->handle = NULL; - desc->size = size; - desc->flags = flags; - desc->height = Drawer2D_AdjHeight(size); - return; - } - - for (i = 0; i < Array_Elems(font_candidates); i++) { - font = &font_candidates[i]; - if (!font->length) continue; - res = Font_Make(desc, &font_candidates[i], size, flags); - - if (res == ERR_INVALID_ARGUMENT) { - /* Fon't doesn't exist in list, skip over it */ - } else if (res) { - Font_Free(desc); - Logger_SysWarn2(res, "creating font", font); - } else { - if (i) String_Copy(&font_candidates[0], font); - return; - } - } - Logger_Abort2(res, "Failed to init default font"); + /* TODO: Scale X and Y independently */ + size = Display_ScaleY(size); + desc->handle = NULL; + desc->size = size; + desc->flags = flags; + desc->height = Drawer2D_AdjHeight(size); } static struct Bitmap fontBitmap; @@ -706,6 +684,8 @@ cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size, desc->height = 0; return 0; } +void Font_MakeDefault(struct FontDesc* desc, int size, int flags) { Font_Make(desc, NULL, size, flags); } + void Font_Free(struct FontDesc* desc) { desc->size = 0; } @@ -1034,6 +1014,29 @@ cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size, return 0; } +void Font_MakeDefault(struct FontDesc* desc, int size, int flags) { + cc_string* font; + cc_result res; + int i; + + for (i = 0; i < Array_Elems(font_candidates); i++) { + font = &font_candidates[i]; + if (!font->length) continue; + res = Font_Make(desc, &font_candidates[i], size, flags); + + if (res == ERR_INVALID_ARGUMENT) { + /* Fon't doesn't exist in list, skip over it */ + } else if (res) { + Font_Free(desc); + Logger_SysWarn2(res, "creating font", font); + } else { + if (i) String_Copy(&font_candidates[0], font); + return; + } + } + Logger_Abort2(res, "Failed to init default font"); +} + void Font_Free(struct FontDesc* desc) { struct SysFont* font; desc->size = 0; diff --git a/src/Drawer2D.h b/src/Drawer2D.h index c5cae8d19..285e00ce5 100644 --- a/src/Drawer2D.h +++ b/src/Drawer2D.h @@ -20,7 +20,7 @@ void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, struct FontDesc* font, cc /* Sets default system font name and raises ChatEvents.FontChanged */ void Drawer2D_SetDefaultFont(const cc_string* fontName); -/* Initialises the given font. When Drawer2D_BitmappedText is false, creates native font handle using Font_Make. */ +/* Initialises the given font. When Drawer2D_BitmappedText is false, creates a system font using Font_MakeDefault. */ CC_API void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags); /* Whether text should be drawn and measured using the currently set font bitmap. */ @@ -110,6 +110,9 @@ void Font_SetPadding(struct FontDesc* desc, int amount); cc_string Font_Lookup(const cc_string* fontName, int flags); /* Allocates a new system font from the given arguments. */ cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size, int flags); +/* Allocates a new system font from the given arguments using default system font. */ +/* NOTE: Unlike Font_Make, this may fallback onto other system fonts (e.g. Arial, Roboto, etc) */ +void Font_MakeDefault(struct FontDesc* desc, int size, int flags); /* Frees an allocated font. */ CC_API void Font_Free(struct FontDesc* desc); /* Attempts to decode one or fonts from the given file. */ diff --git a/src/Http.c b/src/Http.c index 21c9987d4..627873c18 100644 --- a/src/Http.c +++ b/src/Http.c @@ -7,7 +7,8 @@ #include "Game.h" #include "Utils.h" -void HttpRequest_Free(struct HttpRequest* request) { +/* Frees data from a HTTP request. */ +static void HttpRequest_Free(struct HttpRequest* request) { Mem_Free(request->data); request->data = NULL; request->size = 0; @@ -219,7 +220,7 @@ static void Http_CleanCacheTask(struct ScheduledTask* task) { item = &processedReqs.entries[i]; if (item->timeDownloaded + (10 * 1000) >= now) continue; - HttpRequest_Free(item); + Mem_Free(item->data); RequestList_RemoveAt(&processedReqs, i); } } @@ -586,11 +587,7 @@ static cc_result Http_BackendDo(struct HttpRequest* req, cc_string* url) { char urlStr[NATIVE_STR_LEN]; void* post_data = req->data; CURLcode res; - - if (!curlSupported) { - HttpRequest_Free(req); - return ERR_NOT_SUPPORTED; - } + if (!curlSupported) return ERR_NOT_SUPPORTED; req->meta = NULL; Http_SetRequestHeaders(req); @@ -665,6 +662,25 @@ struct HttpCacheEntry { #define HTTP_CACHE_ENTRIES 10 static struct HttpCacheEntry http_cache[HTTP_CACHE_ENTRIES]; +/* Converts characters to UTF8, then calls Http_URlEncode on them. */ +static void HttpCache_UrlEncodeUrl(cc_string* dst, const cc_string* src) { + cc_uint8 data[4]; + int i, len; + char c; + + for (i = 0; i < src->length; i++) { + c = src->buffer[i]; + len = Convert_CP437ToUtf8(c, data); + + /* URL path/query must not be URL encoded (it normally would be) */ + if (c == '/' || c == '?' || c == '=') { + String_Append(dst, c); + } else { + Http_UrlEncode(dst, data, len); + } + } +} + /* Splits up the components of a URL */ static void HttpCache_MakeEntry(const cc_string* url, struct HttpCacheEntry* entry, cc_string* resource) { cc_string scheme, path, addr, name, port, _resource; @@ -678,7 +694,7 @@ static void HttpCache_MakeEntry(const cc_string* url, struct HttpCacheEntry* ent String_UNSAFE_Separate(&path, '/', &addr, &_resource); String_UNSAFE_Separate(&addr, ':', &name, &port); /* Address may have unicode characters - need to percent encode them */ - Http_UrlEncodeUrl(resource, &_resource); + HttpCache_UrlEncodeUrl(resource, &_resource); String_InitArray_NT(entry->Address, entry->_addressBuffer); String_Copy(&entry->Address, &name); @@ -1135,24 +1151,6 @@ void Http_UrlEncodeUtf8(cc_string* dst, const cc_string* src) { } } -void Http_UrlEncodeUrl(cc_string* dst, const cc_string* src) { - cc_uint8 data[4]; - int i, len; - char c; - - for (i = 0; i < src->length; i++) { - c = src->buffer[i]; - len = Convert_CP437ToUtf8(c, data); - - /* URL path/query must not be URL encoded (it normally would be) */ - if (c == '/' || c == '?' || c == '=') { - String_Append(dst, c); - } else { - Http_UrlEncode(dst, data, len); - } - } -} - /*########################################################################################################################* *-----------------------------------------------------Http component------------------------------------------------------* diff --git a/src/Http.h b/src/Http.h index 25fb20b51..e790013ea 100644 --- a/src/Http.h +++ b/src/Http.h @@ -40,9 +40,6 @@ struct HttpRequest { struct StringsBuffer* cookies; /* Cookie list sent in requests. May be modified by the response. */ }; -/* Frees data from a HTTP request. */ -void HttpRequest_Free(struct HttpRequest* request); - /* Aschronously performs a http GET request to download a skin. */ /* If url is a skin, downloads from there. (if not, downloads from SKIN_SERVER/[skinName].png) */ int Http_AsyncGetSkin(const cc_string* skinName); @@ -62,11 +59,8 @@ void Http_TryCancel(int reqID); /* Encodes data using % or URL encoding. */ void Http_UrlEncode(cc_string* dst, const cc_uint8* data, int len); -/* Converts characters to UTF8, then calls Http_URlEncode on them. */ +/* Converts characters to UTF8, then calls Http_UrlEncode on them. */ void Http_UrlEncodeUtf8(cc_string* dst, const cc_string* src); -/* Converts characters to UTF8, then calls Http_URlEncode on them. */ -/* NOTE: '/' is NOT url encoded, whereas Http_UrlEncodeUtf8 url encodes them */ -void Http_UrlEncodeUrl(cc_string* dst, const cc_string* src); /* Outputs more detailed information about errors with http requests. */ cc_bool Http_DescribeError(cc_result res, cc_string* dst);