Text.cpp 32 KB

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