[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / guilib / GUIFontTTF.h
blob2ed270343d4715e48583caf258dead47b5f7111b
1 /*
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.
7 */
9 #pragma once
11 #include "GUIFont.h"
12 #include "utils/ColorUtils.h"
13 #include "utils/Geometry.h"
15 #include <memory>
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
20 #include <ft2build.h>
21 #include FT_FREETYPE_H
22 #include <harfbuzz/hb.h>
24 #ifdef HAS_DX
25 #include <DirectXMath.h>
26 #include <DirectXPackedVector.h>
28 using namespace DirectX;
29 using namespace DirectX::PackedVector;
30 #endif
32 class CGraphicContext;
33 class CTexture;
34 class CRenderSystemBase;
36 struct FT_FaceRec_;
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;
51 /*!
52 \ingroup textures
53 \brief
56 #ifdef HAS_DX
57 struct SVertex
59 float x, y, z;
60 XMFLOAT4 col;
61 float u, v;
62 float u2, v2;
64 #else
65 struct SVertex
67 float x, y, z;
68 unsigned char r, g, b, a;
69 float u, v;
71 #endif
73 #include "GUIFontCache.h"
76 class CGUIFontTTF
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;
85 public:
86 virtual ~CGUIFontTTF();
88 static CGUIFontTTF* CreateGUIFontTTF(const std::string& fontIdent);
90 void Clear();
92 bool Load(const std::string& strFilename,
93 float height = 20.0f,
94 float aspect = 1.0f,
95 float lineSpacing = 1.0f,
96 bool border = false);
98 void Begin();
99 void End();
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
103 assert(false);
104 return CVertexBuffer();
106 virtual void DestroyVertexBuffer(CVertexBuffer& bufferHandle) const {}
108 const std::string& GetFontIdent() const { return m_fontIdent; }
110 protected:
111 explicit CGUIFontTTF(const std::string& fontIdent);
113 struct Glyph
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)
124 struct Character
126 short m_offsetX;
127 short m_offsetY;
128 float m_left;
129 float m_top;
130 float m_right;
131 float m_bottom;
132 float m_advance;
133 FT_UInt m_glyphIndex;
134 character_t m_glyphAndStyle;
137 struct RunInfo
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;
147 void AddReference();
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,
161 float x,
162 float y,
163 const std::vector<UTILS::COLOR::Color>& colors,
164 const vecText& text,
165 uint32_t alignment,
166 float maxPixelWidth,
167 bool scrolling);
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,
175 float posX,
176 float posY,
177 const Character* ch,
178 UTILS::COLOR::Color color,
179 bool roundX,
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,
185 unsigned int x1,
186 unsigned int y1,
187 unsigned int x2,
188 unsigned int y2) = 0;
189 virtual void DeleteHardwareTexture() = 0;
191 // modifying glyphs
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
201 int m_posY{0};
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
225 // freetype stuff
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
238 float m_translateX;
239 float m_translateY;
240 float m_translateZ;
241 const CVertexBuffer* m_vertexBuffer;
242 CRect m_clip;
243 CTranslatedVertices(float translateX,
244 float translateY,
245 float translateZ,
246 const CVertexBuffer* vertexBuffer,
247 const CRect& clip)
248 : m_translateX(translateX),
249 m_translateY(translateY),
250 m_translateZ(translateZ),
251 m_vertexBuffer(vertexBuffer),
252 m_clip(clip)
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;
263 std::vector<uint8_t>
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;
271 private:
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};