Pyrogenesis trunk
Font.h
Go to the documentation of this file.
1/* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#ifndef INCLUDED_FONT
19#define INCLUDED_FONT
20
21#include "graphics/Texture.h"
22
23/**
24 * Storage for a bitmap font. Loaded by CFontManager.
25 */
26class CFont
27{
28public:
29 struct GlyphData
30 {
31 float u0, v0, u1, v1;
35 };
36
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.
42 */
44 {
46 public:
47 GlyphMap();
48 ~GlyphMap();
49 void set(u16 i, const GlyphData& val);
50
51 const GlyphData* get(u16 i) const
52 {
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];
58 }
59
60 private:
62 };
63
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
70 {
71 x0 = m_BoundsX0;
72 y0 = m_BoundsY0;
73 x1 = m_BoundsX1;
74 y1 = m_BoundsY1;
75 }
76 const GlyphMap& GetGlyphs() const { return m_Glyphs; }
77 CTexturePtr GetTexture() const { return m_Texture; }
78
79private:
80 friend class CFontManager;
81
82 CFont() = default;
83
85
86 bool m_HasRGB; // true if RGBA, false if ALPHA
87
89
91 int m_Height; // height of a capital letter, roughly
92
93 // Bounding box of all glyphs
98};
99
100#endif // INCLUDED_FONT
Font manager: loads and caches bitmap fonts.
Definition: FontManager.h:31
Relatively efficient lookup of GlyphData from 16-bit Unicode codepoint.
Definition: Font.h:44
GlyphData * m_Data[256]
Definition: Font.h:61
GlyphMap()
Definition: Font.cpp:29
~GlyphMap()
Definition: Font.cpp:34
NONCOPYABLE(GlyphMap)
const GlyphData * get(u16 i) const
Definition: Font.h:51
void set(u16 i, const GlyphData &val)
Definition: Font.cpp:40
Storage for a bitmap font.
Definition: Font.h:27
int m_LineSpacing
Definition: Font.h:90
int GetLineSpacing() const
Definition: Font.h:65
int GetCharacterWidth(wchar_t c) const
Definition: Font.cpp:48
bool HasRGB() const
Definition: Font.h:64
CFont()=default
int m_Height
Definition: Font.h:91
CTexturePtr m_Texture
Definition: Font.h:84
void CalculateStringSize(const wchar_t *string, int &w, int &h) const
Definition: Font.cpp:61
GlyphMap m_Glyphs
Definition: Font.h:88
int GetHeight() const
Definition: Font.h:66
float m_BoundsY0
Definition: Font.h:95
const GlyphMap & GetGlyphs() const
Definition: Font.h:76
float m_BoundsX1
Definition: Font.h:96
float m_BoundsY1
Definition: Font.h:97
CTexturePtr GetTexture() const
Definition: Font.h:77
bool m_HasRGB
Definition: Font.h:86
void GetGlyphBounds(float &x0, float &y0, float &x1, float &y1) const
Definition: Font.h:69
float m_BoundsX0
Definition: Font.h:94
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:23
Definition: Font.h:30
float v1
Definition: Font.h:31
i16 x1
Definition: Font.h:32
i16 xadvance
Definition: Font.h:33
i16 y1
Definition: Font.h:32
float v0
Definition: Font.h:31
u8 defined
Definition: Font.h:34
float u0
Definition: Font.h:31
i16 y0
Definition: Font.h:32
float u1
Definition: Font.h:31
i16 x0
Definition: Font.h:32
uint8_t u8
Definition: types.h:37
uint16_t u16
Definition: types.h:38
int16_t i16
Definition: types.h:33