FontCache: Make filters non-allocating

Avoids the creation of three allocating static constructors that run
during program start.
This commit is contained in:
Lioncash 2019-09-05 19:19:06 -04:00
parent b25cfdd305
commit 5c54fd73a1
2 changed files with 13 additions and 12 deletions

View File

@ -5,7 +5,6 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -66,7 +65,7 @@ public:
} }
}; };
using FCharFilter = std::pair<std::string, std::function<bool(uint32_t)>>; using FCharFilter = std::pair<std::string_view, bool (*)(uint32_t)>;
class FontAtlas { class FontAtlas {
FT_Face m_face; FT_Face m_face;

View File

@ -43,15 +43,16 @@ extern "C" const FT_Driver_ClassRec tt_driver_class;
namespace specter { namespace specter {
static logvisor::Module Log("specter::FontCache"); static logvisor::Module Log("specter::FontCache");
const FCharFilter AllCharFilter = std::make_pair("all-glyphs", [](uint32_t) -> bool { return true; }); const FCharFilter AllCharFilter{"all-glyphs", [](uint32_t) { return true; }};
const FCharFilter LatinCharFilter = const FCharFilter LatinCharFilter{"latin-glyphs",
std::make_pair("latin-glyphs", [](uint32_t c) -> bool { return c <= 0xff || ((c - 0x2200) <= (0x23FF - 0x2200)); }); [](uint32_t c) { return c <= 0xff || (c - 0x2200) <= (0x23FF - 0x2200); }};
const FCharFilter LatinAndJapaneseCharFilter = std::make_pair("latin-and-jp-glyphs", [](uint32_t c) -> bool { const FCharFilter LatinAndJapaneseCharFilter{"latin-and-jp-glyphs", [](uint32_t c) {
return LatinCharFilter.second(c) || ((c - 0x2E00) <= (0x30FF - 0x2E00)) || ((c - 0x4E00) <= (0x9FFF - 0x4E00)) || return LatinCharFilter.second(c) || (c - 0x2E00) <= (0x30FF - 0x2E00) ||
((c - 0xFF00) <= (0xFFEF - 0xFF00)); (c - 0x4E00) <= (0x9FFF - 0x4E00) ||
}); (c - 0xFF00) <= (0xFFEF - 0xFF00);
}};
FontTag::FontTag(std::string_view name, bool subpixel, float points, uint32_t dpi) { FontTag::FontTag(std::string_view name, bool subpixel, float points, uint32_t dpi) {
XXH64_state_t st; XXH64_state_t st;
@ -628,10 +629,11 @@ FontTag FontCache::prepCustomFont(std::string_view name, FT_Face face, FCharFilt
FT_Set_Char_Size(face, 0, points * 64.0, 0, dpi); FT_Set_Char_Size(face, 0, points * 64.0, 0, dpi);
/* Make tag and search for cached version */ /* Make tag and search for cached version */
FontTag tag(std::string(name) + '_' + filter.first, subpixel, points, dpi); const FontTag tag(std::string(name).append(1, '_').append(filter.first), subpixel, points, dpi);
auto search = m_cachedAtlases.find(tag); const auto search = m_cachedAtlases.find(tag);
if (search != m_cachedAtlases.end()) if (search != m_cachedAtlases.end()) {
return tag; return tag;
}
/* Now check filesystem cache */ /* Now check filesystem cache */
hecl::SystemString cachePath = m_cacheRoot + _SYS_STR('/') + fmt::format(fmt(_SYS_STR("{}")), tag.hash()); hecl::SystemString cachePath = m_cacheRoot + _SYS_STR('/') + fmt::format(fmt(_SYS_STR("{}")), tag.hash());