Text.cpp 23 KB

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