2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/logical-model-impl.h>
22 #include <dali-toolkit/internal/text/bounded-paragraph-helper-functions.h>
23 #include <dali-toolkit/internal/text/input-style.h>
24 #include <dali-toolkit/internal/text/text-run-container.h>
32 void FreeFontFamilyNames(Vector<FontDescriptionRun>& fontDescriptionRuns)
34 for(Vector<FontDescriptionRun>::Iterator it = fontDescriptionRuns.Begin(),
35 endIt = fontDescriptionRuns.End();
39 delete[](*it).familyName;
42 fontDescriptionRuns.Clear();
45 void FreeEmbeddedItems(Vector<EmbeddedItem>& embeddedItem)
47 for(Vector<EmbeddedItem>::Iterator it = embeddedItem.Begin(),
48 endIt = embeddedItem.End();
52 EmbeddedItem& item = *it;
59 void FreeAnchors(Vector<Anchor>& anchors)
61 for(auto&& anchor : anchors)
69 LogicalModelPtr LogicalModel::New()
71 return LogicalModelPtr(new LogicalModel());
74 Script LogicalModel::GetScript(CharacterIndex characterIndex) const
76 // If this operation is too slow, consider a binary search.
78 const ScriptRun* const scriptRunBuffer = mScriptRuns.Begin();
79 for(Length index = 0u, length = mScriptRuns.Count(); index < length; ++index)
81 const ScriptRun* const scriptRun = scriptRunBuffer + index;
83 if((scriptRun->characterRun.characterIndex <= characterIndex) &&
84 (characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters))
86 return scriptRun->script;
90 return TextAbstraction::UNKNOWN;
93 CharacterDirection LogicalModel::GetCharacterDirection(CharacterIndex characterIndex) const
95 if(characterIndex >= mCharacterDirections.Count())
97 // The model has no right to left characters, so the vector of directions is void.
101 return *(mCharacterDirections.Begin() + characterIndex);
104 CharacterIndex LogicalModel::GetLogicalCursorIndex(CharacterIndex visualCursorIndex)
106 // The character's directions buffer.
107 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
109 // The bidirectional line info.
110 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
112 // Whether the paragraph starts with a right to left character.
113 const bool isRightToLeftParagraph = bidirectionalLineInfo->direction;
115 // The total number of characters of the line.
116 const Length lastCharacterIndex = bidirectionalLineInfo->characterRun.characterIndex + bidirectionalLineInfo->characterRun.numberOfCharacters;
118 CharacterIndex logicalCursorIndex = 0u;
120 if(bidirectionalLineInfo->characterRun.characterIndex == visualCursorIndex)
122 if(isRightToLeftParagraph)
124 logicalCursorIndex = lastCharacterIndex;
126 else // else logical position is the first of the line.
128 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
131 else if(lastCharacterIndex == visualCursorIndex)
133 if(isRightToLeftParagraph)
135 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
137 else // else logical position is the number of characters.
139 logicalCursorIndex = lastCharacterIndex;
144 // Get the character indexed by index - 1 and index
145 // and calculate the logical position according the directions of
146 // both characters and the direction of the paragraph.
148 const CharacterIndex previousVisualCursorIndex = visualCursorIndex - 1u;
149 const CharacterIndex previousLogicalCursorIndex = *(bidirectionalLineInfo->visualToLogicalMap + previousVisualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
150 const CharacterIndex currentLogicalCursorIndex = *(bidirectionalLineInfo->visualToLogicalMap + visualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
152 const CharacterDirection previousCharacterDirection = *(modelCharacterDirections + previousLogicalCursorIndex);
153 const CharacterDirection currentCharacterDirection = *(modelCharacterDirections + currentLogicalCursorIndex);
155 if(previousCharacterDirection == currentCharacterDirection)
157 // Both glyphs have the same direction.
158 if(previousCharacterDirection)
160 logicalCursorIndex = previousLogicalCursorIndex;
164 logicalCursorIndex = currentLogicalCursorIndex;
169 if(isRightToLeftParagraph)
171 if(currentCharacterDirection)
173 logicalCursorIndex = currentLogicalCursorIndex + 1u;
177 logicalCursorIndex = previousLogicalCursorIndex;
182 if(previousCharacterDirection)
184 logicalCursorIndex = currentLogicalCursorIndex;
188 logicalCursorIndex = previousLogicalCursorIndex + 1u;
194 return logicalCursorIndex;
197 CharacterIndex LogicalModel::GetLogicalCharacterIndex(CharacterIndex visualCharacterIndex)
199 // The bidirectional line info.
200 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
202 return *(bidirectionalLineInfo->visualToLogicalMap + visualCharacterIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
205 bool LogicalModel::FetchBidirectionalLineInfo(CharacterIndex characterIndex)
207 // The number of bidirectional lines.
208 const Length numberOfBidirectionalLines = mBidirectionalLineInfo.Count();
210 if(0u == numberOfBidirectionalLines)
212 // If there is no bidirectional info.
216 // Find the bidi line where the character is laid-out.
218 const BidirectionalLineInfoRun* const bidirectionalLineInfoBuffer = mBidirectionalLineInfo.Begin();
220 // Check first if the character is in the previously fetched line.
222 BidirectionalLineRunIndex bidiLineIndex = 0u;
223 CharacterIndex lastCharacterOfRightToLeftRun = 0u;
224 if(mBidirectionalLineIndex < numberOfBidirectionalLines)
226 const BidirectionalLineInfoRun& bidiLineRun = *(bidirectionalLineInfoBuffer + mBidirectionalLineIndex);
228 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
229 if((bidiLineRun.characterRun.characterIndex <= characterIndex) &&
230 (characterIndex < lastCharacterOfRunPlusOne))
232 // The character is in the previously fetched bidi line.
237 // The character is not in the previously fetched line.
238 // Set the bidi line index from where to start the fetch.
240 if(characterIndex < bidiLineRun.characterRun.characterIndex)
242 // Start the fetch from the beginning.
247 // Start the fetch from the next line.
248 bidiLineIndex = mBidirectionalLineIndex + 1u;
249 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
254 // The character has not been found in the previously fetched bidi line.
255 for(Vector<BidirectionalLineInfoRun>::ConstIterator it = bidirectionalLineInfoBuffer + bidiLineIndex,
256 endIt = mBidirectionalLineInfo.End();
258 ++it, ++bidiLineIndex)
260 const BidirectionalLineInfoRun& bidiLineRun = *it;
262 if((lastCharacterOfRightToLeftRun < characterIndex) &&
263 (characterIndex < bidiLineRun.characterRun.characterIndex))
265 // The character is not inside a bidi line.
269 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
270 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
271 if((bidiLineRun.characterRun.characterIndex <= characterIndex) &&
272 (characterIndex < lastCharacterOfRunPlusOne))
274 // Bidi line found. Fetch the line.
275 mBidirectionalLineIndex = bidiLineIndex;
283 BidirectionalLineRunIndex LogicalModel::GetBidirectionalLineInfo() const
285 return mBidirectionalLineIndex;
288 void LogicalModel::UpdateTextStyleRuns(CharacterIndex index, int numberOfCharacters)
290 const Length totalNumberOfCharacters = mText.Count();
292 // Process the color runs.
293 Vector<ColorRun> removedColorRuns;
294 UpdateCharacterRuns<ColorRun>(index,
296 totalNumberOfCharacters,
300 // This is needed until now for underline tag in mark-up processor
301 // Process the underlined runs.
302 Vector<UnderlinedCharacterRun> removedUnderlinedCharacterRuns;
303 UpdateCharacterRuns<UnderlinedCharacterRun>(index,
305 totalNumberOfCharacters,
306 mUnderlinedCharacterRuns,
307 removedUnderlinedCharacterRuns);
309 // Process the strikethrough runs.
310 Vector<StrikethroughCharacterRun> removedStrikethroughCharacterRuns;
311 UpdateCharacterRuns<StrikethroughCharacterRun>(index,
313 totalNumberOfCharacters,
314 mStrikethroughCharacterRuns,
315 removedStrikethroughCharacterRuns);
317 // Process the background color runs.
318 Vector<ColorRun> removedBackgroundColorRuns;
319 UpdateCharacterRuns<ColorRun>(index,
321 totalNumberOfCharacters,
322 mBackgroundColorRuns,
323 removedBackgroundColorRuns);
325 // Process the font description runs.
326 Vector<FontDescriptionRun> removedFontDescriptionRuns;
327 UpdateCharacterRuns<FontDescriptionRun>(index,
329 totalNumberOfCharacters,
330 mFontDescriptionRuns,
331 removedFontDescriptionRuns);
333 // Free memory allocated for the font family name.
334 FreeFontFamilyNames(removedFontDescriptionRuns);
336 // Process the bounded paragraph runs
337 MergeBoundedParagraphRunsWhenRemoveCharacters(mText,
340 mBoundedParagraphRuns);
342 Vector<BoundedParagraphRun> removedBoundedParagraphRuns;
343 UpdateCharacterRuns<BoundedParagraphRun>(index,
345 totalNumberOfCharacters,
346 mBoundedParagraphRuns,
347 removedBoundedParagraphRuns);
350 void LogicalModel::RetrieveStyle(CharacterIndex index, InputStyle& style)
352 unsigned int runIndex = 0u;
354 // Set the text color.
355 bool colorOverriden = false;
356 unsigned int colorIndex = 0u;
357 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
358 for(Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
359 endIt = mColorRuns.End();
363 const ColorRun& colorRun = *it;
365 if((colorRun.characterRun.characterIndex <= index) &&
366 (index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters))
368 colorIndex = runIndex;
369 colorOverriden = true;
373 // Set the text's color if it's overriden.
376 style.textColor = (*(colorRunsBuffer + colorIndex)).color;
377 style.isDefaultColor = false;
380 // Reset the run index.
383 // Set the font's parameters.
384 bool nameOverriden = false;
385 bool weightOverriden = false;
386 bool widthOverriden = false;
387 bool slantOverriden = false;
388 bool sizeOverriden = false;
389 unsigned int nameIndex = 0u;
390 unsigned int weightIndex = 0u;
391 unsigned int widthIndex = 0u;
392 unsigned int slantIndex = 0u;
393 unsigned int sizeIndex = 0u;
394 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
395 for(Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
396 endIt = mFontDescriptionRuns.End();
400 const FontDescriptionRun& fontDescriptionRun = *it;
402 if((fontDescriptionRun.characterRun.characterIndex <= index) &&
403 (index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters))
405 if(fontDescriptionRun.familyDefined)
407 nameIndex = runIndex;
408 nameOverriden = true;
411 if(fontDescriptionRun.weightDefined)
413 weightIndex = runIndex;
414 weightOverriden = true;
417 if(fontDescriptionRun.widthDefined)
419 widthIndex = runIndex;
420 widthOverriden = true;
423 if(fontDescriptionRun.slantDefined)
425 slantIndex = runIndex;
426 slantOverriden = true;
429 if(fontDescriptionRun.sizeDefined)
431 sizeIndex = runIndex;
432 sizeOverriden = true;
437 // Set the font's family name if it's overriden.
440 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + nameIndex);
442 style.familyName = std::string(fontDescriptionRun.familyName, fontDescriptionRun.familyLength);
443 style.isFamilyDefined = true;
446 // Set the font's weight if it's overriden.
449 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + weightIndex);
451 style.weight = fontDescriptionRun.weight;
452 style.isWeightDefined = true;
455 // Set the font's width if it's overriden.
458 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + widthIndex);
460 style.width = fontDescriptionRun.width;
461 style.isWidthDefined = true;
464 // Set the font's slant if it's overriden.
467 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + slantIndex);
469 style.slant = fontDescriptionRun.slant;
470 style.isSlantDefined = true;
473 // Set the font's size if it's overriden.
476 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + sizeIndex);
478 style.size = static_cast<float>(fontDescriptionRun.size) / 64.f;
479 style.isSizeDefined = true;
483 void LogicalModel::ClearFontDescriptionRuns()
485 FreeFontFamilyNames(mFontDescriptionRuns);
488 void LogicalModel::ClearStrikethroughRuns()
490 mStrikethroughCharacterRuns.Clear();
493 void LogicalModel::CreateParagraphInfo(CharacterIndex startIndex,
494 Length numberOfCharacters)
496 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
498 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
499 Vector<CharacterIndex> paragraphs;
500 paragraphs.Reserve(numberOfCharacters);
501 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
502 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
503 for(Length index = startIndex; index < lastCharacterIndexPlusOne; ++index)
505 if(TextAbstraction::LINE_MUST_BREAK == *(lineBreakInfoBuffer + index))
507 paragraphs.PushBack(index);
511 // Whether the current paragraphs are updated or set from scratch.
512 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
514 // Reserve space for current paragraphs plus new ones.
515 const Length numberOfNewParagraphs = paragraphs.Count();
516 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
517 mParagraphInfo.Resize(totalNumberOfParagraphs);
519 ParagraphRun* paragraphInfoBuffer = NULL;
520 Vector<ParagraphRun> newParagraphs;
522 if(updateCurrentParagraphs)
524 newParagraphs.Resize(numberOfNewParagraphs);
525 paragraphInfoBuffer = newParagraphs.Begin();
529 paragraphInfoBuffer = mParagraphInfo.Begin();
532 // Find where to insert the new paragraphs.
533 ParagraphRunIndex paragraphIndex = 0u;
534 CharacterIndex firstIndex = startIndex;
536 if(updateCurrentParagraphs)
538 for(Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
539 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
543 const ParagraphRun& paragraph(*it);
545 if(startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters)
547 firstIndex = paragraph.characterRun.characterIndex;
555 // Create the paragraph info.
556 ParagraphRunIndex newParagraphIndex = 0u;
557 for(Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
558 endIt = paragraphs.End();
560 ++it, ++newParagraphIndex)
562 const CharacterIndex index = *it;
564 ParagraphRun& paragraph = *(paragraphInfoBuffer + newParagraphIndex);
565 paragraph.characterRun.characterIndex = firstIndex;
566 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
568 firstIndex += paragraph.characterRun.numberOfCharacters;
571 // Insert the new paragraphs.
572 if(updateCurrentParagraphs)
574 mParagraphInfo.Insert(mParagraphInfo.Begin() + paragraphIndex,
575 newParagraphs.Begin(),
576 newParagraphs.End());
578 mParagraphInfo.Resize(totalNumberOfParagraphs);
580 // Update the next paragraph indices.
581 for(Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
582 endIt = mParagraphInfo.End();
586 ParagraphRun& paragraph(*it);
588 paragraph.characterRun.characterIndex += numberOfCharacters;
593 void LogicalModel::FindParagraphs(CharacterIndex index,
594 Length numberOfCharacters,
595 Vector<ParagraphRunIndex>& paragraphs)
597 // Reserve som space for the paragraph indices.
598 paragraphs.Reserve(mParagraphInfo.Count());
600 // Traverse the paragraphs to find which ones contain the given characters.
601 ParagraphRunIndex paragraphIndex = 0u;
602 for(Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
603 endIt = mParagraphInfo.End();
605 ++it, ++paragraphIndex)
607 const ParagraphRun& paragraph(*it);
609 if((paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index) &&
610 (paragraph.characterRun.characterIndex < index + numberOfCharacters))
612 paragraphs.PushBack(paragraphIndex);
617 Length LogicalModel::GetNumberOfBoundedParagraphRuns() const
619 return mBoundedParagraphRuns.Count();
622 const Vector<BoundedParagraphRun>& LogicalModel::GetBoundedParagraphRuns() const
624 return mBoundedParagraphRuns;
627 void LogicalModel::ClearEmbeddedImages()
629 FreeEmbeddedItems(mEmbeddedItems);
632 void LogicalModel::ClearAnchors()
634 FreeAnchors(mAnchors);
637 LogicalModel::~LogicalModel()
639 ClearFontDescriptionRuns();
640 ClearEmbeddedImages();
643 LogicalModel::LogicalModel()
644 : mBidirectionalLineIndex(0u)
650 } // namespace Toolkit