X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Fcolor-segmentation.cpp;h=9476480c2b7ac88f9d41e2c235b74fbf58e0d489;hp=91692258534598a36137de4df0b88a10e7bf4530;hb=eb86a7e2a223a3876fa8be052872df527aad777f;hpb=213195730a51b211653cb6ad460b23a6e363b779 diff --git a/dali-toolkit/internal/text/color-segmentation.cpp b/dali-toolkit/internal/text/color-segmentation.cpp index 9169225..9476480 100644 --- a/dali-toolkit/internal/text/color-segmentation.cpp +++ b/dali-toolkit/internal/text/color-segmentation.cpp @@ -26,59 +26,107 @@ namespace Dali { - namespace Toolkit { - namespace Text { - -void SetColorSegmentationInfo( const Vector& characterColorRuns, - const Vector& charactersToGlyph, - const Vector& glyphsPerCharacter, - Vector& glyphColorRuns ) +/** + * @brief Finds a color in the vector of colors. + * It inserts the color in the vector if it's not in. + * + * @param[in,out] colors The vector of colors. + * @param[in] color The color to find. + * + * @return The index + 1 where the color is in the vector. The index zero is reserved for the default color. + */ +ColorIndex FindColor(Vector& colors, + const Vector4& color) { - const VectorBase::SizeType numberOfColorRuns = characterColorRuns.Count(); + ColorIndex index = 1u; // The index zero is reserved for the default color. + for(Vector::Iterator it = colors.Begin(), + endIt = colors.End(); + it != endIt; + ++it) + { + if(color == *it) + { + return index; + } + + ++index; + } + + colors.PushBack(color); - if( 0u == numberOfColorRuns ) + return index; +} + +void SetColorSegmentationInfo(const Vector& colorRuns, + const Vector& charactersToGlyph, + const Vector& glyphsPerCharacter, + CharacterIndex startCharacterIndex, + GlyphIndex startGlyphIndex, + Length numberOfCharacters, + Vector& colors, + Vector& colorIndices) +{ + if(0u == charactersToGlyph.Count()) { - // Nothing to do. + // Nothing to do if there is no text. return; } - // Resize the color runs for the glyphs. - glyphColorRuns.Resize( numberOfColorRuns ); - // Get pointers to the buffers. - ColorGlyphRun* glyphColorRunsBuffer = glyphColorRuns.Begin(); - const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin(); - const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin(); + const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin(); + const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin(); + + // Calculate the number of glyphs to insert. + const CharacterIndex lastCharacterIndex = startCharacterIndex + numberOfCharacters - 1u; + const Length numberOfNewGlyphs = *(charactersToGlyphBuffer + lastCharacterIndex) + *(glyphsPerCharacterBuffer + lastCharacterIndex) - *(charactersToGlyphBuffer + startCharacterIndex); + + // Reserve space for the new color indices. + Vector newColorIndices; + newColorIndices.Resize(numberOfNewGlyphs); + + ColorIndex* newColorIndicesBuffer = newColorIndices.Begin(); // Convert from characters to glyphs. Length index = 0u; - for( Vector::ConstIterator it = characterColorRuns.Begin(), - endIt = characterColorRuns.End(); - it != endIt; - ++it, ++index ) + for(Vector::ConstIterator it = colorRuns.Begin(), + endIt = colorRuns.End(); + it != endIt; + ++it, ++index) { const ColorRun& colorRun = *it; - if( 0u < colorRun.characterRun.numberOfCharacters ) + if((startCharacterIndex < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters) && + (colorRun.characterRun.characterIndex < startCharacterIndex + numberOfCharacters)) { - // Get the next color glyph run. - ColorGlyphRun& colorGlyphRun = *( glyphColorRunsBuffer + index ); - colorGlyphRun.color = colorRun.color; - - // Convert the color run index from character to glyph. - colorGlyphRun.glyphRun.glyphIndex = *( charactersToGlyphBuffer + colorRun.characterRun.characterIndex ); - - // Get the index to the last character of the run. - const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u; - - // Calculate the number of glyphs. - colorGlyphRun.glyphRun.numberOfGlyphs = *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - colorGlyphRun.glyphRun.glyphIndex; + if(0u < colorRun.characterRun.numberOfCharacters) + { + // Find the color index. + const ColorIndex colorIndex = FindColor(colors, colorRun.color); + + // Get the index to the last character of the run. + const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u; + + const GlyphIndex glyphIndex = std::max(startGlyphIndex, *(charactersToGlyphBuffer + colorRun.characterRun.characterIndex)) - startGlyphIndex; + // Get the number of glyphs of the run. + const Length lastGlyphIndexPlusOne = std::min(numberOfNewGlyphs, *(charactersToGlyphBuffer + lastIndex) + *(glyphsPerCharacterBuffer + lastIndex) - startGlyphIndex); + + // Set the indices. + for(GlyphIndex i = glyphIndex; i < lastGlyphIndexPlusOne; ++i) + { + *(newColorIndicesBuffer + i) = colorIndex; + } + } } } + + // Insert the new indices. + colorIndices.Insert(colorIndices.Begin() + startGlyphIndex, + newColorIndices.Begin(), + newColorIndices.End()); } } // namespace Text