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 
48  vecText m_text;
49  bool m_carriageReturn; // true if we have a carriage return here
50 };
51 
53 {
54 public:
55  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
56 
57  bool UpdateScrollinfo(CScrollInfo &scrollInfo);
58 
59  // main function to render strings
60  void Render(float x,
61  float y,
62  float angle,
63  UTILS::COLOR::Color color,
64  UTILS::COLOR::Color shadowColor,
65  uint32_t alignment,
66  float maxWidth,
67  bool solid = false);
68  void RenderScrolling(float x,
69  float y,
70  float angle,
71  UTILS::COLOR::Color color,
72  UTILS::COLOR::Color shadowColor,
73  uint32_t alignment,
74  float maxWidth,
75  const CScrollInfo& scrollInfo);
76  void RenderOutline(float x,
77  float y,
78  UTILS::COLOR::Color color,
79  UTILS::COLOR::Color outlineColor,
80  uint32_t alignment,
81  float maxWidth);
82 
88  void GetTextExtent(float &width, float &height) const;
89 
94  float GetTextWidth() const { return m_textWidth; }
95 
96  float GetTextWidth(const std::wstring &text) const;
97 
101  float GetTextHeight() const { return m_textHeight; }
102 
103  bool Update(const std::string &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
104  bool UpdateW(const std::wstring &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
105 
113  void UpdateStyled(const vecText& text,
114  const std::vector<UTILS::COLOR::Color>& colors,
115  float maxWidth = 0,
116  bool forceLTRReadingOrder = false);
117 
118  unsigned int GetTextLength() const;
119  void GetFirstText(vecText &text) const;
120  void Reset();
121 
122  void SetWrap(bool bWrap=true);
123  void SetMaxHeight(float fHeight);
124 
125 
126  static void DrawText(CGUIFont* font,
127  float x,
128  float y,
129  UTILS::COLOR::Color color,
130  UTILS::COLOR::Color shadowColor,
131  const std::string& text,
132  uint32_t align);
133  static void Filter(std::string &text);
134 
135 protected:
136  void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
137  void WrapText(const vecText &text, float maxWidth);
138  static void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
139  static std::wstring BidiFlip(const std::wstring& text,
140  bool forceLTRReadingOrder,
141  int* visualToLogicalMap = nullptr);
142  void CalcTextExtent();
143  void UpdateCommon(const std::wstring &text, float maxWidth, bool forceLTRReadingOrder);
144 
148  std::string GetText() const;
149 
151  void SetMonoFont(CGUIFont* font) { m_monoFont = font; }
152 
154  void UseMonoFont(bool use) { m_font = use && m_monoFont ? m_monoFont : m_varFont; }
155 
156  // our text to render
157  std::vector<UTILS::COLOR::Color> m_colors;
158  std::vector<CGUIString> m_lines;
159  typedef std::vector<CGUIString>::iterator iLine;
160 
161  // the layout and font details
162  CGUIFont *m_font; // has style, colour info
163  CGUIFont *m_borderFont; // only used for outlined text
164  CGUIFont* m_monoFont = nullptr;
166 
167  bool m_wrap; // wrapping (true if justify is enabled!)
168  float m_maxHeight;
169  // the default color (may differ from the font objects defaults)
170  UTILS::COLOR::Color m_textColor;
171 
172  std::string m_lastUtf8Text;
173  std::wstring m_lastText;
175  float m_textWidth;
176  float m_textHeight;
177 private:
178  inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
179  {
180  return (letter & 0xffff) == L' ';
181  };
182  inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
183  {
184  character_t ch = letter & 0xffff;
185  return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
186  };
187  static void AppendToUTF32(const std::string &utf8, character_t colStyle, vecText &utf32);
188  static void AppendToUTF32(const std::wstring &utf16, character_t colStyle, vecText &utf32);
189  static void ParseText(const std::wstring& text,
190  uint32_t defaultStyle,
191  UTILS::COLOR::Color defaultColor,
192  std::vector<UTILS::COLOR::Color>& colors,
193  vecText& parsedText);
194 };
195 
void SetMonoFont(CGUIFont *font)
Set the monospaced font to use.
Definition: GUITextLayout.h:151
void UseMonoFont(bool use)
Set whether or not to use the monospace font.
Definition: GUITextLayout.h:154
Definition: GUIFont.h:58
Definition: GUITextLayout.h:52
float GetTextWidth() const
Returns the precalculated width of the text to be rendered (in constant time).
Definition: GUITextLayout.h:94
bool m_lastUpdateW
true if the last string we updated was the wstring version
Definition: GUITextLayout.h:174
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:101
CGUIFont * m_varFont
Varible-space font to use.
Definition: GUITextLayout.h:165