xbmc
GUITextLayout.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "utils/ColorUtils.h"
12 
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
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
23 
24 class CGUIFont;
25 class CScrollInfo;
26 
27 // Process will be:
28 
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
35 
36 typedef uint32_t character_t;
37 typedef std::vector<character_t> vecText;
38 
40 {
41 public:
42  typedef vecText::const_iterator iString;
43 
44  CGUIString(iString start, iString end, bool carriageReturn);
45 
46  std::string GetAsString() const;
47  std::wstring GetAsWstring() const;
48 
49  // The text is UTF-16 and the data stored in a character_t hold multiple informations by bits:
50  // <16 bits: are unicode code bits
51  // 16-24 bits: are color bits
52  // 24-32 bits: are style bits (see FONT_STYLE_* flags)
53  vecText m_text;
54  bool m_carriageReturn; // true if we have a carriage return here
55 };
56 
58 {
59 public:
60  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 
62  bool UpdateScrollinfo(CScrollInfo &scrollInfo);
63 
64  // main function to render strings
65  void Render(float x,
66  float y,
67  float angle,
68  UTILS::COLOR::Color color,
69  UTILS::COLOR::Color shadowColor,
70  uint32_t alignment,
71  float maxWidth,
72  bool solid = false);
73  void RenderScrolling(float x,
74  float y,
75  float angle,
76  UTILS::COLOR::Color color,
77  UTILS::COLOR::Color shadowColor,
78  uint32_t alignment,
79  float maxWidth,
80  const CScrollInfo& scrollInfo);
81  void RenderOutline(float x,
82  float y,
83  UTILS::COLOR::Color color,
84  UTILS::COLOR::Color outlineColor,
85  uint32_t alignment,
86  float maxWidth);
87 
93  void GetTextExtent(float &width, float &height) const;
94 
99  float GetTextWidth() const { return m_textWidth; }
100 
101  float GetTextWidth(const std::wstring &text) const;
102 
106  float GetTextHeight() const { return m_textHeight; }
107 
108  bool Update(const std::string &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
109  bool UpdateW(const std::wstring &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
110 
118  void UpdateStyled(const vecText& text,
119  const std::vector<UTILS::COLOR::Color>& colors,
120  float maxWidth = 0,
121  bool forceLTRReadingOrder = false);
122 
123  unsigned int GetTextLength() const;
124  void GetFirstText(vecText &text) const;
125  void Reset();
126 
127  void SetWrap(bool bWrap=true);
128  void SetMaxHeight(float fHeight);
129 
130 
131  static void DrawText(CGUIFont* font,
132  float x,
133  float y,
134  UTILS::COLOR::Color color,
135  UTILS::COLOR::Color shadowColor,
136  const std::string& text,
137  uint32_t align);
138  static void Filter(std::string &text);
139 
140 protected:
141  void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
142  void WrapText(const vecText &text, float maxWidth);
143  static void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
144  static std::wstring BidiFlip(const std::wstring& text,
145  bool forceLTRReadingOrder,
146  int* visualToLogicalMap = nullptr);
147  void CalcTextExtent();
148  void UpdateCommon(const std::wstring &text, float maxWidth, bool forceLTRReadingOrder);
149 
153  std::string GetText() const;
154 
156  void SetMonoFont(CGUIFont* font) { m_monoFont = font; }
157 
159  void UseMonoFont(bool use) { m_font = use && m_monoFont ? m_monoFont : m_varFont; }
160 
161  // our text to render
162  std::vector<UTILS::COLOR::Color> m_colors;
163  std::vector<CGUIString> m_lines;
164  typedef std::vector<CGUIString>::iterator iLine;
165 
166  // the layout and font details
167  CGUIFont *m_font; // has style, colour info
168  CGUIFont *m_borderFont; // only used for outlined text
169  CGUIFont* m_monoFont = nullptr;
171 
172  bool m_wrap; // wrapping (true if justify is enabled!)
173  float m_maxHeight;
174  // the default color (may differ from the font objects defaults)
175  UTILS::COLOR::Color m_textColor;
176 
177  std::string m_lastUtf8Text;
178  std::wstring m_lastText;
180  float m_textWidth;
181  float m_textHeight;
182 private:
183  inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
184  {
185  return (letter & 0xffff) == L' ';
186  };
187  inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
188  {
189  character_t ch = letter & 0xffff;
190  return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
191  };
192  static void AppendToUTF32(const std::string &utf8, character_t colStyle, vecText &utf32);
193  static void AppendToUTF32(const std::wstring &utf16, character_t colStyle, vecText &utf32);
194  static void ParseText(const std::wstring& text,
195  uint32_t defaultStyle,
196  UTILS::COLOR::Color defaultColor,
197  std::vector<UTILS::COLOR::Color>& colors,
198  vecText& parsedText);
199 };
200 
void SetMonoFont(CGUIFont *font)
Set the monospaced font to use.
Definition: GUITextLayout.h:156
void UseMonoFont(bool use)
Set whether or not to use the monospace font.
Definition: GUITextLayout.h:159
Definition: GUIFont.h:58
Definition: GUITextLayout.h:57
float GetTextWidth() const
Returns the precalculated width of the text to be rendered (in constant time).
Definition: GUITextLayout.h:99
bool m_lastUpdateW
true if the last string we updated was the wstring version
Definition: GUITextLayout.h:179
Definition: GUIFont.h:107
Definition: GUITextLayout.h:39
float GetTextHeight() const
Returns the precalculated height of the text to be rendered (in constant time).
Definition: GUITextLayout.h:106
CGUIFont * m_varFont
Varible-space font to use.
Definition: GUITextLayout.h:170