diff --git a/src/api/renderer.c b/src/api/renderer.c index fdd6ed72..d02229ad 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -3,6 +3,9 @@ #include "api.h" #include "../renderer.h" #include "../rencache.h" +#ifdef LITE_USE_SDL_RENDERER +#include "../renwindow.h" +#endif #include "lua.h" RenWindow *active_window_renderer = NULL; @@ -116,7 +119,7 @@ static bool font_retrieve(lua_State* L, RenFont** fonts, int idx) { } } #ifdef LITE_USE_SDL_RENDERER - update_font_scale(window_renderer, fonts); + update_font_scale(active_window_renderer, fonts); #endif return is_table; } @@ -226,7 +229,13 @@ static int f_font_get_size(lua_State *L) { static int f_font_set_size(lua_State *L) { RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); float size = luaL_checknumber(L, 2); - ren_font_group_set_size(fonts, size); + int scale = 1; +#ifdef LITE_USE_SDL_RENDERER + if (active_window_renderer != NULL) { + scale = renwin_get_surface(active_window_renderer).scale; + } +#endif + ren_font_group_set_size(fonts, size, scale); return 0; } diff --git a/src/renderer.c b/src/renderer.c index c36f7d02..4c823348 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -68,10 +68,11 @@ typedef struct RenFont { #ifdef LITE_USE_SDL_RENDERER void update_font_scale(RenWindow *window_renderer, RenFont **fonts) { + if (window_renderer == NULL) return; const int surface_scale = renwin_get_surface(window_renderer).scale; for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { if (fonts[i]->scale != surface_scale) { - ren_font_group_set_size(window_renderer, fonts, fonts[0]->size); + ren_font_group_set_size(fonts, fonts[0]->size, surface_scale); return; } } @@ -274,6 +275,9 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial strcpy(font->path, path); font->face = face; font->size = size; +#ifdef LITE_USE_SDL_RENDERER + font->scale = 1; +#endif font->height = (short)((face->height / (float)face->units_per_EM) * font->size); font->baseline = (short)((face->ascender / (float)face->units_per_EM) * font->size); font->antialiasing = antialiasing; @@ -344,11 +348,11 @@ float ren_font_group_get_size(RenFont **fonts) { return fonts[0]->size; } -void ren_font_group_set_size(RenFont **fonts, float size) { +void ren_font_group_set_size(RenFont **fonts, float size, int surface_scale) { for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { font_clear_glyph_cache(fonts[i]); FT_Face face = fonts[i]->face; - FT_Set_Pixel_Sizes(face, 0, (int)(size)); + FT_Set_Pixel_Sizes(face, 0, (int)(size*surface_scale)); fonts[i]->size = size; #ifdef LITE_USE_SDL_RENDERER fonts[i]->scale = surface_scale; @@ -385,7 +389,11 @@ double ren_font_group_get_width(RenFont **fonts, const char *text, size_t len, i if (!set_x_offset) { *x_offset = 0; } +#ifdef LITE_USE_SDL_RENDERER + return width / fonts[0]->scale; +#else return width; +#endif } double ren_draw_text(RenSurface *rs, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) { @@ -597,6 +605,10 @@ void ren_get_size(RenWindow *window_renderer, int *x, int *y) { RenSurface rs = renwin_get_surface(window_renderer); *x = rs.surface->w; *y = rs.surface->h; +#ifdef LITE_USE_SDL_RENDERER + *x /= rs.scale; + *y /= rs.scale; +#endif } size_t ren_get_window_list(RenWindow ***window_list_dest) { diff --git a/src/renderer.h b/src/renderer.h index 6f4fd521..a94d7024 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -31,7 +31,7 @@ void ren_font_free(RenFont *font); int ren_font_group_get_tab_size(RenFont **font); int ren_font_group_get_height(RenFont **font); float ren_font_group_get_size(RenFont **font); -void ren_font_group_set_size(RenFont **font, float size); +void ren_font_group_set_size(RenFont **font, float size, int surface_scale); #ifdef LITE_USE_SDL_RENDERER void update_font_scale(RenWindow *window_renderer, RenFont **fonts); #endif