X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Fpublic-api%2Ftext%2Fshaper.cpp;h=54444ace5a6000c98666243cdd66facc7e6a669a;hp=3be21ab0976dfde95088cb7b582ff69bc5601635;hb=8363c13a95006c0cbfe3074e9b601bdaef9537f5;hpb=2dd044328238768ae8b27a223cb7d0f5cda53513 diff --git a/dali-toolkit/public-api/text/shaper.cpp b/dali-toolkit/public-api/text/shaper.cpp index 3be21ab..54444ac 100644 --- a/dali-toolkit/public-api/text/shaper.cpp +++ b/dali-toolkit/public-api/text/shaper.cpp @@ -18,6 +18,13 @@ // CLASS HEADER #include +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + namespace Dali { @@ -27,6 +34,12 @@ namespace Toolkit namespace Text { +CharacterIndex min( CharacterIndex index0, + CharacterIndex index1 ) +{ + return ( index0 < index1 ) ? index0 : index1; +} + void ShapeText( const Vector& text, const Vector& lineBreakInfo, const Vector& scripts, @@ -35,6 +48,129 @@ void ShapeText( const Vector& text, Vector& characterIndices, Vector& charactersPerGlyph ) { + const Length numberOfCharacters = text.Count(); + + if( 0u == numberOfCharacters ) + { + // Nothing to do if there are no characters. + return; + } + + const Length numberOfFontRuns = fonts.Count(); + + DALI_ASSERT_DEBUG( ( 0u != numberOfFontRuns ) && + ( numberOfCharacters == fonts[numberOfFontRuns - 1u].characterRun.characterIndex + fonts[numberOfFontRuns - 1u].characterRun.numberOfCharacters ) && + "Toolkit::Text::ShapeText. All characters must have a font set." ); + + const Length numberOfScriptRuns = scripts.Count(); + + DALI_ASSERT_DEBUG( ( 0u != numberOfScriptRuns ) && + ( numberOfCharacters == scripts[numberOfScriptRuns - 1u].characterRun.characterIndex + scripts[numberOfScriptRuns - 1u].characterRun.numberOfCharacters ) && + "Toolkit::Text::ShapeText. All characters must have a script set." ); + + // The text needs to be split in chunks of consecutive characters. + // Each chunk must contain characters with the same font id and script set. + // A chunk of consecutive characters must not contain a LINE_MUST_BREAK, if there is one a new chunk have to be created. + + TextAbstraction::Shaping shaping = TextAbstraction::Shaping::Get(); + + // To shape the text a font and an script is needed. + Vector::ConstIterator fontRunIt = fonts.Begin(); + Vector::ConstIterator scriptRunIt = scripts.Begin(); + + // The line must break token converted to LineBreakInfo to be compared and avoid a compile error. + const LineBreakInfo MUST_BREAK = static_cast( TextAbstraction::LINE_MUST_BREAK ); + + // Index to the the next one to be shaped. Is pointing the character after the last one it was shaped. + CharacterIndex previousIndex = 0u; + + // The current font id and script used to shape the text. + FontId currentFontId = 0u; + Script currentScript = TextAbstraction::UNKNOWN; + + // Reserve some space to allocate the glyphs and the glyph to character map. + // There is no way to know the number of glyphs before shaping the text. + // To avoid reallocations it's reserved space for a slightly biger number of glyphs than the number of characters. + + Length numberOfGlyphsReserved = static_cast( numberOfCharacters * 1.3f ); + glyphs.Resize( numberOfGlyphsReserved ); + charactersPerGlyph.Resize( numberOfGlyphsReserved ); + + // The actual number of glyphs. + Length totalNumberOfGlyphs = 0u; + + // Traverse the characters and shape the text. + for( previousIndex = 0; previousIndex < numberOfCharacters; ) + { + // Get the font id and the script. + const FontRun& fontRun = *fontRunIt; + const ScriptRun& scriptRun = *scriptRunIt; + + currentFontId = fontRun.fontId; + currentScript = scriptRun.script; + + // Get the min index to the last character of both runs. + CharacterIndex currentIndex = min( fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters, + scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters ); + + // Check if there is a line must break. + for( CharacterIndex index = previousIndex; index < currentIndex; ++index ) + { + if( MUST_BREAK == lineBreakInfo.Begin() + index ) + { + currentIndex = index; + break; + } + } + + // Shape the text for the current chunk. + const Length numberOfGlyphs = shaping.Shape( text.Begin() + previousIndex, + currentIndex - previousIndex, + currentFontId, + currentScript ); + + const Length glyphIndex = totalNumberOfGlyphs; + totalNumberOfGlyphs += numberOfGlyphs; + + if( totalNumberOfGlyphs > numberOfGlyphsReserved ) + { + // Resize the vectors to get enough space. + numberOfGlyphsReserved = static_cast( totalNumberOfGlyphs * 1.3f ); + glyphs.Resize( numberOfGlyphsReserved ); + charactersPerGlyph.Resize( numberOfGlyphsReserved ); + } + + // Retrieve the glyphs and the glyph to character conversion map. + shaping.GetGlyphs( glyphs.Begin() + glyphIndex, + charactersPerGlyph.Begin() + glyphIndex ); + + // Update the iterators to get the next font or script run. + if( currentIndex == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) + { + ++fontRunIt; + } + if( currentIndex == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters ) + { + ++scriptRunIt; + } + + // Update the previous index. + previousIndex = currentIndex; + } + + characterIndices.Reserve( totalNumberOfGlyphs ); + CharacterIndex characterIndex = 0u; + characterIndices.PushBack( characterIndex ); + for( Length index = 0u, length = totalNumberOfGlyphs - 1u; index < length; ++index ) + { + characterIndex += *( charactersPerGlyph.Begin() + index ); + characterIndices.PushBack( characterIndex ); + } + + // Resize the vectors to set the right number of items. + glyphs.Resize( totalNumberOfGlyphs ); + // characterIndices.Resize( totalNumberOfGlyphs ); + charactersPerGlyph.Resize( totalNumberOfGlyphs ); } } // namespace Text