123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- //===========================================================================//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- #include "afont.h"
- #include "mclib.h"
- #include <mbstring.h>
- aFont::aFont()
- {
- gosFont = 0;
- fontName[0] = 0;
- resID = 0;
- size = 1;
- }
- long aFont::init( const char* newFontName )
- {
- destroy();
- gosFont = 0;
- strcpy( fontName, newFontName );
- char path[256];
- char* pStr = strstr( fontName, "," );
- if ( pStr )
- {
- size = atoi( pStr + 1 );
- }
- strcpy( path, "assets\\graphics\\" );
- strcat( path, fontName );
- _strlwr( path );
- gosFont = gos_LoadFont( path );
- if ( gosFont )
- return 0;
- return FONT_NOT_LOADED;
- }
- long aFont::init( long resourceID )
- {
- destroy();
- gosFont = 0;
- gosFont = loadFont( resourceID, size );
- resID = resourceID;
- if ( gosFont )
- return 0;
- return FONT_NOT_LOADED;
- }
- aFont::~aFont()
- {
- destroy();
- }
- unsigned long aFont::height( ) const
- {
- unsigned long width, height;
- gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
- gos_TextStringLength(&width,&height,"ABCDE");
- return height;
- }
- void aFont::getSize( unsigned long& width, unsigned long& height, const char* pText )
- {
- gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
- gos_TextStringLength(&width,&height,pText);
- }
- unsigned long aFont::height( const char* st, int areaWidth ) const
- {
- unsigned long width, height;
- gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
- gos_TextSetRegion( 0, 0, areaWidth, Environment.screenHeight );
- gos_TextStringLength(&width,&height,st);
- unsigned long lineCount = 1;
- if ( width > areaWidth - 1 )
- {
- unsigned long curLineWidth = 0;
- gosASSERT( strlen( st) < 2048 );
-
- char pLine[2048]; // should be more than adequate
- char* pLastWord = (char*)st;
- char* pTmp = (char*)st;
- char* pTmpLine = (char*)pLine;
- int numberOfWordsPerLine = 0;
- bool bHasSpaces = true;
- if ( !strstr( st, " " ) )
- {
- if ( !strstr( st, "\n" ) )
- bHasSpaces = false;
- }
- while( *pTmp != NULL )
- {
- if ( *pTmp == '\n' )
- {
- lineCount++;
- numberOfWordsPerLine = 0;
- curLineWidth = 0;
- pTmpLine = pLine;
- pLastWord = pTmp+1;
- }
- else if ( !bHasSpaces )
- {
- if ( pTmp > st )
- {
- char tmp = *(pTmp-1);
- if ( !isleadbyte( tmp ) )
- {
- *(pTmpLine) = NULL;
- gos_TextStringLength( &curLineWidth, &height, pLine );
- if ( curLineWidth > areaWidth )
- {
- lineCount++;
- pTmp--;
- pTmpLine = pLine;
- curLineWidth = 0;
- numberOfWordsPerLine = 0;
- }
- }
-
-
- }
- }
- else if ( isspace( *pTmp ) )
- {
- *(pTmpLine) = NULL;
- gos_TextStringLength( &curLineWidth, &height, pLine );
- if ( curLineWidth > areaWidth )
- {
- gos_TextStringLength( &curLineWidth, &height, pLastWord );
- if ( numberOfWordsPerLine == 0 || curLineWidth > areaWidth )
- {
- static bool firstTime = true;
- if (firstTime)
- {
- Assert( true, 0, "this list box item contains a word of greater "
- " area than the list box, giving up" );
- firstTime = false;
- }
- /*There are times when you just can't guarantee that this won't occur,
- so we have to just continue and deal with it.*/
- //return height;
- pLastWord = pTmp;
- }
- lineCount++;
- pTmpLine = pLine;
- pTmp = pLastWord - 1;
- curLineWidth = 0;
- numberOfWordsPerLine = 0;
- }
-
- pLastWord = pTmp;
- numberOfWordsPerLine++;
- }
- *pTmpLine = *pTmp;
- if ( isleadbyte( *pTmpLine ) )
- {
- *(pTmpLine+1) = *(pTmp+1);
- }
- pTmpLine = (char*)_mbsinc( (unsigned char*)pTmpLine );
- pTmp = (char*)_mbsinc( (unsigned char*)pTmp );
- }
- // one last check
- *pTmpLine = NULL;
- gos_TextStringLength( &curLineWidth, &height, pLine );
- if ( curLineWidth > areaWidth )
- {
- lineCount++;
- }
- if ( *pTmp == NULL )
- lineCount++;
- }
- gos_TextStringLength( &width, &height, "A" );
- return (height) * lineCount ;
- }
- unsigned long aFont::width( const char* st ) const
- {
- unsigned long width, height;
- gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
- gos_TextStringLength(&width,&height,st);
- return width;
- }
- long aFont::load( const char* fontName )
- {
- destroy();
- return init( fontName );
- }
- void aFont::destroy()
- {
- if ( gosFont )
- gos_DeleteFont( gosFont );
- gosFont = 0;
- resID = 0;
- fontName[0] = 0;
- }
- void aFont::render( const char* text, int xPos, int yPos, int areaWidth, int areaHeight,
- unsigned long color, bool bBold, int alignment )
- {
- gos_TextSetAttributes( gosFont, color, size, true, true, bBold, false, alignment );
-
- if ( areaWidth < 1 )
- {
- if ( alignment == 1 )
- {
- unsigned long width, height;
- gos_TextStringLength( &width, &height, text );
- xPos -= width;
- areaWidth = width + 1;
- }
- else
- areaWidth = Environment.screenWidth;
- }
- if ( areaHeight < 1 )
- {
- if ( alignment == 3 ) // bottom
- {
- unsigned long width, height;
- gos_TextStringLength( &width, &height, text );
- yPos -= height;
- areaHeight = height + 1;
- }
-
- else
- areaHeight = Environment.screenHeight;
- }
- gos_TextSetRegion( xPos, yPos, xPos + areaWidth, yPos + areaHeight );
-
- gos_TextSetPosition( xPos, yPos );
- gos_TextDraw( text );
- }
- HGOSFONT3D aFont::loadFont( long resourceID, long& size )
- {
- size = 1;
- char buffer[256];
- cLoadString( resourceID, buffer, 255 );
- char* pStr = strstr( buffer, "," );
- if ( pStr )
- {
- size = -atoi( pStr + 1 );
- *pStr = NULL;
- }
- char path[256];
- strcpy( path, "assets\\graphics\\" );
- strcat( path, buffer );
- _strlwr( path );
- HGOSFONT3D retFont = gos_LoadFont( path );
- return retFont;
- }
- aFont::aFont( const aFont& src )
- {
- copyData(src);
- }
- aFont& aFont::operator=( const aFont& src )
- {
- copyData(src);
- return *this;
- }
- void aFont::copyData( const aFont& src )
- {
- if ( &src != this )
- {
- if (src.resID )
- {
- init( src.resID );
- }
- else if ( src.fontName[0] != 0 )
- {
- init( src.fontName );
- }
- size = src.size;
- }
- }
|