2 * Copyright (c) 2019 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>
34 void FreeFontFamilyNames( Vector<FontDescriptionRun>& fontDescriptionRuns )
36 for( Vector<FontDescriptionRun>::Iterator it = fontDescriptionRuns.Begin(),
37 endIt = fontDescriptionRuns.End();
41 delete[] (*it).familyName;
44 fontDescriptionRuns.Clear();
47 void FreeEmbeddedItems( Vector<EmbeddedItem>& embeddedItem )
49 for( Vector<EmbeddedItem>::Iterator it = embeddedItem.Begin(),
50 endIt = embeddedItem.End();
54 EmbeddedItem& item = *it;
61 LogicalModelPtr LogicalModel::New()
63 return LogicalModelPtr( new LogicalModel() );
66 Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
68 // If this operation is too slow, consider a binary search.
70 const ScriptRun* const scriptRunBuffer = mScriptRuns.Begin();
71 for( Length index = 0u, length = mScriptRuns.Count(); index < length; ++index )
73 const ScriptRun* const scriptRun = scriptRunBuffer + index;
75 if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
76 ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
78 return scriptRun->script;
82 return TextAbstraction::UNKNOWN;
85 CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex characterIndex ) const
87 if( characterIndex >= mCharacterDirections.Count() )
89 // The model has no right to left characters, so the vector of directions is void.
93 return *( mCharacterDirections.Begin() + characterIndex );
96 CharacterIndex LogicalModel::GetLogicalCursorIndex( CharacterIndex visualCursorIndex )
98 // The character's directions buffer.
99 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
101 // The bidirectional line info.
102 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
104 // Whether the paragraph starts with a right to left character.
105 const bool isRightToLeftParagraph = bidirectionalLineInfo->direction;
107 // The total number of characters of the line.
108 const Length lastCharacterIndex = bidirectionalLineInfo->characterRun.characterIndex + bidirectionalLineInfo->characterRun.numberOfCharacters;
110 CharacterIndex logicalCursorIndex = 0u;
112 if( bidirectionalLineInfo->characterRun.characterIndex == visualCursorIndex )
114 if( isRightToLeftParagraph )
116 logicalCursorIndex = lastCharacterIndex;
118 else // else logical position is the first of the line.
120 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
123 else if( lastCharacterIndex == visualCursorIndex )
125 if( isRightToLeftParagraph )
127 logicalCursorIndex = bidirectionalLineInfo->characterRun.characterIndex;
129 else // else logical position is the number of characters.
131 logicalCursorIndex = lastCharacterIndex;
136 // Get the character indexed by index - 1 and index
137 // and calculate the logical position according the directions of
138 // both characters and the direction of the paragraph.
140 const CharacterIndex previousVisualCursorIndex = visualCursorIndex - 1u;
141 const CharacterIndex previousLogicalCursorIndex = *( bidirectionalLineInfo->visualToLogicalMap + previousVisualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
142 const CharacterIndex currentLogicalCursorIndex = *( bidirectionalLineInfo->visualToLogicalMap + visualCursorIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
144 const CharacterDirection previousCharacterDirection = *( modelCharacterDirections + previousLogicalCursorIndex );
145 const CharacterDirection currentCharacterDirection = *( modelCharacterDirections + currentLogicalCursorIndex );
147 if( previousCharacterDirection == currentCharacterDirection )
149 // Both glyphs have the same direction.
150 if( previousCharacterDirection )
152 logicalCursorIndex = previousLogicalCursorIndex;
156 logicalCursorIndex = currentLogicalCursorIndex;
161 if( isRightToLeftParagraph )
163 if( currentCharacterDirection )
165 logicalCursorIndex = currentLogicalCursorIndex + 1u;
169 logicalCursorIndex = previousLogicalCursorIndex;
174 if( previousCharacterDirection )
176 logicalCursorIndex = currentLogicalCursorIndex;
180 logicalCursorIndex = previousLogicalCursorIndex + 1u;
186 return logicalCursorIndex;
189 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex )
191 // The bidirectional line info.
192 const BidirectionalLineInfoRun* const bidirectionalLineInfo = mBidirectionalLineInfo.Begin() + mBidirectionalLineIndex;
194 return *( bidirectionalLineInfo->visualToLogicalMap + visualCharacterIndex - bidirectionalLineInfo->characterRun.characterIndex ) + bidirectionalLineInfo->characterRun.characterIndex;
197 bool LogicalModel::FetchBidirectionalLineInfo( CharacterIndex characterIndex )
199 // The number of bidirectional lines.
200 const Length numberOfBidirectionalLines = mBidirectionalLineInfo.Count();
202 if( 0u == numberOfBidirectionalLines )
204 // If there is no bidirectional info.
208 // Find the bidi line where the character is laid-out.
210 const BidirectionalLineInfoRun* const bidirectionalLineInfoBuffer = mBidirectionalLineInfo.Begin();
212 // Check first if the character is in the previously fetched line.
214 BidirectionalLineRunIndex bidiLineIndex = 0u;
215 CharacterIndex lastCharacterOfRightToLeftRun = 0u;
216 if( mBidirectionalLineIndex < numberOfBidirectionalLines )
218 const BidirectionalLineInfoRun& bidiLineRun = *( bidirectionalLineInfoBuffer + mBidirectionalLineIndex );
220 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
221 if( ( bidiLineRun.characterRun.characterIndex <= characterIndex ) &&
222 ( characterIndex < lastCharacterOfRunPlusOne ) )
224 // The character is in the previously fetched bidi line.
229 // The character is not in the previously fetched line.
230 // Set the bidi line index from where to start the fetch.
232 if( characterIndex < bidiLineRun.characterRun.characterIndex )
234 // Start the fetch from the beginning.
239 // Start the fetch from the next line.
240 bidiLineIndex = mBidirectionalLineIndex + 1u;
241 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
246 // The character has not been found in the previously fetched bidi line.
247 for( Vector<BidirectionalLineInfoRun>::ConstIterator it = bidirectionalLineInfoBuffer + bidiLineIndex,
248 endIt = mBidirectionalLineInfo.End();
250 ++it, ++bidiLineIndex )
252 const BidirectionalLineInfoRun& bidiLineRun = *it;
254 if( ( lastCharacterOfRightToLeftRun < characterIndex ) &&
255 ( characterIndex < bidiLineRun.characterRun.characterIndex ) )
257 // The character is not inside a bidi line.
261 const CharacterIndex lastCharacterOfRunPlusOne = bidiLineRun.characterRun.characterIndex + bidiLineRun.characterRun.numberOfCharacters;
262 lastCharacterOfRightToLeftRun = lastCharacterOfRunPlusOne - 1u;
263 if( ( bidiLineRun.characterRun.characterIndex <= characterIndex ) &&
264 ( characterIndex < lastCharacterOfRunPlusOne ) )
266 // Bidi line found. Fetch the line.
267 mBidirectionalLineIndex = bidiLineIndex;
275 BidirectionalLineRunIndex LogicalModel::GetBidirectionalLineInfo() const
277 return mBidirectionalLineIndex;
280 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
282 const Length totalNumberOfCharacters = mText.Count();
284 // Process the color runs.
285 Vector<ColorRun> removedColorRuns;
286 UpdateCharacterRuns<ColorRun>( index,
288 totalNumberOfCharacters,
292 // Process the background color runs.
293 Vector<ColorRun> removedBackgroundColorRuns;
294 UpdateCharacterRuns<ColorRun>( index,
296 totalNumberOfCharacters,
297 mBackgroundColorRuns,
298 removedBackgroundColorRuns );
300 // Process the font description runs.
301 Vector<FontDescriptionRun> removedFontDescriptionRuns;
302 UpdateCharacterRuns<FontDescriptionRun>( index,
304 totalNumberOfCharacters,
305 mFontDescriptionRuns,
306 removedFontDescriptionRuns );
308 // Free memory allocated for the font family name.
309 FreeFontFamilyNames( removedFontDescriptionRuns );
312 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
314 unsigned int runIndex = 0u;
316 // Set the text color.
317 bool colorOverriden = false;
318 unsigned int colorIndex = 0u;
319 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
320 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
321 endIt = mColorRuns.End();
325 const ColorRun& colorRun = *it;
327 if( ( colorRun.characterRun.characterIndex <= index ) &&
328 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
330 colorIndex = runIndex;
331 colorOverriden = true;
335 // Set the text's color if it's overriden.
338 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
339 style.isDefaultColor = false;
342 // Reset the run index.
345 // Set the font's parameters.
346 bool nameOverriden = false;
347 bool weightOverriden = false;
348 bool widthOverriden = false;
349 bool slantOverriden = false;
350 bool sizeOverriden = false;
351 unsigned int nameIndex = 0u;
352 unsigned int weightIndex = 0u;
353 unsigned int widthIndex = 0u;
354 unsigned int slantIndex = 0u;
355 unsigned int sizeIndex = 0u;
356 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
357 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
358 endIt = mFontDescriptionRuns.End();
362 const FontDescriptionRun& fontDescriptionRun = *it;
364 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
365 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
367 if( fontDescriptionRun.familyDefined )
369 nameIndex = runIndex;
370 nameOverriden = true;
373 if( fontDescriptionRun.weightDefined )
375 weightIndex = runIndex;
376 weightOverriden = true;
379 if( fontDescriptionRun.widthDefined )
381 widthIndex = runIndex;
382 widthOverriden = true;
385 if( fontDescriptionRun.slantDefined )
387 slantIndex = runIndex;
388 slantOverriden = true;
391 if( fontDescriptionRun.sizeDefined )
393 sizeIndex = runIndex;
394 sizeOverriden = true;
399 // Set the font's family name if it's overriden.
402 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
404 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
405 style.isFamilyDefined = true;
408 // Set the font's weight if it's overriden.
409 if( weightOverriden )
411 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
413 style.weight = fontDescriptionRun.weight;
414 style.isWeightDefined = true;
417 // Set the font's width if it's overriden.
420 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
422 style.width = fontDescriptionRun.width;
423 style.isWidthDefined = true;
426 // Set the font's slant if it's overriden.
429 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
431 style.slant = fontDescriptionRun.slant;
432 style.isSlantDefined = true;
435 // Set the font's size if it's overriden.
438 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
440 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
441 style.isSizeDefined = true;
445 void LogicalModel::ClearFontDescriptionRuns()
447 FreeFontFamilyNames( mFontDescriptionRuns );
450 void LogicalModel::CreateParagraphInfo( CharacterIndex startIndex,
451 Length numberOfCharacters )
453 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
455 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
456 Vector<CharacterIndex> paragraphs;
457 paragraphs.Reserve( numberOfCharacters );
458 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
459 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
460 for( Length index = startIndex; index < lastCharacterIndexPlusOne; ++index )
462 if( TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index ) )
464 paragraphs.PushBack( index );
468 // Whether the current paragraphs are updated or set from scratch.
469 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
471 // Reserve space for current paragraphs plus new ones.
472 const Length numberOfNewParagraphs = paragraphs.Count();
473 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
474 mParagraphInfo.Resize( totalNumberOfParagraphs );
476 ParagraphRun* paragraphInfoBuffer = NULL;
477 Vector<ParagraphRun> newParagraphs;
479 if( updateCurrentParagraphs )
481 newParagraphs.Resize( numberOfNewParagraphs );
482 paragraphInfoBuffer = newParagraphs.Begin();
486 paragraphInfoBuffer = mParagraphInfo.Begin();
489 // Find where to insert the new paragraphs.
490 ParagraphRunIndex paragraphIndex = 0u;
491 CharacterIndex firstIndex = startIndex;
493 if( updateCurrentParagraphs )
495 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
496 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
500 const ParagraphRun& paragraph( *it );
502 if( startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters )
504 firstIndex = paragraph.characterRun.characterIndex;
512 // Create the paragraph info.
513 ParagraphRunIndex newParagraphIndex = 0u;
514 for( Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
515 endIt = paragraphs.End();
517 ++it, ++newParagraphIndex )
519 const CharacterIndex index = *it;
521 ParagraphRun& paragraph = *( paragraphInfoBuffer + newParagraphIndex );
522 paragraph.characterRun.characterIndex = firstIndex;
523 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
525 firstIndex += paragraph.characterRun.numberOfCharacters;
529 // Insert the new paragraphs.
530 if( updateCurrentParagraphs )
532 mParagraphInfo.Insert( mParagraphInfo.Begin() + paragraphIndex,
533 newParagraphs.Begin(),
534 newParagraphs.End() );
536 mParagraphInfo.Resize( totalNumberOfParagraphs );
538 // Update the next paragraph indices.
539 for( Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
540 endIt = mParagraphInfo.End();
544 ParagraphRun& paragraph( *it );
546 paragraph.characterRun.characterIndex += numberOfCharacters;
551 void LogicalModel::FindParagraphs( CharacterIndex index,
552 Length numberOfCharacters,
553 Vector<ParagraphRunIndex>& paragraphs )
555 // Reserve som space for the paragraph indices.
556 paragraphs.Reserve( mParagraphInfo.Count() );
558 // Traverse the paragraphs to find which ones contain the given characters.
559 ParagraphRunIndex paragraphIndex = 0u;
560 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
561 endIt = mParagraphInfo.End();
563 ++it, ++paragraphIndex )
565 const ParagraphRun& paragraph( *it );
567 if( ( paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index ) &&
568 ( paragraph.characterRun.characterIndex < index + numberOfCharacters ) )
570 paragraphs.PushBack( paragraphIndex );
575 void LogicalModel::ClearEmbeddedImages()
577 FreeEmbeddedItems( mEmbeddedItems );
580 LogicalModel::~LogicalModel()
582 ClearFontDescriptionRuns();
583 ClearEmbeddedImages();
586 LogicalModel::LogicalModel()
587 : mBidirectionalLineIndex( 0u )
593 } // namespace Toolkit