From: Victor Cebollada Date: Mon, 19 Jan 2015 17:37:26 +0000 (+0000) Subject: Bidi implementation X-Git-Tag: new_text_0.1~29 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;ds=sidebyside;h=c3b189a851b7d9f1d82c0aedab89dc9aa1cdd560;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git Bidi implementation Change-Id: Iff1fa9d196766ba9b969503ecc2fc08b9e64588e Signed-off-by: Victor Cebollada --- diff --git a/text/dali/internal/text-abstraction/bidirectional-support-impl.cpp b/text/dali/internal/text-abstraction/bidirectional-support-impl.cpp index b2e4247..bfe67ff 100644 --- a/text/dali/internal/text-abstraction/bidirectional-support-impl.cpp +++ b/text/dali/internal/text-abstraction/bidirectional-support-impl.cpp @@ -21,6 +21,10 @@ // INTERNAL INCLUDES #include +// EXTERNAL INCLUDES +#include +#include + namespace Dali { @@ -30,15 +34,34 @@ namespace TextAbstraction namespace Internal { -BidirectionalSupport::BidirectionalSupport() -: mPlugin( NULL ) +struct BidirectionalSupport::BidirectionalInfo { + FriBidiCharType* characterTypes; ///< The type of each character (right, left, neutral, ...) + FriBidiLevel* embeddedLevels; ///< Embedded levels. + FriBidiParType paragraphDirection; ///< The paragraph's direction. +}; +BidirectionalSupport::BidirectionalSupport() +: mPlugin( NULL ), + mParagraphBidirectionalInfo(), + mFreeIndices() +{ } BidirectionalSupport::~BidirectionalSupport() { + // free all resources. + for( Vector::Iterator it = mParagraphBidirectionalInfo.Begin(), + endIt = mParagraphBidirectionalInfo.End(); + it != endIt; + ++it ) + { + BidirectionalInfo* info = *it; + free( info->embeddedLevels ); + free( info->characterTypes ); + delete info; + } } TextAbstraction::BidirectionalSupport BidirectionalSupport::Get() @@ -69,11 +92,67 @@ TextAbstraction::BidirectionalSupport BidirectionalSupport::Get() BidiInfoIndex BidirectionalSupport::CreateInfo( const Character* const paragraph, Length numberOfCharacters ) { - return 0u; + // Reserve memory for the paragraph's bidirectional info. + BidirectionalInfo* bidirectionalInfo = new BidirectionalInfo(); + + bidirectionalInfo->characterTypes = reinterpret_cast( malloc( numberOfCharacters * sizeof( FriBidiCharType ) ) ); + bidirectionalInfo->embeddedLevels = reinterpret_cast( malloc( numberOfCharacters * sizeof( FriBidiLevel ) ) ); + + // Retrieve the type of each character.. + fribidi_get_bidi_types( paragraph, numberOfCharacters, bidirectionalInfo->characterTypes ); + + // Retrieve the paragraph's direction. + bidirectionalInfo->paragraphDirection = fribidi_get_par_direction( paragraph, numberOfCharacters ); + + // Retrieve the embedding levels. + fribidi_get_par_embedding_levels( paragraph, numberOfCharacters, &bidirectionalInfo->paragraphDirection, bidirectionalInfo->embeddedLevels ); + + // Store the bidirectional info and return the index. + BidiInfoIndex index = 0u; + const std::size_t numberOfItems = mFreeIndices.Count(); + if( numberOfItems != 0u ) + { + Vector::Iterator it = mFreeIndices.End() - 1u; + + index = *it; + + mFreeIndices.Remove( it ); + + *( mParagraphBidirectionalInfo.Begin() + index ) = bidirectionalInfo; + } + else + { + index = static_cast( numberOfItems ); + + mParagraphBidirectionalInfo.PushBack( bidirectionalInfo ); + } + + return index; } void BidirectionalSupport::DestroyInfo( BidiInfoIndex bidiInfoIndex ) { + if( bidiInfoIndex >= mParagraphBidirectionalInfo.Count() ) + { + return; + } + + // Retrieve the paragraph's bidirectional info. + Vector::Iterator it = mParagraphBidirectionalInfo.Begin() + bidiInfoIndex; + BidirectionalInfo* bidirectionalInfo = *it; + + if( NULL != bidirectionalInfo ) + { + // Free resources and destroy the container. + free( bidirectionalInfo->embeddedLevels ); + free( bidirectionalInfo->characterTypes ); + delete bidirectionalInfo; + + *it = NULL; + } + + // Add the index to the free indices vector. + mFreeIndices.PushBack( bidiInfoIndex ); } void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex, @@ -81,6 +160,34 @@ void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex, Length numberOfCharacters, CharacterIndex* visualToLogicalMap ) { + const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC; + + // Retrieve the paragraph's bidirectional info. + const BidirectionalInfo* const bidirectionalInfo = *( mParagraphBidirectionalInfo.Begin() + bidiInfoIndex ); + + // Initialize the visual to logical mapping table to the identity. Otherwise fribidi_reorder_line fails to retrieve a valid mapping table. + for( CharacterIndex index = 0u; index < numberOfCharacters; ++index ) + { + visualToLogicalMap[ index ] = index; + } + + // Copy embedded levels as fribidi_reorder_line() may change them. + const uint32_t embeddedLevelsSize = numberOfCharacters * sizeof( FriBidiLevel ); + FriBidiLevel* embeddedLevels = reinterpret_cast( malloc( embeddedLevelsSize ) ); + memcpy( embeddedLevels, bidirectionalInfo->embeddedLevels + firstCharacterIndex, embeddedLevelsSize ); + + // Reorder the line. + fribidi_reorder_line( flags, + bidirectionalInfo->characterTypes + firstCharacterIndex, + numberOfCharacters, + 0u, + bidirectionalInfo->paragraphDirection, + embeddedLevels, + NULL, + reinterpret_cast( visualToLogicalMap ) ); + + // Free resources. + free( embeddedLevels ); } } // namespace Internal diff --git a/text/dali/internal/text-abstraction/bidirectional-support-impl.h b/text/dali/internal/text-abstraction/bidirectional-support-impl.h index d4ff5b3..e207d1d 100644 --- a/text/dali/internal/text-abstraction/bidirectional-support-impl.h +++ b/text/dali/internal/text-abstraction/bidirectional-support-impl.h @@ -36,7 +36,6 @@ namespace Internal /** * Implementation of the BidirectionalSupport */ - class BidirectionalSupport : public Dali::BaseObject { public: @@ -85,6 +84,16 @@ private: void* mPlugin; ///< TODO replace this with bidirectional support plugin +private: + + /** + * Stores bidirectional info per paragraph. + */ + struct BidirectionalInfo; + + Vector mParagraphBidirectionalInfo; ///< Stores the bidirectional info per paragraph. + Vector mFreeIndices; ///< Stores indices of free positions in the bidirectional info vector. + }; // class BidirectionalSupport } // namespace Internal diff --git a/text/dali/public-api/text-abstraction/bidirectional-support.h b/text/dali/public-api/text-abstraction/bidirectional-support.h index c95b698..d05af44 100644 --- a/text/dali/public-api/text-abstraction/bidirectional-support.h +++ b/text/dali/public-api/text-abstraction/bidirectional-support.h @@ -37,11 +37,6 @@ class BidirectionalSupport; } // Internal -} // TextAbstraction - -namespace TextAbstraction -{ - /** * BidirectionalSupport API *