[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/text/text-visual.h>
20
21 // EXTERNAL INCLUDES
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>
28 #include <string.h>
29
30 // INTERNAL HEADER
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>
45
46 namespace Dali
47 {
48 namespace Toolkit
49 {
50 namespace Internal
51 {
52 namespace
53 {
54 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_TEXT_PERFORMANCE_MARKER, false);
55
56 const int CUSTOM_PROPERTY_COUNT(3); // uTextColorAnimatable, uHasMultipleTextColors, requireRender
57
58 /**
59  * Return Property index for the given string key
60  * param[in] stringKey the string index key
61  * return the key as an index
62  */
63
64 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
65 {
66   Dali::Property::Index result = Property::INVALID_KEY;
67
68   if(stringKey == VISUAL_TYPE)
69   {
70     result = Toolkit::Visual::Property::TYPE;
71   }
72   else if(stringKey == TEXT_PROPERTY)
73   {
74     result = Toolkit::TextVisual::Property::TEXT;
75   }
76   else if(stringKey == FONT_FAMILY_PROPERTY)
77   {
78     result = Toolkit::TextVisual::Property::FONT_FAMILY;
79   }
80   else if(stringKey == FONT_STYLE_PROPERTY)
81   {
82     result = Toolkit::TextVisual::Property::FONT_STYLE;
83   }
84   else if(stringKey == POINT_SIZE_PROPERTY)
85   {
86     result = Toolkit::TextVisual::Property::POINT_SIZE;
87   }
88   else if(stringKey == MULTI_LINE_PROPERTY)
89   {
90     result = Toolkit::TextVisual::Property::MULTI_LINE;
91   }
92   else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
93   {
94     result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
95   }
96   else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
97   {
98     result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
99   }
100   else if(stringKey == TEXT_COLOR_PROPERTY)
101   {
102     result = Toolkit::TextVisual::Property::TEXT_COLOR;
103   }
104   else if(stringKey == ENABLE_MARKUP_PROPERTY)
105   {
106     result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
107   }
108   else if(stringKey == SHADOW_PROPERTY)
109   {
110     result = Toolkit::TextVisual::Property::SHADOW;
111   }
112   else if(stringKey == UNDERLINE_PROPERTY)
113   {
114     result = Toolkit::TextVisual::Property::UNDERLINE;
115   }
116   else if(stringKey == OUTLINE_PROPERTY)
117   {
118     result = Toolkit::DevelTextVisual::Property::OUTLINE;
119   }
120   else if(stringKey == BACKGROUND_PROPERTY)
121   {
122     result = Toolkit::DevelTextVisual::Property::BACKGROUND;
123   }
124
125   return result;
126 }
127
128 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
129 {
130   Vector4 color = inputs[0]->GetVector4();
131   current.r     = color.r * color.a;
132   current.g     = color.g * color.a;
133   current.b     = color.b * color.a;
134   current.a     = color.a;
135 }
136
137 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
138 {
139   // Make zero if the alpha value of text color is zero to skip rendering text
140   if(EqualsZero(inputs[0]->GetVector4().a) && !inputs[1]->GetBoolean())
141   {
142     current = 0.0f;
143   }
144   else
145   {
146     current = 1.0f;
147   }
148 }
149
150 } // unnamed namespace
151
152 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory, const Property::Map& properties)
153 {
154   TextVisualPtr textVisualPtr(new TextVisual(factoryCache, shaderFactory));
155   textVisualPtr->SetProperties(properties);
156   textVisualPtr->Initialize();
157   return textVisualPtr;
158 }
159
160 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
161 {
162   Property::Map outMap;
163
164   for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
165   {
166     const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
167
168     Property::Index indexKey = keyValue.first.indexKey;
169
170     if(keyValue.first.type == Property::Key::STRING)
171     {
172       indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
173     }
174
175     outMap.Insert(indexKey, keyValue.second);
176   }
177
178   return outMap;
179 }
180
181 float TextVisual::GetHeightForWidth(float width)
182 {
183   return mController->GetHeightForWidth(width);
184 }
185
186 void TextVisual::GetNaturalSize(Vector2& naturalSize)
187 {
188   naturalSize = mController->GetNaturalSize().GetVectorXY();
189 }
190
191 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
192 {
193   Property::Value value;
194
195   map.Clear();
196   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
197
198   std::string text;
199   mController->GetText(text);
200   map.Insert(Toolkit::TextVisual::Property::TEXT, text);
201
202   map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
203
204   GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
205   map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
206
207   map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
208
209   map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
210
211   map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
212
213   map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
214
215   map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
216
217   map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
218
219   GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
220   map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
221
222   GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
223   map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
224
225   GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
226   map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
227
228   GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
229   map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
230
231   GetStrikethroughProperties(mController, value, Text::EffectStyle::DEFAULT);
232   map.Insert(Toolkit::DevelTextVisual::Property::STRIKETHROUGH, value);
233 }
234
235 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
236 {
237   map.Clear();
238   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
239   std::string text;
240   mController->GetText(text);
241   map.Insert(Toolkit::TextVisual::Property::TEXT, text);
242 }
243
244 void TextVisual::EnablePreMultipliedAlpha(bool preMultiplied)
245 {
246   // Make always enable pre multiplied alpha whether preMultiplied value is false.
247   if(!preMultiplied)
248   {
249     DALI_LOG_WARNING("Note : TextVisual cannot disable PreMultipliedAlpha\n");
250   }
251 }
252
253 TextVisual::TextVisual(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory)
254 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
255   mController(Text::Controller::New()),
256   mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
257   mTextVisualShaderFactory(shaderFactory),
258   mTextShaderFeatureCache(),
259   mHasMultipleTextColorsIndex(Property::INVALID_INDEX),
260   mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
261   mTextColorAnimatableIndex(Property::INVALID_INDEX),
262   mTextRequireRenderPropertyIndex(Property::INVALID_INDEX),
263   mRendererUpdateNeeded(false),
264   mTextRequireRender(false)
265 {
266   // Enable the pre-multiplied alpha to improve the text quality
267   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
268 }
269
270 TextVisual::~TextVisual()
271 {
272 }
273
274 void TextVisual::OnInitialize()
275 {
276   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
277   Shader   shader   = GetTextShader(mFactoryCache, TextVisualShaderFeature::FeatureBuilder());
278
279   mImpl->mRenderer = VisualRenderer::New(geometry, shader);
280   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
281   mTextRequireRenderPropertyIndex = mImpl->mRenderer.RegisterUniqueProperty("requireRender", mTextRequireRender);
282   mHasMultipleTextColorsIndex = mImpl->mRenderer.RegisterUniqueProperty("uHasMultipleTextColors", static_cast<float>(false));
283 }
284
285 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
286 {
287   for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
288   {
289     const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
290
291     Property::Index indexKey = keyValue.first.indexKey;
292
293     if(keyValue.first.type == Property::Key::STRING)
294     {
295       indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
296     }
297
298     DoSetProperty(indexKey, keyValue.second);
299   }
300
301   // Elide the text if it exceeds the boundaries.
302   mController->SetTextElideEnabled(true);
303
304   // Retrieve the layout engine to set the cursor's width.
305   Text::Layout::Engine& engine = mController->GetLayoutEngine();
306
307   // Sets 0 as cursor's width.
308   engine.SetCursorWidth(0u); // Do not layout space for the cursor.
309 }
310
311 void TextVisual::DoSetOnScene(Actor& actor)
312 {
313   mControl = actor;
314
315   mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
316
317   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
318   if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
319   {
320     mTextColorAnimatableIndex = mImpl->mRenderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
321   }
322   else
323   {
324     mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
325   }
326
327   if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
328   {
329     // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
330     if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
331     {
332       if(!mColorConstraint)
333       {
334         mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
335         mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
336       }
337       mColorConstraint.Apply();
338     }
339
340     // Make zero if the alpha value of text color is zero to skip rendering text
341     if(!mOpacityConstraint)
342     {
343       // VisualRenderer::Property::OPACITY uses same animatable property internally.
344       mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
345       mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
346       mOpacityConstraint.AddSource(Source(mImpl->mRenderer, mTextRequireRenderPropertyIndex));
347     }
348     mOpacityConstraint.Apply();
349   }
350
351   // Renderer needs textures and to be added to control
352   mRendererUpdateNeeded = true;
353
354   UpdateRenderer();
355 }
356
357 void TextVisual::RemoveRenderer(Actor& actor, bool removeDefaultRenderer)
358 {
359   for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
360   {
361     Renderer renderer = (*iter);
362     if(renderer &&
363        (removeDefaultRenderer || (renderer != mImpl->mRenderer)))
364     {
365       // Removes the renderer from the actor.
366       actor.RemoveRenderer(renderer);
367     }
368   }
369   // Clear the renderer list
370   mRendererList.clear();
371 }
372
373 void TextVisual::DoSetOffScene(Actor& actor)
374 {
375   if(mColorConstraint)
376   {
377     mColorConstraint.Remove();
378   }
379   if(mOpacityConstraint)
380   {
381     mOpacityConstraint.Remove();
382   }
383
384   RemoveRenderer(actor, true);
385
386   // Resets the control handle.
387   mControl.Reset();
388 }
389
390 void TextVisual::OnSetTransform()
391 {
392   UpdateRenderer();
393 }
394
395 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
396 {
397   switch(index)
398   {
399     case Toolkit::TextVisual::Property::ENABLE_MARKUP:
400     {
401       const bool enableMarkup = propertyValue.Get<bool>();
402       mController->SetMarkupProcessorEnabled(enableMarkup);
403       break;
404     }
405     case Toolkit::TextVisual::Property::TEXT:
406     {
407       mController->SetText(propertyValue.Get<std::string>());
408       break;
409     }
410     case Toolkit::TextVisual::Property::FONT_FAMILY:
411     {
412       SetFontFamilyProperty(mController, propertyValue);
413       break;
414     }
415     case Toolkit::TextVisual::Property::FONT_STYLE:
416     {
417       SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
418       break;
419     }
420     case Toolkit::TextVisual::Property::POINT_SIZE:
421     {
422       const float pointSize = propertyValue.Get<float>();
423       if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
424       {
425         mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
426       }
427       break;
428     }
429     case Toolkit::TextVisual::Property::MULTI_LINE:
430     {
431       mController->SetMultiLineEnabled(propertyValue.Get<bool>());
432       break;
433     }
434     case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
435     {
436       if(mController)
437       {
438         Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
439         if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
440         {
441           mController->SetHorizontalAlignment(alignment);
442         }
443       }
444       break;
445     }
446     case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
447     {
448       if(mController)
449       {
450         Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
451         if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
452         {
453           mController->SetVerticalAlignment(alignment);
454         }
455       }
456       break;
457     }
458     case Toolkit::TextVisual::Property::TEXT_COLOR:
459     {
460       const Vector4& textColor = propertyValue.Get<Vector4>();
461       if(mController->GetDefaultColor() != textColor)
462       {
463         mController->SetDefaultColor(textColor);
464       }
465       break;
466     }
467     case Toolkit::TextVisual::Property::SHADOW:
468     {
469       SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
470       break;
471     }
472     case Toolkit::TextVisual::Property::UNDERLINE:
473     {
474       SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
475       break;
476     }
477     case Toolkit::DevelTextVisual::Property::OUTLINE:
478     {
479       SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
480       break;
481     }
482     case Toolkit::DevelTextVisual::Property::BACKGROUND:
483     {
484       SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
485       break;
486     }
487     case Toolkit::DevelTextVisual::Property::STRIKETHROUGH:
488     {
489       SetStrikethroughProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
490       break;
491     }
492   }
493 }
494
495 void TextVisual::UpdateRenderer()
496 {
497   Actor control = mControl.GetHandle();
498   if(!control)
499   {
500     // Nothing to do.
501     return;
502   }
503
504   // Calculates the size to be used to relayout.
505   Vector2 relayoutSize;
506
507   const bool isWidthRelative  = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
508   const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
509
510   const float controlWidth = mImpl->mControlSize.width;
511   const float controlHeight = mImpl->mControlSize.height;
512
513   // Round the size and offset to avoid pixel alignement issues.
514   relayoutSize.width  = floorf(0.5f + (isWidthRelative ? controlWidth * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
515   relayoutSize.height = floorf(0.5f + (isHeightRelative ? controlHeight * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
516
517   auto textLengthUtf32 = mController->GetNumberOfCharacters();
518
519   if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || textLengthUtf32 == 0u)
520   {
521     // Remove the texture set and any renderer previously set.
522     RemoveRenderer(control, true);
523
524     // Nothing else to do if the relayout size is zero.
525     ResourceReady(Toolkit::Visual::ResourceStatus::READY);
526     return;
527   }
528
529   Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
530
531   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
532
533   if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
534   {
535     mRendererUpdateNeeded = false;
536
537     // Remove the texture set and any renderer previously set.
538     // Note, we don't need to remove the mImpl->Renderer, since it will be added again after AddRenderer call.
539     RemoveRenderer(control, false);
540
541     if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
542        (relayoutSize.height > Math::MACHINE_EPSILON_1000))
543     {
544       // Check whether it is a markup text with multiple text colors
545       const Vector4* const colorsBuffer          = mController->GetTextModel()->GetColors();
546       bool                 hasMultipleTextColors = (NULL != colorsBuffer);
547
548       // Check whether the text contains any color glyph
549       bool containsColorGlyph = false;
550
551       TextAbstraction::FontClient  fontClient     = TextAbstraction::FontClient::Get();
552       const Text::GlyphInfo* const glyphsBuffer   = mController->GetTextModel()->GetGlyphs();
553       const Text::Length           numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
554       for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
555       {
556         // Retrieve the glyph's info.
557         const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
558
559         // Whether the current glyph is a color one.
560         if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
561         {
562           containsColorGlyph = true;
563           break;
564         }
565       }
566
567       // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
568
569       bool           shadowEnabled = false;
570       const Vector2& shadowOffset  = mController->GetTextModel()->GetShadowOffset();
571       if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
572       {
573         shadowEnabled = true;
574       }
575
576       const bool outlineEnabled               = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
577       const bool backgroundEnabled            = mController->GetTextModel()->IsBackgroundEnabled();
578       const bool markupOrSpannedText          = mController->IsMarkupProcessorEnabled() || mController->GetTextModel()->IsSpannedTextPlaced();
579       const bool markupUnderlineEnabled       = markupOrSpannedText && mController->GetTextModel()->IsMarkupUnderlineSet();
580       const bool markupStrikethroughEnabled   = markupOrSpannedText && mController->GetTextModel()->IsMarkupStrikethroughSet();
581       const bool underlineEnabled             = mController->GetTextModel()->IsUnderlineEnabled() || markupUnderlineEnabled;
582       const bool strikethroughEnabled         = mController->GetTextModel()->IsStrikethroughEnabled() || markupStrikethroughEnabled;
583       const bool backgroundMarkupSet          = mController->GetTextModel()->IsMarkupBackgroundColorSet();
584       const bool cutoutEnabled                = mController->IsTextCutout();
585       const bool backgroundWithCutoutEnabled  = mController->GetTextModel()->IsBackgroundWithCutoutEnabled();
586       const bool styleEnabled                 = (shadowEnabled || outlineEnabled || backgroundEnabled || markupOrSpannedText || backgroundMarkupSet || cutoutEnabled || backgroundWithCutoutEnabled);
587       const bool isOverlayStyle               = underlineEnabled || strikethroughEnabled;
588
589       // if background with cutout is enabled, This text visual must render the entire control size.
590
591       if(cutoutEnabled)
592       {
593         relayoutSize = Vector2(controlWidth, controlHeight);
594         mImpl->mTransform.mSize.width = controlWidth;
595         mImpl->mTransform.mSize.height = controlHeight;
596         mImpl->mTransform.mOffset.x = 0;
597         mImpl->mTransform.mOffset.y = 0;
598       }
599
600       AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
601
602       // Text rendered and ready to display
603       ResourceReady(Toolkit::Visual::ResourceStatus::READY);
604     }
605   }
606 }
607
608 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
609 {
610   Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
611                                  data.GetPixelFormat(),
612                                  data.GetWidth(),
613                                  data.GetHeight());
614   texture.Upload(data);
615
616   textureSet.SetTexture(textureSetIndex, texture);
617   textureSet.SetSampler(textureSetIndex, sampler);
618 }
619
620 void TextVisual::AddTilingTexture(TextureSet& textureSet, TilingInfo& tilingInfo, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
621 {
622   Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
623                                  tilingInfo.textPixelFormat,
624                                  tilingInfo.width,
625                                  tilingInfo.height);
626   DevelTexture::UploadSubPixelData(texture, data, 0u, tilingInfo.offsetHeight, tilingInfo.width, tilingInfo.height);
627
628   textureSet.SetTexture(textureSetIndex, texture);
629   textureSet.SetSampler(textureSetIndex, sampler);
630 }
631
632 void TextVisual::CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler)
633 {
634   TextureSet textureSet      = TextureSet::New();
635   uint32_t   textureSetIndex = 0u;
636
637   // Convert the buffer to pixel data to make it a texture.
638
639   if(info.textPixelData)
640   {
641     AddTilingTexture(textureSet, info, info.textPixelData, sampler, textureSetIndex);
642     ++textureSetIndex;
643   }
644
645   if(mTextShaderFeatureCache.IsEnabledStyle() && info.stylePixelData)
646   {
647     AddTilingTexture(textureSet, info, info.stylePixelData, sampler, textureSetIndex);
648     ++textureSetIndex;
649   }
650
651   if(mTextShaderFeatureCache.IsEnabledOverlay() && info.overlayStylePixelData)
652   {
653     AddTilingTexture(textureSet, info, info.overlayStylePixelData, sampler, textureSetIndex);
654     ++textureSetIndex;
655   }
656
657   if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor() && info.maskPixelData)
658   {
659     AddTilingTexture(textureSet, info, info.maskPixelData, sampler, textureSetIndex);
660     ++textureSetIndex;
661   }
662
663   renderer.SetTextures(textureSet);
664
665   //Register transform properties
666   mImpl->mTransform.SetUniforms(renderer, Direction::LEFT_TO_RIGHT);
667
668   // Enable the pre-multiplied alpha to improve the text quality
669   renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
670   renderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, true);
671
672   // Set size and offset for the tiling.
673   renderer.SetProperty(VisualRenderer::Property::TRANSFORM_SIZE, Vector2(info.width, info.height));
674   renderer.SetProperty(VisualRenderer::Property::TRANSFORM_OFFSET, info.transformOffset);
675   renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
676   renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(mTextShaderFeatureCache.IsEnabledMultiColor()));
677
678   mRendererList.push_back(renderer);
679 }
680
681 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
682 {
683   Shader shader = GetTextShader(mFactoryCache, TextVisualShaderFeature::FeatureBuilder().EnableMultiColor(hasMultipleTextColors).EnableEmoji(containsColorGlyph).EnableStyle(styleEnabled).EnableOverlay(isOverlayStyle));
684   mImpl->mRenderer.SetShader(shader);
685
686   DALI_TRACE_SCOPE(gTraceFilter, "DALI_TEXT_VISUAL_UPDATE_RENDERER");
687
688   // Get the maximum size.
689   const int maxTextureSize = Dali::GetMaxTextureSize();
690
691   // No tiling required. Use the default renderer.
692   if(size.height < maxTextureSize)
693   {
694     TextureSet textureSet = GetTextTexture(size);
695
696     mImpl->mRenderer.SetTextures(textureSet);
697     //Register transform properties
698     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
699     mImpl->mRenderer.SetProperty(mHasMultipleTextColorsIndex, static_cast<float>(hasMultipleTextColors));
700     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
701
702     mRendererList.push_back(mImpl->mRenderer);
703   }
704   // If the pixel data exceeds the maximum size, tiling is required.
705   else
706   {
707     // Filter mode needs to be set to linear to produce better quality while scaling.
708     Sampler sampler = Sampler::New();
709     sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
710
711     // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
712     Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
713
714     // Check the text direction
715     Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
716
717     // Create a texture for the text without any styles
718     PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
719
720     int verifiedWidth  = data.GetWidth();
721     int verifiedHeight = data.GetHeight();
722
723     // Set information for creating textures.
724     TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
725
726     // Get the pixel data of text.
727     info.textPixelData = data;
728
729     if(mTextShaderFeatureCache.IsEnabledStyle())
730     {
731       // Create RGBA texture for all the text styles (without the text itself)
732       info.stylePixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
733     }
734
735     if(mTextShaderFeatureCache.IsEnabledOverlay())
736     {
737       // Create RGBA texture for all the overlay styles
738       info.overlayStylePixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
739     }
740
741     if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor())
742     {
743       // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
744       info.maskPixelData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
745     }
746
747     // Get the current offset for recalculate the offset when tiling.
748     Property::Map retMap;
749     mImpl->mTransform.GetPropertyMap(retMap);
750     Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
751     if(offsetValue)
752     {
753       offsetValue->Get(info.transformOffset);
754     }
755
756     // Create a textureset in the default renderer.
757     CreateTextureSet(info, mImpl->mRenderer, sampler);
758
759     verifiedHeight -= maxTextureSize;
760
761     Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
762
763     // Create a renderer by cutting maxTextureSize.
764     while(verifiedHeight > 0)
765     {
766       VisualRenderer tilingRenderer = VisualRenderer::New(geometry, shader);
767       tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
768       // New offset position of buffer for tiling.
769       info.offsetHeight += maxTextureSize;
770       // New height for tiling.
771       info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
772       // New offset for tiling.
773       info.transformOffset.y += maxTextureSize;
774       // Create a textureset int the new tiling renderer.
775       CreateTextureSet(info, tilingRenderer, sampler);
776
777       verifiedHeight -= maxTextureSize;
778     }
779   }
780
781   mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
782
783   const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
784
785   for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
786   {
787     Renderer renderer = (*iter);
788     if(renderer)
789     {
790       // Note, AddRenderer will ignore renderer if it is already added. @SINCE 2_3.22
791       actor.AddRenderer(renderer);
792
793       if(renderer != mImpl->mRenderer)
794       {
795         // Set constraint for text label's color for non-default renderers.
796         if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
797         {
798           // Register unique property, or get property for default renderer.
799           Property::Index index = renderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
800
801           // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
802           if(index != Property::INVALID_INDEX)
803           {
804             Constraint colorConstraint = Constraint::New<Vector4>(renderer, index, TextColorConstraint);
805             colorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
806             colorConstraint.Apply();
807           }
808
809           // Make zero if the alpha value of text color is zero to skip rendering text
810           // VisualRenderer::Property::OPACITY uses same animatable property internally.
811           Constraint opacityConstraint = Constraint::New<float>(renderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
812           opacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
813           opacityConstraint.AddSource(Source(mImpl->mRenderer, mTextRequireRenderPropertyIndex));
814           opacityConstraint.Apply();
815         }
816       }
817     }
818   }
819 }
820
821 TextureSet TextVisual::GetTextTexture(const Vector2& size)
822 {
823   const bool cutoutEnabled = mController->IsTextCutout();
824
825   // Filter mode needs to be set to linear to produce better quality while scaling.
826   Sampler sampler = Sampler::New();
827   sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
828
829   TextureSet textureSet = TextureSet::New();
830
831   // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
832   Pixel::Format textPixelFormat = (mTextShaderFeatureCache.IsEnabledEmoji() || mTextShaderFeatureCache.IsEnabledMultiColor() || cutoutEnabled) ? Pixel::RGBA8888 : Pixel::L8;
833
834   // Check the text direction
835   Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
836   uint32_t textureSetIndex = 0u;
837   // Create a texture for the text without any styles
838
839   Devel::PixelBuffer cutoutData;
840   float cutoutAlpha = mController->GetTextModel()->GetDefaultColor().a;
841   if(cutoutEnabled)
842   {
843     cutoutData = mTypesetter->RenderWithPixelBuffer(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
844
845     // Make transparent buffer.
846     // If the cutout is enabled, a separate texture is not used for the text.
847     Devel::PixelBuffer buffer = mTypesetter->CreateFullBackgroundBuffer(1, 1, Vector4(0.f, 0.f ,0.f ,0.f));
848     PixelData data = Devel::PixelBuffer::Convert(buffer);
849     AddTexture(textureSet, data, sampler, textureSetIndex);
850     ++textureSetIndex;
851   }
852   else
853   {
854     PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
855     AddTexture(textureSet, data, sampler, textureSetIndex);
856     ++textureSetIndex;
857   }
858
859
860   if(mTextShaderFeatureCache.IsEnabledStyle())
861   {
862     // Create RGBA texture for all the text styles that render in the background (without the text itself)
863     PixelData styleData;
864
865     if(cutoutEnabled && cutoutData)
866     {
867       styleData = mTypesetter->RenderWithCutout(size, textDirection, cutoutData, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888, cutoutAlpha);
868     }
869     else
870     {
871       styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
872     }
873
874     AddTexture(textureSet, styleData, sampler, textureSetIndex);
875     ++textureSetIndex;
876   }
877
878   if(mTextShaderFeatureCache.IsEnabledOverlay())
879   {
880     // Create RGBA texture for overlay styles such as underline and strikethrough (without the text itself)
881     PixelData overlayStyleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
882     AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
883     ++textureSetIndex;
884   }
885
886   if(mTextShaderFeatureCache.IsEnabledEmoji() && !mTextShaderFeatureCache.IsEnabledMultiColor())
887   {
888     // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
889     PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
890
891     AddTexture(textureSet, maskData, sampler, textureSetIndex);
892   }
893
894   return textureSet;
895 }
896
897 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
898 {
899   // Cache feature builder informations.
900   mTextShaderFeatureCache = featureBuilder;
901
902   Shader shader = mTextVisualShaderFactory.GetShader(factoryCache, mTextShaderFeatureCache);
903   return shader;
904 }
905
906 void TextVisual::SetRequireRender(bool requireRender)
907 {
908   mTextRequireRender = requireRender;
909   if(mImpl->mRenderer)
910   {
911     mImpl->mRenderer.SetProperty(mTextRequireRenderPropertyIndex, mTextRequireRender);
912   }
913 }
914
915 } // namespace Internal
916
917 } // namespace Toolkit
918
919 } // namespace Dali