Fix infinite loop detection when placing players randomly on the newer random map...
[0ad.git] / source / graphics / Font.cpp
blob0882d426998e18e446615dd42763e7002d4bba4b
1 /* Copyright (C) 2013 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 for (const wchar_t* c = string; *c != '\0'; c++)
68 const GlyphData* g = m_Glyphs.get(*c);
70 if (!g)
71 g = m_Glyphs.get(0xFFFD); // Use the missing glyph symbol
73 if (g)
74 width += g->xadvance; // Add the character's advance distance