Text.cpp 24 KB

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