IFont.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : CryFont interface.
  9. #ifndef CRYINCLUDE_CRYCOMMON_IFONT_H
  10. #define CRYINCLUDE_CRYCOMMON_IFONT_H
  11. #pragma once
  12. #include <AzCore/PlatformDef.h>
  13. #include <Cry_Math.h>
  14. #include <Cry_Color.h>
  15. #include <smartptr.h>
  16. #include <AzCore/std/smart_ptr/shared_ptr.h>
  17. #include <AzCore/std/string/string.h>
  18. #include <AzCore/EBus/EBus.h>
  19. struct ISystem;
  20. struct ICryFont;
  21. struct IFFont;
  22. struct FontFamily;
  23. struct SVF_P2F_C4B_T2F_F4B;
  24. extern "C"
  25. #ifdef CRYFONT_EXPORTS
  26. AZ_DLL_EXPORT
  27. #else
  28. AZ_DLL_IMPORT
  29. #endif
  30. ICryFont * CreateCryFontInterface(ISystem * pSystem);
  31. typedef ICryFont*(* PFNCREATECRYFONTINTERFACE)(ISystem* pSystem);
  32. typedef AZStd::shared_ptr<FontFamily> FontFamilyPtr;
  33. namespace IFFontConstants
  34. {
  35. //! The default scale applied to individual glyphs when rendering to the font texture.
  36. //!
  37. //! This is a "best guess" to try and fit all glyphs of a font within the
  38. //! bounds of a font texture slot. This value can be defined on a per-font
  39. //! basis via the "sizeratio" *.font XML attribute.
  40. const float defaultSizeRatio = 0.8f;
  41. }
  42. //////////////////////////////////////////////////////////////////////////////////////////////
  43. struct ICryFont
  44. {
  45. static const int defaultGlyphSizeX = -1; //!< Default glyph size indicates that glyphs in the font texture
  46. //!< should be rendered at the maximum resolution supported by
  47. //!< the font texture's glyph cell/slot configuration (configured
  48. //!< via font XML).
  49. static const int defaultGlyphSizeY = -1; //!< Default glyph size indicates that glyphs in the font texture
  50. //!< should be rendered at the maximum resolution supported by
  51. //!< the font texture's glyph cell/slot configuration (configured
  52. //!< via font XML).
  53. // <interfuscator:shuffle>
  54. virtual ~ICryFont(){}
  55. //
  56. virtual void Release() = 0;
  57. // Summary:
  58. // Creates a named font (case insensitive)
  59. virtual IFFont* NewFont(const char* pFontName) = 0;
  60. // Summary:
  61. // Gets a named font (case insensitive)
  62. virtual IFFont* GetFont(const char* pFontName) const = 0;
  63. //! \brief Loads and initializes a Font Family from a *.fontfamily file
  64. //! \param pFontFamilyName Name of font family to load (or name of a *.fontfamily file)
  65. virtual FontFamilyPtr LoadFontFamily(const char* pFontFamilyName) = 0;
  66. // Summary:
  67. // Gets font family (case insensitive)
  68. virtual FontFamilyPtr GetFontFamily(const char* pFontFamilyName) = 0;
  69. //! \brief Adds the characters in the given string to all of the font textures within the font family.
  70. //! All font styles within the given font family (bold, italic, etc.) will have the given
  71. //! characters added to their font textures.
  72. //!
  73. //! \param pFontFamily The font family to add the characters to.
  74. //! \param pChars String of characters to add to font textures (UTF-8 supported).
  75. //! \param glyphSizeX Width (in pixels) of the characters to be rendered at in the font texture.
  76. //! \param glyphSizeY Height (in pixels) of the characters to be rendered at in the font texture.
  77. virtual void AddCharsToFontTextures(FontFamilyPtr pFontFamily, const char* pChars, int glyphSizeX = defaultGlyphSizeX, int glyphSizeY = defaultGlyphSizeY) = 0;
  78. // Summary:
  79. // All font names separated by ,
  80. // Example:
  81. // "console,default,hud"
  82. virtual AZStd::string GetLoadedFontNames() const = 0;
  83. //! \brief Called when the g_language (current language) setting changes.
  84. //!
  85. //! Mainly used to reload font family resources for the new language.
  86. virtual void OnLanguageChanged() = 0;
  87. //! \brief Reload all fonts
  88. virtual void ReloadAllFonts() = 0;
  89. // </interfuscator:shuffle>
  90. };
  91. //////////////////////////////////////////////////////////////////////////////////////////////
  92. #define TTFFLAG_SMOOTH_NONE 0x00000000 // No smooth.
  93. #define TTFFLAG_SMOOTH_BLUR 0x00000001 // Smooth by blurring it.
  94. #define TTFFLAG_SMOOTH_SUPERSAMPLE 0x00000002 // Smooth by rendering the characters into a bigger texture, and then resize it to the normal size using bilinear filtering.
  95. #define TTFFLAG_SMOOTH_MASK 0x0000000f // Mask for retrieving.
  96. #define TTFFLAG_SMOOTH_SHIFT 0 // Shift amount for retrieving.
  97. #define TTFLAG_SMOOTH_AMOUNT_2X 0x00010000 // Blur / supersample [2x]
  98. #define TTFLAG_SMOOTH_AMOUNT_4X 0x00020000 // Blur / supersample [4x]
  99. #define TTFFLAG_SMOOTH_AMOUNT_MASK 0x000f0000 // Mask for retrieving.
  100. #define TTFFLAG_SMOOTH_AMOUNT_SHIFT 16 // Shift amount for retrieving.
  101. #define TTFFLAG_CREATE(smooth, amount) ((((smooth) << TTFFLAG_SMOOTH_SHIFT) & TTFFLAG_SMOOTH_MASK) | (((amount) << TTFFLAG_SMOOTH_AMOUNT_SHIFT) & TTFFLAG_SMOOTH_AMOUNT_MASK))
  102. #define TTFFLAG_GET_SMOOTH(flag) (((flag) & TTFLAG_SMOOTH_MASK) >> TTFFLAG_SMOOTH_SHIFT)
  103. #define TTFFLAG_GET_SMOOTH_AMOUNT(flag) (((flag) & TTFLAG_SMOOTH_SMOUNT_MASK) >> TTFFLAG_SMOOTH_AMOUNT_SHIFT)
  104. //////////////////////////////////////////////////////////////////////////
  105. struct STextDrawContext
  106. {
  107. unsigned int m_fxIdx;
  108. Vec2 m_size;
  109. Vec2i m_requestSize;
  110. float m_widthScale;
  111. float m_lineSpacing;
  112. float m_clipX;
  113. float m_clipY;
  114. float m_clipWidth;
  115. float m_clipHeight;
  116. int m_drawTextFlags;
  117. bool m_proportional;
  118. bool m_sizeIn800x600;
  119. bool m_clippingEnabled;
  120. bool m_framed;
  121. ColorB m_colorOverride;
  122. Matrix34 m_transform;
  123. int m_baseState;
  124. bool m_overrideViewProjMatrices;
  125. bool m_kerningEnabled;
  126. bool m_processSpecialChars;
  127. bool m_pixelAligned; //!< toggles whether rendering is pixel aligned
  128. float m_tracking; //!< extra space between characters in pixels (prior to any transform)
  129. STextDrawContext()
  130. : m_fxIdx(0)
  131. , m_size(16.0f, 16.0f)
  132. , m_requestSize(static_cast<int32>(m_size.x), static_cast<int32>(m_size.y))
  133. , m_widthScale(1.0f)
  134. , m_lineSpacing(0.f)
  135. , m_clipX(0)
  136. , m_clipY(0)
  137. , m_clipWidth(0)
  138. , m_clipHeight(0)
  139. , m_proportional(true)
  140. , m_sizeIn800x600(true)
  141. , m_clippingEnabled(false)
  142. , m_framed(false)
  143. , m_colorOverride(0, 0, 0, 0)
  144. , m_drawTextFlags(0)
  145. , m_transform(IDENTITY)
  146. , m_baseState(-1) // indicates not set, would like to set to GS_DEPTHFUNC_LEQUAL but header dependencies preclude that
  147. , m_overrideViewProjMatrices(true) // the old behavior that overrides the currently set view and projection matrices
  148. , m_kerningEnabled(true)
  149. , m_processSpecialChars(true)
  150. , m_pixelAligned(true)
  151. , m_tracking(0.0f)
  152. {
  153. }
  154. void Reset() { *this = STextDrawContext(); }
  155. void SetEffect(unsigned int fxIdx) { m_fxIdx = fxIdx; }
  156. void SetSize(const Vec2& size) { m_size = size; }
  157. void SetCharWidthScale(float widthScale) { m_widthScale = widthScale; }
  158. void SetClippingRect(float x, float y, float width, float height) { m_clipX = x; m_clipY = y; m_clipWidth = width; m_clipHeight = height; }
  159. void SetProportional(bool proportional) { m_proportional = proportional; }
  160. void SetSizeIn800x600(bool sizeIn800x600) { m_sizeIn800x600 = sizeIn800x600; }
  161. void EnableClipping(bool enable) { m_clippingEnabled = enable; }
  162. void EnableFrame(bool enable) { m_framed = enable; }
  163. void SetColor(const ColorF& col) { m_colorOverride = col; }
  164. void SetFlags(int flags) { m_drawTextFlags = flags; }
  165. void SetTransform(const Matrix34& transform) { m_transform = transform; }
  166. void SetBaseState(int baseState) { m_baseState = baseState; }
  167. void SetOverrideViewProjMatrices(bool overrideViewProjMatrices) { m_overrideViewProjMatrices = overrideViewProjMatrices; }
  168. void SetLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
  169. float GetCharWidth() const { return m_size.x; }
  170. float GetCharHeight() const { return m_size.y; }
  171. float GetCharWidthScale() const { return m_widthScale; }
  172. int GetFlags() const { return m_drawTextFlags; }
  173. float GetLineSpacing() const { return m_lineSpacing; }
  174. bool IsColorOverridden() const { return m_colorOverride.a != 0; }
  175. };
  176. //////////////////////////////////////////////////////////////////////////////////////////////
  177. struct IFFont
  178. {
  179. // <interfuscator:shuffle>
  180. virtual ~IFFont() {}
  181. // We don't derive from CBaseResource currently, that could be an enhancement
  182. virtual int32 AddRef() = 0;
  183. virtual int32 Release() = 0;
  184. //! \brief Loads a font from a TTF file.
  185. //! \param pFontFilePath Path to font file
  186. //! \param width Desired width of font texture.
  187. //! \param height Desired width of font texture.
  188. //! \param widthNumSlots Number of character slots across the width (X-axis) of the font texture
  189. //! \param heightNumSlots Number of character slots across the height (Y-axis) of the font texture
  190. //! \param flags Flags governing font, such as smoothness (see TTFFLAG_CREATE).
  191. //! \param sizeRatio Scale to apply to the font size when storing glyphs in font texture slots.
  192. virtual bool Load(const char* pFontFilePath, unsigned int width, unsigned int height, unsigned int widthNumSlots, unsigned int heightNumSlots, unsigned int flags, float sizeRatio) = 0;
  193. //! \brief Loads a font from a XML file.
  194. //! \param pXMLFile Path to font XML file
  195. virtual bool Load(const char* pXMLFile) = 0;
  196. // Summary:
  197. // Frees internally memory internally allocated by Load().
  198. virtual void Free() = 0;
  199. // Summary:
  200. // Draws a formatted string (UTF-8).
  201. virtual void DrawString(float x, float y, const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  202. // Summary:
  203. // Draws a formatted string (UTF-8), taking z into account.
  204. virtual void DrawString(float x, float y, float z, const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  205. // Summary:
  206. // Computes the text size (UTF-8).
  207. virtual Vec2 GetTextSize(const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  208. // Description:
  209. // Computes virtual text-length (UTF-8) (because of special chars...).
  210. virtual size_t GetTextLength(const char* pStr, const bool asciiMultiLine) const = 0;
  211. // Description:
  212. // Wraps text based on specified maximum line width (UTF-8)
  213. virtual void WrapText(AZStd::string& result, float maxWidth, const char* pStr, const STextDrawContext& ctx) = 0;
  214. // Description:
  215. // useful for special feature rendering interleaved with fonts (e.g. box behind the text)
  216. virtual void GetGradientTextureCoord(float& minU, float& minV, float& maxU, float& maxV) const = 0;
  217. virtual unsigned int GetEffectId(const char* pEffectName) const = 0;
  218. virtual unsigned int GetNumEffects() const = 0;
  219. virtual const char* GetEffectName(unsigned int effectId) const = 0;
  220. virtual Vec2 GetMaxEffectOffset(unsigned int effectId) const = 0;
  221. virtual bool DoesEffectHaveTransparency(unsigned int effectId) const = 0;
  222. //! \brief Adds the given UTF-8 string of chars to this font's font texture.
  223. //! \param pChars String of UTF-8 chars to add to font texture
  224. //! \param glyphSizeX Width (in pixels) of the characters to be rendered at in the font texture.
  225. //! \param glyphSizeY Height (in pixels) of the characters to be rendered at in the font texture.
  226. virtual void AddCharsToFontTexture(const char* pChars, int glyphSizeX = ICryFont::defaultGlyphSizeX, int glyphSizeY = ICryFont::defaultGlyphSizeY) = 0;
  227. //! \brief Returns XY kerning offsets (positive or negative) for two given glyphs.
  228. //!
  229. //! Kerning values are only returned for fonts that have a 'kern' table defined.
  230. //! Even fonts that do have a 'kern' defined do not define kerning values for all
  231. //! possible combination of characters. Zero values will be returned for those
  232. //! cases.
  233. virtual Vec2 GetKerning(uint32_t leftGlyph, uint32_t rightGlyph, const STextDrawContext& ctx) const = 0;
  234. //! \brief Returns the ascender of the font
  235. virtual float GetAscender(const STextDrawContext& ctx) const = 0;
  236. //! \brief Returns the y offset from the top to the baseline
  237. virtual float GetBaseline(const STextDrawContext& ctx) const = 0;
  238. //! \brief Returns the scaling applied to glyphs before being rendered to the font texture from FreeType.
  239. virtual float GetSizeRatio() const = 0;
  240. //! \brief Get the number of quads required to render the text string
  241. //! This is an upper limit. Due to clipping the rendering of the text string might actually use less quads.
  242. virtual uint32 GetNumQuadsForText(const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  243. //! \brief Write the quads for the text into the given vertex and index buffers
  244. //! The actual number of quads written to the buffer is returned
  245. virtual uint32 WriteTextQuadsToBuffers(SVF_P2F_C4B_T2F_F4B* verts, uint16* indices, uint32 maxQuads,
  246. float x, float y, float z, const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  247. //! \brief get the font texture for this font, will return -1 if there is no valid font texture
  248. virtual int GetFontTextureId() = 0;
  249. //! \brief get the font texture version for this font. Incremented each time the texture is changed
  250. virtual uint32 GetFontTextureVersion() = 0;
  251. // </interfuscator:shuffle>
  252. };
  253. //////////////////////////////////////////////////////////////////////////////////////////////
  254. //! Correlates several font files to define a family of fonts, specifically for styling.
  255. struct FontFamily
  256. {
  257. FontFamily()
  258. : normal(nullptr)
  259. , bold(nullptr)
  260. , italic(nullptr)
  261. , boldItalic(nullptr)
  262. {
  263. }
  264. FontFamily(const FontFamily&) = delete;
  265. FontFamily(const FontFamily&&) = delete;
  266. FontFamily& operator=(const FontFamily&) = delete;
  267. FontFamily& operator=(const FontFamily&&) = delete;
  268. AZStd::string familyName;
  269. IFFont* normal;
  270. IFFont* bold;
  271. IFFont* italic;
  272. IFFont* boldItalic;
  273. };
  274. //////////////////////////////////////////////////////////////////////////
  275. struct IFFont_RenderProxy
  276. {
  277. // <interfuscator:shuffle>
  278. virtual ~IFFont_RenderProxy() {}
  279. virtual void RenderCallback(float x, float y, float z, const char* pStr, const bool asciiMultiLine, const STextDrawContext& ctx) = 0;
  280. // </interfuscator:shuffle>
  281. };
  282. //////////////////////////////////////////////////////////////////////////
  283. //! Simple bus that notifies listeners of font changes
  284. class FontNotifications
  285. : public AZ::EBusTraits
  286. {
  287. public:
  288. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
  289. virtual ~FontNotifications() = default;
  290. virtual void OnFontsReloaded() = 0;
  291. virtual void OnFontTextureUpdated(IFFont* /*font*/) {}
  292. };
  293. using FontNotificationBus = AZ::EBus<FontNotifications>;
  294. #endif // CRYINCLUDE_CRYCOMMON_IFONT_H