2 * Copyright (c) 2015 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.
19 #include <dali/internal/text-abstraction/font-client-plugin-impl.h>
22 #include <dali/devel-api/text-abstraction/font-list.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/platform-abstraction.h>
27 #include <dali/internal/text-abstraction/font-client-helper.h>
28 #include <adaptor-impl.h>
31 #include <fontconfig/fontconfig.h>
36 #if defined(DEBUG_ENABLED)
37 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_FONT_CLIENT");
41 * Conversion from Fractional26.6 to float
43 const float FROM_266 = 1.0f / 64.0f;
45 const std::string FONT_FORMAT( "TrueType" );
46 const std::string DEFAULT_FONT_FAMILY_NAME( "Tizen" );
47 const int DEFAULT_FONT_WIDTH = 100; // normal
48 const int DEFAULT_FONT_WEIGHT = 80; // normal
49 const int DEFAULT_FONT_SLANT = 0; // normal
51 const uint32_t ELLIPSIS_CHARACTER = 0x2026;
53 const bool FONT_FIXED_SIZE_BITMAP( true );
55 // http://www.freedesktop.org/software/fontconfig/fontconfig-user.html
66 const int FONT_WIDTH_TYPE_TO_INT[] = { 50, 63, 75, 87, 100, 113, 125, 150, 200 };
67 const unsigned int NUM_FONT_WIDTH_TYPE = sizeof( FONT_WIDTH_TYPE_TO_INT ) / sizeof( int );
70 // ULTRA_LIGHT, EXTRA_LIGHT 40
72 // DEMI_LIGHT, SEMI_LIGHT 55
76 // DEMI_BOLD, SEMI_BOLD 180
78 // ULTRA_BOLD, EXTRA_BOLD 205
79 // BLACK, HEAVY, EXTRA_BLACK 210
80 const int FONT_WEIGHT_TYPE_TO_INT[] = { 0, 40, 50, 55, 75, 80, 100, 180, 200, 205, 210 };
81 const unsigned int NUM_FONT_WEIGHT_TYPE = sizeof( FONT_WEIGHT_TYPE_TO_INT ) / sizeof( int );
86 const int FONT_SLANT_TYPE_TO_INT[] = { 0, 100, 110 };
87 const unsigned int NUM_FONT_SLANT_TYPE = sizeof( FONT_SLANT_TYPE_TO_INT ) / sizeof( int );
96 namespace TextAbstraction
103 * @brief Returns the FontWidth's enum index for the given width value.
105 * @param[in] width The width value.
107 * @return The FontWidth's enum index.
109 FontWidth::Type IntToWidthType( int width )
111 return static_cast<FontWidth::Type>( ValueToIndex( width, FONT_WIDTH_TYPE_TO_INT, NUM_FONT_WIDTH_TYPE - 1u ) );
115 * @brief Returns the FontWeight's enum index for the given weight value.
117 * @param[in] weight The weight value.
119 * @return The FontWeight's enum index.
121 FontWeight::Type IntToWeightType( int weight )
123 return static_cast<FontWeight::Type>( ValueToIndex( weight, FONT_WEIGHT_TYPE_TO_INT, NUM_FONT_WEIGHT_TYPE - 1u ) );
127 * @brief Returns the FontSlant's enum index for the given slant value.
129 * @param[in] slant The slant value.
131 * @return The FontSlant's enum index.
133 FontSlant::Type IntToSlantType( int slant )
135 return static_cast<FontSlant::Type>( ValueToIndex( slant, FONT_SLANT_TYPE_TO_INT, NUM_FONT_SLANT_TYPE - 1u ) );
138 FontClient::Plugin::FallbackCacheItem::FallbackCacheItem( const FontDescription& font, FontList* list )
139 : fontDescription( font ),
140 fallbackFonts( list )
144 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem( const FontDescription& fontDescription,
145 FontDescriptionId index )
146 : fontDescription( fontDescription ),
151 FontClient::Plugin::FontIdCacheItem::FontIdCacheItem( FontDescriptionId validatedFontId,
152 PointSize26Dot6 pointSize,
154 : validatedFontId( validatedFontId ),
155 pointSize( pointSize ),
160 FontClient::Plugin::CacheItem::CacheItem( FT_Face ftFace,
161 const FontPath& path,
162 PointSize26Dot6 pointSize,
164 const FontMetrics& metrics )
165 : mFreeTypeFace( ftFace ),
167 mPointSize( pointSize ),
170 mFixedWidthPixels( 0.0f ),
171 mFixedHeightPixels( 0.0f ),
172 mIsFixedSizeBitmap( false )
176 FontClient::Plugin::CacheItem::CacheItem( FT_Face ftFace,
177 const FontPath& path,
178 PointSize26Dot6 pointSize,
180 const FontMetrics& metrics,
183 : mFreeTypeFace( ftFace ),
185 mPointSize( pointSize ),
188 mFixedWidthPixels( fixedWidth ),
189 mFixedHeightPixels( fixedHeight ),
190 mIsFixedSizeBitmap( true )
194 FontClient::Plugin::Plugin( unsigned int horizontalDpi,
195 unsigned int verticalDpi )
196 : mFreeTypeLibrary( NULL ),
197 mDpiHorizontal( horizontalDpi ),
198 mDpiVertical( verticalDpi ),
199 mDefaultFontDescription(),
203 mValidatedFontCache(),
204 mFontDescriptionCache( 1u ),
207 mDefaultFontDescriptionCached( false )
209 int error = FT_Init_FreeType( &mFreeTypeLibrary );
210 if( FT_Err_Ok != error )
212 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
216 FontClient::Plugin::~Plugin()
218 for( std::vector<FallbackCacheItem>::iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
222 FallbackCacheItem& item = *it;
224 if( item.fallbackFonts )
226 delete item.fallbackFonts;
227 item.fallbackFonts = NULL;
231 FT_Done_FreeType( mFreeTypeLibrary );
234 void FontClient::Plugin::SetDpi( unsigned int horizontalDpi,
235 unsigned int verticalDpi )
237 mDpiHorizontal = horizontalDpi;
238 mDpiVertical = verticalDpi;
241 void FontClient::Plugin::ResetSystemDefaults()
243 mDefaultFontDescriptionCached = false;
246 void FontClient::Plugin::SetFontList( const FontDescription& fontDescription, FontList& fontList )
248 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::SetFontList family(%s)\n", fontDescription.family.c_str() );
252 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
254 FcResult result = FcResultMatch;
256 // Match the pattern.
257 FcFontSet* fontSet = FcFontSort( NULL /* use default configure */,
259 false /* don't trim */,
263 if( NULL != fontSet )
265 // Reserve some space to avoid reallocations.
266 fontList.reserve( fontSet->nfont );
268 for( int i = 0u; i < fontSet->nfont; ++i )
270 FcPattern* fontPattern = fontSet->fonts[i];
274 // Skip fonts with no path
275 if( GetFcString( fontPattern, FC_FILE, path ) )
277 fontList.push_back( FontDescription() );
278 FontDescription& newFontDescription = fontList.back();
280 newFontDescription.path = path;
285 GetFcString( fontPattern, FC_FAMILY, newFontDescription.family );
286 GetFcInt( fontPattern, FC_WIDTH, width );
287 GetFcInt( fontPattern, FC_WEIGHT, weight );
288 GetFcInt( fontPattern, FC_SLANT, slant );
289 newFontDescription.width = IntToWidthType( width );
290 newFontDescription.weight = IntToWeightType( weight );
291 newFontDescription.slant = IntToSlantType( slant );
295 FcFontSetDestroy( fontSet );
298 FcPatternDestroy( fontFamilyPattern );
301 void FontClient::Plugin::GetDefaultFonts( FontList& defaultFonts )
303 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetDefaultFonts mDefaultFonts(%s)\n", ( mDefaultFonts.empty()?"empty":"valid" ) );
305 if( mDefaultFonts.empty() )
307 FontDescription fontDescription;
308 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
309 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
310 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
311 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
312 SetFontList( fontDescription, mDefaultFonts );
315 defaultFonts = mDefaultFonts;
318 void FontClient::Plugin::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetDefaultPlatformFontDescription\n");
322 if( !mDefaultFontDescriptionCached )
324 FcInitReinitialize(); // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
326 FcPattern* matchPattern = FcPatternCreate();
327 FcConfigSubstitute(NULL, matchPattern, FcMatchPattern);
328 FcDefaultSubstitute( matchPattern );
330 MatchFontDescriptionToPattern( matchPattern, mDefaultFontDescription );
331 FcPatternDestroy( matchPattern );
333 mDefaultFontDescriptionCached = true;
336 fontDescription.path = mDefaultFontDescription.path;
337 fontDescription.family = mDefaultFontDescription.family;
338 fontDescription.width = mDefaultFontDescription.width;
339 fontDescription.weight = mDefaultFontDescription.weight;
340 fontDescription.slant = mDefaultFontDescription.slant;
343 void FontClient::Plugin::GetSystemFonts( FontList& systemFonts )
345 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetSystemFonts\n");
347 if( mSystemFonts.empty() )
352 systemFonts = mSystemFonts;
355 void FontClient::Plugin::GetDescription( FontId id,
356 FontDescription& fontDescription ) const
358 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
359 endIt = mFontIdCache.end();
363 const FontIdCacheItem& item = *it;
365 if( item.fontId == id )
367 fontDescription = *( mFontDescriptionCache.begin() + item.validatedFontId );
372 DALI_LOG_ERROR( "FontClient::Plugin::GetDescription. No description found for the font ID %d\n", id );
375 PointSize26Dot6 FontClient::Plugin::GetPointSize( FontId id )
377 const FontId index = id - 1u;
380 index < mFontCache.size() )
382 return ( *( mFontCache.begin() + index ) ).mPointSize;
386 DALI_LOG_ERROR( "FontClient::Plugin::GetPointSize. Invalid font ID %d\n", id );
389 return TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
392 FontId FontClient::Plugin::FindFontForCharacter( const FontList& fontList,
394 PointSize26Dot6 requestedSize,
397 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFontForCharacter\n");
400 bool foundColor(false);
402 // Traverse the list of fonts.
403 // Check for each default font if supports the character.
405 for( FontList::const_iterator it = fontList.begin(), endIt = fontList.end();
409 const FontDescription& description = *it;
411 FcPattern* pattern = CreateFontFamilyPattern( description );
413 FcResult result = FcResultMatch;
414 FcPattern* match = FcFontMatch( NULL /* use default configure */, pattern, &result );
416 FcCharSet* charSet = NULL;
417 FcPatternGetCharSet( match, FC_CHARSET, 0u, &charSet );
419 if( FcCharSetHasChar( charSet, charcode ) )
421 Vector< PointSize26Dot6 > fixedSizes;
422 GetFixedSizes( description,
425 const Vector< PointSize26Dot6 >::SizeType count = fixedSizes.Count();
428 // If the font is not scalable, pick the largest size <= requestedSize
429 PointSize26Dot6 size = fixedSizes[0];
430 for( unsigned int i=1; i<count; ++i )
432 if( fixedSizes[i] <= requestedSize &&
433 fixedSizes[i] > size )
435 size = fixedSizes[i];
438 requestedSize = size;
441 fontId = GetFontId( description,
447 BufferImage bitmap = CreateBitmap( fontId, GetGlyphIndex(fontId,charcode) );
449 Pixel::BGRA8888 == bitmap.GetPixelFormat() )
455 // Keep going unless we prefer a different (color) font
456 if( !preferColor || foundColor )
458 FcPatternDestroy( match );
459 FcPatternDestroy( pattern );
464 FcPatternDestroy( match );
465 FcPatternDestroy( pattern );
471 FontId FontClient::Plugin::FindDefaultFont( Character charcode,
472 PointSize26Dot6 requestedSize,
475 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindDefaultFont DefaultFontsList(%s)\n", (mDefaultFonts.empty()?"empty":"created") );
479 // Create the list of default fonts if it has not been created.
480 if( mDefaultFonts.empty() )
482 FontDescription fontDescription;
483 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
484 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
485 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
486 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
487 SetFontList( fontDescription, mDefaultFonts );
490 // Traverse the list of default fonts.
491 // Check for each default font if supports the character.
492 fontId = FindFontForCharacter( mDefaultFonts, charcode, requestedSize, preferColor );
497 FontId FontClient::Plugin::FindFallbackFont( FontId preferredFont,
499 PointSize26Dot6 requestedSize,
502 // The font id to be returned.
505 FontDescription fontDescription;
506 GetDescription( preferredFont, fontDescription );
508 // Check first if the font's description has been queried before.
509 FontList* fontList( NULL );
511 if( !FindFallbackFontList( fontDescription, fontList ) )
513 fontList = new FontList;
514 SetFontList( fontDescription, *fontList );
516 // Add the font-list to the cache.
517 mFallbackCache.push_back( FallbackCacheItem(fontDescription, fontList) );
522 fontId = FindFontForCharacter( *fontList, charcode, requestedSize, preferColor );
528 FontId FontClient::Plugin::GetFontId( const FontPath& path,
529 PointSize26Dot6 pointSize,
531 bool cacheDescription )
533 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId fontPatch:%s\n", path.c_str() );
537 if( NULL != mFreeTypeLibrary )
540 if( FindFont( path, pointSize, faceIndex, foundId ) )
546 id = CreateFont( path, pointSize, faceIndex, cacheDescription );
553 FontId FontClient::Plugin::GetFontId( const FontDescription& fontDescription,
554 PointSize26Dot6 pointSize,
555 FaceIndex faceIndex )
557 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId font family(%s)\n", fontDescription.family.c_str() );
559 // This method uses three vectors which caches:
560 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
561 // * The path to font file names.
562 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
564 // 1) Checks in the cache if the font's description has been validated before.
565 // If it was it gets an index to the vector with paths to font file names. Otherwise,
566 // retrieves using font config a path to a font file name which matches with the
567 // font's description. The path is stored in the cache.
569 // 2) Checks in the cache if the pair 'font point size, index to the vector with paths to
570 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
571 // the GetFontId() method with the path to the font file name and the point size to
574 // The font id to be returned.
577 // Check first if the font's description have been validated before.
578 FontDescriptionId validatedFontId = 0u;
580 if( !FindValidatedFont( fontDescription,
583 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId Validating Font\n");
585 // Use font config to validate the font's description.
586 ValidateFont( fontDescription,
590 // Check if exists a pair 'validatedFontId, pointSize' in the cache.
591 if( !FindFont( validatedFontId, pointSize, fontId ) )
593 // Retrieve the font file name path.
594 const FontDescription& description = *( mFontDescriptionCache.begin() + validatedFontId );
596 // Retrieve the font id. Do not cache the description as it has been already cached.
597 fontId = GetFontId( description.path,
602 // Cache the pair 'validatedFontId, pointSize' to improve the following queries.
603 mFontIdCache.push_back( FontIdCacheItem( validatedFontId,
611 void FontClient::Plugin::ValidateFont( const FontDescription& fontDescription,
612 FontDescriptionId& validatedFontId )
614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::ValidateFont Validating Font family(%s) \n", fontDescription.family.c_str() );
616 // Create a font pattern.
617 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
619 FontDescription description;
621 bool matched = MatchFontDescriptionToPattern( fontFamilyPattern, description );
622 FcPatternDestroy( fontFamilyPattern );
626 // Set the index to the vector of paths to font file names.
627 validatedFontId = mFontDescriptionCache.size();
629 // Add the path to the cache.
630 mFontDescriptionCache.push_back( description );
632 // Cache the index and the font's description.
633 FontDescriptionCacheItem item( description,
636 mValidatedFontCache.push_back( item );
640 DALI_LOG_ERROR( "FontClient::Plugin::ValidateFont failed for font %s %d %d %d\n",
641 fontDescription.family.c_str(),
642 fontDescription.width,
643 fontDescription.weight,
644 fontDescription.slant );
647 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::ValidateFont validatedFontId(%u) font family(%s)\n", validatedFontId, fontDescription.family.c_str() );
650 void FontClient::Plugin::GetFontMetrics( FontId fontId,
651 FontMetrics& metrics,
652 int desiredFixedSize )
655 fontId-1 < mFontCache.size() )
657 const CacheItem& font = mFontCache[fontId-1];
659 metrics = font.mMetrics;
661 // Adjust the metrics if the fixed-size font should be down-scaled
662 if( font.mIsFixedSizeBitmap &&
663 ( desiredFixedSize > 0 ) )
665 float scaleFactor = static_cast<float>(desiredFixedSize) / static_cast<float>(font.mFixedHeightPixels);
667 metrics.ascender *= scaleFactor;
668 metrics.descender *= scaleFactor;
669 metrics.height *= scaleFactor;
670 metrics.underlinePosition *= scaleFactor;
671 metrics.underlineThickness *= scaleFactor;
676 DALI_LOG_ERROR( "Invalid font ID %d\n", fontId );
680 GlyphIndex FontClient::Plugin::GetGlyphIndex( FontId fontId,
683 GlyphIndex index( 0 );
686 fontId-1 < mFontCache.size() )
688 FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace;
690 index = FT_Get_Char_Index( ftFace, charcode );
696 bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
699 int desiredFixedSize )
701 bool success( true );
703 for( unsigned int i=0; i<size; ++i )
705 FontId fontId = array[i].fontId;
708 fontId-1 < mFontCache.size() )
710 const CacheItem& font = mFontCache[fontId-1];
712 FT_Face ftFace = font.mFreeTypeFace;
714 #ifdef FREETYPE_BITMAP_SUPPORT
715 // Check to see if we should be loading a Fixed Size bitmap?
716 if ( font.mIsFixedSizeBitmap )
718 int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_COLOR );
719 if ( FT_Err_Ok == error )
721 array[i].width = font.mFixedWidthPixels;
722 array[i].height = font.mFixedHeightPixels;
723 array[i].advance = font.mFixedWidthPixels;
724 array[i].xBearing = 0.0f;
725 array[i].yBearing = font.mFixedHeightPixels;
727 // Adjust the metrics if the fixed-size font should be down-scaled
728 if( desiredFixedSize > 0 )
730 float scaleFactor = static_cast<float>(desiredFixedSize) / static_cast<float>(font.mFixedHeightPixels);
732 array[i].width *= scaleFactor;
733 array[i].height *= scaleFactor;
734 array[i].advance *= scaleFactor;
735 array[i].xBearing *= scaleFactor;
736 array[i].yBearing *= scaleFactor;
738 array[i].scaleFactor = scaleFactor;
743 DALI_LOG_ERROR( "FreeType Bitmap Load_Glyph error %d\n", error );
750 int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_DEFAULT );
752 if( FT_Err_Ok == error )
754 array[i].width = static_cast< float >( ftFace->glyph->metrics.width ) * FROM_266;
755 array[i].height = static_cast< float >( ftFace->glyph->metrics.height ) * FROM_266 ;
758 array[i].xBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingX ) * FROM_266;
759 array[i].yBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingY ) * FROM_266;
763 array[i].xBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingX ) * FROM_266;
764 array[i].yBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingY ) * FROM_266;
782 BufferImage FontClient::Plugin::CreateBitmap( FontId fontId,
783 GlyphIndex glyphIndex )
788 fontId-1 < mFontCache.size() )
790 FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace;
794 #ifdef FREETYPE_BITMAP_SUPPORT
795 // Check to see if this is fixed size bitmap
796 if ( mFontCache[fontId-1].mIsFixedSizeBitmap )
798 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
803 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_DEFAULT );
805 if( FT_Err_Ok == error )
808 error = FT_Get_Glyph( ftFace->glyph, &glyph );
810 // Convert to bitmap if necessary
811 if ( FT_Err_Ok == error )
813 if( glyph->format != FT_GLYPH_FORMAT_BITMAP )
815 error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
816 if ( FT_Err_Ok == error )
818 FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph;
819 ConvertBitmap( bitmap, bitmapGlyph->bitmap );
823 DALI_LOG_ERROR( "FT_Get_Glyph Failed with error: %d\n", error );
828 ConvertBitmap( bitmap, ftFace->glyph->bitmap );
831 // Created FT_Glyph object must be released with FT_Done_Glyph
832 FT_Done_Glyph( glyph );
837 DALI_LOG_ERROR( "FT_Load_Glyph Failed with error: %d\n", error );
844 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph( PointSize26Dot6 pointSize )
846 // First look into the cache if there is an ellipsis glyph for the requested point size.
847 for( Vector<EllipsisItem>::ConstIterator it = mEllipsisCache.Begin(),
848 endIt = mEllipsisCache.End();
852 const EllipsisItem& item = *it;
854 if( fabsf( item.size - pointSize ) < Math::MACHINE_EPSILON_1000 )
856 // Use the glyph in the cache.
861 // No glyph has been found. Create one.
862 mEllipsisCache.PushBack( EllipsisItem() );
863 EllipsisItem& item = *( mEllipsisCache.End() - 1u );
865 item.size = pointSize;
867 // Find a font for the ellipsis glyph.
868 item.glyph.fontId = FindDefaultFont( ELLIPSIS_CHARACTER,
872 // Set the character index to access the glyph inside the font.
873 item.glyph.index = FT_Get_Char_Index( mFontCache[item.glyph.fontId-1].mFreeTypeFace,
874 ELLIPSIS_CHARACTER );
876 GetGlyphMetrics( &item.glyph, 1u, true, 0 );
881 void FontClient::Plugin::InitSystemFonts()
883 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::InitSystemFonts \n");
885 FcFontSet* fontSet = GetFcFontSet();
889 // Reserve some space to avoid reallocations.
890 mSystemFonts.reserve( fontSet->nfont );
892 for( int i = 0u; i < fontSet->nfont; ++i )
894 FcPattern* fontPattern = fontSet->fonts[i];
898 // Skip fonts with no path
899 if( GetFcString( fontPattern, FC_FILE, path ) )
901 mSystemFonts.push_back( FontDescription() );
902 FontDescription& fontDescription = mSystemFonts.back();
904 fontDescription.path = path;
909 GetFcString( fontPattern, FC_FAMILY, fontDescription.family );
910 GetFcInt( fontPattern, FC_WIDTH, width );
911 GetFcInt( fontPattern, FC_WEIGHT, weight );
912 GetFcInt( fontPattern, FC_SLANT, slant );
913 fontDescription.width = IntToWidthType( width );
914 fontDescription.weight = IntToWeightType( weight );
915 fontDescription.slant = IntToSlantType( slant );
916 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::InitSystemFonts font family(%s)\n", fontDescription.family.c_str() );
921 FcFontSetDestroy( fontSet );
925 bool FontClient::Plugin::MatchFontDescriptionToPattern( FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription )
927 FcResult result = FcResultMatch;
928 FcPattern* match = FcFontMatch( NULL /* use default configure */, pattern, &result );
937 GetFcString( match, FC_FILE, fontDescription.path );
938 GetFcString( match, FC_FAMILY, fontDescription.family );
939 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::MatchFontDescriptionToPattern matched:%s \n", fontDescription.family.c_str());
940 GetFcInt( match, FC_WIDTH, width );
941 GetFcInt( match, FC_WEIGHT, weight );
942 GetFcInt( match, FC_SLANT, slant );
943 fontDescription.width = IntToWidthType( width );
944 fontDescription.weight = IntToWeightType( weight );
945 fontDescription.slant = IntToSlantType( slant );
946 // destroyed the matched pattern
947 FcPatternDestroy( match );
954 FcPattern* FontClient::Plugin::CreateFontFamilyPattern( const FontDescription& fontDescription )
956 // create the cached font family lookup pattern
957 // a pattern holds a set of names, each name refers to a property of the font
958 FcPattern* fontFamilyPattern = FcPatternCreate();
960 // add a property to the pattern for the font family
961 FcPatternAddString( fontFamilyPattern, FC_FAMILY, reinterpret_cast<const FcChar8*>( fontDescription.family.c_str() ) );
963 FcPatternAddInteger( fontFamilyPattern, FC_WIDTH, FONT_WIDTH_TYPE_TO_INT[fontDescription.width] );
964 FcPatternAddInteger( fontFamilyPattern, FC_WEIGHT, FONT_WEIGHT_TYPE_TO_INT[fontDescription.weight] );
965 FcPatternAddInteger( fontFamilyPattern, FC_SLANT, FONT_SLANT_TYPE_TO_INT[fontDescription.slant] );
967 // Add a property of the pattern, to say we want to match TrueType fonts
968 FcPatternAddString( fontFamilyPattern , FC_FONTFORMAT, reinterpret_cast<const FcChar8*>( FONT_FORMAT.c_str() ) );
970 // modify the config, with the mFontFamilyPatterm
971 FcConfigSubstitute( NULL /* use default configure */, fontFamilyPattern, FcMatchPattern );
973 // provide default values for unspecified properties in the font pattern
974 // e.g. patterns without a specified style or weight are set to Medium
975 FcDefaultSubstitute( fontFamilyPattern );
977 return fontFamilyPattern;
980 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
982 // create a new pattern.
983 // a pattern holds a set of names, each name refers to a property of the font
984 FcPattern* pattern = FcPatternCreate();
986 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
987 FcObjectSet* objectSet = FcObjectSetCreate();
989 // build an object set from a list of property names
990 FcObjectSetAdd( objectSet, FC_FILE );
991 FcObjectSetAdd( objectSet, FC_FAMILY );
992 FcObjectSetAdd( objectSet, FC_WIDTH );
993 FcObjectSetAdd( objectSet, FC_WEIGHT );
994 FcObjectSetAdd( objectSet, FC_SLANT );
996 // get a list of fonts
997 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
998 FcFontSet* fontset = FcFontList( NULL /* the default configuration is checked to be up to date, and used */, pattern, objectSet );
1000 // clear up the object set
1003 FcObjectSetDestroy( objectSet );
1005 // clear up the pattern
1008 FcPatternDestroy( pattern );
1014 bool FontClient::Plugin::GetFcString( const FcPattern* const pattern,
1015 const char* const n,
1016 std::string& string )
1018 FcChar8* file = NULL;
1019 const FcResult retVal = FcPatternGetString( pattern, n, 0u, &file );
1021 if( FcResultMatch == retVal )
1023 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1024 string.assign( reinterpret_cast<const char*>( file ) );
1032 bool FontClient::Plugin::GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal )
1034 const FcResult retVal = FcPatternGetInteger( pattern, n, 0u, &intVal );
1036 if( FcResultMatch == retVal )
1044 FontId FontClient::Plugin::CreateFont( const FontPath& path,
1045 PointSize26Dot6 pointSize,
1046 FaceIndex faceIndex,
1047 bool cacheDescription )
1051 // Create & cache new font face
1053 int error = FT_New_Face( mFreeTypeLibrary,
1058 if( FT_Err_Ok == error )
1060 // Check to see if the font contains fixed sizes?
1061 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1063 // Ensure this size is available
1064 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1066 if ( static_cast<FT_Pos>(pointSize) == ftFace->available_sizes[ i ].size )
1068 // Tell Freetype to use this size
1069 error = FT_Select_Size( ftFace, i );
1070 if ( FT_Err_Ok != error )
1072 DALI_LOG_ERROR( "FreeType Select_Size error: %d\n", error );
1076 float fixedWidth = static_cast< float >( ftFace->available_sizes[ i ].width );
1077 float fixedHeight = static_cast< float >( ftFace->available_sizes[ i ].height );
1079 // Indicate that the font is a fixed sized bitmap
1080 FontMetrics metrics( fixedHeight, // The ascender in pixels.
1082 fixedHeight, // The height in pixels.
1086 mFontCache.push_back( CacheItem( ftFace, path, pointSize, faceIndex, metrics, fixedWidth, fixedHeight ) );
1087 id = mFontCache.size();
1089 if( cacheDescription )
1091 FontDescription description;
1092 description.path = path;
1093 description.family = FontFamily( ftFace->family_name );
1095 // Note FreeType doesn't give too much info to build a proper font style.
1096 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1098 description.slant = FontSlant::ITALIC;
1100 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1102 description.weight = FontWeight::BOLD;
1105 mFontDescriptionCache.push_back( description );
1112 // Can't find this size
1113 std::stringstream sizes;
1114 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1120 sizes << ftFace->available_sizes[ i ].size;
1122 DALI_LOG_ERROR( "FreeType Font: %s, does not contain Bitmaps of size: %d. Available sizes are: %s\n",
1123 path.c_str(), pointSize, sizes.str().c_str() );
1127 error = FT_Set_Char_Size( ftFace,
1133 if( FT_Err_Ok == error )
1136 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1138 FontMetrics metrics( static_cast< float >( ftMetrics.ascender ) * FROM_266,
1139 static_cast< float >( ftMetrics.descender ) * FROM_266,
1140 static_cast< float >( ftMetrics.height ) * FROM_266,
1141 static_cast< float >( ftFace->underline_position ) * FROM_266,
1142 static_cast< float >( ftFace->underline_thickness ) * FROM_266 );
1144 mFontCache.push_back( CacheItem( ftFace, path, pointSize, faceIndex, metrics ) );
1145 id = mFontCache.size();
1147 if( cacheDescription )
1149 FontDescription description;
1150 description.path = path;
1151 description.family = FontFamily( ftFace->family_name );
1153 // Note FreeType doesn't give too much info to build a proper font style.
1154 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1156 description.slant = FontSlant::ITALIC;
1158 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1160 description.weight = FontWeight::BOLD;
1163 mFontDescriptionCache.push_back( description );
1168 DALI_LOG_ERROR( "FreeType Set_Char_Size error: %d for pointSize %d\n", error, pointSize );
1174 DALI_LOG_ERROR( "FreeType New_Face error: %d for %s\n", error, path.c_str() );
1180 void FontClient::Plugin::ConvertBitmap( BufferImage& destBitmap,
1181 FT_Bitmap srcBitmap )
1183 if( srcBitmap.width*srcBitmap.rows > 0 )
1185 switch( srcBitmap.pixel_mode )
1187 case FT_PIXEL_MODE_GRAY:
1189 if( srcBitmap.pitch == static_cast< int >( srcBitmap.width ) )
1191 destBitmap = BufferImage::New( srcBitmap.width, srcBitmap.rows, Pixel::L8 );
1193 PixelBuffer* destBuffer = destBitmap.GetBuffer();
1196 memcpy( destBuffer, srcBitmap.buffer, srcBitmap.width*srcBitmap.rows );
1200 DALI_LOG_ERROR( "GetBuffer returns null\n" );
1206 #ifdef FREETYPE_BITMAP_SUPPORT
1207 case FT_PIXEL_MODE_BGRA:
1209 if ( srcBitmap.pitch == static_cast< int >( srcBitmap.width << 2 ) )
1211 destBitmap = BufferImage::New( srcBitmap.width, srcBitmap.rows, Pixel::BGRA8888 );
1213 PixelBuffer* destBuffer = destBitmap.GetBuffer();
1216 memcpy( destBuffer, srcBitmap.buffer, srcBitmap.width*srcBitmap.rows*4 );
1220 DALI_LOG_ERROR( "GetBuffer returns null\n" );
1228 DALI_LOG_ERROR( "FontClient Unable to create Bitmap of this PixelType\n" );
1235 bool FontClient::Plugin::FindFont( const FontPath& path,
1236 PointSize26Dot6 pointSize,
1237 FaceIndex faceIndex,
1238 FontId& fontId ) const
1241 for( std::vector<CacheItem>::const_iterator it = mFontCache.begin(),
1242 endIt = mFontCache.end();
1246 const CacheItem& cacheItem = *it;
1248 if( cacheItem.mPointSize == pointSize &&
1249 cacheItem.mFaceIndex == faceIndex &&
1250 cacheItem.mPath == path )
1260 bool FontClient::Plugin::FindValidatedFont( const FontDescription& fontDescription,
1261 FontDescriptionId& validatedFontId )
1263 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont fontDescription family(%s)\n", fontDescription.family.c_str() );
1265 validatedFontId = 0u;
1267 for( std::vector<FontDescriptionCacheItem>::const_iterator it = mValidatedFontCache.begin(),
1268 endIt = mValidatedFontCache.end();
1272 const FontDescriptionCacheItem& item = *it;
1274 if( !fontDescription.family.empty() &&
1275 ( fontDescription.family == item.fontDescription.family ) &&
1276 ( fontDescription.width == item.fontDescription.width ) &&
1277 ( fontDescription.weight == item.fontDescription.weight ) &&
1278 ( fontDescription.slant == item.fontDescription.slant ) )
1280 validatedFontId = item.index;
1282 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont validated font family(%s) font id (%u) \n", fontDescription.family.c_str(), validatedFontId );
1288 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont NOT VALIDATED return false\n" );
1293 bool FontClient::Plugin::FindFallbackFontList( const FontDescription& fontDescription,
1294 FontList*& fontList )
1296 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList fontDescription family(%s)\n", fontDescription.family.c_str() );
1300 for( std::vector<FallbackCacheItem>::const_iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
1304 const FallbackCacheItem& item = *it;
1306 if( !fontDescription.family.empty() &&
1307 ( fontDescription.family == item.fontDescription.family ) &&
1308 ( fontDescription.width == item.fontDescription.width ) &&
1309 ( fontDescription.weight == item.fontDescription.weight ) &&
1310 ( fontDescription.slant == item.fontDescription.slant ) )
1312 fontList = item.fallbackFonts;
1314 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList font family(%s) font-list (%p) \n", fontDescription.family.c_str(), fontList );
1320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList NOT FOUND return false\n" );
1325 bool FontClient::Plugin::FindFont( FontDescriptionId validatedFontId,
1326 PointSize26Dot6 pointSize,
1331 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
1332 endIt = mFontIdCache.end();
1336 const FontIdCacheItem& item = *it;
1338 if( ( validatedFontId == item.validatedFontId ) &&
1339 ( pointSize == item.pointSize ) )
1341 fontId = item.fontId;
1349 bool FontClient::Plugin::IsScalable( const FontPath& path )
1352 int error = FT_New_Face( mFreeTypeLibrary,
1356 if( FT_Err_Ok != error )
1358 DALI_LOG_ERROR( "FreeType Cannot check font: %s\n", path.c_str() );
1360 return ( ftFace->num_fixed_sizes == 0 );
1363 bool FontClient::Plugin::IsScalable( const FontDescription& fontDescription )
1365 // Create a font pattern.
1366 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1368 FcResult result = FcResultMatch;
1370 // match the pattern
1371 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1372 bool isScalable = true;
1376 // Get the path to the font file name.
1378 GetFcString( match, FC_FILE, path );
1379 isScalable = IsScalable( path );
1383 DALI_LOG_ERROR( "FreeType Cannot check font: %s %d %d %d\n",
1384 fontDescription.family.c_str(),
1385 fontDescription.width,
1386 fontDescription.weight,
1387 fontDescription.slant );
1389 FcPatternDestroy( fontFamilyPattern );
1390 FcPatternDestroy( match );
1394 void FontClient::Plugin::GetFixedSizes( const FontPath& path, Vector< PointSize26Dot6 >& sizes )
1396 // Empty the caller container
1400 int error = FT_New_Face( mFreeTypeLibrary,
1404 if( FT_Err_Ok != error )
1406 DALI_LOG_ERROR( "FreeType Cannot check font: %s\n", path.c_str() );
1409 // Fetch the number of fixed sizes available
1410 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1412 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1414 sizes.PushBack( ftFace->available_sizes[ i ].size );
1419 void FontClient::Plugin::GetFixedSizes( const FontDescription& fontDescription,
1420 Vector< PointSize26Dot6 >& sizes )
1422 // Create a font pattern.
1423 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1425 FcResult result = FcResultMatch;
1427 // match the pattern
1428 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1432 // Get the path to the font file name.
1434 GetFcString( match, FC_FILE, path );
1435 GetFixedSizes( path, sizes );
1439 DALI_LOG_ERROR( "FreeType Cannot check font: %s %d %d %d\n",
1440 fontDescription.family.c_str(),
1441 fontDescription.width,
1442 fontDescription.weight,
1443 fontDescription.slant );
1445 FcPatternDestroy( match );
1446 FcPatternDestroy( fontFamilyPattern );
1449 } // namespace Internal
1451 } // namespace TextAbstraction