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.

169 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef LINUXFONT_H
  8. #define LINUXFONT_H
  9. #include "utlrbtree.h"
  10. #include <tier0/memdbgoff.h>
  11. #include <tier0/memdbgon.h>
  12. #include "tier1/strtools.h"
  13. #include "tier1/utlstring.h"
  14. #include <ft2build.h>
  15. #include FT_FREETYPE_H
  16. //-----------------------------------------------------------------------------
  17. // Purpose: encapsulates a OSX font
  18. //-----------------------------------------------------------------------------
  19. class CLinuxFont
  20. {
  21. public:
  22. CLinuxFont();
  23. ~CLinuxFont();
  24. // creates the font from windows. returns false if font does not exist in the OS.
  25. virtual bool CreateFromMemory(const char *windowsFontName, void *data, int size, int tall, int weight, int blur, int scanlines, int flags);
  26. // writes the char into the specified 32bpp texture
  27. virtual void GetCharRGBA( wchar_t ch, int rgbaWide, int rgbaTall, unsigned char *rgba);
  28. // returns true if the font is equivalent to that specified
  29. virtual bool IsEqualTo(const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags);
  30. // returns true only if this font is valid for use
  31. virtual bool IsValid();
  32. // gets the abc widths for a character
  33. // A spacing is the distance to add to the current position before drawing the character glyph.
  34. // B spacing is the width of the drawn portion of the glyph.
  35. // C spacing is the distance to add to the current position to provide white space to the right of the glyph.
  36. virtual void GetCharABCWidths(int ch, int &a, int &b, int &c);
  37. // set the font to be the one to currently draw with in the gdi
  38. void *SetAsActiveFont( void *glContext );
  39. // returns the height of the font, in pixels
  40. virtual int GetHeight();
  41. // returns the requested height of the font.
  42. virtual int GetHeightRequested();
  43. // returns the ascent of the font, in pixels (ascent=units above the base line)
  44. virtual int GetAscent();
  45. // returns the maximum width of a character, in pixels
  46. virtual int GetMaxCharWidth();
  47. // returns the flags used to make this font
  48. virtual int GetFlags();
  49. // returns true if this font is underlined
  50. virtual bool GetUnderlined() { return m_bUnderlined; }
  51. // gets the name of this font
  52. const char *GetName() { return m_szName.String(); }
  53. const char *GetFamilyName() { return m_face ? m_face->family_name : NULL; }
  54. // gets the weight of the font
  55. virtual int GetWeight() { return m_iWeight; }
  56. bool HasChar(wchar_t wch);
  57. // gets the width of ch given its position around before and after chars
  58. virtual void GetKernedCharWidth( wchar_t ch, wchar_t chBefore, wchar_t chAfter, float &wide, float &abcA, float &abcC );
  59. #ifdef DBGFLAG_VALIDATE
  60. void Validate( CValidator &validator, char *pchName );
  61. #endif
  62. // Given a font name from windows, match it to the filename and return that.
  63. static char *GetFontFileName(const char *windowsFontName, int flags);
  64. protected:
  65. CUtlString m_szName;
  66. int m_iTall;
  67. int m_iWeight;
  68. int m_iFlags;
  69. bool m_bAntiAliased;
  70. bool m_bRotary;
  71. bool m_bAdditive;
  72. int m_iDropShadowOffset;
  73. bool m_bUnderlined;
  74. int m_iOutlineSize;
  75. int m_iHeight;
  76. int m_iHeightRequested;
  77. int m_iMaxCharWidth;
  78. int m_iAscent;
  79. int m_iScanLines;
  80. int m_iBlur;
  81. private:
  82. static void CreateFontList();
  83. // abc widths
  84. struct abc_t
  85. {
  86. short b;
  87. char a;
  88. char c;
  89. };
  90. // cache for storing asian abc widths (since it's too big too just store them all)
  91. struct abc_cache_t
  92. {
  93. wchar_t wch;
  94. abc_t abc;
  95. };
  96. CUtlRBTree<abc_cache_t, unsigned short> m_ExtendedABCWidthsCache;
  97. static bool ExtendedABCWidthsCacheLessFunc(const abc_cache_t &lhs, const abc_cache_t &rhs);
  98. // cache for storing asian abc widths (since it's too big too just store them all)
  99. struct kernedSize
  100. {
  101. float wide;
  102. };
  103. struct kerned_abc_cache_t
  104. {
  105. wchar_t wch;
  106. wchar_t wchBefore;
  107. wchar_t wchAfter;
  108. kernedSize abc;
  109. };
  110. CUtlRBTree<kerned_abc_cache_t, unsigned short> m_ExtendedKernedABCWidthsCache;
  111. static bool ExtendedKernedABCWidthsCacheLessFunc(const kerned_abc_cache_t &lhs, const kerned_abc_cache_t &rhs);
  112. bool m_faceValid;
  113. FT_Face m_face;
  114. struct font_name_entry
  115. {
  116. font_name_entry()
  117. {
  118. m_pchFile = NULL;
  119. m_pchFriendlyName = NULL;
  120. }
  121. char *m_pchFile;
  122. char *m_pchFriendlyName;
  123. bool operator<( const font_name_entry &rhs ) const
  124. {
  125. return V_stricmp( rhs.m_pchFriendlyName, m_pchFriendlyName ) > 0;
  126. }
  127. };
  128. static CUtlRBTree< font_name_entry > m_FriendlyNameCache;
  129. static bool ms_bSetFriendlyNameCacheLessFunc;
  130. };
  131. #endif // LINUXFONT_H