Text.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Core/Profiler.h"
  6. #include "../GraphicsAPI/Texture2D.h"
  7. #include "../IO/Log.h"
  8. #include "../Resource/Localization.h"
  9. #include "../Resource/ResourceCache.h"
  10. #include "../Resource/ResourceEvents.h"
  11. #include "../UI/Font.h"
  12. #include "../UI/FontFace.h"
  13. #include "../UI/Text.h"
  14. #include "../DebugNew.h"
  15. namespace Urho3D
  16. {
  17. const char* textEffects[] =
  18. {
  19. "None",
  20. "Shadow",
  21. "Stroke",
  22. nullptr
  23. };
  24. static const float MIN_ROW_SPACING = 0.5f;
  25. extern const char* horizontalAlignments[];
  26. extern const char* UI_CATEGORY;
  27. Text::Text(Context* context) :
  28. UISelectable(context),
  29. fontSize_(DEFAULT_FONT_SIZE),
  30. textAlignment_(HA_LEFT),
  31. rowSpacing_(1.0f),
  32. wordWrap_(false),
  33. autoLocalizable_(false),
  34. charLocationsDirty_(true),
  35. selectionStart_(0),
  36. selectionLength_(0),
  37. textEffect_(TE_NONE),
  38. shadowOffset_(IntVector2(1, 1)),
  39. strokeThickness_(1),
  40. roundStroke_(false),
  41. effectColor_(Color::BLACK),
  42. effectDepthBias_(0.0f),
  43. rowHeight_(0)
  44. {
  45. // By default Text does not derive opacity from parent elements
  46. useDerivedOpacity_ = false;
  47. }
  48. Text::~Text() = default;
  49. void Text::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<Text>(UI_CATEGORY);
  52. URHO3D_COPY_BASE_ATTRIBUTES(UISelectable);
  53. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Use Derived Opacity", false);
  54. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_FILE);
  55. URHO3D_ATTRIBUTE("Font Size", float, fontSize_, DEFAULT_FONT_SIZE, AM_FILE);
  56. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Text", GetTextAttr, SetTextAttr, String, String::EMPTY, AM_FILE);
  57. URHO3D_ENUM_ATTRIBUTE("Text Alignment", textAlignment_, horizontalAlignments, HA_LEFT, AM_FILE);
  58. URHO3D_ATTRIBUTE("Row Spacing", float, rowSpacing_, 1.0f, AM_FILE);
  59. URHO3D_ATTRIBUTE("Word Wrap", bool, wordWrap_, false, AM_FILE);
  60. URHO3D_ACCESSOR_ATTRIBUTE("Auto Localizable", GetAutoLocalizable, SetAutoLocalizable, bool, false, AM_FILE);
  61. URHO3D_ENUM_ATTRIBUTE("Text Effect", textEffect_, textEffects, TE_NONE, AM_FILE);
  62. URHO3D_ATTRIBUTE("Shadow Offset", IntVector2, shadowOffset_, IntVector2(1, 1), AM_FILE);
  63. URHO3D_ATTRIBUTE("Stroke Thickness", int, strokeThickness_, 1, AM_FILE);
  64. URHO3D_ATTRIBUTE("Round Stroke", bool, roundStroke_, false, AM_FILE);
  65. URHO3D_ACCESSOR_ATTRIBUTE("Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_FILE);
  66. // Change the default value for UseDerivedOpacity
  67. context->GetAttribute<Text>("Use Derived Opacity")->defaultValue_ = false;
  68. }
  69. void Text::ApplyAttributes()
  70. {
  71. UISelectable::ApplyAttributes();
  72. // Localize now if attributes were loaded out-of-order
  73. if (autoLocalizable_ && stringId_.Length())
  74. {
  75. auto* l10n = GetSubsystem<Localization>();
  76. text_ = l10n->Get(stringId_);
  77. }
  78. DecodeToUnicode();
  79. fontSize_ = Max(fontSize_, 1);
  80. strokeThickness_ = Abs(strokeThickness_);
  81. ValidateSelection();
  82. UpdateText();
  83. }
  84. void Text::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  85. {
  86. FontFace* face = font_ ? font_->GetFace(fontSize_) : nullptr;
  87. if (!face)
  88. {
  89. hovering_ = false;
  90. return;
  91. }
  92. // If face has changed or char locations are not valid anymore, update before rendering
  93. if (charLocationsDirty_ || !fontFace_ || face != fontFace_)
  94. UpdateCharLocations();
  95. // If face uses mutable glyphs mechanism, reacquire glyphs before rendering to make sure they are in the texture
  96. else if (face->HasMutableGlyphs())
  97. {
  98. for (unsigned i = 0; i < printText_.Size(); ++i)
  99. face->GetGlyph(printText_[i]);
  100. }
  101. // Hovering and/or whole selection batch
  102. UISelectable::GetBatches(batches, vertexData, currentScissor);
  103. // Partial selection batch
  104. if (!selected_ && selectionLength_ && charLocations_.Size() >= selectionStart_ + selectionLength_ && selectionColor_.a_ > 0.0f)
  105. {
  106. UIBatch batch(this, BLEND_ALPHA, currentScissor, nullptr, &vertexData);
  107. batch.SetColor(selectionColor_);
  108. Vector2 currentStart = charLocations_[selectionStart_].position_;
  109. Vector2 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 (charLocations_[i].size_ != Vector2::ZERO)
  114. {
  115. if (charLocations_[i].position_.y_ != currentStart.y_)
  116. {
  117. batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_,
  118. currentEnd.y_ - currentStart.y_, 0, 0);
  119. currentStart = charLocations_[i].position_;
  120. currentEnd = currentStart + charLocations_[i].size_;
  121. }
  122. else
  123. {
  124. currentEnd.x_ += charLocations_[i].size_.x_;
  125. currentEnd.y_ = Max(currentStart.y_ + charLocations_[i].size_.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_, 0, 0);
  132. }
  133. UIBatch::AddOrMerge(batch, batches);
  134. }
  135. // Text batch
  136. TextEffect textEffect = font_->IsSDFFont() ? TE_NONE : textEffect_;
  137. const Vector<SharedPtr<Texture2D> >& textures = face->GetTextures();
  138. for (unsigned n = 0; n < textures.Size() && n < pageGlyphLocations_.Size(); ++n)
  139. {
  140. // One batch per texture/page
  141. UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, textures[n], &vertexData);
  142. const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations_[n];
  143. switch (textEffect)
  144. {
  145. case TE_NONE:
  146. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  147. break;
  148. case TE_SHADOW:
  149. ConstructBatch(pageBatch, pageGlyphLocation, shadowOffset_.x_, shadowOffset_.y_, &effectColor_, effectDepthBias_);
  150. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  151. break;
  152. case TE_STROKE:
  153. if (roundStroke_)
  154. {
  155. // Samples should be even or glyph may be redrawn in wrong x y pos making stroke corners rough
  156. // Adding to thickness helps with thickness of 1 not having enought samples for this formula
  157. // or certain fonts with reflex corners requiring more glyph samples for a smooth stroke when large
  158. int thickness = Min(strokeThickness_, fontSize_);
  159. int samples = thickness * thickness + (thickness % 2 == 0 ? 4 : 3);
  160. float angle = 360.f / samples;
  161. auto floatThickness = (float)thickness;
  162. for (int i = 0; i < samples; ++i)
  163. {
  164. float x = Cos(angle * i) * floatThickness;
  165. float y = Sin(angle * i) * floatThickness;
  166. ConstructBatch(pageBatch, pageGlyphLocation, x, y, &effectColor_, effectDepthBias_);
  167. }
  168. }
  169. else
  170. {
  171. int thickness = Min(strokeThickness_, fontSize_);
  172. int x, y;
  173. for (x = -thickness; x <= thickness; ++x)
  174. {
  175. for (y = -thickness; y <= thickness; ++y)
  176. {
  177. // Don't draw glyphs that aren't on the edges
  178. if (x > -thickness && x < thickness &&
  179. y > -thickness && y < thickness)
  180. continue;
  181. ConstructBatch(pageBatch, pageGlyphLocation, x, y, &effectColor_, effectDepthBias_);
  182. }
  183. }
  184. }
  185. ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
  186. break;
  187. }
  188. UIBatch::AddOrMerge(pageBatch, batches);
  189. }
  190. }
  191. void Text::OnResize(const IntVector2& newSize, const IntVector2& delta)
  192. {
  193. if (wordWrap_)
  194. UpdateText(true);
  195. else
  196. charLocationsDirty_ = true;
  197. }
  198. void Text::OnIndentSet()
  199. {
  200. charLocationsDirty_ = true;
  201. }
  202. bool Text::SetFont(const String& fontName, float size)
  203. {
  204. auto* cache = GetSubsystem<ResourceCache>();
  205. return SetFont(cache->GetResource<Font>(fontName), size);
  206. }
  207. bool Text::SetFont(Font* font, float size)
  208. {
  209. if (!font)
  210. {
  211. URHO3D_LOGERROR("Null font for Text");
  212. return false;
  213. }
  214. if (font != font_ || size != fontSize_)
  215. {
  216. font_ = font;
  217. fontSize_ = Max(size, 1);
  218. UpdateText();
  219. }
  220. return true;
  221. }
  222. bool Text::SetFontSize(float size)
  223. {
  224. // Initial font must be set
  225. if (!font_)
  226. return false;
  227. else
  228. return SetFont(font_, size);
  229. }
  230. void Text::DecodeToUnicode()
  231. {
  232. unicodeText_.Clear();
  233. for (i32 i = 0; i < text_.Length();)
  234. unicodeText_.Push(text_.NextUTF8Char(i));
  235. }
  236. void Text::SetText(const String& text)
  237. {
  238. if (autoLocalizable_)
  239. {
  240. stringId_ = text;
  241. auto* l10n = GetSubsystem<Localization>();
  242. text_ = l10n->Get(stringId_);
  243. }
  244. else
  245. {
  246. text_ = text;
  247. }
  248. DecodeToUnicode();
  249. ValidateSelection();
  250. UpdateText();
  251. }
  252. void Text::SetTextAlignment(HorizontalAlignment align)
  253. {
  254. if (align != textAlignment_)
  255. {
  256. textAlignment_ = align;
  257. charLocationsDirty_ = true;
  258. }
  259. }
  260. void Text::SetRowSpacing(float spacing)
  261. {
  262. if (spacing != rowSpacing_)
  263. {
  264. rowSpacing_ = Max(spacing, MIN_ROW_SPACING);
  265. UpdateText();
  266. }
  267. }
  268. void Text::SetWordwrap(bool enable)
  269. {
  270. if (enable != wordWrap_)
  271. {
  272. wordWrap_ = enable;
  273. UpdateText();
  274. }
  275. }
  276. void Text::SetAutoLocalizable(bool enable)
  277. {
  278. if (enable != autoLocalizable_)
  279. {
  280. autoLocalizable_ = enable;
  281. if (enable)
  282. {
  283. stringId_ = text_;
  284. auto* l10n = GetSubsystem<Localization>();
  285. text_ = l10n->Get(stringId_);
  286. SubscribeToEvent(E_CHANGELANGUAGE, URHO3D_HANDLER(Text, HandleChangeLanguage));
  287. }
  288. else
  289. {
  290. text_ = stringId_;
  291. stringId_ = "";
  292. UnsubscribeFromEvent(E_CHANGELANGUAGE);
  293. }
  294. DecodeToUnicode();
  295. ValidateSelection();
  296. UpdateText();
  297. }
  298. }
  299. void Text::HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
  300. {
  301. auto* l10n = GetSubsystem<Localization>();
  302. text_ = l10n->Get(stringId_);
  303. DecodeToUnicode();
  304. ValidateSelection();
  305. UpdateText();
  306. }
  307. void Text::SetSelection(unsigned start, unsigned length)
  308. {
  309. selectionStart_ = start;
  310. selectionLength_ = length;
  311. ValidateSelection();
  312. }
  313. void Text::ClearSelection()
  314. {
  315. selectionStart_ = 0;
  316. selectionLength_ = 0;
  317. }
  318. void Text::SetTextEffect(TextEffect textEffect)
  319. {
  320. textEffect_ = textEffect;
  321. }
  322. void Text::SetEffectShadowOffset(const IntVector2& offset)
  323. {
  324. shadowOffset_ = offset;
  325. }
  326. void Text::SetEffectStrokeThickness(int thickness)
  327. {
  328. strokeThickness_ = Abs(thickness);
  329. }
  330. void Text::SetEffectRoundStroke(bool roundStroke)
  331. {
  332. roundStroke_ = roundStroke;
  333. }
  334. void Text::SetEffectColor(const Color& effectColor)
  335. {
  336. effectColor_ = effectColor;
  337. }
  338. void Text::SetEffectDepthBias(float bias)
  339. {
  340. effectDepthBias_ = bias;
  341. }
  342. float Text::GetRowWidth(unsigned index) const
  343. {
  344. return index < rowWidths_.Size() ? rowWidths_[index] : 0;
  345. }
  346. Vector2 Text::GetCharPosition(unsigned index)
  347. {
  348. if (charLocationsDirty_)
  349. UpdateCharLocations();
  350. if (charLocations_.Empty())
  351. return Vector2::ZERO;
  352. // For convenience, return the position of the text ending if index exceeded
  353. if (index > charLocations_.Size() - 1)
  354. index = charLocations_.Size() - 1;
  355. return charLocations_[index].position_;
  356. }
  357. Vector2 Text::GetCharSize(unsigned index)
  358. {
  359. if (charLocationsDirty_)
  360. UpdateCharLocations();
  361. if (charLocations_.Size() < 2)
  362. return Vector2::ZERO;
  363. // For convenience, return the size of the last char if index exceeded (last size entry is zero)
  364. if (index > charLocations_.Size() - 2)
  365. index = charLocations_.Size() - 2;
  366. return charLocations_[index].size_;
  367. }
  368. void Text::SetFontAttr(const ResourceRef& value)
  369. {
  370. auto* cache = GetSubsystem<ResourceCache>();
  371. font_ = cache->GetResource<Font>(value.name_);
  372. }
  373. ResourceRef Text::GetFontAttr() const
  374. {
  375. return GetResourceRef(font_, Font::GetTypeStatic());
  376. }
  377. void Text::SetTextAttr(const String& value)
  378. {
  379. text_ = value;
  380. if (autoLocalizable_)
  381. stringId_ = value;
  382. }
  383. String Text::GetTextAttr() const
  384. {
  385. if (autoLocalizable_ && stringId_.Length())
  386. return stringId_;
  387. else
  388. return text_;
  389. }
  390. bool Text::FilterImplicitAttributes(XMLElement& dest) const
  391. {
  392. if (!UISelectable::FilterImplicitAttributes(dest))
  393. return false;
  394. if (!IsFixedWidth())
  395. {
  396. if (!RemoveChildXML(dest, "Size"))
  397. return false;
  398. if (!RemoveChildXML(dest, "Min Size"))
  399. return false;
  400. if (!RemoveChildXML(dest, "Max Size"))
  401. return false;
  402. }
  403. return true;
  404. }
  405. void Text::UpdateText(bool onResize)
  406. {
  407. rowWidths_.Clear();
  408. printText_.Clear();
  409. if (font_)
  410. {
  411. FontFace* face = font_->GetFace(fontSize_);
  412. if (!face)
  413. return;
  414. rowHeight_ = face->GetRowHeight();
  415. int width = 0;
  416. int height = 0;
  417. int rowWidth = 0;
  418. auto rowHeight = RoundToInt(rowSpacing_ * rowHeight_);
  419. // First see if the text must be split up
  420. if (!wordWrap_)
  421. {
  422. printText_ = unicodeText_;
  423. printToText_.Resize(printText_.Size());
  424. for (unsigned i = 0; i < printText_.Size(); ++i)
  425. printToText_[i] = i;
  426. }
  427. else
  428. {
  429. int maxWidth = GetWidth();
  430. unsigned nextBreak = 0;
  431. unsigned lineStart = 0;
  432. printToText_.Clear();
  433. for (unsigned i = 0; i < unicodeText_.Size(); ++i)
  434. {
  435. unsigned j;
  436. u32 c = unicodeText_[i];
  437. if (c != '\n')
  438. {
  439. bool ok = true;
  440. if (nextBreak <= i)
  441. {
  442. int futureRowWidth = rowWidth;
  443. for (j = i; j < unicodeText_.Size(); ++j)
  444. {
  445. unsigned d = unicodeText_[j];
  446. if (d == ' ' || d == '\n')
  447. {
  448. nextBreak = j;
  449. break;
  450. }
  451. const FontGlyph* glyph = face->GetGlyph(d);
  452. if (glyph)
  453. {
  454. futureRowWidth += glyph->advanceX_;
  455. if (j < unicodeText_.Size() - 1)
  456. futureRowWidth += face->GetKerning(d, unicodeText_[j + 1]);
  457. }
  458. if (d == '-' && futureRowWidth <= maxWidth)
  459. {
  460. nextBreak = j + 1;
  461. break;
  462. }
  463. if (futureRowWidth > maxWidth)
  464. {
  465. ok = false;
  466. break;
  467. }
  468. }
  469. }
  470. if (!ok)
  471. {
  472. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  473. if (nextBreak == lineStart)
  474. {
  475. while (i < j)
  476. {
  477. printText_.Push(unicodeText_[i]);
  478. printToText_.Push(i);
  479. ++i;
  480. }
  481. }
  482. // Eliminate spaces that have been copied before the forced break
  483. while (printText_.Size() && printText_.Back() == ' ')
  484. {
  485. printText_.Pop();
  486. printToText_.Pop();
  487. }
  488. printText_.Push('\n');
  489. printToText_.Push(Min(i, unicodeText_.Size() - 1));
  490. rowWidth = 0;
  491. nextBreak = lineStart = i;
  492. }
  493. if (i < unicodeText_.Size())
  494. {
  495. // When copying a space, position is allowed to be over row width
  496. c = unicodeText_[i];
  497. const FontGlyph* glyph = face->GetGlyph(c);
  498. if (glyph)
  499. {
  500. rowWidth += glyph->advanceX_;
  501. if (i < unicodeText_.Size() - 1)
  502. rowWidth += face->GetKerning(c, unicodeText_[i + 1]);
  503. }
  504. if (rowWidth <= maxWidth)
  505. {
  506. printText_.Push(c);
  507. printToText_.Push(i);
  508. }
  509. }
  510. }
  511. else
  512. {
  513. printText_.Push('\n');
  514. printToText_.Push(Min(i, unicodeText_.Size() - 1));
  515. rowWidth = 0;
  516. nextBreak = lineStart = i;
  517. }
  518. }
  519. }
  520. rowWidth = 0;
  521. for (unsigned i = 0; i < printText_.Size(); ++i)
  522. {
  523. c32 c = printText_[i];
  524. if (c != '\n')
  525. {
  526. const FontGlyph* glyph = face->GetGlyph(c);
  527. if (glyph)
  528. {
  529. rowWidth += glyph->advanceX_;
  530. if (i < printText_.Size() - 1)
  531. rowWidth += face->GetKerning(c, printText_[i + 1]);
  532. }
  533. }
  534. else
  535. {
  536. width = Max(width, rowWidth);
  537. height += rowHeight;
  538. rowWidths_.Push(rowWidth);
  539. rowWidth = 0;
  540. }
  541. }
  542. if (rowWidth)
  543. {
  544. width = Max(width, rowWidth);
  545. height += rowHeight;
  546. rowWidths_.Push(rowWidth);
  547. }
  548. // Set at least one row height even if text is empty
  549. if (!height)
  550. height = rowHeight;
  551. // Set minimum and current size according to the text size, but respect fixed width if set
  552. if (!IsFixedWidth())
  553. {
  554. if (wordWrap_)
  555. SetMinWidth(0);
  556. else
  557. {
  558. SetMinWidth(width);
  559. SetWidth(width);
  560. }
  561. }
  562. SetFixedHeight(height);
  563. charLocationsDirty_ = true;
  564. }
  565. else
  566. {
  567. // No font, nothing to render
  568. pageGlyphLocations_.Clear();
  569. }
  570. // If wordwrap is on, parent may need layout update to correct for overshoot in size. However, do not do this when the
  571. // update is a response to resize, as that could cause infinite recursion
  572. if (wordWrap_ && !onResize)
  573. {
  574. UIElement* parent = GetParent();
  575. if (parent && parent->GetLayoutMode() != LM_FREE)
  576. parent->UpdateLayout();
  577. }
  578. }
  579. void Text::UpdateCharLocations()
  580. {
  581. // Remember the font face to see if it's still valid when it's time to render
  582. FontFace* face = font_ ? font_->GetFace(fontSize_) : nullptr;
  583. if (!face)
  584. return;
  585. fontFace_ = face;
  586. auto rowHeight = RoundToInt(rowSpacing_ * rowHeight_);
  587. // Store position & size of each character, and locations per texture page
  588. unsigned numChars = unicodeText_.Size();
  589. charLocations_.Resize(numChars + 1);
  590. pageGlyphLocations_.Resize(face->GetTextures().Size());
  591. for (unsigned i = 0; i < pageGlyphLocations_.Size(); ++i)
  592. pageGlyphLocations_[i].Clear();
  593. IntVector2 offset = font_->GetTotalGlyphOffset(fontSize_);
  594. unsigned rowIndex = 0;
  595. unsigned lastFilled = 0;
  596. float x = Round(GetRowStartPosition(rowIndex) + offset.x_);
  597. float y = Round(offset.y_);
  598. for (unsigned i = 0; i < printText_.Size(); ++i)
  599. {
  600. CharLocation loc;
  601. loc.position_ = Vector2(x, y);
  602. c32 c = printText_[i];
  603. if (c != '\n')
  604. {
  605. const FontGlyph* glyph = face->GetGlyph(c);
  606. loc.size_ = Vector2(glyph ? glyph->advanceX_ : 0, rowHeight_);
  607. if (glyph)
  608. {
  609. // Store glyph's location for rendering. Verify that glyph page is valid
  610. if (glyph->page_ < pageGlyphLocations_.Size())
  611. pageGlyphLocations_[glyph->page_].Push(GlyphLocation(x, y, glyph));
  612. x += glyph->advanceX_;
  613. if (i < printText_.Size() - 1)
  614. x += face->GetKerning(c, printText_[i + 1]);
  615. }
  616. }
  617. else
  618. {
  619. loc.size_ = Vector2::ZERO;
  620. x = GetRowStartPosition(++rowIndex);
  621. y += rowHeight;
  622. }
  623. if (lastFilled > printToText_[i])
  624. lastFilled = printToText_[i];
  625. // Fill gaps in case characters were skipped from printing
  626. for (unsigned j = lastFilled; j <= printToText_[i]; ++j)
  627. charLocations_[j] = loc;
  628. lastFilled = printToText_[i] + 1;
  629. }
  630. // Store the ending position
  631. charLocations_[numChars].position_ = Vector2(x, y);
  632. charLocations_[numChars].size_ = Vector2::ZERO;
  633. charLocationsDirty_ = false;
  634. }
  635. void Text::ValidateSelection()
  636. {
  637. unsigned textLength = unicodeText_.Size();
  638. if (textLength)
  639. {
  640. if (selectionStart_ >= textLength)
  641. selectionStart_ = textLength - 1;
  642. if (selectionStart_ + selectionLength_ > textLength)
  643. selectionLength_ = textLength - selectionStart_;
  644. }
  645. else
  646. {
  647. selectionStart_ = 0;
  648. selectionLength_ = 0;
  649. }
  650. }
  651. int Text::GetRowStartPosition(unsigned rowIndex) const
  652. {
  653. float rowWidth = 0;
  654. if (rowIndex < rowWidths_.Size())
  655. rowWidth = rowWidths_[rowIndex];
  656. int ret = GetIndentWidth();
  657. switch (textAlignment_)
  658. {
  659. case HA_LEFT:
  660. break;
  661. case HA_CENTER:
  662. ret += (GetSize().x_ - rowWidth) / 2;
  663. break;
  664. case HA_RIGHT:
  665. ret += GetSize().x_ - rowWidth;
  666. break;
  667. case HA_CUSTOM:
  668. break;
  669. }
  670. return ret;
  671. }
  672. void Text::ConstructBatch(UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, float dx, float dy, Color* color,
  673. float depthBias)
  674. {
  675. unsigned startDataSize = pageBatch.vertexData_->Size();
  676. if (!color)
  677. pageBatch.SetDefaultColor();
  678. else
  679. pageBatch.SetColor(*color);
  680. for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
  681. {
  682. const GlyphLocation& glyphLocation = pageGlyphLocation[i];
  683. const FontGlyph& glyph = *glyphLocation.glyph_;
  684. pageBatch.AddQuad(dx + glyphLocation.x_ + glyph.offsetX_, dy + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
  685. glyph.height_, glyph.x_, glyph.y_, glyph.texWidth_, glyph.texHeight_);
  686. }
  687. if (depthBias != 0.0f)
  688. {
  689. unsigned dataSize = pageBatch.vertexData_->Size();
  690. for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
  691. pageBatch.vertexData_->At(i + 2) += depthBias;
  692. }
  693. }
  694. }