Merge pull request #2810 from jmarshallnz/eval_conditions_before_actions
[vuplus_xbmc] / xbmc / guilib / GUITextLayout.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://www.xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "utils/StdString.h"
24
25 #include <vector>
26
27 #ifdef __GNUC__
28 // under gcc, inline will only take place if optimizations are applied (-O). this will force inline even without optimizations.
29 #define XBMC_FORCE_INLINE __attribute__((always_inline))
30 #else
31 #define XBMC_FORCE_INLINE
32 #endif
33
34 class CGUIFont;
35 class CScrollInfo;
36
37 // Process will be:
38
39 // 1.  String is divided up into a "multiinfo" vector via the infomanager.
40 // 2.  The multiinfo vector is then parsed by the infomanager at rendertime and the resultant string is constructed.
41 // 3.  This is saved for comparison perhaps.  If the same, we are done.  If not, go to 4.
42 // 4.  The string is then parsed into a vector<CGUIString>.
43 // 5.  Each item in the vector is length-calculated, and then layout occurs governed by alignment and wrapping rules.
44 // 6.  A new vector<CGUIString> is constructed
45
46 typedef uint32_t character_t;
47 typedef uint32_t color_t;
48 typedef std::vector<character_t> vecText;
49 typedef std::vector<color_t> vecColors;
50
51 class CGUIString
52 {
53 public:
54   typedef vecText::const_iterator iString;
55
56   CGUIString(iString start, iString end, bool carriageReturn);
57
58   CStdString GetAsString() const;
59
60   vecText m_text;
61   bool m_carriageReturn; // true if we have a carriage return here
62 };
63
64 class CGUITextLayout
65 {
66 public:
67   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
68
69   // main function to render strings
70   void Render(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, bool solid = false);
71   void RenderScrolling(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, CScrollInfo &scrollInfo);
72   void RenderOutline(float x, float y, color_t color, color_t outlineColor, uint32_t alignment, float maxWidth);
73
74   /*! \brief Returns the precalculated width and height of the text to be rendered (in constant time).
75    \param width [out] width of text
76    \param height [out] height of text
77    \sa GetTextWidth, CalcTextExtent
78    */
79   void GetTextExtent(float &width, float &height) const;
80   
81   /*! \brief Returns the precalculated width of the text to be rendered (in constant time).
82    \return width of text
83    \sa GetTextExtent, CalcTextExtent
84    */
85   float GetTextWidth() const { return m_textWidth; };
86   
87   float GetTextWidth(const CStdStringW &text) const;
88   bool Update(const CStdString &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
89   bool UpdateW(const CStdStringW &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
90
91   /*! \brief Update text from a pre-styled vecText/vecColors combination
92    Allows styled text to be passed directly to the text layout.
93    \param text the styled text to set.
94    \param colors the colors used on the text.
95    \param maxWidth the maximum width for wrapping text, defaults to 0 (no max width).
96    \param forceLTRReadingOrder whether to force left to right reading order, defaults to false.
97    */
98   void UpdateStyled(const vecText &text, const vecColors &colors, float maxWidth = 0, bool forceLTRReadingOrder = false);
99
100   unsigned int GetTextLength() const;
101   void GetFirstText(vecText &text) const;
102   void Reset();
103
104   void SetWrap(bool bWrap=true);
105   void SetMaxHeight(float fHeight);
106
107
108   static void DrawText(CGUIFont *font, float x, float y, color_t color, color_t shadowColor, const CStdString &text, uint32_t align);
109   static void Filter(CStdString &text);
110
111 protected:
112   void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
113   void WrapText(const vecText &text, float maxWidth);
114   void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
115   CStdStringW BidiFlip(const CStdStringW &text, bool forceLTRReadingOrder);
116   void CalcTextExtent();
117
118   // our text to render
119   vecColors m_colors;
120   std::vector<CGUIString> m_lines;
121   typedef std::vector<CGUIString>::iterator iLine;
122
123   // the layout and font details
124   CGUIFont *m_font;        // has style, colour info
125   CGUIFont *m_borderFont;  // only used for outlined text
126
127   bool  m_wrap;            // wrapping (true if justify is enabled!)
128   float m_maxHeight;
129   // the default color (may differ from the font objects defaults)
130   color_t m_textColor;
131
132   CStdStringW m_lastText;
133   float m_textWidth;
134   float m_textHeight;
135 private:
136   inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
137   {
138     return (letter & 0xffff) == L' ';
139   };
140   inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
141   {
142     character_t ch = letter & 0xffff;
143     return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
144   };
145   static void AppendToUTF32(const CStdString &utf8, character_t colStyle, vecText &utf32);
146   static void AppendToUTF32(const CStdStringW &utf16, character_t colStyle, vecText &utf32);
147   static void ParseText(const CStdStringW &text, uint32_t defaultStyle, color_t defaultColor, vecColors &colors, vecText &parsedText);
148
149   static void utf8ToW(const CStdString &utf8, CStdStringW &utf16);
150 };
151