Text.cpp 22 KB

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