font_manager.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. Copyright (C) 2004-2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /*
  16. * font_manager.c: Font management.
  17. *
  18. */
  19. #include "../wolfiphone.h"
  20. /*
  21. ! " # $ % & ' ( ) * + , - . /
  22. 0 1 2 3 4 5 6 7 8 9 : ; ( = ) ?
  23. @ A B C D E F G H I J K L M N O
  24. P Q R S T U V W X Y Z [ / ] ^ -
  25. ` a b c d e f g h i j k l m n o
  26. p q r s t u v w x y z { | } ~
  27. */
  28. #define MAX_FONTS 4
  29. font_t *myfonts[ MAX_FONTS ];
  30. PRIVATE W32 num_fonts = 0;
  31. typedef struct
  32. {
  33. char *start, *end;
  34. } string_seg_t;
  35. font_t *createFont( const char *filename )
  36. {
  37. font_t *temp_font;
  38. char *datname;
  39. filehandle_t *fp;
  40. W32 size;
  41. W32 i;
  42. if( num_fonts == (MAX_FONTS - 1) )
  43. {
  44. Com_Printf( "[createFont]: No more font slots open\n" );
  45. return NULL;
  46. }
  47. temp_font = Z_Malloc( sizeof( font_t ) );
  48. temp_font->texfont = TM_FindTexture( filename, TT_Pic );
  49. if( NULL == temp_font->texfont )
  50. {
  51. Com_Printf( "[createFont]: unable to open file (%s)\n", filename );
  52. Z_Free( temp_font );
  53. return NULL;
  54. }
  55. memset( temp_font->nCharWidth, 0, sizeof( temp_font->nCharWidth ) );
  56. datname = MM_MALLOC( strlen( filename ) + 1 );
  57. FS_StripExtension( (char *)filename, datname );
  58. my_strlcat( datname, ".dat", strlen( filename ) + 1 );
  59. fp = FS_OpenFile( datname, 0 );
  60. if( NULL == fp )
  61. {
  62. Com_Printf( "[createFont]: unable to open file (%s)\n", datname );
  63. MM_FREE( datname );
  64. Z_Free( temp_font );
  65. return NULL;
  66. }
  67. size = FS_GetFileSize( fp );
  68. // check header size
  69. if( size < 10 )
  70. {
  71. Com_Printf( "[createFont]: File (%s) has incorrect file length\n", datname );
  72. MM_FREE( datname );
  73. Z_Free( temp_font );
  74. FS_CloseFile( fp );
  75. return NULL;
  76. }
  77. // Check sig of font dat file
  78. FS_ReadFile( &size, 1, 4, fp );
  79. FS_ReadFile( &temp_font->nMaxWidth, 1, 1, fp );
  80. FS_ReadFile( &temp_font->nMaxHeight, 1, 1, fp );
  81. FS_ReadFile( &size, 1, 4, fp );
  82. size = LittleLong( size );
  83. if( size > 127 )
  84. {
  85. Com_Printf( "[createFont]: File (%s) has incorrect Character Width array\n", datname );
  86. MM_FREE( datname );
  87. Z_Free( temp_font );
  88. FS_CloseFile( fp );
  89. return NULL;
  90. }
  91. FS_ReadFile( &temp_font->nCharWidth, 1, size, fp );
  92. FS_CloseFile( fp );
  93. temp_font->nSize = 2;
  94. temp_font->colour[ 3 ] = 255;
  95. temp_font->hFrac = (float)(temp_font->nMaxHeight / (float)temp_font->texfont->height);
  96. temp_font->wFrac = (float)(temp_font->nMaxWidth / (float)temp_font->texfont->width);
  97. for( i = 0 ; i < MAX_FONTS ; ++i )
  98. {
  99. if( ! myfonts[ i ] )
  100. {
  101. break;
  102. }
  103. }
  104. if( i == (MAX_FONTS - 1) )
  105. {
  106. Com_Printf( "[createFont]: No more font slots open\n" );
  107. MM_FREE( datname );
  108. Z_Free( temp_font );
  109. return NULL;
  110. }
  111. myfonts[ i ] = temp_font;
  112. MM_FREE( datname );
  113. return temp_font;
  114. }
  115. void Font_Init( void )
  116. {
  117. W32 i;
  118. for( i = 0 ; i < MAX_FONTS ; ++i )
  119. {
  120. myfonts[ i ] = NULL;
  121. }
  122. (void)createFont( "iphone/font1.tga" );
  123. // (void)createFont( "iphone/font2.tga" );
  124. }
  125. void Font_Shutdown( void )
  126. {
  127. }
  128. void Font_GetMsgDimensions( FONTSELECT fs, const char *string, int *w, int *h )
  129. {
  130. int width = 0;
  131. int mx = 0;
  132. W16 scale;
  133. int height;
  134. if( ! myfonts[ fs ] )
  135. {
  136. *w = *h = 0;
  137. return;
  138. }
  139. scale = myfonts[ fs ]->nMaxHeight * myfonts[ fs ]->nSize;
  140. height = scale;
  141. while( *string )
  142. {
  143. if( *string == '\n' )
  144. {
  145. if( mx > width )
  146. {
  147. width = mx;
  148. }
  149. mx = 0;
  150. height += scale;
  151. ++string;
  152. continue;
  153. }
  154. mx += myfonts[ fs ]->nCharWidth[ (*string)-32 ] * myfonts[ fs ]->nSize;
  155. ++string;
  156. }
  157. if( mx > width )
  158. {
  159. width = mx;
  160. }
  161. *w = width;
  162. *h = height;
  163. }
  164. void Font_SetSize( FONTSELECT fs, W16 size )
  165. {
  166. if( myfonts[ fs ] )
  167. {
  168. myfonts[ fs ]->nSize = size;
  169. }
  170. }
  171. W16 Font_GetSize( FONTSELECT fs )
  172. {
  173. if( myfonts[ fs ] )
  174. {
  175. return( myfonts[ fs ]->nMaxHeight * myfonts[ fs ]->nSize );
  176. }
  177. return 0;
  178. }
  179. void Font_SetColour( FONTSELECT fs, colour3_t c )
  180. {
  181. if( myfonts[ fs ] )
  182. {
  183. myfonts[ fs ]->colour[ 0 ] = c[ 0 ];
  184. myfonts[ fs ]->colour[ 1 ] = c[ 1 ];
  185. myfonts[ fs ]->colour[ 2 ] = c[ 2 ];
  186. }
  187. }
  188. extern void R_Draw_Character( int x, int y, int num, font_t *myfont );
  189. void Font_put_line( FONTSELECT fs, int x, int y, const char *string )
  190. {
  191. int mx = x;
  192. W16 scale;
  193. if( ! myfonts[ fs ] )
  194. {
  195. return;
  196. }
  197. scale = myfonts[ fs ]->nSize;
  198. while( *string )
  199. {
  200. if( *string == '\n' )
  201. {
  202. mx = x;
  203. y += myfonts[ fs ]->nMaxHeight * scale;
  204. ++string;
  205. continue;
  206. }
  207. R_Draw_Character( mx, y, *string, myfonts[ fs ] );
  208. mx += myfonts[ fs ]->nCharWidth[ (*string)-32 ] * scale;
  209. ++string;
  210. }
  211. }
  212. void Font_put_lineR2L( FONTSELECT fs, int x, int y, const char *string )
  213. {
  214. int mx = x;
  215. unsigned int charindex;
  216. unsigned int i;
  217. if( ! myfonts[ fs ] )
  218. {
  219. return;
  220. }
  221. for ( i = 0; i < strlen( string ); ++i )
  222. {
  223. charindex = strlen( string ) - i - 1;
  224. mx -= myfonts[ fs ]->nCharWidth[ string[ charindex ]-32 ] * myfonts[ fs ]->nSize;
  225. R_Draw_Character( mx, y, string[ charindex ], myfonts[ fs ] );
  226. }
  227. }
  228. W16 Font_put_character( FONTSELECT fs, int x, int y, W16 num )
  229. {
  230. if( ! myfonts[ fs ] || num > 126 )
  231. {
  232. return 0;
  233. }
  234. R_Draw_Character( x, y, num, myfonts[ fs ] );
  235. return( myfonts[ fs ]->nCharWidth[ num - 32 ] * myfonts[ fs ]->nSize );
  236. }
  237. void Font_put_line_size( FONTSELECT fs, int x, int y, const char *start, const char *end )
  238. {
  239. int mx = x;
  240. W16 scale;
  241. if( ! myfonts[ fs ] )
  242. {
  243. return;
  244. }
  245. scale = myfonts[ fs ]->nSize;
  246. while( start != end )
  247. {
  248. R_Draw_Character( mx, y, *start, myfonts[ fs ] );
  249. mx += myfonts[ fs ]->nCharWidth[ (*start)-32 ] * scale;
  250. ++start;
  251. }
  252. }
  253. W8 Font_get_line( FONTSELECT fs, int line_width, string_seg_t *sst )
  254. {
  255. int x = 0, last_word_width = 0, last_word_spaces = 0;
  256. int in_a_word = 0;
  257. int t_words = 0;
  258. int t_spaces = 0;
  259. int chars_width = 0;
  260. W16 scale;
  261. const char *word_start = sst->start;
  262. if( ! myfonts[ fs ] )
  263. {
  264. return false;
  265. }
  266. scale = myfonts[ fs ]->nSize;
  267. if( line_width < 0 )
  268. {
  269. line_width = 1000000;
  270. }
  271. while( *sst->end != '\0' && *sst->end != '\n' )
  272. {
  273. char c = *sst->end;
  274. int c_width = myfonts[ fs ]->nCharWidth[ c - 32 ] * scale; // FIX ME
  275. // we exceeded the space available for this line
  276. if( x + c_width > line_width )
  277. {
  278. if( in_a_word )
  279. {
  280. chars_width = last_word_width;
  281. sst->end = (char *)word_start;
  282. t_spaces = last_word_spaces;
  283. }
  284. return t_words ? true : false;
  285. }
  286. x += c_width;
  287. if( c != ' ' )
  288. {
  289. if( ! in_a_word )
  290. {
  291. last_word_width = chars_width;
  292. word_start = sst->end;
  293. }
  294. in_a_word = 1;
  295. chars_width += c_width;
  296. }
  297. else
  298. {
  299. if( in_a_word )
  300. {
  301. in_a_word = 0;
  302. t_words++;
  303. last_word_spaces = t_spaces;
  304. }
  305. t_spaces++;
  306. }
  307. ++sst->end;
  308. }
  309. if( in_a_word )
  310. {
  311. t_words++;
  312. }
  313. if( *sst->end != '\0' && *sst->end == '\n' )
  314. {
  315. ++sst->end;
  316. }
  317. return t_words ? true : false;
  318. }
  319. void Font_put_paragraph( FONTSELECT fs, short x, short y,
  320. const char *string,
  321. int space_between_lines,
  322. int line_width_in_pixel )
  323. {
  324. string_seg_t sst;
  325. sst.start = sst.end = (char *)string;
  326. if( ! myfonts[ fs ] )
  327. {
  328. return;
  329. }
  330. while( Font_get_line( fs, line_width_in_pixel, &sst ) )
  331. {
  332. Font_put_line_size( fs, x, y, sst.start, sst.end );
  333. if( *sst.end != '\0' && *sst.end == ' ' )
  334. {
  335. sst.start = sst.end;
  336. ++sst.start;
  337. sst.end = sst.start;
  338. }
  339. else if( *sst.end != '\0' && *sst.end == '\n' )
  340. {
  341. while( *sst.end == '\n' )
  342. {
  343. ++sst.end;
  344. y += Font_GetSize( fs ) + space_between_lines;
  345. }
  346. sst.start = sst.end;
  347. }
  348. else
  349. {
  350. sst.start = sst.end;
  351. }
  352. y += Font_GetSize( fs ) + space_between_lines;
  353. }
  354. }