Text.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "StringUtils.h"
  29. #include "Text.h"
  30. #include "Texture2D.h"
  31. #include "DebugNew.h"
  32. Text::Text(const std::string& text, const std::string& name) :
  33. UIElement(name),
  34. mFontSize(DEFAULT_FONT_SIZE),
  35. mMaxWidth(0),
  36. mText(text),
  37. mTextAlignment(HA_LEFT),
  38. mTextSpacing(1.0f)
  39. {
  40. }
  41. Text::~Text()
  42. {
  43. }
  44. void Text::setStyle(const XMLElement& element, ResourceCache* cache)
  45. {
  46. UIElement::setStyle(element, cache);
  47. if (element.hasChildElement("font"))
  48. {
  49. XMLElement fontElem = element.getChildElement("font");
  50. setFont(cache->getResource<Font>(fontElem.getString("name")), fontElem.getInt("size"));
  51. }
  52. if (element.hasChildElement("maxwidth"))
  53. setMaxWidth(element.getChildElement("maxwidth").getInt("value"));
  54. if (element.hasChildElement("text"))
  55. {
  56. std::string text = element.getChildElement("text").getString("value");
  57. replaceInPlace(text, "\\n", "\n");
  58. setText(text);
  59. }
  60. if (element.hasChildElement("textspacing"))
  61. setTextSpacing(element.getChildElement("textspacing").getFloat("value"));
  62. if (element.hasChildElement("textalignment"))
  63. {
  64. std::string horiz = element.getChildElement("textalignment").getStringLower("value");
  65. if (horiz == "left")
  66. setTextAlignment(HA_LEFT);
  67. if (horiz == "center")
  68. setTextAlignment(HA_CENTER);
  69. if (horiz == "right")
  70. setTextAlignment(HA_RIGHT);
  71. }
  72. }
  73. bool Text::setFont(Font* font, int size)
  74. {
  75. if (!font)
  76. {
  77. LOGERROR("Null font for Text");
  78. return false;
  79. }
  80. mFont = font;
  81. mFontSize = max(size, 1);
  82. // Catch exception if executed from script
  83. try
  84. {
  85. calculateTextSize();
  86. }
  87. catch (Exception& e)
  88. {
  89. mFont = 0;
  90. SAFE_RETHROW_RET(e, false);
  91. }
  92. return true;
  93. }
  94. void Text::setMaxWidth(int maxWidth)
  95. {
  96. mMaxWidth = max(maxWidth, 0);
  97. calculateTextSize();
  98. }
  99. void Text::setText(const std::string& text)
  100. {
  101. mText = text;
  102. calculateTextSize();
  103. }
  104. void Text::setTextAlignment(HorizontalAlignment align)
  105. {
  106. mTextAlignment = align;
  107. }
  108. void Text::setTextSpacing(float spacing)
  109. {
  110. mTextSpacing = max(spacing, 0.5f);
  111. }
  112. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  113. {
  114. if (!mFont)
  115. {
  116. mHovering = false;
  117. return;
  118. }
  119. const FontFace* face = mFont->getFace(mFontSize);
  120. UIBatch batch;
  121. batch.begin(&quads);
  122. batch.mBlendMode = BLEND_ALPHA;
  123. batch.mScissor = currentScissor;
  124. batch.mTexture = face->mTexture;
  125. unsigned rowIndex = 0;
  126. int x = getRowStartPosition(rowIndex);
  127. int y = 0;
  128. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  129. for (unsigned i = 0; i < mPrintText.length(); ++i)
  130. {
  131. unsigned char c = (unsigned char)mPrintText[i];
  132. if (c != '\n')
  133. {
  134. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  135. if (c != ' ')
  136. batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
  137. x += glyph.mAdvanceX;
  138. }
  139. else
  140. {
  141. rowIndex++;
  142. x = getRowStartPosition(rowIndex);
  143. y += rowHeight;
  144. }
  145. }
  146. UIBatch::addOrMerge(batch, batches);
  147. // Reset hovering for next frame
  148. mHovering = false;
  149. }
  150. int Text::getRowHeight() const
  151. {
  152. if (mFont)
  153. return mFont->getFace(mFontSize)->mRowHeight;
  154. else
  155. return 0;
  156. }
  157. void Text::calculateTextSize()
  158. {
  159. int width = 0;
  160. int height = 0;
  161. mRowWidths.clear();
  162. if (mFont)
  163. {
  164. const FontFace* face = mFont->getFace(mFontSize);
  165. int rowWidth = 0;
  166. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  167. // First see if the text must be split up
  168. if (!mMaxWidth)
  169. mPrintText = mText;
  170. else
  171. {
  172. unsigned nextBreak = 0;
  173. unsigned lineStart = 0;
  174. for (unsigned i = 0; i < mText.length(); ++i)
  175. {
  176. unsigned j;
  177. if (mText[i] != '\n')
  178. {
  179. bool ok = true;
  180. if (nextBreak <= i)
  181. {
  182. int futureRowWidth = rowWidth;
  183. for (j = i; j < mText.length(); ++j)
  184. {
  185. if ((mText[j] == ' ') || (mText[j] == '\n'))
  186. {
  187. nextBreak = j;
  188. break;
  189. }
  190. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  191. if ((mText[j] == '-') && (futureRowWidth <= mMaxWidth))
  192. {
  193. nextBreak = j + 1;
  194. break;
  195. }
  196. if (futureRowWidth > mMaxWidth)
  197. {
  198. ok = false;
  199. break;
  200. }
  201. }
  202. }
  203. if (!ok)
  204. {
  205. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  206. if (nextBreak == lineStart)
  207. {
  208. int copyLength = max(j - i, 1);
  209. mPrintText.append(mText.substr(i, copyLength));
  210. i += copyLength;
  211. }
  212. mPrintText += '\n';
  213. rowWidth = 0;
  214. nextBreak = lineStart = i;
  215. }
  216. if (i < mText.length())
  217. {
  218. // When copying a space, we may be over row width
  219. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  220. if (rowWidth <= mMaxWidth)
  221. mPrintText += mText[i];
  222. }
  223. }
  224. else
  225. {
  226. mPrintText += '\n';
  227. rowWidth = 0;
  228. nextBreak = lineStart = i;
  229. }
  230. }
  231. }
  232. rowWidth = 0;
  233. for (unsigned i = 0; i < mPrintText.length(); ++i)
  234. {
  235. unsigned char c = (unsigned char)mPrintText[i];
  236. if (c != '\n')
  237. {
  238. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  239. rowWidth += glyph.mAdvanceX;
  240. }
  241. else
  242. {
  243. width = max(width, rowWidth);
  244. height += rowHeight;
  245. mRowWidths.push_back(rowWidth);
  246. rowWidth = 0;
  247. }
  248. }
  249. if (rowWidth)
  250. {
  251. width = max(width, rowWidth);
  252. height += rowHeight;
  253. mRowWidths.push_back(rowWidth);
  254. }
  255. }
  256. setSize(width, height);
  257. }
  258. int Text::getRowStartPosition(unsigned rowIndex) const
  259. {
  260. int rowWidth = 0;
  261. if (rowIndex < mRowWidths.size())
  262. rowWidth = mRowWidths[rowIndex];
  263. switch (mTextAlignment)
  264. {
  265. default:
  266. return 0;
  267. case HA_CENTER:
  268. return (getSize().mX - rowWidth) / 2;
  269. case HA_RIGHT:
  270. return getSize().mX - rowWidth;
  271. }
  272. }