#include "stdafx.h" /* ----------------------------------------------------------------------------- Class: Font Desc: See header. Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved. Chris Danford ----------------------------------------------------------------------------- */ #include "Font.h" #include "IniFile.h" #include "RageTextureManager.h" #include "RageUtil.h" #include "RageLog.h" #include "RageException.h" #include #include Font::Font( const CString &sASCIITexturePath ) { //LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() ); int i; for( i=0; iLoadTexture( m_sTexturePath, RageTexturePrefs() ); ASSERT( m_pTexture != NULL ); if( m_pTexture->GetNumFrames() != 16*16 ) throw RageException( "The font '%s' has only %d frames. All fonts must have 16*16 frames.", m_sTexturePath.GetString() ); for( i=0; i<256; i++ ) m_iCharToFrameNo[i] = i; // Find .ini widths path from texture path CString sDir, sFileName, sExtension; splitrelpath( sASCIITexturePath, sDir, sFileName, sExtension ); const CString sIniPath = sDir + sFileName + ".ini"; IniFile ini; ini.SetPath( sIniPath ); ini.ReadFile(); // load character widths for( i=0; i<256; i++ ) if( !ini.GetValueI( "Char Widths", ssprintf("%d",i), m_iFrameNoToWidth[i] ) ) throw RageException( "Error reading width value '%d' from '%s'.", i, sIniPath.GetString() ); m_bCapitalsOnly = false; ini.GetValueB( "Char Widths", "CapitalsOnly", m_bCapitalsOnly ); m_iDrawExtraPixelsLeft = 0; ini.GetValueI( "Char Widths", "DrawExtraPixelsLeft", m_iDrawExtraPixelsLeft ); m_iDrawExtraPixelsRight = 0; ini.GetValueI( "Char Widths", "DrawExtraPixelsRight", m_iDrawExtraPixelsRight ); int iAddToAllWidths = 0; if( ini.GetValueI( "Char Widths", "AddToAllWidths", iAddToAllWidths ) ) { for( int i=0; i<256; i++ ) m_iFrameNoToWidth[i] += iAddToAllWidths; } float fScaleAllWidthsBy = 0; if( ini.GetValueF( "Char Widths", "ScaleAllWidthsBy", fScaleAllWidthsBy ) ) { for( int i=0; i<256; i++ ) m_iFrameNoToWidth[i] = int(roundf( m_iFrameNoToWidth[i] * fScaleAllWidthsBy )); } m_iLineSpacing = m_pTexture->GetSourceFrameHeight(); ini.GetValueI( "Char Widths", "LineSpacing", m_iLineSpacing ); // force widths to even number for( i=0; i<256; i++ ) if( m_iFrameNoToWidth[i]%2 == 1 ) m_iFrameNoToWidth[i]++; } Font::Font( const CString &sTexturePath, const CString& sCharacters ) { //LOG->Trace( "Font::LoadFromFontName(%s)", sFontFilePath.GetString() ); int i; for( i=0; iLoadTexture( m_sTexturePath, RageTexturePrefs() ); ASSERT( m_pTexture != NULL ); m_iLineSpacing = m_pTexture->GetSourceFrameHeight(); // // find out what characters are in this font // // sanity check if( sCharacters.GetLength() != m_pTexture->GetNumFrames() ) throw RageException( "The image '%s' doesn't have the correct number of frames. It has %d frames but should have %d frames.", m_sTexturePath.GetString(), m_pTexture->GetNumFrames(), sCharacters.GetLength() ); // set the char to frame number map for( i=0; iGetNumFrames(); i++ ) m_iFrameNoToWidth[i] = m_pTexture->GetSourceFrameWidth(); } Font::~Font() { if( m_pTexture != NULL ) TEXTUREMAN->UnloadTexture( m_sTexturePath ); } int Font::GetLineWidthInSourcePixels( const char *szLine, int iLength ) { int iLineWidth = 0; for( int i=0; i