more font work

This commit is contained in:
Glenn Maynard
2003-01-04 07:55:42 +00:00
parent d4c8420cb5
commit 5e8ced1a94
6 changed files with 193 additions and 139 deletions
+114 -92
View File
@@ -70,6 +70,8 @@ bool BitmapText::LoadFromFont( CString sFontFilePath )
// load font
m_pFont = FONT->LoadFont( sFontFilePath );
BuildChars();
return true;
}
@@ -86,15 +88,95 @@ bool BitmapText::LoadFromTextureAndChars( CString sTexturePath, CString sChars )
// load font
m_pFont = FONT->LoadFont( sTexturePath, sChars );
BuildChars();
return true;
}
void BitmapText::BuildChars()
{
/* If we don't have a font yet, we'll do this when it loads. */
if(m_pFont == NULL)
return;
verts.clear();
tex.clear();
int TotalHeight = 0;
unsigned i;
for(i = 0; i < m_szTextLines.size(); ++i)
TotalHeight += m_iLineHeights[i];
int iY; // the top position of the first row of characters
switch( m_VertAlign )
{
case align_top: iY = 0; break;
case align_middle: iY = -TotalHeight/2; break;
case align_bottom: iY = -TotalHeight; break;
default: ASSERT( false ); return;
}
for( i=0; i<m_szTextLines.size(); i++ ) // foreach line
{
const CString &szLine = m_szTextLines[i];
const int iLineWidth = m_iLineWidths[i];
int iX;
switch( m_HorizAlign )
{
case align_left: iX = 0; break;
case align_center: iX = -iLineWidth/2; break;
case align_right: iX = -iLineWidth; break;
default: ASSERT( false ); return;
}
for( unsigned j=0; j<szLine.size(); j++ ) // for each character in the line
{
RageVertex v[4];
const glyph &g = m_pFont->GetGlyph(szLine[j]);
/* set vertex positions */
v[0].p = RageVector3( iX+g.hshift, iY+g.vshift, 0 ); // top left
v[1].p = RageVector3( iX+g.hshift, iY+g.vshift+g.height, 0 ); // bottom left
v[2].p = RageVector3( iX+g.hshift+g.width, iY+g.vshift+g.height, 0 ); // bottom right
v[3].p = RageVector3( iX+g.hshift+g.width, iY+g.vshift, 0 ); // top right
/* Advance the cursor. */
iX += g.hadvance;
/* set texture coordinates */
v[0].t = RageVector2( g.rect.left, g.rect.top );
v[1].t = RageVector2( g.rect.left, g.rect.bottom );
v[2].t = RageVector2( g.rect.right, g.rect.bottom );
v[3].t = RageVector2( g.rect.right, g.rect.top );
verts.insert(verts.end(), &v[0], &v[4]);
tex.push_back(m_pFont->GetGlyphTexture(szLine[j]));
}
iY += m_iLineHeights[i];
}
}
void BitmapText::DrawChars()
{
for(unsigned start = 0; start < tex.size(); ++start)
{
unsigned end = start;
while(end < tex.size() && tex[end] == tex[start])
end++;
DISPLAY->SetTexture( tex[start] );
DISPLAY->DrawQuads( &verts[start*4], (end-start)*4 );
start = end+1;
}
}
void BitmapText::SetText( CString sText )
{
ASSERT( m_pFont );
if(m_szText == sText)
return;
m_szText = sText;
@@ -114,6 +196,8 @@ void BitmapText::SetText( CString sText )
m_iLineHeights.push_back(m_pFont->GetLineHeightInSourcePixels( m_szTextLines[l] ));
m_iWidestLineWidth = max(m_iWidestLineWidth, m_iLineWidths.back());
}
BuildChars();
}
void BitmapText::CropToWidth( int iMaxWidthInSourcePixels )
@@ -136,82 +220,6 @@ void BitmapText::DrawPrimitives()
if( m_szTextLines.empty() )
return;
/* XXX */
RageTexture* pTexture = m_pFont->GetGlyph('0').Texture;
static RageVertex *v = NULL;
static int vcnt = 0;
{
int charcnt = 0;
for( unsigned i=0; i<m_szTextLines.size(); i++ )
charcnt += m_szTextLines[i].size();
charcnt *= 4; /* 4 vertices per char */
if(charcnt > vcnt)
{
vcnt = charcnt;
delete [] v;
v = new RageVertex[vcnt];
}
}
int iNumV = 0; // the current vertex number
float TotalHeight = 0;
unsigned i;
for(i = 0; i < m_szTextLines.size(); ++i)
TotalHeight += m_iLineHeights[i];
float iY; // the top position of the first row of characters
switch( m_VertAlign )
{
case align_top: iY = 0; break;
case align_middle: iY = -TotalHeight/2.0f; break;
case align_bottom: iY = -TotalHeight; break;
default: ASSERT( false ); return;
}
for( i=0; i<m_szTextLines.size(); i++ ) // foreach line
{
const CString &szLine = m_szTextLines[i];
const float fLineWidth = float(m_iLineWidths[i]);
float iX;
switch( m_HorizAlign )
{
case align_left: iX = 0; break;
case align_center: iX = -fLineWidth/2.0f; break;
case align_right: iX = -fLineWidth; break;
default: ASSERT( false ); return;
}
for( unsigned j=0; j<szLine.size(); j++ ) // for each character in the line
{
const glyph &g = m_pFont->GetGlyph(szLine[j]);
/* set vertex positions */
v[iNumV++].p = RageVector3( (float)iX+g.hshift, iY+g.vshift, 0 ); // top left
v[iNumV++].p = RageVector3( (float)iX+g.hshift, iY+g.vshift+g.height, 0 ); // bottom left
v[iNumV++].p = RageVector3( (float)iX+g.hshift+g.width, iY+g.vshift+g.height, 0 ); // bottom right
v[iNumV++].p = RageVector3( (float)iX+g.hshift+g.width, iY+g.vshift, 0 ); // top right
/* Advance the cursor. */
iX += g.hadvance;
/* set texture coordinates */
iNumV -= 4;
v[iNumV++].t = RageVector2( g.rect.left, g.rect.top );
v[iNumV++].t = RageVector2( g.rect.left, g.rect.bottom );
v[iNumV++].t = RageVector2( g.rect.right, g.rect.bottom );
v[iNumV++].t = RageVector2( g.rect.right, g.rect.top );
}
iY += m_iLineHeights[i];
}
DISPLAY->SetTexture( pTexture );
DISPLAY->SetTextureModeModulate();
if( m_bBlendAdd )
DISPLAY->SetBlendModeAdd();
@@ -232,9 +240,9 @@ void BitmapText::DrawPrimitives()
RageColor dim(0,0,0,0.5f*m_temp.diffuse[0].a); // semi-transparent black
for( int i=0; i<iNumV; i++ )
v[i].c = dim;
DISPLAY->DrawQuads( v, iNumV );
for( unsigned i=0; i<verts.size(); i++ )
verts[i].c = dim;
DrawChars();
DISPLAY->PopMatrix();
}
@@ -245,27 +253,27 @@ void BitmapText::DrawPrimitives()
if( m_bRainbow )
{
int color_index = int(RageTimer::GetTimeSinceStart() / 0.200) % NUM_RAINBOW_COLORS;
for( int i=0; i<iNumV; i+=4 )
for( unsigned i=0; i<verts.size(); i+=4 )
{
const RageColor color = RAINBOW_COLORS[color_index];
for( int j=i; j<i+4; j++ )
v[j].c = color;
for( unsigned j=i; j<i+4; j++ )
verts[j].c = color;
color_index = (color_index+1)%NUM_RAINBOW_COLORS;
}
}
else
{
for( int i=0; i<iNumV; i+=4 )
for( unsigned i=0; i<verts.size(); i+=4 )
{
v[i+0].c = m_temp.diffuse[0]; // top left
v[i+1].c = m_temp.diffuse[2]; // bottom left
v[i+2].c = m_temp.diffuse[3]; // bottom right
v[i+3].c = m_temp.diffuse[1]; // top right
verts[i+0].c = m_temp.diffuse[0]; // top left
verts[i+1].c = m_temp.diffuse[2]; // bottom left
verts[i+2].c = m_temp.diffuse[3]; // bottom right
verts[i+3].c = m_temp.diffuse[1]; // top right
}
}
DISPLAY->DrawQuads( v, iNumV );
DrawChars();
}
/* render the glow pass */
@@ -273,9 +281,23 @@ void BitmapText::DrawPrimitives()
{
DISPLAY->SetTextureModeGlow();
int i;
for( i=0; i<iNumV; i++ )
v[i].c = m_temp.glow;
DISPLAY->DrawQuads( v, iNumV );
for( unsigned i=0; i<verts.size(); i++ )
verts[i].c = m_temp.glow;
DrawChars();
}
}
/* Rebuild when these change. */
void BitmapText::SetHorizAlign( HorizAlign ha )
{
if(ha == m_HorizAlign) return;
Actor::SetHorizAlign(ha);
BuildChars();
}
void BitmapText::SetVertAlign( VertAlign va )
{
if(va == m_VertAlign) return;
Actor::SetVertAlign(va);
BuildChars();
}
+18
View File
@@ -36,6 +36,9 @@ public:
void TurnRainbowOn() { m_bRainbow = true; };
void TurnRainbowOff() { m_bRainbow = false; };
void SetHorizAlign( HorizAlign ha );
void SetVertAlign( VertAlign va );
public:
CString m_sFontFilePath;
Font* m_pFont;
@@ -50,6 +53,21 @@ protected:
int m_iWidestLineWidth; // in source pixels
bool m_bRainbow;
// struct chr {
// RageVertex v[4];
// RageTexture *t;
// };
vector<RageVertex> verts;
// int num_vertices;
vector<RageTexture *> tex;
// vector<int> vec_cnt;
// vector<one_char> chars;
// static bool sort_chars(const one_char &a, const one_char &b);
void BuildChars();
void DrawChars();
};
+42 -32
View File
@@ -122,6 +122,10 @@ void FontPage::SetTextureCoords(const vector<int> &widths, int LineSpacing)
g.width = float(widths[i]);
g.height = float(m_pTexture->GetSourceFrameHeight());
/* By default, advance one pixel more than the width. (This could be
* an option.) */
g.hadvance = int(g.width + 1);
/* Shift the character up so the top of the rendered quad is at the top
* of the character. */
g.vshift = -(m_pTexture->GetSourceFrameHeight() - LineSpacing)/2.0f;
@@ -131,25 +135,24 @@ void FontPage::SetTextureCoords(const vector<int> &widths, int LineSpacing)
g.hshift = 0;
{
int iPixelsToChopOff = m_pTexture->GetSourceFrameWidth() - widths[i];
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++;
}
float fTexCoordsToChopOff = float(iPixelsToChopOff) / m_pTexture->GetSourceWidth();
g.rect.left += fTexCoordsToChopOff/2;
g.rect.right -= fTexCoordsToChopOff/2;
}
/* By default, advance one pixel more than the width. (This could be
* an option.) */
g.hadvance = g.width + 1;
g.vadvance = float(LineSpacing);
g.vadvance = LineSpacing;
g.Texture = m_pTexture;
glyphs.push_back(g);
}
// force widths to even number
// Why do this? It seems to just artificially widen some characters a little and
// make it look a little worse in 640x480 ...
// for( i=0; i<int(glyphs.size()); i++ )
// if( glyphs[i].width %2 == 1 )
// glyphs[i].width++;
}
void FontPage::SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight)
@@ -159,13 +162,18 @@ void FontPage::SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight)
DrawExtraPixelsRight++;
DrawExtraPixelsLeft++;
if((DrawExtraPixelsLeft % 2) == 1)
DrawExtraPixelsLeft++;
/* Adjust for DrawExtraPixelsLeft and DrawExtraPixelsRight. */
for(unsigned i = 0; i < glyphs.size(); ++i)
{
int iFrameWidth = m_pTexture->GetSourceFrameWidth();
float iCharWidth = glyphs[i].hadvance;
float iCharWidth = glyphs[i].width;
/* Extra pixels to draw to the left and right. */
/* 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. */
float ExtraLeft = min( float(DrawExtraPixelsLeft), (iFrameWidth-iCharWidth)/2.0f );
float ExtraRight = min( float(DrawExtraPixelsRight), (iFrameWidth-iCharWidth)/2.0f );
@@ -174,8 +182,6 @@ void FontPage::SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight)
glyphs[i].rect.right += ExtraRight / m_pTexture->GetSourceWidth();
glyphs[i].hshift -= ExtraLeft;
glyphs[i].width += ExtraLeft + ExtraRight;
glyphs[i].Texture = m_pTexture;
}
}
@@ -186,31 +192,25 @@ FontPage::~FontPage()
}
int Font::GetLineWidthInSourcePixels( const CString &szLine )
int Font::GetLineWidthInSourcePixels( const CString &szLine ) const
{
float LineWidth = 0;
int LineWidth = 0;
for( unsigned i=0; i<szLine.size(); i++ )
{
const glyph &g = GetGlyph(szLine[i]);
LineWidth += g.hadvance;
}
LineWidth += GetGlyph(szLine[i]).hadvance;
return int(LineWidth);
return LineWidth;
}
int Font::GetLineHeightInSourcePixels( const CString &szLine )
int Font::GetLineHeightInSourcePixels( const CString &szLine ) const
{
float iLineSpacing = 0;
int iLineSpacing = 0;
/* The spacing of a line is the spacing of its tallest used font page. */
for( unsigned i=0; i<szLine.size(); i++ )
{
const glyph &g = GetGlyph(szLine[i]);
iLineSpacing = max(iLineSpacing, GetGlyph(szLine[i]).vadvance);
iLineSpacing = max(iLineSpacing, g.vadvance);
}
return int(iLineSpacing);
return iLineSpacing;
}
@@ -248,6 +248,16 @@ const glyph &Font::GetGlyph( int c ) const
return *it->second;
}
RageTexture *Font::GetGlyphTexture( int c )
{
map<int,glyph*>::iterator it = m_iCharToGlyph.find(c);
if(it == m_iCharToGlyph.end())
RageException::Throw( "The font '%s' does not implement the character '%c'", path.GetString(), c );
return it->second->Texture;
}
/* Load font-global settings. */
void Font::LoadINI(IniFile &ini)
{
+3 -4
View File
@@ -18,7 +18,7 @@ struct glyph {
RageTexture *Texture;
/* Number of pixels to advance horizontally after drawing this character. */
float hadvance, vadvance;
int hadvance, vadvance;
/* Size of the actual rendered character. */
float width, height;
@@ -67,8 +67,8 @@ public:
RageTexture *GetGlyphTexture( int c );
const glyph &GetGlyph( int c ) const;
int GetLineWidthInSourcePixels( const CString &szLine );
int GetLineHeightInSourcePixels( const CString &szLine );
int GetLineWidthInSourcePixels( const CString &szLine ) const;
int GetLineHeightInSourcePixels( const CString &szLine ) const;
/* Add a FontPage to this font. */
void AddPage(FontPage *fp);
@@ -78,7 +78,6 @@ public:
private:
vector<FontPage *> pages;
};
#endif
+15 -11
View File
@@ -3,16 +3,15 @@
-----------------------------------------------------------------------------
Class: FontManager
Desc: Interface for loading and releasing textures.
Desc: Interface for loading and releasing fonts.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
Chris Danford
Glenn Maynard
-----------------------------------------------------------------------------
*/
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "FontManager.h"
#include "Font.h"
#include "RageUtil.h"
@@ -23,9 +22,6 @@
FontManager* FONT = NULL;
//-----------------------------------------------------------------------------
// constructor/destructor
//-----------------------------------------------------------------------------
FontManager::FontManager()
{
}
@@ -42,9 +38,6 @@ FontManager::~FontManager()
}
//-----------------------------------------------------------------------------
// Load/Unload textures from disk
//-----------------------------------------------------------------------------
Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
{
sFontOrTextureFilePath.MakeLower();
@@ -143,3 +136,14 @@ void FontManager::UnloadFont( Font *fp )
RageException::Throw( "Unloaded an unknown font (%p)", fp );
}
CString FontManager::GetPageNameFromFileName(const CString &fn)
{
unsigned begin = fn.find_first_of('[');
if(begin == fn.npos) return "main";
unsigned end = fn.find_first_of(']', begin);
if(end == fn.npos) return "main";
begin++; end--;
if(end == begin) return "main";
return fn.substr(begin+1, end-begin+1);
}
+1
View File
@@ -30,6 +30,7 @@ public:
protected:
// map from file name to a texture holder
map<CString, Font*> m_mapPathToFont;
static CString FontManager::GetPageNameFromFileName(const CString &fn);
};
extern FontManager* FONT; // global and accessable from anywhere in our program