Text.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Font.h"
  25. #include "FontFace.h"
  26. #include "Log.h"
  27. #include "Profiler.h"
  28. #include "ResourceCache.h"
  29. #include "Text.h"
  30. #include "Texture2D.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. const char* textEffects[] =
  35. {
  36. "None",
  37. "Shadow",
  38. "Stroke",
  39. 0
  40. };
  41. static const float MIN_ROW_SPACING = 0.5f;
  42. extern const char* horizontalAlignments[];
  43. extern const char* UI_CATEGORY;
  44. Text::Text(Context* context) :
  45. UIElement(context),
  46. usedInText3D_(false),
  47. fontSize_(DEFAULT_FONT_SIZE),
  48. textAlignment_(HA_LEFT),
  49. rowSpacing_(1.0f),
  50. wordWrap_(false),
  51. charLocationsDirty_(true),
  52. selectionStart_(0),
  53. selectionLength_(0),
  54. selectionColor_(Color::TRANSPARENT),
  55. hoverColor_(Color::TRANSPARENT),
  56. textEffect_(TE_NONE),
  57. effectColor_(Color::BLACK),
  58. effectDepthBias_(0.0f),
  59. rowHeight_(0)
  60. {
  61. // By default Text does not derive opacity from parent elements
  62. useDerivedOpacity_ = false;
  63. }
  64. Text::~Text()
  65. {
  66. }
  67. void Text::RegisterObject(Context* context)
  68. {
  69. context->RegisterFactory<Text>(UI_CATEGORY);
  70. COPY_BASE_ATTRIBUTES(Text, UIElement);
  71. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Text, "Use Derived Opacity", false);
  72. MIXED_ACCESSOR_ATTRIBUTE(Text, VAR_RESOURCEREF, "Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_FILE);
  73. ATTRIBUTE(Text, VAR_INT, "Font Size", fontSize_, DEFAULT_FONT_SIZE, AM_FILE);
  74. ATTRIBUTE(Text, VAR_STRING, "Text", text_, String::EMPTY, AM_FILE);
  75. ENUM_ATTRIBUTE(Text, "Text Alignment", textAlignment_, horizontalAlignments, HA_LEFT, AM_FILE);
  76. ATTRIBUTE(Text, VAR_FLOAT, "Row Spacing", rowSpacing_, 1.0f, AM_FILE);
  77. ATTRIBUTE(Text, VAR_BOOL, "Word Wrap", wordWrap_, false, AM_FILE);
  78. REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Selection Color", GetSelectionColor, SetSelectionColor, Color, Color::TRANSPARENT, AM_FILE);
  79. REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Hover Color", GetHoverColor, SetHoverColor, Color, Color::TRANSPARENT, AM_FILE);
  80. ENUM_ATTRIBUTE(Text, "Text Effect", textEffect_, textEffects, TE_NONE, AM_FILE);
  81. REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_FILE);
  82. // Change the default value for UseDerivedOpacity
  83. context->GetAttribute<Text>("Use Derived Opacity")->defaultValue_ = false;
  84. }
  85. void Text::ApplyAttributes()
  86. {
  87. UIElement::ApplyAttributes();
  88. // Decode to Unicode now
  89. unicodeText_.Clear();
  90. for (unsigned i = 0; i < text_.Length();)
  91. unicodeText_.Push(text_.NextUTF8Char(i));
  92. fontSize_ = Max(fontSize_, 1);
  93. ValidateSelection();
  94. UpdateText();
  95. }
  96. void Text::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  97. {
  98. FontFace* face = font_ ? font_->GetFace(fontSize_) : (FontFace*)0;
  99. if (!face)
  100. {
  101. hovering_ = false;
  102. return;
  103. }
  104. // If face has changed or char locations are not valid anymore, update before rendering
  105. if (charLocationsDirty_ || !fontFace_ || face != fontFace_)
  106. UpdateCharLocations();
  107. // If face uses mutable glyphs mechanism, reacquire glyphs before rendering to make sure they are in the texture
  108. else if (face->HasMutableGlyphs())
  109. {
  110. for (unsigned i = 0; i < printText_.Size(); ++i)
  111. face->GetGlyph(printText_[i]);
  112. }
  113. // Hovering and/or whole selection batch
  114. if ((hovering_ && hoverColor_.a_ > 0.0) || (selected_ && selectionColor_.a_ > 0.0f))
  115. {
  116. bool both = hovering_ && selected_ && hoverColor_.a_ > 0.0 && selectionColor_.a_ > 0.0f;
  117. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  118. batch.SetColor(both ? selectionColor_.Lerp(hoverColor_, 0.5f) : (selected_ && selectionColor_.a_ > 0.0f ?
  119. selectionColor_: hoverColor_));
  120. batch.AddQuad(0, 0, GetWidth(), GetHeight(), 0, 0);
  121. UIBatch::AddOrMerge(batch, batches);
  122. }
  123. // Partial selection batch
  124. if (!selected_ && selectionLength_ && charLocations_.Size() >= selectionStart_ + selectionLength_ && selectionColor_.a_ > 0.0f)
  125. {
  126. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  127. batch.SetColor(selectionColor_);
  128. IntVector2 currentStart = charLocations_[selectionStart_].position_;
  129. IntVector2 currentEnd = currentStart;
  130. for (unsigned i = selectionStart_; i < selectionStart_ + selectionLength_; ++i)
  131. {
  132. // Check if row changes, and start a new quad in that case
  133. if (charLocations_[i].size_ != IntVector2::ZERO)
  134. {
  135. if (charLocations_[i].position_.y_ != currentStart.y_)
  136. {
  137. batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
  138. 0, 0);
  139. currentStart = charLocations_[i].position_;
  140. currentEnd = currentStart + charLocations_[i].size_;
  141. }
  142. else
  143. {
  144. currentEnd.x_ += charLocations_[i].size_.x_;
  145. currentEnd.y_ = Max(currentStart.y_ + charLocations_[i].size_.y_, currentEnd.y_);
  146. }
  147. }
  148. }
  149. if (currentEnd != currentStart)
  150. {
  151. batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
  152. 0, 0);
  153. }
  154. UIBatch::AddOrMerge(batch, batches);
  155. }
  156. // Text batch
  157. TextEffect textEffect = font_->IsSDFFont() ? TE_NONE : textEffect_;
  158. const Vector<SharedPtr<Texture2D> >& textures = face->GetTextures();
  159. for (unsigned n = 0; n < textures.Size() && n < pageGlyphLocations_.Size(); ++n)
  160. {
  161. // One batch per texture/page
  162. UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, textures[n], &vertexData);
  163. const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations_[n];
  164. switch (textEffect)
  165. {
  166. case TE_NONE:
  167. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  168. break;
  169. case TE_SHADOW:
  170. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  171. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  172. break;
  173. case TE_STROKE:
  174. ConstructBatch(pageBatch, pageGlyphLocation, -1, -1, &effectColor_, effectDepthBias_);
  175. ConstructBatch(pageBatch, pageGlyphLocation, 0, -1, &effectColor_, effectDepthBias_);
  176. ConstructBatch(pageBatch, pageGlyphLocation, 1, -1, &effectColor_, effectDepthBias_);
  177. ConstructBatch(pageBatch, pageGlyphLocation, -1, 0, &effectColor_, effectDepthBias_);
  178. ConstructBatch(pageBatch, pageGlyphLocation, 1, 0, &effectColor_, effectDepthBias_);
  179. ConstructBatch(pageBatch, pageGlyphLocation, -1, 1, &effectColor_, effectDepthBias_);
  180. ConstructBatch(pageBatch, pageGlyphLocation, 0, 1, &effectColor_, effectDepthBias_);
  181. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  182. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  183. break;
  184. }
  185. UIBatch::AddOrMerge(pageBatch, batches);
  186. }
  187. // Reset hovering for next frame
  188. hovering_ = false;
  189. }
  190. void Text::OnResize()
  191. {
  192. if (wordWrap_)
  193. UpdateText(true);
  194. else
  195. charLocationsDirty_ = true;
  196. }
  197. void Text::OnIndentSet()
  198. {
  199. charLocationsDirty_ = true;
  200. }
  201. bool Text::SetFont(const String& fontName, int size)
  202. {
  203. ResourceCache* cache = GetSubsystem<ResourceCache>();
  204. return SetFont(cache->GetResource<Font>(fontName), size);
  205. }
  206. bool Text::SetFont(Font* font, int size)
  207. {
  208. if (!font)
  209. {
  210. LOGERROR("Null font for Text");
  211. return false;
  212. }
  213. if (font != font_ || size != fontSize_)
  214. {
  215. font_ = font;
  216. fontSize_ = Max(size, 1);
  217. UpdateText();
  218. }
  219. return true;
  220. }
  221. void Text::SetText(const String& text)
  222. {
  223. text_ = text;
  224. // Decode to Unicode now
  225. unicodeText_.Clear();
  226. for (unsigned i = 0; i < text_.Length();)
  227. unicodeText_.Push(text_.NextUTF8Char(i));
  228. ValidateSelection();
  229. UpdateText();
  230. }
  231. void Text::SetTextAlignment(HorizontalAlignment align)
  232. {
  233. if (align != textAlignment_)
  234. {
  235. textAlignment_ = align;
  236. charLocationsDirty_ = true;
  237. }
  238. }
  239. void Text::SetRowSpacing(float spacing)
  240. {
  241. if (spacing != rowSpacing_)
  242. {
  243. rowSpacing_ = Max(spacing, MIN_ROW_SPACING);
  244. UpdateText();
  245. }
  246. }
  247. void Text::SetWordwrap(bool enable)
  248. {
  249. if (enable != wordWrap_)
  250. {
  251. wordWrap_ = enable;
  252. UpdateText();
  253. }
  254. }
  255. void Text::SetSelection(unsigned start, unsigned length)
  256. {
  257. selectionStart_ = start;
  258. selectionLength_ = length;
  259. ValidateSelection();
  260. }
  261. void Text::ClearSelection()
  262. {
  263. selectionStart_ = 0;
  264. selectionLength_ = 0;
  265. }
  266. void Text::SetSelectionColor(const Color& color)
  267. {
  268. selectionColor_ = color;
  269. }
  270. void Text::SetHoverColor(const Color& color)
  271. {
  272. hoverColor_ = color;
  273. }
  274. void Text::SetTextEffect(TextEffect textEffect)
  275. {
  276. textEffect_ = textEffect;
  277. }
  278. void Text::SetEffectColor(const Color& effectColor)
  279. {
  280. effectColor_ = effectColor;
  281. }
  282. void Text::SetUsedInText3D(bool usedInText3D)
  283. {
  284. usedInText3D_ = usedInText3D;
  285. }
  286. void Text::SetEffectDepthBias(float bias)
  287. {
  288. effectDepthBias_ = bias;
  289. }
  290. int Text::GetRowWidth(unsigned index) const
  291. {
  292. return index < rowWidths_.Size() ? rowWidths_[index] : 0;
  293. }
  294. IntVector2 Text::GetCharPosition(unsigned index)
  295. {
  296. if (charLocationsDirty_)
  297. UpdateCharLocations();
  298. if (charLocations_.Empty())
  299. return IntVector2::ZERO;
  300. // For convenience, return the position of the text ending if index exceeded
  301. if (index > charLocations_.Size() - 1)
  302. index = charLocations_.Size() - 1;
  303. return charLocations_[index].position_;
  304. }
  305. IntVector2 Text::GetCharSize(unsigned index)
  306. {
  307. if (charLocationsDirty_)
  308. UpdateCharLocations();
  309. if (charLocations_.Size() < 2)
  310. return IntVector2::ZERO;
  311. // For convenience, return the size of the last char if index exceeded (last size entry is zero)
  312. if (index > charLocations_.Size() - 2)
  313. index = charLocations_.Size() - 2;
  314. return charLocations_[index].size_;
  315. }
  316. void Text::SetFontAttr(const ResourceRef& value)
  317. {
  318. ResourceCache* cache = GetSubsystem<ResourceCache>();
  319. font_ = cache->GetResource<Font>(value.name_);
  320. }
  321. ResourceRef Text::GetFontAttr() const
  322. {
  323. return GetResourceRef(font_, Font::GetTypeStatic());
  324. }
  325. bool Text::FilterImplicitAttributes(XMLElement& dest) const
  326. {
  327. if (!UIElement::FilterImplicitAttributes(dest))
  328. return false;
  329. if (!IsFixedWidth())
  330. {
  331. if (!RemoveChildXML(dest, "Size"))
  332. return false;
  333. if (!RemoveChildXML(dest, "Min Size"))
  334. return false;
  335. if (!RemoveChildXML(dest, "Max Size"))
  336. return false;
  337. }
  338. return true;
  339. }
  340. void Text::UpdateText(bool onResize)
  341. {
  342. rowWidths_.Clear();
  343. printText_.Clear();
  344. if (font_)
  345. {
  346. FontFace* face = font_->GetFace(fontSize_);
  347. if (!face)
  348. return;
  349. rowHeight_ = face->GetRowHeight();
  350. int width = 0;
  351. int height = 0;
  352. int rowWidth = 0;
  353. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  354. // First see if the text must be split up
  355. if (!wordWrap_)
  356. {
  357. printText_ = unicodeText_;
  358. printToText_.Resize(printText_.Size());
  359. for (unsigned i = 0; i < printText_.Size(); ++i)
  360. printToText_[i] = i;
  361. }
  362. else
  363. {
  364. int maxWidth = GetWidth();
  365. unsigned nextBreak = 0;
  366. unsigned lineStart = 0;
  367. printToText_.Clear();
  368. for (unsigned i = 0; i < unicodeText_.Size(); ++i)
  369. {
  370. unsigned j;
  371. unsigned c = unicodeText_[i];
  372. if (c != '\n')
  373. {
  374. bool ok = true;
  375. if (nextBreak <= i)
  376. {
  377. int futureRowWidth = rowWidth;
  378. for (j = i; j < unicodeText_.Size(); ++j)
  379. {
  380. unsigned d = unicodeText_[j];
  381. if (d == ' ' || d == '\n')
  382. {
  383. nextBreak = j;
  384. break;
  385. }
  386. const FontGlyph* glyph = face->GetGlyph(d);
  387. if (glyph)
  388. {
  389. futureRowWidth += glyph->advanceX_;
  390. if (j < unicodeText_.Size() - 1)
  391. futureRowWidth += face->GetKerning(d, unicodeText_[j + 1]);
  392. }
  393. if (d == '-' && futureRowWidth <= maxWidth)
  394. {
  395. nextBreak = j + 1;
  396. break;
  397. }
  398. if (futureRowWidth > maxWidth)
  399. {
  400. ok = false;
  401. break;
  402. }
  403. }
  404. }
  405. if (!ok)
  406. {
  407. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  408. if (nextBreak == lineStart)
  409. {
  410. while (i < j)
  411. {
  412. printText_.Push(unicodeText_[i]);
  413. printToText_.Push(i);
  414. ++i;
  415. }
  416. }
  417. // Eliminate spaces that have been copied before the forced break
  418. while (printText_.Size() && printText_.Back() == ' ')
  419. {
  420. printText_.Pop();
  421. printToText_.Pop();
  422. }
  423. printText_.Push('\n');
  424. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  425. rowWidth = 0;
  426. nextBreak = lineStart = i;
  427. }
  428. if (i < unicodeText_.Size())
  429. {
  430. // When copying a space, position is allowed to be over row width
  431. c = unicodeText_[i];
  432. const FontGlyph* glyph = face->GetGlyph(c);
  433. if (glyph)
  434. {
  435. rowWidth += glyph->advanceX_;
  436. if (i < unicodeText_.Size() - 1)
  437. rowWidth += face->GetKerning(c, unicodeText_[i + 1]);
  438. }
  439. if (rowWidth <= maxWidth)
  440. {
  441. printText_.Push(c);
  442. printToText_.Push(i);
  443. }
  444. }
  445. }
  446. else
  447. {
  448. printText_.Push('\n');
  449. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  450. rowWidth = 0;
  451. nextBreak = lineStart = i;
  452. }
  453. }
  454. }
  455. rowWidth = 0;
  456. for (unsigned i = 0; i < printText_.Size(); ++i)
  457. {
  458. unsigned c = printText_[i];
  459. if (c != '\n')
  460. {
  461. const FontGlyph* glyph = face->GetGlyph(c);
  462. if (glyph)
  463. {
  464. rowWidth += glyph->advanceX_;
  465. if (i < printText_.Size() - 1)
  466. rowWidth += face->GetKerning(c, printText_[i + 1]);
  467. }
  468. }
  469. else
  470. {
  471. width = Max(width, rowWidth);
  472. height += rowHeight;
  473. rowWidths_.Push(rowWidth);
  474. rowWidth = 0;
  475. }
  476. }
  477. if (rowWidth)
  478. {
  479. width = Max(width, rowWidth);
  480. height += rowHeight;
  481. rowWidths_.Push(rowWidth);
  482. }
  483. // Set at least one row height even if text is empty
  484. if (!height)
  485. height = rowHeight;
  486. // Set minimum and current size according to the text size, but respect fixed width if set
  487. if (!IsFixedWidth())
  488. {
  489. SetMinWidth(wordWrap_ ? 0 : width);
  490. SetWidth(width);
  491. }
  492. SetFixedHeight(height);
  493. charLocationsDirty_ = true;
  494. }
  495. else
  496. {
  497. // No font, nothing to render
  498. pageGlyphLocations_.Clear();
  499. }
  500. // If wordwrap is on, parent may need layout update to correct for overshoot in size. However, do not do this when the
  501. // update is a response to resize, as that could cause infinite recursion
  502. if (wordWrap_ && !onResize)
  503. {
  504. UIElement* parent = GetParent();
  505. if (parent && parent->GetLayoutMode() != LM_FREE)
  506. parent->UpdateLayout();
  507. }
  508. }
  509. void Text::UpdateCharLocations()
  510. {
  511. // Remember the font face to see if it's still valid when it's time to render
  512. FontFace* face = font_ ? font_->GetFace(fontSize_) : (FontFace*)0;
  513. if (!face)
  514. return;
  515. fontFace_ = face;
  516. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  517. // Store position & size of each character, and locations per texture page
  518. unsigned numChars = unicodeText_.Size();
  519. charLocations_.Resize(numChars + 1);
  520. pageGlyphLocations_.Resize(face->GetTextures().Size());
  521. for (unsigned i = 0; i < pageGlyphLocations_.Size(); ++i)
  522. pageGlyphLocations_[i].Clear();
  523. IntVector2 offset = font_->GetTotalGlyphOffset(fontSize_);
  524. unsigned rowIndex = 0;
  525. unsigned lastFilled = 0;
  526. int x = GetRowStartPosition(rowIndex) + offset.x_;
  527. int y = offset.y_;
  528. for (unsigned i = 0; i < printText_.Size(); ++i)
  529. {
  530. CharLocation loc;
  531. loc.position_ = IntVector2(x, y);
  532. unsigned c = printText_[i];
  533. if (c != '\n')
  534. {
  535. const FontGlyph* glyph = face->GetGlyph(c);
  536. loc.size_ = IntVector2(glyph ? glyph->advanceX_ : 0, rowHeight_);
  537. if (glyph)
  538. {
  539. // Store glyph's location for rendering. Verify that glyph page is valid
  540. if (glyph->page_ < pageGlyphLocations_.Size())
  541. pageGlyphLocations_[glyph->page_].Push(GlyphLocation(x, y, glyph));
  542. x += glyph->advanceX_;
  543. if (i < printText_.Size() - 1)
  544. x += face->GetKerning(c, printText_[i + 1]);
  545. }
  546. }
  547. else
  548. {
  549. loc.size_ = IntVector2::ZERO;
  550. x = GetRowStartPosition(++rowIndex);
  551. y += rowHeight;
  552. }
  553. // Fill gaps in case characters were skipped from printing
  554. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  555. charLocations_[j] = loc;
  556. lastFilled = printToText_[i] + 1;
  557. }
  558. // Store the ending position
  559. charLocations_[numChars].position_ = IntVector2(x, y);
  560. charLocations_[numChars].size_ = IntVector2::ZERO;
  561. charLocationsDirty_ = false;
  562. }
  563. void Text::ValidateSelection()
  564. {
  565. unsigned textLength = unicodeText_.Size();
  566. if (textLength)
  567. {
  568. if (selectionStart_ >= textLength)
  569. selectionStart_ = textLength - 1;
  570. if (selectionStart_ + selectionLength_ > textLength)
  571. selectionLength_ = textLength - selectionStart_;
  572. }
  573. else
  574. {
  575. selectionStart_ = 0;
  576. selectionLength_ = 0;
  577. }
  578. }
  579. int Text::GetRowStartPosition(unsigned rowIndex) const
  580. {
  581. int rowWidth = 0;
  582. if (rowIndex < rowWidths_.Size())
  583. rowWidth = rowWidths_[rowIndex];
  584. int ret = GetIndentWidth();
  585. switch (textAlignment_)
  586. {
  587. case HA_LEFT:
  588. break;
  589. case HA_CENTER:
  590. ret += (GetSize().x_ - rowWidth) / 2;
  591. break;
  592. case HA_RIGHT:
  593. ret += GetSize().x_ - rowWidth;
  594. break;
  595. }
  596. return ret;
  597. }
  598. void Text::ConstructBatch(UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int dx, int dy, Color* color,
  599. float depthBias)
  600. {
  601. unsigned startDataSize = pageBatch.vertexData_->Size();
  602. if (!color)
  603. pageBatch.SetDefaultColor();
  604. else
  605. pageBatch.SetColor(*color);
  606. for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
  607. {
  608. const GlyphLocation& glyphLocation = pageGlyphLocation[i];
  609. const FontGlyph& glyph = *glyphLocation.glyph_;
  610. pageBatch.AddQuad(dx + glyphLocation.x_ + glyph.offsetX_, dy + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
  611. glyph.height_, glyph.x_, glyph.y_);
  612. }
  613. if (depthBias != 0.0f)
  614. {
  615. unsigned dataSize = pageBatch.vertexData_->Size();
  616. for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
  617. pageBatch.vertexData_->At(i + 2) += depthBias;
  618. }
  619. }
  620. }