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/adaptor-framework/window-devel.h>
24 #include <dali/devel-api/images/pixel-data-devel.h>
25 #include <dali/devel-api/rendering/renderer-devel.h>
26 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
27 #include <dali/public-api/animation/constraints.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
33 #include <dali-toolkit/devel-api/visuals/text-visual-properties-devel.h>
34 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
35 #include <dali-toolkit/internal/text/script-run.h>
36 #include <dali-toolkit/internal/text/text-effects-style.h>
37 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
38 #include <dali-toolkit/internal/text/text-font-style.h>
39 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
40 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
42 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
43 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
44 #include <dali-toolkit/public-api/visuals/visual-properties.h>
54 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
57 * Return Property index for the given string key
58 * param[in] stringKey the string index key
59 * return the key as an index
62 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
64 Dali::Property::Index result = Property::INVALID_KEY;
66 if(stringKey == VISUAL_TYPE)
68 result = Toolkit::Visual::Property::TYPE;
70 else if(stringKey == TEXT_PROPERTY)
72 result = Toolkit::TextVisual::Property::TEXT;
74 else if(stringKey == FONT_FAMILY_PROPERTY)
76 result = Toolkit::TextVisual::Property::FONT_FAMILY;
78 else if(stringKey == FONT_STYLE_PROPERTY)
80 result = Toolkit::TextVisual::Property::FONT_STYLE;
82 else if(stringKey == POINT_SIZE_PROPERTY)
84 result = Toolkit::TextVisual::Property::POINT_SIZE;
86 else if(stringKey == MULTI_LINE_PROPERTY)
88 result = Toolkit::TextVisual::Property::MULTI_LINE;
90 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
92 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
94 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
96 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
98 else if(stringKey == TEXT_COLOR_PROPERTY)
100 result = Toolkit::TextVisual::Property::TEXT_COLOR;
102 else if(stringKey == ENABLE_MARKUP_PROPERTY)
104 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
106 else if(stringKey == SHADOW_PROPERTY)
108 result = Toolkit::TextVisual::Property::SHADOW;
110 else if(stringKey == UNDERLINE_PROPERTY)
112 result = Toolkit::TextVisual::Property::UNDERLINE;
114 else if(stringKey == OUTLINE_PROPERTY)
116 result = Toolkit::DevelTextVisual::Property::OUTLINE;
118 else if(stringKey == BACKGROUND_PROPERTY)
120 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
126 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
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;
135 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
137 // Make zero if the alpha value of text color is zero to skip rendering text
138 if(EqualsZero(inputs[0]->GetVector4().a))
148 } // unnamed namespace
150 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
152 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
153 textVisualPtr->SetProperties(properties);
154 textVisualPtr->Initialize();
155 return textVisualPtr;
158 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
160 Property::Map outMap;
162 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
164 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
166 Property::Index indexKey = keyValue.first.indexKey;
168 if(keyValue.first.type == Property::Key::STRING)
170 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
173 outMap.Insert(indexKey, keyValue.second);
179 float TextVisual::GetHeightForWidth(float width)
181 return mController->GetHeightForWidth(width);
184 void TextVisual::GetNaturalSize(Vector2& naturalSize)
186 naturalSize = mController->GetNaturalSize().GetVectorXY();
189 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
191 Property::Value value;
194 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
197 mController->GetText(text);
198 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
200 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
202 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
203 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
205 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
207 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
209 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
211 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
213 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
215 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
217 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
218 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
220 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
221 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
223 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
224 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
226 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
227 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
230 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
233 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
235 mController->GetText(text);
236 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
239 TextVisual::TextVisual(VisualFactoryCache& factoryCache)
240 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
241 mController(Text::Controller::New()),
242 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
243 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
244 mRendererUpdateNeeded(false)
248 TextVisual::~TextVisual()
252 void TextVisual::OnInitialize()
254 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
255 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
257 mImpl->mRenderer = Renderer::New(geometry, shader);
260 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
262 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
264 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
266 Property::Index indexKey = keyValue.first.indexKey;
268 if(keyValue.first.type == Property::Key::STRING)
270 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
273 DoSetProperty(indexKey, keyValue.second);
276 // Elide the text if it exceeds the boundaries.
277 mController->SetTextElideEnabled(true);
279 // Retrieve the layout engine to set the cursor's width.
280 Text::Layout::Engine& engine = mController->GetLayoutEngine();
282 // Sets 0 as cursor's width.
283 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
286 void TextVisual::DoSetOnScene(Actor& actor)
290 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
292 // Enable the pre-multiplied alpha to improve the text quality
293 EnablePreMultipliedAlpha(true);
295 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
296 Dali::Property::Index shaderTextColorIndex = mImpl->mRenderer.RegisterProperty("uTextColorAnimatable", defaultColor);
298 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
300 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
301 if(shaderTextColorIndex != Property::INVALID_INDEX)
303 Constraint colorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, shaderTextColorIndex, TextColorConstraint);
304 colorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
305 colorConstraint.Apply();
307 // Make zero if the alpha value of text color is zero to skip rendering text
308 Constraint opacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
309 opacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
310 opacityConstraint.Apply();
314 // Renderer needs textures and to be added to control
315 mRendererUpdateNeeded = true;
317 mRendererList.push_back(mImpl->mRenderer);
322 void TextVisual::RemoveRenderer(Actor& actor)
324 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
326 Renderer renderer = (*iter);
329 // Removes the renderer from the actor.
330 actor.RemoveRenderer(renderer);
333 // Clear the renderer list
334 mRendererList.clear();
337 void TextVisual::DoSetOffScene(Actor& actor)
339 RemoveRenderer(actor);
341 // Resets the control handle.
345 void TextVisual::OnSetTransform()
350 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
354 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
356 const bool enableMarkup = propertyValue.Get<bool>();
357 mController->SetMarkupProcessorEnabled(enableMarkup);
360 case Toolkit::TextVisual::Property::TEXT:
362 mController->SetText(propertyValue.Get<std::string>());
365 case Toolkit::TextVisual::Property::FONT_FAMILY:
367 SetFontFamilyProperty(mController, propertyValue);
370 case Toolkit::TextVisual::Property::FONT_STYLE:
372 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
375 case Toolkit::TextVisual::Property::POINT_SIZE:
377 const float pointSize = propertyValue.Get<float>();
378 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
380 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
384 case Toolkit::TextVisual::Property::MULTI_LINE:
386 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
389 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
393 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
394 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
396 mController->SetHorizontalAlignment(alignment);
401 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
405 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
406 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
408 mController->SetVerticalAlignment(alignment);
413 case Toolkit::TextVisual::Property::TEXT_COLOR:
415 const Vector4& textColor = propertyValue.Get<Vector4>();
416 if(mController->GetDefaultColor() != textColor)
418 mController->SetDefaultColor(textColor);
422 case Toolkit::TextVisual::Property::SHADOW:
424 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
427 case Toolkit::TextVisual::Property::UNDERLINE:
429 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
432 case Toolkit::DevelTextVisual::Property::OUTLINE:
434 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
437 case Toolkit::DevelTextVisual::Property::BACKGROUND:
439 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
445 void TextVisual::UpdateRenderer()
447 Actor control = mControl.GetHandle();
454 // Calculates the size to be used to relayout.
455 Vector2 relayoutSize;
457 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
458 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
460 // Round the size and offset to avoid pixel alignement issues.
461 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
462 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
465 mController->GetText(text);
467 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
469 // Remove the texture set and any renderer previously set.
470 RemoveRenderer(control);
472 // Nothing else to do if the relayout size is zero.
473 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
477 Dali::LayoutDirection::Type layoutDirection;
478 if(mController->IsMatchSystemLanguageDirection())
480 layoutDirection = static_cast<Dali::LayoutDirection::Type>(DevelWindow::Get(control).GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
484 layoutDirection = static_cast<Dali::LayoutDirection::Type>(control.GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
487 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
489 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
491 mRendererUpdateNeeded = false;
493 // Remove the texture set and any renderer previously set.
494 RemoveRenderer(control);
496 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
497 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
499 // Check whether it is a markup text with multiple text colors
500 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
501 bool hasMultipleTextColors = (NULL != colorsBuffer);
503 // Check whether the text contains any color glyph
504 bool containsColorGlyph = false;
506 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
507 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
508 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
509 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
511 // Retrieve the glyph's info.
512 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
514 // Whether the current glyph is a color one.
515 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
517 containsColorGlyph = true;
522 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
524 bool shadowEnabled = false;
525 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
526 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
528 shadowEnabled = true;
531 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
532 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
533 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
536 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled);
538 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled);
540 // Text rendered and ready to display
541 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
546 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
548 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
549 data.GetPixelFormat(),
552 texture.Upload(data);
554 textureSet.SetTexture(textureSetIndex, texture);
555 textureSet.SetSampler(textureSetIndex, sampler);
558 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
560 int bpp = Pixel::GetBytesPerPixel(textPixelFormat);
561 unsigned int bufferSize = width * height * bpp;
562 unsigned char* dstBuffer = static_cast<unsigned char*>(malloc(bufferSize));
563 memcpy(dstBuffer, buffer + offsetPosition * bpp, bufferSize);
564 PixelData pixelData = Dali::PixelData::New(dstBuffer,
569 Dali::PixelData::FREE);
573 void TextVisual::CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
575 TextureSet textureSet = TextureSet::New();
576 unsigned int textureSetIndex = 0u;
578 // Convert the buffer to pixel data to make it a texture.
581 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
582 AddTexture(textureSet, data, sampler, textureSetIndex);
586 if(styleEnabled && info.styleBuffer)
588 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
589 AddTexture(textureSet, styleData, sampler, textureSetIndex);
593 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
595 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
596 AddTexture(textureSet, maskData, sampler, textureSetIndex);
599 renderer.SetTextures(textureSet);
601 //Register transform properties
602 mImpl->mTransform.RegisterUniforms(renderer, Direction::LEFT_TO_RIGHT);
604 // Enable the pre-multiplied alpha to improve the text quality
605 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
606 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
608 // Set size and offset for the tiling.
609 renderer.RegisterProperty(SIZE, Vector2(info.width, info.height));
610 renderer.RegisterProperty(OFFSET, Vector2(info.offSet.x, info.offSet.y));
611 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
612 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
614 mRendererList.push_back(renderer);
617 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
619 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
620 mImpl->mRenderer.SetShader(shader);
622 // Get the maximum size.
623 const int maxTextureSize = Dali::GetMaxTextureSize();
625 // No tiling required. Use the default renderer.
626 if(size.height < maxTextureSize)
628 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled);
630 mImpl->mRenderer.SetTextures(textureSet);
631 //Register transform properties
632 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
633 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
634 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
636 mRendererList.push_back(mImpl->mRenderer);
638 // If the pixel data exceeds the maximum size, tiling is required.
641 // Filter mode needs to be set to linear to produce better quality while scaling.
642 Sampler sampler = Sampler::New();
643 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
645 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
646 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
648 // Check the text direction
649 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
651 // Create a texture for the text without any styles
652 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
654 int verifiedWidth = data.GetWidth();
655 int verifiedHeight = data.GetHeight();
657 // Set information for creating textures.
658 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
660 // Get the buffer of text.
661 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
662 info.textBuffer = textPixelData.buffer;
666 // Create RGBA texture for all the text styles (without the text itself)
667 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
668 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(styleData);
669 info.styleBuffer = stylePixelData.buffer;
672 if(containsColorGlyph && !hasMultipleTextColors)
674 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
675 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
676 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(maskData);
677 info.maskBuffer = maskPixelData.buffer;
680 // Get the current offset for recalculate the offset when tiling.
681 Property::Map retMap;
682 mImpl->mTransform.GetPropertyMap(retMap);
683 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
686 offsetValue->Get(info.offSet);
689 // Create a textureset in the default renderer.
690 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled);
692 verifiedHeight -= maxTextureSize;
694 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
696 int offsetPosition = verifiedWidth * maxTextureSize;
697 // Create a renderer by cutting maxTextureSize.
698 while(verifiedHeight > 0)
700 Renderer tilingRenderer = Renderer::New(geometry, shader);
701 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
702 // New offset position of buffer for tiling.
703 info.offsetPosition += offsetPosition;
704 // New height for tiling.
705 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
706 // New offset for tiling.
707 info.offSet.y += maxTextureSize;
708 // Create a textureset int the new tiling renderer.
709 CreateTextureSet(info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled);
711 verifiedHeight -= maxTextureSize;
715 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
717 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
719 Renderer renderer = (*iter);
722 actor.AddRenderer(renderer);
727 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
729 // Filter mode needs to be set to linear to produce better quality while scaling.
730 Sampler sampler = Sampler::New();
731 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
733 TextureSet textureSet = TextureSet::New();
735 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
736 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
738 // Check the text direction
739 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
741 // Create a texture for the text without any styles
742 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
744 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
745 // In that case, create a texture. TODO: should tile the text.
746 unsigned int textureSetIndex = 0u;
748 AddTexture(textureSet, data, sampler, textureSetIndex);
753 // Create RGBA texture for all the text styles (without the text itself)
754 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
756 AddTexture(textureSet, styleData, sampler, textureSetIndex);
760 if(containsColorGlyph && !hasMultipleTextColors)
762 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
763 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
765 AddTexture(textureSet, maskData, sampler, textureSetIndex);
771 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
775 if(hasMultipleTextColors && !styleEnabled)
777 // We don't animate text color if the text contains multiple colors
778 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
781 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG);
782 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
783 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader);
786 else if(hasMultipleTextColors && styleEnabled)
788 // We don't animate text color if the text contains multiple colors
789 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE);
792 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
793 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
794 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader);
797 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
799 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
802 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG);
803 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
804 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader);
807 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
809 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
812 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
813 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
814 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader);
817 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
819 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
822 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG);
823 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
824 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader);
827 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
829 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
832 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG);
833 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
834 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader);
841 } // namespace Internal
843 } // namespace Toolkit