2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/text/text-visual.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/images/pixel-data-devel.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
26 #include <dali/public-api/animation/constraints.h>
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>
53 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
56 * Return Property index for the given string key
57 * param[in] stringKey the string index key
58 * return the key as an index
61 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
63 Dali::Property::Index result = Property::INVALID_KEY;
65 if(stringKey == VISUAL_TYPE)
67 result = Toolkit::Visual::Property::TYPE;
69 else if(stringKey == TEXT_PROPERTY)
71 result = Toolkit::TextVisual::Property::TEXT;
73 else if(stringKey == FONT_FAMILY_PROPERTY)
75 result = Toolkit::TextVisual::Property::FONT_FAMILY;
77 else if(stringKey == FONT_STYLE_PROPERTY)
79 result = Toolkit::TextVisual::Property::FONT_STYLE;
81 else if(stringKey == POINT_SIZE_PROPERTY)
83 result = Toolkit::TextVisual::Property::POINT_SIZE;
85 else if(stringKey == MULTI_LINE_PROPERTY)
87 result = Toolkit::TextVisual::Property::MULTI_LINE;
89 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
91 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
93 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
95 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
97 else if(stringKey == TEXT_COLOR_PROPERTY)
99 result = Toolkit::TextVisual::Property::TEXT_COLOR;
101 else if(stringKey == ENABLE_MARKUP_PROPERTY)
103 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
105 else if(stringKey == SHADOW_PROPERTY)
107 result = Toolkit::TextVisual::Property::SHADOW;
109 else if(stringKey == UNDERLINE_PROPERTY)
111 result = Toolkit::TextVisual::Property::UNDERLINE;
113 else if(stringKey == OUTLINE_PROPERTY)
115 result = Toolkit::DevelTextVisual::Property::OUTLINE;
117 else if(stringKey == BACKGROUND_PROPERTY)
119 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
125 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
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;
134 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
136 // Make zero if the alpha value of text color is zero to skip rendering text
137 if(EqualsZero(inputs[0]->GetVector4().a))
147 } // unnamed namespace
149 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
151 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
152 textVisualPtr->SetProperties(properties);
153 textVisualPtr->Initialize();
154 return textVisualPtr;
157 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
159 Property::Map outMap;
161 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
163 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
165 Property::Index indexKey = keyValue.first.indexKey;
167 if(keyValue.first.type == Property::Key::STRING)
169 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
172 outMap.Insert(indexKey, keyValue.second);
178 float TextVisual::GetHeightForWidth(float width)
180 return mController->GetHeightForWidth(width);
183 void TextVisual::GetNaturalSize(Vector2& naturalSize)
185 naturalSize = mController->GetNaturalSize().GetVectorXY();
188 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
190 Property::Value value;
193 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
196 mController->GetText(text);
197 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
199 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
201 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
202 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
204 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
206 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
208 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
210 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
212 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
214 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
216 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
217 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
219 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
220 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
222 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
223 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
225 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
226 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
229 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
232 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
234 mController->GetText(text);
235 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
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)
247 TextVisual::~TextVisual()
251 void TextVisual::OnInitialize()
253 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
254 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
256 mImpl->mRenderer = Renderer::New(geometry, shader);
259 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
261 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
263 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
265 Property::Index indexKey = keyValue.first.indexKey;
267 if(keyValue.first.type == Property::Key::STRING)
269 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
272 DoSetProperty(indexKey, keyValue.second);
275 // Elide the text if it exceeds the boundaries.
276 mController->SetTextElideEnabled(true);
278 // Retrieve the layout engine to set the cursor's width.
279 Text::Layout::Engine& engine = mController->GetLayoutEngine();
281 // Sets 0 as cursor's width.
282 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
285 void TextVisual::DoSetOnScene(Actor& actor)
289 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
291 // Enable the pre-multiplied alpha to improve the text quality
292 EnablePreMultipliedAlpha(true);
294 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
295 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty("uTextColorAnimatable", defaultColor);
297 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
299 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
300 if(shaderTextColorIndex != Property::INVALID_INDEX)
302 Constraint colorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint);
303 colorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
304 colorConstraint.Apply();
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();
313 // Renderer needs textures and to be added to control
314 mRendererUpdateNeeded = true;
316 mRendererList.push_back(mImpl->mRenderer);
321 void TextVisual::RemoveRenderer(Actor& actor)
323 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
325 Renderer renderer = (*iter);
328 // Removes the renderer from the actor.
329 actor.RemoveRenderer(renderer);
332 // Clear the renderer list
333 mRendererList.clear();
336 void TextVisual::DoSetOffScene(Actor& actor)
338 RemoveRenderer(actor);
340 // Resets the control handle.
344 void TextVisual::OnSetTransform()
349 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
353 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
355 const bool enableMarkup = propertyValue.Get<bool>();
356 mController->SetMarkupProcessorEnabled(enableMarkup);
359 case Toolkit::TextVisual::Property::TEXT:
361 mController->SetText(propertyValue.Get<std::string>());
364 case Toolkit::TextVisual::Property::FONT_FAMILY:
366 SetFontFamilyProperty(mController, propertyValue);
369 case Toolkit::TextVisual::Property::FONT_STYLE:
371 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
374 case Toolkit::TextVisual::Property::POINT_SIZE:
376 const float pointSize = propertyValue.Get<float>();
377 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
379 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
383 case Toolkit::TextVisual::Property::MULTI_LINE:
385 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
388 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
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))
395 mController->SetHorizontalAlignment(alignment);
400 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
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))
407 mController->SetVerticalAlignment(alignment);
412 case Toolkit::TextVisual::Property::TEXT_COLOR:
414 const Vector4& textColor = propertyValue.Get<Vector4>();
415 if(mController->GetDefaultColor() != textColor)
417 mController->SetDefaultColor(textColor);
421 case Toolkit::TextVisual::Property::SHADOW:
423 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
426 case Toolkit::TextVisual::Property::UNDERLINE:
428 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
431 case Toolkit::DevelTextVisual::Property::OUTLINE:
433 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
436 case Toolkit::DevelTextVisual::Property::BACKGROUND:
438 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
444 void TextVisual::UpdateRenderer()
446 Actor control = mControl.GetHandle();
453 // Calculates the size to be used to relayout.
454 Vector2 relayoutSize;
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;
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));
464 mController->GetText(text);
466 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
468 // Remove the texture set and any renderer previously set.
469 RemoveRenderer(control);
471 // Nothing else to do if the relayout size is zero.
472 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
476 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
478 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
480 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
482 mRendererUpdateNeeded = false;
484 // Remove the texture set and any renderer previously set.
485 RemoveRenderer(control);
487 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
488 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
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);
494 // Check whether the text contains any color glyph
495 bool containsColorGlyph = false;
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++)
502 // Retrieve the glyph's info.
503 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
505 // Whether the current glyph is a color one.
506 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
508 containsColorGlyph = true;
513 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
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)
519 shadowEnabled = true;
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();
527 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled || markupProcessorEnabled);
529 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled);
531 // Text rendered and ready to display
532 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
537 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
539 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
540 data.GetPixelFormat(),
543 texture.Upload(data);
545 textureSet.SetTexture(textureSetIndex, texture);
546 textureSet.SetSampler(textureSetIndex, sampler);
549 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
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,
560 Dali::PixelData::FREE);
564 void TextVisual::CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
566 TextureSet textureSet = TextureSet::New();
567 unsigned int textureSetIndex = 0u;
569 // Convert the buffer to pixel data to make it a texture.
572 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
573 AddTexture(textureSet, data, sampler, textureSetIndex);
577 if(styleEnabled && info.styleBuffer)
579 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
580 AddTexture(textureSet, styleData, sampler, textureSetIndex);
584 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
586 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
587 AddTexture(textureSet, maskData, sampler, textureSetIndex);
590 renderer.SetTextures(textureSet);
592 //Register transform properties
593 mImpl->mTransform.RegisterUniforms(renderer, Direction::LEFT_TO_RIGHT);
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);
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);
605 mRendererList.push_back(renderer);
608 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
610 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
611 mImpl->mRenderer.SetShader(shader);
613 // Get the maximum size.
614 const int maxTextureSize = Dali::GetMaxTextureSize();
616 // No tiling required. Use the default renderer.
617 if(size.height < maxTextureSize)
619 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled);
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);
627 mRendererList.push_back(mImpl->mRenderer);
629 // If the pixel data exceeds the maximum size, tiling is required.
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);
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;
639 // Check the text direction
640 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
642 // Create a texture for the text without any styles
643 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
645 int verifiedWidth = data.GetWidth();
646 int verifiedHeight = data.GetHeight();
648 // Set information for creating textures.
649 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
651 // Get the buffer of text.
652 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
653 info.textBuffer = textPixelData.buffer;
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;
663 if(containsColorGlyph && !hasMultipleTextColors)
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;
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);
677 offsetValue->Get(info.offSet);
680 // Create a textureset in the default renderer.
681 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled);
683 verifiedHeight -= maxTextureSize;
685 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
687 int offsetPosition = verifiedWidth * maxTextureSize;
688 // Create a renderer by cutting maxTextureSize.
689 while(verifiedHeight > 0)
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);
702 verifiedHeight -= maxTextureSize;
706 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
708 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
710 Renderer renderer = (*iter);
713 actor.AddRenderer(renderer);
718 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
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);
724 TextureSet textureSet = TextureSet::New();
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;
729 // Check the text direction
730 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
732 // Create a texture for the text without any styles
733 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
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;
739 AddTexture(textureSet, data, sampler, textureSetIndex);
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);
747 AddTexture(textureSet, styleData, sampler, textureSetIndex);
751 if(containsColorGlyph && !hasMultipleTextColors)
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);
756 AddTexture(textureSet, maskData, sampler, textureSetIndex);
762 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
766 if(hasMultipleTextColors && !styleEnabled)
768 // We don't animate text color if the text contains multiple colors
769 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
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);
777 else if(hasMultipleTextColors && styleEnabled)
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);
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);
788 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
790 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
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);
798 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
800 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
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);
808 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
810 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
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);
818 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
820 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
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);
832 } // namespace Internal
834 } // namespace Toolkit