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 ),
202 mValidatedFontCache(),
203 mFontDescriptionCache( 1u ),
207 int error = FT_Init_FreeType( &mFreeTypeLibrary );
208 if( FT_Err_Ok != error )
210 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
214 FontClient::Plugin::~Plugin()
216 for( std::vector<FallbackCacheItem>::iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
220 FallbackCacheItem& item = *it;
222 if( item.fallbackFonts )
224 delete item.fallbackFonts;
225 item.fallbackFonts = NULL;
229 FT_Done_FreeType( mFreeTypeLibrary );
232 void FontClient::Plugin::SetDpi( unsigned int horizontalDpi,
233 unsigned int verticalDpi )
235 mDpiHorizontal = horizontalDpi;
236 mDpiVertical = verticalDpi;
239 void FontClient::Plugin::SetFontList( const FontDescription& fontDescription, FontList& fontList )
241 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::SetFontList family(%s)\n", fontDescription.family.c_str() );
245 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
247 FcResult result = FcResultMatch;
249 // Match the pattern.
250 FcFontSet* fontSet = FcFontSort( NULL /* use default configure */,
252 false /* don't trim */,
256 if( NULL != fontSet )
258 // Reserve some space to avoid reallocations.
259 fontList.reserve( fontSet->nfont );
261 for( int i = 0u; i < fontSet->nfont; ++i )
263 FcPattern* fontPattern = fontSet->fonts[i];
267 // Skip fonts with no path
268 if( GetFcString( fontPattern, FC_FILE, path ) )
270 fontList.push_back( FontDescription() );
271 FontDescription& newFontDescription = fontList.back();
273 newFontDescription.path = path;
278 GetFcString( fontPattern, FC_FAMILY, newFontDescription.family );
279 GetFcInt( fontPattern, FC_WIDTH, width );
280 GetFcInt( fontPattern, FC_WEIGHT, weight );
281 GetFcInt( fontPattern, FC_SLANT, slant );
282 newFontDescription.width = IntToWidthType( width );
283 newFontDescription.weight = IntToWeightType( weight );
284 newFontDescription.slant = IntToSlantType( slant );
288 FcFontSetDestroy( fontSet );
291 FcPatternDestroy( fontFamilyPattern );
294 void FontClient::Plugin::SetDefaultFont( const FontDescription& fontDescription )
296 SetFontList( fontDescription, mDefaultFonts );
299 void FontClient::Plugin::GetDefaultFonts( FontList& defaultFonts )
301 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetDefaultFonts mDefaultFonts(%s)\n", ( mDefaultFonts.empty()?"empty":"valid" ) );
303 if( mDefaultFonts.empty() )
305 FontDescription fontDescription;
306 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
307 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
308 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
309 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
310 SetFontList( fontDescription, mDefaultFonts );
313 defaultFonts = mDefaultFonts;
316 void FontClient::Plugin::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
318 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetDefaultPlatformFontDescription\n");
320 FcInitReinitialize(); // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
322 FcPattern* matchPattern = FcPatternCreate();
323 FcConfigSubstitute(NULL, matchPattern, FcMatchPattern);
324 FcDefaultSubstitute( matchPattern );
326 MatchFontDescriptionToPattern( matchPattern, fontDescription );
327 FcPatternDestroy( matchPattern );
330 void FontClient::Plugin::GetSystemFonts( FontList& systemFonts )
332 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetSystemFonts\n");
334 if( mSystemFonts.empty() )
339 systemFonts = mSystemFonts;
342 void FontClient::Plugin::GetDescription( FontId id,
343 FontDescription& fontDescription ) const
345 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
346 endIt = mFontIdCache.end();
350 const FontIdCacheItem& item = *it;
352 if( item.fontId == id )
354 fontDescription = *( mFontDescriptionCache.begin() + item.validatedFontId );
359 DALI_LOG_ERROR( "FontClient::Plugin::GetDescription. No description found for the font ID %d\n", id );
362 PointSize26Dot6 FontClient::Plugin::GetPointSize( FontId id )
364 const FontId index = id - 1u;
367 index < mFontCache.size() )
369 return ( *( mFontCache.begin() + index ) ).mPointSize;
373 DALI_LOG_ERROR( "FontClient::Plugin::GetPointSize. Invalid font ID %d\n", id );
376 return TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
379 FontId FontClient::Plugin::FindFontForCharacter( const FontList& fontList,
381 PointSize26Dot6 requestedSize,
384 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFontForCharacter\n");
387 bool foundColor(false);
389 // Traverse the list of fonts.
390 // Check for each default font if supports the character.
392 for( FontList::const_iterator it = fontList.begin(), endIt = fontList.end();
396 const FontDescription& description = *it;
398 FcPattern* pattern = CreateFontFamilyPattern( description );
400 FcResult result = FcResultMatch;
401 FcPattern* match = FcFontMatch( NULL /* use default configure */, pattern, &result );
403 FcCharSet* charSet = NULL;
404 FcPatternGetCharSet( match, FC_CHARSET, 0u, &charSet );
406 if( FcCharSetHasChar( charSet, charcode ) )
408 Vector< PointSize26Dot6 > fixedSizes;
409 GetFixedSizes( description,
412 const Vector< PointSize26Dot6 >::SizeType count = fixedSizes.Count();
415 // If the font is not scalable, pick the largest size <= requestedSize
416 PointSize26Dot6 size = fixedSizes[0];
417 for( unsigned int i=1; i<count; ++i )
419 if( fixedSizes[i] <= requestedSize &&
420 fixedSizes[i] > size )
422 size = fixedSizes[i];
425 requestedSize = size;
428 fontId = GetFontId( description,
434 BufferImage bitmap = CreateBitmap( fontId, GetGlyphIndex(fontId,charcode) );
436 Pixel::BGRA8888 == bitmap.GetPixelFormat() )
442 // Keep going unless we prefer a different (color) font
443 if( !preferColor || foundColor )
445 FcPatternDestroy( match );
446 FcPatternDestroy( pattern );
451 FcPatternDestroy( match );
452 FcPatternDestroy( pattern );
458 FontId FontClient::Plugin::FindDefaultFont( Character charcode,
459 PointSize26Dot6 requestedSize,
462 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindDefaultFont DefaultFontsList(%s)\n", (mDefaultFonts.empty()?"empty":"created") );
466 // Create the list of default fonts if it has not been created.
467 if( mDefaultFonts.empty() )
469 FontDescription fontDescription;
470 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
471 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
472 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
473 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
474 SetFontList( fontDescription, mDefaultFonts );
477 // Traverse the list of default fonts.
478 // Check for each default font if supports the character.
479 fontId = FindFontForCharacter( mDefaultFonts, charcode, requestedSize, preferColor );
484 FontId FontClient::Plugin::FindFallbackFont( FontId preferredFont,
486 PointSize26Dot6 requestedSize,
489 // The font id to be returned.
492 FontDescription fontDescription;
493 GetDescription( preferredFont, fontDescription );
495 // Check first if the font's description has been queried before.
496 FontList* fontList( NULL );
498 if( !FindFallbackFontList( fontDescription, fontList ) )
500 fontList = new FontList;
501 SetFontList( fontDescription, *fontList );
503 // Add the font-list to the cache.
504 mFallbackCache.push_back( FallbackCacheItem(fontDescription, fontList) );
509 fontId = FindFontForCharacter( *fontList, charcode, requestedSize, preferColor );
515 FontId FontClient::Plugin::GetFontId( const FontPath& path,
516 PointSize26Dot6 pointSize,
518 bool cacheDescription )
520 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId fontPatch:%s\n", path.c_str() );
524 if( NULL != mFreeTypeLibrary )
527 if( FindFont( path, pointSize, faceIndex, foundId ) )
533 id = CreateFont( path, pointSize, faceIndex, cacheDescription );
540 FontId FontClient::Plugin::GetFontId( const FontDescription& fontDescription,
541 PointSize26Dot6 pointSize,
542 FaceIndex faceIndex )
544 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId font family(%s)\n", fontDescription.family.c_str() );
546 // This method uses three vectors which caches:
547 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
548 // * The path to font file names.
549 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
551 // 1) Checks in the cache if the font's description has been validated before.
552 // If it was it gets an index to the vector with paths to font file names. Otherwise,
553 // retrieves using font config a path to a font file name which matches with the
554 // font's description. The path is stored in the cache.
556 // 2) Checks in the cache if the pair 'font point size, index to the vector with paths to
557 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
558 // the GetFontId() method with the path to the font file name and the point size to
561 // The font id to be returned.
564 // Check first if the font's description have been validated before.
565 FontDescriptionId validatedFontId = 0u;
567 if( !FindValidatedFont( fontDescription,
570 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::GetFontId Validating Font\n");
572 // Use font config to validate the font's description.
573 ValidateFont( fontDescription,
577 // Check if exists a pair 'validatedFontId, pointSize' in the cache.
578 if( !FindFont( validatedFontId, pointSize, fontId ) )
580 // Retrieve the font file name path.
581 const FontDescription& description = *( mFontDescriptionCache.begin() + validatedFontId );
583 // Retrieve the font id. Do not cache the description as it has been already cached.
584 fontId = GetFontId( description.path,
589 // Cache the pair 'validatedFontId, pointSize' to improve the following queries.
590 mFontIdCache.push_back( FontIdCacheItem( validatedFontId,
598 void FontClient::Plugin::ValidateFont( const FontDescription& fontDescription,
599 FontDescriptionId& validatedFontId )
601 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::ValidateFont Validating Font family(%s) \n", fontDescription.family.c_str() );
603 // Create a font pattern.
604 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
606 FontDescription description;
608 bool matched = MatchFontDescriptionToPattern( fontFamilyPattern, description );
609 FcPatternDestroy( fontFamilyPattern );
613 // Set the index to the vector of paths to font file names.
614 validatedFontId = mFontDescriptionCache.size();
616 // Add the path to the cache.
617 mFontDescriptionCache.push_back( description );
619 // Cache the index and the font's description.
620 FontDescriptionCacheItem item( description,
623 mValidatedFontCache.push_back( item );
627 DALI_LOG_ERROR( "FontClient::Plugin::ValidateFont failed for font %s %d %d %d\n",
628 fontDescription.family.c_str(),
629 fontDescription.width,
630 fontDescription.weight,
631 fontDescription.slant );
634 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::ValidateFont validatedFontId(%u) font family(%s)\n", validatedFontId, fontDescription.family.c_str() );
637 void FontClient::Plugin::GetFontMetrics( FontId fontId,
638 FontMetrics& metrics,
642 fontId-1 < mFontCache.size() )
644 const CacheItem& font = mFontCache[fontId-1];
646 metrics = font.mMetrics;
648 // Adjust the metrics if the fixed-size font should be down-scaled
649 if( font.mIsFixedSizeBitmap &&
650 ( maxFixedSize > 0 ) &&
651 ( font.mFixedHeightPixels > maxFixedSize ) )
653 float scaleFactor = static_cast<float>(maxFixedSize) / static_cast<float>(font.mFixedHeightPixels);
655 metrics.ascender *= scaleFactor;
656 metrics.descender *= scaleFactor;
657 metrics.height *= scaleFactor;
658 metrics.underlinePosition *= scaleFactor;
659 metrics.underlineThickness *= scaleFactor;
664 DALI_LOG_ERROR( "Invalid font ID %d\n", fontId );
668 GlyphIndex FontClient::Plugin::GetGlyphIndex( FontId fontId,
671 GlyphIndex index( 0 );
674 fontId-1 < mFontCache.size() )
676 FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace;
678 index = FT_Get_Char_Index( ftFace, charcode );
684 bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
689 bool success( true );
691 for( unsigned int i=0; i<size; ++i )
693 FontId fontId = array[i].fontId;
696 fontId-1 < mFontCache.size() )
698 const CacheItem& font = mFontCache[fontId-1];
700 FT_Face ftFace = font.mFreeTypeFace;
702 #ifdef FREETYPE_BITMAP_SUPPORT
703 // Check to see if we should be loading a Fixed Size bitmap?
704 if ( font.mIsFixedSizeBitmap )
706 int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_COLOR );
707 if ( FT_Err_Ok == error )
709 array[i].width = font.mFixedWidthPixels;
710 array[i].height = font.mFixedHeightPixels;
711 array[i].advance = font.mFixedWidthPixels;
712 array[i].xBearing = 0.0f;
713 array[i].yBearing = font.mFixedHeightPixels;
715 // Adjust the metrics if the fixed-size font should be down-scaled
716 if( ( maxFixedSize > 0 ) &&
717 ( font.mFixedHeightPixels > maxFixedSize ) )
719 float scaleFactor = static_cast<float>(maxFixedSize) / static_cast<float>(font.mFixedHeightPixels);
721 array[i].width *= scaleFactor;
722 array[i].height *= scaleFactor;
723 array[i].advance *= scaleFactor;
724 array[i].xBearing *= scaleFactor;
725 array[i].yBearing *= scaleFactor;
727 array[i].scaleFactor = scaleFactor;
732 DALI_LOG_ERROR( "FreeType Bitmap Load_Glyph error %d\n", error );
739 int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_DEFAULT );
741 if( FT_Err_Ok == error )
743 array[i].width = static_cast< float >( ftFace->glyph->metrics.width ) * FROM_266;
744 array[i].height = static_cast< float >( ftFace->glyph->metrics.height ) * FROM_266 ;
747 array[i].xBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingX ) * FROM_266;
748 array[i].yBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingY ) * FROM_266;
752 array[i].xBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingX ) * FROM_266;
753 array[i].yBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingY ) * FROM_266;
771 BufferImage FontClient::Plugin::CreateBitmap( FontId fontId,
772 GlyphIndex glyphIndex )
777 fontId-1 < mFontCache.size() )
779 FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace;
783 #ifdef FREETYPE_BITMAP_SUPPORT
784 // Check to see if this is fixed size bitmap
785 if ( mFontCache[fontId-1].mIsFixedSizeBitmap )
787 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
792 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_DEFAULT );
794 if( FT_Err_Ok == error )
797 error = FT_Get_Glyph( ftFace->glyph, &glyph );
799 // Convert to bitmap if necessary
800 if ( FT_Err_Ok == error )
802 if( glyph->format != FT_GLYPH_FORMAT_BITMAP )
804 error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
805 if ( FT_Err_Ok == error )
807 FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph;
808 ConvertBitmap( bitmap, bitmapGlyph->bitmap );
812 DALI_LOG_ERROR( "FT_Get_Glyph Failed with error: %d\n", error );
817 ConvertBitmap( bitmap, ftFace->glyph->bitmap );
820 // Created FT_Glyph object must be released with FT_Done_Glyph
821 FT_Done_Glyph( glyph );
826 DALI_LOG_ERROR( "FT_Load_Glyph Failed with error: %d\n", error );
833 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph( PointSize26Dot6 pointSize )
835 // First look into the cache if there is an ellipsis glyph for the requested point size.
836 for( Vector<EllipsisItem>::ConstIterator it = mEllipsisCache.Begin(),
837 endIt = mEllipsisCache.End();
841 const EllipsisItem& item = *it;
843 if( fabsf( item.size - pointSize ) < Math::MACHINE_EPSILON_1000 )
845 // Use the glyph in the cache.
850 // No glyph has been found. Create one.
851 mEllipsisCache.PushBack( EllipsisItem() );
852 EllipsisItem& item = *( mEllipsisCache.End() - 1u );
854 item.size = pointSize;
856 // Find a font for the ellipsis glyph.
857 item.glyph.fontId = FindDefaultFont( ELLIPSIS_CHARACTER,
861 // Set the character index to access the glyph inside the font.
862 item.glyph.index = FT_Get_Char_Index( mFontCache[item.glyph.fontId-1].mFreeTypeFace,
863 ELLIPSIS_CHARACTER );
865 GetGlyphMetrics( &item.glyph, 1u, true, 0 );
870 void FontClient::Plugin::InitSystemFonts()
872 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::InitSystemFonts \n");
874 FcFontSet* fontSet = GetFcFontSet();
878 // Reserve some space to avoid reallocations.
879 mSystemFonts.reserve( fontSet->nfont );
881 for( int i = 0u; i < fontSet->nfont; ++i )
883 FcPattern* fontPattern = fontSet->fonts[i];
887 // Skip fonts with no path
888 if( GetFcString( fontPattern, FC_FILE, path ) )
890 mSystemFonts.push_back( FontDescription() );
891 FontDescription& fontDescription = mSystemFonts.back();
893 fontDescription.path = path;
898 GetFcString( fontPattern, FC_FAMILY, fontDescription.family );
899 GetFcInt( fontPattern, FC_WIDTH, width );
900 GetFcInt( fontPattern, FC_WEIGHT, weight );
901 GetFcInt( fontPattern, FC_SLANT, slant );
902 fontDescription.width = IntToWidthType( width );
903 fontDescription.weight = IntToWeightType( weight );
904 fontDescription.slant = IntToSlantType( slant );
905 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::InitSystemFonts font family(%s)\n", fontDescription.family.c_str() );
910 FcFontSetDestroy( fontSet );
914 bool FontClient::Plugin::MatchFontDescriptionToPattern( FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription )
916 FcResult result = FcResultMatch;
917 FcPattern* match = FcFontMatch( NULL /* use default configure */, pattern, &result );
926 GetFcString( match, FC_FILE, fontDescription.path );
927 GetFcString( match, FC_FAMILY, fontDescription.family );
928 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::MatchFontDescriptionToPattern matched:%s \n", fontDescription.family.c_str());
929 GetFcInt( match, FC_WIDTH, width );
930 GetFcInt( match, FC_WEIGHT, weight );
931 GetFcInt( match, FC_SLANT, slant );
932 fontDescription.width = IntToWidthType( width );
933 fontDescription.weight = IntToWeightType( weight );
934 fontDescription.slant = IntToSlantType( slant );
935 // destroyed the matched pattern
936 FcPatternDestroy( match );
943 FcPattern* FontClient::Plugin::CreateFontFamilyPattern( const FontDescription& fontDescription )
945 // create the cached font family lookup pattern
946 // a pattern holds a set of names, each name refers to a property of the font
947 FcPattern* fontFamilyPattern = FcPatternCreate();
949 // add a property to the pattern for the font family
950 FcPatternAddString( fontFamilyPattern, FC_FAMILY, reinterpret_cast<const FcChar8*>( fontDescription.family.c_str() ) );
952 FcPatternAddInteger( fontFamilyPattern, FC_WIDTH, FONT_WIDTH_TYPE_TO_INT[fontDescription.width] );
953 FcPatternAddInteger( fontFamilyPattern, FC_WEIGHT, FONT_WEIGHT_TYPE_TO_INT[fontDescription.weight] );
954 FcPatternAddInteger( fontFamilyPattern, FC_SLANT, FONT_SLANT_TYPE_TO_INT[fontDescription.slant] );
956 // Add a property of the pattern, to say we want to match TrueType fonts
957 FcPatternAddString( fontFamilyPattern , FC_FONTFORMAT, reinterpret_cast<const FcChar8*>( FONT_FORMAT.c_str() ) );
959 // modify the config, with the mFontFamilyPatterm
960 FcConfigSubstitute( NULL /* use default configure */, fontFamilyPattern, FcMatchPattern );
962 // provide default values for unspecified properties in the font pattern
963 // e.g. patterns without a specified style or weight are set to Medium
964 FcDefaultSubstitute( fontFamilyPattern );
966 return fontFamilyPattern;
969 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
971 // create a new pattern.
972 // a pattern holds a set of names, each name refers to a property of the font
973 FcPattern* pattern = FcPatternCreate();
975 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
976 FcObjectSet* objectSet = FcObjectSetCreate();
978 // build an object set from a list of property names
979 FcObjectSetAdd( objectSet, FC_FILE );
980 FcObjectSetAdd( objectSet, FC_FAMILY );
981 FcObjectSetAdd( objectSet, FC_WIDTH );
982 FcObjectSetAdd( objectSet, FC_WEIGHT );
983 FcObjectSetAdd( objectSet, FC_SLANT );
985 // get a list of fonts
986 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
987 FcFontSet* fontset = FcFontList( NULL /* the default configuration is checked to be up to date, and used */, pattern, objectSet );
989 // clear up the object set
992 FcObjectSetDestroy( objectSet );
994 // clear up the pattern
997 FcPatternDestroy( pattern );
1003 bool FontClient::Plugin::GetFcString( const FcPattern* const pattern,
1004 const char* const n,
1005 std::string& string )
1007 FcChar8* file = NULL;
1008 const FcResult retVal = FcPatternGetString( pattern, n, 0u, &file );
1010 if( FcResultMatch == retVal )
1012 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1013 string.assign( reinterpret_cast<const char*>( file ) );
1021 bool FontClient::Plugin::GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal )
1023 const FcResult retVal = FcPatternGetInteger( pattern, n, 0u, &intVal );
1025 if( FcResultMatch == retVal )
1033 FontId FontClient::Plugin::CreateFont( const FontPath& path,
1034 PointSize26Dot6 pointSize,
1035 FaceIndex faceIndex,
1036 bool cacheDescription )
1040 // Create & cache new font face
1042 int error = FT_New_Face( mFreeTypeLibrary,
1047 if( FT_Err_Ok == error )
1049 // Check to see if the font contains fixed sizes?
1050 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1052 // Ensure this size is available
1053 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1055 if ( static_cast<FT_Pos>(pointSize) == ftFace->available_sizes[ i ].size )
1057 // Tell Freetype to use this size
1058 error = FT_Select_Size( ftFace, i );
1059 if ( FT_Err_Ok != error )
1061 DALI_LOG_ERROR( "FreeType Select_Size error: %d\n", error );
1065 float fixedWidth = static_cast< float >( ftFace->available_sizes[ i ].width );
1066 float fixedHeight = static_cast< float >( ftFace->available_sizes[ i ].height );
1068 // Indicate that the font is a fixed sized bitmap
1069 FontMetrics metrics( fixedHeight, // The ascender in pixels.
1071 fixedHeight, // The height in pixels.
1075 mFontCache.push_back( CacheItem( ftFace, path, pointSize, faceIndex, metrics, fixedWidth, fixedHeight ) );
1076 id = mFontCache.size();
1078 if( cacheDescription )
1080 FontDescription description;
1081 description.path = path;
1082 description.family = FontFamily( ftFace->family_name );
1084 // Note FreeType doesn't give too much info to build a proper font style.
1085 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1087 description.slant = FontSlant::ITALIC;
1089 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1091 description.weight = FontWeight::BOLD;
1094 mFontDescriptionCache.push_back( description );
1101 // Can't find this size
1102 std::stringstream sizes;
1103 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1109 sizes << ftFace->available_sizes[ i ].size;
1111 DALI_LOG_ERROR( "FreeType Font: %s, does not contain Bitmaps of size: %d. Available sizes are: %s\n",
1112 path.c_str(), pointSize, sizes.str().c_str() );
1116 error = FT_Set_Char_Size( ftFace,
1122 if( FT_Err_Ok == error )
1125 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1127 FontMetrics metrics( static_cast< float >( ftMetrics.ascender ) * FROM_266,
1128 static_cast< float >( ftMetrics.descender ) * FROM_266,
1129 static_cast< float >( ftMetrics.height ) * FROM_266,
1130 static_cast< float >( ftFace->underline_position ) * FROM_266,
1131 static_cast< float >( ftFace->underline_thickness ) * FROM_266 );
1133 mFontCache.push_back( CacheItem( ftFace, path, pointSize, faceIndex, metrics ) );
1134 id = mFontCache.size();
1136 if( cacheDescription )
1138 FontDescription description;
1139 description.path = path;
1140 description.family = FontFamily( ftFace->family_name );
1142 // Note FreeType doesn't give too much info to build a proper font style.
1143 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1145 description.slant = FontSlant::ITALIC;
1147 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1149 description.weight = FontWeight::BOLD;
1152 mFontDescriptionCache.push_back( description );
1157 DALI_LOG_ERROR( "FreeType Set_Char_Size error: %d for pointSize %d\n", error, pointSize );
1163 DALI_LOG_ERROR( "FreeType New_Face error: %d for %s\n", error, path.c_str() );
1169 void FontClient::Plugin::ConvertBitmap( BufferImage& destBitmap,
1170 FT_Bitmap srcBitmap )
1172 if( srcBitmap.width*srcBitmap.rows > 0 )
1174 switch( srcBitmap.pixel_mode )
1176 case FT_PIXEL_MODE_GRAY:
1178 if( srcBitmap.pitch == static_cast< int >( srcBitmap.width ) )
1180 destBitmap = BufferImage::New( srcBitmap.width, srcBitmap.rows, Pixel::L8 );
1182 PixelBuffer* destBuffer = destBitmap.GetBuffer();
1185 memcpy( destBuffer, srcBitmap.buffer, srcBitmap.width*srcBitmap.rows );
1189 DALI_LOG_ERROR( "GetBuffer returns null\n" );
1195 #ifdef FREETYPE_BITMAP_SUPPORT
1196 case FT_PIXEL_MODE_BGRA:
1198 if ( srcBitmap.pitch == static_cast< int >( srcBitmap.width << 2 ) )
1200 destBitmap = BufferImage::New( srcBitmap.width, srcBitmap.rows, Pixel::BGRA8888 );
1202 PixelBuffer* destBuffer = destBitmap.GetBuffer();
1205 memcpy( destBuffer, srcBitmap.buffer, srcBitmap.width*srcBitmap.rows*4 );
1209 DALI_LOG_ERROR( "GetBuffer returns null\n" );
1217 DALI_LOG_ERROR( "FontClient Unable to create Bitmap of this PixelType\n" );
1224 bool FontClient::Plugin::FindFont( const FontPath& path,
1225 PointSize26Dot6 pointSize,
1226 FaceIndex faceIndex,
1227 FontId& fontId ) const
1230 for( std::vector<CacheItem>::const_iterator it = mFontCache.begin(),
1231 endIt = mFontCache.end();
1235 const CacheItem& cacheItem = *it;
1237 if( cacheItem.mPointSize == pointSize &&
1238 cacheItem.mFaceIndex == faceIndex &&
1239 cacheItem.mPath == path )
1249 bool FontClient::Plugin::FindValidatedFont( const FontDescription& fontDescription,
1250 FontDescriptionId& validatedFontId )
1252 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont fontDescription family(%s)\n", fontDescription.family.c_str() );
1254 validatedFontId = 0u;
1256 for( std::vector<FontDescriptionCacheItem>::const_iterator it = mValidatedFontCache.begin(),
1257 endIt = mValidatedFontCache.end();
1261 const FontDescriptionCacheItem& item = *it;
1263 if( !fontDescription.family.empty() &&
1264 ( fontDescription.family == item.fontDescription.family ) &&
1265 ( fontDescription.width == item.fontDescription.width ) &&
1266 ( fontDescription.weight == item.fontDescription.weight ) &&
1267 ( fontDescription.slant == item.fontDescription.slant ) )
1269 validatedFontId = item.index;
1271 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont validated font family(%s) font id (%u) \n", fontDescription.family.c_str(), validatedFontId );
1277 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindValidatedFont NOT VALIDATED return false\n" );
1282 bool FontClient::Plugin::FindFallbackFontList( const FontDescription& fontDescription,
1283 FontList*& fontList )
1285 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList fontDescription family(%s)\n", fontDescription.family.c_str() );
1289 for( std::vector<FallbackCacheItem>::const_iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
1293 const FallbackCacheItem& item = *it;
1295 if( !fontDescription.family.empty() &&
1296 ( fontDescription.family == item.fontDescription.family ) &&
1297 ( fontDescription.width == item.fontDescription.width ) &&
1298 ( fontDescription.weight == item.fontDescription.weight ) &&
1299 ( fontDescription.slant == item.fontDescription.slant ) )
1301 fontList = item.fallbackFonts;
1303 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList font family(%s) font-list (%p) \n", fontDescription.family.c_str(), fontList );
1309 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "FontClient::Plugin::FindFallbackFontList NOT FOUND return false\n" );
1314 bool FontClient::Plugin::FindFont( FontDescriptionId validatedFontId,
1315 PointSize26Dot6 pointSize,
1320 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
1321 endIt = mFontIdCache.end();
1325 const FontIdCacheItem& item = *it;
1327 if( ( validatedFontId == item.validatedFontId ) &&
1328 ( pointSize == item.pointSize ) )
1330 fontId = item.fontId;
1338 bool FontClient::Plugin::IsScalable( const FontPath& path )
1341 int error = FT_New_Face( mFreeTypeLibrary,
1345 if( FT_Err_Ok != error )
1347 DALI_LOG_ERROR( "FreeType Cannot check font: %s\n", path.c_str() );
1349 return ( ftFace->num_fixed_sizes == 0 );
1352 bool FontClient::Plugin::IsScalable( const FontDescription& fontDescription )
1354 // Create a font pattern.
1355 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1357 FcResult result = FcResultMatch;
1359 // match the pattern
1360 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1361 bool isScalable = true;
1365 // Get the path to the font file name.
1367 GetFcString( match, FC_FILE, path );
1368 isScalable = IsScalable( path );
1372 DALI_LOG_ERROR( "FreeType Cannot check font: %s %d %d %d\n",
1373 fontDescription.family.c_str(),
1374 fontDescription.width,
1375 fontDescription.weight,
1376 fontDescription.slant );
1378 FcPatternDestroy( fontFamilyPattern );
1379 FcPatternDestroy( match );
1383 void FontClient::Plugin::GetFixedSizes( const FontPath& path, Vector< PointSize26Dot6 >& sizes )
1385 // Empty the caller container
1389 int error = FT_New_Face( mFreeTypeLibrary,
1393 if( FT_Err_Ok != error )
1395 DALI_LOG_ERROR( "FreeType Cannot check font: %s\n", path.c_str() );
1398 // Fetch the number of fixed sizes available
1399 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1401 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1403 sizes.PushBack( ftFace->available_sizes[ i ].size );
1408 void FontClient::Plugin::GetFixedSizes( const FontDescription& fontDescription,
1409 Vector< PointSize26Dot6 >& sizes )
1411 // Create a font pattern.
1412 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1414 FcResult result = FcResultMatch;
1416 // match the pattern
1417 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1421 // Get the path to the font file name.
1423 GetFcString( match, FC_FILE, path );
1424 GetFixedSizes( path, sizes );
1428 DALI_LOG_ERROR( "FreeType Cannot check font: %s %d %d %d\n",
1429 fontDescription.family.c_str(),
1430 fontDescription.width,
1431 fontDescription.weight,
1432 fontDescription.slant );
1434 FcPatternDestroy( match );
1435 FcPatternDestroy( fontFamilyPattern );
1438 } // namespace Internal
1440 } // namespace TextAbstraction