Files
itgmania212121/stepmania/src/Font.cpp
T

171 lines
4.4 KiB
C++
Raw Normal View History

#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: Font
Desc: See header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#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"
2002-06-14 22:25:22 +00:00
2002-08-23 01:06:36 +00:00
#include <stdio.h>
#include <assert.h>
Font::Font( const CString &sASCIITexturePath )
{
//LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() );
int i;
for( i=0; i<MAX_FONT_CHARS; i++ )
{
m_iCharToFrameNo[i] = -1;
m_iFrameNoToWidth[i] = -1;
}
m_iRefCount = 1;
// load texture
m_sTexturePath = sASCIITexturePath;
m_sTexturePath.MakeLower();
2002-11-20 01:24:36 +00:00
m_pTexture = TEXTUREMAN->LoadTexture( 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;
}
2002-03-31 07:55:25 +00:00
float fScaleAllWidthsBy = 0;
if( ini.GetValueF( "Char Widths", "ScaleAllWidthsBy", fScaleAllWidthsBy ) )
{
for( int i=0; i<256; i++ )
2002-09-09 02:59:48 +00:00
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; i<MAX_FONT_CHARS; i++ )
{
m_iCharToFrameNo[i] = -1;
m_iFrameNoToWidth[i] = -1;
}
m_bCapitalsOnly = false;
m_iDrawExtraPixelsLeft = 0;
m_iDrawExtraPixelsRight = 0;
m_iRefCount = 1;
//
// load texture
//
m_sTexturePath = sTexturePath; // save
m_sTexturePath.MakeLower();
2002-11-20 01:24:36 +00:00
m_pTexture = TEXTUREMAN->LoadTexture( 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; i<sCharacters.GetLength(); i++ )
{
char c = sCharacters[i];
int iFrameNo = i;
m_iCharToFrameNo[c] = iFrameNo;
}
// Assume each character is the width of the frame.
for( i=0; i<(int)m_pTexture->GetNumFrames(); i++ )
m_iFrameNoToWidth[i] = m_pTexture->GetSourceFrameWidth();
}
Font::~Font()
{
if( m_pTexture != NULL )
2002-05-19 01:59:48 +00:00
TEXTUREMAN->UnloadTexture( m_sTexturePath );
}
int Font::GetLineWidthInSourcePixels( const char *szLine, int iLength )
{
int iLineWidth = 0;
for( int i=0; i<iLength; i++ )
{
const char c = szLine[i];
2002-09-22 20:34:11 +00:00
const int iFrameNo = m_iCharToFrameNo[ (unsigned char)c ];
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
throw RageException( "The font '%s' does not implement the character '%c'", m_sTexturePath.GetString(), c );
iLineWidth += m_iFrameNoToWidth[iFrameNo];
}
return iLineWidth;
}