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