|
1 | 1 | #include "FontRegistry.h" |
2 | 2 |
|
| 3 | +#include <fmt/format.h> |
| 4 | + |
3 | 5 | #include "engine/UpdateContext.h" |
4 | 6 |
|
| 7 | +#include "ki/sid.h" |
| 8 | + |
5 | 9 | #include "kigl/GLState.h" |
6 | 10 |
|
7 | 11 | namespace |
@@ -60,46 +64,35 @@ namespace text { |
60 | 64 | { |
61 | 65 | std::shared_lock lock(m_lock); |
62 | 66 |
|
63 | | - for (auto& font : m_fonts) { |
| 67 | + for (auto& [fontId, font] : m_fonts) { |
64 | 68 | font.prepare(); |
65 | 69 | font.update(); |
66 | 70 | } |
67 | 71 | } |
68 | 72 |
|
69 | | - //text::FontAtlas* FontRegistry::modifyFont(text::font_id id) |
70 | | - //{ |
71 | | - // if (id < 1) return nullptr; |
72 | | - // |
73 | | - // std::shared_lock lock(m_lock); |
74 | | - // assert(id > 0 && id <= m_fonts.size()); |
75 | | - // |
76 | | - // return &m_fonts[id - 1]; |
77 | | - //} |
78 | | - |
79 | 73 | text::font_id FontRegistry::registerFont( |
80 | 74 | text::FontAtlas&& src) |
81 | 75 | { |
82 | 76 | std::unique_lock lock(m_lock); |
83 | 77 |
|
84 | | - text::font_id fontId = findFont(src); |
85 | | - if (!fontId) { |
86 | | - auto& font = m_fonts.emplace_back(std::move(src)); |
87 | | - font.m_id = static_cast<text::font_id>(m_fonts.size()); |
88 | | - fontId = font.m_id; |
89 | | - } |
| 78 | + text::font_id fontId; |
| 79 | + { |
| 80 | + std::string key = fmt::format( |
| 81 | + "{}_{}_{}", |
| 82 | + src.m_name, |
| 83 | + src.m_fontPath, |
| 84 | + src.m_fontSize); |
90 | 85 |
|
91 | | - return fontId; |
92 | | - } |
| 86 | + fontId = SID(key); |
| 87 | + } |
93 | 88 |
|
94 | | - text::font_id FontRegistry::findFont( |
95 | | - const text::FontAtlas& src) const noexcept |
96 | | - { |
97 | | - const auto& it = std::find_if( |
98 | | - m_fonts.begin(), m_fonts.end(), |
99 | | - [&src](const auto& font) { |
100 | | - return font == src; |
101 | | - }); |
| 89 | + const auto& it = m_fonts.find(fontId); |
| 90 | + if (it == m_fonts.end()) |
| 91 | + { |
| 92 | + src.m_id = fontId; |
| 93 | + m_fonts.insert({ fontId, std::move(src) }); |
| 94 | + } |
102 | 95 |
|
103 | | - return it != m_fonts.end() ? it->m_id : 0; |
| 96 | + return fontId; |
104 | 97 | } |
105 | 98 | } |
0 commit comments