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>
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 int CUSTOM_PROPERTY_COUNT(10); // 5 transform properties + anim,premul,size,offset,multicol
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);
229 GetStrikethroughProperties(mController, value, Text::EffectStyle::DEFAULT);
230 map.Insert(Toolkit::DevelTextVisual::Property::STRIKETHROUGH, value);
233 void TextVisual::DoCreateInstancePropertyMap(Property::Map& map) const
236 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
238 mController->GetText(text);
239 map.Insert(Toolkit::TextVisual::Property::TEXT, text);
242 TextVisual::TextVisual(VisualFactoryCache& factoryCache)
243 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::TEXT),
244 mController(Text::Controller::New()),
245 mTypesetter(Text::Typesetter::New(mController->GetTextModel())),
246 mAnimatableTextColorPropertyIndex(Property::INVALID_INDEX),
247 mTextColorAnimatableIndex(Property::INVALID_INDEX),
248 mRendererUpdateNeeded(false)
252 TextVisual::~TextVisual()
256 void TextVisual::OnInitialize()
258 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
259 Shader shader = GetTextShader(mFactoryCache, TextType::SINGLE_COLOR_TEXT, TextType::NO_EMOJI, TextType::NO_STYLES);
261 mImpl->mRenderer = Renderer::New(geometry, shader);
262 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
265 void TextVisual::DoSetProperties(const Property::Map& propertyMap)
267 for(Property::Map::SizeType index = 0u, count = propertyMap.Count(); index < count; ++index)
269 const KeyValuePair& keyValue = propertyMap.GetKeyValue(index);
271 Property::Index indexKey = keyValue.first.indexKey;
273 if(keyValue.first.type == Property::Key::STRING)
275 indexKey = StringKeyToIndexKey(keyValue.first.stringKey);
278 DoSetProperty(indexKey, keyValue.second);
281 // Elide the text if it exceeds the boundaries.
282 mController->SetTextElideEnabled(true);
284 // Retrieve the layout engine to set the cursor's width.
285 Text::Layout::Engine& engine = mController->GetLayoutEngine();
287 // Sets 0 as cursor's width.
288 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
291 void TextVisual::DoSetOnScene(Actor& actor)
295 mImpl->mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
297 // Enable the pre-multiplied alpha to improve the text quality
298 EnablePreMultipliedAlpha(true);
300 const Vector4& defaultColor = mController->GetTextModel()->GetDefaultColor();
301 if(mTextColorAnimatableIndex == Property::INVALID_INDEX)
303 mTextColorAnimatableIndex = mImpl->mRenderer.RegisterProperty("uTextColorAnimatable", defaultColor);
307 mImpl->mRenderer.SetProperty(mTextColorAnimatableIndex, defaultColor);
310 if(mAnimatableTextColorPropertyIndex != Property::INVALID_INDEX)
312 // Create constraint for the animatable text's color Property with uTextColorAnimatable in the renderer.
313 if(mTextColorAnimatableIndex != Property::INVALID_INDEX)
315 if(!mColorConstraint)
317 mColorConstraint = Constraint::New<Vector4>(mImpl->mRenderer, mTextColorAnimatableIndex, TextColorConstraint);
318 mColorConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
320 mColorConstraint.Apply();
323 // Make zero if the alpha value of text color is zero to skip rendering text
324 if(!mOpacityConstraint)
326 mOpacityConstraint = Constraint::New<float>(mImpl->mRenderer, Dali::DevelRenderer::Property::OPACITY, OpacityConstraint);
327 mOpacityConstraint.AddSource(Source(actor, mAnimatableTextColorPropertyIndex));
329 mOpacityConstraint.Apply();
332 // Renderer needs textures and to be added to control
333 mRendererUpdateNeeded = true;
335 mRendererList.push_back(mImpl->mRenderer);
340 void TextVisual::RemoveRenderer(Actor& actor)
342 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
344 Renderer renderer = (*iter);
347 // Removes the renderer from the actor.
348 actor.RemoveRenderer(renderer);
351 // Clear the renderer list
352 mRendererList.clear();
355 void TextVisual::DoSetOffScene(Actor& actor)
359 mColorConstraint.Remove();
361 if(mOpacityConstraint)
363 mOpacityConstraint.Remove();
366 RemoveRenderer(actor);
368 // Resets the control handle.
372 void TextVisual::OnSetTransform()
377 void TextVisual::DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue)
381 case Toolkit::TextVisual::Property::ENABLE_MARKUP:
383 const bool enableMarkup = propertyValue.Get<bool>();
384 mController->SetMarkupProcessorEnabled(enableMarkup);
387 case Toolkit::TextVisual::Property::TEXT:
389 mController->SetText(propertyValue.Get<std::string>());
392 case Toolkit::TextVisual::Property::FONT_FAMILY:
394 SetFontFamilyProperty(mController, propertyValue);
397 case Toolkit::TextVisual::Property::FONT_STYLE:
399 SetFontStyleProperty(mController, propertyValue, Text::FontStyle::DEFAULT);
402 case Toolkit::TextVisual::Property::POINT_SIZE:
404 const float pointSize = propertyValue.Get<float>();
405 if(!Equals(mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
407 mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
411 case Toolkit::TextVisual::Property::MULTI_LINE:
413 mController->SetMultiLineEnabled(propertyValue.Get<bool>());
416 case Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT:
420 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
421 if(Toolkit::Text::GetHorizontalAlignmentEnumeration(propertyValue, alignment))
423 mController->SetHorizontalAlignment(alignment);
428 case Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT:
432 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
433 if(Toolkit::Text::GetVerticalAlignmentEnumeration(propertyValue, alignment))
435 mController->SetVerticalAlignment(alignment);
440 case Toolkit::TextVisual::Property::TEXT_COLOR:
442 const Vector4& textColor = propertyValue.Get<Vector4>();
443 if(mController->GetDefaultColor() != textColor)
445 mController->SetDefaultColor(textColor);
449 case Toolkit::TextVisual::Property::SHADOW:
451 SetShadowProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
454 case Toolkit::TextVisual::Property::UNDERLINE:
456 SetUnderlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
459 case Toolkit::DevelTextVisual::Property::OUTLINE:
461 SetOutlineProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
464 case Toolkit::DevelTextVisual::Property::BACKGROUND:
466 SetBackgroundProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
469 case Toolkit::DevelTextVisual::Property::STRIKETHROUGH:
471 SetStrikethroughProperties(mController, propertyValue, Text::EffectStyle::DEFAULT);
477 void TextVisual::UpdateRenderer()
479 Actor control = mControl.GetHandle();
486 // Calculates the size to be used to relayout.
487 Vector2 relayoutSize;
489 const bool isWidthRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.z) < Math::MACHINE_EPSILON_1000;
490 const bool isHeightRelative = fabsf(mImpl->mTransform.mOffsetSizeMode.w) < Math::MACHINE_EPSILON_1000;
492 // Round the size and offset to avoid pixel alignement issues.
493 relayoutSize.width = floorf(0.5f + (isWidthRelative ? mImpl->mControlSize.width * mImpl->mTransform.mSize.x : mImpl->mTransform.mSize.width));
494 relayoutSize.height = floorf(0.5f + (isHeightRelative ? mImpl->mControlSize.height * mImpl->mTransform.mSize.y : mImpl->mTransform.mSize.height));
497 mController->GetText(text);
499 if((fabsf(relayoutSize.width) < Math::MACHINE_EPSILON_1000) || (fabsf(relayoutSize.height) < Math::MACHINE_EPSILON_1000) || text.empty())
501 // Remove the texture set and any renderer previously set.
502 RemoveRenderer(control);
504 // Nothing else to do if the relayout size is zero.
505 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
509 Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(control);
511 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(relayoutSize, layoutDirection);
513 if(Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType) || mRendererUpdateNeeded)
515 mRendererUpdateNeeded = false;
517 // Remove the texture set and any renderer previously set.
518 RemoveRenderer(control);
520 if((relayoutSize.width > Math::MACHINE_EPSILON_1000) &&
521 (relayoutSize.height > Math::MACHINE_EPSILON_1000))
523 // Check whether it is a markup text with multiple text colors
524 const Vector4* const colorsBuffer = mController->GetTextModel()->GetColors();
525 bool hasMultipleTextColors = (NULL != colorsBuffer);
527 // Check whether the text contains any color glyph
528 bool containsColorGlyph = false;
530 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
531 const Text::GlyphInfo* const glyphsBuffer = mController->GetTextModel()->GetGlyphs();
532 const Text::Length numberOfGlyphs = mController->GetTextModel()->GetNumberOfGlyphs();
533 for(Text::Length glyphIndex = 0; glyphIndex < numberOfGlyphs; glyphIndex++)
535 // Retrieve the glyph's info.
536 const Text::GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
538 // Whether the current glyph is a color one.
539 if(fontClient.IsColorGlyph(glyphInfo->fontId, glyphInfo->index))
541 containsColorGlyph = true;
546 // Check whether the text contains any style colors (e.g. underline color, shadow color, etc.)
548 bool shadowEnabled = false;
549 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
550 if(fabsf(shadowOffset.x) > Math::MACHINE_EPSILON_1 || fabsf(shadowOffset.y) > Math::MACHINE_EPSILON_1)
552 shadowEnabled = true;
555 const bool underlineEnabled = mController->GetTextModel()->IsUnderlineEnabled();
556 const bool outlineEnabled = (mController->GetTextModel()->GetOutlineWidth() > Math::MACHINE_EPSILON_1);
557 const bool backgroundEnabled = mController->GetTextModel()->IsBackgroundEnabled();
558 const bool markupProcessorEnabled = mController->IsMarkupProcessorEnabled();
559 const bool strikethroughEnabled = mController->GetTextModel()->IsStrikethroughEnabled();
561 const bool styleEnabled = (shadowEnabled || underlineEnabled || outlineEnabled || backgroundEnabled || markupProcessorEnabled || strikethroughEnabled);
562 const bool isOverlayStyle = underlineEnabled || strikethroughEnabled;
564 AddRenderer(control, relayoutSize, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
566 // Text rendered and ready to display
567 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
572 void TextVisual::AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex)
574 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
575 data.GetPixelFormat(),
578 texture.Upload(data);
580 textureSet.SetTexture(textureSetIndex, texture);
581 textureSet.SetSampler(textureSetIndex, sampler);
584 PixelData TextVisual::ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat)
586 int bpp = Pixel::GetBytesPerPixel(textPixelFormat);
587 unsigned int bufferSize = width * height * bpp;
588 unsigned char* dstBuffer = static_cast<unsigned char*>(malloc(bufferSize));
589 memcpy(dstBuffer, buffer + offsetPosition * bpp, bufferSize);
590 PixelData pixelData = Dali::PixelData::New(dstBuffer,
595 Dali::PixelData::FREE);
599 void TextVisual::CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
601 TextureSet textureSet = TextureSet::New();
602 unsigned int textureSetIndex = 0u;
604 // Convert the buffer to pixel data to make it a texture.
608 PixelData data = ConvertToPixelData(info.textBuffer, info.width, info.height, info.offsetPosition, info.textPixelFormat);
609 AddTexture(textureSet, data, sampler, textureSetIndex);
613 if(styleEnabled && info.styleBuffer)
615 PixelData styleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
616 AddTexture(textureSet, styleData, sampler, textureSetIndex);
620 if(styleEnabled && isOverlayStyle)
622 PixelData overlayStyleData = ConvertToPixelData(info.styleBuffer, info.width, info.height, info.offsetPosition, Pixel::RGBA8888);
623 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
627 if(containsColorGlyph && !hasMultipleTextColors && info.maskBuffer)
629 PixelData maskData = ConvertToPixelData(info.maskBuffer, info.width, info.height, info.offsetPosition, Pixel::L8);
630 AddTexture(textureSet, maskData, sampler, textureSetIndex);
633 renderer.SetTextures(textureSet);
635 //Register transform properties
636 mImpl->mTransform.RegisterUniforms(renderer, Direction::LEFT_TO_RIGHT);
638 // Enable the pre-multiplied alpha to improve the text quality
639 renderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true);
640 renderer.RegisterProperty(PREMULTIPLIED_ALPHA, 1.0f);
642 // Set size and offset for the tiling.
643 renderer.RegisterProperty(SIZE, Vector2(info.width, info.height));
644 renderer.RegisterProperty(OFFSET, Vector2(info.offSet.x, info.offSet.y));
645 renderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
646 renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
648 mRendererList.push_back(renderer);
651 void TextVisual::AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
653 Shader shader = GetTextShader(mFactoryCache, hasMultipleTextColors, containsColorGlyph, styleEnabled);
654 mImpl->mRenderer.SetShader(shader);
656 // Get the maximum size.
657 const int maxTextureSize = Dali::GetMaxTextureSize();
659 // No tiling required. Use the default renderer.
660 if(size.height < maxTextureSize)
662 TextureSet textureSet = GetTextTexture(size, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
664 mImpl->mRenderer.SetTextures(textureSet);
665 //Register transform properties
666 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
667 mImpl->mRenderer.RegisterProperty("uHasMultipleTextColors", static_cast<float>(hasMultipleTextColors));
668 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
670 mRendererList.push_back(mImpl->mRenderer);
672 // If the pixel data exceeds the maximum size, tiling is required.
675 // Filter mode needs to be set to linear to produce better quality while scaling.
676 Sampler sampler = Sampler::New();
677 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
679 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
680 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
682 // Check the text direction
683 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
685 // Create a texture for the text without any styles
686 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
688 int verifiedWidth = data.GetWidth();
689 int verifiedHeight = data.GetHeight();
691 // Set information for creating textures.
692 TilingInfo info(verifiedWidth, maxTextureSize, textPixelFormat);
694 // Get the buffer of text.
695 Dali::DevelPixelData::PixelDataBuffer textPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(data);
696 info.textBuffer = textPixelData.buffer;
700 // Create RGBA texture for all the text styles (without the text itself)
701 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
702 Dali::DevelPixelData::PixelDataBuffer stylePixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(styleData);
703 info.styleBuffer = stylePixelData.buffer;
706 if(containsColorGlyph && !hasMultipleTextColors)
708 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
709 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
710 Dali::DevelPixelData::PixelDataBuffer maskPixelData = Dali::DevelPixelData::ReleasePixelDataBuffer(maskData);
711 info.maskBuffer = maskPixelData.buffer;
714 // Get the current offset for recalculate the offset when tiling.
715 Property::Map retMap;
716 mImpl->mTransform.GetPropertyMap(retMap);
717 Property::Value* offsetValue = retMap.Find(Dali::Toolkit::Visual::Transform::Property::OFFSET);
720 offsetValue->Get(info.offSet);
723 // Create a textureset in the default renderer.
724 CreateTextureSet(info, mImpl->mRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
726 verifiedHeight -= maxTextureSize;
728 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
730 int offsetPosition = verifiedWidth * maxTextureSize;
731 // Create a renderer by cutting maxTextureSize.
732 while(verifiedHeight > 0)
734 Renderer tilingRenderer = Renderer::New(geometry, shader);
735 tilingRenderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, Toolkit::DepthIndex::CONTENT);
736 // New offset position of buffer for tiling.
737 info.offsetPosition += offsetPosition;
738 // New height for tiling.
739 info.height = (verifiedHeight - maxTextureSize) > 0 ? maxTextureSize : verifiedHeight;
740 // New offset for tiling.
741 info.offSet.y += maxTextureSize;
742 // Create a textureset int the new tiling renderer.
743 CreateTextureSet(info, tilingRenderer, sampler, hasMultipleTextColors, containsColorGlyph, styleEnabled, isOverlayStyle);
745 verifiedHeight -= maxTextureSize;
749 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
751 for(RendererContainer::iterator iter = mRendererList.begin(); iter != mRendererList.end(); ++iter)
753 Renderer renderer = (*iter);
756 actor.AddRenderer(renderer);
761 TextureSet TextVisual::GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle)
763 // Filter mode needs to be set to linear to produce better quality while scaling.
764 Sampler sampler = Sampler::New();
765 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
767 TextureSet textureSet = TextureSet::New();
769 // Create RGBA texture if the text contains emojis or multiple text colors, otherwise L8 texture
770 Pixel::Format textPixelFormat = (containsColorGlyph || hasMultipleTextColors) ? Pixel::RGBA8888 : Pixel::L8;
772 // Check the text direction
773 Toolkit::DevelText::TextDirection::Type textDirection = mController->GetTextDirection();
775 // Create a texture for the text without any styles
776 PixelData data = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_STYLES, false, textPixelFormat);
778 // It may happen the image atlas can't handle a pixel data it exceeds the maximum size.
779 // In that case, create a texture. TODO: should tile the text.
780 unsigned int textureSetIndex = 0u;
781 AddTexture(textureSet, data, sampler, textureSetIndex);
786 // Create RGBA texture for all the text styles that render in the background (without the text itself)
787 PixelData styleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_NO_TEXT, false, Pixel::RGBA8888);
788 AddTexture(textureSet, styleData, sampler, textureSetIndex);
790 // Create RGBA texture for overlay styles such as underline and strikethrough (without the text itself)
791 PixelData overlayStyleData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_OVERLAY_STYLE, false, Pixel::RGBA8888);
792 AddTexture(textureSet, overlayStyleData, sampler, textureSetIndex);
796 if(containsColorGlyph && !hasMultipleTextColors)
798 // Create a L8 texture as a mask to avoid color glyphs (e.g. emojis) to be affected by text color animation
799 PixelData maskData = mTypesetter->Render(size, textDirection, Text::Typesetter::RENDER_MASK, false, Pixel::L8);
801 AddTexture(textureSet, maskData, sampler, textureSetIndex);
807 Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled)
811 if(hasMultipleTextColors && !styleEnabled)
813 // We don't animate text color if the text contains multiple colors
814 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT);
817 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_SHADER_FRAG);
818 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
819 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT, shader);
822 else if(hasMultipleTextColors && styleEnabled)
824 // We don't animate text color if the text contains multiple colors
825 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE);
828 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_MULTI_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
829 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
830 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE, shader);
833 else if(!hasMultipleTextColors && !containsColorGlyph && !styleEnabled)
835 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT);
838 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_SHADER_FRAG);
839 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
840 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT, shader);
843 else if(!hasMultipleTextColors && !containsColorGlyph && styleEnabled)
845 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE);
848 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_SHADER_FRAG);
849 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
850 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE, shader);
853 else if(!hasMultipleTextColors && containsColorGlyph && !styleEnabled)
855 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI);
858 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_EMOJI_SHADER_FRAG);
859 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
860 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI, shader);
863 else // if( !hasMultipleTextColors && containsColorGlyph && styleEnabled )
865 shader = factoryCache.GetShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI);
868 shader = Shader::New(SHADER_TEXT_VISUAL_SHADER_VERT, SHADER_TEXT_VISUAL_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI_SHADER_FRAG);
869 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
870 factoryCache.SaveShader(VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI, shader);
877 } // namespace Internal
879 } // namespace Toolkit