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>
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>
52 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
55 * Return Property index for the given string key
56 * param[in] stringKey the string index key
57 * return the key as an index
60 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
62 Dali::Property::Index result = Property::INVALID_KEY;
64 if(stringKey == VISUAL_TYPE)
66 result = Toolkit::Visual::Property::TYPE;
68 else if(stringKey == TEXT_PROPERTY)
70 result = Toolkit::TextVisual::Property::TEXT;
72 else if(stringKey == FONT_FAMILY_PROPERTY)
74 result = Toolkit::TextVisual::Property::FONT_FAMILY;
76 else if(stringKey == FONT_STYLE_PROPERTY)
78 result = Toolkit::TextVisual::Property::FONT_STYLE;
80 else if(stringKey == POINT_SIZE_PROPERTY)
82 result = Toolkit::TextVisual::Property::POINT_SIZE;
84 else if(stringKey == MULTI_LINE_PROPERTY)
86 result = Toolkit::TextVisual::Property::MULTI_LINE;
88 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
90 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
92 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
94 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
96 else if(stringKey == TEXT_COLOR_PROPERTY)
98 result = Toolkit::TextVisual::Property::TEXT_COLOR;
100 else if(stringKey == ENABLE_MARKUP_PROPERTY)
102 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
104 else if(stringKey == SHADOW_PROPERTY)
106 result = Toolkit::TextVisual::Property::SHADOW;
108 else if(stringKey == UNDERLINE_PROPERTY)
110 result = Toolkit::TextVisual::Property::UNDERLINE;
112 else if(stringKey == OUTLINE_PROPERTY)
114 result = Toolkit::DevelTextVisual::Property::OUTLINE;
116 else if(stringKey == BACKGROUND_PROPERTY)
118 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
124 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
126 Vector4 color = inputs[0]->GetVector4();
127 current.r = color.r * color.a;
128 current.g = color.g * color.a;
129 current.b = color.b * color.a;
133 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
135 // Make zero if the alpha value of text color is zero to skip rendering text
136 if(EqualsZero(inputs[0]->GetVector4().a))
146 } // unnamed namespace
148 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
150 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
151 textVisualPtr->SetProperties(properties);
152 textVisualPtr->Initialize();
153 return textVisualPtr;
156 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
158 Property::Map outMap;
160 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
162 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
164 Property::Index indexKey = keyValue.first.indexKey;
166 if(keyValue.first.type == Property::Key::STRING)
168 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
171 outMap.Insert(indexKey, keyValue.second);
177 float TextVisual::GetHeightForWidth(float width)
179 return mController->GetHeightForWidth(width);
182 void TextVisual::GetNaturalSize(Vector2& naturalSize)
184 naturalSize = mController->GetNaturalSize().GetVectorXY();
187 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
189 Property::Value value;
192 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
195 mController->GetText(text);
196 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
198 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
200 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
201 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
203 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
205 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
207 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
209 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
211 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
213 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
215 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
216 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
218 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
219 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
221 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
222 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
224 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
225 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
228 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
231 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
233 mController->GetText(text);
234 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
237 TextVisual::TextVisual(VisualFactoryCache& factoryCache)
238 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
239 mController(Text::Controller::New()),
240 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
241 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
242 mTextColorAnimatableIndex(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 if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
297 mTextColorAnimatableIndex = mImpl->mRenderer.RegisterProperty("uTextColorAnimatable", defaultColor);
301 mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
304 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
306 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
307 if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
309 if(!mColorConstraint)
311 mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
312 mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
314 mColorConstraint.Apply();
317 // Make zero if the alpha value of text color is zero to skip rendering text
318 if(!mOpacityConstraint)
320 mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
321 mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
323 mOpacityConstraint.Apply();
326 // Renderer needs textures and to be added to control
327 mRendererUpdateNeeded = true;
329 mRendererList.push_back(mImpl->mRenderer);
334 void TextVisual::RemoveRenderer(Actor& actor)
336 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
338 Renderer renderer = (*iter);
341 // Removes the renderer from the actor.
342 actor.RemoveRenderer(renderer);
345 // Clear the renderer list
346 mRendererList.clear();
349 void TextVisual::DoSetOffScene(Actor& actor)
353 mColorConstraint.Remove();
355 if(mOpacityConstraint)
357 mOpacityConstraint.Remove();
360 RemoveRenderer(actor);
362 // Resets the control handle.
366 void TextVisual::OnSetTransform()
371 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
375 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
377 const bool enableMarkup = propertyValue.Get<bool>();
378 mController->SetMarkupProcessorEnabled(enableMarkup);
381 case Toolkit::TextVisual::Property::TEXT:
383 mController->SetText(propertyValue.Get<std::string>());
386 case Toolkit::TextVisual::Property::FONT_FAMILY:
388 SetFontFamilyProperty(mController, propertyValue);
391 case Toolkit::TextVisual::Property::FONT_STYLE:
393 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
396 case Toolkit::TextVisual::Property::POINT_SIZE:
398 const float pointSize = propertyValue.Get<float>();
399 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
401 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
405 case Toolkit::TextVisual::Property::MULTI_LINE:
407 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
410 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
414 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
415 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
417 mController->SetHorizontalAlignment(alignment);
422 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
426 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
427 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
429 mController->SetVerticalAlignment(alignment);
434 case Toolkit::TextVisual::Property::TEXT_COLOR:
436 const Vector4& textColor = propertyValue.Get<Vector4>();
437 if(mController->GetDefaultColor() != textColor)
439 mController->SetDefaultColor(textColor);
443 case Toolkit::TextVisual::Property::SHADOW:
445 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
448 case Toolkit::TextVisual::Property::UNDERLINE:
450 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
453 case Toolkit::DevelTextVisual::Property::OUTLINE:
455 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
458 case Toolkit::DevelTextVisual::Property::BACKGROUND:
460 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
466 void TextVisual::UpdateRenderer()
468 Actor control = mControl.GetHandle();
475 // Calculates the size to be used to relayout.
476 Vector2 relayoutSize;
478 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
479 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
481 // Round the size and offset to avoid pixel alignement issues.
482 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
483 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
486 mController->GetText(text);
488 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
490 // Remove the texture set and any renderer previously set.
491 RemoveRenderer(control);
493 // Nothing else to do if the relayout size is zero.
494 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
498 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
500 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
502 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
504 mRendererUpdateNeeded = false;
506 // Remove the texture set and any renderer previously set.
507 RemoveRenderer(control);
509 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
510 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
512 // Check whether it is a markup text with multiple text colors
513 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
514 bool hasMultipleTextColors = (NULL != colorsBuffer);
516 // Check whether the text contains any color glyph
517 bool containsColorGlyph = false;
519 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
520 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
521 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
522 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
524 // Retrieve the glyph's info.
525 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
527 // Whether the current glyph is a color one.
528 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
530 containsColorGlyph = true;
535 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
537 bool shadowEnabled = false;
538 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
539 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
541 shadowEnabled = true;
544 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
545 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
546 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
547 const bool markupProcessorEnabled = mController->IsMarkupProcessorEnabled();
549 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled || markupProcessorEnabled);
551 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled);
553 // Text rendered and ready to display
554 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
559 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
561 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
562 data.GetPixelFormat(),
565 texture.Upload(data);
567 textureSet.SetTexture(textureSetIndex, texture);
568 textureSet.SetSampler(textureSetIndex, sampler);
571 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
573 int bpp = Pixel::GetBytesPerPixel(textPixelFormat);
574 unsigned int bufferSize = width * height * bpp;
575 unsigned char* dstBuffer = static_cast<unsigned char*>(malloc(bufferSize));
576 memcpy(dstBuffer, buffer + offsetPosition * bpp, bufferSize);
577 PixelData pixelData = Dali::PixelData::New(dstBuffer,
582 Dali::PixelData::FREE);
586 void TextVisual::CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
588 TextureSet textureSet = TextureSet::New();
589 unsigned int textureSetIndex = 0u;
591 // Convert the buffer to pixel data to make it a texture.
594 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
595 AddTexture(textureSet, data, sampler, textureSetIndex);
599 if(styleEnabled && info.styleBuffer)
601 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
602 AddTexture(textureSet, styleData, sampler, textureSetIndex);
606 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
608 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
609 AddTexture(textureSet, maskData, sampler, textureSetIndex);
612 renderer.SetTextures(textureSet);
614 //Register transform properties
615 mImpl->mTransform.RegisterUniforms(renderer, Direction::LEFT_TO_RIGHT);
617 // Enable the pre-multiplied alpha to improve the text quality
618 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
619 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
621 // Set size and offset for the tiling.
622 renderer.RegisterProperty(SIZE, Vector2(info.width, info.height));
623 renderer.RegisterProperty(OFFSET, Vector2(info.offSet.x, info.offSet.y));
624 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
625 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
627 mRendererList.push_back(renderer);
630 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
632 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
633 mImpl->mRenderer.SetShader(shader);
635 // Get the maximum size.
636 const int maxTextureSize = Dali::GetMaxTextureSize();
638 // No tiling required. Use the default renderer.
639 if(size.height < maxTextureSize)
641 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled);
643 mImpl->mRenderer.SetTextures(textureSet);
644 //Register transform properties
645 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
646 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
647 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
649 mRendererList.push_back(mImpl->mRenderer);
651 // If the pixel data exceeds the maximum size, tiling is required.
654 // Filter mode needs to be set to linear to produce better quality while scaling.
655 Sampler sampler = Sampler::New();
656 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
658 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
659 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
661 // Check the text direction
662 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
664 // Create a texture for the text without any styles
665 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
667 int verifiedWidth = data.GetWidth();
668 int verifiedHeight = data.GetHeight();
670 // Set information for creating textures.
671 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
673 // Get the buffer of text.
674 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
675 info.textBuffer = textPixelData.buffer;
679 // Create RGBA texture for all the text styles (without the text itself)
680 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
681 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(styleData);
682 info.styleBuffer = stylePixelData.buffer;
685 if(containsColorGlyph && !hasMultipleTextColors)
687 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
688 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
689 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(maskData);
690 info.maskBuffer = maskPixelData.buffer;
693 // Get the current offset for recalculate the offset when tiling.
694 Property::Map retMap;
695 mImpl->mTransform.GetPropertyMap(retMap);
696 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
699 offsetValue->Get(info.offSet);
702 // Create a textureset in the default renderer.
703 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled);
705 verifiedHeight -= maxTextureSize;
707 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
709 int offsetPosition = verifiedWidth * maxTextureSize;
710 // Create a renderer by cutting maxTextureSize.
711 while(verifiedHeight > 0)
713 Renderer tilingRenderer = Renderer::New(geometry, shader);
714 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
715 // New offset position of buffer for tiling.
716 info.offsetPosition += offsetPosition;
717 // New height for tiling.
718 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
719 // New offset for tiling.
720 info.offSet.y += maxTextureSize;
721 // Create a textureset int the new tiling renderer.
722 CreateTextureSet(info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled);
724 verifiedHeight -= maxTextureSize;
728 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
730 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
732 Renderer renderer = (*iter);
735 actor.AddRenderer(renderer);
740 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
742 // Filter mode needs to be set to linear to produce better quality while scaling.
743 Sampler sampler = Sampler::New();
744 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
746 TextureSet textureSet = TextureSet::New();
748 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
749 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
751 // Check the text direction
752 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
754 // Create a texture for the text without any styles
755 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
757 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
758 // In that case, create a texture. TODO: should tile the text.
759 unsigned int textureSetIndex = 0u;
761 AddTexture(textureSet, data, sampler, textureSetIndex);
766 // Create RGBA texture for all the text styles (without the text itself)
767 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
769 AddTexture(textureSet, styleData, sampler, textureSetIndex);
773 if(containsColorGlyph && !hasMultipleTextColors)
775 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
776 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
778 AddTexture(textureSet, maskData, sampler, textureSetIndex);
784 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
788 if(hasMultipleTextColors && !styleEnabled)
790 // We don't animate text color if the text contains multiple colors
791 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
794 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG);
795 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
796 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader);
799 else if(hasMultipleTextColors && styleEnabled)
801 // We don't animate text color if the text contains multiple colors
802 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE);
805 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
806 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
807 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader);
810 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
812 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
815 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG);
816 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
817 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader);
820 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
822 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
825 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
826 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
827 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader);
830 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
832 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
835 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG);
836 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
837 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader);
840 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
842 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
845 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG);
846 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
847 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader);
854 } // namespace Internal
856 } // namespace Toolkit