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 // 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);
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
79 void GetTextExtent(float &width, float &height) const;
81 /*! \brief Returns the precalculated width of the text to be rendered (in constant time).
83 \sa GetTextExtent, CalcTextExtent
85 float GetTextWidth() const { return m_textWidth; };
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);
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.
98 void UpdateStyled(const vecText &text, const vecColors &colors, float maxWidth = 0, bool forceLTRReadingOrder = false);
100 unsigned int GetTextLength() const;
101 void GetFirstText(vecText &text) const;
104 void SetWrap(bool bWrap=true);
105 void SetMaxHeight(float fHeight);
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);
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();
118 // our text to render
120 std::vector<CGUIString> m_lines;
121 typedef std::vector<CGUIString>::iterator iLine;
123 // the layout and font details
124 CGUIFont *m_font; // has style, colour info
125 CGUIFont *m_borderFont; // only used for outlined text
127 bool m_wrap; // wrapping (true if justify is enabled!)
129 // the default color (may differ from the font objects defaults)
132 CStdStringW m_lastText;
136 inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
138 return (letter & 0xffff) == L' ';
140 inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
142 character_t ch = letter & 0xffff;
143 return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
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);
149 static void utf8ToW(const CStdString &utf8, CStdStringW &utf16);