2 * Copyright (c) 2016 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.
17 #include <dali/devel-api/text-abstraction/bidirectional-support.h>
18 #include <dali/devel-api/text-abstraction/font-client.h>
19 #include <dali/devel-api/text-abstraction/font-metrics.h>
20 #include <dali/devel-api/text-abstraction/glyph-info.h>
21 #include <dali/devel-api/text-abstraction/script.h>
22 #include <dali/devel-api/text-abstraction/segmentation.h>
23 #include <dali/devel-api/text-abstraction/shaping.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/devel-api/adaptor-framework/singleton-service.h>
34 namespace TextAbstraction
39 class BidirectionalSupport : public BaseObject
42 BidirectionalSupport()
46 ~BidirectionalSupport()
50 static TextAbstraction::BidirectionalSupport Get()
52 TextAbstraction::BidirectionalSupport bidirectionalSupportHandle;
54 Dali::SingletonService service( Dali::SingletonService::Get() );
57 // Check whether the singleton is already created
58 BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::BidirectionalSupport ) );
61 // If so, downcast the handle
62 BidirectionalSupport* impl = dynamic_cast< Internal::BidirectionalSupport* >( handle.GetObjectPtr() );
63 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( impl );
65 else // create and register the object
67 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( new BidirectionalSupport );
68 service.Register( typeid( bidirectionalSupportHandle ), bidirectionalSupportHandle );
71 return bidirectionalSupportHandle;
73 BidiInfoIndex CreateInfo( const Character* const paragraph, Length numberOfCharacters ){return 0;}
75 void DestroyInfo( BidiInfoIndex bidiInfoIndex )
79 void Reorder( BidiInfoIndex bidiInfoIndex,CharacterIndex firstCharacterIndex,Length numberOfCharacters,CharacterIndex* visualToLogicalMap )
83 bool GetMirroredText( Character* text,CharacterDirection* directions,Length numberOfCharacters )
88 bool GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
93 void GetCharactersDirection( BidiInfoIndex bidiInfoIndex, CharacterDirection* directions, Length numberOfCharacters )
96 }; // class BidirectionalSupport
99 class FontClient : public BaseObject
103 : mDpiHorizontal( 0 ),
110 static Dali::TextAbstraction::FontClient Get()
112 Dali::TextAbstraction::FontClient fontClientHandle;
114 Dali::SingletonService service( SingletonService::Get() );
117 // Check whether the singleton is already created
118 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::FontClient ) );
121 // If so, downcast the handle
122 FontClient* impl = dynamic_cast< Dali::TextAbstraction::Internal::FontClient* >( handle.GetObjectPtr() );
123 fontClientHandle = Dali::TextAbstraction::FontClient( impl );
125 else // create and register the object
127 fontClientHandle = Dali::TextAbstraction::FontClient( new FontClient );
128 service.Register( typeid( fontClientHandle ), fontClientHandle );
132 return fontClientHandle;
135 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi ){}
136 void GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi ){horizontalDpi=verticalDpi=96;}
138 void ResetSystemDefaults(){}
139 void GetDefaultFonts( FontList& defaultFonts ){}
140 void GetDefaultPlatformFontDescription( FontDescription& fontDescription ){}
141 void GetSystemFonts( FontList& systemFonts ){}
142 void GetDescription( FontId id, FontDescription& fontDescription ){}
143 PointSize26Dot6 GetPointSize( FontId id ){return 9;}
144 FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor ){return 0;}
145 FontId FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 pointSize, bool preferColor ){return 0;}
146 FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex ){return 0;}
147 FontId GetFontId( const FontDescription& fontDescription,PointSize26Dot6 pointSize, FaceIndex faceIndex ){return 0;}
148 bool IsScalable( const FontPath& path ){return true;}
149 bool IsScalable( const FontDescription& fontDescription ){return true;}
150 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes ){}
151 void GetFixedSizes( const FontDescription& fontDescription, Dali::Vector< PointSize26Dot6 >& sizes ){}
152 void GetFontMetrics( FontId fontId, FontMetrics& metrics ){}
153 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode ){return 0;}
154 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal ){return true;}
155 BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex ){return BufferImage();}
156 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob,
157 unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
161 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize ){return mGlyphInfo;}
163 unsigned int mDpiHorizontal;
164 unsigned int mDpiVertical;
165 GlyphInfo mGlyphInfo;
166 }; // class FontClient
169 class Shaping : public BaseObject
172 Shaping() : mText(NULL)
180 static Dali::TextAbstraction::Shaping Get()
182 Dali::TextAbstraction::Shaping shapingHandle;
184 Dali::SingletonService service( SingletonService::Get() );
187 // Check whether the singleton is already created
188 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::Shaping ) );
191 // If so, downcast the handle
192 Shaping* impl = dynamic_cast< Dali::TextAbstraction::Internal::Shaping* >( handle.GetObjectPtr() );
193 shapingHandle = Dali::TextAbstraction::Shaping( impl );
195 else // create and register the object
197 shapingHandle = Dali::TextAbstraction::Shaping( new Shaping );
198 service.Register( typeid( shapingHandle ), shapingHandle );
201 return shapingHandle;
204 void GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
206 // Return store & mapping table (0, 1, 2, 3... N-1))
209 memcpy( glyphStore, mText, mNumChars );
211 for( unsigned int i=0; i<mNumChars ; ++i )
217 Length Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
219 mText = new unsigned char[numChars];
220 mNumChars = numChars;
222 memcpy( mText, text, numChars );
227 unsigned char* mText;
228 unsigned int mNumChars;
234 inline static TextAbstraction::Internal::BidirectionalSupport& GetImplementation( TextAbstraction::BidirectionalSupport& bidirectionalSupport )
236 DALI_ASSERT_ALWAYS( bidirectionalSupport && "bidirectional support handle is empty" );
237 BaseObject& object = bidirectionalSupport.GetBaseObject();
238 return static_cast<TextAbstraction::Internal::BidirectionalSupport&>(object);
241 inline static const TextAbstraction::Internal::BidirectionalSupport& GetImplementation( const TextAbstraction::BidirectionalSupport& bidirectionalSupport )
243 DALI_ASSERT_ALWAYS( bidirectionalSupport && "bidirectional support handle is empty" );
244 const BaseObject& object = bidirectionalSupport.GetBaseObject();
245 return static_cast<const TextAbstraction::Internal::BidirectionalSupport&>(object);
248 inline static TextAbstraction::Internal::FontClient& GetImplementation(TextAbstraction::FontClient& fontClient)
250 DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
251 BaseObject& handle = fontClient.GetBaseObject();
252 return static_cast<TextAbstraction::Internal::FontClient&>(handle);
255 inline static const TextAbstraction::Internal::FontClient& GetImplementation(const TextAbstraction::FontClient& fontClient)
257 DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
258 const BaseObject& handle = fontClient.GetBaseObject();
259 return static_cast<const TextAbstraction::Internal::FontClient&>(handle);
262 inline static TextAbstraction::Internal::Shaping& GetImplementation(TextAbstraction::Shaping& shaping)
264 DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
265 BaseObject& handle = shaping.GetBaseObject();
266 return static_cast<TextAbstraction::Internal::Shaping&>(handle);
269 inline static const TextAbstraction::Internal::Shaping& GetImplementation(const TextAbstraction::Shaping& shaping)
271 DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
272 const BaseObject& handle = shaping.GetBaseObject();
273 return static_cast<const TextAbstraction::Internal::Shaping&>(handle);
277 ////////////////////////////////////////////////////////////////////////////////
278 /******************************************************************************/
280 namespace TextAbstraction
282 const PointSize26Dot6 FontClient::DEFAULT_POINT_SIZE = 768u; // 12*64
284 BidirectionalSupport::BidirectionalSupport()
288 BidirectionalSupport::~BidirectionalSupport()
292 BidirectionalSupport::BidirectionalSupport( Internal::BidirectionalSupport* implementation )
293 : BaseHandle( implementation )
297 BidirectionalSupport BidirectionalSupport::Get()
299 return Internal::BidirectionalSupport::Get();
302 BidiInfoIndex BidirectionalSupport::CreateInfo( const Character* const paragraph,
303 Length numberOfCharacters )
305 return GetImplementation( *this ).CreateInfo( paragraph, numberOfCharacters );
308 void BidirectionalSupport::DestroyInfo( BidiInfoIndex bidiInfoIndex )
310 GetImplementation( *this ).DestroyInfo( bidiInfoIndex );
313 void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex,
314 CharacterIndex firstCharacterIndex,
315 Length numberOfCharacters,
316 CharacterIndex* visualToLogicalMap )
318 GetImplementation( *this ).Reorder( bidiInfoIndex, firstCharacterIndex, numberOfCharacters, visualToLogicalMap );
321 bool BidirectionalSupport::GetMirroredText( Character* text,
322 CharacterDirection* directions,
323 Length numberOfCharacters )
325 return GetImplementation( *this ).GetMirroredText( text, directions, numberOfCharacters );
328 bool BidirectionalSupport::GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
330 return GetImplementation( *this ).GetParagraphDirection( bidiInfoIndex );
333 void BidirectionalSupport::GetCharactersDirection( BidiInfoIndex bidiInfoIndex,
334 CharacterDirection* directions,
335 Length numberOfCharacters )
337 GetImplementation( *this ).GetCharactersDirection( bidiInfoIndex, directions, numberOfCharacters );
341 FontClient FontClient::Get()
343 return Internal::FontClient::Get();
346 FontClient::FontClient()
350 FontClient::~FontClient()
354 FontClient::FontClient( const FontClient& handle )
355 : BaseHandle( handle )
359 FontClient& FontClient::operator=( const FontClient& handle )
361 BaseHandle::operator=( handle );
365 void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi )
367 GetImplementation(*this).SetDpi( horizontalDpi, verticalDpi );
370 void FontClient::GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi )
372 GetImplementation(*this).GetDpi( horizontalDpi, verticalDpi );
375 void FontClient::ResetSystemDefaults()
377 GetImplementation(*this).ResetSystemDefaults();
380 void FontClient::GetDefaultFonts( FontList& defaultFonts )
382 GetImplementation(*this).GetDefaultFonts( defaultFonts );
385 void FontClient::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
387 GetImplementation(*this).GetDefaultPlatformFontDescription( fontDescription );
390 void FontClient::GetSystemFonts( FontList& systemFonts )
392 GetImplementation(*this).GetSystemFonts( systemFonts );
395 void FontClient::GetDescription( FontId id, FontDescription& fontDescription )
397 GetImplementation(*this).GetDescription( id, fontDescription );
400 PointSize26Dot6 FontClient::GetPointSize( FontId id )
402 return GetImplementation(*this).GetPointSize( id );
405 FontId FontClient::FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor )
407 return GetImplementation(*this).FindDefaultFont( charcode, pointSize, preferColor );
410 FontId FontClient::FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 pointSize, bool preferColor )
412 return GetImplementation(*this).FindFallbackFont( preferredFont, charcode, pointSize, preferColor );
415 FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
417 return GetImplementation(*this).GetFontId( path, pointSize, faceIndex );
420 FontId FontClient::GetFontId( const FontDescription& fontDescription,
421 PointSize26Dot6 pointSize,
422 FaceIndex faceIndex )
424 return GetImplementation(*this).GetFontId( fontDescription,
429 bool FontClient::IsScalable( const FontPath& path )
431 return GetImplementation(*this).IsScalable( path );
434 bool FontClient::IsScalable( const FontDescription& fontDescription )
436 return GetImplementation(*this).IsScalable( fontDescription );
439 void FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes )
441 GetImplementation(*this).GetFixedSizes( path, sizes );
444 void FontClient::GetFixedSizes( const FontDescription& fontDescription,
445 Dali::Vector< PointSize26Dot6 >& sizes )
447 GetImplementation(*this).GetFixedSizes( fontDescription, sizes );
450 void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
452 GetImplementation(*this).GetFontMetrics( fontId, metrics );
455 GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
457 return GetImplementation(*this).GetGlyphIndex( fontId, charcode );
460 bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal )
462 return GetImplementation(*this).GetGlyphMetrics( array, size, horizontal );
465 BufferImage FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
467 return GetImplementation(*this).CreateBitmap( fontId, glyphIndex );
471 void FontClient::CreateVectorBlob( FontId fontId,
472 GlyphIndex glyphIndex,
474 unsigned int& blobLength,
475 unsigned int& nominalWidth,
476 unsigned int& nominalHeight )
478 GetImplementation(*this).CreateVectorBlob( fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
481 const GlyphInfo& FontClient::GetEllipsisGlyph( PointSize26Dot6 pointSize )
483 return GetImplementation(*this).GetEllipsisGlyph( pointSize );
486 FontClient::FontClient( Internal::FontClient* internal )
487 : BaseHandle( internal )
491 FontMetrics::FontMetrics()
495 underlinePosition( 0.f ),
496 underlineThickness( 0.f )
500 GlyphInfo::GlyphInfo()
504 Script GetCharacterScript(unsigned int x)
508 bool HasLigatureMustBreak(Script x){return false;}
509 bool IsCommonScript(unsigned int character){ return false;}
510 bool IsNewParagraph(unsigned int character){return false;}
511 bool IsRightToLeftScript(Script){return false;}
512 bool IsWhiteSpace(unsigned int character)
514 return character < 0x21;
517 Segmentation Segmentation::Get(){ return Segmentation();}
518 Segmentation::Segmentation(){}
519 Segmentation::~Segmentation(){}
520 void Segmentation::GetLineBreakPositions(unsigned int const*, unsigned int, char*){}
521 void Segmentation::GetWordBreakPositions(unsigned int const*, unsigned int, char*){}
523 Shaping Shaping::Get()
525 return TextAbstraction::Internal::Shaping::Get();
532 Shaping::Shaping( Internal::Shaping* internal )
533 : BaseHandle( internal )
541 void Shaping::GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
543 // Return store & mapping table (0, 1, 2, 3... N-1))
544 GetImplementation(*this).GetGlyphs(glyphStore, mappingTable);
547 Length Shaping::Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
549 return GetImplementation(*this).Shape( text, numChars, fontId, script );