2 * Copyright (c) 2015 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 LogicalModelPtr LogicalModel::New()
49 return LogicalModelPtr( new LogicalModel() );
52 Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
54 // If this operation is too slow, consider a binary search.
56 for( Length index = 0u, length = mScriptRuns.Count(); index < length; ++index )
58 const ScriptRun* const scriptRun = mScriptRuns.Begin() + index;
60 if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
61 ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
63 return scriptRun->script;
67 return TextAbstraction::UNKNOWN;
70 CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex characterIndex ) const
72 if( characterIndex >= mCharacterDirections.Count() )
74 // The model has no right to left characters, so the vector of directions is void.
78 return *( mCharacterDirections.Begin() + characterIndex );
81 void LogicalModel::SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
83 CharacterIndex startIndex,
84 Length numberOfCharacters )
86 mVisualToLogicalMap.Resize( numberOfCharacters );
87 mLogicalToVisualMap.Resize( numberOfCharacters );
89 const Length numberOfCharactersPlus = numberOfCharacters + 1u;
90 mVisualToLogicalCursorMap.Resize( numberOfCharactersPlus );
92 CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
93 CharacterIndex* modelLogicalToVisualMapBuffer = mLogicalToVisualMap.Begin();
95 CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
97 CharacterIndex lastIndex = startIndex;
98 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
100 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
102 if( bidiLineInfo.characterRun.characterIndex + bidiLineInfo.characterRun.numberOfCharacters <= startIndex )
104 // Skip this paragraph. It has been already processed.
108 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
110 // Fill with the identity.
111 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
113 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
117 // Fill the conversion table of the run.
118 for( CharacterIndex index = 0u;
119 index < bidiLineInfo.characterRun.numberOfCharacters;
120 ++index, ++lastIndex )
122 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
126 // Complete with the identity if there are some left to right characters after the last right to left.
127 for( ; lastIndex < numberOfCharacters; ++lastIndex )
129 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
132 // Sets the logical to visual conversion map.
133 for( CharacterIndex index = startIndex; index < numberOfCharacters; ++index )
135 *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
138 // Sets the visual to logical conversion map for cursor positions.
140 const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
141 BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
142 BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
144 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
146 Length bidirectionalParagraphIndex = 0u;
147 bool isRightToLeftParagraph = false;
148 for( CharacterIndex index = startIndex; index < numberOfCharactersPlus; ++index )
150 if( bidirectionalParagraph &&
151 ( bidirectionalParagraph->characterRun.characterIndex == index ) )
153 isRightToLeftParagraph = *( modelCharacterDirections + index );
158 if( isRightToLeftParagraph )
160 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
162 else // else logical position is zero.
164 *( modelVisualToLogicalCursorMap + index ) = 0u;
167 else if( numberOfCharacters == index )
169 if( isRightToLeftParagraph )
171 *( modelVisualToLogicalCursorMap + index ) = 0u;
173 else // else logical position is the number of characters.
175 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
180 // Get the character indexed by index - 1 and index
181 // and calculate the logical position according the directions of
182 // both characters and the direction of the paragraph.
184 const CharacterIndex previousIndex = index - 1u;
185 const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
186 const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
188 const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
189 const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
191 if( direction0 == direction1 )
193 // Both glyphs have the same direction.
196 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
200 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
205 if( isRightToLeftParagraph )
209 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
213 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
220 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
224 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
230 if( bidirectionalParagraph &&
231 ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
233 isRightToLeftParagraph = false;
234 ++bidirectionalParagraphIndex;
235 if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
237 bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
241 bidirectionalParagraph = NULL;
247 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
249 if( 0u == mVisualToLogicalMap.Count() )
251 // If there is no visual to logical info is because the whole text is left to right.
252 // Return the identity.
253 return visualCharacterIndex;
256 return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
259 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
261 const Length totalNumberOfCharacters = mText.Count();
263 // Process the color runs.
264 Vector<ColorRun> removedColorRuns;
265 UpdateCharacterRuns<ColorRun>( index,
267 totalNumberOfCharacters,
271 // Process the font description runs.
272 Vector<FontDescriptionRun> removedFontDescriptionRuns;
273 UpdateCharacterRuns<FontDescriptionRun>( index,
275 totalNumberOfCharacters,
276 mFontDescriptionRuns,
277 removedFontDescriptionRuns );
279 // Free memory allocated for the font family name.
280 FreeFontFamilyNames( removedFontDescriptionRuns );
283 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
285 unsigned int runIndex = 0u;
287 // Set the text color.
288 bool colorOverriden = false;
289 unsigned int colorIndex = 0u;
290 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
291 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
292 endIt = mColorRuns.End();
296 const ColorRun& colorRun = *it;
298 if( ( colorRun.characterRun.characterIndex <= index ) &&
299 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
301 colorIndex = runIndex;
302 colorOverriden = true;
306 // Set the text's color if it's overriden.
309 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
310 style.isDefaultColor = false;
313 // Reset the run index.
316 // Set the font's parameters.
317 bool nameOverriden = false;
318 bool weightOverriden = false;
319 bool widthOverriden = false;
320 bool slantOverriden = false;
321 bool sizeOverriden = false;
322 unsigned int nameIndex = 0u;
323 unsigned int weightIndex = 0u;
324 unsigned int widthIndex = 0u;
325 unsigned int slantIndex = 0u;
326 unsigned int sizeIndex = 0u;
327 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
328 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
329 endIt = mFontDescriptionRuns.End();
333 const FontDescriptionRun& fontDescriptionRun = *it;
335 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
336 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
338 if( fontDescriptionRun.familyDefined )
340 nameIndex = runIndex;
341 nameOverriden = true;
344 if( fontDescriptionRun.weightDefined )
346 weightIndex = runIndex;
347 weightOverriden = true;
350 if( fontDescriptionRun.widthDefined )
352 widthIndex = runIndex;
353 widthOverriden = true;
356 if( fontDescriptionRun.slantDefined )
358 slantIndex = runIndex;
359 slantOverriden = true;
362 if( fontDescriptionRun.sizeDefined )
364 sizeIndex = runIndex;
365 sizeOverriden = true;
370 // Set the font's family name if it's overriden.
373 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
375 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
376 style.familyDefined = true;
379 // Set the font's weight if it's overriden.
380 if( weightOverriden )
382 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
384 style.weight = fontDescriptionRun.weight;
385 style.weightDefined = true;
388 // Set the font's width if it's overriden.
391 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
393 style.width = fontDescriptionRun.width;
394 style.widthDefined = true;
397 // Set the font's slant if it's overriden.
400 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
402 style.slant = fontDescriptionRun.slant;
403 style.slantDefined = true;
406 // Set the font's size if it's overriden.
409 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
411 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
412 style.sizeDefined = true;
416 void LogicalModel::ClearFontDescriptionRuns()
418 FreeFontFamilyNames( mFontDescriptionRuns );
421 void LogicalModel::CreateParagraphInfo( CharacterIndex startIndex,
422 Length numberOfCharacters )
424 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
426 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
427 Vector<CharacterIndex> paragraphs;
428 paragraphs.Reserve( numberOfCharacters );
429 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
430 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
431 for( Length index = startIndex; index < lastCharacterIndexPlusOne; ++index )
433 if( TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index ) )
435 paragraphs.PushBack( index );
439 // Whether the current paragraphs are updated or set from scratch.
440 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
442 // Reserve space for current paragraphs plus new ones.
443 const Length numberOfNewParagraphs = paragraphs.Count();
444 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
445 mParagraphInfo.Resize( totalNumberOfParagraphs );
447 ParagraphRun* paragraphInfoBuffer = NULL;
448 Vector<ParagraphRun> newParagraphs;
450 if( updateCurrentParagraphs )
452 newParagraphs.Resize( numberOfNewParagraphs );
453 paragraphInfoBuffer = newParagraphs.Begin();
457 paragraphInfoBuffer = mParagraphInfo.Begin();
460 // Find where to insert the new paragraphs.
461 ParagraphRunIndex paragraphIndex = 0u;
462 CharacterIndex firstIndex = startIndex;
464 if( updateCurrentParagraphs )
466 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
467 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
471 const ParagraphRun& paragraph( *it );
473 if( startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters )
475 firstIndex = paragraph.characterRun.characterIndex;
483 // Create the paragraph info.
484 ParagraphRunIndex newParagraphIndex = 0u;
485 for( Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
486 endIt = paragraphs.End();
488 ++it, ++newParagraphIndex )
490 const CharacterIndex index = *it;
492 ParagraphRun& paragraph = *( paragraphInfoBuffer + newParagraphIndex );
493 paragraph.characterRun.characterIndex = firstIndex;
494 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
496 firstIndex += paragraph.characterRun.numberOfCharacters;
500 // Insert the new paragraphs.
501 if( updateCurrentParagraphs )
503 mParagraphInfo.Insert( mParagraphInfo.Begin() + paragraphIndex,
504 newParagraphs.Begin(),
505 newParagraphs.End() );
507 mParagraphInfo.Resize( totalNumberOfParagraphs );
509 // Update the next paragraph indices.
510 for( Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
511 endIt = mParagraphInfo.End();
515 ParagraphRun& paragraph( *it );
517 paragraph.characterRun.characterIndex += numberOfCharacters;
522 void LogicalModel::FindParagraphs( CharacterIndex index,
523 Length numberOfCharacters,
524 Vector<ParagraphRunIndex>& paragraphs )
526 // Reserve som space for the paragraph indices.
527 paragraphs.Reserve( mParagraphInfo.Count() );
529 // Traverse the paragraphs to find which ones contain the given characters.
530 ParagraphRunIndex paragraphIndex = 0u;
531 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
532 endIt = mParagraphInfo.End();
534 ++it, ++paragraphIndex )
536 const ParagraphRun& paragraph( *it );
538 if( ( paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index ) &&
539 ( paragraph.characterRun.characterIndex < index + numberOfCharacters ) )
541 paragraphs.PushBack( paragraphIndex );
546 LogicalModel::~LogicalModel()
548 ClearFontDescriptionRuns();
551 LogicalModel::LogicalModel()
557 } // namespace Toolkit