From 03f65e069f3ec5d387fdd8cfe95624b21bc81712 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Wed, 24 Dec 2025 18:17:58 +0100 Subject: [PATCH] Add TextMapSystem WIP --- include/pd/lithium/font.hpp | 13 ++++++++++ pd/lithium/source/drawlist.cpp | 3 +++ pd/lithium/source/font.cpp | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/include/pd/lithium/font.hpp b/include/pd/lithium/font.hpp index 4aed25e..2d1d06c 100755 --- a/include/pd/lithium/font.hpp +++ b/include/pd/lithium/font.hpp @@ -96,6 +96,11 @@ class PD_LITHIUM_API Font { void CmdTextEx(std::vector& cmds, const fvec2& pos, u32 color, float scale, const std::string& text, LiTextFlags flags = 0, const fvec2& box = 0); + + /** + * Garbage collection for TextMapSystem + */ + void CleanupTMS(); /** * Utility function to create a font atlas * During TTF loading (Internal and should not be called) @@ -116,6 +121,14 @@ class PD_LITHIUM_API Font { * **Now using unordered map** */ std::unordered_map CodeMap; + /** TMS */ + struct TMELEM { + PD::u32 ID; + PD::fvec2 Size; + std::string Text; + u64 TimeStamp; + }; + std::unordered_map pTMS; }; } // namespace Li } // namespace PD \ No newline at end of file diff --git a/pd/lithium/source/drawlist.cpp b/pd/lithium/source/drawlist.cpp index 990ef90..25f5da0 100755 --- a/pd/lithium/source/drawlist.cpp +++ b/pd/lithium/source/drawlist.cpp @@ -38,6 +38,9 @@ PD_LITHIUM_API void DrawList::Clear() { pNumVertices = 0; pDrawList.clear(); pPath.clear(); + if (pCurrentFont) { + pCurrentFont->CleanupTMS(); + } while (!pClipRects.empty()) { pClipRects.pop(); } diff --git a/pd/lithium/source/font.cpp b/pd/lithium/source/font.cpp index c602467..e26f747 100755 --- a/pd/lithium/source/font.cpp +++ b/pd/lithium/source/font.cpp @@ -196,6 +196,11 @@ PD_LITHIUM_API Font::Codepoint &Font::GetCodepoint(u32 cp) { } PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) { + u32 id = PD::FNV1A32(text); + if (pTMS.find(id) != pTMS.end()) { + pTMS[id].TimeStamp = PD::OS::GetTime(); + return pTMS[id].Size; + } // Use wstring for exemple for german äöü auto wtext = Strings::MakeWstring(text); // Create a temp position and offset as [0, 0] @@ -237,6 +242,9 @@ PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) { } res.x = std::max(res.x, x); res.y += lh; + pTMS[id].ID = id; + pTMS[id].Size = res; + pTMS[id].TimeStamp = PD::OS::GetTime(); return res; } @@ -331,6 +339,14 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector &cmds, PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale, const PD::fvec2 &max, PD::fvec2 &dim) { + u32 id = PD::FNV1A32(txt); + if (pTMS.find(id) != pTMS.end()) { + if (pTMS[id].Text.size()) { + dim = pTMS[id].Size; + pTMS[id].TimeStamp = PD::OS::GetTime(); + return pTMS[id].Text; + } + } std::string ret; std::string line; int lx = 0; @@ -350,12 +366,24 @@ PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale, } ret += line; dim = GetTextBounds(ret, scale); + pTMS[id].ID = id; + pTMS[id].Size = dim; + pTMS[id].Text = ret; + pTMS[id].TimeStamp = PD::OS::GetTime(); return ret; } PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale, const PD::fvec2 &max, PD::fvec2 &dim) { + u32 id = PD::FNV1A32(txt); + if (pTMS.find(id) != pTMS.end()) { + if (pTMS[id].Text.size()) { + dim = pTMS[id].Size; + pTMS[id].TimeStamp = PD::OS::GetTime(); + return pTMS[id].Text; + } + } auto test = GetTextBounds(txt, scale); if (test.x < max.x) { return txt; @@ -382,8 +410,23 @@ PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale, } ret += it; } + pTMS[id].ID = id; + pTMS[id].Size = dim; + pTMS[id].Text = ret; + pTMS[id].TimeStamp = PD::OS::GetTime(); return ret; } +PD_LITHIUM_API void Font::CleanupTMS() { + u64 t = PD::OS::GetTime(); + for (auto it = pTMS.begin(); it != pTMS.end();) { + if (t - it->second.TimeStamp > 1000) { + it = pTMS.erase(it); + } else { + it++; + } + } +} + } // namespace Li } // namespace PD \ No newline at end of file