2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
12 #include "utils/ColorUtils.h"
13 #include "utils/Geometry.h"
21 #include FT_FREETYPE_H
22 #include <harfbuzz/hb.h>
25 #include <DirectXMath.h>
26 #include <DirectXPackedVector.h>
28 using namespace DirectX
;
29 using namespace DirectX::PackedVector
;
32 class CGraphicContext
;
34 class CRenderSystemBase
;
37 struct FT_LibraryRec_
;
38 struct FT_GlyphSlotRec_
;
39 struct FT_BitmapGlyphRec_
;
40 struct FT_StrokerRec_
;
42 typedef struct FT_FaceRec_
* FT_Face
;
43 typedef struct FT_LibraryRec_
* FT_Library
;
44 typedef struct FT_GlyphSlotRec_
* FT_GlyphSlot
;
45 typedef struct FT_BitmapGlyphRec_
* FT_BitmapGlyph
;
46 typedef struct FT_StrokerRec_
* FT_Stroker
;
48 typedef uint32_t character_t
;
49 typedef std::vector
<character_t
> vecText
;
68 unsigned char r
, g
, b
, a
;
73 #include "GUIFontCache.h"
78 // use lookup table for the first 4096 glyphs (almost any letter or symbol) to
79 // speed up GUI rendering and decrease CPU usage and less memory reallocations
80 static constexpr int MAX_GLYPH_IDX
= 4096;
81 static constexpr size_t LOOKUPTABLE_SIZE
= MAX_GLYPH_IDX
* FONT_STYLES_COUNT
;
83 friend class CGUIFont
;
86 virtual ~CGUIFontTTF();
88 static CGUIFontTTF
* CreateGUIFontTTF(const std::string
& fontIdent
);
92 bool Load(const std::string
& strFilename
,
95 float lineSpacing
= 1.0f
,
100 /* The next two should only be called if we've declared we can do hardware clipping */
101 virtual CVertexBuffer
CreateVertexBuffer(const std::vector
<SVertex
>& vertices
) const
104 return CVertexBuffer();
106 virtual void DestroyVertexBuffer(CVertexBuffer
& bufferHandle
) const {}
108 const std::string
& GetFontIdent() const { return m_fontIdent
; }
111 explicit CGUIFontTTF(const std::string
& fontIdent
);
115 hb_glyph_info_t m_glyphInfo
{};
116 hb_glyph_position_t m_glyphPosition
{};
118 Glyph(const hb_glyph_info_t
& glyphInfo
, const hb_glyph_position_t
& glyphPosition
)
119 : m_glyphInfo(glyphInfo
), m_glyphPosition(glyphPosition
)
133 FT_UInt m_glyphIndex
;
134 character_t m_glyphAndStyle
;
139 unsigned int m_startOffset
;
140 unsigned int m_endOffset
;
141 hb_buffer_t
* m_buffer
;
142 hb_script_t m_script
;
143 hb_glyph_info_t
* m_glyphInfos
;
144 hb_glyph_position_t
* m_glyphPositions
;
148 void RemoveReference();
150 std::vector
<Glyph
> GetHarfBuzzShapedGlyphs(const vecText
& text
);
152 float GetTextWidthInternal(const vecText
& text
);
153 float GetTextWidthInternal(const vecText
& text
, const std::vector
<Glyph
>& glyph
);
154 float GetCharWidthInternal(character_t ch
);
155 float GetTextHeight(float lineSpacing
, int numLines
) const;
156 float GetTextBaseLine() const { return static_cast<float>(m_cellBaseLine
); }
157 float GetLineHeight(float lineSpacing
) const;
158 float GetFontHeight() const { return m_height
; }
160 void DrawTextInternal(CGraphicContext
& context
,
163 const std::vector
<UTILS::COLOR::Color
>& colors
,
169 float m_height
{0.0f
};
171 // Stuff for pre-rendering for speed
172 Character
* GetCharacter(character_t letter
, FT_UInt glyphIndex
);
173 bool CacheCharacter(FT_UInt glyphIndex
, uint32_t style
, Character
* ch
);
174 void RenderCharacter(CGraphicContext
& context
,
178 UTILS::COLOR::Color color
,
180 std::vector
<SVertex
>& vertices
);
181 void ClearCharacterCache();
183 virtual std::unique_ptr
<CTexture
> ReallocTexture(unsigned int& newHeight
) = 0;
184 virtual bool CopyCharToTexture(FT_BitmapGlyph bitGlyph
,
188 unsigned int y2
) = 0;
189 virtual void DeleteHardwareTexture() = 0;
192 void SetGlyphStrength(FT_GlyphSlot slot
, int glyphStrength
);
193 static void ObliqueGlyph(FT_GlyphSlot slot
);
195 std::unique_ptr
<CTexture
>
196 m_texture
; // texture that holds our rendered characters (8bit alpha only)
198 unsigned int m_textureWidth
{0}; // width of our texture
199 unsigned int m_textureHeight
{0}; // height of our texture
200 int m_posX
{0}; // current position in the texture
203 /*! \brief the height of each line in the texture.
204 Accounts for spacing between lines to avoid characters overlapping.
206 unsigned int GetTextureLineHeight() const;
207 unsigned int GetMaxFontHeight() const;
209 UTILS::COLOR::Color m_color
{UTILS::COLOR::NONE
};
211 std::vector
<Character
> m_char
; // our characters
213 // room for the first MAX_GLYPH_IDX glyphs in 7 styles
214 Character
* m_charquick
[LOOKUPTABLE_SIZE
]{nullptr};
216 bool m_ellipseCached
{false};
217 float m_ellipsesWidth
{0.0f
}; // this is used every character (width of '.')
219 unsigned int m_cellBaseLine
{0};
220 unsigned int m_cellHeight
{0};
221 unsigned int m_maxFontHeight
{0};
223 unsigned int m_nestedBeginCount
{0}; // speedups
226 FT_Face m_face
{nullptr};
227 FT_Stroker m_stroker
{nullptr};
229 hb_font_t
* m_hbFont
{nullptr};
231 float m_originX
{0.0f
};
232 float m_originY
{0.0f
};
234 unsigned int m_nTexture
{0};
236 struct CTranslatedVertices
241 const CVertexBuffer
* m_vertexBuffer
;
243 CTranslatedVertices(float translateX
,
246 const CVertexBuffer
* vertexBuffer
,
248 : m_translateX(translateX
),
249 m_translateY(translateY
),
250 m_translateZ(translateZ
),
251 m_vertexBuffer(vertexBuffer
),
256 std::vector
<CTranslatedVertices
> m_vertexTrans
;
257 std::vector
<SVertex
> m_vertex
;
259 float m_textureScaleX
{0.0f
};
260 float m_textureScaleY
{0.0f
};
262 const std::string m_fontIdent
;
264 m_fontFileInMemory
; // used only in some cases, see CFreeTypeLibrary::GetFont()
266 CGUIFontCache
<CGUIFontCacheStaticPosition
, CGUIFontCacheStaticValue
> m_staticCache
;
267 CGUIFontCache
<CGUIFontCacheDynamicPosition
, CGUIFontCacheDynamicValue
> m_dynamicCache
;
269 CRenderSystemBase
* m_renderSystem
;
272 float GetTabSpaceLength();
274 virtual bool FirstBegin() = 0;
275 virtual void LastEnd() = 0;
276 CGUIFontTTF(const CGUIFontTTF
&) = delete;
277 CGUIFontTTF
& operator=(const CGUIFontTTF
&) = delete;
278 int m_referenceCount
{0};