diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 208334b7..06f9cda4 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -1247,8 +1247,7 @@ static int l_lovrGraphicsNewFont(lua_State* L) { padding = luaL_optinteger(L, 2, padding); spread = luaL_optnumber(L, 3, spread); } - TextureFilter defaultFilter = lovrGraphicsGetDefaultFilter(); - Font* font = lovrFontCreate(rasterizer, padding, spread, defaultFilter.mode); + Font* font = lovrFontCreate(rasterizer, padding, spread); luax_pushtype(L, Font, font); lovrRelease(rasterizer, lovrRasterizerDestroy); lovrRelease(font, lovrFontDestroy); diff --git a/src/api/l_graphics_font.c b/src/api/l_graphics_font.c index 515d5c02..968fa2bc 100644 --- a/src/api/l_graphics_font.c +++ b/src/api/l_graphics_font.c @@ -109,6 +109,23 @@ static int l_lovrFontHasGlyphs(lua_State* L) { return 1; } +static int l_lovrFontGetFilter(lua_State* L) { + Font* font = luax_checktype(L, 1, Font); + TextureFilter filter = lovrFontGetFilter(font); + luax_pushenum(L, FilterMode, filter.mode); + lua_pushnumber(L, filter.anisotropy); + return 2; +} + +static int l_lovrFontSetFilter(lua_State* L) { + Font* font = luax_checktype(L, 1, Font); + FilterMode mode = luax_checkenum(L, 2, FilterMode, NULL); + float anisotropy = luax_optfloat(L, 3, 1.f); + TextureFilter filter = { .mode = mode, .anisotropy = anisotropy }; + lovrFontSetFilter(font, filter); + return 0; +} + const luaL_Reg lovrFont[] = { { "getWidth", l_lovrFontGetWidth }, { "getHeight", l_lovrFontGetHeight }, @@ -123,5 +140,7 @@ const luaL_Reg lovrFont[] = { { "setPixelDensity", l_lovrFontSetPixelDensity }, { "getRasterizer", l_lovrFontGetRasterizer}, { "hasGlyphs", l_lovrFontHasGlyphs }, + { "getFilter", l_lovrFontGetFilter }, + { "setFilter", l_lovrFontSetFilter }, { NULL, NULL } }; diff --git a/src/modules/graphics/font.c b/src/modules/graphics/font.c index fdfe787d..7cf7c873 100644 --- a/src/modules/graphics/font.c +++ b/src/modules/graphics/font.c @@ -1,4 +1,5 @@ #include "graphics/font.h" +#include "graphics/graphics.h" #include "graphics/texture.h" #include "data/rasterizer.h" #include "data/image.h" @@ -27,7 +28,7 @@ struct Font { uint32_t padding; float lineHeight; float pixelDensity; - FilterMode filterMode; + TextureFilter filter; bool flip; }; @@ -50,7 +51,7 @@ static void lovrFontAddGlyph(Font* font, Glyph* glyph); static void lovrFontExpandTexture(Font* font); static void lovrFontCreateTexture(Font* font); -Font* lovrFontCreate(Rasterizer* rasterizer, uint32_t padding, double spread, FilterMode filterMode) { +Font* lovrFontCreate(Rasterizer* rasterizer, uint32_t padding, double spread) { Font* font = calloc(1, sizeof(Font)); lovrAssert(font, "Out of memory"); font->ref = 1; @@ -61,7 +62,7 @@ Font* lovrFontCreate(Rasterizer* rasterizer, uint32_t padding, double spread, Fi font->spread = spread; font->lineHeight = 1.f; font->pixelDensity = (float) lovrRasterizerGetHeight(rasterizer); - font->filterMode = filterMode; + font->filter = lovrGraphicsGetDefaultFilter(); map_init(&font->kerning, 0); // Atlas @@ -108,6 +109,17 @@ Texture* lovrFontGetTexture(Font* font) { return font->texture; } +TextureFilter lovrFontGetFilter(Font* font) { + return font->filter; +} + +void lovrFontSetFilter(Font* font, TextureFilter filter) { + if (font->filter.mode != filter.mode || font->filter.anisotropy != filter.anisotropy) { + font->filter = filter; + lovrTextureSetFilter(font->texture, filter); + } +} + void lovrFontRender(Font* font, const char* str, size_t length, float wrap, HorizontalAlign halign, float* vertices, uint16_t* indices, uint16_t baseVertex) { FontAtlas* atlas = &font->atlas; bool flip = font->flip; @@ -357,7 +369,7 @@ static void lovrFontCreateTexture(Font* font) { lovrRelease(font->texture, lovrTextureDestroy); Image* image = lovrImageCreate(font->atlas.width, font->atlas.height, NULL, 0x0, FORMAT_RGBA16F); font->texture = lovrTextureCreate(TEXTURE_2D, &image, 1, false, false, 0); - lovrTextureSetFilter(font->texture, (TextureFilter) { .mode = font->filterMode }); + lovrTextureSetFilter(font->texture, font->filter); lovrTextureSetWrap(font->texture, (TextureWrap) { .s = WRAP_CLAMP, .t = WRAP_CLAMP }); lovrRelease(image, lovrImageDestroy); } diff --git a/src/modules/graphics/font.h b/src/modules/graphics/font.h index 6a562621..74ae9bdb 100644 --- a/src/modules/graphics/font.h +++ b/src/modules/graphics/font.h @@ -21,10 +21,12 @@ typedef enum { } VerticalAlign; typedef struct Font Font; -Font* lovrFontCreate(struct Rasterizer* rasterizer, uint32_t padding, double spread, FilterMode filterMode); +Font* lovrFontCreate(struct Rasterizer* rasterizer, uint32_t padding, double spread); void lovrFontDestroy(void* ref); struct Rasterizer* lovrFontGetRasterizer(Font* font); struct Texture* lovrFontGetTexture(Font* font); +TextureFilter lovrFontGetFilter(Font* font); +void lovrFontSetFilter(Font* font, TextureFilter filter); void lovrFontRender(Font* font, const char* str, size_t length, float wrap, HorizontalAlign halign, float* vertices, uint16_t* indices, uint16_t baseVertex); void lovrFontMeasure(Font* font, const char* string, size_t length, float wrap, float* width, float* lastLineWidth, float* height, uint32_t* lineCount, uint32_t* glyphCount); uint32_t lovrFontGetPadding(Font* font); diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 081b38cc..6a36ebbe 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -483,7 +483,7 @@ Font* lovrGraphicsGetFont() { if (!state.font) { if (!state.defaultFont) { Rasterizer* rasterizer = lovrRasterizerCreate(NULL, 32); - state.defaultFont = lovrFontCreate(rasterizer, 1, 3., state.defaultFilter.mode); + state.defaultFont = lovrFontCreate(rasterizer, 1, 3.); lovrRelease(rasterizer, lovrRasterizerDestroy); }