2023-10-03 20:37:00 +02:00
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
# include "CGUIFont.h"
# include "os.h"
# include "coreutil.h"
# include "IGUIEnvironment.h"
# include "IReadFile.h"
# include "IVideoDriver.h"
# include "IGUISpriteBank.h"
namespace irr
{
namespace gui
{
//! constructor
2024-03-20 19:35:52 +01:00
CGUIFont : : CGUIFont ( IGUIEnvironment * env , const io : : path & filename ) :
Driver ( 0 ) , SpriteBank ( 0 ) , Environment ( env ) , WrongCharacter ( 0 ) ,
MaxHeight ( 0 ) , GlobalKerningWidth ( 0 ) , GlobalKerningHeight ( 0 )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
# ifdef _DEBUG
2023-10-03 20:37:00 +02:00
setDebugName ( " CGUIFont " ) ;
2024-03-20 19:35:52 +01:00
# endif
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( Environment ) {
2023-10-03 20:37:00 +02:00
// don't grab environment, to avoid circular references
Driver = Environment - > getVideoDriver ( ) ;
SpriteBank = Environment - > getSpriteBank ( filename ) ;
2024-03-20 19:35:52 +01:00
if ( ! SpriteBank ) // could be default-font which has no file
2023-10-03 20:37:00 +02:00
SpriteBank = Environment - > addEmptySpriteBank ( filename ) ;
if ( SpriteBank )
SpriteBank - > grab ( ) ;
}
if ( Driver )
Driver - > grab ( ) ;
2024-03-20 19:35:52 +01:00
setInvisibleCharacters ( L " " ) ;
2023-10-03 20:37:00 +02:00
}
//! destructor
CGUIFont : : ~ CGUIFont ( )
{
if ( Driver )
Driver - > drop ( ) ;
2024-03-20 19:35:52 +01:00
if ( SpriteBank ) {
2023-10-03 20:37:00 +02:00
SpriteBank - > drop ( ) ;
// TODO: spritebank still exists in gui-environment and should be removed here when it's
// reference-count is 1. Just can't do that from here at the moment.
// But spritebank would not be able to drop textures anyway because those are in texture-cache
// where they can't be removed unless materials start reference-couting 'em.
}
}
#if 0
//! loads a font file from xml
bool CGUIFont : : load ( io : : IXMLReader * xml , const io : : path & directory )
{
if ( ! SpriteBank )
return false ;
SpriteBank - > clear ( ) ;
while ( xml - > read ( ) )
{
if ( io : : EXN_ELEMENT = = xml - > getNodeType ( ) )
{
if ( core : : stringw ( L " Texture " ) = = xml - > getNodeName ( ) )
{
// add a texture
core : : stringc fn = xml - > getAttributeValue ( L " filename " ) ;
u32 i = ( u32 ) xml - > getAttributeValueAsInt ( L " index " ) ;
core : : stringw alpha = xml - > getAttributeValue ( L " hasAlpha " ) ;
while ( i + 1 > SpriteBank - > getTextureCount ( ) )
SpriteBank - > addTexture ( 0 ) ;
bool flags [ 3 ] ;
pushTextureCreationFlags ( flags ) ;
// load texture
io : : path textureFullName = core : : mergeFilename ( directory , fn ) ;
SpriteBank - > setTexture ( i , Driver - > getTexture ( textureFullName ) ) ;
popTextureCreationFlags ( flags ) ;
// couldn't load texture, abort.
if ( ! SpriteBank - > getTexture ( i ) )
{
os : : Printer : : log ( " Unable to load all textures in the font, aborting " , ELL_ERROR ) ;
return false ;
}
else
{
// colorkey texture rather than alpha channel?
if ( alpha = = core : : stringw ( " false " ) )
Driver - > makeColorKeyTexture ( SpriteBank - > getTexture ( i ) , core : : position2di ( 0 , 0 ) ) ;
}
}
else if ( core : : stringw ( L " c " ) = = xml - > getNodeName ( ) )
{
// adding a character to this font
SFontArea a ;
SGUISpriteFrame f ;
SGUISprite s ;
core : : rect < s32 > rectangle ;
a . underhang = xml - > getAttributeValueAsInt ( L " u " ) ;
a . overhang = xml - > getAttributeValueAsInt ( L " o " ) ;
a . spriteno = SpriteBank - > getSprites ( ) . size ( ) ;
s32 texno = xml - > getAttributeValueAsInt ( L " i " ) ;
// parse rectangle
core : : stringc rectstr = xml - > getAttributeValue ( L " r " ) ;
wchar_t ch = xml - > getAttributeValue ( L " c " ) [ 0 ] ;
const c8 * c = rectstr . c_str ( ) ;
s32 val ;
val = 0 ;
while ( * c > = ' 0 ' & & * c < = ' 9 ' )
{
val * = 10 ;
val + = * c - ' 0 ' ;
c + + ;
}
rectangle . UpperLeftCorner . X = val ;
while ( * c = = L ' ' | | * c = = L ' , ' ) c + + ;
val = 0 ;
while ( * c > = ' 0 ' & & * c < = ' 9 ' )
{
val * = 10 ;
val + = * c - ' 0 ' ;
c + + ;
}
rectangle . UpperLeftCorner . Y = val ;
while ( * c = = L ' ' | | * c = = L ' , ' ) c + + ;
val = 0 ;
while ( * c > = ' 0 ' & & * c < = ' 9 ' )
{
val * = 10 ;
val + = * c - ' 0 ' ;
c + + ;
}
rectangle . LowerRightCorner . X = val ;
while ( * c = = L ' ' | | * c = = L ' , ' ) c + + ;
val = 0 ;
while ( * c > = ' 0 ' & & * c < = ' 9 ' )
{
val * = 10 ;
val + = * c - ' 0 ' ;
c + + ;
}
rectangle . LowerRightCorner . Y = val ;
CharacterMap . emplace ( ch , Areas . size ( ) ) ;
// make frame
f . rectNumber = SpriteBank - > getPositions ( ) . size ( ) ;
f . textureNumber = texno ;
// add frame to sprite
s . Frames . push_back ( f ) ;
s . frameTime = 0 ;
// add rectangle to sprite bank
SpriteBank - > getPositions ( ) . push_back ( rectangle ) ;
a . width = rectangle . getWidth ( ) ;
// add sprite to sprite bank
SpriteBank - > getSprites ( ) . push_back ( s ) ;
// add character to font
Areas . push_back ( a ) ;
}
}
}
// set bad character
WrongCharacter = getAreaFromCharacter ( L ' ' ) ;
setMaxHeight ( ) ;
return true ;
}
# endif
void CGUIFont : : setMaxHeight ( )
{
2024-03-20 19:35:52 +01:00
if ( ! SpriteBank )
2023-10-03 20:37:00 +02:00
return ;
MaxHeight = 0 ;
2024-03-20 19:35:52 +01:00
core : : array < core : : rect < s32 > > & p = SpriteBank - > getPositions ( ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
for ( u32 i = 0 ; i < p . size ( ) ; + + i ) {
2023-10-03 20:37:00 +02:00
const s32 t = p [ i ] . getHeight ( ) ;
2024-03-20 19:35:52 +01:00
if ( t > MaxHeight )
2023-10-03 20:37:00 +02:00
MaxHeight = t ;
}
}
2024-03-20 19:35:52 +01:00
void CGUIFont : : pushTextureCreationFlags ( bool ( & flags ) [ 3 ] )
2023-10-03 20:37:00 +02:00
{
flags [ 0 ] = Driver - > getTextureCreationFlag ( video : : ETCF_ALLOW_NON_POWER_2 ) ;
flags [ 1 ] = Driver - > getTextureCreationFlag ( video : : ETCF_CREATE_MIP_MAPS ) ;
flags [ 2 ] = Driver - > getTextureCreationFlag ( video : : ETCF_ALLOW_MEMORY_COPY ) ;
Driver - > setTextureCreationFlag ( video : : ETCF_ALLOW_NON_POWER_2 , true ) ;
Driver - > setTextureCreationFlag ( video : : ETCF_CREATE_MIP_MAPS , false ) ;
Driver - > setTextureCreationFlag ( video : : ETCF_ALLOW_MEMORY_COPY , true ) ;
}
2024-03-20 19:35:52 +01:00
void CGUIFont : : popTextureCreationFlags ( const bool ( & flags ) [ 3 ] )
2023-10-03 20:37:00 +02:00
{
Driver - > setTextureCreationFlag ( video : : ETCF_ALLOW_NON_POWER_2 , flags [ 0 ] ) ;
Driver - > setTextureCreationFlag ( video : : ETCF_CREATE_MIP_MAPS , flags [ 1 ] ) ;
Driver - > setTextureCreationFlag ( video : : ETCF_ALLOW_MEMORY_COPY , flags [ 2 ] ) ;
}
//! loads a font file, native file needed, for texture parsing
2024-03-20 19:35:52 +01:00
bool CGUIFont : : load ( io : : IReadFile * file )
2023-10-03 20:37:00 +02:00
{
if ( ! Driver )
return false ;
return loadTexture ( Driver - > createImageFromFile ( file ) ,
2024-03-20 19:35:52 +01:00
file - > getFileName ( ) ) ;
2023-10-03 20:37:00 +02:00
}
//! loads a font file, native file needed, for texture parsing
2024-03-20 19:35:52 +01:00
bool CGUIFont : : load ( const io : : path & filename )
2023-10-03 20:37:00 +02:00
{
if ( ! Driver )
return false ;
2024-03-20 19:35:52 +01:00
return loadTexture ( Driver - > createImageFromFile ( filename ) ,
filename ) ;
2023-10-03 20:37:00 +02:00
}
//! load & prepare font from ITexture
2024-03-20 19:35:52 +01:00
bool CGUIFont : : loadTexture ( video : : IImage * image , const io : : path & name )
2023-10-03 20:37:00 +02:00
{
if ( ! image | | ! SpriteBank )
return false ;
s32 lowerRightPositions = 0 ;
2024-03-20 19:35:52 +01:00
video : : IImage * tmpImage = image ;
bool deleteTmpImage = false ;
switch ( image - > getColorFormat ( ) ) {
2023-10-03 20:37:00 +02:00
case video : : ECF_R5G6B5 :
2024-03-20 19:35:52 +01:00
tmpImage = Driver - > createImage ( video : : ECF_A1R5G5B5 , image - > getDimension ( ) ) ;
2023-10-03 20:37:00 +02:00
image - > copyTo ( tmpImage ) ;
2024-03-20 19:35:52 +01:00
deleteTmpImage = true ;
2023-10-03 20:37:00 +02:00
break ;
case video : : ECF_A1R5G5B5 :
case video : : ECF_A8R8G8B8 :
break ;
case video : : ECF_R8G8B8 :
2024-03-20 19:35:52 +01:00
tmpImage = Driver - > createImage ( video : : ECF_A8R8G8B8 , image - > getDimension ( ) ) ;
2023-10-03 20:37:00 +02:00
image - > copyTo ( tmpImage ) ;
2024-03-20 19:35:52 +01:00
deleteTmpImage = true ;
2023-10-03 20:37:00 +02:00
break ;
default :
os : : Printer : : log ( " Unknown texture format provided for CGUIFont::loadTexture " , ELL_ERROR ) ;
return false ;
}
readPositions ( tmpImage , lowerRightPositions ) ;
WrongCharacter = getAreaFromCharacter ( L ' ' ) ;
// output warnings
if ( ! lowerRightPositions | | ! SpriteBank - > getSprites ( ) . size ( ) )
os : : Printer : : log ( " Either no upper or lower corner pixels in the font file. If this font was made using the new font tool, please load the XML file instead. If not, the font may be corrupted. " , ELL_ERROR ) ;
2024-03-20 19:35:52 +01:00
else if ( lowerRightPositions ! = ( s32 ) SpriteBank - > getPositions ( ) . size ( ) )
2023-10-03 20:37:00 +02:00
os : : Printer : : log ( " The amount of upper corner pixels and the lower corner pixels is not equal, font file may be corrupted. " , ELL_ERROR ) ;
2024-03-20 19:35:52 +01:00
bool ret = ( ! SpriteBank - > getSprites ( ) . empty ( ) & & lowerRightPositions ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( ret ) {
2023-10-03 20:37:00 +02:00
bool flags [ 3 ] ;
pushTextureCreationFlags ( flags ) ;
SpriteBank - > addTexture ( Driver - > addTexture ( name , tmpImage ) ) ;
popTextureCreationFlags ( flags ) ;
}
if ( deleteTmpImage )
tmpImage - > drop ( ) ;
image - > drop ( ) ;
setMaxHeight ( ) ;
return ret ;
}
2024-03-20 19:35:52 +01:00
void CGUIFont : : readPositions ( video : : IImage * image , s32 & lowerRightPositions )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
if ( ! SpriteBank )
2023-10-03 20:37:00 +02:00
return ;
const core : : dimension2d < u32 > size = image - > getDimension ( ) ;
2024-03-20 19:35:52 +01:00
video : : SColor colorTopLeft = image - > getPixel ( 0 , 0 ) ;
2023-10-03 20:37:00 +02:00
colorTopLeft . setAlpha ( 255 ) ;
2024-03-20 19:35:52 +01:00
image - > setPixel ( 0 , 0 , colorTopLeft ) ;
video : : SColor colorLowerRight = image - > getPixel ( 1 , 0 ) ;
video : : SColor colorBackGround = image - > getPixel ( 2 , 0 ) ;
2023-10-03 20:37:00 +02:00
video : : SColor colorBackGroundTransparent = 0 ;
2024-03-20 19:35:52 +01:00
image - > setPixel ( 1 , 0 , colorBackGround ) ;
2023-10-03 20:37:00 +02:00
// start parsing
2024-03-20 19:35:52 +01:00
core : : position2d < s32 > pos ( 0 , 0 ) ;
for ( pos . Y = 0 ; pos . Y < ( s32 ) size . Height ; + + pos . Y ) {
for ( pos . X = 0 ; pos . X < ( s32 ) size . Width ; + + pos . X ) {
2023-10-03 20:37:00 +02:00
const video : : SColor c = image - > getPixel ( pos . X , pos . Y ) ;
2024-03-20 19:35:52 +01:00
if ( c = = colorTopLeft ) {
2023-10-03 20:37:00 +02:00
image - > setPixel ( pos . X , pos . Y , colorBackGroundTransparent ) ;
SpriteBank - > getPositions ( ) . push_back ( core : : rect < s32 > ( pos , pos ) ) ;
2024-03-20 19:35:52 +01:00
} else if ( c = = colorLowerRight ) {
2023-10-03 20:37:00 +02:00
// too many lower right points
2024-03-20 19:35:52 +01:00
if ( SpriteBank - > getPositions ( ) . size ( ) < = ( u32 ) lowerRightPositions ) {
2023-10-03 20:37:00 +02:00
lowerRightPositions = 0 ;
return ;
}
image - > setPixel ( pos . X , pos . Y , colorBackGroundTransparent ) ;
SpriteBank - > getPositions ( ) [ lowerRightPositions ] . LowerRightCorner = pos ;
// add frame to sprite bank
SGUISpriteFrame f ;
f . rectNumber = lowerRightPositions ;
f . textureNumber = 0 ;
SGUISprite s ;
s . Frames . push_back ( f ) ;
s . frameTime = 0 ;
SpriteBank - > getSprites ( ) . push_back ( s ) ;
// add character to font
SFontArea a ;
a . overhang = 0 ;
a . underhang = 0 ;
a . spriteno = lowerRightPositions ;
a . width = SpriteBank - > getPositions ( ) [ lowerRightPositions ] . getWidth ( ) ;
Areas . push_back ( a ) ;
// map letter to character
wchar_t ch = ( wchar_t ) ( lowerRightPositions + 32 ) ;
CharacterMap [ ch ] = lowerRightPositions ;
+ + lowerRightPositions ;
2024-03-20 19:35:52 +01:00
} else if ( c = = colorBackGround )
2023-10-03 20:37:00 +02:00
image - > setPixel ( pos . X , pos . Y , colorBackGroundTransparent ) ;
}
}
}
//! set an Pixel Offset on Drawing ( scale position on width )
void CGUIFont : : setKerningWidth ( s32 kerning )
{
GlobalKerningWidth = kerning ;
}
//! set an Pixel Offset on Drawing ( scale position on width )
2024-03-20 19:35:52 +01:00
s32 CGUIFont : : getKerningWidth ( const wchar_t * thisLetter , const wchar_t * previousLetter ) const
2023-10-03 20:37:00 +02:00
{
s32 ret = GlobalKerningWidth ;
2024-03-20 19:35:52 +01:00
if ( thisLetter ) {
2023-10-03 20:37:00 +02:00
ret + = Areas [ getAreaFromCharacter ( * thisLetter ) ] . overhang ;
2024-03-20 19:35:52 +01:00
if ( previousLetter ) {
2023-10-03 20:37:00 +02:00
ret + = Areas [ getAreaFromCharacter ( * previousLetter ) ] . underhang ;
}
}
return ret ;
}
//! set an Pixel Offset on Drawing ( scale position on height )
void CGUIFont : : setKerningHeight ( s32 kerning )
{
GlobalKerningHeight = kerning ;
}
//! set an Pixel Offset on Drawing ( scale position on height )
2024-03-20 19:35:52 +01:00
s32 CGUIFont : : getKerningHeight ( ) const
2023-10-03 20:37:00 +02:00
{
return GlobalKerningHeight ;
}
//! returns the sprite number from a given character
u32 CGUIFont : : getSpriteNoFromChar ( const wchar_t * c ) const
{
return Areas [ getAreaFromCharacter ( * c ) ] . spriteno ;
}
s32 CGUIFont : : getAreaFromCharacter ( const wchar_t c ) const
{
auto n = CharacterMap . find ( c ) ;
if ( n ! = CharacterMap . end ( ) )
return n - > second ;
else
return WrongCharacter ;
}
2024-03-20 19:35:52 +01:00
void CGUIFont : : setInvisibleCharacters ( const wchar_t * s )
2023-10-03 20:37:00 +02:00
{
Invisible = s ;
}
//! returns the dimension of text
2024-03-20 19:35:52 +01:00
core : : dimension2d < u32 > CGUIFont : : getDimension ( const wchar_t * text ) const
2023-10-03 20:37:00 +02:00
{
core : : dimension2d < u32 > dim ( 0 , 0 ) ;
core : : dimension2d < u32 > thisLine ( 0 , MaxHeight ) ;
2024-03-20 19:35:52 +01:00
for ( const wchar_t * p = text ; * p ; + + p ) {
bool lineBreak = false ;
2023-10-04 20:10:58 +02:00
if ( * p = = L ' \r ' ) { // Mac or Windows breaks
2023-10-03 20:37:00 +02:00
lineBreak = true ;
if ( p [ 1 ] = = L ' \n ' ) // Windows breaks
+ + p ;
2023-10-04 20:10:58 +02:00
} else if ( * p = = L ' \n ' ) { // Unix breaks
2023-10-03 20:37:00 +02:00
lineBreak = true ;
}
2024-03-20 19:35:52 +01:00
if ( lineBreak ) {
2023-10-03 20:37:00 +02:00
dim . Height + = thisLine . Height ;
if ( dim . Width < thisLine . Width )
dim . Width = thisLine . Width ;
thisLine . Width = 0 ;
continue ;
}
const SFontArea & area = Areas [ getAreaFromCharacter ( * p ) ] ;
thisLine . Width + = area . underhang ;
thisLine . Width + = area . width + area . overhang + GlobalKerningWidth ;
}
dim . Height + = thisLine . Height ;
if ( dim . Width < thisLine . Width )
dim . Width = thisLine . Width ;
return dim ;
}
//! draws some text and clips it to the specified rectangle if wanted
2024-03-20 19:35:52 +01:00
void CGUIFont : : draw ( const core : : stringw & text , const core : : rect < s32 > & position ,
video : : SColor color ,
bool hcenter , bool vcenter , const core : : rect < s32 > * clip )
2023-10-03 20:37:00 +02:00
{
if ( ! Driver | | ! SpriteBank )
return ;
2024-03-20 19:35:52 +01:00
core : : dimension2d < s32 > textDimension ; // NOTE: don't make this u32 or the >> later on can fail when the dimension width is < position width
2023-10-03 20:37:00 +02:00
core : : position2d < s32 > offset = position . UpperLeftCorner ;
if ( hcenter | | vcenter | | clip )
textDimension = getDimension ( text . c_str ( ) ) ;
if ( hcenter )
offset . X + = ( position . getWidth ( ) - textDimension . Width ) > > 1 ;
if ( vcenter )
offset . Y + = ( position . getHeight ( ) - textDimension . Height ) > > 1 ;
2024-03-20 19:35:52 +01:00
if ( clip ) {
2023-10-03 20:37:00 +02:00
core : : rect < s32 > clippedRect ( offset , textDimension ) ;
clippedRect . clipAgainst ( * clip ) ;
if ( ! clippedRect . isValid ( ) )
return ;
}
core : : array < u32 > indices ( text . size ( ) ) ;
core : : array < core : : position2di > offsets ( text . size ( ) ) ;
2024-03-20 19:35:52 +01:00
for ( u32 i = 0 ; i < text . size ( ) ; i + + ) {
2023-10-03 20:37:00 +02:00
wchar_t c = text [ i ] ;
2024-03-20 19:35:52 +01:00
bool lineBreak = false ;
2023-10-04 20:10:58 +02:00
if ( c = = L ' \r ' ) { // Mac or Windows breaks
2023-10-03 20:37:00 +02:00
lineBreak = true ;
2024-03-20 19:35:52 +01:00
if ( text [ i + 1 ] = = L ' \n ' ) // Windows breaks
2023-10-03 20:37:00 +02:00
c = text [ + + i ] ;
2023-10-04 20:10:58 +02:00
} else if ( c = = L ' \n ' ) { // Unix breaks
2023-10-03 20:37:00 +02:00
lineBreak = true ;
}
2024-03-20 19:35:52 +01:00
if ( lineBreak ) {
2023-10-03 20:37:00 +02:00
offset . Y + = MaxHeight ;
offset . X = position . UpperLeftCorner . X ;
2024-03-20 19:35:52 +01:00
if ( hcenter ) {
2023-10-03 20:37:00 +02:00
offset . X + = ( position . getWidth ( ) - textDimension . Width ) > > 1 ;
}
continue ;
}
2024-03-20 19:35:52 +01:00
SFontArea & area = Areas [ getAreaFromCharacter ( c ) ] ;
2023-10-03 20:37:00 +02:00
offset . X + = area . underhang ;
2024-03-20 19:35:52 +01:00
if ( Invisible . findFirst ( c ) < 0 ) {
2023-10-03 20:37:00 +02:00
indices . push_back ( area . spriteno ) ;
offsets . push_back ( offset ) ;
}
offset . X + = area . width + area . overhang + GlobalKerningWidth ;
}
SpriteBank - > draw2DSpriteBatch ( indices , offsets , clip , color ) ;
}
//! Calculates the index of the character in the text which is on a specific position.
2024-03-20 19:35:52 +01:00
s32 CGUIFont : : getCharacterFromPos ( const wchar_t * text , s32 pixel_x ) const
2023-10-03 20:37:00 +02:00
{
s32 x = 0 ;
s32 idx = 0 ;
2024-03-20 19:35:52 +01:00
while ( text [ idx ] ) {
const SFontArea & a = Areas [ getAreaFromCharacter ( text [ idx ] ) ] ;
2023-10-03 20:37:00 +02:00
x + = a . width + a . overhang + a . underhang + GlobalKerningWidth ;
if ( x > = pixel_x )
return idx ;
+ + idx ;
}
return - 1 ;
}
2024-03-20 19:35:52 +01:00
IGUISpriteBank * CGUIFont : : getSpriteBank ( ) const
2023-10-03 20:37:00 +02:00
{
return SpriteBank ;
}
} // end namespace gui
} // end namespace irr