Text.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. 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. const Vector<SharedPtr<Texture2D> >& textures = face->GetTextures();
  158. for (unsigned n = 0; n < textures.Size() && n < pageGlyphLocations_.Size(); ++n)
  159. {
  160. // One batch per texture/page
  161. UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, textures[n], &vertexData);
  162. const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations_[n];
  163. switch (textEffect_)
  164. {
  165. case TE_NONE:
  166. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  167. break;
  168. case TE_SHADOW:
  169. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  170. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  171. break;
  172. case TE_STROKE:
  173. ConstructBatch(pageBatch, pageGlyphLocation, -1, -1, &effectColor_, effectDepthBias_);
  174. ConstructBatch(pageBatch, pageGlyphLocation, 0, -1, &effectColor_, effectDepthBias_);
  175. ConstructBatch(pageBatch, pageGlyphLocation, 1, -1, &effectColor_, effectDepthBias_);
  176. ConstructBatch(pageBatch, pageGlyphLocation, -1, 0, &effectColor_, effectDepthBias_);
  177. ConstructBatch(pageBatch, pageGlyphLocation, 1, 0, &effectColor_, effectDepthBias_);
  178. ConstructBatch(pageBatch, pageGlyphLocation, -1, 1, &effectColor_, effectDepthBias_);
  179. ConstructBatch(pageBatch, pageGlyphLocation, 0, 1, &effectColor_, effectDepthBias_);
  180. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  181. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  182. break;
  183. }
  184. UIBatch::AddOrMerge(pageBatch, batches);
  185. }
  186. // Reset hovering for next frame
  187. hovering_ = false;
  188. }
  189. void Text::OnResize()
  190. {
  191. if (wordWrap_)
  192. UpdateText();
  193. else
  194. charLocationsDirty_ = true;
  195. }
  196. void Text::OnIndentSet()
  197. {
  198. charLocationsDirty_ = true;
  199. }
  200. bool Text::SetFont(const String& fontName, int size)
  201. {
  202. ResourceCache* cache = GetSubsystem<ResourceCache>();
  203. return SetFont(cache->GetResource<Font>(fontName), size);
  204. }
  205. bool Text::SetFont(Font* font, int size)
  206. {
  207. if (!font)
  208. {
  209. LOGERROR("Null font for Text");
  210. return false;
  211. }
  212. if (font != font_ || size != fontSize_)
  213. {
  214. font_ = font;
  215. fontSize_ = Max(size, 1);
  216. UpdateText();
  217. }
  218. return true;
  219. }
  220. void Text::SetText(const String& text)
  221. {
  222. text_ = text;
  223. // Decode to Unicode now
  224. unicodeText_.Clear();
  225. for (unsigned i = 0; i < text_.Length();)
  226. unicodeText_.Push(text_.NextUTF8Char(i));
  227. ValidateSelection();
  228. UpdateText();
  229. }
  230. void Text::SetTextAlignment(HorizontalAlignment align)
  231. {
  232. if (align != textAlignment_)
  233. {
  234. textAlignment_ = align;
  235. charLocationsDirty_ = true;
  236. }
  237. }
  238. void Text::SetRowSpacing(float spacing)
  239. {
  240. if (spacing != rowSpacing_)
  241. {
  242. rowSpacing_ = Max(spacing, MIN_ROW_SPACING);
  243. UpdateText();
  244. }
  245. }
  246. void Text::SetWordwrap(bool enable)
  247. {
  248. if (enable != wordWrap_)
  249. {
  250. wordWrap_ = enable;
  251. UpdateText();
  252. }
  253. }
  254. void Text::SetSelection(unsigned start, unsigned length)
  255. {
  256. selectionStart_ = start;
  257. selectionLength_ = length;
  258. ValidateSelection();
  259. }
  260. void Text::ClearSelection()
  261. {
  262. selectionStart_ = 0;
  263. selectionLength_ = 0;
  264. }
  265. void Text::SetSelectionColor(const Color& color)
  266. {
  267. selectionColor_ = color;
  268. }
  269. void Text::SetHoverColor(const Color& color)
  270. {
  271. hoverColor_ = color;
  272. }
  273. void Text::SetTextEffect(TextEffect textEffect)
  274. {
  275. textEffect_ = textEffect;
  276. }
  277. void Text::SetEffectColor(const Color& effectColor)
  278. {
  279. effectColor_ = effectColor;
  280. }
  281. void Text::SetUsedInText3D(bool usedInText3D)
  282. {
  283. usedInText3D_ = usedInText3D;
  284. }
  285. void Text::SetEffectDepthBias(float bias)
  286. {
  287. effectDepthBias_ = bias;
  288. }
  289. int Text::GetRowWidth(unsigned index) const
  290. {
  291. return index < rowWidths_.Size() ? rowWidths_[index] : 0;
  292. }
  293. IntVector2 Text::GetCharPosition(unsigned index)
  294. {
  295. if (charLocationsDirty_)
  296. UpdateCharLocations();
  297. if (charLocations_.Empty())
  298. return IntVector2::ZERO;
  299. // For convenience, return the position of the text ending if index exceeded
  300. if (index > charLocations_.Size() - 1)
  301. index = charLocations_.Size() - 1;
  302. return charLocations_[index].position_;
  303. }
  304. IntVector2 Text::GetCharSize(unsigned index)
  305. {
  306. if (charLocationsDirty_)
  307. UpdateCharLocations();
  308. if (charLocations_.Size() < 2)
  309. return IntVector2::ZERO;
  310. // For convenience, return the size of the last char if index exceeded (last size entry is zero)
  311. if (index > charLocations_.Size() - 2)
  312. index = charLocations_.Size() - 2;
  313. return charLocations_[index].size_;
  314. }
  315. void Text::SetFontAttr(ResourceRef value)
  316. {
  317. ResourceCache* cache = GetSubsystem<ResourceCache>();
  318. font_ = cache->GetResource<Font>(value.name_);
  319. }
  320. ResourceRef Text::GetFontAttr() const
  321. {
  322. return GetResourceRef(font_, Font::GetTypeStatic());
  323. }
  324. bool Text::FilterImplicitAttributes(XMLElement& dest) const
  325. {
  326. if (!UIElement::FilterImplicitAttributes(dest))
  327. return false;
  328. if (!IsFixedWidth())
  329. {
  330. if (!RemoveChildXML(dest, "Size"))
  331. return false;
  332. if (!RemoveChildXML(dest, "Min Size"))
  333. return false;
  334. if (!RemoveChildXML(dest, "Max Size"))
  335. return false;
  336. }
  337. return true;
  338. }
  339. void Text::UpdateText()
  340. {
  341. rowWidths_.Clear();
  342. printText_.Clear();
  343. if (font_)
  344. {
  345. FontFace* face = font_->GetFace(fontSize_);
  346. if (!face)
  347. return;
  348. rowHeight_ = face->GetRowHeight();
  349. int width = 0;
  350. int height = 0;
  351. int rowWidth = 0;
  352. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  353. // First see if the text must be split up
  354. if (!wordWrap_)
  355. {
  356. printText_ = unicodeText_;
  357. printToText_.Resize(printText_.Size());
  358. for (unsigned i = 0; i < printText_.Size(); ++i)
  359. printToText_[i] = i;
  360. }
  361. else
  362. {
  363. int maxWidth = GetWidth();
  364. unsigned nextBreak = 0;
  365. unsigned lineStart = 0;
  366. printToText_.Clear();
  367. for (unsigned i = 0; i < unicodeText_.Size(); ++i)
  368. {
  369. unsigned j;
  370. unsigned c = unicodeText_[i];
  371. if (c != '\n')
  372. {
  373. bool ok = true;
  374. if (nextBreak <= i)
  375. {
  376. int futureRowWidth = rowWidth;
  377. for (j = i; j < unicodeText_.Size(); ++j)
  378. {
  379. unsigned d = unicodeText_[j];
  380. if (d == ' ' || d == '\n')
  381. {
  382. nextBreak = j;
  383. break;
  384. }
  385. const FontGlyph* glyph = face->GetGlyph(d);
  386. if (glyph)
  387. {
  388. futureRowWidth += glyph->advanceX_;
  389. if (j < unicodeText_.Size() - 1)
  390. futureRowWidth += face->GetKerning(d, unicodeText_[j + 1]);
  391. }
  392. if (d == '-' && futureRowWidth <= maxWidth)
  393. {
  394. nextBreak = j + 1;
  395. break;
  396. }
  397. if (futureRowWidth > maxWidth)
  398. {
  399. ok = false;
  400. break;
  401. }
  402. }
  403. }
  404. if (!ok)
  405. {
  406. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  407. if (nextBreak == lineStart)
  408. {
  409. while (i < j)
  410. {
  411. printText_.Push(unicodeText_[i]);
  412. printToText_.Push(i);
  413. ++i;
  414. }
  415. }
  416. // Eliminate spaces that have been copied before the forced break
  417. while (printText_.Size() && printText_.Back() == ' ')
  418. {
  419. printText_.Pop();
  420. printToText_.Pop();
  421. }
  422. printText_.Push('\n');
  423. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  424. rowWidth = 0;
  425. nextBreak = lineStart = i;
  426. }
  427. if (i < unicodeText_.Size())
  428. {
  429. // When copying a space, position is allowed to be over row width
  430. c = unicodeText_[i];
  431. const FontGlyph* glyph = face->GetGlyph(c);
  432. if (glyph)
  433. {
  434. rowWidth += glyph->advanceX_;
  435. if (i < text_.Length() - 1)
  436. rowWidth += face->GetKerning(c, unicodeText_[i + 1]);
  437. }
  438. if (rowWidth <= maxWidth)
  439. {
  440. printText_.Push(c);
  441. printToText_.Push(i);
  442. }
  443. }
  444. }
  445. else
  446. {
  447. printText_.Push('\n');
  448. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  449. rowWidth = 0;
  450. nextBreak = lineStart = i;
  451. }
  452. }
  453. }
  454. rowWidth = 0;
  455. for (unsigned i = 0; i < printText_.Size(); ++i)
  456. {
  457. unsigned c = printText_[i];
  458. if (c != '\n')
  459. {
  460. const FontGlyph* glyph = face->GetGlyph(c);
  461. if (glyph)
  462. {
  463. rowWidth += glyph->advanceX_;
  464. if (i < printText_.Size() - 1)
  465. rowWidth += face->GetKerning(c, printText_[i + 1]);
  466. }
  467. }
  468. else
  469. {
  470. width = Max(width, rowWidth);
  471. height += rowHeight;
  472. rowWidths_.Push(rowWidth);
  473. rowWidth = 0;
  474. }
  475. }
  476. if (rowWidth)
  477. {
  478. width = Max(width, rowWidth);
  479. height += rowHeight;
  480. rowWidths_.Push(rowWidth);
  481. }
  482. // Set at least one row height even if text is empty
  483. if (!height)
  484. height = rowHeight;
  485. // Set minimum and current size according to the text size, but respect fixed width if set
  486. if (!IsFixedWidth())
  487. {
  488. SetMinWidth(wordWrap_ ? 0 : width);
  489. SetWidth(width);
  490. }
  491. SetFixedHeight(height);
  492. charLocationsDirty_ = true;
  493. }
  494. else
  495. {
  496. // No font, nothing to render
  497. pageGlyphLocations_.Clear();
  498. }
  499. }
  500. void Text::UpdateCharLocations()
  501. {
  502. // Remember the font face to see if it's still valid when it's time to render
  503. FontFace* face = font_ ? font_->GetFace(fontSize_) : (FontFace*)0;
  504. if (!face)
  505. return;
  506. fontFace_ = face;
  507. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  508. // Store position & size of each character, and locations per texture page
  509. unsigned numChars = unicodeText_.Size();
  510. charLocations_.Resize(numChars + 1);
  511. pageGlyphLocations_.Resize(face->GetTextures().Size());
  512. for (unsigned i = 0; i < pageGlyphLocations_.Size(); ++i)
  513. pageGlyphLocations_[i].Clear();
  514. unsigned rowIndex = 0;
  515. unsigned lastFilled = 0;
  516. int x = GetRowStartPosition(rowIndex);
  517. int y = 0;
  518. for (unsigned i = 0; i < printText_.Size(); ++i)
  519. {
  520. CharLocation loc;
  521. loc.position_ = IntVector2(x, y);
  522. unsigned c = printText_[i];
  523. if (c != '\n')
  524. {
  525. const FontGlyph* glyph = face->GetGlyph(c);
  526. loc.size_ = IntVector2(glyph ? glyph->advanceX_ : 0, rowHeight_);
  527. if (glyph)
  528. {
  529. // Store glyph's location for rendering. Verify that glyph page is valid
  530. if (glyph->page_ < pageGlyphLocations_.Size())
  531. pageGlyphLocations_[glyph->page_].Push(GlyphLocation(x, y, glyph));
  532. x += glyph->advanceX_;
  533. if (i < printText_.Size() - 1)
  534. x += face->GetKerning(c, printText_[i + 1]);
  535. }
  536. }
  537. else
  538. {
  539. loc.size_ = IntVector2::ZERO;
  540. x = GetRowStartPosition(++rowIndex);
  541. y += rowHeight;
  542. }
  543. // Fill gaps in case characters were skipped from printing
  544. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  545. charLocations_[j] = loc;
  546. lastFilled = printToText_[i] + 1;
  547. }
  548. // Store the ending position
  549. charLocations_[numChars].position_ = IntVector2(x, y);
  550. charLocations_[numChars].size_ = IntVector2::ZERO;
  551. charLocationsDirty_ = false;
  552. }
  553. void Text::ValidateSelection()
  554. {
  555. unsigned textLength = unicodeText_.Size();
  556. if (textLength)
  557. {
  558. if (selectionStart_ >= textLength)
  559. selectionStart_ = textLength - 1;
  560. if (selectionStart_ + selectionLength_ > textLength)
  561. selectionLength_ = textLength - selectionStart_;
  562. }
  563. else
  564. {
  565. selectionStart_ = 0;
  566. selectionLength_ = 0;
  567. }
  568. }
  569. int Text::GetRowStartPosition(unsigned rowIndex) const
  570. {
  571. int rowWidth = 0;
  572. if (rowIndex < rowWidths_.Size())
  573. rowWidth = rowWidths_[rowIndex];
  574. int ret = GetIndentWidth();
  575. switch (textAlignment_)
  576. {
  577. case HA_LEFT:
  578. break;
  579. case HA_CENTER:
  580. ret += (GetSize().x_ - rowWidth) / 2;
  581. break;
  582. case HA_RIGHT:
  583. ret += GetSize().x_ - rowWidth;
  584. break;
  585. }
  586. return ret;
  587. }
  588. void Text::ConstructBatch(UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int dx, int dy, Color* color,
  589. float depthBias)
  590. {
  591. unsigned startDataSize = pageBatch.vertexData_->Size();
  592. if (!color)
  593. pageBatch.SetDefaultColor();
  594. else
  595. pageBatch.SetColor(*color);
  596. for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
  597. {
  598. const GlyphLocation& glyphLocation = pageGlyphLocation[i];
  599. const FontGlyph& glyph = *glyphLocation.glyph_;
  600. pageBatch.AddQuad(dx + glyphLocation.x_ + glyph.offsetX_, dy + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
  601. glyph.height_, glyph.x_, glyph.y_);
  602. }
  603. if (depthBias != 0.0f)
  604. {
  605. unsigned dataSize = pageBatch.vertexData_->Size();
  606. for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
  607. pageBatch.vertexData_->At(i + 2) += depthBias;
  608. }
  609. }
  610. }