Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "vgui_surfacelib/FontAmalgam.h"
  8. #include <tier0/dbg.h>
  9. #include <vgui/VGUI.h>
  10. #include <vgui/ISurface.h>
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Constructor
  15. //-----------------------------------------------------------------------------
  16. CFontAmalgam::CFontAmalgam()
  17. {
  18. m_Fonts.EnsureCapacity( 4 );
  19. m_iMaxHeight = 0;
  20. m_iMaxWidth = 0;
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Destructor
  24. //-----------------------------------------------------------------------------
  25. CFontAmalgam::~CFontAmalgam()
  26. {
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose: Data accessor
  30. //-----------------------------------------------------------------------------
  31. const char *CFontAmalgam::Name()
  32. {
  33. return m_szName;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Data accessor
  37. //-----------------------------------------------------------------------------
  38. void CFontAmalgam::SetName(const char *name)
  39. {
  40. Q_strncpy(m_szName, name, sizeof(m_szName));
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: adds a font to the amalgam
  44. //-----------------------------------------------------------------------------
  45. void CFontAmalgam::AddFont(font_t *font, int lowRange, int highRange)
  46. {
  47. int i = m_Fonts.AddToTail();
  48. m_Fonts[i].font = font;
  49. m_Fonts[i].lowRange = lowRange;
  50. m_Fonts[i].highRange = highRange;
  51. m_iMaxHeight = max(font->GetHeight(), m_iMaxHeight);
  52. m_iMaxWidth = max(font->GetMaxCharWidth(), m_iMaxWidth);
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose: clears the fonts
  56. //-----------------------------------------------------------------------------
  57. void CFontAmalgam::RemoveAll()
  58. {
  59. // clear out
  60. m_Fonts.RemoveAll();
  61. m_iMaxHeight = 0;
  62. m_iMaxWidth = 0;
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: returns the font for the given character
  66. //-----------------------------------------------------------------------------
  67. font_t *CFontAmalgam::GetFontForChar(int ch)
  68. {
  69. for (int i = 0; i < m_Fonts.Count(); i++)
  70. {
  71. #if defined(LINUX)
  72. if ( ch >= m_Fonts[i].lowRange && ch <= m_Fonts[i].highRange && m_Fonts[i].font->HasChar(ch))
  73. #else
  74. if (ch >= m_Fonts[i].lowRange && ch <= m_Fonts[i].highRange)
  75. #endif
  76. {
  77. Assert( m_Fonts[i].font->IsValid() );
  78. return m_Fonts[i].font;
  79. }
  80. }
  81. return NULL;
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: sets the scale of the font
  85. //-----------------------------------------------------------------------------
  86. void CFontAmalgam::SetFontScale(float sx, float sy)
  87. {
  88. if (!m_Fonts.Count())
  89. return;
  90. // Make sure this is a bitmap font!
  91. if ( GetFlags( 0 ) & vgui::ISurface::FONTFLAG_BITMAP )
  92. {
  93. reinterpret_cast< CBitmapFont* >( m_Fonts[0].font )->SetScale( sx, sy );
  94. }
  95. else
  96. {
  97. Warning( "%s: Can't set font scale on a non-bitmap font!\n", m_Fonts[0].font->GetName() );
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: returns the max height of the font set
  102. //-----------------------------------------------------------------------------
  103. int CFontAmalgam::GetFontHeight()
  104. {
  105. if (!m_Fonts.Count())
  106. {
  107. return m_iMaxHeight;
  108. }
  109. return m_Fonts[0].font->GetHeight();
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose: returns requested height of the font
  113. //-----------------------------------------------------------------------------
  114. int CFontAmalgam::GetFontHeightRequested()
  115. {
  116. if (!m_Fonts.Count())
  117. {
  118. return m_iMaxHeight;
  119. }
  120. return m_Fonts[0].font->GetHeightRequested();
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: returns the maximum width of a character in a font
  124. //-----------------------------------------------------------------------------
  125. int CFontAmalgam::GetFontMaxWidth()
  126. {
  127. return m_iMaxWidth;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: returns the name of the font that is loaded
  131. //-----------------------------------------------------------------------------
  132. const char *CFontAmalgam::GetFontName(int i)
  133. {
  134. if ( m_Fonts.IsValidIndex( i ) && m_Fonts[ i ].font )
  135. {
  136. return m_Fonts[ i ].font->GetName();
  137. }
  138. else
  139. {
  140. return NULL;
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose: returns the family name of the font that is loaded
  145. //-----------------------------------------------------------------------------
  146. const char *CFontAmalgam::GetFontFamilyName( int i )
  147. {
  148. if ( m_Fonts.IsValidIndex( i ) && m_Fonts[ i ].font )
  149. {
  150. return m_Fonts[ i ].font->GetFamilyName();
  151. }
  152. return "";
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: returns the name of the font that is loaded
  156. //-----------------------------------------------------------------------------
  157. int CFontAmalgam::GetFlags(int i)
  158. {
  159. if ( m_Fonts.Count() && m_Fonts[i].font )
  160. {
  161. return m_Fonts[i].font->GetFlags();
  162. }
  163. else
  164. {
  165. return 0;
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose: returns the number of fonts this amalgam contains
  170. //-----------------------------------------------------------------------------
  171. int CFontAmalgam::GetCount()
  172. {
  173. return m_Fonts.Count();
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose: returns the max height of the font set
  177. //-----------------------------------------------------------------------------
  178. bool CFontAmalgam::GetUnderlined()
  179. {
  180. if (!m_Fonts.Count())
  181. {
  182. return false;
  183. }
  184. return m_Fonts[0].font->GetUnderlined();
  185. }
  186. #ifdef DBGFLAG_VALIDATE
  187. //-----------------------------------------------------------------------------
  188. // Purpose: Ensure that all of our internal structures are consistent, and
  189. // account for all memory that we've allocated.
  190. // Input: validator - Our global validator object
  191. // pchName - Our name (typically a member var in our container)
  192. //-----------------------------------------------------------------------------
  193. void CFontAmalgam::Validate( CValidator &validator, char *pchName )
  194. {
  195. validator.Push( "CFontAmalgam", this, pchName );
  196. ValidateObj( m_Fonts );
  197. validator.Pop();
  198. }
  199. #endif // DBGFLAG_VALIDATE