2 * Copyright (c) 2021 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/input-style.h>
23 #include <dali-toolkit/internal/text/text-run-container.h>
31 void FreeFontFamilyNames(Vector<FontDescriptionRun>& fontDescriptionRuns)
33 for(Vector<FontDescriptionRun>::Iterator it = fontDescriptionRuns.Begin(),
34 endIt = fontDescriptionRuns.End();
38 delete[](*it).familyName;
41 fontDescriptionRuns.Clear();
44 void FreeEmbeddedItems(Vector<EmbeddedItem>& embeddedItem)
46 for(Vector<EmbeddedItem>::Iterator it = embeddedItem.Begin(),
47 endIt = embeddedItem.End();
51 EmbeddedItem& item = *it;
58 void FreeAnchors(Vector<Anchor>& anchors)
60 for(auto&& anchor : anchors)
68 LogicalModelPtr LogicalModel::New()
70 return LogicalModelPtr(new LogicalModel());
73 Script LogicalModel::GetScript(CharacterIndex characterIndex) const
75 // If this operation is too slow, consider a binary search.
77 const ScriptRun* const scriptRunBuffer = mScriptRuns.Begin();
78 for(Length index = 0u, length = mScriptRuns.Count(); index < length; ++index)
80 const ScriptRun* const scriptRun = scriptRunBuffer + index;
82 if((scriptRun->characterRun.characterIndex <= characterIndex) &&
83 (characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters))
85 return scriptRun->script;
89 return TextAbstraction::UNKNOWN;
92 CharacterDirection LogicalModel::GetCharacterDirection(CharacterIndex characterIndex) const
94 if(characterIndex >= mCharacterDirections.Count())
96 // The model has no right to left characters, so the vector of directions is void.
100 return *(mCharacterDirections.Begin() + characterIndex);
103 CharacterIndex LogicalModel::GetLogicalCursorIndex(CharacterIndex visualCursorIndex)
105 // The character's directions buffer.
106 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
108 // The bidirectional line info.
109 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
111 // Whether the paragraph starts with a right to left character.
112 const bool isRightToLeftParagraph = bidirectionalLineInfo->direction;
114 // The total number of characters of the line.
115 const Length lastCharacterIndex = bidirectionalLineInfo->characterRun.characterIndex + bidirectionalLineInfo->characterRun.numberOfCharacters;
117 CharacterIndex logicalCursorIndex = 0u;
119 if(bidirectionalLineInfo->characterRun.characterIndex == visualCursorIndex)
121 if(isRightToLeftParagraph)
123 logicalCursorIndex = lastCharacterIndex;
125 else // else logical position is the first of the line.
127 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
130 else if(lastCharacterIndex == visualCursorIndex)
132 if(isRightToLeftParagraph)
134 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
136 else // else logical position is the number of characters.
138 logicalCursorIndex = lastCharacterIndex;
143 // Get the character indexed by index - 1 and index
144 // and calculate the logical position according the directions of
145 // both characters and the direction of the paragraph.
147 const CharacterIndex previousVisualCursorIndex = visualCursorIndex - 1u;
148 const CharacterIndex previousLogicalCursorIndex = *(bidirectionalLineInfo->visualToLogicalMap + previousVisualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
149 const CharacterIndex currentLogicalCursorIndex = *(bidirectionalLineInfo->visualToLogicalMap + visualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
151 const CharacterDirection previousCharacterDirection = *(modelCharacterDirections + previousLogicalCursorIndex);
152 const CharacterDirection currentCharacterDirection = *(modelCharacterDirections + currentLogicalCursorIndex);
154 if(previousCharacterDirection == currentCharacterDirection)
156 // Both glyphs have the same direction.
157 if(previousCharacterDirection)
159 logicalCursorIndex = previousLogicalCursorIndex;
163 logicalCursorIndex = currentLogicalCursorIndex;
168 if(isRightToLeftParagraph)
170 if(currentCharacterDirection)
172 logicalCursorIndex = currentLogicalCursorIndex + 1u;
176 logicalCursorIndex = previousLogicalCursorIndex;
181 if(previousCharacterDirection)
183 logicalCursorIndex = currentLogicalCursorIndex;
187 logicalCursorIndex = previousLogicalCursorIndex + 1u;
193 return logicalCursorIndex;
196 CharacterIndex LogicalModel::GetLogicalCharacterIndex(CharacterIndex visualCharacterIndex)
198 // The bidirectional line info.
199 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
201 return *(bidirectionalLineInfo->visualToLogicalMap + visualCharacterIndex - bidirectionalLineInfo->characterRun.characterIndex) + bidirectionalLineInfo->characterRun.characterIndex;
204 bool LogicalModel::FetchBidirectionalLineInfo(CharacterIndex characterIndex)
206 // The number of bidirectional lines.
207 const Length numberOfBidirectionalLines = mBidirectionalLineInfo.Count();
209 if(0u == numberOfBidirectionalLines)
211 // If there is no bidirectional info.
215 // Find the bidi line where the character is laid-out.
217 const BidirectionalLineInfoRun* const bidirectionalLineInfoBuffer = mBidirectionalLineInfo.Begin();
219 // Check first if the character is in the previously fetched line.
221 BidirectionalLineRunIndex bidiLineIndex = 0u;
222 CharacterIndex lastCharacterOfRightToLeftRun = 0u;
223 if(mBidirectionalLineIndex < numberOfBidirectionalLines)
225 const BidirectionalLineInfoRun& bidiLineRun = *(bidirectionalLineInfoBuffer + mBidirectionalLineIndex);
227 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
228 if((bidiLineRun.characterRun.characterIndex <= characterIndex) &&
229 (characterIndex < lastCharacterOfRunPlusOne))
231 // The character is in the previously fetched bidi line.
236 // The character is not in the previously fetched line.
237 // Set the bidi line index from where to start the fetch.
239 if(characterIndex < bidiLineRun.characterRun.characterIndex)
241 // Start the fetch from the beginning.
246 // Start the fetch from the next line.
247 bidiLineIndex = mBidirectionalLineIndex + 1u;
248 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
253 // The character has not been found in the previously fetched bidi line.
254 for(Vector<BidirectionalLineInfoRun>::ConstIterator it = bidirectionalLineInfoBuffer + bidiLineIndex,
255 endIt = mBidirectionalLineInfo.End();
257 ++it, ++bidiLineIndex)
259 const BidirectionalLineInfoRun& bidiLineRun = *it;
261 if((lastCharacterOfRightToLeftRun < characterIndex) &&
262 (characterIndex < bidiLineRun.characterRun.characterIndex))
264 // The character is not inside a bidi line.
268 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
269 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
270 if((bidiLineRun.characterRun.characterIndex <= characterIndex) &&
271 (characterIndex < lastCharacterOfRunPlusOne))
273 // Bidi line found. Fetch the line.
274 mBidirectionalLineIndex = bidiLineIndex;
282 BidirectionalLineRunIndex LogicalModel::GetBidirectionalLineInfo() const
284 return mBidirectionalLineIndex;
287 void LogicalModel::UpdateTextStyleRuns(CharacterIndex index, int numberOfCharacters)
289 const Length totalNumberOfCharacters = mText.Count();
291 // Process the color runs.
292 Vector<ColorRun> removedColorRuns;
293 UpdateCharacterRuns<ColorRun>(index,
295 totalNumberOfCharacters,
299 // This is needed until now for underline tag in mark-up processor
300 // Process the underlined runs.
301 Vector<UnderlinedCharacterRun> removedUnderlinedCharacterRuns;
302 UpdateCharacterRuns<UnderlinedCharacterRun>(index,
304 totalNumberOfCharacters,
305 mUnderlinedCharacterRuns,
306 removedUnderlinedCharacterRuns);
308 // Process the background color runs.
309 Vector<ColorRun> removedBackgroundColorRuns;
310 UpdateCharacterRuns<ColorRun>(index,
312 totalNumberOfCharacters,
313 mBackgroundColorRuns,
314 removedBackgroundColorRuns);
316 // Process the font description runs.
317 Vector<FontDescriptionRun> removedFontDescriptionRuns;
318 UpdateCharacterRuns<FontDescriptionRun>(index,
320 totalNumberOfCharacters,
321 mFontDescriptionRuns,
322 removedFontDescriptionRuns);
324 // Free memory allocated for the font family name.
325 FreeFontFamilyNames(removedFontDescriptionRuns);
328 void LogicalModel::RetrieveStyle(CharacterIndex index, InputStyle& style)
330 unsigned int runIndex = 0u;
332 // Set the text color.
333 bool colorOverriden = false;
334 unsigned int colorIndex = 0u;
335 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
336 for(Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
337 endIt = mColorRuns.End();
341 const ColorRun& colorRun = *it;
343 if((colorRun.characterRun.characterIndex <= index) &&
344 (index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters))
346 colorIndex = runIndex;
347 colorOverriden = true;
351 // Set the text's color if it's overriden.
354 style.textColor = (*(colorRunsBuffer + colorIndex)).color;
355 style.isDefaultColor = false;
358 // Reset the run index.
361 // Set the font's parameters.
362 bool nameOverriden = false;
363 bool weightOverriden = false;
364 bool widthOverriden = false;
365 bool slantOverriden = false;
366 bool sizeOverriden = false;
367 unsigned int nameIndex = 0u;
368 unsigned int weightIndex = 0u;
369 unsigned int widthIndex = 0u;
370 unsigned int slantIndex = 0u;
371 unsigned int sizeIndex = 0u;
372 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
373 for(Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
374 endIt = mFontDescriptionRuns.End();
378 const FontDescriptionRun& fontDescriptionRun = *it;
380 if((fontDescriptionRun.characterRun.characterIndex <= index) &&
381 (index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters))
383 if(fontDescriptionRun.familyDefined)
385 nameIndex = runIndex;
386 nameOverriden = true;
389 if(fontDescriptionRun.weightDefined)
391 weightIndex = runIndex;
392 weightOverriden = true;
395 if(fontDescriptionRun.widthDefined)
397 widthIndex = runIndex;
398 widthOverriden = true;
401 if(fontDescriptionRun.slantDefined)
403 slantIndex = runIndex;
404 slantOverriden = true;
407 if(fontDescriptionRun.sizeDefined)
409 sizeIndex = runIndex;
410 sizeOverriden = true;
415 // Set the font's family name if it's overriden.
418 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + nameIndex);
420 style.familyName = std::string(fontDescriptionRun.familyName, fontDescriptionRun.familyLength);
421 style.isFamilyDefined = true;
424 // Set the font's weight if it's overriden.
427 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + weightIndex);
429 style.weight = fontDescriptionRun.weight;
430 style.isWeightDefined = true;
433 // Set the font's width if it's overriden.
436 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + widthIndex);
438 style.width = fontDescriptionRun.width;
439 style.isWidthDefined = true;
442 // Set the font's slant if it's overriden.
445 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + slantIndex);
447 style.slant = fontDescriptionRun.slant;
448 style.isSlantDefined = true;
451 // Set the font's size if it's overriden.
454 const FontDescriptionRun& fontDescriptionRun = *(fontDescriptionRunsBuffer + sizeIndex);
456 style.size = static_cast<float>(fontDescriptionRun.size) / 64.f;
457 style.isSizeDefined = true;
461 void LogicalModel::ClearFontDescriptionRuns()
463 FreeFontFamilyNames(mFontDescriptionRuns);
466 void LogicalModel::CreateParagraphInfo(CharacterIndex startIndex,
467 Length numberOfCharacters)
469 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
471 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
472 Vector<CharacterIndex> paragraphs;
473 paragraphs.Reserve(numberOfCharacters);
474 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
475 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
476 for(Length index = startIndex; index < lastCharacterIndexPlusOne; ++index)
478 if(TextAbstraction::LINE_MUST_BREAK == *(lineBreakInfoBuffer + index))
480 paragraphs.PushBack(index);
484 // Whether the current paragraphs are updated or set from scratch.
485 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
487 // Reserve space for current paragraphs plus new ones.
488 const Length numberOfNewParagraphs = paragraphs.Count();
489 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
490 mParagraphInfo.Resize(totalNumberOfParagraphs);
492 ParagraphRun* paragraphInfoBuffer = NULL;
493 Vector<ParagraphRun> newParagraphs;
495 if(updateCurrentParagraphs)
497 newParagraphs.Resize(numberOfNewParagraphs);
498 paragraphInfoBuffer = newParagraphs.Begin();
502 paragraphInfoBuffer = mParagraphInfo.Begin();
505 // Find where to insert the new paragraphs.
506 ParagraphRunIndex paragraphIndex = 0u;
507 CharacterIndex firstIndex = startIndex;
509 if(updateCurrentParagraphs)
511 for(Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
512 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
516 const ParagraphRun& paragraph(*it);
518 if(startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters)
520 firstIndex = paragraph.characterRun.characterIndex;
528 // Create the paragraph info.
529 ParagraphRunIndex newParagraphIndex = 0u;
530 for(Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
531 endIt = paragraphs.End();
533 ++it, ++newParagraphIndex)
535 const CharacterIndex index = *it;
537 ParagraphRun& paragraph = *(paragraphInfoBuffer + newParagraphIndex);
538 paragraph.characterRun.characterIndex = firstIndex;
539 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
541 firstIndex += paragraph.characterRun.numberOfCharacters;
544 // Insert the new paragraphs.
545 if(updateCurrentParagraphs)
547 mParagraphInfo.Insert(mParagraphInfo.Begin() + paragraphIndex,
548 newParagraphs.Begin(),
549 newParagraphs.End());
551 mParagraphInfo.Resize(totalNumberOfParagraphs);
553 // Update the next paragraph indices.
554 for(Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
555 endIt = mParagraphInfo.End();
559 ParagraphRun& paragraph(*it);
561 paragraph.characterRun.characterIndex += numberOfCharacters;
566 void LogicalModel::FindParagraphs(CharacterIndex index,
567 Length numberOfCharacters,
568 Vector<ParagraphRunIndex>& paragraphs)
570 // Reserve som space for the paragraph indices.
571 paragraphs.Reserve(mParagraphInfo.Count());
573 // Traverse the paragraphs to find which ones contain the given characters.
574 ParagraphRunIndex paragraphIndex = 0u;
575 for(Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
576 endIt = mParagraphInfo.End();
578 ++it, ++paragraphIndex)
580 const ParagraphRun& paragraph(*it);
582 if((paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index) &&
583 (paragraph.characterRun.characterIndex < index + numberOfCharacters))
585 paragraphs.PushBack(paragraphIndex);
590 void LogicalModel::ClearEmbeddedImages()
592 FreeEmbeddedItems(mEmbeddedItems);
595 void LogicalModel::ClearAnchors()
597 FreeAnchors(mAnchors);
600 LogicalModel::~LogicalModel()
602 ClearFontDescriptionRuns();
603 ClearEmbeddedImages();
606 LogicalModel::LogicalModel()
607 : mBidirectionalLineIndex(0u)
613 } // namespace Toolkit