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.h>
25 #include <dali-toolkit/internal/text/bidirectional-line-info-run.h>
26 #include <dali-toolkit/internal/text/bidirectional-paragraph-info-run.h>
27 #include <dali-toolkit/internal/text/font-run.h>
28 #include <dali-toolkit/internal/text/script-run.h>
39 * @brief caches some temporary values of the GetNumberOfScriptRuns( characterIndex, numberOfCharacters )
40 * operation and the GetNumberOfFontRuns( characterIndex, numberOfCharacters ) as they are going to be
41 * used in the GetScriptRuns() and the GetFontRuns() calls.
45 CharacterIndex characterIndex; ///< The character index.
46 Length numberOfCharacters; ///< The number of characters.
47 Length firstRun; ///< Index to the first run.
48 Length numberOfRuns; ///< The number of runs.
51 struct LogicalModel::Impl
53 Vector<Character> mText;
54 Vector<ScriptRun> mScriptRuns;
55 Vector<FontRun> mFontRuns;
56 Vector<LineBreakInfo> mLineBreakInfo;
57 Vector<WordBreakInfo> mWordBreakInfo;
58 Vector<BidirectionalParagraphInfoRun> mBidirectionalParagraphInfo;
60 Vector<BidirectionalLineInfoRun> mBidirectionalLineInfo;
61 Vector<CharacterIndex> mLogicalToVisualMap; ///< Bidirectional logical to visual conversion table.
62 Vector<CharacterIndex> mVisualToLogicalMap; ///< Bidirectional visual to logical conversion table.
64 GetRunCache mGetScriptCache; ///< Caches the GetNumberOfScriptRuns( characterIndex, numberOfCharacters ) operation.
65 GetRunCache mGetFontCache; ///< Caches the GetNumberOfFontRuns( characterIndex, numberOfCharacters ) operation.
66 GetRunCache mGetBidirectionalCache; ///< Caches the GetNumberOfBidirectionalInfoRuns( characterIndex, numberOfCharacters ) operation.
69 LogicalModelPtr LogicalModel::New()
71 return LogicalModelPtr( new LogicalModel() );
74 void LogicalModel::SetText( const Character* const text,
75 Length numberOfCharacters )
77 Vector<Character>& modelText = mImpl->mText;
79 if( 0u == numberOfCharacters )
85 modelText.Resize( numberOfCharacters );
86 memcpy( modelText.Begin(), text, numberOfCharacters * sizeof( Character ) );
90 Length LogicalModel::GetNumberOfCharacters() const
92 return mImpl->mText.Count();
95 void LogicalModel::GetText( Character* text,
96 CharacterIndex characterIndex,
97 Length numberOfCharacters ) const
99 Vector<Character>& modelText = mImpl->mText;
100 memcpy( text, modelText.Begin() + characterIndex, numberOfCharacters * sizeof( Character ) );
103 Character LogicalModel::GetCharacter( CharacterIndex characterIndex ) const
105 return mImpl->mText[characterIndex];
108 void LogicalModel::SetScripts( const ScriptRun* const scripts,
109 Length numberOfRuns )
111 Vector<ScriptRun>& scriptRuns = mImpl->mScriptRuns;
113 if( 0u == numberOfRuns )
119 scriptRuns.Resize( numberOfRuns );
120 memcpy( scriptRuns.Begin(), scripts, numberOfRuns * sizeof( ScriptRun ) );
123 mImpl->mGetScriptCache.characterIndex = 0u;
124 mImpl->mGetScriptCache.numberOfCharacters = 0u;
125 mImpl->mGetScriptCache.firstRun = 0u;
126 mImpl->mGetScriptCache.numberOfRuns = 0u;
129 Length LogicalModel::GetNumberOfScriptRuns( CharacterIndex characterIndex,
130 Length numberOfCharacters ) const
132 GetRunCache& scriptCache = mImpl->mGetScriptCache;
134 // Set the character index and the number of characters into the cache.
135 scriptCache.characterIndex = characterIndex;
136 scriptCache.numberOfCharacters = numberOfCharacters;
138 if( ( 0u == characterIndex ) &&
139 ( mImpl->mText.Count() == numberOfCharacters ) )
141 scriptCache.firstRun = 0u;
142 scriptCache.numberOfRuns = mImpl->mScriptRuns.Count();
143 return scriptCache.numberOfRuns;
146 // Initialize the number of scripts and the index to the first script.
147 scriptCache.firstRun = 0u;
148 scriptCache.numberOfRuns = 0;
149 bool firstScriptFound = false;
151 const Vector<ScriptRun>& modelScripts = mImpl->mScriptRuns;
152 const CharacterIndex lastCharacterIndex = characterIndex + numberOfCharacters;
154 // Traverse the scripts and count those scripts within the range of characters.
155 for( Vector<ScriptRun>::ConstIterator it = modelScripts.Begin(),
156 endIt = modelScripts.End();
160 const ScriptRun& script = *it;
162 if( ( script.characterRun.characterIndex + script.characterRun.numberOfCharacters > characterIndex ) &&
163 ( lastCharacterIndex > script.characterRun.characterIndex ) )
165 firstScriptFound = true;
166 ++scriptCache.numberOfRuns;
168 else if( lastCharacterIndex <= script.characterRun.characterIndex )
170 // nothing else to do.
174 if( !firstScriptFound )
176 ++scriptCache.firstRun;
180 return scriptCache.numberOfRuns;
183 void LogicalModel::GetScriptRuns( ScriptRun* scriptRuns,
184 CharacterIndex characterIndex,
185 Length numberOfCharacters ) const
187 const Vector<ScriptRun>& modelScripts = mImpl->mScriptRuns;
188 GetRunCache& scriptCache = mImpl->mGetScriptCache;
190 if( ( characterIndex != scriptCache.characterIndex ) ||
191 ( numberOfCharacters != scriptCache.numberOfCharacters ) )
193 GetNumberOfScriptRuns( characterIndex,
194 numberOfCharacters );
197 memcpy( scriptRuns, modelScripts.Begin() + scriptCache.firstRun, scriptCache.numberOfRuns * sizeof( ScriptRun ) );
200 Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
202 // If this operation is too slow, consider a binary search.
204 for( Length index = 0u, length = mImpl->mScriptRuns.Count(); index < length; ++index )
206 const ScriptRun* const scriptRun = mImpl->mScriptRuns.Begin() + index;
208 if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
209 ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
211 return scriptRun->script;
215 return TextAbstraction::UNKNOWN;
218 void LogicalModel::SetFonts( const FontRun* const fonts,
219 Length numberOfRuns )
221 Vector<FontRun>& fontRuns = mImpl->mFontRuns;
223 if( 0u == numberOfRuns )
229 fontRuns.Resize( numberOfRuns );
230 memcpy( fontRuns.Begin(), fonts, numberOfRuns * sizeof( FontRun ) );
233 mImpl->mGetFontCache.characterIndex = 0u;
234 mImpl->mGetFontCache.numberOfCharacters = 0u;
235 mImpl->mGetFontCache.firstRun = 0u;
236 mImpl->mGetFontCache.numberOfRuns = 0u;
239 Length LogicalModel::GetNumberOfFontRuns( CharacterIndex characterIndex,
240 Length numberOfCharacters ) const
242 GetRunCache& fontCache = mImpl->mGetFontCache;
244 // Set the character index and the number of characters into the cache.
245 fontCache.characterIndex = characterIndex;
246 fontCache.numberOfCharacters = numberOfCharacters;
248 if( ( 0u == characterIndex ) &&
249 ( mImpl->mText.Count() == numberOfCharacters ) )
251 fontCache.firstRun = 0u;
252 fontCache.numberOfRuns = mImpl->mFontRuns.Count();
253 return fontCache.numberOfRuns;
256 // Initialize the number of fonts and the index to the first font.
257 fontCache.firstRun = 0u;
258 fontCache.numberOfRuns = 0;
259 bool firstFontFound = false;
261 const Vector<FontRun>& modelFonts = mImpl->mFontRuns;
262 const CharacterIndex lastCharacterIndex = characterIndex + numberOfCharacters;
264 // Traverse the fonts and count those fonts within the range of characters.
265 for( Vector<FontRun>::ConstIterator it = modelFonts.Begin(),
266 endIt = modelFonts.End();
270 const FontRun& font = *it;
272 if( ( font.characterRun.characterIndex + font.characterRun.numberOfCharacters > characterIndex ) &&
273 ( characterIndex + numberOfCharacters > font.characterRun.characterIndex ) )
275 firstFontFound = true;
276 ++fontCache.numberOfRuns;
278 else if( lastCharacterIndex <= font.characterRun.characterIndex )
280 // nothing else to do.
284 if( !firstFontFound )
286 ++fontCache.firstRun;
290 return fontCache.numberOfRuns;
293 void LogicalModel::GetFontRuns( FontRun* fontRuns,
294 CharacterIndex characterIndex,
295 Length numberOfCharacters ) const
297 const Vector<FontRun>& modelFonts = mImpl->mFontRuns;
298 GetRunCache& fontCache = mImpl->mGetFontCache;
300 if( ( characterIndex != fontCache.characterIndex ) ||
301 ( numberOfCharacters != fontCache.numberOfCharacters ) )
303 GetNumberOfFontRuns( characterIndex,
304 numberOfCharacters );
307 memcpy( fontRuns, modelFonts.Begin() + fontCache.firstRun, fontCache.numberOfRuns * sizeof( FontRun ) );
310 FontId LogicalModel::GetFont( CharacterIndex characterIndex ) const
312 for( Length index = 0u, length = mImpl->mFontRuns.Count(); index < length; ++index )
314 const FontRun* const fontRun = mImpl->mFontRuns.Begin() + index;
316 if( ( fontRun->characterRun.characterIndex <= characterIndex ) &&
317 ( characterIndex < fontRun->characterRun.characterIndex + fontRun->characterRun.numberOfCharacters ) )
319 return fontRun->fontId;
326 void LogicalModel::SetLineBreakInfo( const LineBreakInfo* const lineBreakInfo,
329 Vector<LineBreakInfo>& modelLineBreakInfo = mImpl->mLineBreakInfo;
333 modelLineBreakInfo.Clear();
337 modelLineBreakInfo.Resize( length );
338 memcpy( modelLineBreakInfo.Begin(), lineBreakInfo, length * sizeof( LineBreakInfo ) );
342 void LogicalModel::GetLineBreakInfo( LineBreakInfo* lineBreakInfo,
343 CharacterIndex characterIndex,
344 Length numberOfItems ) const
346 memcpy( lineBreakInfo, mImpl->mLineBreakInfo.Begin() + characterIndex, numberOfItems * sizeof( LineBreakInfo ) );
349 LineBreakInfo LogicalModel::GetLineBreakInfo( CharacterIndex characterIndex ) const
351 return *( mImpl->mLineBreakInfo.Begin() + characterIndex );
354 void LogicalModel::SetWordBreakInfo( const WordBreakInfo* const wordBreakInfo,
357 Vector<WordBreakInfo>& modelWordBreakInfo = mImpl->mWordBreakInfo;
361 modelWordBreakInfo.Clear();
365 modelWordBreakInfo.Resize( length );
366 memcpy( modelWordBreakInfo.Begin(), wordBreakInfo, length * sizeof( WordBreakInfo ) );
370 void LogicalModel::GetWordBreakInfo( WordBreakInfo* wordBreakInfo,
371 CharacterIndex characterIndex,
372 Length numberOfItems ) const
374 memcpy( wordBreakInfo, mImpl->mWordBreakInfo.Begin() + characterIndex, numberOfItems * sizeof( WordBreakInfo ) );
377 WordBreakInfo LogicalModel::GetWordBreakInfo( CharacterIndex characterIndex ) const
379 return *( mImpl->mWordBreakInfo.Begin() + characterIndex );
382 void LogicalModel::SetBidirectionalInfo( const BidirectionalParagraphInfoRun* const bidirectionalInfo,
383 Length numberOfRuns )
385 Vector<BidirectionalParagraphInfoRun>& modelBidirectionalParagraphInfo = mImpl->mBidirectionalParagraphInfo;
387 if( 0u == numberOfRuns )
389 modelBidirectionalParagraphInfo.Clear();
393 modelBidirectionalParagraphInfo.Resize( numberOfRuns );
394 memcpy( modelBidirectionalParagraphInfo.Begin(), bidirectionalInfo, numberOfRuns * sizeof( BidirectionalParagraphInfoRun ) );
397 mImpl->mGetBidirectionalCache.characterIndex = 0u;
398 mImpl->mGetBidirectionalCache.numberOfCharacters = 0u;
399 mImpl->mGetBidirectionalCache.firstRun = 0u;
400 mImpl->mGetBidirectionalCache.numberOfRuns = 0u;
403 Length LogicalModel::GetNumberOfBidirectionalInfoRuns( CharacterIndex characterIndex,
404 Length numberOfCharacters ) const
406 GetRunCache& bidiCache = mImpl->mGetBidirectionalCache;
408 // Set the character index and the number of characters into the cache.
409 bidiCache.characterIndex = characterIndex;
410 bidiCache.numberOfCharacters = numberOfCharacters;
412 if( ( 0u == characterIndex ) &&
413 ( mImpl->mText.Count() == numberOfCharacters ) )
415 bidiCache.firstRun = 0u;
416 bidiCache.numberOfRuns = mImpl->mBidirectionalParagraphInfo.Count();
417 return bidiCache.numberOfRuns;
420 // Initialize the number of bidi paragraphs and the index to the first paragraph.
421 bidiCache.firstRun = 0u;
422 bidiCache.numberOfRuns = 0;
423 bool firstParagraphFound = false;
425 const Vector<BidirectionalParagraphInfoRun>& modelBidirectionalParagraphInfo = mImpl->mBidirectionalParagraphInfo;
427 // Traverse the bidirectional paragraph info and count those bidi paragraphs within the range of characters.
428 for( Vector<BidirectionalParagraphInfoRun>::ConstIterator it = modelBidirectionalParagraphInfo.Begin(),
429 endIt = modelBidirectionalParagraphInfo.End();
433 const BidirectionalParagraphInfoRun& bidi = *it;
435 if( ( bidi.characterRun.characterIndex + bidi.characterRun.numberOfCharacters > characterIndex ) &&
436 ( characterIndex + numberOfCharacters > bidi.characterRun.characterIndex ) )
438 firstParagraphFound = true;
439 ++bidiCache.numberOfRuns;
442 if( !firstParagraphFound )
444 ++bidiCache.firstRun;
448 return bidiCache.numberOfRuns;
451 void LogicalModel::GetBidirectionalInfo( BidirectionalParagraphInfoRun* bidirectionalInfo,
452 CharacterIndex characterIndex,
453 Length numberOfCharacters ) const
455 const Vector<BidirectionalParagraphInfoRun>& modelBidirectionalParagraphInfo = mImpl->mBidirectionalParagraphInfo;
456 GetRunCache& bidiCache = mImpl->mGetBidirectionalCache;
458 if( ( characterIndex != bidiCache.characterIndex ) ||
459 ( numberOfCharacters != bidiCache.numberOfCharacters ) )
461 GetNumberOfBidirectionalInfoRuns( characterIndex,
462 numberOfCharacters );
465 memcpy( bidirectionalInfo, modelBidirectionalParagraphInfo.Begin() + bidiCache.firstRun, bidiCache.numberOfRuns * sizeof( BidirectionalParagraphInfoRun ) );
468 void LogicalModel::GetCharacterDirections( CharacterDirection* directions,
469 CharacterIndex characterIndex,
470 Length numberOfCharacters ) const
474 CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex characterIndex ) const
479 void LogicalModel::SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
480 Length numberOfRuns )
482 Vector<CharacterIndex>& modelVisualToLogicalMap = mImpl->mVisualToLogicalMap;
483 Vector<CharacterIndex>& modelLogicalToVisualMap = mImpl->mLogicalToVisualMap;
485 if( 0u == numberOfRuns )
487 modelVisualToLogicalMap.Clear();
488 modelLogicalToVisualMap.Clear();
492 const Length numberOfCharacters = mImpl->mText.Count();
493 modelVisualToLogicalMap.Resize( numberOfCharacters );
494 modelLogicalToVisualMap.Resize( numberOfCharacters );
496 CharacterIndex* modelVisualToLogicalMapBuffer = modelVisualToLogicalMap.Begin();
497 CharacterIndex* modelLogicalToVisualMapBuffer = modelLogicalToVisualMap.Begin();
499 CharacterIndex lastIndex = 0u;
500 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
502 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
504 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
506 // Fill with the identity.
507 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
509 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
513 // Fill the conversion table of the run.
514 for( CharacterIndex index = 0u;
515 index < bidiLineInfo.characterRun.numberOfCharacters;
516 ++index, ++lastIndex )
518 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
522 // Complete with the identity if there are some left to right characters after the last right to left.
523 for( ; lastIndex < numberOfCharacters; ++lastIndex )
525 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
528 // Sets the logical to visual conversion map.
529 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
531 *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
536 CharacterIndex LogicalModel::GetVisualCharacterIndex( CharacterIndex logicalCharacterIndex ) const
538 if( 0u == mImpl->mLogicalToVisualMap.Count() )
540 // If there is no logical to visual info is because the whole text is left to right.
541 // Return the identity.
542 return logicalCharacterIndex;
545 return *( mImpl->mLogicalToVisualMap.Begin() + logicalCharacterIndex );
548 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
550 if( 0u == mImpl->mVisualToLogicalMap.Count() )
552 // If there is no visual to logical info is because the whole text is left to right.
553 // Return the identity.
554 return visualCharacterIndex;
557 return *( mImpl->mVisualToLogicalMap.Begin() + visualCharacterIndex );
560 void LogicalModel::GetLogicalToVisualMap( CharacterIndex* logicalToVisualMap,
561 CharacterIndex characterIndex,
562 Length numberOfCharacters ) const
564 memcpy( logicalToVisualMap, mImpl->mLogicalToVisualMap.Begin() + characterIndex, numberOfCharacters * sizeof( CharacterIndex ) );
567 void LogicalModel::GetVisualToLogicalMap( CharacterIndex* visualToLogicalMap,
568 CharacterIndex characterIndex,
569 Length numberOfCharacters ) const
571 memcpy( visualToLogicalMap, mImpl->mVisualToLogicalMap.Begin() + characterIndex, numberOfCharacters * sizeof( CharacterIndex ) );
574 LogicalModel::~LogicalModel()
579 LogicalModel::LogicalModel()
582 mImpl = new LogicalModel::Impl();
587 } // namespace Toolkit