2021-02-13 17:34:22 +01:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2022-09-15 20:57:10 +02:00
|
|
|
/** @file truetypefontcache.h Common base definition for font file based font caches. */
|
2021-02-13 17:34:22 +01:00
|
|
|
|
2022-09-15 20:57:10 +02:00
|
|
|
#ifndef TRUETYPEFONTCACHE_H
|
|
|
|
#define TRUETYPEFONTCACHE_H
|
2021-02-13 17:34:22 +01:00
|
|
|
|
2022-09-15 20:57:10 +02:00
|
|
|
#include "../fontcache.h"
|
2021-02-13 17:34:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
static const int MAX_FONT_SIZE = 72; ///< Maximum font size.
|
|
|
|
|
2024-03-16 23:59:32 +01:00
|
|
|
static const uint8_t FACE_COLOUR = 1;
|
|
|
|
static const uint8_t SHADOW_COLOUR = 2;
|
2021-02-13 17:34:22 +01:00
|
|
|
|
|
|
|
/** Font cache for fonts that are based on a TrueType font. */
|
|
|
|
class TrueTypeFontCache : public FontCache {
|
|
|
|
protected:
|
2021-02-13 22:44:45 +01:00
|
|
|
static constexpr int MAX_GLYPH_DIM = 256; ///< Maximum glyph dimensions.
|
|
|
|
static constexpr uint MAX_FONT_MIN_REC_SIZE = 20u; ///< Upper limit for the recommended font size in case a font file contains nonsensical values.
|
|
|
|
|
2021-02-13 17:34:22 +01:00
|
|
|
int req_size; ///< Requested font size.
|
|
|
|
int used_size; ///< Used font size.
|
|
|
|
|
|
|
|
/** Container for information about a glyph. */
|
|
|
|
struct GlyphEntry {
|
2024-05-12 16:03:03 +02:00
|
|
|
std::unique_ptr<uint8_t[]> data; ///< The loaded sprite.
|
|
|
|
uint8_t width = 0; ///< The width of the glyph.
|
|
|
|
|
|
|
|
Sprite *GetSprite() { return reinterpret_cast<Sprite *>(data.get()); }
|
2021-02-13 17:34:22 +01:00
|
|
|
};
|
|
|
|
|
2024-05-12 16:03:03 +02:00
|
|
|
std::unordered_map<GlyphID, GlyphEntry> glyph_to_sprite_map{};
|
2021-02-13 17:34:22 +01:00
|
|
|
|
|
|
|
GlyphEntry *GetGlyphPtr(GlyphID key);
|
2024-05-12 16:03:03 +02:00
|
|
|
GlyphEntry &SetGlyphPtr(GlyphID key, GlyphEntry &&glyph);
|
2021-02-13 17:34:22 +01:00
|
|
|
|
|
|
|
virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TrueTypeFontCache(FontSize fs, int pixels);
|
|
|
|
virtual ~TrueTypeFontCache();
|
|
|
|
int GetFontSize() const override { return this->used_size; }
|
2023-05-08 19:01:06 +02:00
|
|
|
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
|
2021-02-13 17:34:22 +01:00
|
|
|
void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
|
|
|
|
const Sprite *GetGlyph(GlyphID key) override;
|
|
|
|
void ClearFontCache() override;
|
|
|
|
uint GetGlyphWidth(GlyphID key) override;
|
|
|
|
bool GetDrawGlyphShadow() override;
|
|
|
|
bool IsBuiltInFont() override { return false; }
|
|
|
|
};
|
|
|
|
|
2022-09-15 20:57:10 +02:00
|
|
|
#endif /* TRUETYPEFONTCACHE_H */
|