Text.cpp 23 KB

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