Text.cpp 24 KB

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