Text.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. charPositionsDirty_(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. // Hovering and/or whole selection batch
  97. if ((hovering_ && hoverColor_.a_ > 0.0) || (selected_ && selectionColor_.a_ > 0.0f))
  98. {
  99. bool both = hovering_ && selected_ && hoverColor_.a_ > 0.0 && selectionColor_.a_ > 0.0f;
  100. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  101. batch.SetColor(both ? selectionColor_.Lerp(hoverColor_, 0.5f) : (selected_ && selectionColor_.a_ > 0.0f ?
  102. selectionColor_: hoverColor_));
  103. batch.AddQuad(0, 0, GetWidth(), GetHeight(), 0, 0);
  104. UIBatch::AddOrMerge(batch, batches);
  105. }
  106. // Partial selection batch
  107. if (!selected_ && selectionLength_ && charSizes_.Size() >= selectionStart_ + selectionLength_ && selectionColor_.a_ > 0.0f)
  108. {
  109. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  110. batch.SetColor(selectionColor_);
  111. IntVector2 currentStart = charPositions_[selectionStart_];
  112. IntVector2 currentEnd = currentStart;
  113. for (unsigned i = selectionStart_; i < selectionStart_ + selectionLength_; ++i)
  114. {
  115. // Check if row changes, and start a new quad in that case
  116. if (charSizes_[i].x_ && charSizes_[i].y_)
  117. {
  118. if (charPositions_[i].y_ != currentStart.y_)
  119. {
  120. batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
  121. 0, 0);
  122. currentStart = charPositions_[i];
  123. currentEnd = currentStart + charSizes_[i];
  124. }
  125. else
  126. {
  127. currentEnd.x_ += charSizes_[i].x_;
  128. currentEnd.y_ = Max(currentStart.y_ + charSizes_[i].y_, currentEnd.y_);
  129. }
  130. }
  131. }
  132. if (currentEnd != currentStart)
  133. {
  134. batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
  135. 0, 0);
  136. }
  137. UIBatch::AddOrMerge(batch, batches);
  138. }
  139. // Text batch
  140. if (font_)
  141. {
  142. FontFace* face = font_->GetFace(fontSize_);
  143. if (!face)
  144. return;
  145. // If face has changed or char positions are not valid anymore, update before rendering
  146. if (charPositionsDirty_ || !fontFace_ || face != fontFace_)
  147. UpdateCharPositions();
  148. // If face uses mutable glyphs mechanism, reacquire glyphs before rendering to make sure they are in the texture
  149. else if (face->HasMutableGlyphs())
  150. {
  151. for (unsigned i = 0; i < printText_.Size(); ++i)
  152. face->GetGlyph(printText_[i]);
  153. }
  154. const Vector<SharedPtr<Texture2D> >& textures = face->GetTextures();
  155. for (unsigned n = 0; n < textures.Size() && n < pageGlyphLocations_.Size(); ++n)
  156. {
  157. // One batch per texture/page
  158. UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, textures[n], &vertexData);
  159. const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations_[n];
  160. switch (textEffect_)
  161. {
  162. case TE_NONE:
  163. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  164. break;
  165. case TE_SHADOW:
  166. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  167. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  168. break;
  169. case TE_STROKE:
  170. ConstructBatch(pageBatch, pageGlyphLocation, -1, -1, &effectColor_, effectDepthBias_);
  171. ConstructBatch(pageBatch, pageGlyphLocation, 0, -1, &effectColor_, effectDepthBias_);
  172. ConstructBatch(pageBatch, pageGlyphLocation, 1, -1, &effectColor_, effectDepthBias_);
  173. ConstructBatch(pageBatch, pageGlyphLocation, -1, 0, &effectColor_, effectDepthBias_);
  174. ConstructBatch(pageBatch, pageGlyphLocation, 1, 0, &effectColor_, effectDepthBias_);
  175. ConstructBatch(pageBatch, pageGlyphLocation, -1, 1, &effectColor_, effectDepthBias_);
  176. ConstructBatch(pageBatch, pageGlyphLocation, 0, 1, &effectColor_, effectDepthBias_);
  177. ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
  178. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  179. break;
  180. }
  181. UIBatch::AddOrMerge(pageBatch, batches);
  182. }
  183. }
  184. // Reset hovering for next frame
  185. hovering_ = false;
  186. }
  187. void Text::OnResize()
  188. {
  189. if (wordWrap_)
  190. UpdateText();
  191. else
  192. charPositionsDirty_ = true;
  193. }
  194. void Text::OnIndentSet()
  195. {
  196. charPositionsDirty_ = 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. charPositionsDirty_ = 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. const PODVector<IntVector2>& Text::GetCharPositions()
  284. {
  285. if (charPositionsDirty_)
  286. UpdateCharPositions();
  287. return charPositions_;
  288. }
  289. const PODVector<IntVector2>& Text::GetCharSizes()
  290. {
  291. if (charPositionsDirty_)
  292. UpdateCharPositions();
  293. return charSizes_;
  294. }
  295. void Text::SetFontAttr(ResourceRef value)
  296. {
  297. ResourceCache* cache = GetSubsystem<ResourceCache>();
  298. font_ = cache->GetResource<Font>(value.name_);
  299. }
  300. ResourceRef Text::GetFontAttr() const
  301. {
  302. return GetResourceRef(font_, Font::GetTypeStatic());
  303. }
  304. bool Text::FilterImplicitAttributes(XMLElement& dest) const
  305. {
  306. if (!UIElement::FilterImplicitAttributes(dest))
  307. return false;
  308. if (!IsFixedWidth())
  309. {
  310. if (!RemoveChildXML(dest, "Size"))
  311. return false;
  312. if (!RemoveChildXML(dest, "Min Size"))
  313. return false;
  314. if (!RemoveChildXML(dest, "Max Size"))
  315. return false;
  316. }
  317. return true;
  318. }
  319. void Text::UpdateText()
  320. {
  321. rowWidths_.Clear();
  322. printText_.Clear();
  323. if (font_)
  324. {
  325. FontFace* face = font_->GetFace(fontSize_);
  326. if (!face)
  327. return;
  328. rowHeight_ = face->GetRowHeight();
  329. int width = 0;
  330. int height = 0;
  331. int rowWidth = 0;
  332. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  333. // First see if the text must be split up
  334. if (!wordWrap_)
  335. {
  336. printText_ = unicodeText_;
  337. printToText_.Resize(printText_.Size());
  338. for (unsigned i = 0; i < printText_.Size(); ++i)
  339. printToText_[i] = i;
  340. }
  341. else
  342. {
  343. int maxWidth = GetWidth();
  344. unsigned nextBreak = 0;
  345. unsigned lineStart = 0;
  346. printToText_.Clear();
  347. for (unsigned i = 0; i < unicodeText_.Size(); ++i)
  348. {
  349. unsigned j;
  350. unsigned c = unicodeText_[i];
  351. if (c != '\n')
  352. {
  353. bool ok = true;
  354. if (nextBreak <= i)
  355. {
  356. int futureRowWidth = rowWidth;
  357. for (j = i; j < unicodeText_.Size(); ++j)
  358. {
  359. unsigned d = unicodeText_[j];
  360. if (d == ' ' || d == '\n')
  361. {
  362. nextBreak = j;
  363. break;
  364. }
  365. const FontGlyph* glyph = face->GetGlyph(d);
  366. if (glyph)
  367. {
  368. futureRowWidth += glyph->advanceX_;
  369. if (j < unicodeText_.Size() - 1)
  370. futureRowWidth += face->GetKerning(d, unicodeText_[j + 1]);
  371. }
  372. if (d == '-' && futureRowWidth <= maxWidth)
  373. {
  374. nextBreak = j + 1;
  375. break;
  376. }
  377. if (futureRowWidth > maxWidth)
  378. {
  379. ok = false;
  380. break;
  381. }
  382. }
  383. }
  384. if (!ok)
  385. {
  386. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  387. if (nextBreak == lineStart)
  388. {
  389. while (i < j)
  390. {
  391. printText_.Push(unicodeText_[i]);
  392. printToText_.Push(i);
  393. ++i;
  394. }
  395. }
  396. // Eliminate spaces that have been copied before the forced break
  397. while (printText_.Size() && printText_.Back() == ' ')
  398. {
  399. printText_.Pop();
  400. printToText_.Pop();
  401. }
  402. printText_.Push('\n');
  403. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  404. rowWidth = 0;
  405. nextBreak = lineStart = i;
  406. }
  407. if (i < unicodeText_.Size())
  408. {
  409. // When copying a space, position is allowed to be over row width
  410. c = unicodeText_[i];
  411. const FontGlyph* glyph = face->GetGlyph(c);
  412. if (glyph)
  413. {
  414. rowWidth += glyph->advanceX_;
  415. if (i < text_.Length() - 1)
  416. rowWidth += face->GetKerning(c, unicodeText_[i + 1]);
  417. }
  418. if (rowWidth <= maxWidth)
  419. {
  420. printText_.Push(c);
  421. printToText_.Push(i);
  422. }
  423. }
  424. }
  425. else
  426. {
  427. printText_.Push('\n');
  428. printToText_.Push(Min((int)i, (int)unicodeText_.Size() - 1));
  429. rowWidth = 0;
  430. nextBreak = lineStart = i;
  431. }
  432. }
  433. }
  434. rowWidth = 0;
  435. for (unsigned i = 0; i < printText_.Size(); ++i)
  436. {
  437. unsigned c = printText_[i];
  438. if (c != '\n')
  439. {
  440. const FontGlyph* glyph = face->GetGlyph(c);
  441. if (glyph)
  442. {
  443. rowWidth += glyph->advanceX_;
  444. if (i < printText_.Size() - 1)
  445. rowWidth += face->GetKerning(c, printText_[i + 1]);
  446. }
  447. }
  448. else
  449. {
  450. width = Max(width, rowWidth);
  451. height += rowHeight;
  452. rowWidths_.Push(rowWidth);
  453. rowWidth = 0;
  454. }
  455. }
  456. if (rowWidth)
  457. {
  458. width = Max(width, rowWidth);
  459. height += rowHeight;
  460. rowWidths_.Push(rowWidth);
  461. }
  462. // Set at least one row height even if text is empty
  463. if (!height)
  464. height = rowHeight;
  465. // Set minimum and current size according to the text size, but respect fixed width if set
  466. if (!IsFixedWidth())
  467. {
  468. SetMinWidth(wordWrap_ ? 0 : width);
  469. SetWidth(width);
  470. }
  471. SetFixedHeight(height);
  472. charPositionsDirty_ = true;
  473. }
  474. else
  475. {
  476. // No font, nothing to render
  477. pageGlyphLocations_.Clear();
  478. }
  479. }
  480. void Text::UpdateCharPositions()
  481. {
  482. if (font_)
  483. {
  484. // Remember the font face to see if it's still valid when it's time to render
  485. FontFace* face = font_->GetFace(fontSize_);
  486. if (!face)
  487. return;
  488. fontFace_ = face;
  489. int rowHeight = (int)(rowSpacing_ * rowHeight_);
  490. // Store position & size of each character, and locations per texture page
  491. charPositions_.Resize(unicodeText_.Size() + 1);
  492. charSizes_.Resize(unicodeText_.Size());
  493. pageGlyphLocations_.Resize(face->GetTextures().Size());
  494. for (unsigned i = 0; i < pageGlyphLocations_.Size(); ++i)
  495. pageGlyphLocations_[i].Clear();
  496. unsigned rowIndex = 0;
  497. unsigned lastFilled = 0;
  498. int x = GetRowStartPosition(rowIndex);
  499. int y = 0;
  500. for (unsigned i = 0; i < printText_.Size(); ++i)
  501. {
  502. IntVector2 pos(x, y);
  503. // Fill gaps in case characters were skipped from printing
  504. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  505. charPositions_[j] = pos;
  506. unsigned c = printText_[i];
  507. if (c != '\n')
  508. {
  509. const FontGlyph* glyph = face->GetGlyph(c);
  510. IntVector2 size(glyph ? glyph->advanceX_ : 0, rowHeight_);
  511. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  512. charSizes_[j] = size;
  513. if (glyph)
  514. {
  515. // Store glyph's location for rendering. Verify that glyph page is valid
  516. if (glyph->page_ < pageGlyphLocations_.Size())
  517. pageGlyphLocations_[glyph->page_].Push(GlyphLocation(x, y, glyph));
  518. x += glyph->advanceX_;
  519. if (i < printText_.Size() - 1)
  520. x += face->GetKerning(c, printText_[i + 1]);
  521. }
  522. }
  523. else
  524. {
  525. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  526. charSizes_[j] = IntVector2::ZERO;
  527. x = GetRowStartPosition(++rowIndex);
  528. y += rowHeight;
  529. }
  530. lastFilled = printToText_[i] + 1;
  531. }
  532. // Store the ending position
  533. charPositions_[unicodeText_.Size()] = IntVector2(x, y);
  534. charPositionsDirty_ = false;
  535. }
  536. }
  537. void Text::ValidateSelection()
  538. {
  539. unsigned textLength = unicodeText_.Size();
  540. if (textLength)
  541. {
  542. if (selectionStart_ >= textLength)
  543. selectionStart_ = textLength - 1;
  544. if (selectionStart_ + selectionLength_ > textLength)
  545. selectionLength_ = textLength - selectionStart_;
  546. }
  547. else
  548. {
  549. selectionStart_ = 0;
  550. selectionLength_ = 0;
  551. }
  552. }
  553. int Text::GetRowStartPosition(unsigned rowIndex) const
  554. {
  555. int rowWidth = 0;
  556. if (rowIndex < rowWidths_.Size())
  557. rowWidth = rowWidths_[rowIndex];
  558. int ret = GetIndentWidth();
  559. switch (textAlignment_)
  560. {
  561. case HA_LEFT:
  562. break;
  563. case HA_CENTER:
  564. ret += (GetSize().x_ - rowWidth) / 2;
  565. break;
  566. case HA_RIGHT:
  567. ret += GetSize().x_ - rowWidth;
  568. break;
  569. }
  570. return ret;
  571. }
  572. void Text::ConstructBatch(UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int dx, int dy, Color* color,
  573. float depthBias)
  574. {
  575. unsigned startDataSize = pageBatch.vertexData_->Size();
  576. if (!color)
  577. pageBatch.SetDefaultColor();
  578. else
  579. pageBatch.SetColor(*color);
  580. for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
  581. {
  582. const GlyphLocation& glyphLocation = pageGlyphLocation[i];
  583. const FontGlyph& glyph = *glyphLocation.glyph_;
  584. pageBatch.AddQuad(dx + glyphLocation.x_ + glyph.offsetX_, dy + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
  585. glyph.height_, glyph.x_, glyph.y_);
  586. }
  587. if (depthBias != 0.0f)
  588. {
  589. unsigned dataSize = pageBatch.vertexData_->Size();
  590. for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
  591. pageBatch.vertexData_->At(i + 2) += depthBias;
  592. }
  593. }
  594. }