[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / Font.cpp
blob5f7894b2b6232e6c01c02791a6e543ffb3b54f09
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 #include "precompiled.h"
19 #include "Font.h"
21 #include "graphics/FontManager.h"
22 #include "ps/Filesystem.h"
23 #include "ps/CLogger.h"
24 #include "renderer/Renderer.h"
26 #include <map>
27 #include <string>
29 CFont::GlyphMap::GlyphMap()
31 memset(m_Data, 0, sizeof(m_Data));
34 CFont::GlyphMap::~GlyphMap()
36 for (size_t i = 0; i < ARRAY_SIZE(m_Data); i++)
37 delete[] m_Data[i];
40 void CFont::GlyphMap::set(u16 i, const GlyphData& val)
42 if (!m_Data[i >> 8])
43 m_Data[i >> 8] = new GlyphData[256]();
44 m_Data[i >> 8][i & 0xff] = val;
45 m_Data[i >> 8][i & 0xff].defined = 1;
48 int CFont::GetCharacterWidth(wchar_t c) const
50 const GlyphData* g = m_Glyphs.get(c);
52 if (!g)
53 g = m_Glyphs.get(0xFFFD); // Use the missing glyph symbol
55 if (!g)
56 return 0;
58 return g->xadvance;
61 void CFont::CalculateStringSize(const wchar_t* string, int& width, int& height) const
63 width = 0;
64 height = m_Height;
66 // Compute the width as the width of the longest line.
67 int lineWidth = 0;
68 for (const wchar_t* c = string; *c != '\0'; c++)
70 const GlyphData* g = m_Glyphs.get(*c);
72 if (!g)
73 g = m_Glyphs.get(0xFFFD); // Use the missing glyph symbol
75 if (g)
76 lineWidth += g->xadvance; // Add the character's advance distance
78 if (*c == L'\n')
80 height += m_LineSpacing;
81 width = std::max(width, lineWidth);
82 lineWidth = 0;
85 width = std::max(width, lineWidth);