4 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
23 #include "utils/StdString.h"
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))
31 #define XBMC_FORCE_INLINE
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
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;
54 typedef vecText::const_iterator iString;
56 CGUIString(iString start, iString end, bool carriageReturn);
58 CStdString GetAsString() const;
61 bool m_carriageReturn; // true if we have a carriage return here
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
69 bool UpdateScrollinfo(CScrollInfo &scrollInfo);
71 // main function to render strings
72 void Render(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, bool solid = false);
73 void RenderScrolling(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, const CScrollInfo &scrollInfo);
74 void RenderOutline(float x, float y, color_t color, color_t outlineColor, uint32_t alignment, float maxWidth);
76 /*! \brief Returns the precalculated width and height of the text to be rendered (in constant time).
77 \param width [out] width of text
78 \param height [out] height of text
79 \sa GetTextWidth, CalcTextExtent
81 void GetTextExtent(float &width, float &height) const;
83 /*! \brief Returns the precalculated width of the text to be rendered (in constant time).
85 \sa GetTextExtent, CalcTextExtent
87 float GetTextWidth() const { return m_textWidth; };
89 float GetTextWidth(const CStdStringW &text) const;
90 bool Update(const CStdString &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
91 bool UpdateW(const CStdStringW &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
93 /*! \brief Update text from a pre-styled vecText/vecColors combination
94 Allows styled text to be passed directly to the text layout.
95 \param text the styled text to set.
96 \param colors the colors used on the text.
97 \param maxWidth the maximum width for wrapping text, defaults to 0 (no max width).
98 \param forceLTRReadingOrder whether to force left to right reading order, defaults to false.
100 void UpdateStyled(const vecText &text, const vecColors &colors, float maxWidth = 0, bool forceLTRReadingOrder = false);
102 unsigned int GetTextLength() const;
103 void GetFirstText(vecText &text) const;
106 void SetWrap(bool bWrap=true);
107 void SetMaxHeight(float fHeight);
110 static void DrawText(CGUIFont *font, float x, float y, color_t color, color_t shadowColor, const CStdString &text, uint32_t align);
111 static void Filter(CStdString &text);
114 void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
115 void WrapText(const vecText &text, float maxWidth);
116 static void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
117 static CStdStringW BidiFlip(const CStdStringW &text, bool forceLTRReadingOrder);
118 void CalcTextExtent();
120 // our text to render
122 std::vector<CGUIString> m_lines;
123 typedef std::vector<CGUIString>::iterator iLine;
125 // the layout and font details
126 CGUIFont *m_font; // has style, colour info
127 CGUIFont *m_borderFont; // only used for outlined text
129 bool m_wrap; // wrapping (true if justify is enabled!)
131 // the default color (may differ from the font objects defaults)
134 CStdStringW m_lastText;
138 inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
140 return (letter & 0xffff) == L' ';
142 inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
144 character_t ch = letter & 0xffff;
145 return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
147 static void AppendToUTF32(const CStdString &utf8, character_t colStyle, vecText &utf32);
148 static void AppendToUTF32(const CStdStringW &utf16, character_t colStyle, vecText &utf32);
149 static void ParseText(const CStdStringW &text, uint32_t defaultStyle, color_t defaultColor, vecColors &colors, vecText &parsedText);
151 static void utf8ToW(const CStdString &utf8, CStdStringW &utf16);