Counter Strike : Global Offensive Source Code
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.

183 lines
5.4 KiB

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