srctree

Robin Linden parent a447a4f9 c653661d
type/sfml: Cache font lookup failures as well

We were only caching successful font lookups, so if a lookup failed,we'd still look at all the fonts on disk the next time the non-existentfont was requested.

inlinesplit
type/sfml.cpp added: 8, removed: 7, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -62,12 +62,13 @@ Size SfmlFont::measure(std::string_view text, Px font_size) const {
}
 
std::optional<std::shared_ptr<IFont const>> SfmlType::font(std::string_view name) const {
if (auto font = font_cache_.find(name); font != font_cache_.end()) {
if (auto font = font_cache_.find(name); font != font_cache_.end() && font->second.has_value()) {
return font->second;
}
 
sf::Font font;
if (auto path = find_path_to_font(name); !path || !font.loadFromFile(*path)) {
font_cache_.insert(std::pair{std::string{name}, std::nullopt});
return std::nullopt;
}
 
 
type/sfml.h added: 8, removed: 7, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2023-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -35,12 +35,12 @@ class SfmlType : public IType {
public:
std::optional<std::shared_ptr<IFont const>> font(std::string_view name) const override;
 
void set_font(std::string name, std::shared_ptr<SfmlFont const> font) {
void set_font(std::string name, std::optional<std::shared_ptr<SfmlFont const>> font) {
font_cache_.insert_or_assign(std::move(name), std::move(font));
}
 
private:
mutable std::map<std::string, std::shared_ptr<SfmlFont const>, std::less<>> font_cache_;
mutable std::map<std::string, std::optional<std::shared_ptr<SfmlFont const>>, std::less<>> font_cache_;
};
 
} // namespace type