Add: MockFontCache for testing GUI code that only needs to know font sizes.

This commit is contained in:
Peter Nelson 2023-11-02 11:15:41 +00:00 committed by Peter Nelson
parent af4c7799fe
commit 46f63074da
3 changed files with 47 additions and 2 deletions

View File

@ -19,9 +19,8 @@ static const GlyphID SPRITE_GLYPH = 1U << 30;
/** Font cache for basic fonts. */
class FontCache {
private:
static FontCache *caches[FS_END]; ///< All the font caches.
protected:
static FontCache *caches[FS_END]; ///< All the font caches.
FontCache *parent; ///< The parent of this font cache.
const FontSize fs; ///< The size of the font.
int height; ///< The height of the font.

View File

@ -1,6 +1,7 @@
add_test_files(
landscape_partial_pixel_z.cpp
math_func.cpp
mock_fontcache.h
string_func.cpp
strings_func.cpp
test_main.cpp

View File

@ -0,0 +1,45 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file mock_fontcache.h Mock font cache implementation definition. */
#ifndef MOCK_FONTCACHE_H
#define MOCK_FONTCACHE_H
#include "../stdafx.h"
#include "../fontcache.h"
#include "../string_func.h"
/** Font cache for mocking basic use of fonts. */
class MockFontCache : public FontCache {
public:
MockFontCache(FontSize fs) : FontCache(fs)
{
this->height = FontCache::GetDefaultFontHeight(this->fs);
}
void SetUnicodeGlyph(char32_t, SpriteID) override {}
void InitializeUnicodeGlyphMap() override {}
void ClearFontCache() override {}
const Sprite *GetGlyph(GlyphID) override { return nullptr; }
uint GetGlyphWidth(GlyphID) override { return this->height / 2; }
bool GetDrawGlyphShadow() override { return false; }
GlyphID MapCharToGlyph(char32_t key) override { return key; }
const void *GetFontTable(uint32_t, size_t &length) override { length = 0; return nullptr; }
std::string GetFontName() override { return "mock"; }
bool IsBuiltInFont() override { return true; }
static void InitializeFontCaches()
{
for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
if (FontCache::caches[fs] == nullptr) new MockFontCache(fs); /* FontCache inserts itself into to the cache. */
}
}
};
#endif /* MOCK_FONTCACHE_H */