2 * Copyright (c) 2015 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/color-segmentation.h>
34 * @brief Finds a color in the vector of colors.
35 * It inserts the color in the vector if it's not in.
37 * @param[in,out] colors The vector of colors.
38 * @param[in] color The color to find.
40 * @return The index + 1 where the color is in the vector. The index zero is reserved for the default color.
42 ColorIndex FindColor(Vector<Vector4>& colors,
45 ColorIndex index = 1u; // The index zero is reserved for the default color.
46 for(Vector<Vector4>::Iterator it = colors.Begin(),
59 colors.PushBack(color);
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)
73 if(0u == charactersToGlyph.Count())
75 // Nothing to do if there is no text.
79 // Get pointers to the buffers.
80 const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
81 const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
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);
87 // Reserve space for the new color indices.
88 Vector<ColorIndex> newColorIndices;
89 newColorIndices.Resize(numberOfNewGlyphs);
91 ColorIndex* newColorIndicesBuffer = newColorIndices.Begin();
93 // Convert from characters to glyphs.
95 for(Vector<ColorRun>::ConstIterator it = colorRuns.Begin(),
96 endIt = colorRuns.End();
100 const ColorRun& colorRun = *it;
102 if((startCharacterIndex < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters) &&
103 (colorRun.characterRun.characterIndex < startCharacterIndex + numberOfCharacters))
105 if(0u < colorRun.characterRun.numberOfCharacters)
107 // Find the color index.
108 const ColorIndex colorIndex = FindColor(colors, colorRun.color);
110 // Get the index to the last character of the run.
111 const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u;
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);
118 for(GlyphIndex i = glyphIndex; i < lastGlyphIndexPlusOne; ++i)
120 *(newColorIndicesBuffer + i) = colorIndex;
126 // Insert the new indices.
127 colorIndices.Insert(colorIndices.Begin() + startGlyphIndex,
128 newColorIndices.Begin(),
129 newColorIndices.End());
134 } // namespace Toolkit