Update README for dali-swig
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-text-abstraction.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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>
26 #include <cstring>
27
28 using namespace Dali;
29
30 namespace Dali
31 {
32 class Adaptor;
33
34 namespace TextAbstraction
35 {
36 namespace Internal
37 {
38
39 class BidirectionalSupport : public BaseObject
40 {
41 public:
42   BidirectionalSupport()
43   {
44   }
45
46   ~BidirectionalSupport()
47   {
48   }
49
50   static TextAbstraction::BidirectionalSupport Get()
51   {
52     TextAbstraction::BidirectionalSupport bidirectionalSupportHandle;
53
54     Dali::SingletonService service( Dali::SingletonService::Get() );
55     if( service )
56     {
57       // Check whether the singleton is already created
58       BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::BidirectionalSupport ) );
59       if(handle)
60       {
61         // If so, downcast the handle
62         BidirectionalSupport* impl = dynamic_cast< Internal::BidirectionalSupport* >( handle.GetObjectPtr() );
63         bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( impl );
64       }
65       else // create and register the object
66       {
67         bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport( new BidirectionalSupport );
68         service.Register( typeid( bidirectionalSupportHandle ), bidirectionalSupportHandle );
69       }
70     }
71     return bidirectionalSupportHandle;
72   }
73   BidiInfoIndex CreateInfo( const Character* const paragraph, Length numberOfCharacters ){return 0;}
74
75   void DestroyInfo( BidiInfoIndex bidiInfoIndex )
76   {
77   }
78
79   void Reorder( BidiInfoIndex bidiInfoIndex,CharacterIndex firstCharacterIndex,Length numberOfCharacters,CharacterIndex* visualToLogicalMap )
80   {
81   }
82
83   bool GetMirroredText( Character* text,CharacterDirection* directions,Length numberOfCharacters )
84   {
85     return true;
86   }
87
88   bool GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
89   {
90     return true;
91   }
92
93   void GetCharactersDirection( BidiInfoIndex bidiInfoIndex, CharacterDirection* directions, Length numberOfCharacters )
94   {
95   }
96 }; // class BidirectionalSupport
97
98
99 class FontClient : public BaseObject
100 {
101 public:
102   FontClient()
103   : mDpiHorizontal( 0 ),
104     mDpiVertical( 0 )
105   {
106   }
107
108   ~FontClient(){}
109
110   static Dali::TextAbstraction::FontClient Get()
111   {
112     Dali::TextAbstraction::FontClient fontClientHandle;
113
114     Dali::SingletonService service( SingletonService::Get() );
115     if ( service )
116     {
117       // Check whether the singleton is already created
118       Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::FontClient ) );
119       if(handle)
120       {
121         // If so, downcast the handle
122         FontClient* impl = dynamic_cast< Dali::TextAbstraction::Internal::FontClient* >( handle.GetObjectPtr() );
123         fontClientHandle = Dali::TextAbstraction::FontClient( impl );
124       }
125       else // create and register the object
126       {
127         fontClientHandle = Dali::TextAbstraction::FontClient( new FontClient );
128         service.Register( typeid( fontClientHandle ), fontClientHandle );
129       }
130     }
131
132     return fontClientHandle;
133   }
134
135   void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi ){}
136   void GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi ){horizontalDpi=verticalDpi=96;}
137
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( Character charcode, const FontDescription& fontDescription, 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   PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex ){return PixelData();}
156   void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob,
157                          unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
158   {
159     blobLength = 0;
160   }
161   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize ){return mGlyphInfo;}
162 private:
163   unsigned int mDpiHorizontal;
164   unsigned int mDpiVertical;
165   GlyphInfo    mGlyphInfo;
166 }; // class FontClient
167
168
169 class Shaping : public BaseObject
170 {
171 public:
172   Shaping() : mText(NULL)
173   {}
174
175   ~Shaping()
176   {
177     delete [] mText;
178   }
179
180   static Dali::TextAbstraction::Shaping Get()
181   {
182     Dali::TextAbstraction::Shaping shapingHandle;
183
184     Dali::SingletonService service( SingletonService::Get() );
185     if ( service )
186     {
187       // Check whether the singleton is already created
188       Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::Shaping ) );
189       if(handle)
190       {
191         // If so, downcast the handle
192         Shaping* impl = dynamic_cast< Dali::TextAbstraction::Internal::Shaping* >( handle.GetObjectPtr() );
193         shapingHandle = Dali::TextAbstraction::Shaping( impl );
194       }
195       else // create and register the object
196       {
197         shapingHandle = Dali::TextAbstraction::Shaping( new Shaping );
198         service.Register( typeid( shapingHandle ), shapingHandle );
199       }
200     }
201     return shapingHandle;
202   }
203
204   void GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
205   {
206     // Return store & mapping table (0, 1, 2, 3... N-1))
207     if( glyphStore )
208     {
209       memcpy( glyphStore, mText, mNumChars );
210     }
211     for( unsigned int i=0; i<mNumChars ; ++i )
212     {
213       mappingTable[i] = i;
214     }
215   }
216
217   Length Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
218   {
219     mText = new unsigned char[numChars];
220     mNumChars = numChars;
221
222     memcpy( mText, text, numChars );
223
224     return numChars;
225   }
226 private:
227   unsigned char* mText;
228   unsigned int mNumChars;
229 };
230
231 } // Internal
232 } // TextAbstraction
233
234 inline static TextAbstraction::Internal::BidirectionalSupport& GetImplementation( TextAbstraction::BidirectionalSupport& bidirectionalSupport )
235 {
236   DALI_ASSERT_ALWAYS( bidirectionalSupport && "bidirectional support handle is empty" );
237   BaseObject& object = bidirectionalSupport.GetBaseObject();
238   return static_cast<TextAbstraction::Internal::BidirectionalSupport&>(object);
239 }
240
241 inline static const TextAbstraction::Internal::BidirectionalSupport& GetImplementation( const TextAbstraction::BidirectionalSupport& bidirectionalSupport )
242 {
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);
246 }
247
248 inline static TextAbstraction::Internal::FontClient& GetImplementation(TextAbstraction::FontClient& fontClient)
249 {
250   DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
251   BaseObject& handle = fontClient.GetBaseObject();
252   return static_cast<TextAbstraction::Internal::FontClient&>(handle);
253 }
254
255 inline static const TextAbstraction::Internal::FontClient& GetImplementation(const TextAbstraction::FontClient& fontClient)
256 {
257   DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
258   const BaseObject& handle = fontClient.GetBaseObject();
259   return static_cast<const TextAbstraction::Internal::FontClient&>(handle);
260 }
261
262 inline static TextAbstraction::Internal::Shaping& GetImplementation(TextAbstraction::Shaping& shaping)
263 {
264   DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
265   BaseObject& handle = shaping.GetBaseObject();
266   return static_cast<TextAbstraction::Internal::Shaping&>(handle);
267 }
268
269 inline static const TextAbstraction::Internal::Shaping& GetImplementation(const TextAbstraction::Shaping& shaping)
270 {
271   DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
272   const BaseObject& handle = shaping.GetBaseObject();
273   return static_cast<const TextAbstraction::Internal::Shaping&>(handle);
274 }
275
276
277 ////////////////////////////////////////////////////////////////////////////////
278 /******************************************************************************/
279
280 namespace TextAbstraction
281 {
282 const PointSize26Dot6 FontClient::DEFAULT_POINT_SIZE = 768u; // 12*64
283
284 BidirectionalSupport::BidirectionalSupport()
285 {
286 }
287
288 BidirectionalSupport::~BidirectionalSupport()
289 {
290 }
291
292 BidirectionalSupport::BidirectionalSupport( Internal::BidirectionalSupport* implementation )
293 : BaseHandle( implementation )
294 {
295 }
296
297 BidirectionalSupport BidirectionalSupport::Get()
298 {
299   return Internal::BidirectionalSupport::Get();
300 }
301
302 BidiInfoIndex BidirectionalSupport::CreateInfo( const Character* const paragraph,
303                                                 Length numberOfCharacters )
304 {
305   return GetImplementation( *this ).CreateInfo( paragraph, numberOfCharacters );
306 }
307
308 void BidirectionalSupport::DestroyInfo( BidiInfoIndex bidiInfoIndex )
309 {
310   GetImplementation( *this ).DestroyInfo( bidiInfoIndex );
311 }
312
313 void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex,
314                                     CharacterIndex firstCharacterIndex,
315                                     Length numberOfCharacters,
316                                     CharacterIndex* visualToLogicalMap )
317 {
318   GetImplementation( *this ).Reorder( bidiInfoIndex, firstCharacterIndex, numberOfCharacters, visualToLogicalMap );
319 }
320
321 bool BidirectionalSupport::GetMirroredText( Character* text,
322                                             CharacterDirection* directions,
323                                             Length numberOfCharacters )
324 {
325   return GetImplementation( *this ).GetMirroredText( text, directions, numberOfCharacters );
326 }
327
328 bool BidirectionalSupport::GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
329 {
330   return GetImplementation( *this ).GetParagraphDirection( bidiInfoIndex );
331 }
332
333 void BidirectionalSupport::GetCharactersDirection( BidiInfoIndex bidiInfoIndex,
334                                                    CharacterDirection* directions,
335                                                    Length numberOfCharacters )
336 {
337   GetImplementation( *this ).GetCharactersDirection( bidiInfoIndex, directions, numberOfCharacters );
338 }
339
340
341 FontClient FontClient::Get()
342 {
343   return Internal::FontClient::Get();
344 }
345
346 FontClient::FontClient()
347 {
348 }
349
350 FontClient::~FontClient()
351 {
352 }
353
354 FontClient::FontClient( const FontClient& handle )
355 : BaseHandle( handle )
356 {
357 }
358
359 FontClient& FontClient::operator=( const FontClient& handle )
360 {
361   BaseHandle::operator=( handle );
362   return *this;
363 }
364
365 void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi  )
366 {
367   GetImplementation(*this).SetDpi( horizontalDpi, verticalDpi );
368 }
369
370 void FontClient::GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi )
371 {
372   GetImplementation(*this).GetDpi( horizontalDpi, verticalDpi );
373 }
374
375 void FontClient::ResetSystemDefaults()
376 {
377   GetImplementation(*this).ResetSystemDefaults();
378 }
379
380 void FontClient::GetDefaultFonts( FontList& defaultFonts )
381 {
382   GetImplementation(*this).GetDefaultFonts( defaultFonts );
383 }
384
385 void FontClient::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
386 {
387   GetImplementation(*this).GetDefaultPlatformFontDescription( fontDescription );
388 }
389
390 void FontClient::GetSystemFonts( FontList& systemFonts )
391 {
392   GetImplementation(*this).GetSystemFonts( systemFonts );
393 }
394
395 void FontClient::GetDescription( FontId id, FontDescription& fontDescription )
396 {
397   GetImplementation(*this).GetDescription( id, fontDescription );
398 }
399
400 PointSize26Dot6 FontClient::GetPointSize( FontId id )
401 {
402   return GetImplementation(*this).GetPointSize( id );
403 }
404
405 FontId FontClient::FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor )
406 {
407   return GetImplementation(*this).FindDefaultFont( charcode, pointSize, preferColor );
408 }
409
410 FontId FontClient::FindFallbackFont( Character charcode, const FontDescription& fontDescription, PointSize26Dot6 pointSize, bool preferColor )
411 {
412   return GetImplementation(*this).FindFallbackFont( charcode, fontDescription, pointSize, preferColor );
413 }
414
415 FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
416 {
417   return GetImplementation(*this).GetFontId( path, pointSize, faceIndex );
418 }
419
420 FontId FontClient::GetFontId( const FontDescription& fontDescription,
421                               PointSize26Dot6 pointSize,
422                               FaceIndex faceIndex )
423 {
424   return GetImplementation(*this).GetFontId( fontDescription,
425                                              pointSize,
426                                              faceIndex );
427 }
428
429 bool FontClient::IsScalable( const FontPath& path )
430 {
431   return GetImplementation(*this).IsScalable( path );
432 }
433
434 bool FontClient::IsScalable( const FontDescription& fontDescription )
435 {
436   return GetImplementation(*this).IsScalable( fontDescription );
437 }
438
439 void FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes )
440 {
441   GetImplementation(*this).GetFixedSizes( path, sizes );
442 }
443
444 void FontClient::GetFixedSizes( const FontDescription& fontDescription,
445                                 Dali::Vector< PointSize26Dot6 >& sizes )
446 {
447   GetImplementation(*this).GetFixedSizes( fontDescription, sizes );
448 }
449
450 void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
451 {
452   GetImplementation(*this).GetFontMetrics( fontId, metrics );
453 }
454
455 GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
456 {
457   return GetImplementation(*this).GetGlyphIndex( fontId, charcode );
458 }
459
460 bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal )
461 {
462   return GetImplementation(*this).GetGlyphMetrics( array, size, horizontal );
463 }
464
465 PixelData FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
466 {
467   return GetImplementation(*this).CreateBitmap( fontId, glyphIndex );
468 }
469
470
471 void FontClient::CreateVectorBlob( FontId fontId,
472                                    GlyphIndex glyphIndex,
473                                    VectorBlob*& blob,
474                                    unsigned int& blobLength,
475                                    unsigned int& nominalWidth,
476                                    unsigned int& nominalHeight )
477 {
478   GetImplementation(*this).CreateVectorBlob( fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
479 }
480
481 const GlyphInfo& FontClient::GetEllipsisGlyph( PointSize26Dot6 pointSize )
482 {
483   return GetImplementation(*this).GetEllipsisGlyph( pointSize );
484 }
485
486 FontClient::FontClient( Internal::FontClient* internal )
487 : BaseHandle( internal )
488 {
489 }
490
491 FontMetrics::FontMetrics()
492 : ascender( 0.f ),
493   descender( 0.f ),
494   height( 0.f ),
495   underlinePosition( 0.f ),
496   underlineThickness( 0.f )
497 {
498 }
499
500 GlyphInfo::GlyphInfo()
501 {
502 }
503
504 Script GetCharacterScript(unsigned int x)
505 {
506   return LATIN;
507 }
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)
513 {
514   return character < 0x21;
515 }
516
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*){}
522
523 Shaping Shaping::Get()
524 {
525   return TextAbstraction::Internal::Shaping::Get();
526 }
527
528 Shaping::Shaping()
529 {
530 }
531
532 Shaping::Shaping( Internal::Shaping* internal )
533 : BaseHandle( internal )
534 {
535 }
536
537 Shaping::~Shaping()
538 {
539 }
540
541 void Shaping::GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
542 {
543   // Return store & mapping table (0, 1, 2, 3... N-1))
544   GetImplementation(*this).GetGlyphs(glyphStore, mappingTable);
545 }
546
547 Length Shaping::Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
548 {
549   return GetImplementation(*this).Shape( text, numChars, fontId, script );
550 }
551
552 } // TextAbstraction
553 } // Dali