2 * Copyright (c) 2022 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/integration-api/debug.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 int CUSTOM_PROPERTY_COUNT(5); // anim,premul,size,offset,multicol
55 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
58 * Return Property index for the given string key
59 * param[in] stringKey the string index key
60 * return the key as an index
63 Dali::Property::Index StringKeyToIndexKey(const std::string& stringKey)
65 Dali::Property::Index result = Property::INVALID_KEY;
67 if(stringKey == VISUAL_TYPE)
69 result = Toolkit::Visual::Property::TYPE;
71 else if(stringKey == TEXT_PROPERTY)
73 result = Toolkit::TextVisual::Property::TEXT;
75 else if(stringKey == FONT_FAMILY_PROPERTY)
77 result = Toolkit::TextVisual::Property::FONT_FAMILY;
79 else if(stringKey == FONT_STYLE_PROPERTY)
81 result = Toolkit::TextVisual::Property::FONT_STYLE;
83 else if(stringKey == POINT_SIZE_PROPERTY)
85 result = Toolkit::TextVisual::Property::POINT_SIZE;
87 else if(stringKey == MULTI_LINE_PROPERTY)
89 result = Toolkit::TextVisual::Property::MULTI_LINE;
91 else if(stringKey == HORIZONTAL_ALIGNMENT_PROPERTY)
93 result = Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT;
95 else if(stringKey == VERTICAL_ALIGNMENT_PROPERTY)
97 result = Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT;
99 else if(stringKey == TEXT_COLOR_PROPERTY)
101 result = Toolkit::TextVisual::Property::TEXT_COLOR;
103 else if(stringKey == ENABLE_MARKUP_PROPERTY)
105 result = Toolkit::TextVisual::Property::ENABLE_MARKUP;
107 else if(stringKey == SHADOW_PROPERTY)
109 result = Toolkit::TextVisual::Property::SHADOW;
111 else if(stringKey == UNDERLINE_PROPERTY)
113 result = Toolkit::TextVisual::Property::UNDERLINE;
115 else if(stringKey == OUTLINE_PROPERTY)
117 result = Toolkit::DevelTextVisual::Property::OUTLINE;
119 else if(stringKey == BACKGROUND_PROPERTY)
121 result = Toolkit::DevelTextVisual::Property::BACKGROUND;
127 void TextColorConstraint(Vector4& current, const PropertyInputContainer& inputs)
129 Vector4 color = inputs[0]->GetVector4();
130 current.r = color.r * color.a;
131 current.g = color.g * color.a;
132 current.b = color.b * color.a;
136 void OpacityConstraint(float& current, const PropertyInputContainer& inputs)
138 // Make zero if the alpha value of text color is zero to skip rendering text
139 if(EqualsZero(inputs[0]->GetVector4().a))
149 } // unnamed namespace
151 TextVisualPtr TextVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
153 TextVisualPtr textVisualPtr(new TextVisual(factoryCache));
154 textVisualPtr->SetProperties(properties);
155 textVisualPtr->Initialize();
156 return textVisualPtr;
159 Property::Map TextVisual::ConvertStringKeysToIndexKeys(const Property::Map& propertyMap)
161 Property::Map outMap;
163 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
165 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
167 Property::Index indexKey = keyValue.first.indexKey;
169 if(keyValue.first.type == Property::Key::STRING)
171 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
174 outMap.Insert(indexKey, keyValue.second);
180 float TextVisual::GetHeightForWidth(float width)
182 return mController->GetHeightForWidth(width);
185 void TextVisual::GetNaturalSize(Vector2& naturalSize)
187 naturalSize = mController->GetNaturalSize().GetVectorXY();
190 void TextVisual::DoCreatePropertyMap(Property::Map& map) const
192 Property::Value value;
195 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
198 mController->GetText(text);
199 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
201 map.Insert(Toolkit::TextVisual::Property::FONT_FAMILY, mController->GetDefaultFontFamily());
203 GetFontStyleProperty(mController, value, Text::FontStyle::DEFAULT);
204 map.Insert(Toolkit::TextVisual::Property::FONT_STYLE, value);
206 map.Insert(Toolkit::TextVisual::Property::POINT_SIZE, mController->GetDefaultFontSize(Text::Controller::POINT_SIZE));
208 map.Insert(Toolkit::TextVisual::Property::MULTI_LINE, mController->IsMultiLineEnabled());
210 map.Insert(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, mController->GetHorizontalAlignment());
212 map.Insert(Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT, mController->GetVerticalAlignment());
214 map.Insert(Toolkit::TextVisual::Property::TEXT_COLOR, mController->GetDefaultColor());
216 map.Insert(Toolkit::TextVisual::Property::ENABLE_MARKUP, mController->IsMarkupProcessorEnabled());
218 GetShadowProperties(mController, value, Text::EffectStyle::DEFAULT);
219 map.Insert(Toolkit::TextVisual::Property::SHADOW, value);
221 GetUnderlineProperties(mController, value, Text::EffectStyle::DEFAULT);
222 map.Insert(Toolkit::TextVisual::Property::UNDERLINE, value);
224 GetOutlineProperties(mController, value, Text::EffectStyle::DEFAULT);
225 map.Insert(Toolkit::DevelTextVisual::Property::OUTLINE, value);
227 GetBackgroundProperties(mController, value, Text::EffectStyle::DEFAULT);
228 map.Insert(Toolkit::DevelTextVisual::Property::BACKGROUND, value);
230 GetStrikethroughProperties(mController, value, Text::EffectStyle::DEFAULT);
231 map.Insert(Toolkit::DevelTextVisual::Property::STRIKETHROUGH, value);
234 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
237 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
239 mController->GetText(text);
240 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
243 void TextVisual::EnablePreMultipliedAlpha(bool preMultiplied)
245 // Make always enable pre multiplied alpha whether preMultiplied value is false.
248 DALI_LOG_WARNING("Note : TextVisual cannot disable PreMultipliedAlpha\n");
252 TextVisual::TextVisual(VisualFactoryCache& factoryCache)
253 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
254 mController(Text::Controller::New()),
255 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
256 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
257 mTextColorAnimatableIndex(Property::INVALID_INDEX),
258 mRendererUpdateNeeded(false)
260 // Enable the pre-multiplied alpha to improve the text quality
261 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
264 TextVisual::~TextVisual()
268 void TextVisual::OnInitialize()
270 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
271 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
273 mImpl->mRenderer = VisualRenderer::New(geometry, shader);
274 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
277 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
279 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
281 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
283 Property::Index indexKey = keyValue.first.indexKey;
285 if(keyValue.first.type == Property::Key::STRING)
287 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
290 DoSetProperty(indexKey, keyValue.second);
293 // Elide the text if it exceeds the boundaries.
294 mController->SetTextElideEnabled(true);
296 // Retrieve the layout engine to set the cursor's width.
297 Text::Layout::Engine& engine = mController->GetLayoutEngine();
299 // Sets 0 as cursor's width.
300 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
303 void TextVisual::DoSetOnScene(Actor& actor)
307 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
309 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
310 if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
312 mTextColorAnimatableIndex = mImpl->mRenderer.RegisterUniqueProperty("uTextColorAnimatable", defaultColor);
316 mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
319 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
321 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
322 if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
324 if(!mColorConstraint)
326 mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
327 mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
329 mColorConstraint.Apply();
332 // Make zero if the alpha value of text color is zero to skip rendering text
333 if(!mOpacityConstraint)
335 // VisualRenderer::Property::OPACITY uses same animatable property internally.
336 mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
337 mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
339 mOpacityConstraint.Apply();
342 // Renderer needs textures and to be added to control
343 mRendererUpdateNeeded = true;
345 mRendererList.push_back(mImpl->mRenderer);
350 void TextVisual::RemoveRenderer(Actor& actor)
352 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
354 Renderer renderer = (*iter);
357 // Removes the renderer from the actor.
358 actor.RemoveRenderer(renderer);
361 // Clear the renderer list
362 mRendererList.clear();
365 void TextVisual::DoSetOffScene(Actor& actor)
369 mColorConstraint.Remove();
371 if(mOpacityConstraint)
373 mOpacityConstraint.Remove();
376 RemoveRenderer(actor);
378 // Resets the control handle.
382 void TextVisual::OnSetTransform()
387 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
391 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
393 const bool enableMarkup = propertyValue.Get<bool>();
394 mController->SetMarkupProcessorEnabled(enableMarkup);
397 case Toolkit::TextVisual::Property::TEXT:
399 mController->SetText(propertyValue.Get<std::string>());
402 case Toolkit::TextVisual::Property::FONT_FAMILY:
404 SetFontFamilyProperty(mController, propertyValue);
407 case Toolkit::TextVisual::Property::FONT_STYLE:
409 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
412 case Toolkit::TextVisual::Property::POINT_SIZE:
414 const float pointSize = propertyValue.Get<float>();
415 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
417 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
421 case Toolkit::TextVisual::Property::MULTI_LINE:
423 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
426 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
430 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
431 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
433 mController->SetHorizontalAlignment(alignment);
438 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
442 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
443 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
445 mController->SetVerticalAlignment(alignment);
450 case Toolkit::TextVisual::Property::TEXT_COLOR:
452 const Vector4& textColor = propertyValue.Get<Vector4>();
453 if(mController->GetDefaultColor() != textColor)
455 mController->SetDefaultColor(textColor);
459 case Toolkit::TextVisual::Property::SHADOW:
461 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
464 case Toolkit::TextVisual::Property::UNDERLINE:
466 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
469 case Toolkit::DevelTextVisual::Property::OUTLINE:
471 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
474 case Toolkit::DevelTextVisual::Property::BACKGROUND:
476 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
479 case Toolkit::DevelTextVisual::Property::STRIKETHROUGH:
481 SetStrikethroughProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
487 void TextVisual::UpdateRenderer()
489 Actor control = mControl.GetHandle();
496 // Calculates the size to be used to relayout.
497 Vector2 relayoutSize;
499 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
500 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
502 // Round the size and offset to avoid pixel alignement issues.
503 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
504 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
507 mController->GetText(text);
509 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
511 // Remove the texture set and any renderer previously set.
512 RemoveRenderer(control);
514 // Nothing else to do if the relayout size is zero.
515 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
519 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
521 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
523 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
525 mRendererUpdateNeeded = false;
527 // Remove the texture set and any renderer previously set.
528 RemoveRenderer(control);
530 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
531 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
533 // Check whether it is a markup text with multiple text colors
534 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
535 bool hasMultipleTextColors = (NULL != colorsBuffer);
537 // Check whether the text contains any color glyph
538 bool containsColorGlyph = false;
540 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
541 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
542 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
543 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
545 // Retrieve the glyph's info.
546 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
548 // Whether the current glyph is a color one.
549 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
551 containsColorGlyph = true;
556 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
558 bool shadowEnabled = false;
559 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
560 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
562 shadowEnabled = true;
565 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
566 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
567 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
568 const bool markupProcessorEnabled = mController->IsMarkupProcessorEnabled();
569 const bool strikethroughEnabled = mController->GetTextModel()->IsStrikethroughEnabled();
571 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled || markupProcessorEnabled || strikethroughEnabled);
572 const bool isOverlayStyle = underlineEnabled || strikethroughEnabled;
574 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
576 // Text rendered and ready to display
577 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
582 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
584 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
585 data.GetPixelFormat(),
588 texture.Upload(data);
590 textureSet.SetTexture(textureSetIndex, texture);
591 textureSet.SetSampler(textureSetIndex, sampler);
594 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
596 int bpp = Pixel::GetBytesPerPixel(textPixelFormat);
597 unsigned int bufferSize = width * height * bpp;
598 unsigned char* dstBuffer = static_cast<unsigned char*>(malloc(bufferSize));
599 memcpy(dstBuffer, buffer + offsetPosition * bpp, bufferSize);
600 PixelData pixelData = Dali::PixelData::New(dstBuffer,
605 Dali::PixelData::FREE);
609 void TextVisual::CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
611 TextureSet textureSet = TextureSet::New();
612 unsigned int textureSetIndex = 0u;
614 // Convert the buffer to pixel data to make it a texture.
618 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
619 AddTexture(textureSet, data, sampler, textureSetIndex);
623 if(styleEnabled && info.styleBuffer)
625 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
626 AddTexture(textureSet, styleData, sampler, textureSetIndex);
630 if(styleEnabled && isOverlayStyle && info.styleBuffer)
632 PixelData overlayStyleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
633 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
637 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
639 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
640 AddTexture(textureSet, maskData, sampler, textureSetIndex);
643 renderer.SetTextures(textureSet);
645 //Register transform properties
646 mImpl->mTransform.SetUniforms(renderer, Direction::LEFT_TO_RIGHT);
648 // Enable the pre-multiplied alpha to improve the text quality
649 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
650 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
652 // Set size and offset for the tiling.
653 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_SIZE, Vector2(info.width, info.height));
654 renderer.SetProperty(VisualRenderer::Property::TRANSFORM_OFFSET, Vector2(info.offSet.x, info.offSet.y));
655 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
656 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
658 mRendererList.push_back(renderer);
661 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
663 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
664 mImpl->mRenderer.SetShader(shader);
666 // Get the maximum size.
667 const int maxTextureSize = Dali::GetMaxTextureSize();
669 // No tiling required. Use the default renderer.
670 if(size.height < maxTextureSize)
672 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
674 mImpl->mRenderer.SetTextures(textureSet);
675 //Register transform properties
676 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
677 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
678 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
680 mRendererList.push_back(mImpl->mRenderer);
682 // If the pixel data exceeds the maximum size, tiling is required.
685 // Filter mode needs to be set to linear to produce better quality while scaling.
686 Sampler sampler = Sampler::New();
687 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
689 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
690 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
692 // Check the text direction
693 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
695 // Create a texture for the text without any styles
696 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
698 int verifiedWidth = data.GetWidth();
699 int verifiedHeight = data.GetHeight();
701 // Set information for creating textures.
702 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
704 // Get the buffer of text.
705 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
706 info.textBuffer = textPixelData.buffer;
710 // Create RGBA texture for all the text styles (without the text itself)
711 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
712 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(styleData);
713 info.styleBuffer = stylePixelData.buffer;
716 if(containsColorGlyph && !hasMultipleTextColors)
718 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
719 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
720 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(maskData);
721 info.maskBuffer = maskPixelData.buffer;
724 // Get the current offset for recalculate the offset when tiling.
725 Property::Map retMap;
726 mImpl->mTransform.GetPropertyMap(retMap);
727 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
730 offsetValue->Get(info.offSet);
733 // Create a textureset in the default renderer.
734 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
736 verifiedHeight -= maxTextureSize;
738 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
740 int offsetPosition = verifiedWidth * maxTextureSize;
741 // Create a renderer by cutting maxTextureSize.
742 while(verifiedHeight > 0)
744 VisualRenderer tilingRenderer = VisualRenderer::New(geometry, shader);
745 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
746 // New offset position of buffer for tiling.
747 info.offsetPosition += offsetPosition;
748 // New height for tiling.
749 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
750 // New offset for tiling.
751 info.offSet.y += maxTextureSize;
752 // Create a textureset int the new tiling renderer.
753 CreateTextureSet(info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
755 verifiedHeight -= maxTextureSize;
759 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
761 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
763 Renderer renderer = (*iter);
766 actor.AddRenderer(renderer);
771 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
773 // Filter mode needs to be set to linear to produce better quality while scaling.
774 Sampler sampler = Sampler::New();
775 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
777 TextureSet textureSet = TextureSet::New();
779 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
780 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
782 // Check the text direction
783 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
785 // Create a texture for the text without any styles
786 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
788 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
789 // In that case, create a texture. TODO: should tile the text.
790 unsigned int textureSetIndex = 0u;
791 AddTexture(textureSet, data, sampler, textureSetIndex);
796 // Create RGBA texture for all the text styles that render in the background (without the text itself)
797 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
798 AddTexture(textureSet, styleData, sampler, textureSetIndex);
800 // Create RGBA texture for overlay styles such as underline and strikethrough (without the text itself)
801 PixelData overlayStyleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
802 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
806 if(containsColorGlyph && !hasMultipleTextColors)
808 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
809 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
811 AddTexture(textureSet, maskData, sampler, textureSetIndex);
817 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
821 if(hasMultipleTextColors && !styleEnabled)
823 // We don't animate text color if the text contains multiple colors
824 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
827 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG);
828 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
829 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader);
832 else if(hasMultipleTextColors && styleEnabled)
834 // We don't animate text color if the text contains multiple colors
835 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE);
838 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
839 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
840 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader);
843 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
845 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
848 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG);
849 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
850 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader);
853 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
855 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
858 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
859 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
860 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader);
863 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
865 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
868 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG);
869 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
870 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader);
873 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
875 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
878 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG);
879 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
880 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader);
887 } // namespace Internal
889 } // namespace Toolkit