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/text/text-controller-background-actor.h>
22 #include <dali/public-api/rendering/renderer.h>
25 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
26 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
27 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
28 #include <dali-toolkit/internal/text/text-view.h>
30 namespace Dali::Toolkit::Text
34 struct BackgroundVertex
36 Vector2 mPosition; ///< Vertex posiiton
37 Vector4 mColor; ///< Vertex color
42 Vector<BackgroundVertex> mVertices; ///< container of vertices
43 Vector<unsigned short> mIndices; ///< container of indices
45 } // unnamed namespace
47 Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr& textVisualModel, const LogicalModelPtr& textLogicalModel, Shader& textShaderBackground)
49 // NOTE: Currently we only support background color for left-to-right text.
53 Length numberOfGlyphs = textView.GetNumberOfGlyphs();
54 if(numberOfGlyphs > 0u)
56 Vector<GlyphInfo> glyphs;
57 glyphs.Resize(numberOfGlyphs);
59 Vector<Vector2> positions;
60 positions.Resize(numberOfGlyphs);
62 // Get the line where the glyphs are laid-out.
63 const LineRun* lineRun = textVisualModel->mLines.Begin();
64 float alignmentOffset = lineRun->alignmentOffset;
65 numberOfGlyphs = textView.GetGlyphs(glyphs.Begin(),
71 glyphs.Resize(numberOfGlyphs);
72 positions.Resize(numberOfGlyphs);
74 const GlyphInfo* const glyphsBuffer = glyphs.Begin();
75 const Vector2* const positionsBuffer = positions.Begin();
78 mesh.mVertices.Reserve(4u * glyphs.Size());
79 mesh.mIndices.Reserve(6u * glyphs.Size());
81 const Vector2 textSize = textView.GetLayoutSize();
83 const float offsetX = alignmentOffset + textSize.width * 0.5f;
84 const float offsetY = textSize.height * 0.5f;
86 const Vector4* const backgroundColorsBuffer = textView.GetBackgroundColors();
87 const ColorIndex* const backgroundColorIndicesBuffer = textView.GetBackgroundColorIndices();
88 const Vector4& defaultBackgroundColor = textVisualModel->IsBackgroundEnabled() ? textVisualModel->GetBackgroundColor() : Color::TRANSPARENT;
89 const float characterSpacing = textVisualModel->GetCharacterSpacing();
90 Vector<CharacterIndex>& glyphToCharacterMap = textVisualModel->mGlyphsToCharacters;
91 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
92 float calculatedAdvance = 0.f;
95 uint32_t numberOfQuads = 0u;
96 Length yLineOffset = 0;
97 Length prevLineIndex = 0;
101 for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
103 const GlyphInfo& glyph = *(glyphsBuffer + i);
105 // Get the background color of the character.
106 // The color index zero is reserved for the default background color (i.e. Color::TRANSPARENT)
107 const bool isMarkupBackground = textView.IsMarkupBackgroundColorSet();
108 const ColorIndex backgroundColorIndex = isMarkupBackground ? *(backgroundColorIndicesBuffer + i) : 0u;
109 const bool isDefaultBackgroundColor = (0u == backgroundColorIndex);
110 const Vector4& backgroundColor = isDefaultBackgroundColor ? defaultBackgroundColor : *(backgroundColorsBuffer + backgroundColorIndex - 1u);
112 textVisualModel->GetNumberOfLines(i, 1, lineIndex, numberOfLines);
113 Length lineHeight = lineRun[lineIndex].ascender + -(lineRun[lineIndex].descender) + lineRun[lineIndex].lineSpacing;
115 if(lineIndex != prevLineIndex)
117 yLineOffset += lineHeight;
120 // Only create quads for glyphs with a background color
121 if(backgroundColor != Color::TRANSPARENT)
123 const Vector2 position = *(positionsBuffer + i);
124 calculatedAdvance = GetCalculatedAdvance(*(textLogicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + i))), characterSpacing, glyph.advance);
126 if(i == 0u && glyphSize == 1u) // Only one glyph in the whole text
129 quad.y = yLineOffset;
130 quad.z = quad.x + std::max(calculatedAdvance, glyph.xBearing + glyph.width);
133 else if((lineIndex != prevLineIndex) || (i == 0u)) // The first glyph in the line
136 quad.y = yLineOffset;
137 quad.z = quad.x - glyph.xBearing + calculatedAdvance;
138 quad.w = quad.y + lineHeight;
140 else if(i == glyphSize - 1u) // The last glyph in the whole text
142 quad.x = position.x - glyph.xBearing;
143 quad.y = yLineOffset;
144 quad.z = quad.x + std::max(calculatedAdvance, glyph.xBearing + glyph.width);
145 quad.w = quad.y + lineHeight;
147 else // The glyph in the middle of the text
149 quad.x = position.x - glyph.xBearing;
150 quad.y = yLineOffset;
151 quad.z = quad.x + calculatedAdvance;
152 quad.w = quad.y + lineHeight;
155 BackgroundVertex vertex;
158 vertex.mPosition.x = quad.x - offsetX;
159 vertex.mPosition.y = quad.y - offsetY;
160 vertex.mColor = backgroundColor;
161 mesh.mVertices.PushBack(vertex);
164 vertex.mPosition.x = quad.z - offsetX;
165 vertex.mPosition.y = quad.y - offsetY;
166 vertex.mColor = backgroundColor;
167 mesh.mVertices.PushBack(vertex);
170 vertex.mPosition.x = quad.x - offsetX;
171 vertex.mPosition.y = quad.w - offsetY;
172 vertex.mColor = backgroundColor;
173 mesh.mVertices.PushBack(vertex);
176 vertex.mPosition.x = quad.z - offsetX;
177 vertex.mPosition.y = quad.w - offsetY;
178 vertex.mColor = backgroundColor;
179 mesh.mVertices.PushBack(vertex);
181 // Six indices in counter clockwise winding
182 mesh.mIndices.PushBack(1u + 4 * numberOfQuads);
183 mesh.mIndices.PushBack(0u + 4 * numberOfQuads);
184 mesh.mIndices.PushBack(2u + 4 * numberOfQuads);
185 mesh.mIndices.PushBack(2u + 4 * numberOfQuads);
186 mesh.mIndices.PushBack(3u + 4 * numberOfQuads);
187 mesh.mIndices.PushBack(1u + 4 * numberOfQuads);
192 if(lineIndex != prevLineIndex)
194 prevLineIndex = lineIndex;
198 // Only create the background actor if there are glyphs with background color
199 if(mesh.mVertices.Count() > 0u)
201 Property::Map quadVertexFormat;
202 quadVertexFormat["aPosition"] = Property::VECTOR2;
203 quadVertexFormat["aColor"] = Property::VECTOR4;
205 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
206 quadVertices.SetData(&mesh.mVertices[0], mesh.mVertices.Size());
208 Geometry quadGeometry = Geometry::New();
209 quadGeometry.AddVertexBuffer(quadVertices);
210 quadGeometry.SetIndexBuffer(&mesh.mIndices[0], mesh.mIndices.Size());
212 if(!textShaderBackground)
214 textShaderBackground = Shader::New(SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_VERT, SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_FRAG);
217 Dali::Renderer renderer = Dali::Renderer::New(quadGeometry, textShaderBackground);
218 renderer.SetProperty(Dali::Renderer::Property::BLEND_MODE, BlendMode::ON);
219 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT);
221 actor = Actor::New();
222 actor.SetProperty(Dali::Actor::Property::NAME, "TextBackgroundColorActor");
223 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
224 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
225 actor.SetProperty(Actor::Property::SIZE, textSize);
226 actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
227 actor.AddRenderer(renderer);
234 } // namespace Dali::Toolkit::Text