0ad/source/ps/Font.cpp
Ykkrosh 7dca91f26b # Various changes to the text rendering system.
Rewrite font builder tool to be much simpler and to support more text
effects.
Change GUI to use new set of fonts.
Switch font textures from TGA to PNG so they're easier for the font
builder to create.
Support RGBA font textures (for e.g. stroked text).
Greatly improve text rendering performance by using vertex arrays.
Fix rendering code leaving vertex buffers bound.
Add 'clip' property to GUI text objects, to disable clipping when
rendering.
Delete part of unused console function registration system.

This was SVN commit r7595.
2010-05-30 13:42:56 +00:00

93 lines
2.1 KiB
C++

/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "Font.h"
#include "ps/ConfigDB.h"
#include "lib/res/graphics/unifont.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY L"graphics"
#include <map>
#include <string>
const wchar_t* DefaultFont = L"sans-10";
CFont::CFont(const wchar_t* name)
{
// TODO perhaps: cache the resultant filename (or Handle) for each
// font name; but it's nice to allow run-time alteration of the fonts
std::string fontFilename;
CStrW fontName = L"font."; fontName += name;
// See if the config value can be loaded
CConfigValue* fontFilenameVar = g_ConfigDB.GetValue(CFG_USER, fontName);
if (fontFilenameVar && fontFilenameVar->GetString(fontFilename))
{
h = unifont_load(CStrW(fontFilename));
}
else
{
// Not found in the config file -- try it as a simple filename
h = unifont_load(name);
// Found it
if (h > 0)
return;
// Not found as a font -- give up and use the default.
LOG_ONCE(CLogger::Error, LOG_CATEGORY, L"Failed to find font '%ls'", name);
h = unifont_load(DefaultFont);
// Assume this worked
}
}
CFont::~CFont()
{
unifont_unload(h);
}
void CFont::Bind()
{
unifont_bind(h);
}
int CFont::GetLineSpacing()
{
return unifont_linespacing(h);
}
int CFont::GetHeight()
{
return unifont_height(h);
}
int CFont::GetCharacterWidth(wchar_t c)
{
return unifont_character_width(h, c);
}
void CFont::CalculateStringSize(const CStrW& string, int& width, int& height)
{
unifont_stringsize(h, (const wchar_t*)string, width, height);
}