Text.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Font.h"
  25. #include "Log.h"
  26. #include "Profiler.h"
  27. #include "ResourceCache.h"
  28. #include "Text.h"
  29. #include "Texture.h"
  30. #include "DebugNew.h"
  31. Text::Text(const std::string& text, const std::string& name) :
  32. UIElement(name),
  33. mFontSize(DEFAULT_FONT_SIZE),
  34. mMaxWidth(0),
  35. mText(text),
  36. mTextAlignment(HA_LEFT),
  37. mTextSpacing(1.0f)
  38. {
  39. }
  40. Text::~Text()
  41. {
  42. }
  43. void Text::setStyle(const XMLElement& element, ResourceCache* cache)
  44. {
  45. if (!cache)
  46. EXCEPTION("Null resource cache for UI element");
  47. UIElement::setStyle(element, cache);
  48. if (element.hasChildElement("font"))
  49. {
  50. XMLElement fontElem = element.getChildElement("font");
  51. setFont(cache->getResource<Font>(fontElem.getString("name")), fontElem.getInt("size"));
  52. }
  53. if (element.hasChildElement("maxwidth"))
  54. setMaxWidth(element.getChildElement("maxwidth").getInt("value"));
  55. if (element.hasChildElement("text"))
  56. setText(element.getChildElement("text").getString("value"));
  57. if (element.hasChildElement("textspacing"))
  58. setTextSpacing(element.getChildElement("textspacing").getFloat("value"));
  59. if (element.hasChildElement("textalignment"))
  60. {
  61. std::string horiz = element.getChildElement("textalignment").getStringLower("value");
  62. if (horiz == "left")
  63. setTextAlignment(HA_LEFT);
  64. if (horiz == "center")
  65. setTextAlignment(HA_CENTER);
  66. if (horiz == "right")
  67. setTextAlignment(HA_RIGHT);
  68. }
  69. }
  70. bool Text::setFont(Font* font, int size)
  71. {
  72. if (!font)
  73. {
  74. LOGERROR("Null font for Text");
  75. return false;
  76. }
  77. mFont = font;
  78. mFontSize = max(size, 1);
  79. // Catch exception if executed from script
  80. try
  81. {
  82. calculateTextSize();
  83. }
  84. catch (Exception& e)
  85. {
  86. mFont = 0;
  87. SAFE_RETHROW_RET(e, false);
  88. }
  89. return true;
  90. }
  91. void Text::setMaxWidth(int maxWidth)
  92. {
  93. mMaxWidth = max(maxWidth, 0);
  94. calculateTextSize();
  95. }
  96. void Text::setText(const std::string& text)
  97. {
  98. mText = text;
  99. calculateTextSize();
  100. }
  101. void Text::setTextAlignment(HorizontalAlignment align)
  102. {
  103. mTextAlignment = align;
  104. }
  105. void Text::setTextSpacing(float spacing)
  106. {
  107. mTextSpacing = max(spacing, 0.5f);
  108. }
  109. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  110. {
  111. if (!mFont)
  112. {
  113. mHovering = false;
  114. return;
  115. }
  116. const FontFace* face = mFont->getFace(mFontSize);
  117. UIBatch batch;
  118. batch.begin(&quads);
  119. batch.mBlendMode = BLEND_ALPHA;
  120. batch.mScissor = currentScissor;
  121. batch.mTexture = face->mTexture;
  122. unsigned rowIndex = 0;
  123. int x = getRowStartPosition(rowIndex);
  124. int y = 0;
  125. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  126. for (unsigned i = 0; i < mPrintText.length(); ++i)
  127. {
  128. unsigned char c = (unsigned char)mPrintText[i];
  129. if (c != '\n')
  130. {
  131. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  132. if (c != ' ')
  133. batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
  134. x += glyph.mAdvanceX;
  135. }
  136. else
  137. {
  138. rowIndex++;
  139. x = getRowStartPosition(rowIndex);
  140. y += rowHeight;
  141. }
  142. }
  143. UIBatch::addOrMerge(batch, batches);
  144. // Reset hovering for next frame
  145. mHovering = false;
  146. }
  147. int Text::getRowHeight() const
  148. {
  149. if (mFont)
  150. return mFont->getFace(mFontSize)->mRowHeight;
  151. else
  152. return 0;
  153. }
  154. void Text::calculateTextSize()
  155. {
  156. int width = 0;
  157. int height = 0;
  158. mRowWidths.clear();
  159. if (mFont)
  160. {
  161. const FontFace* face = mFont->getFace(mFontSize);
  162. int rowWidth = 0;
  163. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  164. // First see if the text must be split up
  165. if (!mMaxWidth)
  166. mPrintText = mText;
  167. else
  168. {
  169. unsigned nextBreak = 0;
  170. unsigned lineStart = 0;
  171. for (unsigned i = 0; i < mText.length(); ++i)
  172. {
  173. unsigned j;
  174. if (mText[i] != '\n')
  175. {
  176. bool ok = true;
  177. if (nextBreak <= i)
  178. {
  179. int futureRowWidth = rowWidth;
  180. for (j = i; j < mText.length(); ++j)
  181. {
  182. if ((mText[j] == ' ') || (mText[j] == '\n'))
  183. {
  184. nextBreak = j;
  185. break;
  186. }
  187. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  188. if ((mText[j] == '-') && (futureRowWidth <= mMaxWidth))
  189. {
  190. nextBreak = j + 1;
  191. break;
  192. }
  193. if (futureRowWidth > mMaxWidth)
  194. {
  195. ok = false;
  196. break;
  197. }
  198. }
  199. }
  200. if (!ok)
  201. {
  202. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  203. if (nextBreak == lineStart)
  204. {
  205. int copyLength = max(j - i, 1);
  206. mPrintText.append(mText.substr(i, copyLength));
  207. i += copyLength;
  208. }
  209. mPrintText += '\n';
  210. rowWidth = 0;
  211. nextBreak = lineStart = i;
  212. }
  213. if (i < mText.length())
  214. {
  215. // When copying a space, we may be over row width
  216. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  217. if (rowWidth <= mMaxWidth)
  218. mPrintText += mText[i];
  219. }
  220. }
  221. else
  222. {
  223. mPrintText += '\n';
  224. rowWidth = 0;
  225. nextBreak = lineStart = i;
  226. }
  227. }
  228. }
  229. rowWidth = 0;
  230. for (unsigned i = 0; i < mPrintText.length(); ++i)
  231. {
  232. unsigned char c = (unsigned char)mPrintText[i];
  233. if (c != '\n')
  234. {
  235. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  236. rowWidth += glyph.mAdvanceX;
  237. }
  238. else
  239. {
  240. width = max(width, rowWidth);
  241. height += rowHeight;
  242. mRowWidths.push_back(rowWidth);
  243. rowWidth = 0;
  244. }
  245. }
  246. if (rowWidth)
  247. {
  248. width = max(width, rowWidth);
  249. height += rowHeight;
  250. mRowWidths.push_back(rowWidth);
  251. }
  252. }
  253. setSize(width, height);
  254. }
  255. int Text::getRowStartPosition(unsigned rowIndex) const
  256. {
  257. int rowWidth = 0;
  258. if (rowIndex < mRowWidths.size())
  259. rowWidth = mRowWidths[rowIndex];
  260. switch (mTextAlignment)
  261. {
  262. default:
  263. return 0;
  264. case HA_CENTER:
  265. return (getSize().mX - rowWidth) / 2;
  266. case HA_RIGHT:
  267. return getSize().mX - rowWidth;
  268. }
  269. }