2 * Copyright (c) 2023 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/rendering/renderer-devel.h>
24 #include <dali/devel-api/rendering/texture-devel.h>
25 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/trace.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
33 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
34 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
35 #include <dali-toolkit/internal/text/script-run.h>
36 #include <dali-toolkit/internal/text/text-effects-style.h>
37 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
38 #include <dali-toolkit/internal/text/text-font-style.h>
39 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
40 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
42 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
43 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
44 #include <dali-toolkit/public-api/visuals/visual-properties.h>
54 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_TEXT_PERFORMANCE_MARKER, false);
56 const int CUSTOM_PROPERTY_COUNT(5); // anim,premul,size,offset,multicol
58 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
61 * Return Property index for the given string key
62 * param[in] stringKey the string index key
63 * return the key as an index
66 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
68 Dali::Property::Index result = Property::INVALID_KEY;
70 if(stringKey == VISUAL_TYPE)
72 result = Toolkit::Visual::Property::TYPE;
74 else if(stringKey == TEXT_PROPERTY)
76 result = Toolkit::TextVisual::Property::TEXT;
78 else if(stringKey == FONT_FAMILY_PROPERTY)
80 result = Toolkit::TextVisual::Property::FONT_FAMILY;
82 else if(stringKey == FONT_STYLE_PROPERTY)
84 result = Toolkit::TextVisual::Property::FONT_STYLE;
86 else if(stringKey == POINT_SIZE_PROPERTY)
88 result = Toolkit::TextVisual::Property::POINT_SIZE;
90 else if(stringKey == MULTI_LINE_PROPERTY)
92 result = Toolkit::TextVisual::Property::MULTI_LINE;
94 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
96 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
98 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
100 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
102 else if(stringKey == TEXT_COLOR_PROPERTY)
104 result = Toolkit::TextVisual::Property::TEXT_COLOR;
106 else if(stringKey == ENABLE_MARKUP_PROPERTY)
108 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
110 else if(stringKey == SHADOW_PROPERTY)
112 result = Toolkit::TextVisual::Property::SHADOW;
114 else if(stringKey == UNDERLINE_PROPERTY)
116 result = Toolkit::TextVisual::Property::UNDERLINE;
118 else if(stringKey == OUTLINE_PROPERTY)
120 result = Toolkit::DevelTextVisual::Property::OUTLINE;
122 else if(stringKey == BACKGROUND_PROPERTY)
124 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
130 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
132 Vector4 color = inputs[0]->GetVector4();
133 current.r = color.r * color.a;
134 current.g = color.g * color.a;
135 current.b = color.b * color.a;
139 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
141 // Make zero if the alpha value of text color is zero to skip rendering text
142 if(EqualsZero(inputs[0]->GetVector4().a))
152 } // unnamed namespace
154 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory, const Property::Map& properties)
156 TextVisualPtr textVisualPtr(new TextVisual(factoryCache, shaderFactory));
157 textVisualPtr->SetProperties(properties);
158 textVisualPtr->Initialize();
159 return textVisualPtr;
162 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
164 Property::Map outMap;
166 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
168 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
170 Property::Index indexKey = keyValue.first.indexKey;
172 if(keyValue.first.type == Property::Key::STRING)
174 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
177 outMap.Insert(indexKey, keyValue.second);
183 float TextVisual::GetHeightForWidth(float width)
185 return mController->GetHeightForWidth(width);
188 void TextVisual::GetNaturalSize(Vector2& naturalSize)
190 naturalSize = mController->GetNaturalSize().GetVectorXY();
193 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
195 Property::Value value;
198 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
201 mController->GetText(text);
202 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
204 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
206 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
207 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
209 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
211 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
213 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
215 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
217 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
219 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
221 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
222 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
224 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
225 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
227 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
228 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
230 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
231 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
233 GetStrikethroughProperties(mController, value, Text::EffectStyle::DEFAULT);
234 map.Insert(Toolkit::DevelTextVisual::Property::STRIKETHROUGH, value);
237 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
240 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
242 mController->GetText(text);
243 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
246 void TextVisual::EnablePreMultipliedAlpha(bool preMultiplied)
248 // Make always enable pre multiplied alpha whether preMultiplied value is false.
251 DALI_LOG_WARNING("Note : TextVisual cannot disable PreMultipliedAlpha\n");
255 TextVisual::TextVisual(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory)
256 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
257 mController(Text::Controller::New()),
258 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
259 mTextVisualShaderFactory(shaderFactory),
260 mTextShaderFeatureCache(),
261 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
262 mTextColorAnimatableIndex(Property::INVALID_INDEX),
263 mRendererUpdateNeeded(false)
265 // Enable the pre-multiplied alpha to improve the text quality
266 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
269 TextVisual::~TextVisual()
273 void TextVisual::OnInitialize()
275 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
276 Shader shader = GetTextShader(mFactoryCache, TextVisualShaderFeature::FeatureBuilder());
278 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
279 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
282 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
284 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
286 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
288 Property::Index indexKey = keyValue.first.indexKey;
290 if(keyValue.first.type == Property::Key::STRING)
292 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
295 DoSetProperty(indexKey, keyValue.second);
298 // Elide the text if it exceeds the boundaries.
299 mController->SetTextElideEnabled(true);
301 // Retrieve the layout engine to set the cursor's width.
302 Text::Layout::Engine& engine = mController->GetLayoutEngine();
304 // Sets 0 as cursor's width.
305 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
308 void TextVisual::DoSetOnScene(Actor& actor)
312 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
314 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
315 if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
317 mTextColorAnimatableIndex = mImpl->mRenderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
321 mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
324 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
326 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
327 if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
329 if(!mColorConstraint)
331 mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
332 mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
334 mColorConstraint.Apply();
337 // Make zero if the alpha value of text color is zero to skip rendering text
338 if(!mOpacityConstraint)
340 // VisualRenderer::Property::OPACITY uses same animatable property internally.
341 mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
342 mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
344 mOpacityConstraint.Apply();
347 // Renderer needs textures and to be added to control
348 mRendererUpdateNeeded = true;
353 void TextVisual::RemoveRenderer(Actor& actor)
355 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
357 Renderer renderer = (*iter);
360 // Removes the renderer from the actor.
361 actor.RemoveRenderer(renderer);
364 // Clear the renderer list
365 mRendererList.clear();
368 void TextVisual::DoSetOffScene(Actor& actor)
372 mColorConstraint.Remove();
374 if(mOpacityConstraint)
376 mOpacityConstraint.Remove();
379 RemoveRenderer(actor);
381 // Resets the control handle.
385 void TextVisual::OnSetTransform()
390 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
394 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
396 const bool enableMarkup = propertyValue.Get<bool>();
397 mController->SetMarkupProcessorEnabled(enableMarkup);
400 case Toolkit::TextVisual::Property::TEXT:
402 mController->SetText(propertyValue.Get<std::string>());
405 case Toolkit::TextVisual::Property::FONT_FAMILY:
407 SetFontFamilyProperty(mController, propertyValue);
410 case Toolkit::TextVisual::Property::FONT_STYLE:
412 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
415 case Toolkit::TextVisual::Property::POINT_SIZE:
417 const float pointSize = propertyValue.Get<float>();
418 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
420 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
424 case Toolkit::TextVisual::Property::MULTI_LINE:
426 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
429 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
433 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
434 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
436 mController->SetHorizontalAlignment(alignment);
441 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
445 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
446 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
448 mController->SetVerticalAlignment(alignment);
453 case Toolkit::TextVisual::Property::TEXT_COLOR:
455 const Vector4& textColor = propertyValue.Get<Vector4>();
456 if(mController->GetDefaultColor() != textColor)
458 mController->SetDefaultColor(textColor);
462 case Toolkit::TextVisual::Property::SHADOW:
464 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
467 case Toolkit::TextVisual::Property::UNDERLINE:
469 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
472 case Toolkit::DevelTextVisual::Property::OUTLINE:
474 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
477 case Toolkit::DevelTextVisual::Property::BACKGROUND:
479 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
482 case Toolkit::DevelTextVisual::Property::STRIKETHROUGH:
484 SetStrikethroughProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
490 void TextVisual::UpdateRenderer()
492 Actor control = mControl.GetHandle();
499 // Calculates the size to be used to relayout.
500 Vector2 relayoutSize;
502 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
503 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
505 // Round the size and offset to avoid pixel alignement issues.
506 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
507 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
509 auto textLengthUtf32 = mController->GetNumberOfCharacters();
511 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || textLengthUtf32 == 0u)
513 // Remove the texture set and any renderer previously set.
514 RemoveRenderer(control);
516 // Nothing else to do if the relayout size is zero.
517 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
521 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
523 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
525 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
527 mRendererUpdateNeeded = false;
529 // Remove the texture set and any renderer previously set.
530 RemoveRenderer(control);
532 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
533 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
535 // Check whether it is a markup text with multiple text colors
536 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
537 bool hasMultipleTextColors = (NULL != colorsBuffer);
539 // Check whether the text contains any color glyph
540 bool containsColorGlyph = false;
542 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
543 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
544 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
545 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
547 // Retrieve the glyph's info.
548 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
550 // Whether the current glyph is a color one.
551 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
553 containsColorGlyph = true;
558 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
560 bool shadowEnabled = false;
561 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
562 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
564 shadowEnabled = true;
567 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
568 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
569 const bool markupOrSpannedText = mController->IsMarkupProcessorEnabled() || mController->GetTextModel()->IsSpannedTextPlaced();
570 const bool markupUnderlineEnabled = markupOrSpannedText && mController->GetTextModel()->IsMarkupUnderlineSet();
571 const bool markupStrikethroughEnabled = markupOrSpannedText && mController->GetTextModel()->IsMarkupStrikethroughSet();
572 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled() || markupUnderlineEnabled;
573 const bool strikethroughEnabled = mController->GetTextModel()->IsStrikethroughEnabled() || markupStrikethroughEnabled;
574 const bool backgroundMarkupSet = mController->GetTextModel()->IsMarkupBackgroundColorSet();
575 const bool styleEnabled = (shadowEnabled || outlineEnabled || backgroundEnabled || markupOrSpannedText || backgroundMarkupSet);
576 const bool isOverlayStyle = underlineEnabled || strikethroughEnabled;
578 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
580 // Text rendered and ready to display
581 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
586 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
588 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
589 data.GetPixelFormat(),
592 texture.Upload(data);
594 textureSet.SetTexture(textureSetIndex, texture);
595 textureSet.SetSampler(textureSetIndex, sampler);
598 void TextVisual::AddTilingTexture(TextureSet& textureSet, TilingInfo& tilingInfo, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
600 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
601 tilingInfo.textPixelFormat,
604 DevelTexture::UploadSubPixelData(texture, data, 0u, tilingInfo.offsetHeight, tilingInfo.width, tilingInfo.height);
606 textureSet.SetTexture(textureSetIndex, texture);
607 textureSet.SetSampler(textureSetIndex, sampler);
610 void TextVisual::CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler)
612 TextureSet textureSet = TextureSet::New();
613 uint32_t textureSetIndex = 0u;
615 // Convert the buffer to pixel data to make it a texture.
617 if(info.textPixelData)
619 AddTilingTexture(textureSet, info, info.textPixelData, sampler, textureSetIndex);
623 if(mTextShaderFeatureCache.IsEnabledStyle() && info.stylePixelData)
625 AddTilingTexture(textureSet, info, info.stylePixelData, sampler, textureSetIndex);
629 if(mTextShaderFeatureCache.IsEnabledOverlay() && info.overlayStylePixelData)
631 AddTilingTexture(textureSet, info, info.overlayStylePixelData, sampler, textureSetIndex);
635 if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor() && info.maskPixelData)
637 AddTilingTexture(textureSet, info, info.maskPixelData, sampler, textureSetIndex);
641 renderer.SetTextures(textureSet);
643 //Register transform properties
644 mImpl->mTransform.SetUniforms(renderer, Direction::LEFT_TO_RIGHT);
646 // Enable the pre-multiplied alpha to improve the text quality
647 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
648 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
650 // Set size and offset for the tiling.
651 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_SIZE, Vector2(info.width, info.height));
652 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_OFFSET, info.transformOffset);
653 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
654 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(mTextShaderFeatureCache.IsEnabledMultiColor()));
656 mRendererList.push_back(renderer);
659 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
661 Shader shader = GetTextShader(mFactoryCache, TextVisualShaderFeature::FeatureBuilder().EnableMultiColor(hasMultipleTextColors).EnableEmoji(containsColorGlyph).EnableStyle(styleEnabled).EnableOverlay(isOverlayStyle));
662 mImpl->mRenderer.SetShader(shader);
664 DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_VISUAL_UPDATE_RENDERER");
666 // Get the maximum size.
667 const int maxTextureSize = Dali::GetMaxTextureSize();
669 // No tiling required. Use the default renderer.
670 if(size.height < maxTextureSize)
672 TextureSet textureSet = GetTextTexture(size);
674 mImpl->mRenderer.SetTextures(textureSet);
675 //Register transform properties
676 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
677 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
678 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
680 mRendererList.push_back(mImpl->mRenderer);
682 // If the pixel data exceeds the maximum size, tiling is required.
685 // Filter mode needs to be set to linear to produce better quality while scaling.
686 Sampler sampler = Sampler::New();
687 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
689 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
690 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
692 // Check the text direction
693 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
695 // Create a texture for the text without any styles
696 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
698 int verifiedWidth = data.GetWidth();
699 int verifiedHeight = data.GetHeight();
701 // Set information for creating textures.
702 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
704 // Get the pixel data of text.
705 info.textPixelData = data;
707 if(mTextShaderFeatureCache.IsEnabledStyle())
709 // Create RGBA texture for all the text styles (without the text itself)
710 info.stylePixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
713 if(mTextShaderFeatureCache.IsEnabledOverlay())
715 // Create RGBA texture for all the overlay styles
716 info.overlayStylePixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
719 if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor())
721 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
722 info.maskPixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
725 // Get the current offset for recalculate the offset when tiling.
726 Property::Map retMap;
727 mImpl->mTransform.GetPropertyMap(retMap);
728 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
731 offsetValue->Get(info.transformOffset);
734 // Create a textureset in the default renderer.
735 CreateTextureSet(info, mImpl->mRenderer, sampler);
737 verifiedHeight -= maxTextureSize;
739 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
741 // Create a renderer by cutting maxTextureSize.
742 while(verifiedHeight > 0)
744 VisualRenderer tilingRenderer = VisualRenderer::New(geometry, shader);
745 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
746 // New offset position of buffer for tiling.
747 info.offsetHeight += maxTextureSize;
748 // New height for tiling.
749 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
750 // New offset for tiling.
751 info.transformOffset.y += maxTextureSize;
752 // Create a textureset int the new tiling renderer.
753 CreateTextureSet(info, tilingRenderer, sampler);
755 verifiedHeight -= maxTextureSize;
759 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
761 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
763 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
765 Renderer renderer = (*iter);
768 actor.AddRenderer(renderer);
770 if(renderer != mImpl->mRenderer)
772 // Set constraint for text label's color for non-default renderers.
773 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
775 // Register unique property, or get property for default renderer.
776 Property::Index index = renderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
778 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
779 if(index != Property::INVALID_INDEX)
781 Constraint colorConstraint = Constraint::New<Vector4>(renderer, index, TextColorConstraint);
782 colorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
783 colorConstraint.Apply();
786 // Make zero if the alpha value of text color is zero to skip rendering text
787 // VisualRenderer::Property::OPACITY uses same animatable property internally.
788 Constraint opacityConstraint = Constraint::New<float>(renderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
789 opacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
790 opacityConstraint.Apply();
797 TextureSet TextVisual::GetTextTexture(const Vector2& size)
799 // Filter mode needs to be set to linear to produce better quality while scaling.
800 Sampler sampler = Sampler::New();
801 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
803 TextureSet textureSet = TextureSet::New();
805 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
806 Pixel::Format textPixelFormat = (mTextShaderFeatureCache.IsEnabledEmoji() || mTextShaderFeatureCache.IsEnabledMultiColor()) ? Pixel::RGBA8888 : Pixel::L8;
808 // Check the text direction
809 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
811 // Create a texture for the text without any styles
812 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
814 uint32_t textureSetIndex = 0u;
815 AddTexture(textureSet, data, sampler, textureSetIndex);
818 if(mTextShaderFeatureCache.IsEnabledStyle())
820 // Create RGBA texture for all the text styles that render in the background (without the text itself)
821 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
822 AddTexture(textureSet, styleData, sampler, textureSetIndex);
826 if(mTextShaderFeatureCache.IsEnabledOverlay())
828 // Create RGBA texture for overlay styles such as underline and strikethrough (without the text itself)
829 PixelData overlayStyleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
830 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
834 if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor())
836 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
837 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
839 AddTexture(textureSet, maskData, sampler, textureSetIndex);
845 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
847 // Cache feature builder informations.
848 mTextShaderFeatureCache = featureBuilder;
850 Shader shader = mTextVisualShaderFactory.GetShader(factoryCache, mTextShaderFeatureCache);
851 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
855 } // namespace Internal
857 } // namespace Toolkit