2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/text/text-visual.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/images/pixel-data-devel.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
29 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
30 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
31 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
32 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
33 #include <dali-toolkit/internal/text/script-run.h>
34 #include <dali-toolkit/internal/text/text-effects-style.h>
35 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
36 #include <dali-toolkit/internal/text/text-font-style.h>
37 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
40 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
41 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
42 #include <dali-toolkit/public-api/visuals/visual-properties.h>
52 const int CUSTOM_PROPERTY_COUNT(5); // anim,premul,size,offset,multicol
54 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
57 * Return Property index for the given string key
58 * param[in] stringKey the string index key
59 * return the key as an index
62 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
64 Dali::Property::Index result = Property::INVALID_KEY;
66 if(stringKey == VISUAL_TYPE)
68 result = Toolkit::Visual::Property::TYPE;
70 else if(stringKey == TEXT_PROPERTY)
72 result = Toolkit::TextVisual::Property::TEXT;
74 else if(stringKey == FONT_FAMILY_PROPERTY)
76 result = Toolkit::TextVisual::Property::FONT_FAMILY;
78 else if(stringKey == FONT_STYLE_PROPERTY)
80 result = Toolkit::TextVisual::Property::FONT_STYLE;
82 else if(stringKey == POINT_SIZE_PROPERTY)
84 result = Toolkit::TextVisual::Property::POINT_SIZE;
86 else if(stringKey == MULTI_LINE_PROPERTY)
88 result = Toolkit::TextVisual::Property::MULTI_LINE;
90 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
92 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
94 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
96 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
98 else if(stringKey == TEXT_COLOR_PROPERTY)
100 result = Toolkit::TextVisual::Property::TEXT_COLOR;
102 else if(stringKey == ENABLE_MARKUP_PROPERTY)
104 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
106 else if(stringKey == SHADOW_PROPERTY)
108 result = Toolkit::TextVisual::Property::SHADOW;
110 else if(stringKey == UNDERLINE_PROPERTY)
112 result = Toolkit::TextVisual::Property::UNDERLINE;
114 else if(stringKey == OUTLINE_PROPERTY)
116 result = Toolkit::DevelTextVisual::Property::OUTLINE;
118 else if(stringKey == BACKGROUND_PROPERTY)
120 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
126 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
128 Vector4 color = inputs[0]->GetVector4();
129 current.r = color.r * color.a;
130 current.g = color.g * color.a;
131 current.b = color.b * color.a;
135 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
137 // Make zero if the alpha value of text color is zero to skip rendering text
138 if(EqualsZero(inputs[0]->GetVector4().a))
148 } // unnamed namespace
150 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
152 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
153 textVisualPtr->SetProperties(properties);
154 textVisualPtr->Initialize();
155 return textVisualPtr;
158 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
160 Property::Map outMap;
162 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
164 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
166 Property::Index indexKey = keyValue.first.indexKey;
168 if(keyValue.first.type == Property::Key::STRING)
170 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
173 outMap.Insert(indexKey, keyValue.second);
179 float TextVisual::GetHeightForWidth(float width)
181 return mController->GetHeightForWidth(width);
184 void TextVisual::GetNaturalSize(Vector2& naturalSize)
186 naturalSize = mController->GetNaturalSize().GetVectorXY();
189 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
191 Property::Value value;
194 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
197 mController->GetText(text);
198 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
200 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
202 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
203 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
205 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
207 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
209 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
211 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
213 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
215 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
217 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
218 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
220 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
221 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
223 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
224 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
226 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
227 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
229 GetStrikethroughProperties(mController, value, Text::EffectStyle::DEFAULT);
230 map.Insert(Toolkit::DevelTextVisual::Property::STRIKETHROUGH, value);
233 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
236 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
238 mController->GetText(text);
239 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
242 TextVisual::TextVisual(VisualFactoryCache& factoryCache)
243 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
244 mController(Text::Controller::New()),
245 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
246 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
247 mTextColorAnimatableIndex(Property::INVALID_INDEX),
248 mRendererUpdateNeeded(false)
252 TextVisual::~TextVisual()
256 void TextVisual::OnInitialize()
258 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
259 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
261 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
262 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
265 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
267 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
269 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
271 Property::Index indexKey = keyValue.first.indexKey;
273 if(keyValue.first.type == Property::Key::STRING)
275 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
278 DoSetProperty(indexKey, keyValue.second);
281 // Elide the text if it exceeds the boundaries.
282 mController->SetTextElideEnabled(true);
284 // Retrieve the layout engine to set the cursor's width.
285 Text::Layout::Engine& engine = mController->GetLayoutEngine();
287 // Sets 0 as cursor's width.
288 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
291 void TextVisual::DoSetOnScene(Actor& actor)
295 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
297 // Enable the pre-multiplied alpha to improve the text quality
298 EnablePreMultipliedAlpha(true);
300 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
301 if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
303 mTextColorAnimatableIndex = mImpl->mRenderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
307 mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
310 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
312 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
313 if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
315 if(!mColorConstraint)
317 mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
318 mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
320 mColorConstraint.Apply();
323 // Make zero if the alpha value of text color is zero to skip rendering text
324 if(!mOpacityConstraint)
326 // VisualRenderer::Property::OPACITY uses same animatable property internally.
327 mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
328 mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
330 mOpacityConstraint.Apply();
333 // Renderer needs textures and to be added to control
334 mRendererUpdateNeeded = true;
336 mRendererList.push_back(mImpl->mRenderer);
341 void TextVisual::RemoveRenderer(Actor& actor)
343 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
345 Renderer renderer = (*iter);
348 // Removes the renderer from the actor.
349 actor.RemoveRenderer(renderer);
352 // Clear the renderer list
353 mRendererList.clear();
356 void TextVisual::DoSetOffScene(Actor& actor)
360 mColorConstraint.Remove();
362 if(mOpacityConstraint)
364 mOpacityConstraint.Remove();
367 RemoveRenderer(actor);
369 // Resets the control handle.
373 void TextVisual::OnSetTransform()
378 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
382 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
384 const bool enableMarkup = propertyValue.Get<bool>();
385 mController->SetMarkupProcessorEnabled(enableMarkup);
388 case Toolkit::TextVisual::Property::TEXT:
390 mController->SetText(propertyValue.Get<std::string>());
393 case Toolkit::TextVisual::Property::FONT_FAMILY:
395 SetFontFamilyProperty(mController, propertyValue);
398 case Toolkit::TextVisual::Property::FONT_STYLE:
400 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
403 case Toolkit::TextVisual::Property::POINT_SIZE:
405 const float pointSize = propertyValue.Get<float>();
406 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
408 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
412 case Toolkit::TextVisual::Property::MULTI_LINE:
414 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
417 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
421 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
422 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
424 mController->SetHorizontalAlignment(alignment);
429 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
433 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
434 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
436 mController->SetVerticalAlignment(alignment);
441 case Toolkit::TextVisual::Property::TEXT_COLOR:
443 const Vector4& textColor = propertyValue.Get<Vector4>();
444 if(mController->GetDefaultColor() != textColor)
446 mController->SetDefaultColor(textColor);
450 case Toolkit::TextVisual::Property::SHADOW:
452 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
455 case Toolkit::TextVisual::Property::UNDERLINE:
457 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
460 case Toolkit::DevelTextVisual::Property::OUTLINE:
462 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
465 case Toolkit::DevelTextVisual::Property::BACKGROUND:
467 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
470 case Toolkit::DevelTextVisual::Property::STRIKETHROUGH:
472 SetStrikethroughProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
478 void TextVisual::UpdateRenderer()
480 Actor control = mControl.GetHandle();
487 // Calculates the size to be used to relayout.
488 Vector2 relayoutSize;
490 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
491 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
493 // Round the size and offset to avoid pixel alignement issues.
494 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
495 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
498 mController->GetText(text);
500 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
502 // Remove the texture set and any renderer previously set.
503 RemoveRenderer(control);
505 // Nothing else to do if the relayout size is zero.
506 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
510 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
512 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
514 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
516 mRendererUpdateNeeded = false;
518 // Remove the texture set and any renderer previously set.
519 RemoveRenderer(control);
521 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
522 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
524 // Check whether it is a markup text with multiple text colors
525 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
526 bool hasMultipleTextColors = (NULL != colorsBuffer);
528 // Check whether the text contains any color glyph
529 bool containsColorGlyph = false;
531 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
532 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
533 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
534 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
536 // Retrieve the glyph's info.
537 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
539 // Whether the current glyph is a color one.
540 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
542 containsColorGlyph = true;
547 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
549 bool shadowEnabled = false;
550 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
551 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
553 shadowEnabled = true;
556 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
557 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
558 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
559 const bool markupProcessorEnabled = mController->IsMarkupProcessorEnabled();
560 const bool strikethroughEnabled = mController->GetTextModel()->IsStrikethroughEnabled();
562 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled || markupProcessorEnabled || strikethroughEnabled);
563 const bool isOverlayStyle = underlineEnabled || strikethroughEnabled;
565 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
567 // Text rendered and ready to display
568 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
573 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
575 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
576 data.GetPixelFormat(),
579 texture.Upload(data);
581 textureSet.SetTexture(textureSetIndex, texture);
582 textureSet.SetSampler(textureSetIndex, sampler);
585 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
587 int bpp = Pixel::GetBytesPerPixel(textPixelFormat);
588 unsigned int bufferSize = width * height * bpp;
589 unsigned char* dstBuffer = static_cast<unsigned char*>(malloc(bufferSize));
590 memcpy(dstBuffer, buffer + offsetPosition * bpp, bufferSize);
591 PixelData pixelData = Dali::PixelData::New(dstBuffer,
596 Dali::PixelData::FREE);
600 void TextVisual::CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
602 TextureSet textureSet = TextureSet::New();
603 unsigned int textureSetIndex = 0u;
605 // Convert the buffer to pixel data to make it a texture.
609 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
610 AddTexture(textureSet, data, sampler, textureSetIndex);
614 if(styleEnabled && info.styleBuffer)
616 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
617 AddTexture(textureSet, styleData, sampler, textureSetIndex);
621 if(styleEnabled && isOverlayStyle && info.styleBuffer)
623 PixelData overlayStyleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
624 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
628 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
630 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
631 AddTexture(textureSet, maskData, sampler, textureSetIndex);
634 renderer.SetTextures(textureSet);
636 //Register transform properties
637 mImpl->mTransform.SetUniforms(renderer, Direction::LEFT_TO_RIGHT);
639 // Enable the pre-multiplied alpha to improve the text quality
640 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
641 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
643 // Set size and offset for the tiling.
644 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_SIZE, Vector2(info.width, info.height));
645 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_OFFSET, Vector2(info.offSet.x, info.offSet.y));
646 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
647 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
649 mRendererList.push_back(renderer);
652 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
654 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
655 mImpl->mRenderer.SetShader(shader);
657 // Get the maximum size.
658 const int maxTextureSize = Dali::GetMaxTextureSize();
660 // No tiling required. Use the default renderer.
661 if(size.height < maxTextureSize)
663 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
665 mImpl->mRenderer.SetTextures(textureSet);
666 //Register transform properties
667 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
668 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
669 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
671 mRendererList.push_back(mImpl->mRenderer);
673 // If the pixel data exceeds the maximum size, tiling is required.
676 // Filter mode needs to be set to linear to produce better quality while scaling.
677 Sampler sampler = Sampler::New();
678 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
680 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
681 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
683 // Check the text direction
684 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
686 // Create a texture for the text without any styles
687 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
689 int verifiedWidth = data.GetWidth();
690 int verifiedHeight = data.GetHeight();
692 // Set information for creating textures.
693 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
695 // Get the buffer of text.
696 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
697 info.textBuffer = textPixelData.buffer;
701 // Create RGBA texture for all the text styles (without the text itself)
702 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
703 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(styleData);
704 info.styleBuffer = stylePixelData.buffer;
707 if(containsColorGlyph && !hasMultipleTextColors)
709 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
710 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
711 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(maskData);
712 info.maskBuffer = maskPixelData.buffer;
715 // Get the current offset for recalculate the offset when tiling.
716 Property::Map retMap;
717 mImpl->mTransform.GetPropertyMap(retMap);
718 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
721 offsetValue->Get(info.offSet);
724 // Create a textureset in the default renderer.
725 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
727 verifiedHeight -= maxTextureSize;
729 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
731 int offsetPosition = verifiedWidth * maxTextureSize;
732 // Create a renderer by cutting maxTextureSize.
733 while(verifiedHeight > 0)
735 VisualRenderer tilingRenderer = VisualRenderer::New(geometry, shader);
736 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
737 // New offset position of buffer for tiling.
738 info.offsetPosition += offsetPosition;
739 // New height for tiling.
740 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
741 // New offset for tiling.
742 info.offSet.y += maxTextureSize;
743 // Create a textureset int the new tiling renderer.
744 CreateTextureSet(info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
746 verifiedHeight -= maxTextureSize;
750 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
752 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
754 Renderer renderer = (*iter);
757 actor.AddRenderer(renderer);
762 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
764 // Filter mode needs to be set to linear to produce better quality while scaling.
765 Sampler sampler = Sampler::New();
766 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
768 TextureSet textureSet = TextureSet::New();
770 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
771 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
773 // Check the text direction
774 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
776 // Create a texture for the text without any styles
777 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
779 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
780 // In that case, create a texture. TODO: should tile the text.
781 unsigned int textureSetIndex = 0u;
782 AddTexture(textureSet, data, sampler, textureSetIndex);
787 // Create RGBA texture for all the text styles that render in the background (without the text itself)
788 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
789 AddTexture(textureSet, styleData, sampler, textureSetIndex);
791 // Create RGBA texture for overlay styles such as underline and strikethrough (without the text itself)
792 PixelData overlayStyleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
793 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
797 if(containsColorGlyph && !hasMultipleTextColors)
799 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
800 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
802 AddTexture(textureSet, maskData, sampler, textureSetIndex);
808 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
812 if(hasMultipleTextColors && !styleEnabled)
814 // We don't animate text color if the text contains multiple colors
815 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
818 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG);
819 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
820 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader);
823 else if(hasMultipleTextColors && styleEnabled)
825 // We don't animate text color if the text contains multiple colors
826 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE);
829 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
830 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
831 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader);
834 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
836 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
839 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG);
840 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
841 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader);
844 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
846 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
849 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
850 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
851 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader);
854 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
856 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
859 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG);
860 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
861 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader);
864 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
866 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
869 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG);
870 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
871 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader);
878 } // namespace Internal
880 } // namespace Toolkit