From 46f63074dad2f8462909f53b2de8cca6b018ba24 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 2 Nov 2023 11:15:41 +0000 Subject: [PATCH] Add: MockFontCache for testing GUI code that only needs to know font sizes. --- src/fontcache.h | 3 +-- src/tests/CMakeLists.txt | 1 + src/tests/mock_fontcache.h | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/tests/mock_fontcache.h diff --git a/src/fontcache.h b/src/fontcache.h index 79af2e2992..f52df8d8b1 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -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. diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 9a01d4e8fe..ad7701b26b 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -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 diff --git a/src/tests/mock_fontcache.h b/src/tests/mock_fontcache.h new file mode 100644 index 0000000000..c9eb326029 --- /dev/null +++ b/src/tests/mock_fontcache.h @@ -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 . + */ + +/** @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 */