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