[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / Font.h
blobfa56083e5c0c2858c96ada16047369cb581dd548
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_FONT
19 #define INCLUDED_FONT
21 #include "graphics/Texture.h"
23 /**
24 * Storage for a bitmap font. Loaded by CFontManager.
26 class CFont
28 public:
29 struct GlyphData
31 float u0, v0, u1, v1;
32 i16 x0, y0, x1, y1;
33 i16 xadvance;
34 u8 defined;
37 /**
38 * Relatively efficient lookup of GlyphData from 16-bit Unicode codepoint.
39 * This is stored as a sparse 2D array, exploiting the knowledge that a font
40 * typically only supports a small number of 256-codepoint blocks, so most
41 * elements of m_Data will be NULL.
43 class GlyphMap
45 NONCOPYABLE(GlyphMap);
46 public:
47 GlyphMap();
48 ~GlyphMap();
49 void set(u16 i, const GlyphData& val);
51 const GlyphData* get(u16 i) const
53 if (!m_Data[i >> 8])
54 return NULL;
55 if (!m_Data[i >> 8][i & 0xff].defined)
56 return NULL;
57 return &m_Data[i >> 8][i & 0xff];
60 private:
61 GlyphData* m_Data[256];
64 bool HasRGB() const { return m_HasRGB; }
65 int GetLineSpacing() const { return m_LineSpacing; }
66 int GetHeight() const { return m_Height; }
67 int GetCharacterWidth(wchar_t c) const;
68 void CalculateStringSize(const wchar_t* string, int& w, int& h) const;
69 void GetGlyphBounds(float& x0, float& y0, float& x1, float& y1) const
71 x0 = m_BoundsX0;
72 y0 = m_BoundsY0;
73 x1 = m_BoundsX1;
74 y1 = m_BoundsY1;
76 const GlyphMap& GetGlyphs() const { return m_Glyphs; }
77 CTexturePtr GetTexture() const { return m_Texture; }
79 private:
80 friend class CFontManager;
82 CFont() = default;
84 CTexturePtr m_Texture;
86 bool m_HasRGB; // true if RGBA, false if ALPHA
88 GlyphMap m_Glyphs;
90 int m_LineSpacing;
91 int m_Height; // height of a capital letter, roughly
93 // Bounding box of all glyphs
94 float m_BoundsX0;
95 float m_BoundsY0;
96 float m_BoundsX1;
97 float m_BoundsY1;
100 #endif // INCLUDED_FONT