1 #ifndef DALI_TOOLKIT_TEXT_RUN_CONTAINER_H
2 #define DALI_TOOLKIT_TEXT_RUN_CONTAINER_H
5 * Copyright (c) 2021 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>
31 * @brief Clears the runs starting from the given character index.
33 * @param[in] startIndex The starting character index used to remove runs.
34 * @param[in] endIndex The ending character index used to remove runs.
35 * @param[in,out] runs The text's runs.
36 * @param[out] startRemoveIndex The index to the first run to be removed.
37 * @param[out] endRemoveIndex The index to the last run to be removed.
40 void ClearCharacterRuns(CharacterIndex startIndex,
41 CharacterIndex endIndex,
43 uint32_t& startRemoveIndex,
44 uint32_t& endRemoveIndex)
46 T* runsBuffer = runs.Begin();
49 const Length length = runs.Count();
51 for(index = 0u; index < length; ++index)
53 if((run->characterRun.characterIndex <= endIndex) &&
54 (startIndex < run->characterRun.characterIndex + run->characterRun.numberOfCharacters))
58 // Set the index to the first run to be removed.
59 startRemoveIndex = index;
66 run = (runsBuffer + startRemoveIndex);
67 for(index = startRemoveIndex; index < length; ++index)
69 if((run->characterRun.characterIndex > endIndex) ||
70 (startIndex >= run->characterRun.characterIndex + run->characterRun.numberOfCharacters))
72 // Run found. Nothing else to do.
77 endRemoveIndex = index;
79 // The number of characters to remove.
80 const Length numberOfCharactersRemoved = 1u + endIndex - startIndex;
82 // Update the character index of the next runs.
84 for(Length index = 0u; index < length; ++index)
86 if(run->characterRun.characterIndex > startIndex)
88 run->characterRun.characterIndex -= numberOfCharactersRemoved;
96 * @brief Clears the runs starting from the given character index.
98 * @param[in] startIndex The starting character index used to remove runs.
99 * @param[in] endIndex The ending character index used to remove runs.
100 * @param[in,out] runs The text's runs.
103 void ClearCharacterRuns(CharacterIndex startIndex,
104 CharacterIndex endIndex,
107 uint32_t startRemoveIndex = runs.Count();
108 uint32_t endRemoveIndex = startRemoveIndex;
109 ClearCharacterRuns(startIndex,
115 // Remove all remaining runs.
116 T* runBuffer = runs.Begin();
117 runs.Erase(runBuffer + startRemoveIndex, runBuffer + endRemoveIndex);
121 * @brief Updates the number of characters and the character index of the text's style runs.
123 * If the @p numberOfCharacters is a negative value, it means the number of characters that are removed starting from the @p index.
125 * It deletes runs if all their characters are removed.
127 * @param[in] index Index to the first character updated.
128 * @param[in] numberOfCharacters The number of characters to be updated.
129 * @param[in] totalNumberOfCharacters Total number of characters of the text.
130 * @param[in,out] runs The text's style runs.
131 * @param[out] removedRuns The text's style removed runs.
134 void UpdateCharacterRuns(CharacterIndex index,
135 int numberOfCharacters,
136 Length totalNumberOfCharacters,
138 Vector<T>& removedRuns)
140 if(0 > numberOfCharacters)
142 // Remove characters.
143 const Length numberOfRemovedCharacters = -numberOfCharacters;
145 if((0u == index) && (numberOfRemovedCharacters == totalNumberOfCharacters))
147 // Set the removed runs.
150 // All characters are removed.
153 // Nothing else to do.
157 const VectorBase::SizeType size = runs.Count();
158 // Temporary vector used to remove runs.
160 // Reserve some space for the temporary vector.
161 tempRuns.Reserve(size);
162 removedRuns.Reserve(size);
164 // Whether any run has to be removed.
165 bool runsRemoved = false;
167 // Index to the last character added/removed.
168 const CharacterIndex lastIndex = index + numberOfRemovedCharacters - 1u;
170 // Update the style runs
171 for(typename Vector<T>::Iterator it = runs.Begin(),
178 if(run.characterRun.numberOfCharacters == 0)
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 if(run.characterRun.numberOfCharacters == 0)
246 // Update the number of characters of the style run.
248 if((0u == index) && (0u == run.characterRun.characterIndex))
250 run.characterRun.numberOfCharacters += numberOfCharacters;
252 else if(index <= run.characterRun.characterIndex)
254 run.characterRun.characterIndex += numberOfCharacters;
256 else if(index <= run.characterRun.characterIndex + run.characterRun.numberOfCharacters)
258 run.characterRun.numberOfCharacters += numberOfCharacters;
265 * @brief Clears the runs starting from the given glyph index.
267 * @param[in] startIndex The starting glyph index used to remove runs.
268 * @param[in] endIndex The ending glyph index used to remove runs.
269 * @param[in,out] runs The text's runs.
270 * @param[out] startRemoveIndex The index to the first run to be removed.
271 * @param[out] endRemoveIndex The index to the last run to be removed.
274 void ClearGlyphRuns(GlyphIndex startIndex,
277 uint32_t& startRemoveIndex,
278 uint32_t& endRemoveIndex)
280 T* runsBuffer = runs.Begin();
283 const Length length = runs.Count();
285 for(index = 0u; index < length; ++index)
287 if((run->glyphRun.glyphIndex <= endIndex) &&
288 (startIndex < run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs))
292 // Set the index to the first run to be removed.
293 startRemoveIndex = index;
299 run = (runsBuffer + startRemoveIndex);
300 for(index = startRemoveIndex; index < length; ++index)
302 if((run->glyphRun.glyphIndex > endIndex) ||
303 (startIndex >= run->glyphRun.glyphIndex + run->glyphRun.numberOfGlyphs))
305 // Run found. Nothing else to do.
311 endRemoveIndex = index;
313 // The number of glyphs to remove.
314 const Length numberOfGlyphsRemoved = 1u + endIndex - startIndex;
316 // Update the glyph index of the next runs.
318 for(Length index = 0u; index < length; ++index)
320 if(run->glyphRun.glyphIndex > startIndex)
322 run->glyphRun.glyphIndex -= numberOfGlyphsRemoved;
328 * @brief Clears the runs starting from the given glyph index.
330 * @param[in] startIndex The starting glyph index used to remove runs.
331 * @param[in] endIndex The ending glyph index used to remove runs.
332 * @param[in,out] runs The text's runs.
335 void ClearGlyphRuns(GlyphIndex startIndex,
339 uint32_t startRemoveIndex = runs.Count();
340 uint32_t endRemoveIndex = startRemoveIndex;
341 ClearGlyphRuns(startIndex,
347 // Remove all remaining runs.
348 T* runBuffer = runs.Begin();
349 runs.Erase(runBuffer + startRemoveIndex, runBuffer + endRemoveIndex);
354 } // namespace Toolkit
358 #endif // DALI_TOOLKIT_TEXT_RUN_CONTAINER_H