From bf8f24f9a83fb5f05092e0ee2ba6c72e3cf3e156 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 18 May 2023 08:36:54 +0100 Subject: [PATCH] Codechange: Use unique_ptr for text layout font mapping. This must stay a pointer as the value passed to other structures. --- src/gfx_layout.cpp | 9 +++------ src/gfx_layout.h | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 3755851cdb..73420b0add 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -297,10 +297,10 @@ ptrdiff_t Layouter::GetCharAtPosition(int x) const Font *Layouter::GetFont(FontSize size, TextColour colour) { FontColourMap::iterator it = fonts[size].find(colour); - if (it != fonts[size].end()) return it->second; + if (it != fonts[size].end()) return it->second.get(); - fonts[size][colour] = new Font(size, colour); - return fonts[size][colour]; + fonts[size][colour] = std::make_unique(size, colour); + return fonts[size][colour].get(); } /** @@ -309,9 +309,6 @@ Font *Layouter::GetFont(FontSize size, TextColour colour) */ void Layouter::ResetFontCache(FontSize size) { - for (auto &pair : fonts[size]) { - delete pair.second; - } fonts[size].clear(); /* We must reset the linecache since it references the just freed fonts */ diff --git a/src/gfx_layout.h b/src/gfx_layout.h index d2fd987af2..181bef5561 100644 --- a/src/gfx_layout.h +++ b/src/gfx_layout.h @@ -80,7 +80,7 @@ public: Font(FontSize size, TextColour colour); }; -/** Mapping from index to font. */ +/** Mapping from index to font. The pointer is owned by FontColourMap. */ using FontMap = std::map; /** @@ -169,7 +169,7 @@ private: static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state); - using FontColourMap = std::map; + using FontColourMap = std::map>; static FontColourMap fonts[FS_END]; public: static Font *GetFont(FontSize size, TextColour colour);