1 #ifndef __DALI_TOOLKIT_TEXT_STYLE_RUN_CONTAINER_H__
2 #define __DALI_TOOLKIT_TEXT_STYLE_RUN_CONTAINER_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali-toolkit/internal/text/character-run.h>
34 * @brief Updates the number of characters and the character index of the text's style runs.
36 * If the @p numberOfCharacters is a negative value, it means the number of characters that are removed starting from the @p index.
38 * It deletes runs if all their characters are removed.
40 * @param[in] index Index to the first character updated.
41 * @param[in] numberOfCharacters The number of characters to be updated.
42 * @param[in] totalNumberOfCharacters Total number of characters of the text.
43 * @param[in,out] runs The text's style runs.
44 * @param[out] removedRuns The text's style removed runs.
46 template< typename T >
47 void UpdateCharacterRuns( CharacterIndex index,
48 int numberOfCharacters,
49 Length totalNumberOfCharacters,
51 Vector<T>& removedRuns )
53 if( 0 > numberOfCharacters )
56 const Length numberOfRemovedCharacters = -numberOfCharacters;
58 if( ( 0u == index ) && ( numberOfRemovedCharacters == totalNumberOfCharacters ) )
60 // Set the removed runs.
63 // All characters are removed.
66 // Nothing else to do.
70 const VectorBase::SizeType size = runs.Count();
71 // Temporary vector used to remove runs.
73 // Reserve some space for the temporary vector.
74 tempRuns.Reserve( size );
75 removedRuns.Reserve( size );
77 // Whether any run has to be removed.
78 bool runsRemoved = false;
80 // Index to the last character added/removed.
81 const CharacterIndex lastIndex = index + numberOfRemovedCharacters - 1u;
83 // Update the style runs
84 for( typename Vector<T>::Iterator it = runs.Begin(),
91 const CharacterIndex lastRunIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
93 if( lastRunIndex < index )
95 // The style run is not affected by the removed text.
96 tempRuns.PushBack( run );
100 if( ( index <= run.characterRun.characterIndex ) &&
101 ( lastIndex >= lastRunIndex ) )
103 // Add the removed run into the vector.
104 removedRuns.PushBack( run );
106 // All the characters are removed.
111 if( lastIndex < run.characterRun.characterIndex )
113 // Just move the character index.
114 run.characterRun.characterIndex -= numberOfRemovedCharacters;
118 if( run.characterRun.characterIndex < index )
120 // Remove characters starting from a character within the run.
121 run.characterRun.numberOfCharacters -= std::min( numberOfRemovedCharacters, 1u + lastRunIndex - index );
125 // Remove characters starting from a character located before the first index of the run.
126 run.characterRun.numberOfCharacters -= 1u + lastIndex - run.characterRun.characterIndex;
127 run.characterRun.characterIndex = index;
131 tempRuns.PushBack( run );
135 // Copy the temporary vector if there are runs removed.
145 // Update the style runs
146 for( typename Vector<T>::Iterator it = runs.Begin(),
153 // Update the number of characters of the style run.
155 if( ( 0u == index ) && ( 0u == run.characterRun.characterIndex ) )
157 run.characterRun.numberOfCharacters += numberOfCharacters;
159 else if( index <= run.characterRun.characterIndex )
161 run.characterRun.characterIndex += numberOfCharacters;
163 else if( index <= run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
165 run.characterRun.numberOfCharacters += numberOfCharacters;
173 } // namespace Toolkit
177 #endif // __DALI_TOOLKIT_TEXT_STYLE_RUN_CONTAINER_H__