Text.cpp 22 KB

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