Changeset - r27385:dea7924e367a
[Not reviewed]
master
0 2 0
Peter Nelson - 17 months ago 2023-05-18 07:36:54
peter1138@openttd.org
Codechange: Use unique_ptr for text layout font mapping.

This must stay a pointer as the value passed to other structures.
2 files changed with 5 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/gfx_layout.cpp
Show inline comments
 
@@ -294,27 +294,24 @@ ptrdiff_t Layouter::GetCharAtPosition(in
 
/**
 
 * Get a static font instance.
 
 */
 
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<Font>(size, colour);
 
	return fonts[size][colour].get();
 
}
 

	
 
/**
 
 * Reset cached font information.
 
 * @param size Font size to reset.
 
 */
 
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 */
 
	ResetLineCache();
 

	
 
#if defined(WITH_UNISCRIBE)
src/gfx_layout.h
Show inline comments
 
@@ -77,13 +77,13 @@ public:
 
	FontCache *fc;     ///< The font we are using.
 
	TextColour colour; ///< The colour this font has to be.
 

	
 
	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<int, Font *>;
 

	
 
/**
 
 * Interface to glue fallback and normal layouter into one.
 
 */
 
class ParagraphLayouter {
 
@@ -166,13 +166,13 @@ public:
 
private:
 
	typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
 
	static LineCache *linecache;
 

	
 
	static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
 

	
 
	using FontColourMap = std::map<TextColour, Font *>;
 
	using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
 
	static FontColourMap fonts[FS_END];
 
public:
 
	static Font *GetFont(FontSize size, TextColour colour);
 

	
 
	Layouter(std::string_view str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
 
	Dimension GetBounds();
0 comments (0 inline, 0 general)