| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "Font.h"
- #include "Log.h"
- #include "Profiler.h"
- #include "ResourceCache.h"
- #include "StringUtils.h"
- #include "Text.h"
- #include "Texture2D.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- const char* textEffects[] =
- {
- "None",
- "Shadow",
- "Stroke",
- 0
- };
- static const float MIN_ROW_SPACING = 0.5f;
- extern const char* horizontalAlignments[];
- extern const char* UI_CATEGORY;
- Text::Text(Context* context) :
- UIElement(context),
- fontSize_(DEFAULT_FONT_SIZE),
- textAlignment_(HA_LEFT),
- rowSpacing_(1.0f),
- wordWrap_(false),
- selectionStart_(0),
- selectionLength_(0),
- selectionColor_(Color::TRANSPARENT),
- hoverColor_(Color::TRANSPARENT),
- textEffect_(TE_NONE),
- effectColor_(Color::BLACK),
- effectDepthBias_(0.0f),
- rowHeight_(0)
- {
- // By default Text does not derive opacity from parent elements
- useDerivedOpacity_ = false;
- }
- Text::~Text()
- {
- }
- void Text::RegisterObject(Context* context)
- {
- context->RegisterFactory<Text>(UI_CATEGORY);
- COPY_BASE_ATTRIBUTES(Text, UIElement);
- UPDATE_ATTRIBUTE_DEFAULT_VALUE(Text, "Use Derived Opacity", false);
- ACCESSOR_ATTRIBUTE(Text, VAR_RESOURCEREF, "Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_FILE);
- ATTRIBUTE(Text, VAR_INT, "Font Size", fontSize_, DEFAULT_FONT_SIZE, AM_FILE);
- ATTRIBUTE(Text, VAR_STRING, "Text", text_, String::EMPTY, AM_FILE);
- ENUM_ATTRIBUTE(Text, "Text Alignment", textAlignment_, horizontalAlignments, HA_LEFT, AM_FILE);
- ATTRIBUTE(Text, VAR_FLOAT, "Row Spacing", rowSpacing_, 1.0f, AM_FILE);
- ATTRIBUTE(Text, VAR_BOOL, "Word Wrap", wordWrap_, false, AM_FILE);
- REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Selection Color", GetSelectionColor, SetSelectionColor, Color, Color::TRANSPARENT, AM_FILE);
- REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Hover Color", GetHoverColor, SetHoverColor, Color, Color::TRANSPARENT, AM_FILE);
- ENUM_ATTRIBUTE(Text, "Text Effect", textEffect_, textEffects, TE_NONE, AM_FILE);
- REF_ACCESSOR_ATTRIBUTE(Text, VAR_COLOR, "Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_FILE);
- // Change the default value for UseDerivedOpacity
- context->GetAttribute<Text>("Use Derived Opacity")->defaultValue_ = false;
- }
- void Text::ApplyAttributes()
- {
- UIElement::ApplyAttributes();
- // Decode to Unicode now
- unicodeText_.Clear();
- for (unsigned i = 0; i < text_.Length();)
- unicodeText_.Push(text_.NextUTF8Char(i));
- fontSize_ = Max(fontSize_, 1);
- ValidateSelection();
- UpdateText();
- }
- void Text::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
- {
- // Hovering and/or whole selection batch
- if ((hovering_ && hoverColor_.a_ > 0.0) || (selected_ && selectionColor_.a_ > 0.0f))
- {
- bool both = hovering_ && selected_ && hoverColor_.a_ > 0.0 && selectionColor_.a_ > 0.0f;
- UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
- batch.AddQuad(0, 0, GetWidth(), GetHeight(), 0, 0, 0, 0, both ? selectionColor_.Lerp(hoverColor_, 0.5f) :
- (selected_ && selectionColor_.a_ > 0.0f ? selectionColor_ : hoverColor_));
- UIBatch::AddOrMerge(batch, batches);
- }
- // Partial selection batch
- if (!selected_ && selectionLength_ && charSizes_.Size() >= selectionStart_ + selectionLength_ && selectionColor_.a_ > 0.0f)
- {
- UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
- IntVector2 currentStart = charPositions_[selectionStart_];
- IntVector2 currentEnd = currentStart;
- for (unsigned i = selectionStart_; i < selectionStart_ + selectionLength_; ++i)
- {
- // Check if row changes, and start a new quad in that case
- if (charSizes_[i].x_ && charSizes_[i].y_)
- {
- if (charPositions_[i].y_ != currentStart.y_)
- {
- batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
- 0, 0, 0, 0, selectionColor_);
- currentStart = charPositions_[i];
- currentEnd = currentStart + charSizes_[i];
- }
- else
- {
- currentEnd.x_ += charSizes_[i].x_;
- currentEnd.y_ = Max(currentStart.y_ + charSizes_[i].y_, currentEnd.y_);
- }
- }
- }
- if (currentEnd != currentStart)
- {
- batch.AddQuad(currentStart.x_, currentStart.y_, currentEnd.x_ - currentStart.x_, currentEnd.y_ - currentStart.y_,
- 0, 0, 0, 0, selectionColor_);
- }
- UIBatch::AddOrMerge(batch, batches);
- }
- // Text batch
- if (font_)
- {
- const FontFace* face = font_->GetFace(fontSize_);
- if (!face)
- return;
- if (face->textures_.Size() > 1)
- {
- // Only traversing thru the printText once regardless of number of textures/pages in the font
- Vector<PODVector<GlyphLocation> > pageGlyphLocations(face->textures_.Size());
- unsigned rowIndex = 0;
- int x = GetRowStartPosition(rowIndex);
- int y = 0;
- for (unsigned i = 0; i < printText_.Size(); ++i)
- {
- unsigned c = printText_[i];
- if (c != '\n')
- {
- const FontGlyph* p = face->GetGlyph(c);
- if (!p)
- continue;
- pageGlyphLocations[p->page_].Push(GlyphLocation(x, y, p));
- x += p->advanceX_;
- if (i < printText_.Size() - 1)
- x += face->GetKerning(c, printText_[i + 1]);
- }
- else
- {
- x = GetRowStartPosition(++rowIndex);
- y += rowHeight_;
- }
- }
- for (unsigned n = 0; n < face->textures_.Size(); ++n)
- {
- // One batch per texture/page
- UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, face->textures_[n], &vertexData);
- const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations[n];
- switch (textEffect_)
- {
- case TE_NONE:
- ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
- break;
-
- case TE_SHADOW:
- ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
- break;
-
- case TE_STROKE:
- ConstructBatch(pageBatch, pageGlyphLocation, -1, -1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 0, -1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 1, -1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, -1, 0, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 1, 0, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, -1, 1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 0, 1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 1, 1, &effectColor_, effectDepthBias_);
- ConstructBatch(pageBatch, pageGlyphLocation, 0, 0);
- break;
- }
- batches.Push(pageBatch);
- }
- }
- else
- {
- // If only one texture page, construct the UI batch directly
- int x = GetRowStartPosition(0);
- int y = 0;
- UIBatch batch(this, BLEND_ALPHA, currentScissor, face->textures_[0], &vertexData);
- switch (textEffect_)
- {
- case TE_NONE:
- ConstructBatch(batch, face, x, y);
- break;
-
- case TE_SHADOW:
- ConstructBatch(batch, face, x + 1, y + 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x, y);
- break;
-
- case TE_STROKE:
- ConstructBatch(batch, face, x - 1, y - 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x, y - 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x + 1, y - 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x - 1, y, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x + 1, y, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x - 1, y + 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x, y + 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x + 1, y + 1, &effectColor_, effectDepthBias_);
- ConstructBatch(batch, face, x, y);
- break;
- }
- UIBatch::AddOrMerge(batch, batches);
- }
- }
- // Reset hovering for next frame
- hovering_ = false;
- }
- void Text::OnResize()
- {
- if (wordWrap_)
- UpdateText();
- }
- bool Text::SetFont(const String& fontName, int size)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- return SetFont(cache->GetResource<Font>(fontName), size);
- }
- bool Text::SetFont(Font* font, int size)
- {
- if (!font)
- {
- LOGERROR("Null font for Text");
- return false;
- }
- if (font != font_ || size != fontSize_)
- {
- font_ = font;
- fontSize_ = Max(size, 1);
- UpdateText();
- }
- return true;
- }
- void Text::SetText(const String& text)
- {
- text_ = text;
- // Decode to Unicode now
- unicodeText_.Clear();
- for (unsigned i = 0; i < text_.Length();)
- unicodeText_.Push(text_.NextUTF8Char(i));
- ValidateSelection();
- UpdateText();
- }
- void Text::SetTextAlignment(HorizontalAlignment align)
- {
- if (align != textAlignment_)
- {
- textAlignment_ = align;
- UpdateText();
- }
- }
- void Text::SetRowSpacing(float spacing)
- {
- if (spacing != rowSpacing_)
- {
- rowSpacing_ = Max(spacing, MIN_ROW_SPACING);
- UpdateText();
- }
- }
- void Text::SetWordwrap(bool enable)
- {
- if (enable != wordWrap_)
- {
- wordWrap_ = enable;
- UpdateText();
- }
- }
- void Text::SetSelection(unsigned start, unsigned length)
- {
- selectionStart_ = start;
- selectionLength_ = length;
- ValidateSelection();
- }
- void Text::ClearSelection()
- {
- selectionStart_ = 0;
- selectionLength_ = 0;
- }
- void Text::SetSelectionColor(const Color& color)
- {
- selectionColor_ = color;
- }
- void Text::SetHoverColor(const Color& color)
- {
- hoverColor_ = color;
- }
- void Text::SetTextEffect(TextEffect textEffect)
- {
- textEffect_ = textEffect;
- }
- void Text::SetEffectColor(const Color& effectColor)
- {
- effectColor_ = effectColor;
- }
- void Text::SetEffectDepthBias(float bias)
- {
- effectDepthBias_ = bias;
- }
- void Text::SetFontAttr(ResourceRef value)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- font_ = cache->GetResource<Font>(value.id_);
- }
- ResourceRef Text::GetFontAttr() const
- {
- return GetResourceRef(font_, Font::GetTypeStatic());
- }
- bool Text::FilterImplicitAttributes(XMLElement& dest) const
- {
- if (!UIElement::FilterImplicitAttributes(dest))
- return false;
- if (!IsFixedWidth())
- {
- if (!RemoveChildXML(dest, "Size"))
- return false;
- if (!RemoveChildXML(dest, "Min Size"))
- return false;
- if (!RemoveChildXML(dest, "Max Size"))
- return false;
- }
- return true;
- }
- void Text::UpdateText()
- {
- int width = 0;
- int height = 0;
- rowWidths_.Clear();
- printText_.Clear();
- PODVector<unsigned> printToText;
- if (font_)
- {
- const FontFace* face = font_->GetFace(fontSize_);
- if (!face)
- return;
- rowHeight_ = face->rowHeight_;
- int rowWidth = 0;
- int rowHeight = (int)(rowSpacing_ * rowHeight_);
- // First see if the text must be split up
- if (!wordWrap_)
- {
- printText_ = unicodeText_;
- printToText.Resize(printText_.Size());
- for (unsigned i = 0; i < printText_.Size(); ++i)
- printToText[i] = i;
- }
- else
- {
- int maxWidth = GetWidth();
- unsigned nextBreak = 0;
- unsigned lineStart = 0;
- for (unsigned i = 0; i < unicodeText_.Size(); ++i)
- {
- unsigned j;
- unsigned c = unicodeText_[i];
- if (c != '\n')
- {
- bool ok = true;
- if (nextBreak <= i)
- {
- int futureRowWidth = rowWidth;
- for (j = i; j < unicodeText_.Size(); ++j)
- {
- unsigned d = unicodeText_[j];
- if (d == ' ' || d == '\n')
- {
- nextBreak = j;
- break;
- }
- const FontGlyph* glyph = face->GetGlyph(d);
- if (glyph)
- {
- futureRowWidth += glyph->advanceX_;
- if (j < unicodeText_.Size() - 1)
- futureRowWidth += face->GetKerning(d, unicodeText_[j + 1]);
- }
- if (d == '-' && futureRowWidth <= maxWidth)
- {
- nextBreak = j + 1;
- break;
- }
- if (futureRowWidth > maxWidth)
- {
- ok = false;
- break;
- }
- }
- }
- if (!ok)
- {
- // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
- if (nextBreak == lineStart)
- {
- while (i < j)
- {
- printText_.Push(unicodeText_[i]);
- printToText.Push(i);
- ++i;
- }
- }
- printText_.Push('\n');
- printToText.Push(Min((int)i, (int)unicodeText_.Size() - 1));
- rowWidth = 0;
- nextBreak = lineStart = i;
- }
- if (i < unicodeText_.Size())
- {
- // When copying a space, position is allowed to be over row width
- c = unicodeText_[i];
- const FontGlyph* glyph = face->GetGlyph(c);
- if (glyph)
- {
- rowWidth += glyph->advanceX_;
- if (i < text_.Length() - 1)
- rowWidth += face->GetKerning(c, unicodeText_[i + 1]);
- }
- if (rowWidth <= maxWidth)
- {
- printText_.Push(c);
- printToText.Push(i);
- }
- }
- }
- else
- {
- printText_.Push('\n');
- printToText.Push(Min((int)i, (int)unicodeText_.Size() - 1));
- rowWidth = 0;
- nextBreak = lineStart = i;
- }
- }
- }
- rowWidth = 0;
- for (unsigned i = 0; i < printText_.Size(); ++i)
- {
- unsigned c = printText_[i];
- if (c != '\n')
- {
- const FontGlyph* glyph = face->GetGlyph(c);
- if (glyph)
- {
- rowWidth += glyph->advanceX_;
- if (i < printText_.Size() - 1)
- rowWidth += face->GetKerning(c, printText_[i + 1]);
- }
- }
- else
- {
- width = Max(width, rowWidth);
- height += rowHeight;
- rowWidths_.Push(rowWidth);
- rowWidth = 0;
- }
- }
- if (rowWidth)
- {
- width = Max(width, rowWidth);
- height += rowHeight;
- rowWidths_.Push(rowWidth);
- }
- // Set row height even if text is empty
- if (!height)
- height = rowHeight;
- // Store position & size of each character
- charPositions_.Resize(unicodeText_.Size() + 1);
- charSizes_.Resize(unicodeText_.Size());
- unsigned rowIndex = 0;
- int x = GetRowStartPosition(rowIndex);
- int y = 0;
- for (unsigned i = 0; i < printText_.Size(); ++i)
- {
- charPositions_[printToText[i]] = IntVector2(x, y);
- unsigned c = printText_[i];
- if (c != '\n')
- {
- const FontGlyph* glyph = face->GetGlyph(c);
- charSizes_[printToText[i]] = IntVector2(glyph ? glyph->advanceX_ : 0, rowHeight_);
- if (glyph)
- {
- x += glyph->advanceX_;
- if (i < printText_.Size() - 1)
- x += face->GetKerning(c, printText_[i + 1]);
- }
- }
- else
- {
- charSizes_[printToText[i]] = IntVector2::ZERO;
- x = GetRowStartPosition(++rowIndex);
- y += rowHeight;
- }
- }
- // Store the ending position
- charPositions_[unicodeText_.Size()] = IntVector2(x, y);
- }
- // Set minimum and current size according to the text size, but respect fixed width if set
- if (!IsFixedWidth())
- {
- SetMinWidth(wordWrap_ ? 0 : width);
- SetWidth(width);
- }
- SetFixedHeight(height);
- }
- void Text::ValidateSelection()
- {
- unsigned textLength = unicodeText_.Size();
- if (textLength)
- {
- if (selectionStart_ >= textLength)
- selectionStart_ = textLength - 1;
- if (selectionStart_ + selectionLength_ > textLength)
- selectionLength_ = textLength - selectionStart_;
- }
- else
- {
- selectionStart_ = 0;
- selectionLength_ = 0;
- }
- }
- int Text::GetRowStartPosition(unsigned rowIndex) const
- {
- int rowWidth = 0;
- if (rowIndex < rowWidths_.Size())
- rowWidth = rowWidths_[rowIndex];
- int ret = GetIndentWidth();
- switch (textAlignment_)
- {
- case HA_LEFT:
- break;
- case HA_CENTER:
- ret += (GetSize().x_ - rowWidth) / 2;
- break;
- case HA_RIGHT:
- ret += GetSize().x_ - rowWidth;
- break;
- }
- return ret;
- }
- void Text::ConstructBatch(UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int x, int y, Color* color,
- float depthBias)
- {
- unsigned startDataSize = pageBatch.vertexData_->Size();
-
- for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
- {
- const GlyphLocation& glyphLocation = pageGlyphLocation[i];
- const FontGlyph& glyph = *glyphLocation.glyph_;
- if (!color)
- {
- pageBatch.AddQuad(x + glyphLocation.x_ + glyph.offsetX_, y + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
- glyph.height_, glyph.x_, glyph.y_);
- }
- else
- {
- pageBatch.AddQuad(x + glyphLocation.x_ + glyph.offsetX_, y + glyphLocation.y_ + glyph.offsetY_, glyph.width_,
- glyph.height_, glyph.x_, glyph.y_, 0, 0, color);
- }
- }
- if (depthBias != 0.0f)
- {
- unsigned dataSize = pageBatch.vertexData_->Size();
- for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
- pageBatch.vertexData_->At(i + 2) += depthBias;
- }
- }
- void Text::ConstructBatch(UIBatch& batch, const FontFace* face, int x, int y, Color* color, float depthBias)
- {
- unsigned rowIndex = 0;
- unsigned startDataSize = batch.vertexData_->Size();
-
- for (unsigned i = 0; i < printText_.Size(); ++i)
- {
- unsigned c = printText_[i];
- if (c != '\n')
- {
- const FontGlyph* p = face->GetGlyph(c);
- if (!p)
- continue;
- if (!color)
- batch.AddQuad(x + p->offsetX_, y + p->offsetY_, p->width_, p->height_, p->x_, p->y_);
- else
- batch.AddQuad(x + p->offsetX_, y + p->offsetY_, p->width_, p->height_, p->x_, p->y_, 0, 0, color);
- x += p->advanceX_;
- if (i < printText_.Size() - 1)
- x += face->GetKerning(c, printText_[i + 1]);
- }
- else
- {
- x = GetRowStartPosition(++rowIndex);
- y += rowHeight_;
- }
- }
-
- if (depthBias != 0.0f)
- {
- unsigned dataSize = batch.vertexData_->Size();
- for (unsigned i = startDataSize; i < dataSize; i += UI_VERTEX_SIZE)
- batch.vertexData_->At(i + 2) += depthBias;
- }
- }
- }
|