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( CharacterIndex startIndex,
82 Length numberOfCharacters )
84 mVisualToLogicalMap.Resize( numberOfCharacters );
86 CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
88 const BidirectionalLineInfoRun* const bidirectionalInfo = mBidirectionalLineInfo.Begin();
89 const Length numberOfRuns = mBidirectionalLineInfo.Count();
91 CharacterIndex lastIndex = startIndex;
92 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
94 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
96 if( bidiLineInfo.characterRun.characterIndex + bidiLineInfo.characterRun.numberOfCharacters <= startIndex )
98 // Skip this paragraph. It has been already processed.
102 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
104 // Fill with the identity.
105 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
107 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
111 // Fill the conversion table of the run.
112 for( CharacterIndex index = 0u;
113 index < bidiLineInfo.characterRun.numberOfCharacters;
114 ++index, ++lastIndex )
116 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
120 // Complete with the identity if there are some left to right characters after the last right to left.
121 for( ; lastIndex < numberOfCharacters; ++lastIndex )
123 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
126 // Sets the visual to logical conversion map for cursor positions.
127 const Length numberOfCharactersPlusOne = numberOfCharacters + 1u;
128 mVisualToLogicalCursorMap.Resize( numberOfCharactersPlusOne );
130 CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
132 const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
133 BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
134 BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
136 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
138 Length bidirectionalParagraphIndex = 0u;
139 bool isRightToLeftParagraph = false;
140 for( CharacterIndex index = startIndex; index < numberOfCharactersPlusOne; ++index )
142 if( bidirectionalParagraph &&
143 ( bidirectionalParagraph->characterRun.characterIndex == index ) )
145 isRightToLeftParagraph = *( modelCharacterDirections + index );
150 if( isRightToLeftParagraph )
152 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
154 else // else logical position is zero.
156 *( modelVisualToLogicalCursorMap + index ) = 0u;
159 else if( numberOfCharacters == index )
161 if( isRightToLeftParagraph )
163 *( modelVisualToLogicalCursorMap + index ) = 0u;
165 else // else logical position is the number of characters.
167 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
172 // Get the character indexed by index - 1 and index
173 // and calculate the logical position according the directions of
174 // both characters and the direction of the paragraph.
176 const CharacterIndex previousIndex = index - 1u;
177 const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
178 const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
180 const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
181 const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
183 if( direction0 == direction1 )
185 // Both glyphs have the same direction.
188 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
192 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
197 if( isRightToLeftParagraph )
201 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
205 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
212 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
216 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
222 if( bidirectionalParagraph &&
223 ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
225 isRightToLeftParagraph = false;
226 ++bidirectionalParagraphIndex;
227 if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
229 bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
233 bidirectionalParagraph = NULL;
239 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
241 if( 0u == mVisualToLogicalMap.Count() )
243 // If there is no visual to logical info is because the whole text is left to right.
244 // Return the identity.
245 return visualCharacterIndex;
248 return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
251 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
253 const Length totalNumberOfCharacters = mText.Count();
255 // Process the color runs.
256 Vector<ColorRun> removedColorRuns;
257 UpdateCharacterRuns<ColorRun>( index,
259 totalNumberOfCharacters,
263 // Process the font description runs.
264 Vector<FontDescriptionRun> removedFontDescriptionRuns;
265 UpdateCharacterRuns<FontDescriptionRun>( index,
267 totalNumberOfCharacters,
268 mFontDescriptionRuns,
269 removedFontDescriptionRuns );
271 // Free memory allocated for the font family name.
272 FreeFontFamilyNames( removedFontDescriptionRuns );
275 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
277 unsigned int runIndex = 0u;
279 // Set the text color.
280 bool colorOverriden = false;
281 unsigned int colorIndex = 0u;
282 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
283 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
284 endIt = mColorRuns.End();
288 const ColorRun& colorRun = *it;
290 if( ( colorRun.characterRun.characterIndex <= index ) &&
291 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
293 colorIndex = runIndex;
294 colorOverriden = true;
298 // Set the text's color if it's overriden.
301 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
302 style.isDefaultColor = false;
305 // Reset the run index.
308 // Set the font's parameters.
309 bool nameOverriden = false;
310 bool weightOverriden = false;
311 bool widthOverriden = false;
312 bool slantOverriden = false;
313 bool sizeOverriden = false;
314 unsigned int nameIndex = 0u;
315 unsigned int weightIndex = 0u;
316 unsigned int widthIndex = 0u;
317 unsigned int slantIndex = 0u;
318 unsigned int sizeIndex = 0u;
319 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
320 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
321 endIt = mFontDescriptionRuns.End();
325 const FontDescriptionRun& fontDescriptionRun = *it;
327 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
328 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
330 if( fontDescriptionRun.familyDefined )
332 nameIndex = runIndex;
333 nameOverriden = true;
336 if( fontDescriptionRun.weightDefined )
338 weightIndex = runIndex;
339 weightOverriden = true;
342 if( fontDescriptionRun.widthDefined )
344 widthIndex = runIndex;
345 widthOverriden = true;
348 if( fontDescriptionRun.slantDefined )
350 slantIndex = runIndex;
351 slantOverriden = true;
354 if( fontDescriptionRun.sizeDefined )
356 sizeIndex = runIndex;
357 sizeOverriden = true;
362 // Set the font's family name if it's overriden.
365 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
367 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
368 style.familyDefined = true;
371 // Set the font's weight if it's overriden.
372 if( weightOverriden )
374 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
376 style.weight = fontDescriptionRun.weight;
377 style.weightDefined = true;
380 // Set the font's width if it's overriden.
383 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
385 style.width = fontDescriptionRun.width;
386 style.widthDefined = true;
389 // Set the font's slant if it's overriden.
392 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
394 style.slant = fontDescriptionRun.slant;
395 style.slantDefined = true;
398 // Set the font's size if it's overriden.
401 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
403 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
404 style.sizeDefined = true;
408 void LogicalModel::ClearFontDescriptionRuns()
410 FreeFontFamilyNames( mFontDescriptionRuns );
413 void LogicalModel::CreateParagraphInfo( CharacterIndex startIndex,
414 Length numberOfCharacters )
416 const Length totalNumberOfCharacters = mLineBreakInfo.Count();
418 // Count the number of LINE_MUST_BREAK to reserve some space for the vector of paragraph's info.
419 Vector<CharacterIndex> paragraphs;
420 paragraphs.Reserve( numberOfCharacters );
421 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = mLineBreakInfo.Begin();
422 const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
423 for( Length index = startIndex; index < lastCharacterIndexPlusOne; ++index )
425 if( TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index ) )
427 paragraphs.PushBack( index );
431 // Whether the current paragraphs are updated or set from scratch.
432 const bool updateCurrentParagraphs = numberOfCharacters < totalNumberOfCharacters;
434 // Reserve space for current paragraphs plus new ones.
435 const Length numberOfNewParagraphs = paragraphs.Count();
436 const Length totalNumberOfParagraphs = mParagraphInfo.Count() + numberOfNewParagraphs;
437 mParagraphInfo.Resize( totalNumberOfParagraphs );
439 ParagraphRun* paragraphInfoBuffer = NULL;
440 Vector<ParagraphRun> newParagraphs;
442 if( updateCurrentParagraphs )
444 newParagraphs.Resize( numberOfNewParagraphs );
445 paragraphInfoBuffer = newParagraphs.Begin();
449 paragraphInfoBuffer = mParagraphInfo.Begin();
452 // Find where to insert the new paragraphs.
453 ParagraphRunIndex paragraphIndex = 0u;
454 CharacterIndex firstIndex = startIndex;
456 if( updateCurrentParagraphs )
458 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
459 endIt = mParagraphInfo.Begin() + totalNumberOfParagraphs - numberOfNewParagraphs;
463 const ParagraphRun& paragraph( *it );
465 if( startIndex < paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters )
467 firstIndex = paragraph.characterRun.characterIndex;
475 // Create the paragraph info.
476 ParagraphRunIndex newParagraphIndex = 0u;
477 for( Vector<CharacterIndex>::ConstIterator it = paragraphs.Begin(),
478 endIt = paragraphs.End();
480 ++it, ++newParagraphIndex )
482 const CharacterIndex index = *it;
484 ParagraphRun& paragraph = *( paragraphInfoBuffer + newParagraphIndex );
485 paragraph.characterRun.characterIndex = firstIndex;
486 paragraph.characterRun.numberOfCharacters = 1u + index - firstIndex;
488 firstIndex += paragraph.characterRun.numberOfCharacters;
492 // Insert the new paragraphs.
493 if( updateCurrentParagraphs )
495 mParagraphInfo.Insert( mParagraphInfo.Begin() + paragraphIndex,
496 newParagraphs.Begin(),
497 newParagraphs.End() );
499 mParagraphInfo.Resize( totalNumberOfParagraphs );
501 // Update the next paragraph indices.
502 for( Vector<ParagraphRun>::Iterator it = mParagraphInfo.Begin() + paragraphIndex + newParagraphs.Count(),
503 endIt = mParagraphInfo.End();
507 ParagraphRun& paragraph( *it );
509 paragraph.characterRun.characterIndex += numberOfCharacters;
514 void LogicalModel::FindParagraphs( CharacterIndex index,
515 Length numberOfCharacters,
516 Vector<ParagraphRunIndex>& paragraphs )
518 // Reserve som space for the paragraph indices.
519 paragraphs.Reserve( mParagraphInfo.Count() );
521 // Traverse the paragraphs to find which ones contain the given characters.
522 ParagraphRunIndex paragraphIndex = 0u;
523 for( Vector<ParagraphRun>::ConstIterator it = mParagraphInfo.Begin(),
524 endIt = mParagraphInfo.End();
526 ++it, ++paragraphIndex )
528 const ParagraphRun& paragraph( *it );
530 if( ( paragraph.characterRun.characterIndex + paragraph.characterRun.numberOfCharacters > index ) &&
531 ( paragraph.characterRun.characterIndex < index + numberOfCharacters ) )
533 paragraphs.PushBack( paragraphIndex );
538 LogicalModel::~LogicalModel()
540 ClearFontDescriptionRuns();
543 LogicalModel::LogicalModel()
549 } // namespace Toolkit