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/internal/text/text-abstraction/bidirectional-support-impl.h>
22 #include <dali/internal/system/common/singleton-service-impl.h>
26 #include <fribidi/fribidi.h>
27 #include <dali/integration-api/debug.h>
32 namespace TextAbstraction
40 typedef unsigned char BidiDirection;
42 // Internal charcter's direction.
43 const BidiDirection LEFT_TO_RIGHT = 0u;
44 const BidiDirection NEUTRAL = 1u;
45 const BidiDirection RIGHT_TO_LEFT = 2u;
48 * @param[in] paragraphDirection The FriBiDi paragraph's direction.
50 * @return Whether the paragraph is right to left.
52 bool GetBidiParagraphDirection( FriBidiParType paragraphDirection )
54 switch( paragraphDirection )
56 case FRIBIDI_PAR_RTL: // Right-To-Left paragraph.
57 case FRIBIDI_PAR_WRTL: // Weak Right To Left paragraph.
61 case FRIBIDI_PAR_LTR: // Left-To-Right paragraph.
62 case FRIBIDI_PAR_ON: // DirectiOn-Neutral paragraph.
63 case FRIBIDI_PAR_WLTR: // Weak Left To Right paragraph.
72 BidiDirection GetBidiCharacterDirection( FriBidiCharType characterDirection )
74 switch( characterDirection )
76 case FRIBIDI_TYPE_LTR: // Left-To-Right letter.
77 case FRIBIDI_TYPE_EN: // European Numeral.
78 case FRIBIDI_TYPE_AN: // Arabic Numeral.
79 case FRIBIDI_TYPE_ES: // European number Separator.
80 case FRIBIDI_TYPE_ET: // European number Terminator.
84 case FRIBIDI_TYPE_RTL: // Right-To-Left letter.
85 case FRIBIDI_TYPE_AL: // Arabic Letter.
95 struct BidirectionalSupport::Plugin
98 * Stores bidirectional info per paragraph.
100 struct BidirectionalInfo
102 FriBidiCharType* characterTypes; ///< The type of each character (right, left, neutral, ...)
103 FriBidiLevel* embeddedLevels; ///< Embedded levels.
104 FriBidiParType paragraphDirection; ///< The paragraph's direction.
108 : mParagraphBidirectionalInfo(),
114 // free all resources.
115 for( Vector<BidirectionalInfo*>::Iterator it = mParagraphBidirectionalInfo.Begin(),
116 endIt = mParagraphBidirectionalInfo.End();
120 BidirectionalInfo* info = *it;
122 free( info->embeddedLevels );
123 free( info->characterTypes );
128 BidiInfoIndex CreateInfo( const Character* const paragraph,
129 Length numberOfCharacters )
131 // Reserve memory for the paragraph's bidirectional info.
132 BidirectionalInfo* bidirectionalInfo = new BidirectionalInfo();
134 bidirectionalInfo->characterTypes = reinterpret_cast<FriBidiCharType*>( malloc( numberOfCharacters * sizeof( FriBidiCharType ) ) );
135 if( !bidirectionalInfo->characterTypes )
137 delete bidirectionalInfo;
141 bidirectionalInfo->embeddedLevels = reinterpret_cast<FriBidiLevel*>( malloc( numberOfCharacters * sizeof( FriBidiLevel ) ) );
142 if( !bidirectionalInfo->embeddedLevels )
144 free( bidirectionalInfo->characterTypes );
145 delete bidirectionalInfo;
149 // Retrieve the type of each character..
150 fribidi_get_bidi_types( paragraph, numberOfCharacters, bidirectionalInfo->characterTypes );
152 // Retrieve the paragraph's direction.
153 bidirectionalInfo->paragraphDirection = fribidi_get_par_direction( bidirectionalInfo->characterTypes, numberOfCharacters );
155 // Retrieve the embedding levels.
156 if (fribidi_get_par_embedding_levels( bidirectionalInfo->characterTypes, numberOfCharacters, &bidirectionalInfo->paragraphDirection, bidirectionalInfo->embeddedLevels ) == 0)
158 free( bidirectionalInfo->characterTypes );
159 delete bidirectionalInfo;
163 // Store the bidirectional info and return the index.
164 BidiInfoIndex index = 0u;
165 if( 0u != mFreeIndices.Count() )
167 Vector<BidiInfoIndex>::Iterator it = mFreeIndices.End() - 1u;
171 mFreeIndices.Remove( it );
173 *( mParagraphBidirectionalInfo.Begin() + index ) = bidirectionalInfo;
177 index = static_cast<BidiInfoIndex>( mParagraphBidirectionalInfo.Count() );
179 mParagraphBidirectionalInfo.PushBack( bidirectionalInfo );
185 void DestroyInfo( BidiInfoIndex bidiInfoIndex )
187 if( bidiInfoIndex >= mParagraphBidirectionalInfo.Count() )
192 // Retrieve the paragraph's bidirectional info.
193 Vector<BidirectionalInfo*>::Iterator it = mParagraphBidirectionalInfo.Begin() + bidiInfoIndex;
194 BidirectionalInfo* bidirectionalInfo = *it;
196 if( NULL != bidirectionalInfo )
198 // Free resources and destroy the container.
199 free( bidirectionalInfo->embeddedLevels );
200 free( bidirectionalInfo->characterTypes );
201 delete bidirectionalInfo;
206 // Add the index to the free indices vector.
207 mFreeIndices.PushBack( bidiInfoIndex );
210 void Reorder( BidiInfoIndex bidiInfoIndex,
211 CharacterIndex firstCharacterIndex,
212 Length numberOfCharacters,
213 CharacterIndex* visualToLogicalMap )
215 const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC;
217 // Retrieve the paragraph's bidirectional info.
218 const BidirectionalInfo* const bidirectionalInfo = *( mParagraphBidirectionalInfo.Begin() + bidiInfoIndex );
220 // Initialize the visual to logical mapping table to the identity. Otherwise fribidi_reorder_line fails to retrieve a valid mapping table.
221 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
223 visualToLogicalMap[ index ] = index;
226 // Copy embedded levels as fribidi_reorder_line() may change them.
227 const uint32_t embeddedLevelsSize = numberOfCharacters * sizeof( FriBidiLevel );
228 FriBidiLevel* embeddedLevels = reinterpret_cast<FriBidiLevel*>( malloc( embeddedLevelsSize ) );
231 memcpy( embeddedLevels, bidirectionalInfo->embeddedLevels + firstCharacterIndex, embeddedLevelsSize );
234 if (fribidi_reorder_line( flags,
235 bidirectionalInfo->characterTypes + firstCharacterIndex,
238 bidirectionalInfo->paragraphDirection,
241 reinterpret_cast<FriBidiStrIndex*>( visualToLogicalMap ) ) == 0)
243 DALI_LOG_ERROR("fribidi_reorder_line is failed\n");
247 free( embeddedLevels );
251 bool GetMirroredText( Character* text,
252 CharacterDirection* directions,
253 Length numberOfCharacters ) const
255 bool updated = false;
257 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
259 // Get a reference to the character inside the text.
260 Character& character = *( text + index );
262 // Retrieve the mirrored character.
263 FriBidiChar mirroredCharacter = character;
264 bool mirrored = false;
265 if( *( directions + index ) )
267 mirrored = fribidi_get_mirror_char( character, &mirroredCharacter );
269 updated = updated || mirrored;
271 // Update the character inside the text.
272 character = mirroredCharacter;
278 bool GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
280 // Retrieve the paragraph's bidirectional info.
281 const BidirectionalInfo* const bidirectionalInfo = *( mParagraphBidirectionalInfo.Begin() + bidiInfoIndex );
283 return GetBidiParagraphDirection( bidirectionalInfo->paragraphDirection );
286 void GetCharactersDirection( BidiInfoIndex bidiInfoIndex,
287 CharacterDirection* directions,
288 Length numberOfCharacters )
290 const BidirectionalInfo* const bidirectionalInfo = *( mParagraphBidirectionalInfo.Begin() + bidiInfoIndex );
292 const CharacterDirection paragraphDirection = GetBidiParagraphDirection( bidirectionalInfo->paragraphDirection );
293 CharacterDirection previousDirection = paragraphDirection;
295 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
297 CharacterDirection& characterDirection = *( directions + index );
298 characterDirection = false;
300 // Get the bidi direction.
301 const BidiDirection bidiDirection = GetBidiCharacterDirection( *( bidirectionalInfo->characterTypes + index ) );
303 if( RIGHT_TO_LEFT == bidiDirection )
305 characterDirection = true;
307 else if( NEUTRAL == bidiDirection )
309 // For neutral characters it check's the next and previous directions.
310 // If they are equals set that direction. If they are not, sets the paragraph's direction.
311 // If there is no next, sets the paragraph's direction.
313 CharacterDirection nextDirection = paragraphDirection;
315 // Look for the next non-neutral character.
316 Length nextIndex = index + 1u;
317 for( ; nextIndex < numberOfCharacters; ++nextIndex )
319 BidiDirection nextBidiDirection = GetBidiCharacterDirection( *( bidirectionalInfo->characterTypes + nextIndex ) );
320 if( nextBidiDirection != NEUTRAL )
322 nextDirection = RIGHT_TO_LEFT == nextBidiDirection;
327 // Calculate the direction for all the neutral characters.
328 characterDirection = previousDirection == nextDirection ? previousDirection : paragraphDirection;
330 // Set the direction to all the neutral characters.
332 for( ; index < nextIndex; ++index )
334 CharacterDirection& nextCharacterDirection = *( directions + index );
335 nextCharacterDirection = characterDirection;
338 // Set the direction of the next non-neutral character.
339 if( nextIndex < numberOfCharacters )
341 *( directions + nextIndex ) = nextDirection;
345 previousDirection = characterDirection;
349 Vector<BidirectionalInfo*> mParagraphBidirectionalInfo; ///< Stores the bidirectional info per paragraph.
350 Vector<BidiInfoIndex> mFreeIndices; ///< Stores indices of free positions in the bidirectional info vector.
353 BidirectionalSupport::BidirectionalSupport()
358 BidirectionalSupport::~BidirectionalSupport()
363 TextAbstraction::BidirectionalSupport BidirectionalSupport::Get()
365 TextAbstraction::BidirectionalSupport bidirectionalSupportHandle;
367 SingletonService service( SingletonService::Get() );
370 // Check whether the singleton is already created
371 BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::BidirectionalSupport ) );
374 // If so, downcast the handle
375 BidirectionalSupport* impl = dynamic_cast< Internal::BidirectionalSupport* >( handle.GetObjectPtr() );
376 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( impl );
378 else // create and register the object
380 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( new BidirectionalSupport );
381 service.Register( typeid( bidirectionalSupportHandle ), bidirectionalSupportHandle );
385 return bidirectionalSupportHandle;
388 BidiInfoIndex BidirectionalSupport::CreateInfo( const Character* const paragraph,
389 Length numberOfCharacters )
393 return mPlugin->CreateInfo( paragraph,
394 numberOfCharacters );
397 void BidirectionalSupport::DestroyInfo( BidiInfoIndex bidiInfoIndex )
401 mPlugin->DestroyInfo( bidiInfoIndex );
404 void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex,
405 CharacterIndex firstCharacterIndex,
406 Length numberOfCharacters,
407 CharacterIndex* visualToLogicalMap )
411 mPlugin->Reorder( bidiInfoIndex,
414 visualToLogicalMap );
417 bool BidirectionalSupport::GetMirroredText( Character* text,
418 CharacterDirection* directions,
419 Length numberOfCharacters )
423 return mPlugin->GetMirroredText( text, directions, numberOfCharacters );
426 bool BidirectionalSupport::GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
433 return mPlugin->GetParagraphDirection( bidiInfoIndex );
436 void BidirectionalSupport::GetCharactersDirection( BidiInfoIndex bidiInfoIndex,
437 CharacterDirection* directions,
438 Length numberOfCharacters )
442 mPlugin->GetCharactersDirection( bidiInfoIndex,
444 numberOfCharacters );
447 void BidirectionalSupport::CreatePlugin()
451 mPlugin = new Plugin();
455 } // namespace Internal
457 } // namespace TextAbstraction