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/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/text-view.h>
29 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, 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;
91 uint32_t numberOfQuads = 0u;
92 Length yLineOffset = 0;
93 Length prevLineIndex = 0;
97 for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
99 const GlyphInfo& glyph = *(glyphsBuffer + i);
101 // Get the background color of the character.
102 // The color index zero is reserved for the default background color (i.e. Color::TRANSPARENT)
103 const bool isMarkupBackground = textView.IsMarkupBackgroundColorSet();
104 const ColorIndex backgroundColorIndex = isMarkupBackground ? *(backgroundColorIndicesBuffer + i) : 0u;
105 const bool isDefaultBackgroundColor = (0u == backgroundColorIndex);
106 const Vector4& backgroundColor = isDefaultBackgroundColor ? defaultBackgroundColor : *(backgroundColorsBuffer + backgroundColorIndex - 1u);
108 textVisualModel->GetNumberOfLines(i, 1, lineIndex, numberOfLines);
109 Length lineHeight = lineRun[lineIndex].ascender + -(lineRun[lineIndex].descender) + lineRun[lineIndex].lineSpacing;
111 if(lineIndex != prevLineIndex)
113 yLineOffset += lineHeight;
116 // Only create quads for glyphs with a background color
117 if(backgroundColor != Color::TRANSPARENT)
119 const Vector2 position = *(positionsBuffer + i);
121 if(i == 0u && glyphSize == 1u) // Only one glyph in the whole text
124 quad.y = yLineOffset;
125 quad.z = quad.x + std::max(glyph.advance, glyph.xBearing + glyph.width);
128 else if((lineIndex != prevLineIndex) || (i == 0u)) // The first glyph in the line
131 quad.y = yLineOffset;
132 quad.z = quad.x - glyph.xBearing + glyph.advance;
133 quad.w = quad.y + lineHeight;
135 else if(i == glyphSize - 1u) // The last glyph in the whole text
137 quad.x = position.x - glyph.xBearing;
138 quad.y = yLineOffset;
139 quad.z = quad.x + std::max(glyph.advance, glyph.xBearing + glyph.width);
140 quad.w = quad.y + lineHeight;
142 else // The glyph in the middle of the text
144 quad.x = position.x - glyph.xBearing;
145 quad.y = yLineOffset;
146 quad.z = quad.x + glyph.advance;
147 quad.w = quad.y + lineHeight;
150 BackgroundVertex vertex;
153 vertex.mPosition.x = quad.x - offsetX;
154 vertex.mPosition.y = quad.y - offsetY;
155 vertex.mColor = backgroundColor;
156 mesh.mVertices.PushBack(vertex);
159 vertex.mPosition.x = quad.z - offsetX;
160 vertex.mPosition.y = quad.y - offsetY;
161 vertex.mColor = backgroundColor;
162 mesh.mVertices.PushBack(vertex);
165 vertex.mPosition.x = quad.x - offsetX;
166 vertex.mPosition.y = quad.w - offsetY;
167 vertex.mColor = backgroundColor;
168 mesh.mVertices.PushBack(vertex);
171 vertex.mPosition.x = quad.z - offsetX;
172 vertex.mPosition.y = quad.w - offsetY;
173 vertex.mColor = backgroundColor;
174 mesh.mVertices.PushBack(vertex);
176 // Six indices in counter clockwise winding
177 mesh.mIndices.PushBack(1u + 4 * numberOfQuads);
178 mesh.mIndices.PushBack(0u + 4 * numberOfQuads);
179 mesh.mIndices.PushBack(2u + 4 * numberOfQuads);
180 mesh.mIndices.PushBack(2u + 4 * numberOfQuads);
181 mesh.mIndices.PushBack(3u + 4 * numberOfQuads);
182 mesh.mIndices.PushBack(1u + 4 * numberOfQuads);
187 if(lineIndex != prevLineIndex)
189 prevLineIndex = lineIndex;
193 // Only create the background actor if there are glyphs with background color
194 if(mesh.mVertices.Count() > 0u)
196 Property::Map quadVertexFormat;
197 quadVertexFormat["aPosition"] = Property::VECTOR2;
198 quadVertexFormat["aColor"] = Property::VECTOR4;
200 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
201 quadVertices.SetData(&mesh.mVertices[0], mesh.mVertices.Size());
203 Geometry quadGeometry = Geometry::New();
204 quadGeometry.AddVertexBuffer(quadVertices);
205 quadGeometry.SetIndexBuffer(&mesh.mIndices[0], mesh.mIndices.Size());
207 if(!textShaderBackground)
209 textShaderBackground = Shader::New(SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_VERT, SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_FRAG);
212 Dali::Renderer renderer = Dali::Renderer::New(quadGeometry, textShaderBackground);
213 renderer.SetProperty(Dali::Renderer::Property::BLEND_MODE, BlendMode::ON);
214 renderer.SetProperty(Dali::Renderer::Property::DEPTH_INDEX, DepthIndex::CONTENT);
216 actor = Actor::New();
217 actor.SetProperty(Dali::Actor::Property::NAME, "TextBackgroundColorActor");
218 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
219 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
220 actor.SetProperty(Actor::Property::SIZE, textSize);
221 actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
222 actor.AddRenderer(renderer);
229 } // namespace Dali::Toolkit::Text