Change common test application
[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   void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth ){}
156   PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth ){return PixelData();}
157   void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob,
158                          unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
159   {
160     blobLength = 0;
161   }
162   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize ){return mGlyphInfo;}
163   bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex ){return false;}
164 private:
165   unsigned int mDpiHorizontal;
166   unsigned int mDpiVertical;
167   GlyphInfo    mGlyphInfo;
168 }; // class FontClient
169
170
171 class Shaping : public BaseObject
172 {
173 public:
174   Shaping() : mText(NULL)
175   {}
176
177   ~Shaping()
178   {
179     delete [] mText;
180   }
181
182   static Dali::TextAbstraction::Shaping Get()
183   {
184     Dali::TextAbstraction::Shaping shapingHandle;
185
186     Dali::SingletonService service( SingletonService::Get() );
187     if ( service )
188     {
189       // Check whether the singleton is already created
190       Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::Shaping ) );
191       if(handle)
192       {
193         // If so, downcast the handle
194         Shaping* impl = dynamic_cast< Dali::TextAbstraction::Internal::Shaping* >( handle.GetObjectPtr() );
195         shapingHandle = Dali::TextAbstraction::Shaping( impl );
196       }
197       else // create and register the object
198       {
199         shapingHandle = Dali::TextAbstraction::Shaping( new Shaping );
200         service.Register( typeid( shapingHandle ), shapingHandle );
201       }
202     }
203     return shapingHandle;
204   }
205
206   void GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
207   {
208     // Return store & mapping table (0, 1, 2, 3... N-1))
209     if( glyphStore )
210     {
211       memcpy( glyphStore, mText, mNumChars );
212     }
213     for( unsigned int i=0; i<mNumChars ; ++i )
214     {
215       mappingTable[i] = i;
216     }
217   }
218
219   Length Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
220   {
221     mText = new unsigned char[numChars];
222     mNumChars = numChars;
223
224     memcpy( mText, text, numChars );
225
226     return numChars;
227   }
228 private:
229   unsigned char* mText;
230   unsigned int mNumChars;
231 };
232
233 } // Internal
234 } // TextAbstraction
235
236 inline static TextAbstraction::Internal::BidirectionalSupport& GetImplementation( TextAbstraction::BidirectionalSupport& bidirectionalSupport )
237 {
238   DALI_ASSERT_ALWAYS( bidirectionalSupport && "bidirectional support handle is empty" );
239   BaseObject& object = bidirectionalSupport.GetBaseObject();
240   return static_cast<TextAbstraction::Internal::BidirectionalSupport&>(object);
241 }
242
243 inline static const TextAbstraction::Internal::BidirectionalSupport& GetImplementation( const TextAbstraction::BidirectionalSupport& bidirectionalSupport )
244 {
245   DALI_ASSERT_ALWAYS( bidirectionalSupport && "bidirectional support handle is empty" );
246   const BaseObject& object = bidirectionalSupport.GetBaseObject();
247   return static_cast<const TextAbstraction::Internal::BidirectionalSupport&>(object);
248 }
249
250 inline static TextAbstraction::Internal::FontClient& GetImplementation(TextAbstraction::FontClient& fontClient)
251 {
252   DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
253   BaseObject& handle = fontClient.GetBaseObject();
254   return static_cast<TextAbstraction::Internal::FontClient&>(handle);
255 }
256
257 inline static const TextAbstraction::Internal::FontClient& GetImplementation(const TextAbstraction::FontClient& fontClient)
258 {
259   DALI_ASSERT_ALWAYS( fontClient && "fontClient handle is empty" );
260   const BaseObject& handle = fontClient.GetBaseObject();
261   return static_cast<const TextAbstraction::Internal::FontClient&>(handle);
262 }
263
264 inline static TextAbstraction::Internal::Shaping& GetImplementation(TextAbstraction::Shaping& shaping)
265 {
266   DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
267   BaseObject& handle = shaping.GetBaseObject();
268   return static_cast<TextAbstraction::Internal::Shaping&>(handle);
269 }
270
271 inline static const TextAbstraction::Internal::Shaping& GetImplementation(const TextAbstraction::Shaping& shaping)
272 {
273   DALI_ASSERT_ALWAYS( shaping && "shaping handle is empty" );
274   const BaseObject& handle = shaping.GetBaseObject();
275   return static_cast<const TextAbstraction::Internal::Shaping&>(handle);
276 }
277
278
279 ////////////////////////////////////////////////////////////////////////////////
280 /******************************************************************************/
281
282 namespace TextAbstraction
283 {
284 const PointSize26Dot6 FontClient::DEFAULT_POINT_SIZE = 768u; // 12*64
285
286 BidirectionalSupport::BidirectionalSupport()
287 {
288 }
289
290 BidirectionalSupport::~BidirectionalSupport()
291 {
292 }
293
294 BidirectionalSupport::BidirectionalSupport( Internal::BidirectionalSupport* implementation )
295 : BaseHandle( implementation )
296 {
297 }
298
299 BidirectionalSupport BidirectionalSupport::Get()
300 {
301   return Internal::BidirectionalSupport::Get();
302 }
303
304 BidiInfoIndex BidirectionalSupport::CreateInfo( const Character* const paragraph,
305                                                 Length numberOfCharacters )
306 {
307   return GetImplementation( *this ).CreateInfo( paragraph, numberOfCharacters );
308 }
309
310 void BidirectionalSupport::DestroyInfo( BidiInfoIndex bidiInfoIndex )
311 {
312   GetImplementation( *this ).DestroyInfo( bidiInfoIndex );
313 }
314
315 void BidirectionalSupport::Reorder( BidiInfoIndex bidiInfoIndex,
316                                     CharacterIndex firstCharacterIndex,
317                                     Length numberOfCharacters,
318                                     CharacterIndex* visualToLogicalMap )
319 {
320   GetImplementation( *this ).Reorder( bidiInfoIndex, firstCharacterIndex, numberOfCharacters, visualToLogicalMap );
321 }
322
323 bool BidirectionalSupport::GetMirroredText( Character* text,
324                                             CharacterDirection* directions,
325                                             Length numberOfCharacters )
326 {
327   return GetImplementation( *this ).GetMirroredText( text, directions, numberOfCharacters );
328 }
329
330 bool BidirectionalSupport::GetParagraphDirection( BidiInfoIndex bidiInfoIndex ) const
331 {
332   return GetImplementation( *this ).GetParagraphDirection( bidiInfoIndex );
333 }
334
335 void BidirectionalSupport::GetCharactersDirection( BidiInfoIndex bidiInfoIndex,
336                                                    CharacterDirection* directions,
337                                                    Length numberOfCharacters )
338 {
339   GetImplementation( *this ).GetCharactersDirection( bidiInfoIndex, directions, numberOfCharacters );
340 }
341
342
343 FontClient FontClient::Get()
344 {
345   return Internal::FontClient::Get();
346 }
347
348 FontClient::FontClient()
349 {
350 }
351
352 FontClient::~FontClient()
353 {
354 }
355
356 FontClient::FontClient( const FontClient& handle )
357 : BaseHandle( handle )
358 {
359 }
360
361 FontClient::GlyphBufferData::GlyphBufferData()
362 {
363 }
364
365 FontClient::GlyphBufferData::~GlyphBufferData()
366 {
367 }
368
369 FontClient& FontClient::operator=( const FontClient& handle )
370 {
371   BaseHandle::operator=( handle );
372   return *this;
373 }
374
375 void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi  )
376 {
377   GetImplementation(*this).SetDpi( horizontalDpi, verticalDpi );
378 }
379
380 void FontClient::GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi )
381 {
382   GetImplementation(*this).GetDpi( horizontalDpi, verticalDpi );
383 }
384
385 void FontClient::ResetSystemDefaults()
386 {
387   GetImplementation(*this).ResetSystemDefaults();
388 }
389
390 void FontClient::GetDefaultFonts( FontList& defaultFonts )
391 {
392   GetImplementation(*this).GetDefaultFonts( defaultFonts );
393 }
394
395 void FontClient::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
396 {
397   GetImplementation(*this).GetDefaultPlatformFontDescription( fontDescription );
398 }
399
400 void FontClient::GetSystemFonts( FontList& systemFonts )
401 {
402   GetImplementation(*this).GetSystemFonts( systemFonts );
403 }
404
405 void FontClient::GetDescription( FontId id, FontDescription& fontDescription )
406 {
407   GetImplementation(*this).GetDescription( id, fontDescription );
408 }
409
410 PointSize26Dot6 FontClient::GetPointSize( FontId id )
411 {
412   return GetImplementation(*this).GetPointSize( id );
413 }
414
415 FontId FontClient::FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor )
416 {
417   return GetImplementation(*this).FindDefaultFont( charcode, pointSize, preferColor );
418 }
419
420 FontId FontClient::FindFallbackFont( Character charcode, const FontDescription& fontDescription, PointSize26Dot6 pointSize, bool preferColor )
421 {
422   return GetImplementation(*this).FindFallbackFont( charcode, fontDescription, pointSize, preferColor );
423 }
424
425 FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
426 {
427   return GetImplementation(*this).GetFontId( path, pointSize, faceIndex );
428 }
429
430 FontId FontClient::GetFontId( const FontDescription& fontDescription,
431                               PointSize26Dot6 pointSize,
432                               FaceIndex faceIndex )
433 {
434   return GetImplementation(*this).GetFontId( fontDescription,
435                                              pointSize,
436                                              faceIndex );
437 }
438
439 bool FontClient::IsScalable( const FontPath& path )
440 {
441   return GetImplementation(*this).IsScalable( path );
442 }
443
444 bool FontClient::IsScalable( const FontDescription& fontDescription )
445 {
446   return GetImplementation(*this).IsScalable( fontDescription );
447 }
448
449 void FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes )
450 {
451   GetImplementation(*this).GetFixedSizes( path, sizes );
452 }
453
454 void FontClient::GetFixedSizes( const FontDescription& fontDescription,
455                                 Dali::Vector< PointSize26Dot6 >& sizes )
456 {
457   GetImplementation(*this).GetFixedSizes( fontDescription, sizes );
458 }
459
460 void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
461 {
462   GetImplementation(*this).GetFontMetrics( fontId, metrics );
463 }
464
465 GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
466 {
467   return GetImplementation(*this).GetGlyphIndex( fontId, charcode );
468 }
469
470 bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal )
471 {
472   return GetImplementation(*this).GetGlyphMetrics( array, size, horizontal );
473 }
474
475 void FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
476 {
477   GetImplementation(*this).CreateBitmap( fontId, glyphIndex, data, outlineWidth );
478 }
479
480 PixelData FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
481 {
482   return GetImplementation(*this).CreateBitmap( fontId, glyphIndex, outlineWidth );
483 }
484
485 void FontClient::CreateVectorBlob( FontId fontId,
486                                    GlyphIndex glyphIndex,
487                                    VectorBlob*& blob,
488                                    unsigned int& blobLength,
489                                    unsigned int& nominalWidth,
490                                    unsigned int& nominalHeight )
491 {
492   GetImplementation(*this).CreateVectorBlob( fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
493 }
494
495 const GlyphInfo& FontClient::GetEllipsisGlyph( PointSize26Dot6 pointSize )
496 {
497   return GetImplementation(*this).GetEllipsisGlyph( pointSize );
498 }
499
500 bool FontClient::IsColorGlyph( FontId fontId, GlyphIndex glyphIndex )
501 {
502   return GetImplementation(*this).IsColorGlyph( fontId, glyphIndex );
503 }
504
505 FontClient::FontClient( Internal::FontClient* internal )
506 : BaseHandle( internal )
507 {
508 }
509
510 FontMetrics::FontMetrics()
511 : ascender( 0.f ),
512   descender( 0.f ),
513   height( 0.f ),
514   underlinePosition( 0.f ),
515   underlineThickness( 0.f )
516 {
517 }
518
519 GlyphInfo::GlyphInfo()
520 {
521 }
522
523 Script GetCharacterScript(unsigned int x)
524 {
525   return LATIN;
526 }
527 bool HasLigatureMustBreak(Script x){return false;}
528 bool IsCommonScript(unsigned int character){ return false;}
529 bool IsNewParagraph(unsigned int character){return false;}
530 bool IsRightToLeftScript(Script){return false;}
531 bool IsWhiteSpace(unsigned int character)
532 {
533   return character < 0x21;
534 }
535
536 Segmentation Segmentation::Get(){ return Segmentation();}
537 Segmentation::Segmentation(){}
538 Segmentation::~Segmentation(){}
539 void Segmentation::GetLineBreakPositions(unsigned int const*, unsigned int, char*){}
540 void Segmentation::GetWordBreakPositions(unsigned int const*, unsigned int, char*){}
541
542 Shaping Shaping::Get()
543 {
544   return TextAbstraction::Internal::Shaping::Get();
545 }
546
547 Shaping::Shaping()
548 {
549 }
550
551 Shaping::Shaping( Internal::Shaping* internal )
552 : BaseHandle( internal )
553 {
554 }
555
556 Shaping::~Shaping()
557 {
558 }
559
560 void Shaping::GetGlyphs(GlyphInfo* glyphStore, unsigned int*mappingTable)
561 {
562   // Return store & mapping table (0, 1, 2, 3... N-1))
563   GetImplementation(*this).GetGlyphs(glyphStore, mappingTable);
564 }
565
566 Length Shaping::Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
567 {
568   return GetImplementation(*this).Shape( text, numChars, fontId, script );
569 }
570
571 } // TextAbstraction
572 } // Dali