X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=text%2Fdali%2Finternal%2Ftext-abstraction%2Ffont-client-impl.cpp;h=1bed788f4386b25ce44908b15ee07eddbc6bc45b;hb=cc06bc9577950bea77b30f1482980b1e52131562;hp=04ec0b8be4d957638b9f82f85a6de6e486930c0b;hpb=bf5d34bf59bbb96a97c8eca4a0bcf4e2f16a54b5;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git diff --git a/text/dali/internal/text-abstraction/font-client-impl.cpp b/text/dali/internal/text-abstraction/font-client-impl.cpp index 04ec0b8..1bed788 100644 --- a/text/dali/internal/text-abstraction/font-client-impl.cpp +++ b/text/dali/internal/text-abstraction/font-client-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,19 +15,17 @@ * */ -// CLASS HEADER -#include "font-client-impl.h" +// CLASS HEADER +#include // EXTERNAL INCLUDES -#include -#include -#include FT_FREETYPE_H -#include FT_GLYPH_H -#include -#include +#ifndef DALI_PROFILE_UBUNTU +#include +#endif // INTERNAL INCLUDES -#include +#include +#include namespace Dali { @@ -38,490 +36,256 @@ namespace TextAbstraction namespace Internal { -struct FontClient::Plugin +FontClient::FontClient() +: mPlugin( NULL ), + mDpiHorizontal( 0 ), + mDpiVertical( 0 ) { - struct CacheItem - { - CacheItem( FontId id, FT_Face ftFace, const std::string& path, PointSize26Dot6 pointSize, FaceIndex face, const FontMetrics& metrics ) - : mFreeTypeFace( ftFace ), - mPath( path ), - mPointSize( pointSize ), - mFaceIndex( face ), - mMetrics( metrics ) - { - } - - FT_Face mFreeTypeFace; - std::string mPath; - PointSize26Dot6 mPointSize; - FaceIndex mFaceIndex; - FontMetrics mMetrics; - }; - - Plugin( unsigned int horizontalDpi, unsigned int verticalDpi ) - : mFreeTypeLibrary( NULL ), - mDpiHorizontal( horizontalDpi ), - mDpiVertical( verticalDpi ) - { - } - - ~Plugin() - { - FT_Done_FreeType( mFreeTypeLibrary ); - } +} - void Initialize() - { - int error = FT_Init_FreeType( &mFreeTypeLibrary ); - if( FT_Err_Ok != error ) - { - DALI_LOG_ERROR( "FreeType Init error: %d\n", error ); - } - } +FontClient::~FontClient() +{ + delete mPlugin; +} - void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi ) - { - mDpiHorizontal = horizontalDpi; - mDpiVertical = verticalDpi; - } +Dali::TextAbstraction::FontClient FontClient::Get() +{ + Dali::TextAbstraction::FontClient fontClientHandle; - void GetSystemFonts( FontList& systemFonts ) + Dali::SingletonService service( SingletonService::Get() ); + if ( service ) { - if( mSystemFonts.empty() ) + // Check whether the singleton is already created + Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::FontClient ) ); + if(handle) { - InitSystemFonts(); + // If so, downcast the handle + FontClient* impl = dynamic_cast< Dali::TextAbstraction::Internal::FontClient* >( handle.GetObjectPtr() ); + fontClientHandle = Dali::TextAbstraction::FontClient( impl ); } - - systemFonts = mSystemFonts; - } - - void InitSystemFonts() - { - FcFontSet* fontSet = GetFcFontSet(); - - if( fontSet ) + else // create and register the object { - mSystemFonts.reserve( fontSet->nfont ); - - for( int i = 0u; i < fontSet->nfont; ++i ) - { - FcPattern* fontPattern = fontSet->fonts[i]; - - std::string path; - - // Skip fonts with no path - if( GetFcString( fontPattern, FC_FILE, path ) ) - { - mSystemFonts.push_back( FontDescription() ); - FontDescription& fontDescription = mSystemFonts.back(); - - fontDescription.path = path; - - GetFcString( fontPattern, FC_FAMILY, fontDescription.family ); - GetFcString( fontPattern, FC_STYLE, fontDescription.style ); - } - } - - FcFontSetDestroy( fontSet ); + fontClientHandle = Dali::TextAbstraction::FontClient( new FontClient ); + service.Register( typeid( fontClientHandle ), fontClientHandle ); } } - _FcFontSet* GetFcFontSet() const - { - // create a new pattern. - // a pattern holds a set of names, each name refers to a property of the font - FcPattern* pattern = FcPatternCreate(); - - // create an object set used to define which properties are to be returned in the patterns from FcFontList. - FcObjectSet* objectSet = FcObjectSetCreate(); - - // build an object set from a list of property names - FcObjectSetAdd( objectSet, FC_FILE ); - FcObjectSetAdd( objectSet, FC_FAMILY ); - FcObjectSetAdd( objectSet, FC_STYLE ); - - // get a list of fonts - // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns - FcFontSet* fontset = FcFontList( NULL /* the default configuration is checked to be up to date, and used */, pattern, objectSet ); - - // clear up the object set - if( objectSet ) - { - FcObjectSetDestroy( objectSet ); - } - // clear up the pattern - if( pattern ) - { - FcPatternDestroy( pattern ); - } + return fontClientHandle; +} - return fontset; - } +void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi ) +{ + mDpiHorizontal = horizontalDpi; + mDpiVertical = verticalDpi; - bool GetFcString( const FcPattern* pattern, const char* n, std::string& string ) + // Allow DPI to be set without loading plugin + if( mPlugin ) { - FcChar8* file = NULL; - const FcResult retVal = FcPatternGetString( pattern, n, 0u, &file ); - - if( FcResultMatch == retVal ) - { - // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*. - string.assign( reinterpret_cast( file ) ); - - return true; - } - - return false; + mPlugin->SetDpi( horizontalDpi, verticalDpi ); } +} - bool FindSystemFont( Character charcode, FontDescription& systemFont ) - { - // TODO - Use FcCharSetHasChar() - - return false; - } +void FontClient::GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi ) +{ + horizontalDpi = mDpiHorizontal; + verticalDpi = mDpiVertical; +} - FontId GetFontId( const std::string& path, PointSize26Dot6 pointSize, FaceIndex faceIndex ) - { - FontId id( 0 ); +int FontClient::GetDefaultFontSize() +{ + int fontSize( -1 ); - if( NULL != mFreeTypeLibrary ) - { - FontId foundId(0); - if( FindFont( path, pointSize, faceIndex, foundId ) ) - { - id = foundId; - } - else - { - id = CreateFont( path, pointSize, faceIndex ); - } - } +#ifndef DALI_PROFILE_UBUNTU + vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize ); +#endif // DALI_PROFILE_UBUNTU - return id; - } + return fontSize; +} - GlyphIndex GetGlyphIndex( FontId fontId, Character charcode ) - { - GlyphIndex index( 0 ); +void FontClient::ResetSystemDefaults() +{ + CreatePlugin(); - if( fontId > 0 && - fontId-1 < mFontCache.size() ) - { - FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace; + mPlugin->ResetSystemDefaults(); +} - index = FT_Get_Char_Index( ftFace, charcode ); - } +void FontClient::GetDefaultFonts( FontList& defaultFonts ) +{ + CreatePlugin(); - return index; - } + mPlugin->GetDefaultFonts( defaultFonts ); +} - FontId CreateFont( const std::string& path, PointSize26Dot6 pointSize, FaceIndex faceIndex ) - { - FontId id( 0 ); +void FontClient::GetDefaultPlatformFontDescription( FontDescription& fontDescription ) +{ + CreatePlugin(); - // Create & cache new font face - FT_Face ftFace; - int error = FT_New_Face( mFreeTypeLibrary, - path.c_str(), - 0, - &ftFace ); + mPlugin->GetDefaultPlatformFontDescription( fontDescription ); +} - if( FT_Err_Ok == error ) - { - error = FT_Set_Char_Size( ftFace, - 0, - pointSize, - mDpiHorizontal, - mDpiVertical ); - - if( FT_Err_Ok == error ) - { - id = mFontCache.size() + 1; - - FT_Size_Metrics& ftMetrics = ftFace->size->metrics; - FontMetrics metrics( ftMetrics.ascender, ftMetrics.descender, ftMetrics.height ); - - mFontCache.push_back( CacheItem( id, ftFace, path, pointSize, faceIndex, metrics ) ); - } - else - { - DALI_LOG_ERROR( "FreeType Set_Char_Size error: %d for pointSize %d\n", pointSize ); - } - } - else - { - DALI_LOG_ERROR( "FreeType New_Face error: %d for %s\n", error, path.c_str() ); - } +void FontClient::GetDescription( FontId id, FontDescription& fontDescription ) +{ + CreatePlugin(); - return id; - } + mPlugin->GetDescription( id, fontDescription ); +} - FontId FindDefaultFont( Character charcode ) - { - // TODO - Return cached results based on script - return FontId(0); - } +PointSize26Dot6 FontClient::GetPointSize( FontId id ) +{ + CreatePlugin(); - void GetFontMetrics( FontId fontId, FontMetrics& metrics ) - { - if( fontId > 0 && - fontId-1 < mFontCache.size() ) - { - metrics = mFontCache[fontId-1].mMetrics; - } - else - { - DALI_LOG_ERROR( "Invalid font ID %d\n", fontId ); - } - } + return mPlugin->GetPointSize( id ); +} - bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal ) - { - bool success( true ); +bool FontClient::IsCharacterSupportedByFont( FontId fontId, Character character ) +{ + CreatePlugin(); - for( unsigned int i=0; i 0 && - fontId-1 < mFontCache.size() ) - { - FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace; - - int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_DEFAULT ); - - if( FT_Err_Ok == error ) - { - array[i].width = ftFace->glyph->metrics.width; - array[i].height = ftFace->glyph->metrics.height; - if( horizontal ) - { - array[i].xBearing = ftFace->glyph->metrics.horiBearingX; - array[i].yBearing = ftFace->glyph->metrics.horiBearingY; - array[i].advance = ftFace->glyph->metrics.horiAdvance; - } - else - { - array[i].xBearing = ftFace->glyph->metrics.vertBearingX; - array[i].yBearing = ftFace->glyph->metrics.vertBearingY; - array[i].advance = ftFace->glyph->metrics.vertAdvance; - } - } - else - { - success = false; - } - } - else - { - success = false; - } - } + return mPlugin->IsCharacterSupportedByFont( fontId, character ); +} - return success; - } +void FontClient::GetSystemFonts( FontList& systemFonts ) +{ + CreatePlugin(); - BitmapImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex ) - { - BitmapImage bitmap; + mPlugin->GetSystemFonts( systemFonts ); +} - if( fontId > 0 && - fontId-1 < mFontCache.size() ) - { - FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace; - - FT_Error error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_DEFAULT ); - if( FT_Err_Ok == error ) - { - FT_Glyph glyph; - error = FT_Get_Glyph( ftFace->glyph, &glyph ); - - // Convert to bitmap if necessary - if ( FT_Err_Ok == error ) - { - if( glyph->format != FT_GLYPH_FORMAT_BITMAP ) - { - error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 ); - } - else - { - DALI_LOG_ERROR( "FT_Glyph_To_Bitmap Failed with error: %d\n", error ); - } - } - else - { - DALI_LOG_ERROR( "FT_Get_Glyph Failed with error: %d\n", error ); - } - - if( FT_Err_Ok == error ) - { - // Access the underlying bitmap data - FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph; - ConvertBitmap( bitmap, bitmapGlyph->bitmap ); - } - - // Created FT_Glyph object must be released with FT_Done_Glyph - FT_Done_Glyph( glyph ); - } - else - { - DALI_LOG_ERROR( "FT_Load_Glyph Failed with error: %d\n", error ); - } - } +FontId FontClient::FindDefaultFont( Character charcode, + PointSize26Dot6 requestedPointSize, + bool preferColor ) +{ + CreatePlugin(); - return bitmap; - } + return mPlugin->FindDefaultFont( charcode, + requestedPointSize, + preferColor ); +} - void ConvertBitmap( BitmapImage& destBitmap, FT_Bitmap srcBitmap ) - { - if( srcBitmap.width*srcBitmap.rows > 0 ) - { - // TODO - Support all pixel modes - if( FT_PIXEL_MODE_GRAY == srcBitmap.pixel_mode ) - { - if( srcBitmap.pitch == srcBitmap.width ) - { - destBitmap = BitmapImage::New( srcBitmap.width, srcBitmap.rows, Pixel::L8 ); - - PixelBuffer* destBuffer = destBitmap.GetBuffer(); - memcpy( destBuffer, srcBitmap.buffer, srcBitmap.width*srcBitmap.rows ); - } - } - } - } +FontId FontClient::FindFallbackFont( Character charcode, + const FontDescription& preferredFontDescription, + PointSize26Dot6 requestedPointSize, + bool preferColor ) +{ + CreatePlugin(); -private: + return mPlugin->FindFallbackFont( charcode, + preferredFontDescription, + requestedPointSize, + preferColor ); +} - bool FindFont( const std::string& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& found ) const - { - for( unsigned int i=0; iIsScalable( path ); +} - FT_Library mFreeTypeLibrary; +bool FontClient::IsScalable( const FontDescription& fontDescription ) +{ + CreatePlugin(); - FontList mSystemFonts; + return mPlugin->IsScalable( fontDescription ); +} - std::vector mFontCache; +void FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes ) +{ + CreatePlugin(); - unsigned int mDpiHorizontal; - unsigned int mDpiVertical; -}; + mPlugin->GetFixedSizes( path, sizes ); +} -FontClient::FontClient() -: mPlugin( NULL ), - mDpiHorizontal( 0 ), - mDpiVertical( 0 ) +void FontClient::GetFixedSizes( const FontDescription& fontDescription, + Dali::Vector< PointSize26Dot6 >& sizes ) { + CreatePlugin(); + + mPlugin->GetFixedSizes( fontDescription, sizes ); } -FontClient::~FontClient() +FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex ) { - delete mPlugin; + CreatePlugin(); + + return mPlugin->GetFontId( path, + requestedPointSize, + faceIndex, + true ); } -Dali::TextAbstraction::FontClient FontClient::Get() +FontId FontClient::GetFontId( const FontDescription& fontDescription, + PointSize26Dot6 requestedPointSize, + FaceIndex faceIndex ) { - Dali::TextAbstraction::FontClient fontClientHandle; - - Dali::SingletonService service( SingletonService::Get() ); - if ( service ) - { - // Check whether the singleton is already created - Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::FontClient ) ); - if(handle) - { - // If so, downcast the handle - FontClient* impl = dynamic_cast< Dali::TextAbstraction::Internal::FontClient* >( handle.GetObjectPtr() ); - fontClientHandle = Dali::TextAbstraction::FontClient( impl ); - } - else // create and register the object - { - fontClientHandle = Dali::TextAbstraction::FontClient( new FontClient ); - service.Register( typeid( fontClientHandle ), fontClientHandle ); - } - } + CreatePlugin(); - return fontClientHandle; + return mPlugin->GetFontId( fontDescription, + requestedPointSize, + faceIndex ); } -void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi ) +void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics ) { - mDpiHorizontal = horizontalDpi; - mDpiVertical = verticalDpi; + CreatePlugin(); - // Allow DPI to be set without loading plugin - if( mPlugin ) - { - mPlugin->SetDpi( horizontalDpi, verticalDpi ); - } + return mPlugin->GetFontMetrics( fontId, metrics ); } -void FontClient::GetSystemFonts( FontList& systemFonts ) +GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode ) { CreatePlugin(); - mPlugin->GetSystemFonts( systemFonts ); + return mPlugin->GetGlyphIndex( fontId, charcode ); } -bool FontClient::FindSystemFont( Character charcode, FontDescription& systemFont ) +bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal ) { CreatePlugin(); - return mPlugin->FindSystemFont( charcode, systemFont ); + return mPlugin->GetGlyphMetrics( array, size, type, horizontal ); } -FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex ) +void FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth ) { CreatePlugin(); - return mPlugin->GetFontId( path, pointSize, faceIndex ); + mPlugin->CreateBitmap( fontId, glyphIndex, data, outlineWidth ); } -FontId FontClient::FindDefaultFont( Character charcode ) +PixelData FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth ) { CreatePlugin(); - return mPlugin->FindDefaultFont( charcode ); + return mPlugin->CreateBitmap( fontId, glyphIndex, outlineWidth ); } -void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics ) +void FontClient::CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight ) { CreatePlugin(); - return mPlugin->GetFontMetrics( fontId, metrics ); + return mPlugin->CreateVectorBlob( fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight ); } -GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode ) +const GlyphInfo& FontClient::GetEllipsisGlyph( PointSize26Dot6 requestedPointSize ) { CreatePlugin(); - return mPlugin->GetGlyphIndex( fontId, charcode ); + return mPlugin->GetEllipsisGlyph( requestedPointSize ); } -bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal ) +bool FontClient::IsColorGlyph( FontId fontId, GlyphIndex glyphIndex ) { CreatePlugin(); - return mPlugin->GetGlyphMetrics( array, size, horizontal ); + return mPlugin->IsColorGlyph( fontId, glyphIndex ); } -BitmapImage FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex ) +bool FontClient::AddCustomFontDirectory( const char* path ) { CreatePlugin(); - return mPlugin->CreateBitmap( fontId, glyphIndex ); + return mPlugin->AddCustomFontDirectory( path ); } void FontClient::CreatePlugin() @@ -529,12 +293,11 @@ void FontClient::CreatePlugin() if( !mPlugin ) { mPlugin = new Plugin( mDpiHorizontal, mDpiVertical ); - mPlugin->Initialize(); } } } // namespace Internal -} // namespace FontClient +} // namespace TextAbstraction } // namespace Dali