2002-03-30 20:00:13 +00:00
|
|
|
#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.
|
2002-03-30 20:00:13 +00:00
|
|
|
Chris Danford
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Font.h"
|
|
|
|
|
#include "IniFile.h"
|
2002-08-23 01:06:36 +00:00
|
|
|
|
2002-03-30 20:00:13 +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>
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
Font::Font( const CString &sASCIITexturePath )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
//LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() );
|
2002-09-03 22:31:06 +00:00
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for( i=0; i<MAX_FONT_CHARS; i++ )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
|
|
|
|
m_iCharToFrameNo[i] = -1;
|
|
|
|
|
m_iFrameNoToWidth[i] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_iRefCount = 1;
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
// load texture
|
|
|
|
|
m_sTexturePath = sASCIITexturePath;
|
|
|
|
|
m_sTexturePath.MakeLower();
|
|
|
|
|
m_pTexture = TEXTUREMAN->LoadTexture( m_sTexturePath );
|
|
|
|
|
ASSERT( m_pTexture != NULL );
|
|
|
|
|
if( m_pTexture->GetNumFrames() != 16*16 )
|
2002-10-29 07:58:44 +00:00
|
|
|
throw RageException( "The font '%s' has only %d frames. All fonts must have 16*16 frames.", m_sTexturePath.GetString() );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
for( i=0; i<256; i++ )
|
|
|
|
|
m_iCharToFrameNo[i] = i;
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
// Find .ini widths path from texture path
|
|
|
|
|
CString sDir, sFileName, sExtension;
|
|
|
|
|
splitrelpath( sASCIITexturePath, sDir, sFileName, sExtension );
|
|
|
|
|
const CString sIniPath = sDir + sFileName + ".ini";
|
2002-03-30 20:00:13 +00:00
|
|
|
IniFile ini;
|
2002-09-03 22:31:06 +00:00
|
|
|
ini.SetPath( sIniPath );
|
|
|
|
|
ini.ReadFile();
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
// load character widths
|
|
|
|
|
for( i=0; i<256; i++ )
|
|
|
|
|
if( !ini.GetValueI( "Char Widths", ssprintf("%d",i), m_iFrameNoToWidth[i] ) )
|
2002-10-29 07:58:44 +00:00
|
|
|
throw RageException( "Error reading width value '%d' from '%s'.", i, sIniPath.GetString() );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_bCapitalsOnly = false;
|
|
|
|
|
ini.GetValueB( "Char Widths", "CapitalsOnly", m_bCapitalsOnly );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_iDrawExtraPixelsLeft = 0;
|
|
|
|
|
ini.GetValueI( "Char Widths", "DrawExtraPixelsLeft", m_iDrawExtraPixelsLeft );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_iDrawExtraPixelsRight = 0;
|
|
|
|
|
ini.GetValueI( "Char Widths", "DrawExtraPixelsRight", m_iDrawExtraPixelsRight );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
int iAddToAllWidths = 0;
|
|
|
|
|
if( ini.GetValueI( "Char Widths", "AddToAllWidths", iAddToAllWidths ) )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
2002-09-03 22:31:06 +00:00
|
|
|
for( int i=0; i<256; i++ )
|
|
|
|
|
m_iFrameNoToWidth[i] += iAddToAllWidths;
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|
2002-03-31 07:55:25 +00:00
|
|
|
|
2002-09-04 03:49:08 +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 ));
|
2002-09-04 03:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
2002-09-04 03:49:08 +00:00
|
|
|
ini.GetValueI( "Char Widths", "LineSpacing", m_iLineSpacing );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
// force widths to even number
|
|
|
|
|
for( i=0; i<256; i++ )
|
|
|
|
|
if( m_iFrameNoToWidth[i]%2 == 1 )
|
|
|
|
|
m_iFrameNoToWidth[i]++;
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-13 23:26:46 +00:00
|
|
|
Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
|
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
//LOG->Trace( "Font::LoadFromFontName(%s)", sFontFilePath.GetString() );
|
2002-08-13 23:26:46 +00:00
|
|
|
int i;
|
|
|
|
|
for( i=0; i<MAX_FONT_CHARS; i++ )
|
|
|
|
|
{
|
|
|
|
|
m_iCharToFrameNo[i] = -1;
|
|
|
|
|
m_iFrameNoToWidth[i] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_bCapitalsOnly = false;
|
2002-09-03 22:31:06 +00:00
|
|
|
m_iDrawExtraPixelsLeft = 0;
|
|
|
|
|
m_iDrawExtraPixelsRight = 0;
|
2002-08-13 23:26:46 +00:00
|
|
|
|
|
|
|
|
m_iRefCount = 1;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// load texture
|
|
|
|
|
//
|
2002-09-03 22:31:06 +00:00
|
|
|
m_sTexturePath = sTexturePath; // save
|
2002-08-13 23:26:46 +00:00
|
|
|
m_sTexturePath.MakeLower();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_pTexture = TEXTUREMAN->LoadTexture( m_sTexturePath );
|
2002-09-03 22:31:06 +00:00
|
|
|
ASSERT( m_pTexture != NULL );
|
|
|
|
|
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
2002-08-13 23:26:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// find out what characters are in this font
|
|
|
|
|
//
|
|
|
|
|
// sanity check
|
|
|
|
|
if( sCharacters.GetLength() != m_pTexture->GetNumFrames() )
|
2002-09-03 22:31:06 +00:00
|
|
|
throw RageException( "The image '%s' doesn't have the correct number of frames. It has %d frames but should have %d frames.",
|
2002-10-29 07:58:44 +00:00
|
|
|
m_sTexturePath.GetString(), m_pTexture->GetNumFrames(), sCharacters.GetLength() );
|
2002-08-13 23:26:46 +00:00
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
Font::~Font()
|
|
|
|
|
{
|
|
|
|
|
if( m_pTexture != NULL )
|
2002-05-19 01:59:48 +00:00
|
|
|
TEXTUREMAN->UnloadTexture( m_sTexturePath );
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-28 00:35:55 +00:00
|
|
|
int Font::GetLineWidthInSourcePixels( const char *szLine, int iLength )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
|
|
|
|
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 ];
|
2002-03-30 20:00:13 +00:00
|
|
|
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
|
2002-10-29 07:58:44 +00:00
|
|
|
throw RageException( "The font '%s' does not implement the character '%c'", m_sTexturePath.GetString(), c );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
|
|
|
|
iLineWidth += m_iFrameNoToWidth[iFrameNo];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return iLineWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|