font3d.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando / G 3D Library *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/font3d.h $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 4/05/01 2:19p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*/
  33. #if defined(_MSC_VER)
  34. #pragma once
  35. #endif
  36. #ifndef FONT3D_H
  37. #define FONT3D_H
  38. #include "always.h"
  39. #include "refcount.h"
  40. #include "vector4.h"
  41. #include "widestring.h"
  42. #include "rect.h"
  43. class TextureClass;
  44. class SurfaceClass;
  45. /******************************************************************
  46. **
  47. ** Font3DDataClass
  48. **
  49. ** This class provides an interface to a font texture. Once
  50. ** created and loaded with a font, the object can return texture
  51. ** u v coordinate for any character in the font, as well as the
  52. ** character width for proportional fonts. Fonts are loaded as
  53. ** 16-bit Targa files, then converted to proportional fonts by
  54. ** finding the minimum bounding box for each chacter. The font
  55. ** texture is then minimized to a 256x256 or 128x128 texture
  56. ** material by re-stacking chars by thier minimum bounding box.
  57. **
  58. ** During use, this class is really no more than a data table accessor
  59. ** Only during creation is any real code run.
  60. **
  61. ** Since the space char nevers needs to be drawn, do not use
  62. ** the conventional method of acessing and drawing chars (which will
  63. ** still work). Instead, call Get_Space_Width to determine the user-
  64. ** settable width, and skip the drawing.
  65. **
  66. *******************************************************************/
  67. class Font3DDataClass : public RefCountClass {
  68. public:
  69. /*
  70. ** Constructor, Constructor which loads a targa file,
  71. ** and Destructor
  72. */
  73. Font3DDataClass( const char *filename );
  74. ~Font3DDataClass();
  75. // the name of the font data (used for name matching and the like.)
  76. char *Name;
  77. /*
  78. ** access character width and height in pixels (clamp char to 0.255)
  79. */
  80. unsigned char Char_Width( WCHAR ch = (WCHAR)'H' ) { return CharWidthTable[ch&0xFF]; }// & 0xFF]; } // No need to "& 0xff" with chars!!!
  81. unsigned char Char_Height( WCHAR /*ch = 'H'*/ ) { return CharHeight; }
  82. // u and v are in normalized texture space
  83. inline float Char_U_Offset( WCHAR ch = (WCHAR)'H') { return UOffsetTable[ch&0xFF]; }// & 0xFF]; }
  84. inline float Char_V_Offset( WCHAR ch = (WCHAR)'H') { return VOffsetTable[ch&0xFF]; }// & 0xFF]; }
  85. inline float Char_U_Width( WCHAR ch = (WCHAR)'H' ) { return UWidthTable[ch&0xFF]; }// & 0xFF]; }
  86. inline float Char_V_Height( WCHAR /*ch = 'H'*/) { return VHeight; }
  87. // get all four UV values as one vector4
  88. Vector4 Char_UV_Corners( WCHAR ch = (WCHAR)'H' )
  89. {
  90. // ch &= 0xFF;
  91. return Vector4( UOffsetTable[ch], VOffsetTable[ch],
  92. UOffsetTable[ch] + UWidthTable[ch],
  93. VOffsetTable[ch] + VHeight );
  94. }
  95. /*
  96. ** access texture material
  97. */
  98. TextureClass * Peek_Texture( void ) { return Texture; }
  99. private:
  100. /*
  101. ** The material (texture) which holds the font
  102. */
  103. TextureClass * Texture;
  104. /*
  105. ** Normalized texture page offsets for each character
  106. */
  107. float UOffsetTable[ 256 ];
  108. float VOffsetTable[ 256 ];
  109. float UWidthTable[ 256 ];
  110. float VHeight;
  111. unsigned char CharWidthTable[ 256 ];
  112. unsigned char CharHeight;
  113. /*
  114. ** load a targa font image (.TGA)
  115. */
  116. bool Load_Font_Image( const char *filename );
  117. /*
  118. ** routines to convert a mono-spaced font to a proportional
  119. ** font and minimize texture image size as a result
  120. */
  121. SurfaceClass *Make_Proportional( SurfaceClass *font_image );
  122. SurfaceClass *Minimize_Font_Image( SurfaceClass *font_image );
  123. };
  124. /******************************************************************
  125. **
  126. ** Font3DInstanceClass
  127. **
  128. *******************************************************************/
  129. class Font3DInstanceClass : public RefCountClass {
  130. public:
  131. /*
  132. ** Constructor which creates/gets a Font3DDataClass object,
  133. ** and Destructor
  134. */
  135. Font3DInstanceClass( const char *filename );
  136. ~Font3DInstanceClass();
  137. /*
  138. ** access texture material
  139. */
  140. TextureClass *Peek_Texture( void ) { return FontData->Peek_Texture(); }
  141. /*
  142. ** The non-scaled monospace char width in pixels ( set to 0 for proportional spaced font )
  143. */
  144. void Set_Mono_Spaced( void );
  145. void Set_Proportional( void ) { MonoSpacing = 0; Build_Cached_Tables(); }
  146. /*
  147. ** Set the font scale (default to 1)
  148. ** This amount will be automatically applied to all Char_Screen_Width calls
  149. */
  150. void Set_Scale( float scale ) { Scale = scale; Build_Cached_Tables(); }
  151. // float Get_Scale() const { return Scale; }
  152. /*
  153. ** The scaled character pixel width, height, and spacing data (clamp char to 0.255)
  154. */
  155. float Char_Width( WCHAR ch ) const { return ScaledWidthTable[ch&0xFF]; }
  156. float Char_Spacing( WCHAR ch ) const { return ScaledSpacingTable[ch&0xFF]; }
  157. float Char_Height( void ) const { return ScaledHeight; }
  158. /*
  159. ** The scaled pixel width of a string; useful before printing to avoid screen overflows.
  160. */
  161. float String_Width( const WCHAR *test_str );
  162. float String_Width( const char *test_str );
  163. /*
  164. ** Char UVs
  165. */
  166. // u and v are in normalized texture space
  167. // inline float Char_U_Offset( WCHAR ch = (WCHAR)'H') { return FontData->Char_U_Offset( ch & 0xFF ); }
  168. // inline float Char_V_Offset( WCHAR ch = (WCHAR)'H') { return FontData->Char_V_Offset( ch & 0xFF ); }
  169. // inline float Char_U_Width( WCHAR ch = (WCHAR)'H' ) { return FontData->Char_U_Width( ch & 0xFF ); }
  170. // inline float Char_V_Height( WCHAR ch = (WCHAR)'H') { return FontData->Char_V_Height( ch & 0xFF ); }
  171. // Vector4 Char_UV_Corners( WCHAR ch = (WCHAR)'H' ) { return FontData->Char_UV_Corners( ch & 0xFF ); }
  172. RectClass Char_UV( WCHAR ch ) { return RectClass( FontData->Char_U_Offset(ch),
  173. FontData->Char_V_Offset(ch),
  174. FontData->Char_U_Offset(ch) + FontData->Char_U_Width(ch),
  175. FontData->Char_V_Offset(ch) + FontData->Char_V_Height(ch) ); }
  176. private:
  177. Font3DDataClass * FontData; // The font data
  178. float Scale; // The current scale factor
  179. float SpaceSpacing; // non-scaled width of space in pixels ( defaults to 1/2 'H' width )
  180. float InterCharSpacing; // non-scaled width between chars in pixels
  181. float MonoSpacing; // non-scaled monospace char width in pixels (0 for proportional)
  182. float ScaledWidthTable[256]; // scaled cache of the chars pixel widths
  183. float ScaledSpacingTable[256]; // scaled cache of the chars pixel spacing
  184. float ScaledHeight; // scaled cache of the chars pixel height
  185. void Build_Cached_Tables();
  186. };
  187. #endif