Text.cpp 15 KB

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