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.

129 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef FONTMANAGER_H
  8. #define FONTMANAGER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui/VGUI.h>
  13. #include "vgui_surfacelib/FontAmalgam.h"
  14. #include "materialsystem/imaterialsystem.h"
  15. #include "filesystem.h"
  16. #include "vguifont.h"
  17. #ifdef LINUX
  18. #include <ft2build.h>
  19. #include FT_FREETYPE_H
  20. typedef void *(*FontDataHelper)( const char *pchFontName, int &size, const char *fontFileName );
  21. #endif
  22. #ifdef CreateFont
  23. #undef CreateFont
  24. #endif
  25. using vgui::HFont;
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Creates and maintains list of actively used fonts
  28. //-----------------------------------------------------------------------------
  29. class CFontManager
  30. {
  31. public:
  32. CFontManager();
  33. ~CFontManager();
  34. void SetLanguage(const char *language);
  35. // clears the current font list, frees any resources
  36. void ClearAllFonts();
  37. HFont CreateFont();
  38. bool SetFontGlyphSet(HFont font, const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags);
  39. bool SetFontGlyphSet(HFont font, const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags, int nRangeMin, int nRangeMax);
  40. bool SetBitmapFontGlyphSet(HFont font, const char *windowsFontName, float scalex, float scaley, int flags);
  41. void SetFontScale(HFont font, float sx, float sy);
  42. const char *GetFontName( HFont font );
  43. const char *GetFontFamilyName( HFont font );
  44. void GetCharABCwide(HFont font, int ch, int &a, int &b, int &c);
  45. int GetFontTall(HFont font);
  46. int GetFontTallRequested(HFont font);
  47. int GetFontAscent(HFont font, wchar_t wch);
  48. int GetCharacterWidth(HFont font, int ch);
  49. bool GetFontUnderlined( HFont font );
  50. void GetTextSize(HFont font, const wchar_t *text, int &wide, int &tall);
  51. font_t *GetFontForChar(HFont, wchar_t wch);
  52. bool IsFontAdditive(HFont font);
  53. bool IsBitmapFont(HFont font );
  54. void SetInterfaces( IFileSystem *pFileSystem, IMaterialSystem *pMaterialSystem )
  55. {
  56. m_pFileSystem = pFileSystem;
  57. m_pMaterialSystem = pMaterialSystem;
  58. }
  59. IFileSystem *FileSystem() { return m_pFileSystem; }
  60. IMaterialSystem *MaterialSystem() { return m_pMaterialSystem; }
  61. #ifdef LINUX
  62. FT_Library GetFontLibraryHandle() { return library; }
  63. void SetFontDataHelper( FontDataHelper helper ) { m_pFontDataHelper = helper; }
  64. #endif
  65. #if defined( _X360 )
  66. // secondary cache to speed TTF setup
  67. bool GetCachedXUIMetrics( const char *pWindowsFontName, int tall, int style, XUIFontMetrics *pFontMetrics, XUICharMetrics charMetrics[256] );
  68. void SetCachedXUIMetrics( const char *pWindowsFontName, int tall, int style, XUIFontMetrics *pFontMetrics, XUICharMetrics charMetrics[256] );
  69. #endif
  70. // used as a hint that intensive TTF operations are finished
  71. void ClearTemporaryFontCache();
  72. void GetKernedCharWidth( vgui::HFont font, wchar_t ch, wchar_t chBefore, wchar_t chAfter, float &wide, float &abcA, float &abcC );
  73. private:
  74. bool IsFontForeignLanguageCapable(const char *windowsFontName);
  75. font_t *CreateOrFindWin32Font(const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags);
  76. CBitmapFont *CreateOrFindBitmapFont(const char *windowsFontName, float scalex, float scaley, int flags);
  77. const char *GetFallbackFontName(const char *windowsFontName);
  78. const char *GetForeignFallbackFontName();
  79. CUtlVector<CFontAmalgam> m_FontAmalgams;
  80. CUtlVector<font_t *> m_Win32Fonts;
  81. #ifdef LINUX
  82. FT_Library library;
  83. FontDataHelper m_pFontDataHelper;
  84. #endif
  85. char m_szLanguage[64];
  86. IFileSystem *m_pFileSystem;
  87. IMaterialSystem *m_pMaterialSystem;
  88. #if defined( _X360 )
  89. // These are really bounded by the number of fonts that the game would ever realistically create, so ~100 is expected.
  90. // Many of these fonts are redundant and the same underlying metrics can be used. This avoid the very expensive TTF font metric lookup.
  91. struct XUIMetricCache_t
  92. {
  93. // the font signature that can change
  94. CUtlSymbol fontSymbol;
  95. int tall;
  96. int style;
  97. // the metrics
  98. XUIFontMetrics fontMetrics;
  99. XUICharMetrics charMetrics[256];
  100. };
  101. CUtlVector< XUIMetricCache_t > m_XUIMetricCache;
  102. #endif
  103. };
  104. // singleton accessor
  105. extern CFontManager &FontManager();
  106. #endif // FONTMANAGER_H