Fix key event propagation in text controller
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / color-segmentation.cpp
1 /*
2  * Copyright (c) 2015 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 // FILE HEADER
19 #include <dali-toolkit/internal/text/color-segmentation.h>
20
21 // EXTERNAL INCLUDES
22
23 // INTERNAL INCLUDES
24
25 #include <iostream>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Text
32 {
33 /**
34  * @brief Finds a color in the vector of colors.
35  *        It inserts the color in the vector if it's not in.
36  *
37  * @param[in,out] colors The vector of colors.
38  * @param[in] color The color to find.
39  *
40  * @return The index + 1 where the color is in the vector. The index zero is reserved for the default color.
41  */
42 ColorIndex FindColor(Vector<Vector4>& colors,
43                      const Vector4&   color)
44 {
45   ColorIndex index = 1u; // The index zero is reserved for the default color.
46   for(Vector<Vector4>::Iterator it    = colors.Begin(),
47                                 endIt = colors.End();
48       it != endIt;
49       ++it)
50   {
51     if(color == *it)
52     {
53       return index;
54     }
55
56     ++index;
57   }
58
59   colors.PushBack(color);
60
61   return index;
62 }
63
64 void SetColorSegmentationInfo(const Vector<ColorRun>&   colorRuns,
65                               const Vector<GlyphIndex>& charactersToGlyph,
66                               const Vector<Length>&     glyphsPerCharacter,
67                               CharacterIndex            startCharacterIndex,
68                               GlyphIndex                startGlyphIndex,
69                               Length                    numberOfCharacters,
70                               Vector<Vector4>&          colors,
71                               Vector<ColorIndex>&       colorIndices)
72 {
73   if(0u == charactersToGlyph.Count())
74   {
75     // Nothing to do if there is no text.
76     return;
77   }
78
79   // Get pointers to the buffers.
80   const GlyphIndex* const charactersToGlyphBuffer  = charactersToGlyph.Begin();
81   const Length* const     glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
82
83   // Calculate the number of glyphs to insert.
84   const CharacterIndex lastCharacterIndex = startCharacterIndex + numberOfCharacters - 1u;
85   const Length         numberOfNewGlyphs  = *(charactersToGlyphBuffer + lastCharacterIndex) + *(glyphsPerCharacterBuffer + lastCharacterIndex) - *(charactersToGlyphBuffer + startCharacterIndex);
86
87   // Reserve space for the new color indices.
88   Vector<ColorIndex> newColorIndices;
89   newColorIndices.Resize(numberOfNewGlyphs);
90
91   ColorIndex* newColorIndicesBuffer = newColorIndices.Begin();
92
93   // Convert from characters to glyphs.
94   Length index = 0u;
95   for(Vector<ColorRun>::ConstIterator it    = colorRuns.Begin(),
96                                       endIt = colorRuns.End();
97       it != endIt;
98       ++it, ++index)
99   {
100     const ColorRun& colorRun = *it;
101
102     if((startCharacterIndex < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters) &&
103        (colorRun.characterRun.characterIndex < startCharacterIndex + numberOfCharacters))
104     {
105       if(0u < colorRun.characterRun.numberOfCharacters)
106       {
107         // Find the color index.
108         const ColorIndex colorIndex = FindColor(colors, colorRun.color);
109
110         // Get the index to the last character of the run.
111         const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u;
112
113         const GlyphIndex glyphIndex = std::max(startGlyphIndex, *(charactersToGlyphBuffer + colorRun.characterRun.characterIndex)) - startGlyphIndex;
114         // Get the number of glyphs of the run.
115         const Length lastGlyphIndexPlusOne = std::min(numberOfNewGlyphs, *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex);
116
117         // Set the indices.
118         for(GlyphIndex i = glyphIndex; i < lastGlyphIndexPlusOne; ++i)
119         {
120           *(newColorIndicesBuffer + i) = colorIndex;
121         }
122       }
123     }
124   }
125
126   // Insert the new indices.
127   colorIndices.Insert(colorIndices.Begin() + startGlyphIndex,
128                       newColorIndices.Begin(),
129                       newColorIndices.End());
130 }
131
132 } // namespace Text
133
134 } // namespace Toolkit
135
136 } // namespace Dali