X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=base%2Fdali-toolkit%2Fpublic-api%2Ftext%2Flogical-model.cpp;h=4547f996227916c9e38c29b4f724be333ad36c8d;hp=90082332489af63dcd82335e417d7df4e0d1fcf5;hb=e6119e0806fc7460e95d5569ff53fd1577214ae5;hpb=c803046c24398c2037a83d105a932976dede943f diff --git a/base/dali-toolkit/public-api/text/logical-model.cpp b/base/dali-toolkit/public-api/text/logical-model.cpp index 9008233..4547f99 100644 --- a/base/dali-toolkit/public-api/text/logical-model.cpp +++ b/base/dali-toolkit/public-api/text/logical-model.cpp @@ -18,8 +18,13 @@ // CLASS HEADER #include +// INTERNAL INCLUDES +#include +#include + // EXTERNAL INCLUDES -#include +#include +#include namespace Dali { @@ -33,6 +38,8 @@ namespace Text struct LogicalModel::Impl { Vector mText; + Vector mScriptRuns; + Vector mFontRuns; }; LogicalModelPtr LogicalModel::New() @@ -58,6 +65,174 @@ void LogicalModel::GetText( CharacterIndex characterIndex, Character* text, Leng memcpy( text, &modelText[characterIndex], numberOfCharacters*sizeof(Character) ); } +void LogicalModel::SetScripts( const ScriptRun* const scripts, + Length numberOfRuns ) +{ + Vector& scriptRuns = mImpl->mScriptRuns; + scriptRuns.Resize( numberOfRuns ); + memcpy( scriptRuns.Begin(), scripts, numberOfRuns * sizeof( ScriptRun ) ); +} + +Length LogicalModel::GetNumberOfScriptRuns( CharacterIndex characterIndex, + Length numberOfCharacters ) const +{ + if( ( 0u == characterIndex ) && ( mImpl->mText.Count() == numberOfCharacters ) ) + { + return mImpl->mScriptRuns.Count(); + } + + const CharacterIndex charcterEndIndex = characterIndex + numberOfCharacters; + Length numberOfScriptRuns = 0u; + bool firstIndexFound = false; + + for( Length index = 0u, length = mImpl->mScriptRuns.Count(); index < length; ++index ) + { + const ScriptRun* const scriptRun = mImpl->mScriptRuns.Begin() + index; + + if( !firstIndexFound && + ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) ) + { + // The character index is within this script run. + // Starts the counter of script runs. + firstIndexFound = true; + } + + if( firstIndexFound ) + { + ++numberOfScriptRuns; + if( scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters > charcterEndIndex ) + { + // This script run exceeds the given range. The number of scripts can be returned. + return numberOfScriptRuns; + } + } + } + + return numberOfScriptRuns; +} + +void LogicalModel::GetScriptRuns( ScriptRun* scriptRuns, + CharacterIndex characterIndex, + Length numberOfCharacters ) const +{ + // A better implementation can cache the first script run and the number of then when the GetNumberOfScriptRuns() is called. + + Length numberOfScriptRuns = GetNumberOfScriptRuns( characterIndex, + numberOfCharacters ); + + for( Length index = 0u, length = mImpl->mScriptRuns.Count(); index < length; ++index ) + { + const ScriptRun* const scriptRun = mImpl->mScriptRuns.Begin() + index; + + if( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) + { + memcpy( scriptRuns, scriptRun, sizeof( ScriptRun ) * numberOfScriptRuns ); + return; + } + } +} + +Script LogicalModel::GetScript( CharacterIndex characterIndex ) const +{ + // If this operation is too slow, consider a binary search. + + for( Length index = 0u, length = mImpl->mScriptRuns.Count(); index < length; ++index ) + { + const ScriptRun* const scriptRun = mImpl->mScriptRuns.Begin() + index; + + if( ( scriptRun->characterRun.characterIndex <= characterIndex ) && + ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) ) + { + return scriptRun->script; + } + } + + return TextAbstraction::UNKNOWN; +} + +void LogicalModel::SetFonts( const FontRun* const fonts, + Length numberOfRuns ) +{ + Vector& fontRuns = mImpl->mFontRuns; + fontRuns.Resize( numberOfRuns ); + memcpy( fontRuns.Begin(), fonts, numberOfRuns * sizeof( FontRun ) ); +} + +Length LogicalModel::GetNumberOfFontRuns( CharacterIndex characterIndex, + Length numberOfCharacters ) const +{ + if( ( 0u == characterIndex ) && ( mImpl->mText.Count() == numberOfCharacters ) ) + { + return mImpl->mFontRuns.Count(); + } + + const CharacterIndex charcterEndIndex = characterIndex + numberOfCharacters; + Length numberOfFontRuns = 0u; + bool firstIndexFound = false; + + for( Length index = 0u, length = mImpl->mFontRuns.Count(); index < length; ++index ) + { + const FontRun* const fontRun = mImpl->mFontRuns.Begin() + index; + + if( !firstIndexFound && + ( characterIndex < fontRun->characterRun.characterIndex + fontRun->characterRun.numberOfCharacters ) ) + { + // The character index is within this font run. + // Starts the counter of font runs. + firstIndexFound = true; + } + + if( firstIndexFound ) + { + ++numberOfFontRuns; + if( fontRun->characterRun.characterIndex + fontRun->characterRun.numberOfCharacters > charcterEndIndex ) + { + // This font run exceeds the given range. The number of fonts can be returned. + return numberOfFontRuns; + } + } + } + + return numberOfFontRuns; +} + +void LogicalModel::GetFontRuns( FontRun* fontRuns, + CharacterIndex characterIndex, + Length numberOfCharacters ) const +{ + // A better implementation can cache the first font run and the number of then when the GetNumberOfFontRuns() is called. + + Length numberOfFontRuns = GetNumberOfFontRuns( characterIndex, + numberOfCharacters ); + + for( Length index = 0u, length = mImpl->mFontRuns.Count(); index < length; ++index ) + { + const FontRun* const fontRun = mImpl->mFontRuns.Begin() + index; + + if( characterIndex < fontRun->characterRun.characterIndex + fontRun->characterRun.numberOfCharacters ) + { + memcpy( fontRuns, fontRun, sizeof( FontRun ) * numberOfFontRuns ); + return; + } + } +} + +FontId LogicalModel::GetFont( CharacterIndex characterIndex ) const +{ + for( Length index = 0u, length = mImpl->mFontRuns.Count(); index < length; ++index ) + { + const FontRun* const fontRun = mImpl->mFontRuns.Begin() + index; + + if( ( fontRun->characterRun.characterIndex <= characterIndex ) && + ( characterIndex < fontRun->characterRun.characterIndex + fontRun->characterRun.numberOfCharacters ) ) + { + return fontRun->fontId; + } + } + + return 0u; +} + LogicalModel::~LogicalModel() { delete mImpl;