Uses TextArray new type definition.
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / slp-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2014 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
18 #include "slp-platform-abstraction.h"
19
20 #ifndef DALI_PROFILE_UBUNTU
21 #include <vconf.h>
22 #endif // DALI_PROFILE_UBUNTU
23 #include <dirent.h>
24
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/integration-api/resource-types.h>
28
29 #include "resource-loader/resource-loader.h"
30 #include "dynamics/dynamics-factory.h"
31
32 #include "slp-font-configuration-parser.h"
33 #include "data-cache/metrics-cache.h"
34 #include "image-loaders/image-loader.h"
35
36 namespace Dali
37 {
38
39 DALI_IMPORT_API Integration::PlatformAbstraction* CreatePlatformAbstraction()
40 {
41   return new SlpPlatform::SlpPlatformAbstraction();
42 }
43
44
45 namespace SlpPlatform
46 {
47
48 namespace
49 {
50 const std::string FONT_CONFIGURATION_FILE( FONT_CONFIGURATION_FILE_PATH ); ///< Default font configuration file
51 const std::string DEFAULT_FONT_FAMILY( "HelveticaNeue" );                  ///< Default font family when unable to retrieve from font configuration file
52 const std::string DEFAULT_FONT_STYLE( "Book" );                            ///< Default font style when unable to retrieve from font configuration file
53 const std::string NULL_FONT_FAMILY_NAME( "" );
54 const unsigned int NANOSECS_TO_MICROSECS( 1000 );                          ///< 1000 nanoseconds = 1 microsecond
55
56 /// Settings to float point conversion table
57 const float FONT_SIZE_TABLE[5] =
58 {
59   8.0f,  ///< Small
60   10.0f, ///< Normal
61   15.0f, ///< Large
62   19.0f, ///< Huge
63   25.0f  ///< Giant
64 };
65 }
66
67 SlpPlatformAbstraction::SlpPlatformAbstraction()
68 : mResourceLoader(new ResourceLoader),
69   mDefaultFontSize(FONT_SIZE_TABLE[1]),
70   mDynamicsFactory(NULL)
71 {
72   int error = FT_Init_FreeType(&mFreeTypeHandle);
73   DALI_ASSERT_ALWAYS( error == 0 && "Freetype initialization failed" );
74
75   UpdateDefaultsFromDevice();
76 }
77
78 SlpPlatformAbstraction::~SlpPlatformAbstraction()
79 {
80   delete mResourceLoader;
81   delete mDynamicsFactory;
82
83   if (mFreeTypeHandle)
84   {
85     FT_Done_FreeType(mFreeTypeHandle);
86   }
87 }
88
89 void SlpPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
90 {
91   timespec time;
92   clock_gettime(CLOCK_MONOTONIC, &time);
93   seconds = time.tv_sec;
94   microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
95 }
96
97 void SlpPlatformAbstraction::Suspend()
98 {
99   if (mResourceLoader)
100   {
101     mResourceLoader->Pause();
102   }
103 }
104
105 void SlpPlatformAbstraction::Resume()
106 {
107   if (mResourceLoader)
108   {
109     mResourceLoader->Resume();
110   }
111 }
112
113 const std::string& SlpPlatformAbstraction::GetDefaultFontFamily() const
114 {
115   // VCC TODO: return default font style as well.
116   return mDefaultFontFamily;
117 }
118
119 float SlpPlatformAbstraction::GetDefaultFontSize() const
120 {
121   return mDefaultFontSize;
122 }
123
124 PixelSize SlpPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
125 {
126   PixelSize result(0);
127
128   if (mResourceLoader)
129   {
130     result = mResourceLoader->GetFontLineHeightFromCapsHeight(fontFamily, fontStyle, capsHeight, mFreeTypeHandle);
131   }
132
133   return result;
134 }
135
136 Integration::GlyphSet* SlpPlatformAbstraction::GetGlyphData ( const Dali::Integration::TextResourceType& textRequest,
137                                                               const std::string& fontFamily,
138                                                               bool getBitmap) const
139 {
140   if (mResourceLoader)
141   {
142     return mResourceLoader->GetGlyphData(textRequest,
143                                          mFreeTypeHandle,
144                                          fontFamily,
145                                          getBitmap);
146   }
147   return NULL;
148 }
149
150 Integration::GlyphSet* SlpPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
151                                                                    const std::string& fontFamily ) const
152 {
153   if (mResourceLoader)
154   {
155     return mResourceLoader->GetCachedGlyphData( textRequest, fontFamily );
156   }
157   return NULL;
158 }
159
160
161 void SlpPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily, const std::string& fontStyle, Integration::GlobalMetrics& globalMetrics ) const
162 {
163   if( mResourceLoader )
164   {
165     mResourceLoader->GetGlobalMetrics( mFreeTypeHandle,
166                                        fontFamily,
167                                        fontStyle,
168                                        globalMetrics );
169   }
170 }
171
172 void SlpPlatformAbstraction::GetClosestImageSize( const std::string& filename,
173                                                   const ImageAttributes& attributes,
174                                                   Vector2& closestSize )
175 {
176   closestSize = Vector2::ZERO;
177   ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
178 }
179
180 void SlpPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
181                                                   const ImageAttributes& attributes,
182                                                   Vector2& closestSize )
183 {
184   closestSize = Vector2::ZERO;
185   ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
186 }
187
188
189 void SlpPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
190 {
191   if (mResourceLoader)
192   {
193     mResourceLoader->LoadResource(request);
194   }
195 }
196
197 Integration::ResourcePointer SlpPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
198 {
199   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
200 }
201
202
203 void SlpPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
204 {
205   if (mResourceLoader)
206   {
207     mResourceLoader->SaveResource(request);
208   }
209 }
210
211 void SlpPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
212 {
213   if (mResourceLoader)
214   {
215     mResourceLoader->CancelLoad(id, typeId);
216   }
217 }
218
219 bool SlpPlatformAbstraction::IsLoading()
220 {
221   if (mResourceLoader)
222   {
223     return mResourceLoader->IsLoading();
224   }
225
226   return false;
227 }
228
229 void SlpPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
230 {
231   if (mResourceLoader)
232   {
233     mResourceLoader->GetResources(cache);
234   }
235 }
236
237 void SlpPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
238 {
239   if (mResourceLoader)
240   {
241     mResourceLoader->SetDpi(dpiHor, dpiVer);
242   }
243 }
244
245 const std::string& SlpPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
246 {
247   if( mResourceLoader )
248   {
249     return mResourceLoader->GetFontFamilyForChars(charsRequested);
250   }
251
252   return NULL_FONT_FAMILY_NAME;
253 }
254
255 bool SlpPlatformAbstraction::AllGlyphsSupported(const std::string &fontFamily, const std::string& fontStyle, const Integration::TextArray& charsRequested) const
256 {
257   bool ret = false;
258   if (mResourceLoader)
259   {
260     ret = mResourceLoader->AllGlyphsSupported(fontFamily, fontStyle, charsRequested);
261   }
262   return ret;
263 }
264
265 bool SlpPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
266 {
267   bool ret = false;
268   if( mResourceLoader )
269   {
270     // TODO: Consider retrieve both isDefaultSystemFontFamily and isDefaultSystemFontStyle.
271     bool isDefaultFamily = false;
272     bool isDefaultStyle = false;
273     ret = mResourceLoader->ValidateFontFamilyName( fontFamily, fontStyle, isDefaultFamily, isDefaultStyle, closestMatch, closestStyleMatch );
274     isDefaultSystemFont = isDefaultFamily && isDefaultStyle;
275   }
276   return ret;
277 }
278
279 void SlpPlatformAbstraction::GetFontList(  Dali::Integration::PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList ) const
280 {
281   if( mResourceLoader )
282   {
283     mResourceLoader->GetFontList( mode, fontList );
284   }
285 }
286
287 bool SlpPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
288 {
289   bool result = false;
290
291   if (mResourceLoader)
292   {
293     result = mResourceLoader->LoadFile(filename, buffer);
294   }
295
296   return result;
297 }
298
299 std::string SlpPlatformAbstraction::LoadFile( const std::string& filename )
300 {
301   std::string result;
302   if (mResourceLoader)
303   {
304     result = mResourceLoader->LoadFile(filename);
305   }
306
307   return result;
308 }
309
310 bool SlpPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
311 {
312   bool result = false;
313
314   if (mResourceLoader)
315   {
316     result = mResourceLoader->SaveFile(filename, buffer);
317   }
318
319   return result;
320 }
321
322 void SlpPlatformAbstraction::JoinLoaderThreads()
323 {
324   delete mResourceLoader;
325   mResourceLoader = NULL;
326 }
327
328 void SlpPlatformAbstraction::UpdateDefaultsFromDevice()
329 {
330   // FontConfigurationParser::Parse sets the default font family and the default font style.
331   // If the isn't a configuration file or is invalid, or it doesn't have any tag with the default
332   // font family nor font style then default values set by the application are used.
333   mDefaultFontFamily = DEFAULT_FONT_FAMILY;
334   mDefaultFontStyle = DEFAULT_FONT_STYLE;
335
336   // The initialized values above are not used to parse the configuration file. These values
337   // are set just in case FontConfigurationParser::Parse is not able to set default values.
338   FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, mDefaultFontFamily, mDefaultFontStyle);
339
340   if ( mResourceLoader )
341   {
342     mResourceLoader->SetDefaultFontFamily( mDefaultFontFamily, mDefaultFontStyle );
343   }
344
345   int fontSize(0);
346 #ifndef DALI_PROFILE_UBUNTU
347   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
348 #endif // DALI_PROFILE_UBUNTU
349   mDefaultFontSize = FONT_SIZE_TABLE[fontSize];
350 }
351
352 Integration::DynamicsFactory* SlpPlatformAbstraction::GetDynamicsFactory()
353 {
354   if( NULL == mDynamicsFactory )
355   {
356     mDynamicsFactory = new DynamicsFactory;
357   }
358
359   return mDynamicsFactory;
360 }
361
362 bool SlpPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
363                                                          const std::string& fontStyle,
364                                                          Integration::GlobalMetrics& globalMetrics )
365 {
366   return MetricsCache::ReadGlobal( fontFamily, fontStyle, globalMetrics );
367 }
368
369 void SlpPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
370                                                         const std::string& fontStyle,
371                                                         const Integration::GlobalMetrics& globalMetrics )
372 {
373   MetricsCache::WriteGlobal( fontFamily, fontStyle, globalMetrics);
374 }
375
376 bool SlpPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
377                                                    const std::string& fontStyle,
378                                                    std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
379 {
380   return MetricsCache::Read( fontFamily, fontStyle, glyphMetricsContainer );
381 }
382
383 void SlpPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
384                                                   const std::string& fontStyle,
385                                                   const Integration::GlyphSet& glyphSet )
386 {
387   MetricsCache::Write( fontFamily, fontStyle, glyphSet );
388 }
389
390 void SlpPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
391                                                         std::vector<std::string>& fileNames )
392 {
393   dirent* de = NULL;
394   DIR* dp;
395   dp = opendir( directoryName.c_str() );
396   if( dp )
397   {
398     const std::string dot( "." );
399     const std::string dotDot( ".." );
400     while( true )
401     {
402       de = readdir( dp );
403       if( de == NULL )
404       {
405         break;
406       }
407       const std::string fileName( de->d_name );
408       if( ( fileName != dot ) &&
409           ( fileName != dotDot ) )
410       {
411         fileNames.push_back( fileName );
412       }
413     }
414     closedir( dp );
415   }
416 }
417
418 Integration::BitmapPtr SlpPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) const
419 {
420   Integration::BitmapPtr glyphImage;
421
422   if( mResourceLoader )
423   {
424     glyphImage = mResourceLoader->GetGlyphImage( mFreeTypeHandle, fontFamily, fontStyle, fontSize, character );
425   }
426
427   return glyphImage;
428 }
429
430 }  // namespace SlpPlatform
431
432 }  // namespace Dali