1
0
forked from 0ad/0ad

Altered font handling -- now uses CFont in preference to unifont_* (yay, pointless OO) and allows font mapping ("default"=>"palatino12", etc) using .cfg files

This was SVN commit r1074.
This commit is contained in:
Ykkrosh 2004-08-27 22:08:30 +00:00
parent 9280aa15a7
commit 70d84beab2
9 changed files with 163 additions and 43 deletions

View File

@ -46,7 +46,7 @@ void CButton::SetupText()
CStr font;
if (GUI<CStr>::GetSetting(this, "font", font) != PS_OK || font.Length()==0)
// Use the default if none is specified
font = "palatino12";
font = "default";
CGUIString caption;
GUI<CGUIString>::GetSetting(this, "caption", caption);

View File

@ -69,7 +69,7 @@ void CCheckBox::SetupText()
//GUI<CGUIString>::GetSetting(this, "square-side", square_side);
// TODO Gee: Establish buffer zones
*m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, CStr("palatino12"), m_CachedActualSize.GetWidth()-20, 0);
*m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, CStr("default"), m_CachedActualSize.GetWidth()-20, 0);
// Set position of text
// TODO Gee: Big TODO

View File

@ -15,6 +15,7 @@ gee@pyro.nu
#include "CRadioButton.h"
#include "ps/Xeromyces.h"
#include "ps/Font.h"
#include "Prometheus.h"
#include "input.h"
@ -847,7 +848,7 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor,
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Handle font = 0;
CFont* font = NULL;
CStr LastFontName;
for (vector<SGUIText::STextCall>::const_iterator it = Text.m_TextCalls.begin();
@ -860,10 +861,9 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor,
// Switch fonts when necessary, but remember the last one used
if (it->m_Font != LastFontName)
{
if (font)
unifont_unload(font);
font = unifont_load(it->m_Font);
unifont_bind(font);
delete font;
font = new CFont(it->m_Font);
font->Bind();
LastFontName = it->m_Font;
}
@ -879,8 +879,7 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor,
}
if (font)
unifont_unload(font);
delete font;
for (vector<SGUIText::SSpriteCall>::const_iterator it=Text.m_SpriteCalls.begin();
it!=Text.m_SpriteCalls.end();

View File

@ -56,7 +56,7 @@ void CText::SetupText()
CStr font;
if (GUI<CStr>::GetSetting(this, "font", font) != PS_OK || font.Length()==0)
// Use the default if none is specified
font = "palatino12";
font = "default";
CGUIString caption;
bool scrollbar;

View File

@ -11,6 +11,8 @@ gee@pyro.nu
#include "OverlayText.h"
#include <algorithm>
#include "ps/Font.h"
// TODO Gee: Remove, just for temp-output
#include <fstream>
@ -132,16 +134,12 @@ void CGUIString::GenerateTextCall(SFeedback &Feedback,
}
}
// Calculate the size of the font
CSize size;
CFont font (TextCall.m_Font);
font.CalculateStringSize(TextCall.m_String, size.cx, size.cy);
//COverlayText txt(0, 0, 0, TextCall.m_Font, TextCall.m_String, TextCall.m_Color);
// TODO Gee: Ask Rich to change to (size);
//txt.GetOutputStringSize((int&)size.cx, (int&)size.cy);
Handle font = unifont_load(TextCall.m_Font);
unifont_stringsize(font, TextCall.m_String.c_str(), size.cx, size.cy);
unifont_unload(font);
// append width, and make maximum height the height.
// Append width, and make maximum height the height.
Feedback.m_Size.cx += size.cx;
Feedback.m_Size.cy = max(Feedback.m_Size.cy, size.cy);

View File

@ -1,5 +1,5 @@
/*
$Id: unifont.cpp,v 1.13 2004/08/24 21:07:20 philip Exp $
$Id: unifont.cpp,v 1.14 2004/08/27 22:07:34 philip Exp $
Unicode OpenGL texture font
@ -67,8 +67,14 @@ static int UniFont_reload(UniFont* f, const char* fn, Handle UNUSEDPARAM(h))
void* RawFNT;
size_t FNTSize;
std::string FilenameFnt = FilenameBase+".fnt";
const char* fnt_fn = FilenameFnt.c_str();
std::string FilenameFnt = FilenameBase+".fnt";
const char* fnt_fn = FilenameFnt.c_str();
// Fail quietly if the file simply doesn't exist
if (! vfs_exists(fnt_fn))
return -1;
Handle fh = vfs_load(fnt_fn, RawFNT, FNTSize);
CHECK_ERR(fh);

View File

@ -19,6 +19,8 @@
#endif
#include "lib/res/cursor.h"
#include "ps/Font.h"
#include "ps/CConsole.h"
#include "ps/Game.h"
@ -104,9 +106,6 @@ static float g_Gamma = 1.0f;
CGameAttributes g_GameAttributes;
extern int game_view_handler(const SDL_Event* ev);
static Handle g_Font_Console; // for the console
static Handle g_Font_Misc; // random font for miscellaneous things
static CMusicPlayer MusicPlayer;
CStr g_CursorName = "test";
@ -459,9 +458,11 @@ static void Render()
// Temp GUI message GeeTODO
glLoadIdentity();
glTranslatef(10, 60, 0);
unifont_bind(g_Font_Misc);
glwprintf( L"%hs", g_GUI.TEMPmessage.c_str() );
{
CFont font("misc");
font.Bind();
glwprintf( L"%hs", g_GUI.TEMPmessage.c_str() );
}
oglCheck();
glLoadIdentity();
@ -479,8 +480,6 @@ static void Render()
oglCheck();
unifont_bind(g_Font_Misc);
glColor4f(1.0f, 0.8f, 0.0f, 1.0f);
glLoadIdentity();
@ -489,14 +488,22 @@ static void Render()
glScalef(1.0, -1.0, 1.0);
CStrW fps_display = translate(L"$num FPS") << fps;
glwprintf(fps_display);
{
CFont font("misc");
font.Bind();
glwprintf(fps_display);
}
oglCheck();
unifont_bind(g_Font_Console);
glLoadIdentity();
MICROLOG(L"render console");
g_Console->Render();
{
glLoadIdentity();
MICROLOG(L"render console");
CFont font("console");
font.Bind();
g_Console->Render();
}
oglCheck();
@ -663,14 +670,16 @@ static void InitVfs(char* argv0)
static void psInit()
{
g_Font_Console = unifont_load("console");
g_Font_Misc = unifont_load("verdana16");
g_Console->SetSize(0, g_yres-600.f, (float)g_xres, 600.f);
g_Console->m_iFontHeight = unifont_linespacing(g_Font_Console);
g_Console->m_iFontOffset = 9;
{
// Calculate and store the line spacing
CFont font("console");
g_Console->m_iFontHeight = font.GetLineSpacing();
// Offset by an arbitrary amount, to make it fit more nicely
g_Console->m_iFontOffset = 9;
}
I18n::LoadLanguage("pseudogreek");
I18n::LoadLanguage("english");
loadHotkeys();
@ -691,9 +700,6 @@ static void psShutdown()
delete &g_GUI;
#endif
unifont_unload(g_Font_Misc);
unifont_unload(g_Font_Console);
delete g_Console;
// disable the special Windows cursor, or free textures for OGL cursors

72
source/ps/Font.cpp Executable file
View File

@ -0,0 +1,72 @@
#include "precompiled.h"
#include "Font.h"
#include "ps/ConfigDB.h"
#include "lib/res/unifont.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY "graphics"
#include <map>
#include <string>
const char* DefaultFont = "verdana16";
CFont::CFont(const char* 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;
CStr fontName = "font."; fontName += name;
// See if the config value can be loaded
CConfigValue* fontFilenameVar = g_ConfigDB.GetValue(CFG_SYSTEM, fontName);
if (fontFilenameVar && fontFilenameVar->GetString(fontFilename))
{
h = unifont_load(fontFilename.c_str());
}
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(ERROR, LOG_CATEGORY, "Failed to find font '%s'", name);
h = unifont_load(DefaultFont);
// Assume this worked
}
}
CFont::~CFont()
{
unifont_unload(h);
}
// Copy constructor
CFont::CFont(const CFont& font)
{
// Reload the font, to maintain the ref count
h = unifont_load( h_filename(font.h) );
}
void CFont::Bind()
{
unifont_bind(h);
}
int CFont::GetLineSpacing()
{
return unifont_linespacing(h);
}
void CFont::CalculateStringSize(const CStr& string, int& width, int& height)
{
unifont_stringsize(h, string.c_str(), width, height);
}

39
source/ps/Font.h Executable file
View File

@ -0,0 +1,39 @@
#ifndef _FONT_H
#define _FONT_H
#include "ps/Singleton.h"
#include "ps/CStr.h"
#include "lib/res/h_mgr.h"
#include <map>
#include <string>
/*
To use CFont:
CFont font("name");
font.Bind();
glwprintf(L"Hello world");
*/
class CFont
{
public:
CFont(const char* name);
~CFont();
CFont(const CFont&);
void Bind();
int GetLineSpacing();
void CalculateStringSize(const CStr& string, int& w, int& h);
private:
Handle h;
};
#endif // _FONT_H