Skip to content

Commit

Permalink
Fallback fonts with atlas clearing. (#1790)
Browse files Browse the repository at this point in the history
* Mechanism to define priority fallback fonts through lua AddFallbackFont
and ClearFallbackFonts.
* First search game defined fonts and then system fonts.
  • Loading branch information
saurtron authored Dec 17, 2024
1 parent a019b0a commit 34671c0
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 29 deletions.
2 changes: 2 additions & 0 deletions cont/LuaUI/callins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ CallInsList = {
"DrawScreen",
"DrawInMiniMap",

"FontsChanged",

"SunChanged",

"Explosion",
Expand Down
13 changes: 13 additions & 0 deletions cont/LuaUI/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ local flexCallIns = {
'DrawScreenEffects',
'DrawScreenPost',
'DrawInMiniMap',
'FontsChanged',
'SunChanged',
'RecvSkirmishAIMessage',
}
Expand Down Expand Up @@ -2167,6 +2168,18 @@ function widgetHandler:DownloadProgress(id, downloaded, total)
end
end


--------------------------------------------------------------------------------
--
-- Font call-ins
--

function widgetHandler:FontsChaged()
for _,w in ripairs(self.FontsChangedList) do
w:FontsChanged()
end
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions cont/base/springcontent/LuaGadgets/callins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ CALLIN_LIST = {
"DrawProjectile",
"DrawMaterial",

"FontsChanged",

"SunChanged",

-- unsynced message callins
Expand Down
9 changes: 9 additions & 0 deletions cont/base/springcontent/LuaGadgets/gadgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,15 @@ end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function gadgetHandler:FontsChanged()
for _,g in r_ipairs(self.FontsChangedList) do
g:FontsChanged()
end
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

gadgetHandler:Initialize()

--------------------------------------------------------------------------------
Expand Down
45 changes: 45 additions & 0 deletions rts/Lua/LuaFonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ bool LuaFonts::PushEntries(lua_State* L)

REGISTER_LUA_CFUNC(LoadFont);
REGISTER_LUA_CFUNC(DeleteFont);
REGISTER_LUA_CFUNC(AddFallbackFont);
REGISTER_LUA_CFUNC(ClearFallbackFonts);

return true;
}
Expand Down Expand Up @@ -205,6 +207,49 @@ int LuaFonts::DeleteFont(lua_State* L)
return meta_gc(L);
}

/*** Adds a fallback font for the font rendering engine.
*
* Fonts added first will have higher priority.
* When a glyph isn't found when rendering a font, the fallback fonts
* will be searched first, otherwise os fonts will be used.
*
* The application should listen for the unsynced 'FontsChanged' callin so
* modules can clear any already reserved display lists or other relevant
* caches.
*
* Note the callin won't be executed at the time of calling this method,
* but later, on the Update cycle (before other Update and Draw callins).
*
* @function gl.AddFallbackFont
* @string filePath VFS path to the file, for example "fonts/myfont.ttf". Uses VFS.RAW_FIRST access mode.
* @treturn bool success
*/
int LuaFonts::AddFallbackFont(lua_State* L)
{
RECOIL_DETAILED_TRACY_ZONE;

const auto font = luaL_checkstring(L, 1);

const bool res = CFontTexture::AddFallbackFont(font);
lua_pushboolean(L, res);
return 1;
}

/*** Clears all fallback fonts.
*
* See the note at 'AddFallbackFont' about the 'FontsChanged' callin,
* it also applies when calling this method.
*
* @function gl.ClearFallbackFonts
* @treturn nil
*/
int LuaFonts::ClearFallbackFonts(lua_State* L)
{
RECOIL_DETAILED_TRACY_ZONE;

CFontTexture::ClearFallbackFonts();
return 0;
}

/******************************************************************************/
/******************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions rts/Lua/LuaFonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class LuaFonts {
private: // call-outs
static int LoadFont(lua_State* L);
static int DeleteFont(lua_State* L);
static int AddFallbackFont(lua_State* L);
static int ClearFallbackFonts(lua_State* L);

private: // userdata call-outs
static int Print(lua_State* L);
Expand Down
21 changes: 21 additions & 0 deletions rts/Lua/LuaHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,27 @@ void CLuaHandle::ViewResize()
RunCallIn(L, cmdStr, 1, 0);
}

/*** Called whenever fonts are updated. Signals the game display lists
* and other caches should be discarded.
*
* Gets called before other Update and Draw callins.
*
* @function FontsChanged
*/
void CLuaHandle::FontsChanged()
{
RECOIL_DETAILED_TRACY_ZONE;
LUA_CALL_IN_CHECK(L);
luaL_checkstack(L, 2, __func__);

static const LuaHashString cmdStr(__func__);

if (!cmdStr.GetGlobalFunc(L))
return;

RunCallIn(L, cmdStr, 0, 0);
}

/***
* @function SunChanged
*/
Expand Down
2 changes: 2 additions & 0 deletions rts/Lua/LuaHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ class CLuaHandle : public CEventClient

void ViewResize() override;

void FontsChanged() override;

void SunChanged() override;

void DrawGenesis() override;
Expand Down
Loading

0 comments on commit 34671c0

Please sign in to comment.