2 * Copyright (c) 2014 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.
18 #include "slp-platform-abstraction.h"
20 #ifndef DALI_PROFILE_UBUNTU
22 #endif // DALI_PROFILE_UBUNTU
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/integration-api/resource-types.h>
29 #include "resource-loader/resource-loader.h"
30 #include "dynamics/dynamics-factory.h"
32 #include "slp-font-configuration-parser.h"
33 #include "data-cache/metrics-cache.h"
34 #include "image-loaders/image-loader.h"
39 DALI_IMPORT_API Integration::PlatformAbstraction* CreatePlatformAbstraction()
41 return new SlpPlatform::SlpPlatformAbstraction();
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
56 /// Settings to float point conversion table
57 const float FONT_SIZE_TABLE[5] =
67 SlpPlatformAbstraction::SlpPlatformAbstraction()
68 : mResourceLoader(new ResourceLoader),
69 mDefaultFontSize(FONT_SIZE_TABLE[1]),
70 mDynamicsFactory(NULL)
72 int error = FT_Init_FreeType(&mFreeTypeHandle);
73 DALI_ASSERT_ALWAYS( error == 0 && "Freetype initialization failed" );
75 UpdateDefaultsFromDevice();
78 SlpPlatformAbstraction::~SlpPlatformAbstraction()
80 delete mResourceLoader;
81 delete mDynamicsFactory;
85 FT_Done_FreeType(mFreeTypeHandle);
89 void SlpPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int µSeconds)
92 clock_gettime(CLOCK_MONOTONIC, &time);
93 seconds = time.tv_sec;
94 microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
97 void SlpPlatformAbstraction::Suspend()
101 mResourceLoader->Pause();
105 void SlpPlatformAbstraction::Resume()
109 mResourceLoader->Resume();
113 const std::string& SlpPlatformAbstraction::GetDefaultFontFamily() const
115 // VCC TODO: return default font style as well.
116 return mDefaultFontFamily;
119 float SlpPlatformAbstraction::GetDefaultFontSize() const
121 return mDefaultFontSize;
124 PixelSize SlpPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
130 result = mResourceLoader->GetFontLineHeightFromCapsHeight(fontFamily, fontStyle, capsHeight, mFreeTypeHandle);
136 Integration::GlyphSet* SlpPlatformAbstraction::GetGlyphData ( const Dali::Integration::TextResourceType& textRequest,
137 const std::string& fontFamily,
138 bool getBitmap) const
142 return mResourceLoader->GetGlyphData(textRequest,
150 Integration::GlyphSet* SlpPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
151 const std::string& fontFamily ) const
155 return mResourceLoader->GetCachedGlyphData( textRequest, fontFamily );
161 void SlpPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily, const std::string& fontStyle, Integration::GlobalMetrics& globalMetrics ) const
163 if( mResourceLoader )
165 mResourceLoader->GetGlobalMetrics( mFreeTypeHandle,
172 void SlpPlatformAbstraction::GetClosestImageSize( const std::string& filename,
173 const ImageAttributes& attributes,
174 Vector2& closestSize )
176 closestSize = Vector2::ZERO;
177 ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
180 void SlpPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
181 const ImageAttributes& attributes,
182 Vector2& closestSize )
184 closestSize = Vector2::ZERO;
185 ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
189 void SlpPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
193 mResourceLoader->LoadResource(request);
197 Integration::ResourcePointer SlpPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
199 return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
203 void SlpPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
207 mResourceLoader->SaveResource(request);
211 void SlpPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
215 mResourceLoader->CancelLoad(id, typeId);
219 bool SlpPlatformAbstraction::IsLoading()
223 return mResourceLoader->IsLoading();
229 void SlpPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
233 mResourceLoader->GetResources(cache);
237 void SlpPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
241 mResourceLoader->SetDpi(dpiHor, dpiVer);
245 const std::string& SlpPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
247 if( mResourceLoader )
249 return mResourceLoader->GetFontFamilyForChars(charsRequested);
252 return NULL_FONT_FAMILY_NAME;
255 bool SlpPlatformAbstraction::AllGlyphsSupported(const std::string &fontFamily, const std::string& fontStyle, const Integration::TextArray& charsRequested) const
260 ret = mResourceLoader->AllGlyphsSupported(fontFamily, fontStyle, charsRequested);
265 bool SlpPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
268 if( mResourceLoader )
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;
279 void SlpPlatformAbstraction::GetFontList( Dali::Integration::PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList ) const
281 if( mResourceLoader )
283 mResourceLoader->GetFontList( mode, fontList );
287 bool SlpPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
293 result = mResourceLoader->LoadFile(filename, buffer);
299 std::string SlpPlatformAbstraction::LoadFile( const std::string& filename )
304 result = mResourceLoader->LoadFile(filename);
310 bool SlpPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
316 result = mResourceLoader->SaveFile(filename, buffer);
322 void SlpPlatformAbstraction::JoinLoaderThreads()
324 delete mResourceLoader;
325 mResourceLoader = NULL;
328 void SlpPlatformAbstraction::UpdateDefaultsFromDevice()
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;
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);
340 if ( mResourceLoader )
342 mResourceLoader->SetDefaultFontFamily( mDefaultFontFamily, mDefaultFontStyle );
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];
352 Integration::DynamicsFactory* SlpPlatformAbstraction::GetDynamicsFactory()
354 if( NULL == mDynamicsFactory )
356 mDynamicsFactory = new DynamicsFactory;
359 return mDynamicsFactory;
362 bool SlpPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
363 const std::string& fontStyle,
364 Integration::GlobalMetrics& globalMetrics )
366 return MetricsCache::ReadGlobal( fontFamily, fontStyle, globalMetrics );
369 void SlpPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
370 const std::string& fontStyle,
371 const Integration::GlobalMetrics& globalMetrics )
373 MetricsCache::WriteGlobal( fontFamily, fontStyle, globalMetrics);
376 bool SlpPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
377 const std::string& fontStyle,
378 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
380 return MetricsCache::Read( fontFamily, fontStyle, glyphMetricsContainer );
383 void SlpPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
384 const std::string& fontStyle,
385 const Integration::GlyphSet& glyphSet )
387 MetricsCache::Write( fontFamily, fontStyle, glyphSet );
390 void SlpPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
391 std::vector<std::string>& fileNames )
395 dp = opendir( directoryName.c_str() );
398 const std::string dot( "." );
399 const std::string dotDot( ".." );
407 const std::string fileName( de->d_name );
408 if( ( fileName != dot ) &&
409 ( fileName != dotDot ) )
411 fileNames.push_back( fileName );
418 Integration::BitmapPtr SlpPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) const
420 Integration::BitmapPtr glyphImage;
422 if( mResourceLoader )
424 glyphImage = mResourceLoader->GetGlyphImage( mFreeTypeHandle, fontFamily, fontStyle, fontSize, character );
430 } // namespace SlpPlatform