Files
itgmania212121/stepmania/src/FontManager.cpp
T

171 lines
4.8 KiB
C++
Raw Normal View History

#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: FontManager
2003-01-04 07:55:42 +00:00
Desc: Interface for loading and releasing fonts.
2003-01-04 07:55:42 +00:00
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
Chris Danford
Glenn Maynard
-----------------------------------------------------------------------------
*/
#include "FontManager.h"
#include "Font.h"
#include "RageUtil.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "RageTimer.h"
2002-06-14 22:25:22 +00:00
2003-01-06 02:50:25 +00:00
FontManager::aliasmap FontManager::CharAliases;
map<CString,CString> FontManager::CharAliasRepl;
FontManager* FONT = NULL;
FontManager::FontManager()
{
}
FontManager::~FontManager()
{
2002-09-07 08:20:10 +00:00
for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
2002-09-07 08:20:10 +00:00
Font* pFont = i->second;
LOG->Trace( "FONT LEAK: '%s', RefCount = %d.", i->first.GetString(), pFont->m_iRefCount );
delete pFont;
}
2003-01-06 02:50:25 +00:00
}
2003-01-05 03:32:41 +00:00
2003-01-06 02:50:25 +00:00
/* A longchar is at least 32 bits. If c & 0xFF000000, it's a game-custom
* character; game 0 is 0x01nnnnnn, game 1 is 0x02nnnnnn, and so on. */
longchar FontManager::MakeGameGlyph(longchar c, Game g)
{
2003-01-07 04:02:57 +00:00
ASSERT(g >= 0 && g <= 0xFF && c >= 0 && c <= 0xFFFF);
return longchar (((g+1) << 24) + c);
2003-01-06 02:50:25 +00:00
}
bool FontManager::ExtractGameGlyph(longchar ch, int &c, Game &g)
{
if((ch & 0xFF000000) == 0) return false; /* not a game glyph */
g = Game((ch >> 24) - 1);
c = ch & 0xFFFFFF;
return true;
}
void FontManager::ReloadFonts()
{
for(map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
i->second->Reload();
2003-01-05 03:32:41 +00:00
}
}
Font* FontManager::LoadFont( const CString &sFontOrTextureFilePath, CString sChars )
{
2003-01-06 05:53:03 +00:00
InitCharAliases();
// Convert the path to lowercase so that we don't load duplicates.
// Really, this does not solve the duplicate problem. We could have two copies
// of the same bitmap if there are equivalent but different paths
// (e.g. "graphics\blah.png" and "..\stepmania\graphics\blah.png" ).
2003-01-04 05:20:40 +00:00
map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontOrTextureFilePath);
2002-09-07 08:20:10 +00:00
if(p != m_mapPathToFont.end()) {
2003-01-04 05:20:40 +00:00
Font *pFont=p->second;
2003-01-04 08:12:19 +00:00
// LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontOrTextureFilePath.GetString(), pFont->m_iRefCount) );
pFont->m_iRefCount++;
2003-01-04 05:20:40 +00:00
return pFont;
}
/* XXX: This will mismatch cached fonts if we load the same font with two
* different sChars. */
Font *f = new Font;
f->Load(sFontOrTextureFilePath, sChars);
2003-01-05 03:32:41 +00:00
m_mapPathToFont[sFontOrTextureFilePath] = f;
return f;
}
2003-01-04 05:20:40 +00:00
void FontManager::UnloadFont( Font *fp )
{
2003-01-05 03:32:41 +00:00
LOG->Trace( "FontManager::UnloadTexture(%s).", fp->path.GetString() );
2003-01-04 05:20:40 +00:00
for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
2003-01-04 05:20:40 +00:00
if(i->second == fp)
{
i->second->m_iRefCount--;
2003-01-04 05:20:40 +00:00
if( fp->m_iRefCount == 0 )
{
delete i->second; // free the texture
m_mapPathToFont.erase( i ); // and remove the key in the map
}
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
2003-01-04 05:20:40 +00:00
return;
}
}
RageException::Throw( "Unloaded an unknown font (%p)", fp );
}
2003-01-04 07:55:42 +00:00
2003-01-06 05:53:03 +00:00
void FontManager::InitCharAliases()
{
if(!CharAliases.empty())
return;
/* XXX; these should be in a data file somewhere (theme metrics?)
2003-01-06 23:49:43 +00:00
* (The comments here are UTF-8; they won't show up in VC6.) */
2003-01-06 05:53:03 +00:00
CharAliases["default"] = Font::DEFAULT_GLYPH; /* ? */
CharAliases["kakumei1"] = 0x9769; /* 革 */
CharAliases["kakumei2"] = 0x547D; /* 命 */
CharAliases["matsuri"] = 0x796D; /* 祭 */
CharAliases["oni"] = 0x9B3C; /* 鬼 */
CharAliases["michi"] = 0x9053; /* 道 */
CharAliases["futatsu"] = 0x5F10; /* 弐 */
CharAliases["omega"] = 0x03a9; /* Ω */
CharAliases["whiteheart"] = 0x2661; /* ♡ */
CharAliases["blackstar"] = 0x2605; /* ★ */
2003-01-06 23:49:43 +00:00
CharAliases["whitestar"] = 0x2606; /* ☆ */
2003-01-06 05:53:03 +00:00
2003-01-06 23:49:43 +00:00
/* These are internal-use glyphs; they don't have real Unicode codepoints. */
CharAliases["up"] = 0xE000;
CharAliases["down"] = 0xE001;
CharAliases["left"] = 0xE002;
CharAliases["right"] = 0xE003;
CharAliases["menuup"] = 0xE004;
CharAliases["menudown"] = 0xE005;
CharAliases["menuleft"] = 0xE006;
CharAliases["menuright"] = 0xE007;
CharAliases["start"] = 0xE008;
2003-01-06 05:53:03 +00:00
CharAliases["invalid"] = INVALID_CHAR; /* 0xFFFF */
2003-01-06 05:53:03 +00:00
for(aliasmap::const_iterator i = CharAliases.begin(); i != CharAliases.end(); ++i)
{
CString from = ssprintf("&%s;", i->first.GetString());
CString to = LcharToUTF8(i->second);
from.MakeUpper();
CharAliasRepl[from] = to;
LOG->Trace("from '%s' to '%s'", from.GetString(), to.GetString());
}
}
/* Replace all &markers; and &#NNNN;s with UTF-8. I'm not really
* sure where to put this; this is used elsewhere, too. */
void FontManager::ReplaceMarkers( CString &sText )
{
InitCharAliases();
ReplaceText(sText, FontManager::CharAliasRepl);
Replace_Unicode_Markers(sText);
}