AFONT.CPP 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "afont.h"
  5. #include "mclib.h"
  6. #include <mbstring.h>
  7. aFont::aFont()
  8. {
  9. gosFont = 0;
  10. fontName[0] = 0;
  11. resID = 0;
  12. size = 1;
  13. }
  14. long aFont::init( const char* newFontName )
  15. {
  16. destroy();
  17. gosFont = 0;
  18. strcpy( fontName, newFontName );
  19. char path[256];
  20. char* pStr = strstr( fontName, "," );
  21. if ( pStr )
  22. {
  23. size = atoi( pStr + 1 );
  24. }
  25. strcpy( path, "assets\\graphics\\" );
  26. strcat( path, fontName );
  27. _strlwr( path );
  28. gosFont = gos_LoadFont( path );
  29. if ( gosFont )
  30. return 0;
  31. return FONT_NOT_LOADED;
  32. }
  33. long aFont::init( long resourceID )
  34. {
  35. destroy();
  36. gosFont = 0;
  37. gosFont = loadFont( resourceID, size );
  38. resID = resourceID;
  39. if ( gosFont )
  40. return 0;
  41. return FONT_NOT_LOADED;
  42. }
  43. aFont::~aFont()
  44. {
  45. destroy();
  46. }
  47. unsigned long aFont::height( ) const
  48. {
  49. unsigned long width, height;
  50. gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
  51. gos_TextStringLength(&width,&height,"ABCDE");
  52. return height;
  53. }
  54. void aFont::getSize( unsigned long& width, unsigned long& height, const char* pText )
  55. {
  56. gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
  57. gos_TextStringLength(&width,&height,pText);
  58. }
  59. unsigned long aFont::height( const char* st, int areaWidth ) const
  60. {
  61. unsigned long width, height;
  62. gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
  63. gos_TextSetRegion( 0, 0, areaWidth, Environment.screenHeight );
  64. gos_TextStringLength(&width,&height,st);
  65. unsigned long lineCount = 1;
  66. if ( width > areaWidth - 1 )
  67. {
  68. unsigned long curLineWidth = 0;
  69. gosASSERT( strlen( st) < 2048 );
  70. char pLine[2048]; // should be more than adequate
  71. char* pLastWord = (char*)st;
  72. char* pTmp = (char*)st;
  73. char* pTmpLine = (char*)pLine;
  74. int numberOfWordsPerLine = 0;
  75. bool bHasSpaces = true;
  76. if ( !strstr( st, " " ) )
  77. {
  78. if ( !strstr( st, "\n" ) )
  79. bHasSpaces = false;
  80. }
  81. while( *pTmp != NULL )
  82. {
  83. if ( *pTmp == '\n' )
  84. {
  85. lineCount++;
  86. numberOfWordsPerLine = 0;
  87. curLineWidth = 0;
  88. pTmpLine = pLine;
  89. pLastWord = pTmp+1;
  90. }
  91. else if ( !bHasSpaces )
  92. {
  93. if ( pTmp > st )
  94. {
  95. char tmp = *(pTmp-1);
  96. if ( !isleadbyte( tmp ) )
  97. {
  98. *(pTmpLine) = NULL;
  99. gos_TextStringLength( &curLineWidth, &height, pLine );
  100. if ( curLineWidth > areaWidth )
  101. {
  102. lineCount++;
  103. pTmp--;
  104. pTmpLine = pLine;
  105. curLineWidth = 0;
  106. numberOfWordsPerLine = 0;
  107. }
  108. }
  109. }
  110. }
  111. else if ( isspace( *pTmp ) )
  112. {
  113. *(pTmpLine) = NULL;
  114. gos_TextStringLength( &curLineWidth, &height, pLine );
  115. if ( curLineWidth > areaWidth )
  116. {
  117. gos_TextStringLength( &curLineWidth, &height, pLastWord );
  118. if ( numberOfWordsPerLine == 0 || curLineWidth > areaWidth )
  119. {
  120. static bool firstTime = true;
  121. if (firstTime)
  122. {
  123. Assert( true, 0, "this list box item contains a word of greater "
  124. " area than the list box, giving up" );
  125. firstTime = false;
  126. }
  127. /*There are times when you just can't guarantee that this won't occur,
  128. so we have to just continue and deal with it.*/
  129. //return height;
  130. pLastWord = pTmp;
  131. }
  132. lineCount++;
  133. pTmpLine = pLine;
  134. pTmp = pLastWord - 1;
  135. curLineWidth = 0;
  136. numberOfWordsPerLine = 0;
  137. }
  138. pLastWord = pTmp;
  139. numberOfWordsPerLine++;
  140. }
  141. *pTmpLine = *pTmp;
  142. if ( isleadbyte( *pTmpLine ) )
  143. {
  144. *(pTmpLine+1) = *(pTmp+1);
  145. }
  146. pTmpLine = (char*)_mbsinc( (unsigned char*)pTmpLine );
  147. pTmp = (char*)_mbsinc( (unsigned char*)pTmp );
  148. }
  149. // one last check
  150. *pTmpLine = NULL;
  151. gos_TextStringLength( &curLineWidth, &height, pLine );
  152. if ( curLineWidth > areaWidth )
  153. {
  154. lineCount++;
  155. }
  156. if ( *pTmp == NULL )
  157. lineCount++;
  158. }
  159. gos_TextStringLength( &width, &height, "A" );
  160. return (height) * lineCount ;
  161. }
  162. unsigned long aFont::width( const char* st ) const
  163. {
  164. unsigned long width, height;
  165. gos_TextSetAttributes (gosFont, 0, size, false, true, false, false);
  166. gos_TextStringLength(&width,&height,st);
  167. return width;
  168. }
  169. long aFont::load( const char* fontName )
  170. {
  171. destroy();
  172. return init( fontName );
  173. }
  174. void aFont::destroy()
  175. {
  176. if ( gosFont )
  177. gos_DeleteFont( gosFont );
  178. gosFont = 0;
  179. resID = 0;
  180. fontName[0] = 0;
  181. }
  182. void aFont::render( const char* text, int xPos, int yPos, int areaWidth, int areaHeight,
  183. unsigned long color, bool bBold, int alignment )
  184. {
  185. gos_TextSetAttributes( gosFont, color, size, true, true, bBold, false, alignment );
  186. if ( areaWidth < 1 )
  187. {
  188. if ( alignment == 1 )
  189. {
  190. unsigned long width, height;
  191. gos_TextStringLength( &width, &height, text );
  192. xPos -= width;
  193. areaWidth = width + 1;
  194. }
  195. else
  196. areaWidth = Environment.screenWidth;
  197. }
  198. if ( areaHeight < 1 )
  199. {
  200. if ( alignment == 3 ) // bottom
  201. {
  202. unsigned long width, height;
  203. gos_TextStringLength( &width, &height, text );
  204. yPos -= height;
  205. areaHeight = height + 1;
  206. }
  207. else
  208. areaHeight = Environment.screenHeight;
  209. }
  210. gos_TextSetRegion( xPos, yPos, xPos + areaWidth, yPos + areaHeight );
  211. gos_TextSetPosition( xPos, yPos );
  212. gos_TextDraw( text );
  213. }
  214. HGOSFONT3D aFont::loadFont( long resourceID, long& size )
  215. {
  216. size = 1;
  217. char buffer[256];
  218. cLoadString( resourceID, buffer, 255 );
  219. char* pStr = strstr( buffer, "," );
  220. if ( pStr )
  221. {
  222. size = -atoi( pStr + 1 );
  223. *pStr = NULL;
  224. }
  225. char path[256];
  226. strcpy( path, "assets\\graphics\\" );
  227. strcat( path, buffer );
  228. _strlwr( path );
  229. HGOSFONT3D retFont = gos_LoadFont( path );
  230. return retFont;
  231. }
  232. aFont::aFont( const aFont& src )
  233. {
  234. copyData(src);
  235. }
  236. aFont& aFont::operator=( const aFont& src )
  237. {
  238. copyData(src);
  239. return *this;
  240. }
  241. void aFont::copyData( const aFont& src )
  242. {
  243. if ( &src != this )
  244. {
  245. if (src.resID )
  246. {
  247. init( src.resID );
  248. }
  249. else if ( src.fontName[0] != 0 )
  250. {
  251. init( src.fontName );
  252. }
  253. size = src.size;
  254. }
  255. }