[dali_2.1.7] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-background-actor.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // HEADER
19 #include <dali-toolkit/internal/text/text-controller-background-actor.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/rendering/renderer.h>
23
24 // INTERNAL INCLUDES
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>
29
30 namespace Dali::Toolkit::Text
31 {
32 namespace
33 {
34 struct BackgroundVertex
35 {
36   Vector2 mPosition; ///< Vertex posiiton
37   Vector4 mColor;    ///< Vertex color
38 };
39
40 struct BackgroundMesh
41 {
42   Vector<BackgroundVertex> mVertices; ///< container of vertices
43   Vector<unsigned short>   mIndices;  ///< container of indices
44 };
45 } // unnamed namespace
46
47 Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr& textVisualModel, const LogicalModelPtr& textLogicalModel, Shader& textShaderBackground)
48 {
49   // NOTE: Currently we only support background color for left-to-right text.
50
51   Actor actor;
52
53   Length numberOfGlyphs = textView.GetNumberOfGlyphs();
54   if(numberOfGlyphs > 0u)
55   {
56     Vector<GlyphInfo> glyphs;
57     glyphs.Resize(numberOfGlyphs);
58
59     Vector<Vector2> positions;
60     positions.Resize(numberOfGlyphs);
61
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(),
66                                         positions.Begin(),
67                                         alignmentOffset,
68                                         0u,
69                                         numberOfGlyphs);
70
71     glyphs.Resize(numberOfGlyphs);
72     positions.Resize(numberOfGlyphs);
73
74     const GlyphInfo* const glyphsBuffer    = glyphs.Begin();
75     const Vector2* const   positionsBuffer = positions.Begin();
76
77     BackgroundMesh mesh;
78     mesh.mVertices.Reserve(4u * glyphs.Size());
79     mesh.mIndices.Reserve(6u * glyphs.Size());
80
81     const Vector2 textSize = textView.GetLayoutSize();
82
83     const float offsetX = alignmentOffset + textSize.width * 0.5f;
84     const float offsetY = textSize.height * 0.5f;
85
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;
93
94     Vector4   quad;
95     uint32_t  numberOfQuads = 0u;
96     Length    yLineOffset   = 0;
97     Length    prevLineIndex = 0;
98     LineIndex lineIndex;
99     Length    numberOfLines;
100
101     for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
102     {
103       const GlyphInfo& glyph = *(glyphsBuffer + i);
104
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);
111
112       textVisualModel->GetNumberOfLines(i, 1, lineIndex, numberOfLines);
113       Length lineHeight = lineRun[lineIndex].ascender + -(lineRun[lineIndex].descender) + lineRun[lineIndex].lineSpacing;
114
115       if(lineIndex != prevLineIndex)
116       {
117         yLineOffset += lineHeight;
118       }
119
120       // Only create quads for glyphs with a background color
121       if(backgroundColor != Color::TRANSPARENT)
122       {
123         const Vector2 position = *(positionsBuffer + i);
124         calculatedAdvance      = GetCalculatedAdvance(*(textLogicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + i))), characterSpacing, glyph.advance);
125
126         if(i == 0u && glyphSize == 1u) // Only one glyph in the whole text
127         {
128           quad.x = position.x;
129           quad.y = yLineOffset;
130           quad.z = quad.x + std::max(calculatedAdvance, glyph.xBearing + glyph.width);
131           quad.w = lineHeight;
132         }
133         else if((lineIndex != prevLineIndex) || (i == 0u)) // The first glyph in the line
134         {
135           quad.x = position.x;
136           quad.y = yLineOffset;
137           quad.z = quad.x - glyph.xBearing + calculatedAdvance;
138           quad.w = quad.y + lineHeight;
139         }
140         else if(i == glyphSize - 1u) // The last glyph in the whole text
141         {
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;
146         }
147         else // The glyph in the middle of the text
148         {
149           quad.x = position.x - glyph.xBearing;
150           quad.y = yLineOffset;
151           quad.z = quad.x + calculatedAdvance;
152           quad.w = quad.y + lineHeight;
153         }
154
155         BackgroundVertex vertex;
156
157         // Top left
158         vertex.mPosition.x = quad.x - offsetX;
159         vertex.mPosition.y = quad.y - offsetY;
160         vertex.mColor      = backgroundColor;
161         mesh.mVertices.PushBack(vertex);
162
163         // Top right
164         vertex.mPosition.x = quad.z - offsetX;
165         vertex.mPosition.y = quad.y - offsetY;
166         vertex.mColor      = backgroundColor;
167         mesh.mVertices.PushBack(vertex);
168
169         // Bottom left
170         vertex.mPosition.x = quad.x - offsetX;
171         vertex.mPosition.y = quad.w - offsetY;
172         vertex.mColor      = backgroundColor;
173         mesh.mVertices.PushBack(vertex);
174
175         // Bottom right
176         vertex.mPosition.x = quad.z - offsetX;
177         vertex.mPosition.y = quad.w - offsetY;
178         vertex.mColor      = backgroundColor;
179         mesh.mVertices.PushBack(vertex);
180
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);
188
189         numberOfQuads++;
190       }
191
192       if(lineIndex != prevLineIndex)
193       {
194         prevLineIndex = lineIndex;
195       }
196     }
197
198     // Only create the background actor if there are glyphs with background color
199     if(mesh.mVertices.Count() > 0u)
200     {
201       Property::Map quadVertexFormat;
202       quadVertexFormat["aPosition"] = Property::VECTOR2;
203       quadVertexFormat["aColor"]    = Property::VECTOR4;
204
205       VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
206       quadVertices.SetData(&mesh.mVertices[0], mesh.mVertices.Size());
207
208       Geometry quadGeometry = Geometry::New();
209       quadGeometry.AddVertexBuffer(quadVertices);
210       quadGeometry.SetIndexBuffer(&mesh.mIndices[0], mesh.mIndices.Size());
211
212       if(!textShaderBackground)
213       {
214         textShaderBackground = Shader::New(SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_VERT, SHADER_TEXT_CONTROLLER_BACKGROUND_SHADER_FRAG);
215       }
216
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);
220
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);
228     }
229   }
230
231   return actor;
232 }
233
234 } // namespace Dali::Toolkit::Text