Text.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. mRowHeight(0)
  40. {
  41. }
  42. Text::~Text()
  43. {
  44. }
  45. void Text::setStyle(const XMLElement& element, ResourceCache* cache)
  46. {
  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. {
  57. std::string text = element.getChildElement("text").getString("value");
  58. replaceInPlace(text, "\\n", "\n");
  59. setText(text);
  60. }
  61. if (element.hasChildElement("textspacing"))
  62. setTextSpacing(element.getChildElement("textspacing").getFloat("value"));
  63. if (element.hasChildElement("textalignment"))
  64. {
  65. std::string horiz = element.getChildElement("textalignment").getStringLower("value");
  66. if (horiz == "left")
  67. setTextAlignment(HA_LEFT);
  68. if (horiz == "center")
  69. setTextAlignment(HA_CENTER);
  70. if (horiz == "right")
  71. setTextAlignment(HA_RIGHT);
  72. }
  73. }
  74. bool Text::setFont(Font* font, int size)
  75. {
  76. if (!font)
  77. {
  78. LOGERROR("Null font for Text");
  79. return false;
  80. }
  81. mFont = font;
  82. mFontSize = max(size, 1);
  83. calculateTextSize();
  84. return true;
  85. }
  86. void Text::setMaxWidth(int maxWidth)
  87. {
  88. mMaxWidth = max(maxWidth, 0);
  89. calculateTextSize();
  90. }
  91. void Text::setText(const std::string& text)
  92. {
  93. mText = text;
  94. calculateTextSize();
  95. }
  96. void Text::setTextAlignment(HorizontalAlignment align)
  97. {
  98. mTextAlignment = align;
  99. }
  100. void Text::setTextSpacing(float spacing)
  101. {
  102. mTextSpacing = max(spacing, 0.5f);
  103. }
  104. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  105. {
  106. if (!mFont)
  107. {
  108. mHovering = false;
  109. return;
  110. }
  111. const FontFace* face = mFont->getFace(mFontSize);
  112. UIBatch batch;
  113. batch.begin(&quads);
  114. batch.mBlendMode = BLEND_ALPHA;
  115. batch.mScissor = currentScissor;
  116. batch.mTexture = face->mTexture;
  117. unsigned rowIndex = 0;
  118. int x = getRowStartPosition(rowIndex);
  119. int y = 0;
  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 += mRowHeight;
  135. }
  136. }
  137. UIBatch::addOrMerge(batch, batches);
  138. // Reset hovering for next frame
  139. mHovering = false;
  140. }
  141. void Text::calculateTextSize()
  142. {
  143. int width = 0;
  144. int height = 0;
  145. mRowWidths.clear();
  146. mPrintText.clear();
  147. static std::vector<unsigned> printToText;
  148. printToText.clear();
  149. if (mFont)
  150. {
  151. const FontFace* face = mFont->getFace(mFontSize);
  152. mRowHeight = face->mRowHeight;
  153. int rowWidth = 0;
  154. int rowHeight = (int)(mTextSpacing * mRowHeight);
  155. // First see if the text must be split up
  156. if (!mMaxWidth)
  157. {
  158. mPrintText = mText;
  159. printToText.resize(mText.length());
  160. for (unsigned i = 0; i < mText.length(); ++i)
  161. printToText[i] = i;
  162. }
  163. else
  164. {
  165. unsigned nextBreak = 0;
  166. unsigned lineStart = 0;
  167. for (unsigned i = 0; i < mText.length(); ++i)
  168. {
  169. unsigned j;
  170. if (mText[i] != '\n')
  171. {
  172. bool ok = true;
  173. if (nextBreak <= i)
  174. {
  175. int futureRowWidth = rowWidth;
  176. for (j = i; j < mText.length(); ++j)
  177. {
  178. if ((mText[j] == ' ') || (mText[j] == '\n'))
  179. {
  180. nextBreak = j;
  181. break;
  182. }
  183. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  184. if ((mText[j] == '-') && (futureRowWidth <= mMaxWidth))
  185. {
  186. nextBreak = j + 1;
  187. break;
  188. }
  189. if (futureRowWidth > mMaxWidth)
  190. {
  191. ok = false;
  192. break;
  193. }
  194. }
  195. }
  196. if (!ok)
  197. {
  198. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  199. if (nextBreak == lineStart)
  200. {
  201. int copyLength = max(j - i, 1);
  202. mPrintText.append(mText.substr(i, copyLength));
  203. for (int k = 0; k < copyLength; ++k)
  204. {
  205. printToText.push_back(i);
  206. ++i;
  207. }
  208. }
  209. mPrintText += '\n';
  210. printToText.push_back(i);
  211. rowWidth = 0;
  212. nextBreak = lineStart = i;
  213. }
  214. if (i < mText.length())
  215. {
  216. // When copying a space, we may be over row width
  217. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  218. if (rowWidth <= mMaxWidth)
  219. {
  220. mPrintText += mText[i];
  221. printToText.push_back(i);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. mPrintText += '\n';
  228. printToText.push_back(i);
  229. rowWidth = 0;
  230. nextBreak = lineStart = i;
  231. }
  232. }
  233. }
  234. rowWidth = 0;
  235. for (unsigned i = 0; i < mPrintText.length(); ++i)
  236. {
  237. unsigned char c = (unsigned char)mPrintText[i];
  238. if (c != '\n')
  239. {
  240. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  241. rowWidth += glyph.mAdvanceX;
  242. }
  243. else
  244. {
  245. width = max(width, rowWidth);
  246. height += rowHeight;
  247. mRowWidths.push_back(rowWidth);
  248. rowWidth = 0;
  249. }
  250. }
  251. if (rowWidth)
  252. {
  253. width = max(width, rowWidth);
  254. height += rowHeight;
  255. mRowWidths.push_back(rowWidth);
  256. }
  257. // Set row height even if text is empty
  258. if (!height)
  259. height = rowHeight;
  260. // Store position of each character
  261. mCharPositions.resize(mText.length() + 1);
  262. unsigned rowIndex = 0;
  263. int x = getRowStartPosition(rowIndex);
  264. int y = 0;
  265. for (unsigned i = 0; i < mPrintText.length(); ++i)
  266. {
  267. mCharPositions[printToText[i]] = IntVector2(x, y);
  268. unsigned char c = (unsigned char)mPrintText[i];
  269. if (c != '\n')
  270. {
  271. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  272. x += glyph.mAdvanceX;
  273. }
  274. else
  275. {
  276. rowIndex++;
  277. x = getRowStartPosition(rowIndex);
  278. y += rowHeight;
  279. }
  280. }
  281. // Store the ending position
  282. mCharPositions[mText.length()] = IntVector2(x, y);
  283. }
  284. // If maxwidth is nonzero, fix the element width
  285. if (mMaxWidth)
  286. width = mMaxWidth;
  287. setSize(width, height);
  288. }
  289. int Text::getRowStartPosition(unsigned rowIndex) const
  290. {
  291. int rowWidth = 0;
  292. if (rowIndex < mRowWidths.size())
  293. rowWidth = mRowWidths[rowIndex];
  294. switch (mTextAlignment)
  295. {
  296. default:
  297. return 0;
  298. case HA_CENTER:
  299. return (getSize().mX - rowWidth) / 2;
  300. case HA_RIGHT:
  301. return getSize().mX - rowWidth;
  302. }
  303. }