[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / guilib / GUITextLayout.h
blobbe365469d8d6e3b4619f6dcffca4d11c3ecd4115
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 "utils/ColorUtils.h"
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
17 #ifdef __GNUC__
18 // under gcc, inline will only take place if optimizations are applied (-O). this will force inline even without optimizations.
19 #define XBMC_FORCE_INLINE __attribute__((always_inline))
20 #else
21 #define XBMC_FORCE_INLINE
22 #endif
24 class CGUIFont;
25 class CScrollInfo;
27 // Process will be:
29 // 1. String is divided up into a "multiinfo" vector via the infomanager.
30 // 2. The multiinfo vector is then parsed by the infomanager at rendertime and the resultant string is constructed.
31 // 3. This is saved for comparison perhaps. If the same, we are done. If not, go to 4.
32 // 4. The string is then parsed into a vector<CGUIString>.
33 // 5. Each item in the vector is length-calculated, and then layout occurs governed by alignment and wrapping rules.
34 // 6. A new vector<CGUIString> is constructed
36 typedef uint32_t character_t;
37 typedef std::vector<character_t> vecText;
39 class CGUIString
41 public:
42 typedef vecText::const_iterator iString;
44 CGUIString(iString start, iString end, bool carriageReturn);
46 std::string GetAsString() const;
48 // The text is UTF-16 and the data stored in a character_t hold multiple informations by bits:
49 // <16 bits: are unicode code bits
50 // 16-24 bits: are color bits
51 // 24-32 bits: are style bits (see FONT_STYLE_* flags)
52 vecText m_text;
53 bool m_carriageReturn; // true if we have a carriage return here
56 class CGUITextLayout
58 public:
59 CGUITextLayout(CGUIFont *font, bool wrap, float fHeight=0.0f, CGUIFont *borderFont = NULL); // this may need changing - we may just use this class to replace CLabelInfo completely
61 bool UpdateScrollinfo(CScrollInfo &scrollInfo);
63 // main function to render strings
64 void Render(float x,
65 float y,
66 float angle,
67 UTILS::COLOR::Color color,
68 UTILS::COLOR::Color shadowColor,
69 uint32_t alignment,
70 float maxWidth,
71 bool solid = false);
72 void RenderScrolling(float x,
73 float y,
74 float angle,
75 UTILS::COLOR::Color color,
76 UTILS::COLOR::Color shadowColor,
77 uint32_t alignment,
78 float maxWidth,
79 const CScrollInfo& scrollInfo);
80 void RenderOutline(float x,
81 float y,
82 UTILS::COLOR::Color color,
83 UTILS::COLOR::Color outlineColor,
84 uint32_t alignment,
85 float maxWidth);
87 /*! \brief Returns the precalculated width and height of the text to be rendered (in constant time).
88 \param width [out] width of text
89 \param height [out] height of text
90 \sa GetTextWidth, CalcTextExtent
92 void GetTextExtent(float &width, float &height) const;
94 /*! \brief Returns the precalculated width of the text to be rendered (in constant time).
95 \return width of text
96 \sa GetTextExtent, CalcTextExtent
98 float GetTextWidth() const { return m_textWidth; }
100 float GetTextWidth(const std::wstring &text) const;
102 float GetTextWidth(const vecText& text) const;
104 /*! \brief Returns the precalculated height of the text to be rendered (in constant time).
105 \return height of text
107 float GetTextHeight() const { return m_textHeight; }
109 bool Update(const std::string &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
110 bool UpdateW(const std::wstring &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
112 /*! \brief Update text from a pre-styled vecText/std::vector<UTILS::COLOR::Color> combination
113 Allows styled text to be passed directly to the text layout.
114 \param text the styled text to set.
115 \param colors the colors used on the text.
116 \param maxWidth the maximum width for wrapping text, defaults to 0 (no max width).
117 \param forceLTRReadingOrder whether to force left to right reading order, defaults to false.
119 void UpdateStyled(const vecText& text,
120 const std::vector<UTILS::COLOR::Color>& colors,
121 float maxWidth = 0,
122 bool forceLTRReadingOrder = false);
124 unsigned int GetTextLength() const;
125 void GetFirstText(vecText &text) const;
126 void Reset();
128 void SetWrap(bool bWrap=true);
129 void SetMaxHeight(float fHeight);
132 static void DrawText(CGUIFont* font,
133 float x,
134 float y,
135 UTILS::COLOR::Color color,
136 UTILS::COLOR::Color shadowColor,
137 const std::string& text,
138 uint32_t align);
139 static void Filter(std::string &text);
141 protected:
142 void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
143 void WrapText(const vecText &text, float maxWidth);
144 static void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
145 static std::wstring BidiFlip(const std::wstring& text,
146 bool forceLTRReadingOrder,
147 int* visualToLogicalMap = nullptr);
148 void CalcTextExtent();
149 void UpdateCommon(const std::wstring &text, float maxWidth, bool forceLTRReadingOrder);
151 /*! \brief Returns the text, utf8 encoded
152 \return utf8 text
154 std::string GetText() const;
156 //! \brief Set the monospaced font to use
157 void SetMonoFont(CGUIFont* font) { m_monoFont = font; }
159 //! \brief Set whether or not to use the monospace font
160 void UseMonoFont(bool use) { m_font = use && m_monoFont ? m_monoFont : m_varFont; }
162 // our text to render
163 std::vector<UTILS::COLOR::Color> m_colors;
164 std::vector<CGUIString> m_lines;
165 typedef std::vector<CGUIString>::iterator iLine;
167 // the layout and font details
168 CGUIFont *m_font; // has style, colour info
169 CGUIFont *m_borderFont; // only used for outlined text
170 CGUIFont* m_monoFont = nullptr; //!< Mono-space font to use
171 CGUIFont* m_varFont; //!< Varible-space font to use
173 bool m_wrap; // wrapping (true if justify is enabled!)
174 float m_maxHeight;
175 // the default color (may differ from the font objects defaults)
176 UTILS::COLOR::Color m_textColor;
178 std::string m_lastUtf8Text;
179 std::wstring m_lastText;
180 bool m_lastUpdateW; ///< true if the last string we updated was the wstring version
181 float m_textWidth;
182 float m_textHeight;
183 private:
184 inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
186 return (letter & 0xffff) == L' ';
188 inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
190 character_t ch = letter & 0xffff;
191 return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
193 static void AppendToUTF32(const std::string &utf8, character_t colStyle, vecText &utf32);
194 static void AppendToUTF32(const std::wstring &utf16, character_t colStyle, vecText &utf32);
195 static void ParseText(const std::wstring& text,
196 uint32_t defaultStyle,
197 UTILS::COLOR::Color defaultColor,
198 std::vector<UTILS::COLOR::Color>& colors,
199 vecText& parsedText);