1 #ifndef __DALI_TOOLKIT_TEXT_RUN_CONTAINER_H__
2 #define __DALI_TOOLKIT_TEXT_RUN_CONTAINER_H__
5 * Copyright (c) 2016 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();
52 const Length length = runs.Count();
54 for( index = 0u; index < length; ++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;
69 run = ( runsBuffer + startRemoveIndex );
70 for( index = startRemoveIndex; index < length; ++index )
72 if( ( run->characterRun.characterIndex > endIndex ) ||
73 ( startIndex >= run->characterRun.characterIndex + run->characterRun.numberOfCharacters ) )
75 // Run found. Nothing else to do.
80 endRemoveIndex = index;
82 // The number of characters to remove.
83 const Length numberOfCharactersRemoved = 1u + endIndex - startIndex;
85 // Update the character index of the next runs.
87 for( Length index = 0u; index < length; ++index )
89 if( run->characterRun.characterIndex > startIndex )
91 run->characterRun.characterIndex -= numberOfCharactersRemoved;
99 * @brief Clears the runs starting from the given character index.
101 * @param[in] startIndex The starting character index used to remove runs.
102 * @param[in] endIndex The ending character index used to remove runs.
103 * @param[in,out] runs The text's runs.
105 template< typename T >
106 void ClearCharacterRuns( CharacterIndex startIndex,
107 CharacterIndex endIndex,
110 uint32_t startRemoveIndex = runs.Count();
111 uint32_t endRemoveIndex = startRemoveIndex;
112 ClearCharacterRuns( startIndex,
118 // Remove all remaining runs.
119 T* runBuffer = runs.Begin();
120 runs.Erase( runBuffer + startRemoveIndex, runBuffer + endRemoveIndex );
124 * @brief Updates the number of characters and the character index of the text's style runs.
126 * If the @p numberOfCharacters is a negative value, it means the number of characters that are removed starting from the @p index.
128 * It deletes runs if all their characters are removed.
130 * @param[in] index Index to the first character updated.
131 * @param[in] numberOfCharacters The number of characters to be updated.
132 * @param[in] totalNumberOfCharacters Total number of characters of the text.
133 * @param[in,out] runs The text's style runs.
134 * @param[out] removedRuns The text's style removed runs.
136 template< typename T >
137 void UpdateCharacterRuns( CharacterIndex index,
138 int numberOfCharacters,
139 Length totalNumberOfCharacters,
141 Vector<T>& removedRuns )
143 if( 0 > numberOfCharacters )
145 // Remove characters.
146 const Length numberOfRemovedCharacters = -numberOfCharacters;
148 if( ( 0u == index ) && ( numberOfRemovedCharacters == totalNumberOfCharacters ) )
150 // Set the removed runs.
153 // All characters are removed.
156 // Nothing else to do.
160 const VectorBase::SizeType size = runs.Count();
161 // Temporary vector used to remove runs.
163 // Reserve some space for the temporary vector.
164 tempRuns.Reserve( size );
165 removedRuns.Reserve( size );
167 // Whether any run has to be removed.
168 bool runsRemoved = false;
170 // Index to the last character added/removed.
171 const CharacterIndex lastIndex = index + numberOfRemovedCharacters - 1u;
173 // Update the style runs
174 for( typename Vector<T>::Iterator it = runs.Begin(),
181 const CharacterIndex lastRunIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
183 if( lastRunIndex < index )
185 // The style run is not affected by the removed text.
186 tempRuns.PushBack( run );
190 if( ( index <= run.characterRun.characterIndex ) &&
191 ( lastIndex >= lastRunIndex ) )
193 // Add the removed run into the vector.
194 removedRuns.PushBack( run );
196 // All the characters are removed.
201 if( lastIndex < run.characterRun.characterIndex )
203 // Just move the character index.
204 run.characterRun.characterIndex -= numberOfRemovedCharacters;
208 if( run.characterRun.characterIndex < index )
210 // Remove characters starting from a character within the run.
211 run.characterRun.numberOfCharacters -= std::min( numberOfRemovedCharacters, 1u + lastRunIndex - index );
215 // Remove characters starting from a character located before the first index of the run.
216 run.characterRun.numberOfCharacters -= 1u + lastIndex - run.characterRun.characterIndex;
217 run.characterRun.characterIndex = index;
221 tempRuns.PushBack( run );
225 // Copy the temporary vector if there are runs removed.
235 // Update the style runs
236 for( typename Vector<T>::Iterator it = runs.Begin(),
243 // Update the number of characters of the style run.
245 if( ( 0u == index ) && ( 0u == run.characterRun.characterIndex ) )
247 run.characterRun.numberOfCharacters += numberOfCharacters;
249 else if( index <= run.characterRun.characterIndex )
251 run.characterRun.characterIndex += numberOfCharacters;
253 else if( index <= run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
255 run.characterRun.numberOfCharacters += numberOfCharacters;
262 * @brief Clears the runs starting from the given glyph index.
264 * @param[in] startIndex The starting glyph index used to remove runs.
265 * @param[in] endIndex The ending glyph index used to remove runs.
266 * @param[in,out] runs The text's runs.
267 * @param[out] startRemoveIndex The index to the first run to be removed.
268 * @param[out] endRemoveIndex The index to the last run to be removed.
270 template< typename T >
271 void ClearGlyphRuns( GlyphIndex startIndex,
274 uint32_t& startRemoveIndex,
275 uint32_t& endRemoveIndex )
277 T* runsBuffer = runs.Begin();
280 const Length length = runs.Count();
282 for( index = 0u; index < length; ++index )
284 if( ( run->glyphRun.glyphIndex <= endIndex ) &&
285 ( startIndex < run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs ) )
289 // Set the index to the first run to be removed.
290 startRemoveIndex = index;
296 run = ( runsBuffer + startRemoveIndex );
297 for( index = startRemoveIndex; index < length; ++index )
299 if( ( run->glyphRun.glyphIndex > endIndex ) ||
300 ( startIndex >= run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs ) )
302 // Run found. Nothing else to do.
308 endRemoveIndex = index;
310 // The number of glyphs to remove.
311 const Length numberOfGlyphsRemoved = 1u + endIndex - startIndex;
313 // Update the glyph index of the next runs.
315 for( Length index = 0u; index < length; ++index )
318 if( run->glyphRun.glyphIndex > startIndex )
320 run->glyphRun.glyphIndex -= numberOfGlyphsRemoved;
326 * @brief Clears the runs starting from the given glyph index.
328 * @param[in] startIndex The starting glyph index used to remove runs.
329 * @param[in] endIndex The ending glyph index used to remove runs.
330 * @param[in,out] runs The text's runs.
332 template< typename T >
333 void ClearGlyphRuns( GlyphIndex startIndex,
337 uint32_t startRemoveIndex = runs.Count();
338 uint32_t endRemoveIndex = startRemoveIndex;
339 ClearGlyphRuns( startIndex,
345 // Remove all remaining runs.
346 T* runBuffer = runs.Begin();
347 runs.Erase( runBuffer + startRemoveIndex, runBuffer + endRemoveIndex );
352 } // namespace Toolkit
356 #endif // __DALI_TOOLKIT_TEXT_RUN_CONTAINER_H__