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