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 Clears the runs starting from the given character index.
36 * @param[in] startIndex The starting character index used to remove runs.
37 * @param[in] endIndex The ending character index used to remove runs.
38 * @param[in,out] runs The text's runs.
39 * @param[out] startRemoveIndex The index to the first run to be removed.
40 * @param[out] endRemoveIndex The index to the last run to be removed.
42 template< typename T >
43 void ClearCharacterRuns( CharacterIndex startIndex,
44 CharacterIndex endIndex,
46 uint32_t& startRemoveIndex,
47 uint32_t& endRemoveIndex )
49 T* runsBuffer = runs.Begin();
51 const Length length = runs.Count();
52 for( Length index = 0u; index < length; ++index )
54 T* run = ( runsBuffer + index );
56 if( ( run->characterRun.characterIndex <= endIndex ) &&
57 ( startIndex < run->characterRun.characterIndex + run->characterRun.numberOfCharacters ) )
61 // Set the index to the first run to be removed.
62 startRemoveIndex = index;
67 for( Length index = startRemoveIndex; index < length; ++index )
69 T* run = ( runsBuffer + index );
71 if( ( run->characterRun.characterIndex <= endIndex ) &&
72 ( startIndex < run->characterRun.characterIndex + run->characterRun.numberOfCharacters ) )
74 // Update the index to the last run to be removed.
75 endRemoveIndex = index + 1u;
79 // Run found. Nothing else to do.
84 // The number of characters to remove.
85 const Length numberOfCharactersRemoved = 1u + endIndex - startIndex;
87 // Update the character index of the next runs.
88 for( Length index = 0u; index < length; ++index )
90 T* run = ( runsBuffer + index );
92 if( run->characterRun.characterIndex > startIndex )
94 run->characterRun.characterIndex -= numberOfCharactersRemoved;
100 * @brief Clears the runs starting from the given character index.
102 * @param[in] startIndex The starting character index used to remove runs.
103 * @param[in] endIndex The ending character index used to remove runs.
104 * @param[in,out] runs The text's runs.
106 template< typename T >
107 void ClearCharacterRuns( CharacterIndex startIndex,
108 CharacterIndex endIndex,
111 uint32_t startRemoveIndex = runs.Count();
112 uint32_t endRemoveIndex = startRemoveIndex;
113 ClearCharacterRuns( startIndex,
119 // Remove all remaining runs.
120 T* runBuffer = runs.Begin();
121 runs.Erase( runBuffer + startRemoveIndex, runBuffer + endRemoveIndex );
125 * @brief Updates the number of characters and the character index of the text's style runs.
127 * If the @p numberOfCharacters is a negative value, it means the number of characters that are removed starting from the @p index.
129 * It deletes runs if all their characters are removed.
131 * @param[in] index Index to the first character updated.
132 * @param[in] numberOfCharacters The number of characters to be updated.
133 * @param[in] totalNumberOfCharacters Total number of characters of the text.
134 * @param[in,out] runs The text's style runs.
135 * @param[out] removedRuns The text's style removed runs.
137 template< typename T >
138 void UpdateCharacterRuns( CharacterIndex index,
139 int numberOfCharacters,
140 Length totalNumberOfCharacters,
142 Vector<T>& removedRuns )
144 if( 0 > numberOfCharacters )
146 // Remove characters.
147 const Length numberOfRemovedCharacters = -numberOfCharacters;
149 if( ( 0u == index ) && ( numberOfRemovedCharacters == totalNumberOfCharacters ) )
151 // Set the removed runs.
154 // All characters are removed.
157 // Nothing else to do.
161 const VectorBase::SizeType size = runs.Count();
162 // Temporary vector used to remove runs.
164 // Reserve some space for the temporary vector.
165 tempRuns.Reserve( size );
166 removedRuns.Reserve( size );
168 // Whether any run has to be removed.
169 bool runsRemoved = false;
171 // Index to the last character added/removed.
172 const CharacterIndex lastIndex = index + numberOfRemovedCharacters - 1u;
174 // Update the style runs
175 for( typename Vector<T>::Iterator it = runs.Begin(),
182 const CharacterIndex lastRunIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
184 if( lastRunIndex < index )
186 // The style run is not affected by the removed text.
187 tempRuns.PushBack( run );
191 if( ( index <= run.characterRun.characterIndex ) &&
192 ( lastIndex >= lastRunIndex ) )
194 // Add the removed run into the vector.
195 removedRuns.PushBack( run );
197 // All the characters are removed.
202 if( lastIndex < run.characterRun.characterIndex )
204 // Just move the character index.
205 run.characterRun.characterIndex -= numberOfRemovedCharacters;
209 if( run.characterRun.characterIndex < index )
211 // Remove characters starting from a character within the run.
212 run.characterRun.numberOfCharacters -= std::min( numberOfRemovedCharacters, 1u + lastRunIndex - index );
216 // Remove characters starting from a character located before the first index of the run.
217 run.characterRun.numberOfCharacters -= 1u + lastIndex - run.characterRun.characterIndex;
218 run.characterRun.characterIndex = index;
222 tempRuns.PushBack( run );
226 // Copy the temporary vector if there are runs removed.
236 // Update the style runs
237 for( typename Vector<T>::Iterator it = runs.Begin(),
244 // Update the number of characters of the style run.
246 if( ( 0u == index ) && ( 0u == run.characterRun.characterIndex ) )
248 run.characterRun.numberOfCharacters += numberOfCharacters;
250 else if( index <= run.characterRun.characterIndex )
252 run.characterRun.characterIndex += numberOfCharacters;
254 else if( index <= run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
256 run.characterRun.numberOfCharacters += numberOfCharacters;
264 } // namespace Toolkit
268 #endif // __DALI_TOOLKIT_TEXT_STYLE_RUN_CONTAINER_H__