Files
itgmania212121/stepmania/src/Font.cpp
T

292 lines
7.4 KiB
C++
Raw Normal View History

#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: Font
Desc: See header.
2003-01-04 05:20:40 +00:00
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
Chris Danford
2003-01-04 05:17:51 +00:00
Glenn Maynard
-----------------------------------------------------------------------------
*/
#include "Font.h"
#include "IniFile.h"
2002-08-23 01:06:36 +00:00
#include "RageTextureManager.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 "FontManager.h"
2003-01-06 02:50:25 +00:00
#include "GameState.h"
2003-01-06 04:24:28 +00:00
const longchar Font::DEFAULT_GLYPH = 0xFFFFFF;
2002-06-14 22:25:22 +00:00
2003-01-04 05:17:51 +00:00
FontPage::FontPage()
{
2003-01-04 05:17:51 +00:00
m_pTexture = NULL;
}
2003-01-05 03:32:41 +00:00
void FontPage::Load( const CString &TexturePath, const FontPageSettings &cfg )
{
2003-01-03 22:53:22 +00:00
m_sTexturePath = TexturePath;
// load texture
m_sTexturePath.MakeLower();
m_pTexture = TEXTUREMAN->LoadTexture( m_sTexturePath );
ASSERT( m_pTexture != NULL );
2003-01-06 23:49:43 +00:00
/* All characters on a page have the same vertical spacing, baseline
* and center. */
baseline = cfg.Baseline;
if(baseline == -1)
{
/* The default baseline is 75% down the frame; this is where it
* tends to be. */
baseline = int(m_pTexture->GetTextureFrameHeight() * .75f);
}
center = cfg.Center;
if(center == -1)
{
/* We don't know the center, so assume it's the center of the frame. */
center = m_pTexture->GetSourceFrameHeight()/2;
}
kanji = cfg.Kanji;
// load character widths
2003-01-05 05:13:45 +00:00
vector<int> FrameWidths;
2003-01-03 22:53:22 +00:00
int i;
// Assume each character is the width of the frame by default.
2003-01-04 05:17:51 +00:00
for( i=0; i<m_pTexture->GetNumFrames(); i++ )
2003-01-03 22:53:22 +00:00
{
2003-01-05 03:32:41 +00:00
map<int,int>::const_iterator it = cfg.GlyphWidths.find(i);
if(it != cfg.GlyphWidths.end())
2003-01-03 22:53:22 +00:00
{
2003-01-05 03:32:41 +00:00
FrameWidths.push_back(it->second);
} else {
FrameWidths.push_back(m_pTexture->GetSourceFrameWidth());
2003-01-03 22:53:22 +00:00
}
}
2003-01-05 03:32:41 +00:00
if( cfg.AddToAllWidths )
{
for( int i=0; i<256; i++ )
2003-01-05 03:32:41 +00:00
FrameWidths[i] += cfg.AddToAllWidths;
}
2002-03-31 07:55:25 +00:00
2003-01-05 03:32:41 +00:00
if( cfg.ScaleAllWidthsBy != 1 )
{
for( int i=0; i<256; i++ )
2003-01-05 03:32:41 +00:00
FrameWidths[i] = int(roundf( FrameWidths[i] * cfg.ScaleAllWidthsBy ));
}
2003-01-05 03:32:41 +00:00
m_iCharToGlyphNo = cfg.CharToGlyphNo;
int LineSpacing = cfg.LineSpacing;
if(LineSpacing == -1)
LineSpacing = m_pTexture->GetSourceFrameHeight();
2003-01-04 05:17:51 +00:00
SetTextureCoords(FrameWidths, LineSpacing);
2003-01-05 03:32:41 +00:00
SetExtraPixels(cfg.DrawExtraPixelsLeft, cfg.DrawExtraPixelsRight);
}
2003-01-04 05:17:51 +00:00
void FontPage::SetTextureCoords(const vector<int> &widths, int LineSpacing)
{
for(int i = 0; i < m_pTexture->GetNumFrames(); ++i)
{
2003-01-03 22:53:22 +00:00
glyph g;
2003-01-06 23:49:43 +00:00
g.fp = this;
/* Make a copy of each texture rect, reducing each to the actual dimensions
* of the character (most characters don't take a full block). */
2003-01-03 22:53:22 +00:00
g.rect = *m_pTexture->GetTextureCoordRect(i);;
2003-01-04 01:47:01 +00:00
/* Set the width and height to the width and line spacing, respectively. */
g.width = float(widths[i]);
g.height = float(m_pTexture->GetSourceFrameHeight());
2003-01-04 07:55:42 +00:00
/* By default, advance one pixel more than the width. (This could be
* an option.) */
g.hadvance = int(g.width + 1);
2003-01-06 23:49:43 +00:00
g.vadvance = LineSpacing;
2003-01-04 07:55:42 +00:00
2003-01-04 01:47:01 +00:00
/* Shift the character up so the top of the rendered quad is at the top
* of the character. */
2003-01-06 23:49:43 +00:00
g.vshift = -(m_pTexture->GetSourceFrameHeight() - g.vadvance)/2.0f;
2003-01-03 22:53:22 +00:00
2003-01-04 01:47:01 +00:00
/* Do the same thing with X. Do this by changing the actual rendered
* rect, instead of shifting it, so we don't render more than we need to. */
g.hshift = 0;
{
int iPixelsToChopOff = m_pTexture->GetSourceFrameWidth() - widths[i];
2003-01-04 07:55:42 +00:00
if((iPixelsToChopOff % 2) == 1)
{
/* We don't want to chop off an odd number of pixels, since that'll
* put our texture coordinates between texels and make things blurrier. */
iPixelsToChopOff--;
g.width++;
}
2003-01-06 02:50:25 +00:00
float fTexCoordsToChopOff = float(iPixelsToChopOff) / m_pTexture->GetTextureWidth();
2003-01-04 07:55:42 +00:00
2003-01-04 01:47:01 +00:00
g.rect.left += fTexCoordsToChopOff/2;
g.rect.right -= fTexCoordsToChopOff/2;
}
2003-01-04 07:55:42 +00:00
g.Texture = m_pTexture;
2003-01-03 22:53:22 +00:00
glyphs.push_back(g);
}
}
2003-01-04 05:17:51 +00:00
void FontPage::SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight)
{
2003-01-04 05:17:51 +00:00
/* Hack: do one more than we were asked to; I think a lot of fonts are one
* too low. */
DrawExtraPixelsRight++;
DrawExtraPixelsLeft++;
2003-01-04 07:55:42 +00:00
if((DrawExtraPixelsLeft % 2) == 1)
DrawExtraPixelsLeft++;
/* Adjust for DrawExtraPixelsLeft and DrawExtraPixelsRight. */
2003-01-03 22:53:22 +00:00
for(unsigned i = 0; i < glyphs.size(); ++i)
{
int iFrameWidth = m_pTexture->GetSourceFrameWidth();
2003-01-04 07:55:42 +00:00
float iCharWidth = glyphs[i].width;
2003-01-04 07:55:42 +00:00
/* Extra pixels to draw to the left and right. We don't have to
* worry about alignment here; CharWidth is always even (by
* SetTextureCoords) and iFrameWidth are almost always even. */
2003-01-04 01:47:01 +00:00
float ExtraLeft = min( float(DrawExtraPixelsLeft), (iFrameWidth-iCharWidth)/2.0f );
float ExtraRight = min( float(DrawExtraPixelsRight), (iFrameWidth-iCharWidth)/2.0f );
/* Move left and expand right. */
2003-01-06 02:50:25 +00:00
glyphs[i].rect.left -= ExtraLeft / m_pTexture->GetTextureWidth();
glyphs[i].rect.right += ExtraRight / m_pTexture->GetTextureWidth();
2003-01-04 01:47:01 +00:00
glyphs[i].hshift -= ExtraLeft;
glyphs[i].width += ExtraLeft + ExtraRight;
}
}
2003-01-04 05:17:51 +00:00
FontPage::~FontPage()
{
if( m_pTexture != NULL )
2003-01-03 22:53:22 +00:00
TEXTUREMAN->UnloadTexture( m_pTexture );
}
int Font::GetLineWidthInSourcePixels( const lstring &szLine ) const
{
2003-01-04 07:55:42 +00:00
int LineWidth = 0;
2002-12-29 22:55:26 +00:00
for( unsigned i=0; i<szLine.size(); i++ )
2003-01-04 07:55:42 +00:00
LineWidth += GetGlyph(szLine[i]).hadvance;
2003-01-04 07:55:42 +00:00
return LineWidth;
}
int Font::GetLineHeightInSourcePixels( const lstring &szLine ) const
2003-01-04 01:47:01 +00:00
{
2003-01-04 07:55:42 +00:00
int iLineSpacing = 0;
2003-01-04 05:17:51 +00:00
2003-01-04 07:55:42 +00:00
/* The spacing of a line is the spacing of its tallest used font page. */
for( unsigned i=0; i<szLine.size(); i++ )
iLineSpacing = max(iLineSpacing, GetGlyph(szLine[i]).vadvance);
2003-01-04 05:17:51 +00:00
2003-01-04 07:55:42 +00:00
return iLineSpacing;
2003-01-04 05:17:51 +00:00
}
Font::Font()
{
//LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() );
m_iRefCount = 1;
}
Font::~Font()
{
for(unsigned i = 0; i < pages.size(); ++i)
delete pages[i];
/* Free any fonts that we merged into us. */
for(unsigned i = 0; i < merged_fonts.size(); ++i)
FONT->UnloadFont(merged_fonts[i]);
2003-01-04 01:47:01 +00:00
}
2003-01-04 05:17:51 +00:00
void Font::AddPage(FontPage *fp)
{
pages.push_back(fp);
for(map<longchar,int>::const_iterator it = fp->m_iCharToGlyphNo.begin();
2003-01-04 05:17:51 +00:00
it != fp->m_iCharToGlyphNo.end(); ++it)
{
m_iCharToGlyph[it->first] = &fp->glyphs[it->second];
}
}
void Font::MergeFont(Font *f)
{
for(map<longchar,glyph*>::iterator it = f->m_iCharToGlyph.begin();
it != f->m_iCharToGlyph.end(); ++it)
{
m_iCharToGlyph[it->first] = it->second;
}
/* We now have ownership of f. Mark it to be freed. */
merged_fonts.push_back(f);
}
const glyph &Font::GetGlyph( longchar c ) const
2003-01-04 05:17:51 +00:00
{
2003-01-06 04:24:28 +00:00
ASSERT(c >= 0 && c <= 0xFFFFFF);
2003-01-06 02:50:25 +00:00
/* See if there's a game-specific version of this character. */
int gc = FontManager::MakeGameGlyph(c, GAMESTATE->m_CurGame);
map<longchar,glyph*>::const_iterator it = m_iCharToGlyph.find(gc);
2003-01-04 05:17:51 +00:00
2003-01-06 02:50:25 +00:00
/* If there isn't, try the regular character. */
if(it == m_iCharToGlyph.end()) it = m_iCharToGlyph.find(c);
2003-01-04 05:17:51 +00:00
2003-01-06 02:50:25 +00:00
/* If *that's* missing, use the default glyph. */
if(it == m_iCharToGlyph.end()) it = m_iCharToGlyph.find(DEFAULT_GLYPH);
if(it == m_iCharToGlyph.end())
RageException::Throw( "The default glyph is missing from the font '%s'", path.GetString() );
2003-01-04 05:17:51 +00:00
return *it->second;
}
RageTexture *Font::GetGlyphTexture( longchar c )
2003-01-04 07:55:42 +00:00
{
2003-01-06 02:50:25 +00:00
glyph &g = const_cast<glyph &> (GetGlyph(c));
return g.Texture;
2003-01-04 07:55:42 +00:00
}
2003-01-05 03:32:41 +00:00
void Font::CapsOnly()
2003-01-04 05:17:51 +00:00
{
2003-01-05 03:32:41 +00:00
/* For each uppercase character that we have a mapping for, add
* a lowercase one. */
for(char c = 'A'; c <= 'Z'; ++c)
2003-01-04 05:17:51 +00:00
{
map<longchar,glyph*>::const_iterator it = m_iCharToGlyph.find(c);
2003-01-04 05:17:51 +00:00
2003-01-05 03:32:41 +00:00
if(it == m_iCharToGlyph.end())
continue;
2003-01-04 05:17:51 +00:00
2003-01-05 05:13:45 +00:00
m_iCharToGlyph[(char) tolower(c)] = it->second;
2003-01-04 05:17:51 +00:00
}
}
2003-01-06 23:49:43 +00:00
void Font::SetDefaultGlyph(FontPage *fp)
{
ASSERT(fp);
ASSERT(!fp->glyphs.empty());
def = fp;
}