From 19f2ca5baf650952f212747a9607b33ac97382f7 Mon Sep 17 00:00:00 2001 From: Liam Twigger <4939563+SnapperTT@users.noreply.github.com> Date: Sat, 14 Sep 2019 10:26:51 +1000 Subject: [PATCH 1/4] Fixes gcc wnarrowing spam (issue #38) --- SDL_FontCache.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SDL_FontCache.c b/SDL_FontCache.c index 7d3c508..80cd375 100644 --- a/SDL_FontCache.c +++ b/SDL_FontCache.c @@ -231,7 +231,7 @@ char* FC_GetStringASCII_Latin1(void) FC_Rect FC_MakeRect(float x, float y, float w, float h) { - FC_Rect r = {x, y, w, h}; + FC_Rect r = {(int)x, (int)y, (int)w, (int)h}; return r; } @@ -617,7 +617,7 @@ static_inline FC_Rect FC_RectUnion(FC_Rect A, FC_Rect B) x2 = FC_MAX(A.x+A.w, B.x+B.w); y2 = FC_MAX(A.y+A.h, B.y+B.h); { - FC_Rect result = {x, y, FC_MAX(0, x2 - x), FC_MAX(0, y2 - y)}; + FC_Rect result = {(int)x, (int)y, FC_MAX(0, (int)(x2 - x)), FC_MAX(0, (int)(y2 - y))}; return result; } } @@ -2184,7 +2184,7 @@ FC_Rect FC_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, const char* formatted_text, ...) { - FC_Rect box = {x, y, width, 0}; + FC_Rect box = {(int)x, (int)y, (int)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2201,7 +2201,7 @@ FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 w FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_AlignEnum align, const char* formatted_text, ...) { - FC_Rect box = {x, y, width, 0}; + FC_Rect box = {(int)x, (int)y, (int)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2230,7 +2230,7 @@ FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Scale scale, const char* formatted_text, ...) { - FC_Rect box = {x, y, width, 0}; + FC_Rect box = {(int)x, (int)y, (int)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2247,7 +2247,7 @@ FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, SDL_Color color, const char* formatted_text, ...) { - FC_Rect box = {x, y, width, 0}; + FC_Rect box = {(int)x, (int)y, (int)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2264,7 +2264,7 @@ FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...) { - FC_Rect box = {x, y, width, 0}; + FC_Rect box = {(int)x, (int)y, (int)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2293,7 +2293,7 @@ FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Ui static FC_Rect FC_RenderCenter(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text) { - FC_Rect result = {x, y, 0, 0}; + FC_Rect result = {(int)x, (int)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2326,7 +2326,7 @@ static FC_Rect FC_RenderCenter(FC_Font* font, FC_Target* dest, float x, float y, static FC_Rect FC_RenderRight(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text) { - FC_Rect result = {x, y, 0, 0}; + FC_Rect result = {(int)x, (int)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2721,7 +2721,7 @@ SDL_Color FC_GetDefaultColor(FC_Font* font) FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...) { - FC_Rect result = {x, y, 0, 0}; + FC_Rect result = {(int)x, (int)y, 0, 0}; if(formatted_text == NULL) return result; From 39ad9d52e53e73e6473ccb033b329ef3419f9834 Mon Sep 17 00:00:00 2001 From: Liam Twigger <4939563+SnapperTT@users.noreply.github.com> Date: Mon, 21 Oct 2019 14:13:38 +1000 Subject: [PATCH 2/4] added FC_COORD define, switched includes to <> --- SDL_FontCache.c | 26 +++++++++++++------------- SDL_FontCache.h | 9 ++++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/SDL_FontCache.c b/SDL_FontCache.c index 80cd375..13300ba 100644 --- a/SDL_FontCache.c +++ b/SDL_FontCache.c @@ -231,7 +231,7 @@ char* FC_GetStringASCII_Latin1(void) FC_Rect FC_MakeRect(float x, float y, float w, float h) { - FC_Rect r = {(int)x, (int)y, (int)w, (int)h}; + FC_Rect r = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)w, (FC_COORD)h}; return r; } @@ -617,7 +617,7 @@ static_inline FC_Rect FC_RectUnion(FC_Rect A, FC_Rect B) x2 = FC_MAX(A.x+A.w, B.x+B.w); y2 = FC_MAX(A.y+A.h, B.y+B.h); { - FC_Rect result = {(int)x, (int)y, FC_MAX(0, (int)(x2 - x)), FC_MAX(0, (int)(y2 - y))}; + FC_Rect result = {(FC_COORD)x, (FC_COORD)y, FC_MAX(0, (FC_COORD)(x2 - x)), FC_MAX(0, (FC_COORD)(y2 - y))}; return result; } } @@ -686,16 +686,16 @@ FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* des if(xscale < 0) { xscale = -xscale; - flip = (SDL_RendererFlip) ((int)flip | (int)SDL_FLIP_HORIZONTAL); + flip = (SDL_RendererFlip) ((FC_COORD)flip | (FC_COORD)SDL_FLIP_HORIZONTAL); } if(yscale < 0) { yscale = -yscale; - flip = (SDL_RendererFlip) ((int)flip | (int)SDL_FLIP_VERTICAL); + flip = (SDL_RendererFlip) ((FC_COORD)flip | (FC_COORD)SDL_FLIP_VERTICAL); } SDL_Rect r = *srcrect; - SDL_Rect dr = {(int)x, (int)y, (int)(xscale*r.w), (int)(yscale*r.h)}; + SDL_Rect dr = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)(xscale*r.w), (FC_COORD)(yscale*r.h)}; SDL_RenderCopyEx(dest, src, &r, &dr, 0, NULL, flip); } #endif @@ -2184,7 +2184,7 @@ FC_Rect FC_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, const char* formatted_text, ...) { - FC_Rect box = {(int)x, (int)y, (int)width, 0}; + FC_Rect box = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2201,7 +2201,7 @@ FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 w FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_AlignEnum align, const char* formatted_text, ...) { - FC_Rect box = {(int)x, (int)y, (int)width, 0}; + FC_Rect box = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2230,7 +2230,7 @@ FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Scale scale, const char* formatted_text, ...) { - FC_Rect box = {(int)x, (int)y, (int)width, 0}; + FC_Rect box = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2247,7 +2247,7 @@ FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, SDL_Color color, const char* formatted_text, ...) { - FC_Rect box = {(int)x, (int)y, (int)width, 0}; + FC_Rect box = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2264,7 +2264,7 @@ FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uin FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...) { - FC_Rect box = {(int)x, (int)y, (int)width, 0}; + FC_Rect box = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2293,7 +2293,7 @@ FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Ui static FC_Rect FC_RenderCenter(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text) { - FC_Rect result = {(int)x, (int)y, 0, 0}; + FC_Rect result = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2326,7 +2326,7 @@ static FC_Rect FC_RenderCenter(FC_Font* font, FC_Target* dest, float x, float y, static FC_Rect FC_RenderRight(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text) { - FC_Rect result = {(int)x, (int)y, 0, 0}; + FC_Rect result = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2721,7 +2721,7 @@ SDL_Color FC_GetDefaultColor(FC_Font* font) FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...) { - FC_Rect result = {(int)x, (int)y, 0, 0}; + FC_Rect result = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(formatted_text == NULL) return result; diff --git a/SDL_FontCache.h b/SDL_FontCache.h index 77f5fb4..df94f08 100644 --- a/SDL_FontCache.h +++ b/SDL_FontCache.h @@ -33,11 +33,14 @@ THE SOFTWARE. #ifndef _SDL_FONTCACHE_H__ #define _SDL_FONTCACHE_H__ -#include "SDL.h" -#include "SDL_ttf.h" +#include +#include #ifdef FC_USE_SDL_GPU - #include "SDL_gpu.h" + #include + #define FC_COORD float +#else + #define FC_COORD int #endif From 7a0bd999d7d6aa8fd6f7b4658e539dff7ccac327 Mon Sep 17 00:00:00 2001 From: Liam Twigger <4939563+SnapperTT@users.noreply.github.com> Date: Tue, 22 Oct 2019 12:35:24 +1000 Subject: [PATCH 3/4] Reverted <>, added fallback font support and commented it out --- SDL_FontCache.c | 34 +++++++++++++++++++++++++++++++++- SDL_FontCache.h | 8 ++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/SDL_FontCache.c b/SDL_FontCache.c index 13300ba..0d5a4fe 100644 --- a/SDL_FontCache.c +++ b/SDL_FontCache.c @@ -7,6 +7,7 @@ See SDL_FontCache.h for license info. #include "SDL_FontCache.h" + #include #include #include @@ -446,6 +447,8 @@ struct FC_Font FC_Image** glyph_cache; char* loading_string; + // Fallback font - if a codepoint is not found in this font, then search this font + FC_Font * fallback; }; @@ -897,6 +900,8 @@ static void FC_Init(FC_Font* font) if(fc_buffer == NULL) fc_buffer = (char*)malloc(fc_buffer_size); + + font->fallback = NULL; } static Uint8 FC_GrowGlyphCache(FC_Font* font) @@ -1607,7 +1612,23 @@ Uint8 FC_GetGlyphData(FC_Font* font, FC_GlyphData* result, Uint32 codepoint) SDL_QueryTexture(cache_image, NULL, NULL, &w, &h); #endif - surf = TTF_RenderUTF8_Blended(font->ttf_source, buff, white); + // Find the font in the linked list that has this character + FC_Font * working = font; +// std::cout << "FC_Font: " << font << "glyphProvided " << TTF_GlyphIsProvided(working->ttf_source, codepoint) << ", glypth: [" << buff << "]" << ", codepoint: [" << (codepoint) << "]"<< std::endl; +// while (working) +// { +// //if (!FT_Get_Char_Index((FT_Face) working->ttf_source, codepoint)) +// if (!TTF_GlyphIsProvided(working->ttf_source, codepoint)) +// { +// working = working->fallback; +// } +// else +// break; +// } +// if (!working) +// working = font; + + surf = TTF_RenderUTF8_Blended(working->ttf_source, buff, white); if(surf == NULL) { return 0; @@ -2898,3 +2919,14 @@ void FC_SetDefaultColor(FC_Font* font, SDL_Color color) font->default_color = color; } + + +void FC_AddFallbackFont(FC_Font* base, FC_Font* fallback) +{ + FC_Font * working = base; + if (!working) return; + while (working->fallback) + working = working->fallback; + working->fallback = fallback; +} + diff --git a/SDL_FontCache.h b/SDL_FontCache.h index df94f08..435b522 100644 --- a/SDL_FontCache.h +++ b/SDL_FontCache.h @@ -33,11 +33,11 @@ THE SOFTWARE. #ifndef _SDL_FONTCACHE_H__ #define _SDL_FONTCACHE_H__ -#include -#include +#include "SDL.h" +#include "SDL_ttf.h" #ifdef FC_USE_SDL_GPU - #include + #include "SDL_gpu.h" #define FC_COORD float #else #define FC_COORD int @@ -319,7 +319,7 @@ void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter); void FC_SetSpacing(FC_Font* font, int LetterSpacing); void FC_SetLineSpacing(FC_Font* font, int LineSpacing); void FC_SetDefaultColor(FC_Font* font, SDL_Color color); - +void FC_AddFallbackFont(FC_Font* base, FC_Font* fallback); #ifdef __cplusplus } From cb3ed4180747f159d317d5af9de1d3749727e059 Mon Sep 17 00:00:00 2001 From: Liam Twigger <4939563+SnapperTT@users.noreply.github.com> Date: Tue, 22 Oct 2019 16:34:58 +1000 Subject: [PATCH 4/4] cleanup of new stuff, replaced TTF_GlyphIsProvided with TTF_GlyphIsProvided32 --- SDL_FontCache.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/SDL_FontCache.c b/SDL_FontCache.c index 0d5a4fe..15e0aaf 100644 --- a/SDL_FontCache.c +++ b/SDL_FontCache.c @@ -1614,14 +1614,11 @@ Uint8 FC_GetGlyphData(FC_Font* font, FC_GlyphData* result, Uint32 codepoint) // Find the font in the linked list that has this character FC_Font * working = font; -// std::cout << "FC_Font: " << font << "glyphProvided " << TTF_GlyphIsProvided(working->ttf_source, codepoint) << ", glypth: [" << buff << "]" << ", codepoint: [" << (codepoint) << "]"<< std::endl; +// std::cout << "FC_Font: " << font << "glyphProvided " << TTF_GlyphIsProvided32(working->ttf_source, codepoint) << ", glypth: [" << buff << "]" << ", codepoint: [" << (codepoint) << "]"<< std::endl; // while (working) // { -// //if (!FT_Get_Char_Index((FT_Face) working->ttf_source, codepoint)) -// if (!TTF_GlyphIsProvided(working->ttf_source, codepoint)) -// { +// if (!TTF_GlyphIsProvided32(working->ttf_source, codepoint)) // Requires SDL_TTF 2.0.16 // working = working->fallback; -// } // else // break; // }