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 font description runs.
293 Vector<FontDescriptionRun> removedFontDescriptionRuns;
294 UpdateCharacterRuns<FontDescriptionRun>( index,
296 totalNumberOfCharacters,
297 mFontDescriptionRuns,
298 removedFontDescriptionRuns );
300 // Free memory allocated for the font family name.
301 FreeFontFamilyNames( removedFontDescriptionRuns );
304 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
306 unsigned int runIndex = 0u;
308 // Set the text color.
309 bool colorOverriden = false;
310 unsigned int colorIndex = 0u;
311 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
312 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
313 endIt = mColorRuns.End();
317 const ColorRun& colorRun = *it;
319 if( ( colorRun.characterRun.characterIndex <= index ) &&
320 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
322 colorIndex = runIndex;
323 colorOverriden = true;
327 // Set the text's color if it's overriden.
330 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
331 style.isDefaultColor = false;
334 // Reset the run index.
337 // Set the font's parameters.
338 bool nameOverriden = false;
339 bool weightOverriden = false;
340 bool widthOverriden = false;
341 bool slantOverriden = false;
342 bool sizeOverriden = false;
343 unsigned int nameIndex = 0u;
344 unsigned int weightIndex = 0u;
345 unsigned int widthIndex = 0u;
346 unsigned int slantIndex = 0u;
347 unsigned int sizeIndex = 0u;
348 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
349 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
350 endIt = mFontDescriptionRuns.End();
354 const FontDescriptionRun& fontDescriptionRun = *it;
356 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
357 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
359 if( fontDescriptionRun.familyDefined )
361 nameIndex = runIndex;
362 nameOverriden = true;
365 if( fontDescriptionRun.weightDefined )
367 weightIndex = runIndex;
368 weightOverriden = true;
371 if( fontDescriptionRun.widthDefined )
373 widthIndex = runIndex;
374 widthOverriden = true;
377 if( fontDescriptionRun.slantDefined )
379 slantIndex = runIndex;
380 slantOverriden = true;
383 if( fontDescriptionRun.sizeDefined )
385 sizeIndex = runIndex;
386 sizeOverriden = true;
391 // Set the font's family name if it's overriden.
394 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
396 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
397 style.isFamilyDefined = true;
400 // Set the font's weight if it's overriden.
401 if( weightOverriden )
403 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
405 style.weight = fontDescriptionRun.weight;
406 style.isWeightDefined = true;
409 // Set the font's width if it's overriden.
412 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
414 style.width = fontDescriptionRun.width;
415 style.isWidthDefined = true;
418 // Set the font's slant if it's overriden.
421 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
423 style.slant = fontDescriptionRun.slant;
424 style.isSlantDefined = true;
427 // Set the font's size if it's overriden.
430 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
432 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
433 style.isSizeDefined = true;
437 void LogicalModel::ClearFontDescriptionRuns()
439 FreeFontFamilyNames( mFontDescriptionRuns );
442 void LogicalModel::CreateParagraphInfo( CharacterIndex startIndex,
443 Length numberOfCharacters )
445 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
447 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
448 Vector<CharacterIndex> paragraphs;
449 paragraphs.Reserve( numberOfCharacters );
450 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
451 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
452 for( Length index = startIndex; index < lastCharacterIndexPlusOne; ++index )
454 if( TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index ) )
456 paragraphs.PushBack( index );
460 // Whether the current paragraphs are updated or set from scratch.
461 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
463 // Reserve space for current paragraphs plus new ones.
464 const Length numberOfNewParagraphs = paragraphs.Count();
465 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
466 mParagraphInfo.Resize( totalNumberOfParagraphs );
468 ParagraphRun* paragraphInfoBuffer = NULL;
469 Vector<ParagraphRun> newParagraphs;
471 if( updateCurrentParagraphs )
473 newParagraphs.Resize( numberOfNewParagraphs );
474 paragraphInfoBuffer = newParagraphs.Begin();
478 paragraphInfoBuffer = mParagraphInfo.Begin();
481 // Find where to insert the new paragraphs.
482 ParagraphRunIndex paragraphIndex = 0u;
483 CharacterIndex firstIndex = startIndex;
485 if( updateCurrentParagraphs )
487 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
488 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
492 const ParagraphRun& paragraph( *it );
494 if( startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters )
496 firstIndex = paragraph.characterRun.characterIndex;
504 // Create the paragraph info.
505 ParagraphRunIndex newParagraphIndex = 0u;
506 for( Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
507 endIt = paragraphs.End();
509 ++it, ++newParagraphIndex )
511 const CharacterIndex index = *it;
513 ParagraphRun& paragraph = *( paragraphInfoBuffer + newParagraphIndex );
514 paragraph.characterRun.characterIndex = firstIndex;
515 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
517 firstIndex += paragraph.characterRun.numberOfCharacters;
521 // Insert the new paragraphs.
522 if( updateCurrentParagraphs )
524 mParagraphInfo.Insert( mParagraphInfo.Begin() + paragraphIndex,
525 newParagraphs.Begin(),
526 newParagraphs.End() );
528 mParagraphInfo.Resize( totalNumberOfParagraphs );
530 // Update the next paragraph indices.
531 for( Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
532 endIt = mParagraphInfo.End();
536 ParagraphRun& paragraph( *it );
538 paragraph.characterRun.characterIndex += numberOfCharacters;
543 void LogicalModel::FindParagraphs( CharacterIndex index,
544 Length numberOfCharacters,
545 Vector<ParagraphRunIndex>& paragraphs )
547 // Reserve som space for the paragraph indices.
548 paragraphs.Reserve( mParagraphInfo.Count() );
550 // Traverse the paragraphs to find which ones contain the given characters.
551 ParagraphRunIndex paragraphIndex = 0u;
552 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
553 endIt = mParagraphInfo.End();
555 ++it, ++paragraphIndex )
557 const ParagraphRun& paragraph( *it );
559 if( ( paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index ) &&
560 ( paragraph.characterRun.characterIndex < index + numberOfCharacters ) )
562 paragraphs.PushBack( paragraphIndex );
567 void LogicalModel::ClearEmbeddedImages()
569 FreeEmbeddedItems( mEmbeddedItems );
572 LogicalModel::~LogicalModel()
574 ClearFontDescriptionRuns();
575 ClearEmbeddedImages();
578 LogicalModel::LogicalModel()
579 : mBidirectionalLineIndex( 0u )
585 } // namespace Toolkit