diff --git a/SDL_FontCache.c b/SDL_FontCache.c index 7d3c508..15e0aaf 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 @@ -231,7 +232,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)w, (FC_COORD)h}; return r; } @@ -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; }; @@ -617,7 +620,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 = {(FC_COORD)x, (FC_COORD)y, FC_MAX(0, (FC_COORD)(x2 - x)), FC_MAX(0, (FC_COORD)(y2 - y))}; return result; } } @@ -686,16 +689,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 @@ -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,20 @@ 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_GlyphIsProvided32(working->ttf_source, codepoint) << ", glypth: [" << buff << "]" << ", codepoint: [" << (codepoint) << "]"<< std::endl; +// while (working) +// { +// if (!TTF_GlyphIsProvided32(working->ttf_source, codepoint)) // Requires SDL_TTF 2.0.16 +// working = working->fallback; +// else +// break; +// } +// if (!working) +// working = font; + + surf = TTF_RenderUTF8_Blended(working->ttf_source, buff, white); if(surf == NULL) { return 0; @@ -2184,7 +2202,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2201,7 +2219,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2230,7 +2248,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2247,7 +2265,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2264,7 +2282,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 = {(FC_COORD)x, (FC_COORD)y, (FC_COORD)width, 0}; int total_height; if(formatted_text == NULL || font == NULL) @@ -2293,7 +2311,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 = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2326,7 +2344,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 = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(text == NULL || font == NULL) return result; @@ -2721,7 +2739,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 = {(FC_COORD)x, (FC_COORD)y, 0, 0}; if(formatted_text == NULL) return result; @@ -2898,3 +2916,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 77f5fb4..435b522 100644 --- a/SDL_FontCache.h +++ b/SDL_FontCache.h @@ -38,6 +38,9 @@ THE SOFTWARE. #ifdef FC_USE_SDL_GPU #include "SDL_gpu.h" + #define FC_COORD float +#else + #define FC_COORD int #endif @@ -316,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 }