1 #ifndef DALI_TOOLKIT_TEXT_RUN_CONTAINER_H
2 #define DALI_TOOLKIT_TEXT_RUN_CONTAINER_H
5 * Copyright (c) 2019 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 if (run.characterRun.numberOfCharacters == 0)
184 const CharacterIndex lastRunIndex = run.characterRun.characterIndex + run.characterRun.numberOfCharacters - 1u;
186 if( lastRunIndex < index )
188 // The style run is not affected by the removed text.
189 tempRuns.PushBack( run );
193 if( ( index <= run.characterRun.characterIndex ) &&
194 ( lastIndex >= lastRunIndex ) )
196 // Add the removed run into the vector.
197 removedRuns.PushBack( run );
199 // All the characters are removed.
204 if( lastIndex < run.characterRun.characterIndex )
206 // Just move the character index.
207 run.characterRun.characterIndex -= numberOfRemovedCharacters;
211 if( run.characterRun.characterIndex < index )
213 // Remove characters starting from a character within the run.
214 run.characterRun.numberOfCharacters -= std::min( numberOfRemovedCharacters, 1u + lastRunIndex - index );
218 // Remove characters starting from a character located before the first index of the run.
219 run.characterRun.numberOfCharacters -= 1u + lastIndex - run.characterRun.characterIndex;
220 run.characterRun.characterIndex = index;
224 tempRuns.PushBack( run );
228 // Copy the temporary vector if there are runs removed.
238 // Update the style runs
239 for( typename Vector<T>::Iterator it = runs.Begin(),
246 if (run.characterRun.numberOfCharacters == 0)
249 // Update the number of characters of the style run.
251 if( ( 0u == index ) && ( 0u == run.characterRun.characterIndex ) )
253 run.characterRun.numberOfCharacters += numberOfCharacters;
255 else if( index <= run.characterRun.characterIndex )
257 run.characterRun.characterIndex += numberOfCharacters;
259 else if( index <= run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
261 run.characterRun.numberOfCharacters += numberOfCharacters;
268 * @brief Clears the runs starting from the given glyph index.
270 * @param[in] startIndex The starting glyph index used to remove runs.
271 * @param[in] endIndex The ending glyph index used to remove runs.
272 * @param[in,out] runs The text's runs.
273 * @param[out] startRemoveIndex The index to the first run to be removed.
274 * @param[out] endRemoveIndex The index to the last run to be removed.
276 template< typename T >
277 void ClearGlyphRuns( GlyphIndex startIndex,
280 uint32_t& startRemoveIndex,
281 uint32_t& endRemoveIndex )
283 T* runsBuffer = runs.Begin();
286 const Length length = runs.Count();
288 for( index = 0u; index < length; ++index )
290 if( ( run->glyphRun.glyphIndex <= endIndex ) &&
291 ( startIndex < run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs ) )
295 // Set the index to the first run to be removed.
296 startRemoveIndex = index;
302 run = ( runsBuffer + startRemoveIndex );
303 for( index = startRemoveIndex; index < length; ++index )
305 if( ( run->glyphRun.glyphIndex > endIndex ) ||
306 ( startIndex >= run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs ) )
308 // Run found. Nothing else to do.
314 endRemoveIndex = index;
316 // The number of glyphs to remove.
317 const Length numberOfGlyphsRemoved = 1u + endIndex - startIndex;
319 // Update the glyph index of the next runs.
321 for( Length index = 0u; index < length; ++index )
324 if( run->glyphRun.glyphIndex > startIndex )
326 run->glyphRun.glyphIndex -= numberOfGlyphsRemoved;
332 * @brief Clears the runs starting from the given glyph index.
334 * @param[in] startIndex The starting glyph index used to remove runs.
335 * @param[in] endIndex The ending glyph index used to remove runs.
336 * @param[in,out] runs The text's runs.
338 template< typename T >
339 void ClearGlyphRuns( GlyphIndex startIndex,
343 uint32_t startRemoveIndex = runs.Count();
344 uint32_t endRemoveIndex = startRemoveIndex;
345 ClearGlyphRuns( startIndex,
351 // Remove all remaining runs.
352 T* runBuffer = runs.Begin();
353 runs.Erase( runBuffer + startRemoveIndex, runBuffer + endRemoveIndex );
358 } // namespace Toolkit
362 #endif // DALI_TOOLKIT_TEXT_RUN_CONTAINER_H