Text.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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& name, const std::string& text) :
  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. calculateTextSize();
  83. return true;
  84. }
  85. void Text::setMaxWidth(int maxWidth)
  86. {
  87. mMaxWidth = max(maxWidth, 0);
  88. calculateTextSize();
  89. }
  90. void Text::setText(const std::string& text)
  91. {
  92. mText = text;
  93. calculateTextSize();
  94. }
  95. void Text::setTextAlignment(HorizontalAlignment align)
  96. {
  97. mTextAlignment = align;
  98. }
  99. void Text::setTextSpacing(float spacing)
  100. {
  101. mTextSpacing = max(spacing, 0.5f);
  102. }
  103. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  104. {
  105. if (!mFont)
  106. {
  107. mHovering = false;
  108. return;
  109. }
  110. const FontFace* face = mFont->getFace(mFontSize);
  111. UIBatch batch;
  112. batch.begin(&quads);
  113. batch.mBlendMode = BLEND_ALPHA;
  114. batch.mScissor = currentScissor;
  115. batch.mTexture = face->mTexture;
  116. unsigned rowIndex = 0;
  117. int x = getRowStartPosition(rowIndex);
  118. int y = 0;
  119. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  120. for (unsigned i = 0; i < mPrintText.length(); ++i)
  121. {
  122. unsigned char c = (unsigned char)mPrintText[i];
  123. if (c != '\n')
  124. {
  125. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  126. if (c != ' ')
  127. batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
  128. x += glyph.mAdvanceX;
  129. }
  130. else
  131. {
  132. rowIndex++;
  133. x = getRowStartPosition(rowIndex);
  134. y += rowHeight;
  135. }
  136. }
  137. UIBatch::addOrMerge(batch, batches);
  138. // Reset hovering for next frame
  139. mHovering = false;
  140. }
  141. int Text::getRowHeight() const
  142. {
  143. if (mFont)
  144. return mFont->getFace(mFontSize)->mRowHeight;
  145. else
  146. return 0;
  147. }
  148. void Text::calculateTextSize()
  149. {
  150. int width = 0;
  151. int height = 0;
  152. mRowWidths.clear();
  153. if (mFont)
  154. {
  155. const FontFace* face = mFont->getFace(mFontSize);
  156. int rowWidth = 0;
  157. int rowHeight = (int)(mTextSpacing * face->mRowHeight);
  158. // First see if the text must be split up
  159. if (!mMaxWidth)
  160. mPrintText = mText;
  161. else
  162. {
  163. unsigned nextBreak = 0;
  164. unsigned lineStart = 0;
  165. for (unsigned i = 0; i < mText.length(); ++i)
  166. {
  167. unsigned j;
  168. if (mText[i] != '\n')
  169. {
  170. bool ok = true;
  171. if (nextBreak <= i)
  172. {
  173. int futureRowWidth = rowWidth;
  174. for (j = i; j < mText.length(); ++j)
  175. {
  176. if ((mText[j] == ' ') || (mText[j] == '\n'))
  177. {
  178. nextBreak = j;
  179. break;
  180. }
  181. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  182. if ((mText[j] == '-') && (futureRowWidth <= mMaxWidth))
  183. {
  184. nextBreak = j + 1;
  185. break;
  186. }
  187. if (futureRowWidth > mMaxWidth)
  188. {
  189. ok = false;
  190. break;
  191. }
  192. }
  193. }
  194. if (!ok)
  195. {
  196. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  197. if (nextBreak == lineStart)
  198. {
  199. int copyLength = max(j - i, 1);
  200. mPrintText.append(mText.substr(i, copyLength));
  201. i += copyLength;
  202. }
  203. mPrintText += '\n';
  204. rowWidth = 0;
  205. nextBreak = lineStart = i;
  206. }
  207. if (i < mText.length())
  208. {
  209. // When copying a space, we may be over row width
  210. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  211. if (rowWidth <= mMaxWidth)
  212. mPrintText += mText[i];
  213. }
  214. }
  215. else
  216. {
  217. mPrintText += '\n';
  218. rowWidth = 0;
  219. nextBreak = lineStart = i;
  220. }
  221. }
  222. }
  223. rowWidth = 0;
  224. for (unsigned i = 0; i < mPrintText.length(); ++i)
  225. {
  226. unsigned char c = (unsigned char)mPrintText[i];
  227. if (c != '\n')
  228. {
  229. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  230. rowWidth += glyph.mAdvanceX;
  231. }
  232. else
  233. {
  234. width = max(width, rowWidth);
  235. height += rowHeight;
  236. mRowWidths.push_back(rowWidth);
  237. rowWidth = 0;
  238. }
  239. }
  240. if (rowWidth)
  241. {
  242. width = max(width, rowWidth);
  243. height += rowHeight;
  244. mRowWidths.push_back(rowWidth);
  245. }
  246. // Set row height even if text is empty
  247. if (!height)
  248. height = rowHeight;
  249. }
  250. setSize(width, height);
  251. }
  252. int Text::getRowStartPosition(unsigned rowIndex) const
  253. {
  254. int rowWidth = 0;
  255. if (rowIndex < mRowWidths.size())
  256. rowWidth = mRowWidths[rowIndex];
  257. switch (mTextAlignment)
  258. {
  259. default:
  260. return 0;
  261. case HA_CENTER:
  262. return (getSize().mX - rowWidth) / 2;
  263. case HA_RIGHT:
  264. return getSize().mX - rowWidth;
  265. }
  266. }