Text.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. mText(text),
  36. mTextAlignment(HA_LEFT),
  37. mRowSpacing(1.0f),
  38. mWordwrap(false),
  39. mSelectionStart(0),
  40. mSelectionLength(0),
  41. mSelectionColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
  42. mHoverColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
  43. mRowHeight(0)
  44. {
  45. }
  46. Text::~Text()
  47. {
  48. }
  49. void Text::setStyle(const XMLElement& element, ResourceCache* cache)
  50. {
  51. UIElement::setStyle(element, cache);
  52. // Set word wrap first to preserve any pre-defined width
  53. if (element.hasChildElement("wordwrap"))
  54. setWordwrap(element.getChildElement("wordwrap").getBool("enable"));
  55. if (element.hasChildElement("font"))
  56. {
  57. XMLElement fontElem = element.getChildElement("font");
  58. setFont(cache->getResource<Font>(fontElem.getString("name")), fontElem.getInt("size"));
  59. }
  60. if (element.hasChildElement("textalignment"))
  61. {
  62. std::string horiz = element.getChildElement("textalignment").getStringLower("value");
  63. if (horiz == "left")
  64. setTextAlignment(HA_LEFT);
  65. if (horiz == "center")
  66. setTextAlignment(HA_CENTER);
  67. if (horiz == "right")
  68. setTextAlignment(HA_RIGHT);
  69. }
  70. if (element.hasChildElement("rowspacing"))
  71. setRowSpacing(element.getChildElement("rowspacing").getFloat("value"));
  72. if (element.hasChildElement("selection"))
  73. {
  74. XMLElement selectionElem = element.getChildElement("selection");
  75. setSelection(selectionElem.getInt("start"), selectionElem.getInt("length"));
  76. }
  77. if (element.hasChildElement("selectioncolor"))
  78. setSelectionColor(element.getChildElement("selectioncolor").getColor("value"));
  79. if (element.hasChildElement("hovercolor"))
  80. setHoverColor(element.getChildElement("hovercolor").getColor("value"));
  81. if (element.hasChildElement("text"))
  82. {
  83. std::string text = element.getChildElement("text").getString("value");
  84. replaceInPlace(text, "\\n", "\n");
  85. setText(text);
  86. }
  87. }
  88. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  89. {
  90. // Hovering or whole selection batch
  91. if ((mHovering && (mHoverColor.mA > 0.0f)) || (mSelected && (mSelectionColor.mA > 0.0f)))
  92. {
  93. UIBatch batch;
  94. batch.begin(&quads);
  95. batch.mBlendMode = BLEND_ALPHA;
  96. batch.mScissor = currentScissor;
  97. batch.mTexture = 0;
  98. batch.addQuad(*this, 0, 0, getWidth(), getHeight(), 0, 0, 0, 0, (mSelected && (mSelectionColor.mA > 0.0f)) ? mSelectionColor :
  99. mHoverColor);
  100. UIBatch::addOrMerge(batch, batches);
  101. }
  102. // Partial selection batch
  103. if ((!mSelected) && (mSelectionLength) && (mCharSizes.size() >= mSelectionStart + mSelectionLength) && (mSelectionColor.mA > 0.0f))
  104. {
  105. UIBatch batch;
  106. batch.begin(&quads);
  107. batch.mBlendMode = BLEND_ALPHA;
  108. batch.mScissor = currentScissor;
  109. batch.mTexture = 0;
  110. IntVector2 currentStart = mCharPositions[mSelectionStart];
  111. IntVector2 currentEnd = currentStart;
  112. for (unsigned i = mSelectionStart; i < mSelectionStart + mSelectionLength; ++i)
  113. {
  114. // Check if row changes, and start a new quad in that case
  115. if ((mCharSizes[i].mX) && (mCharSizes[i].mY))
  116. {
  117. if (mCharPositions[i].mY != currentStart.mY)
  118. {
  119. batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
  120. 0, 0, 0, 0, mSelectionColor);
  121. currentStart = mCharPositions[i];
  122. currentEnd = currentStart + mCharSizes[i];
  123. }
  124. else
  125. {
  126. currentEnd.mX += mCharSizes[i].mX;
  127. currentEnd.mY = max(currentStart.mY + mCharSizes[i].mY, currentEnd.mY);
  128. }
  129. }
  130. }
  131. if (currentEnd != currentStart)
  132. {
  133. batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
  134. 0, 0, 0, 0, mSelectionColor);
  135. }
  136. UIBatch::addOrMerge(batch, batches);
  137. }
  138. // Text batch
  139. if (mFont)
  140. {
  141. const FontFace* face = mFont->getFace(mFontSize);
  142. UIBatch batch;
  143. batch.begin(&quads);
  144. batch.mBlendMode = BLEND_ALPHA;
  145. batch.mScissor = currentScissor;
  146. batch.mTexture = face->mTexture;
  147. unsigned rowIndex = 0;
  148. int x = getRowStartPosition(rowIndex);
  149. int y = 0;
  150. for (unsigned i = 0; i < mPrintText.length(); ++i)
  151. {
  152. unsigned char c = (unsigned char)mPrintText[i];
  153. if (c != '\n')
  154. {
  155. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  156. if (c != ' ')
  157. batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
  158. x += glyph.mAdvanceX;
  159. }
  160. else
  161. {
  162. rowIndex++;
  163. x = getRowStartPosition(rowIndex);
  164. y += mRowHeight;
  165. }
  166. }
  167. UIBatch::addOrMerge(batch, batches);
  168. }
  169. // Reset hovering for next frame
  170. mHovering = false;
  171. }
  172. void Text::onResize()
  173. {
  174. if (mWordwrap)
  175. updateText();
  176. }
  177. bool Text::setFont(Font* font, int size)
  178. {
  179. if (!font)
  180. {
  181. LOGERROR("Null font for Text");
  182. return false;
  183. }
  184. if ((font != mFont) || (size != mFontSize))
  185. {
  186. mFont = font;
  187. mFontSize = max(size, 1);
  188. updateText();
  189. }
  190. return true;
  191. }
  192. void Text::setText(const std::string& text)
  193. {
  194. mText = text;
  195. validateSelection();
  196. updateText();
  197. }
  198. void Text::setTextAlignment(HorizontalAlignment align)
  199. {
  200. if (align != mTextAlignment)
  201. {
  202. mTextAlignment = align;
  203. updateText();
  204. }
  205. }
  206. void Text::setRowSpacing(float spacing)
  207. {
  208. if (spacing != mRowSpacing)
  209. {
  210. mRowSpacing = max(spacing, 0.5f);
  211. updateText();
  212. }
  213. }
  214. void Text::setWordwrap(bool enable)
  215. {
  216. if (enable != mWordwrap)
  217. {
  218. mWordwrap = enable;
  219. updateText();
  220. }
  221. }
  222. void Text::setSelection(unsigned start, unsigned length)
  223. {
  224. mSelectionStart = start;
  225. mSelectionLength = length;
  226. validateSelection();
  227. }
  228. void Text::clearSelection()
  229. {
  230. mSelectionStart = 0;
  231. mSelectionLength = 0;
  232. }
  233. void Text::setSelectionColor(const Color& color)
  234. {
  235. mSelectionColor = color;
  236. }
  237. void Text::setHoverColor(const Color& color)
  238. {
  239. mHoverColor = color;
  240. }
  241. void Text::updateText(bool inResize)
  242. {
  243. int width = 0;
  244. int height = 0;
  245. mRowWidths.clear();
  246. mPrintText.clear();
  247. static std::vector<unsigned> printToText;
  248. printToText.clear();
  249. if (mFont)
  250. {
  251. const FontFace* face = mFont->getFace(mFontSize);
  252. mRowHeight = face->mRowHeight;
  253. int rowWidth = 0;
  254. int rowHeight = (int)(mRowSpacing * mRowHeight);
  255. // First see if the text must be split up
  256. if (!mWordwrap)
  257. {
  258. mPrintText = mText;
  259. printToText.resize(mText.length());
  260. for (unsigned i = 0; i < mText.length(); ++i)
  261. printToText[i] = i;
  262. }
  263. else
  264. {
  265. int maxWidth = getWidth();
  266. unsigned nextBreak = 0;
  267. unsigned lineStart = 0;
  268. for (unsigned i = 0; i < mText.length(); ++i)
  269. {
  270. unsigned j;
  271. if (mText[i] != '\n')
  272. {
  273. bool ok = true;
  274. if (nextBreak <= i)
  275. {
  276. int futureRowWidth = rowWidth;
  277. for (j = i; j < mText.length(); ++j)
  278. {
  279. if ((mText[j] == ' ') || (mText[j] == '\n'))
  280. {
  281. nextBreak = j;
  282. break;
  283. }
  284. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  285. if ((mText[j] == '-') && (futureRowWidth <= maxWidth))
  286. {
  287. nextBreak = j + 1;
  288. break;
  289. }
  290. if (futureRowWidth > maxWidth)
  291. {
  292. ok = false;
  293. break;
  294. }
  295. }
  296. }
  297. if (!ok)
  298. {
  299. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  300. if (nextBreak == lineStart)
  301. {
  302. int copyLength = max(j - i, 1);
  303. mPrintText.append(mText.substr(i, copyLength));
  304. for (int k = 0; k < copyLength; ++k)
  305. {
  306. printToText.push_back(i);
  307. ++i;
  308. }
  309. }
  310. mPrintText += '\n';
  311. printToText.push_back(min((int)i, (int)mText.length() - 1));
  312. rowWidth = 0;
  313. nextBreak = lineStart = i;
  314. }
  315. if (i < mText.length())
  316. {
  317. // When copying a space, we may be over row width
  318. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  319. if (rowWidth <= maxWidth)
  320. {
  321. mPrintText += mText[i];
  322. printToText.push_back(i);
  323. }
  324. }
  325. }
  326. else
  327. {
  328. mPrintText += '\n';
  329. printToText.push_back(min((int)i, (int)mText.length() - 1));
  330. rowWidth = 0;
  331. nextBreak = lineStart = i;
  332. }
  333. }
  334. }
  335. rowWidth = 0;
  336. for (unsigned i = 0; i < mPrintText.length(); ++i)
  337. {
  338. unsigned char c = (unsigned char)mPrintText[i];
  339. if (c != '\n')
  340. {
  341. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  342. rowWidth += glyph.mAdvanceX;
  343. }
  344. else
  345. {
  346. width = max(width, rowWidth);
  347. height += rowHeight;
  348. mRowWidths.push_back(rowWidth);
  349. rowWidth = 0;
  350. }
  351. }
  352. if (rowWidth)
  353. {
  354. width = max(width, rowWidth);
  355. height += rowHeight;
  356. mRowWidths.push_back(rowWidth);
  357. }
  358. // Set row height even if text is empty
  359. if (!height)
  360. height = rowHeight;
  361. // Store position & size of each character
  362. mCharPositions.resize(mText.length() + 1);
  363. mCharSizes.resize(mText.length());
  364. unsigned rowIndex = 0;
  365. int x = getRowStartPosition(rowIndex);
  366. int y = 0;
  367. for (unsigned i = 0; i < mPrintText.length(); ++i)
  368. {
  369. mCharPositions[printToText[i]] = IntVector2(x, y);
  370. unsigned char c = (unsigned char)mPrintText[i];
  371. if (c != '\n')
  372. {
  373. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  374. mCharSizes[printToText[i]] = IntVector2(glyph.mAdvanceX, mRowHeight);
  375. x += glyph.mAdvanceX;
  376. }
  377. else
  378. {
  379. mCharSizes[printToText[i]] = IntVector2::sZero;
  380. rowIndex++;
  381. x = getRowStartPosition(rowIndex);
  382. y += rowHeight;
  383. }
  384. }
  385. // Store the ending position
  386. mCharPositions[mText.length()] = IntVector2(x, y);
  387. }
  388. // Set minimum size to correspond to the text size
  389. if (!mWordwrap)
  390. setMinSize(width, height);
  391. else
  392. setMinHeight(height);
  393. }
  394. void Text::validateSelection()
  395. {
  396. unsigned textLength = mText.length();
  397. if (textLength)
  398. {
  399. if (mSelectionStart >= textLength)
  400. mSelectionStart = textLength - 1;
  401. if (mSelectionStart + mSelectionLength > textLength)
  402. mSelectionLength = textLength - mSelectionStart;
  403. }
  404. else
  405. {
  406. mSelectionStart = 0;
  407. mSelectionLength = 0;
  408. }
  409. }
  410. int Text::getRowStartPosition(unsigned rowIndex) const
  411. {
  412. int rowWidth = 0;
  413. if (rowIndex < mRowWidths.size())
  414. rowWidth = mRowWidths[rowIndex];
  415. switch (mTextAlignment)
  416. {
  417. default:
  418. return 0;
  419. case HA_CENTER:
  420. return (getSize().mX - rowWidth) / 2;
  421. case HA_RIGHT:
  422. return getSize().mX - rowWidth;
  423. }
  424. }