Line data Source code
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 : #include "precompiled.h"
19 : #include "Font.h"
20 :
21 : #include "graphics/FontManager.h"
22 : #include "ps/Filesystem.h"
23 : #include "ps/CLogger.h"
24 : #include "renderer/Renderer.h"
25 :
26 : #include <map>
27 : #include <string>
28 :
29 5 : CFont::GlyphMap::GlyphMap()
30 : {
31 5 : memset(m_Data, 0, sizeof(m_Data));
32 5 : }
33 :
34 10 : CFont::GlyphMap::~GlyphMap()
35 : {
36 1285 : for (size_t i = 0; i < ARRAY_SIZE(m_Data); i++)
37 1280 : delete[] m_Data[i];
38 5 : }
39 :
40 5905 : void CFont::GlyphMap::set(u16 i, const GlyphData& val)
41 : {
42 5905 : if (!m_Data[i >> 8])
43 57 : m_Data[i >> 8] = new GlyphData[256]();
44 5905 : m_Data[i >> 8][i & 0xff] = val;
45 5905 : m_Data[i >> 8][i & 0xff].defined = 1;
46 5905 : }
47 :
48 1148 : int CFont::GetCharacterWidth(wchar_t c) const
49 : {
50 1148 : const GlyphData* g = m_Glyphs.get(c);
51 :
52 1148 : if (!g)
53 0 : g = m_Glyphs.get(0xFFFD); // Use the missing glyph symbol
54 :
55 1148 : if (!g)
56 0 : return 0;
57 :
58 1148 : return g->xadvance;
59 : }
60 :
61 1937 : void CFont::CalculateStringSize(const wchar_t* string, int& width, int& height) const
62 : {
63 1937 : width = 0;
64 1937 : height = m_Height;
65 :
66 : // Compute the width as the width of the longest line.
67 1937 : int lineWidth = 0;
68 26091 : for (const wchar_t* c = string; *c != '\0'; c++)
69 : {
70 24154 : const GlyphData* g = m_Glyphs.get(*c);
71 :
72 24154 : if (!g)
73 45 : g = m_Glyphs.get(0xFFFD); // Use the missing glyph symbol
74 :
75 24154 : if (g)
76 24154 : lineWidth += g->xadvance; // Add the character's advance distance
77 :
78 24154 : if (*c == L'\n')
79 : {
80 45 : height += m_LineSpacing;
81 45 : width = std::max(width, lineWidth);
82 45 : lineWidth = 0;
83 : }
84 : }
85 1937 : width = std::max(width, lineWidth);
86 1940 : }
|