Text.cpp 24 KB

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