2 * Copyright (c) 2017 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 <platform-abstractions/portable/image-operations.h>
29 #include <adaptor-impl.h>
32 #include <fontconfig/fontconfig.h>
37 #if defined(DEBUG_ENABLED)
38 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_FONT_CLIENT");
42 * Conversion from Fractional26.6 to float
44 const float FROM_266 = 1.0f / 64.0f;
45 const float POINTS_PER_INCH = 72.f;
47 const std::string FONT_FORMAT( "TrueType" );
48 const std::string DEFAULT_FONT_FAMILY_NAME( "Tizen" );
49 const int DEFAULT_FONT_WIDTH = 100; // normal
50 const int DEFAULT_FONT_WEIGHT = 80; // normal
51 const int DEFAULT_FONT_SLANT = 0; // normal
53 const uint32_t ELLIPSIS_CHARACTER = 0x2026;
55 // http://www.freedesktop.org/software/fontconfig/fontconfig-user.html
57 // NONE -1 --> DEFAULT_FONT_WIDTH (NORMAL) will be used.
67 const int FONT_WIDTH_TYPE_TO_INT[] = { -1, 50, 63, 75, 87, 100, 113, 125, 150, 200 };
68 const unsigned int NUM_FONT_WIDTH_TYPE = sizeof( FONT_WIDTH_TYPE_TO_INT ) / sizeof( int );
70 // NONE -1 --> DEFAULT_FONT_WEIGHT (NORMAL) will be used.
72 // ULTRA_LIGHT, EXTRA_LIGHT 40
74 // DEMI_LIGHT, SEMI_LIGHT 55
78 // DEMI_BOLD, SEMI_BOLD 180
80 // ULTRA_BOLD, EXTRA_BOLD 205
81 // BLACK, HEAVY, EXTRA_BLACK 210
82 const int FONT_WEIGHT_TYPE_TO_INT[] = { -1, 0, 40, 50, 55, 75, 80, 100, 180, 200, 205, 210 };
83 const unsigned int NUM_FONT_WEIGHT_TYPE = sizeof( FONT_WEIGHT_TYPE_TO_INT ) / sizeof( int );
85 // NONE -1 --> DEFAULT_FONT_SLANT (NORMAL) will be used.
89 const int FONT_SLANT_TYPE_TO_INT[] = { -1, 0, 100, 110 };
90 const unsigned int NUM_FONT_SLANT_TYPE = sizeof( FONT_SLANT_TYPE_TO_INT ) / sizeof( int );
99 namespace TextAbstraction
106 * @brief Returns the FontWidth's enum index for the given width value.
108 * @param[in] width The width value.
110 * @return The FontWidth's enum index.
112 FontWidth::Type IntToWidthType( int width )
114 return static_cast<FontWidth::Type>( ValueToIndex( width, FONT_WIDTH_TYPE_TO_INT, NUM_FONT_WIDTH_TYPE - 1u ) );
118 * @brief Returns the FontWeight's enum index for the given weight value.
120 * @param[in] weight The weight value.
122 * @return The FontWeight's enum index.
124 FontWeight::Type IntToWeightType( int weight )
126 return static_cast<FontWeight::Type>( ValueToIndex( weight, FONT_WEIGHT_TYPE_TO_INT, NUM_FONT_WEIGHT_TYPE - 1u ) );
130 * @brief Returns the FontSlant's enum index for the given slant value.
132 * @param[in] slant The slant value.
134 * @return The FontSlant's enum index.
136 FontSlant::Type IntToSlantType( int slant )
138 return static_cast<FontSlant::Type>( ValueToIndex( slant, FONT_SLANT_TYPE_TO_INT, NUM_FONT_SLANT_TYPE - 1u ) );
141 FontClient::Plugin::FallbackCacheItem::FallbackCacheItem( const FontDescription& font, FontList* fallbackFonts, CharacterSetList* characterSets )
142 : fontDescription( font ),
143 fallbackFonts( fallbackFonts ),
144 characterSets( characterSets )
148 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem( const FontDescription& fontDescription,
149 FontDescriptionId index )
150 : fontDescription( fontDescription ),
155 FontClient::Plugin::FontIdCacheItem::FontIdCacheItem( FontDescriptionId validatedFontId,
156 PointSize26Dot6 requestedPointSize,
158 : validatedFontId( validatedFontId ),
159 requestedPointSize( requestedPointSize ),
164 FontClient::Plugin::FontFaceCacheItem::FontFaceCacheItem( FT_Face ftFace,
165 const FontPath& path,
166 PointSize26Dot6 requestedPointSize,
168 const FontMetrics& metrics )
169 : mFreeTypeFace( ftFace ),
171 mRequestedPointSize( requestedPointSize ),
174 mCharacterSet( NULL ),
175 mFixedWidthPixels( 0.0f ),
176 mFixedHeightPixels( 0.0f ),
178 mIsFixedSizeBitmap( false ),
179 mHasColorTables( false )
183 FontClient::Plugin::FontFaceCacheItem::FontFaceCacheItem( FT_Face ftFace,
184 const FontPath& path,
185 PointSize26Dot6 requestedPointSize,
187 const FontMetrics& metrics,
190 bool hasColorTables )
191 : mFreeTypeFace( ftFace ),
193 mRequestedPointSize( requestedPointSize ),
196 mCharacterSet( NULL ),
197 mFixedWidthPixels( fixedWidth ),
198 mFixedHeightPixels( fixedHeight ),
200 mIsFixedSizeBitmap( true ),
201 mHasColorTables( hasColorTables )
205 FontClient::Plugin::Plugin( unsigned int horizontalDpi,
206 unsigned int verticalDpi )
207 : mFreeTypeLibrary( NULL ),
208 mDpiHorizontal( horizontalDpi ),
209 mDpiVertical( verticalDpi ),
210 mDefaultFontDescription(),
214 mValidatedFontCache(),
215 mFontDescriptionCache( 1u ),
216 mCharacterSetCache(),
218 mVectorFontCache( NULL ),
220 mDefaultFontDescriptionCached( false )
222 mCharacterSetCache.Resize( 1u );
224 int error = FT_Init_FreeType( &mFreeTypeLibrary );
225 if( FT_Err_Ok != error )
227 DALI_LOG_INFO( gLogFilter, Debug::General, "FreeType Init error: %d\n", error );
230 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
231 mVectorFontCache = new VectorFontCache( mFreeTypeLibrary );
235 FontClient::Plugin::~Plugin()
237 for( std::vector<FallbackCacheItem>::iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
241 FallbackCacheItem& item = *it;
243 if( item.fallbackFonts )
245 delete item.fallbackFonts;
246 delete item.characterSets;
247 item.fallbackFonts = NULL;
248 item.characterSets = NULL;
252 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
253 delete mVectorFontCache;
256 FT_Done_FreeType( mFreeTypeLibrary );
259 void FontClient::Plugin::SetDpi( unsigned int horizontalDpi,
260 unsigned int verticalDpi )
262 mDpiHorizontal = horizontalDpi;
263 mDpiVertical = verticalDpi;
266 void FontClient::Plugin::ResetSystemDefaults()
268 mDefaultFontDescriptionCached = false;
271 void FontClient::Plugin::SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList )
273 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::SetFontList\n" );
274 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
275 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
276 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
277 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
281 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
283 FcResult result = FcResultMatch;
285 // Match the pattern.
286 FcFontSet* fontSet = FcFontSort( NULL /* use default configure */,
288 false /* don't trim */,
292 if( NULL != fontSet )
294 DALI_LOG_INFO( gLogFilter, Debug::General, " number of fonts found : [%d]\n", fontSet->nfont );
295 // Reserve some space to avoid reallocations.
296 fontList.reserve( fontSet->nfont );
298 for( int i = 0u; i < fontSet->nfont; ++i )
300 FcPattern* fontPattern = fontSet->fonts[i];
304 // Skip fonts with no path
305 if( GetFcString( fontPattern, FC_FILE, path ) )
307 FcCharSet* characterSet = NULL;
308 FcPatternGetCharSet( fontPattern, FC_CHARSET, 0u, &characterSet );
310 characterSetList.PushBack( characterSet );
311 fontList.push_back( FontDescription() );
312 FontDescription& newFontDescription = fontList.back();
314 newFontDescription.path = path;
319 GetFcString( fontPattern, FC_FAMILY, newFontDescription.family );
320 GetFcInt( fontPattern, FC_WIDTH, width );
321 GetFcInt( fontPattern, FC_WEIGHT, weight );
322 GetFcInt( fontPattern, FC_SLANT, slant );
323 newFontDescription.width = IntToWidthType( width );
324 newFontDescription.weight = IntToWeightType( weight );
325 newFontDescription.slant = IntToSlantType( slant );
327 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", newFontDescription.family.c_str() );
328 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", newFontDescription.path.c_str() );
329 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[newFontDescription.width] );
330 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[newFontDescription.weight] );
331 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[newFontDescription.slant] );
335 FcFontSetDestroy( fontSet );
339 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " No fonts found.\n" );
342 FcPatternDestroy( fontFamilyPattern );
343 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::SetFontList\n" );
346 void FontClient::Plugin::GetDefaultFonts( FontList& defaultFonts )
348 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDefaultFonts\n" );
350 if( mDefaultFonts.empty() )
352 FontDescription fontDescription;
353 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
354 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
355 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
356 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
357 SetFontList( fontDescription, mDefaultFonts, mDefaultFontCharacterSets );
360 defaultFonts = mDefaultFonts;
362 DALI_LOG_INFO( gLogFilter, Debug::General, " number of default fonts : [%d]\n", mDefaultFonts.size() );
363 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDefaultFonts\n" );
366 void FontClient::Plugin::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
368 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDefaultPlatformFontDescription\n");
370 if( !mDefaultFontDescriptionCached )
372 FcInitReinitialize(); // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
374 FcPattern* matchPattern = FcPatternCreate();
378 FcConfigSubstitute( NULL, matchPattern, FcMatchPattern );
379 FcDefaultSubstitute( matchPattern );
381 FcCharSet* characterSet = NULL;
382 MatchFontDescriptionToPattern( matchPattern, mDefaultFontDescription, &characterSet );
383 FcPatternDestroy( matchPattern );
386 mDefaultFontDescriptionCached = true;
389 fontDescription.path = mDefaultFontDescription.path;
390 fontDescription.family = mDefaultFontDescription.family;
391 fontDescription.width = mDefaultFontDescription.width;
392 fontDescription.weight = mDefaultFontDescription.weight;
393 fontDescription.slant = mDefaultFontDescription.slant;
395 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
396 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
397 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
398 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
399 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
400 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDefaultPlatformFontDescription\n");
403 void FontClient::Plugin::GetSystemFonts( FontList& systemFonts )
405 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetSystemFonts\n");
407 if( mSystemFonts.empty() )
412 systemFonts = mSystemFonts;
413 DALI_LOG_INFO( gLogFilter, Debug::General, " number of system fonts : [%d]\n", mSystemFonts.size() );
414 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetSystemFonts\n");
417 void FontClient::Plugin::GetDescription( FontId id,
418 FontDescription& fontDescription ) const
420 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDescription\n");
421 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
423 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
424 endIt = mFontIdCache.end();
428 const FontIdCacheItem& item = *it;
430 if( item.fontId == id )
432 fontDescription = *( mFontDescriptionCache.begin() + item.validatedFontId );
434 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
435 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
436 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
437 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
438 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
439 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDescription\n");
444 DALI_LOG_INFO( gLogFilter, Debug::General, " No description found for the font ID %d\n", id );
445 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDescription\n");
448 PointSize26Dot6 FontClient::Plugin::GetPointSize( FontId id )
450 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetPointSize\n");
451 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
452 const FontId index = id - 1u;
455 ( index < mFontCache.size() ) )
457 DALI_LOG_INFO( gLogFilter, Debug::General, " point size : %d\n", ( *( mFontCache.begin() + index ) ).mRequestedPointSize );
458 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetPointSize\n");
459 return ( *( mFontCache.begin() + index ) ).mRequestedPointSize;
463 DALI_LOG_INFO( gLogFilter, Debug::General, " Invalid font ID %d\n", id );
466 DALI_LOG_INFO( gLogFilter, Debug::General, " default point size : %d\n", TextAbstraction::FontClient::DEFAULT_POINT_SIZE );
467 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetPointSize\n");
468 return TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
471 bool FontClient::Plugin::IsCharacterSupportedByFont( FontId fontId, Character character )
473 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::IsCharacterSupportedByFont\n");
474 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
475 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", character );
477 if( ( fontId < 1u ) || ( fontId > mFontCache.size() ) )
479 DALI_LOG_INFO( gLogFilter, Debug::General, " Invalid font id. Number of items in the cache: %d\n",mFontCache.size());
480 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::IsCharacterSupportedByFont\n");
486 bool isSupported = false;
488 const FontFaceCacheItem& cacheItem = mFontCache[fontId];
490 isSupported = FcCharSetHasChar( cacheItem.mCharacterSet, character );
492 DALI_LOG_INFO( gLogFilter, Debug::General, " is supported : %s\n", (isSupported ? "true" : "false") );
493 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::IsCharacterSupportedByFont\n");
497 FontId FontClient::Plugin::FindFontForCharacter( const FontList& fontList,
498 const CharacterSetList& characterSetList,
500 PointSize26Dot6 requestedPointSize,
503 DALI_ASSERT_DEBUG( ( fontList.size() == characterSetList.Count() ) && "FontClient::Plugin::FindFontForCharacter. Different number of fonts and character sets." );
505 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFontForCharacter\n" );
506 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", character );
507 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
508 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
511 bool foundColor = false;
513 DALI_LOG_INFO( gLogFilter, Debug::General, " number of fonts : %d\n", fontList.size() );
515 // Traverse the list of fonts.
516 // Check for each font if supports the character.
517 for( unsigned int index = 0u, numberOfFonts = fontList.size(); index < numberOfFonts; ++index )
519 const FontDescription& description = fontList[index];
520 const FcCharSet* const characterSet = characterSetList[index];
522 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", description.family.c_str() );
523 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", description.path.c_str() );
524 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[description.width] );
525 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[description.weight] );
526 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[description.slant] );
528 bool foundInRanges = false;
529 if( NULL != characterSet )
531 foundInRanges = FcCharSetHasChar( characterSet, character );
536 fontId = GetFontId( description,
540 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " font id : %d\n", fontId );
544 if( ( fontId > 0 ) &&
545 ( fontId - 1u < mFontCache.size() ) )
547 const FontFaceCacheItem& item = mFontCache[fontId - 1u];
549 foundColor = item.mHasColorTables;
552 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " foundColor : %s\n", ( foundColor ? "true" : "false" ) );
555 // Keep going unless we prefer a different (color) font.
556 if( !preferColor || foundColor )
563 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
564 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFontForCharacter\n" );
568 FontId FontClient::Plugin::FindDefaultFont( Character charcode,
569 PointSize26Dot6 requestedPointSize,
572 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindDefaultFont\n" );
573 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", charcode );
574 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
575 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
579 // Create the list of default fonts if it has not been created.
580 if( mDefaultFonts.empty() )
582 FontDescription fontDescription;
583 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
584 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
585 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
586 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
588 SetFontList( fontDescription, mDefaultFonts, mDefaultFontCharacterSets );
590 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of default fonts : %d\n", mDefaultFonts.size() );
593 // Traverse the list of default fonts.
594 // Check for each default font if supports the character.
595 fontId = FindFontForCharacter( mDefaultFonts, mDefaultFontCharacterSets, charcode, requestedPointSize, preferColor );
597 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
598 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindDefaultFont\n" );
602 FontId FontClient::Plugin::FindFallbackFont( Character charcode,
603 const FontDescription& preferredFontDescription,
604 PointSize26Dot6 requestedPointSize,
607 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFallbackFont\n" );
608 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", charcode );
609 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
610 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
612 // The font id to be returned.
615 FontDescription fontDescription;
617 // Fill the font description with the preferred font description and complete with the defaults.
618 fontDescription.family = preferredFontDescription.family.empty() ? DEFAULT_FONT_FAMILY_NAME : preferredFontDescription.family;
619 fontDescription.weight = ( ( FontWeight::NONE == preferredFontDescription.weight ) ? IntToWeightType( DEFAULT_FONT_WEIGHT ) : preferredFontDescription.weight );
620 fontDescription.width = ( ( FontWidth::NONE == preferredFontDescription.width ) ? IntToWidthType( DEFAULT_FONT_WIDTH ) : preferredFontDescription.width );
621 fontDescription.slant = ( ( FontSlant::NONE == preferredFontDescription.slant ) ? IntToSlantType( DEFAULT_FONT_SLANT ) : preferredFontDescription.slant );
623 DALI_LOG_INFO( gLogFilter, Debug::General, " preferredFontDescription --> fontDescription\n" );
624 DALI_LOG_INFO( gLogFilter, Debug::General, " [%s] --> [%s]\n", preferredFontDescription.family.c_str(), fontDescription.family.c_str() );
625 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWeight::Name[preferredFontDescription.weight], FontWeight::Name[fontDescription.weight] );
626 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWidth::Name[preferredFontDescription.width], FontWidth::Name[fontDescription.width] );
627 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontSlant::Name[preferredFontDescription.slant], FontSlant::Name[fontDescription.slant] );
629 // Check first if the font's description has been queried before.
630 FontList* fontList = NULL;
631 CharacterSetList* characterSetList = NULL;
633 if( !FindFallbackFontList( fontDescription, fontList, characterSetList ) )
635 fontList = new FontList;
636 characterSetList = new CharacterSetList;
638 SetFontList( fontDescription, *fontList, *characterSetList );
640 // Add the font-list to the cache.
641 mFallbackCache.push_back( FallbackCacheItem( fontDescription, fontList, characterSetList ) );
644 if( fontList && characterSetList )
646 fontId = FindFontForCharacter( *fontList, *characterSetList, charcode, requestedPointSize, preferColor );
649 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
650 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFont\n");
654 FontId FontClient::Plugin::GetFontId( const FontPath& path,
655 PointSize26Dot6 requestedPointSize,
657 bool cacheDescription )
659 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetFontId\n" );
660 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
661 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
665 if( NULL != mFreeTypeLibrary )
668 if( FindFont( path, requestedPointSize, faceIndex, foundId ) )
674 id = CreateFont( path, requestedPointSize, faceIndex, cacheDescription );
678 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
679 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetFontId\n" );
684 FontId FontClient::Plugin::GetFontId( const FontDescription& fontDescription,
685 PointSize26Dot6 requestedPointSize,
686 FaceIndex faceIndex )
688 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetFontId\n" );
689 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
690 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
691 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
692 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
693 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
694 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
696 // This method uses three vectors which caches:
697 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
698 // * The path to font file names.
699 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
701 // 1) Checks in the cache if the font's description has been validated before.
702 // If it was it gets an index to the vector with paths to font file names. Otherwise,
703 // retrieves using font config a path to a font file name which matches with the
704 // font's description. The path is stored in the cache.
706 // 2) Checks in the cache if the pair 'font point size, index to the vector with paths to
707 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
708 // the GetFontId() method with the path to the font file name and the point size to
711 // The font id to be returned.
714 // Check first if the font's description have been validated before.
715 FontDescriptionId validatedFontId = 0u;
717 if( !FindValidatedFont( fontDescription,
720 // Use font config to validate the font's description.
721 ValidateFont( fontDescription,
725 // Check if exists a pair 'validatedFontId, requestedPointSize' in the cache.
726 if( !FindFont( validatedFontId, requestedPointSize, fontId ) )
728 // Retrieve the font file name path.
729 const FontDescription& description = *( mFontDescriptionCache.begin() + validatedFontId );
731 // Retrieve the font id. Do not cache the description as it has been already cached.
732 fontId = GetFontId( description.path,
737 mFontCache[fontId-1u].mCharacterSet = mCharacterSetCache[validatedFontId];
739 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
740 mFontIdCache.push_back( FontIdCacheItem( validatedFontId,
745 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
746 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetFontId\n" );
751 void FontClient::Plugin::ValidateFont( const FontDescription& fontDescription,
752 FontDescriptionId& validatedFontId )
754 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::ValidateFont\n" );
755 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
756 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
757 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
758 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
759 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
761 // Create a font pattern.
762 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
764 FontDescription description;
766 FcCharSet* characterSet = NULL;
767 bool matched = MatchFontDescriptionToPattern( fontFamilyPattern, description, &characterSet );
768 FcPatternDestroy( fontFamilyPattern );
770 if( matched && ( NULL != characterSet ) )
772 // Set the index to the vector of paths to font file names.
773 validatedFontId = mFontDescriptionCache.size();
775 DALI_LOG_INFO( gLogFilter, Debug::General, " matched description; family : [%s]\n", description.family.c_str() );
776 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", description.path.c_str() );
777 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[description.width] );
778 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[description.weight] );
779 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[description.slant] );
780 DALI_LOG_INFO( gLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId );
782 // Add the path to the cache.
783 mFontDescriptionCache.push_back( description );
784 mCharacterSetCache.PushBack( characterSet );
786 // Cache the index and the matched font's description.
787 FontDescriptionCacheItem item( description,
790 mValidatedFontCache.push_back( item );
792 if( ( fontDescription.family != description.family ) ||
793 ( fontDescription.width != description.width ) ||
794 ( fontDescription.weight != description.weight ) ||
795 ( fontDescription.slant != description.slant ) )
797 // Cache the given font's description if it's different than the matched.
798 FontDescriptionCacheItem item( fontDescription,
801 mValidatedFontCache.push_back( item );
806 DALI_LOG_INFO( gLogFilter, Debug::General, " font validation failed for font [%s]\n", fontDescription.family.c_str() );
809 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::ValidateFont\n" );
812 void FontClient::Plugin::GetFontMetrics( FontId fontId,
813 FontMetrics& metrics )
815 if( ( fontId > 0 ) &&
816 ( fontId - 1u < mFontCache.size() ) )
818 const FontFaceCacheItem& font = mFontCache[fontId-1];
820 metrics = font.mMetrics;
822 // Adjust the metrics if the fixed-size font should be down-scaled
823 if( font.mIsFixedSizeBitmap )
825 const float desiredFixedSize = static_cast<float>( font.mRequestedPointSize ) * FROM_266 / POINTS_PER_INCH * mDpiVertical;
827 if( desiredFixedSize > 0.f )
829 const float scaleFactor = desiredFixedSize / static_cast<float>( font.mFixedHeightPixels );
831 metrics.ascender = floorf( metrics.ascender * scaleFactor );
832 metrics.descender = floorf( metrics.descender * scaleFactor );
833 metrics.height = floorf( metrics.height * scaleFactor );
834 metrics.underlinePosition = floorf( metrics.underlinePosition * scaleFactor );
835 metrics.underlineThickness = floorf( metrics.underlineThickness * scaleFactor );
841 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFontMetrics. Invalid font id : %d\n", fontId );
845 GlyphIndex FontClient::Plugin::GetGlyphIndex( FontId fontId,
848 GlyphIndex index = 0u;
850 if( ( fontId > 0u ) &&
851 ( fontId - 1u < mFontCache.size() ) )
853 FT_Face ftFace = mFontCache[fontId-1u].mFreeTypeFace;
855 index = FT_Get_Char_Index( ftFace, charcode );
861 bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
866 if( VECTOR_GLYPH == type )
868 return GetVectorMetrics( array, size, horizontal );
871 return GetBitmapMetrics( array, size, horizontal );
874 bool FontClient::Plugin::GetBitmapMetrics( GlyphInfo* array,
878 bool success( true );
880 for( unsigned int i=0; i<size; ++i )
882 GlyphInfo& glyph = array[i];
884 FontId fontId = glyph.fontId;
887 fontId-1 < mFontCache.size() )
889 const FontFaceCacheItem& font = mFontCache[fontId-1];
891 FT_Face ftFace = font.mFreeTypeFace;
893 #ifdef FREETYPE_BITMAP_SUPPORT
894 // Check to see if we should be loading a Fixed Size bitmap?
895 if ( font.mIsFixedSizeBitmap )
897 int error = FT_Load_Glyph( ftFace, glyph.index, FT_LOAD_COLOR );
898 if ( FT_Err_Ok == error )
900 glyph.width = font.mFixedWidthPixels;
901 glyph.height = font.mFixedHeightPixels;
902 glyph.advance = font.mFixedWidthPixels;
903 glyph.xBearing = 0.0f;
904 glyph.yBearing = font.mFixedHeightPixels;
906 // Adjust the metrics if the fixed-size font should be down-scaled
907 const float desiredFixedSize = static_cast<float>( font.mRequestedPointSize ) * FROM_266 / POINTS_PER_INCH * mDpiVertical;
909 if( desiredFixedSize > 0.f )
911 const float scaleFactor = desiredFixedSize / static_cast<float>( font.mFixedHeightPixels );
913 glyph.width = floorf( glyph.width * scaleFactor );
914 glyph.height = floorf( glyph.height * scaleFactor );
915 glyph.advance = floorf( glyph.advance * scaleFactor );
916 glyph.xBearing = floorf( glyph.xBearing * scaleFactor );
917 glyph.yBearing = floorf( glyph.yBearing * scaleFactor );
919 glyph.scaleFactor = scaleFactor;
924 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetBitmapMetrics. FreeType Bitmap Load_Glyph error %d\n", error );
931 int error = FT_Load_Glyph( ftFace, glyph.index, FT_LOAD_DEFAULT );
933 if( FT_Err_Ok == error )
935 glyph.width = static_cast< float >( ftFace->glyph->metrics.width ) * FROM_266;
936 glyph.height = static_cast< float >( ftFace->glyph->metrics.height ) * FROM_266 ;
939 glyph.xBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingX ) * FROM_266;
940 glyph.yBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingY ) * FROM_266;
944 glyph.xBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingX ) * FROM_266;
945 glyph.yBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingY ) * FROM_266;
963 bool FontClient::Plugin::GetVectorMetrics( GlyphInfo* array,
967 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
968 bool success( true );
970 for( unsigned int i=0; i<size; ++i )
972 FontId fontId = array[i].fontId;
975 fontId-1 < mFontCache.size() )
977 FontFaceCacheItem& font = mFontCache[fontId-1];
979 if( ! font.mVectorFontId )
981 font.mVectorFontId = mVectorFontCache->GetFontId( font.mPath );
984 mVectorFontCache->GetGlyphMetrics( font.mVectorFontId, array[i] );
986 // Vector metrics are in EMs, convert to pixels
987 const float scale = ( static_cast<float>( font.mRequestedPointSize ) * FROM_266 ) * static_cast<float>( mDpiVertical ) / POINTS_PER_INCH;
988 array[i].width *= scale;
989 array[i].height *= scale;
990 array[i].xBearing *= scale;
991 array[i].yBearing *= scale;
992 array[i].advance *= scale;
1006 void FontClient::Plugin::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data )
1008 if( ( fontId > 0 ) &&
1009 ( fontId - 1u < mFontCache.size() ) )
1011 FT_Face ftFace = mFontCache[fontId - 1u].mFreeTypeFace;
1015 #ifdef FREETYPE_BITMAP_SUPPORT
1016 // Check to see if this is fixed size bitmap
1017 if ( mFontCache[fontId - 1u].mIsFixedSizeBitmap )
1019 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
1024 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_DEFAULT );
1026 if( FT_Err_Ok == error )
1029 error = FT_Get_Glyph( ftFace->glyph, &glyph );
1031 // Convert to bitmap if necessary
1032 if ( FT_Err_Ok == error )
1034 if( glyph->format != FT_GLYPH_FORMAT_BITMAP )
1036 error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
1037 if ( FT_Err_Ok == error )
1039 FT_BitmapGlyph bitmapGlyph = reinterpret_cast< FT_BitmapGlyph >( glyph );
1040 ConvertBitmap( data, bitmapGlyph->bitmap );
1044 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::CreateBitmap. FT_Get_Glyph Failed with error: %d\n", error );
1049 ConvertBitmap( data, ftFace->glyph->bitmap );
1052 // Created FT_Glyph object must be released with FT_Done_Glyph
1053 FT_Done_Glyph( glyph );
1058 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::CreateBitmap. FT_Load_Glyph Failed with error: %d\n", error );
1063 PixelData FontClient::Plugin::CreateBitmap( FontId fontId,
1064 GlyphIndex glyphIndex )
1066 TextAbstraction::FontClient::GlyphBufferData data;
1068 CreateBitmap( fontId, glyphIndex, data );
1070 return PixelData::New( data.buffer,
1071 data.width * data.height * Pixel::GetBytesPerPixel( data.format ),
1075 PixelData::DELETE_ARRAY );
1078 void FontClient::Plugin::CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
1083 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1085 fontId-1 < mFontCache.size() )
1087 FontFaceCacheItem& font = mFontCache[fontId-1];
1089 if( ! font.mVectorFontId )
1091 font.mVectorFontId = mVectorFontCache->GetFontId( font.mPath );
1094 mVectorFontCache->GetVectorBlob( font.mVectorFontId, fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
1099 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph( PointSize26Dot6 requestedPointSize )
1101 // First look into the cache if there is an ellipsis glyph for the requested point size.
1102 for( Vector<EllipsisItem>::ConstIterator it = mEllipsisCache.Begin(),
1103 endIt = mEllipsisCache.End();
1107 const EllipsisItem& item = *it;
1109 if( fabsf( item.requestedPointSize - requestedPointSize ) < Math::MACHINE_EPSILON_1000 )
1111 // Use the glyph in the cache.
1116 // No glyph has been found. Create one.
1117 mEllipsisCache.PushBack( EllipsisItem() );
1118 EllipsisItem& item = *( mEllipsisCache.End() - 1u );
1120 item.requestedPointSize = requestedPointSize;
1122 // Find a font for the ellipsis glyph.
1123 item.glyph.fontId = FindDefaultFont( ELLIPSIS_CHARACTER,
1127 // Set the character index to access the glyph inside the font.
1128 item.glyph.index = FT_Get_Char_Index( mFontCache[item.glyph.fontId-1].mFreeTypeFace,
1129 ELLIPSIS_CHARACTER );
1131 GetBitmapMetrics( &item.glyph, 1u, true );
1136 bool FontClient::Plugin::IsColorGlyph( FontId fontId, GlyphIndex glyphIndex )
1138 FT_Error error = -1;
1140 #ifdef FREETYPE_BITMAP_SUPPORT
1141 if( ( fontId > 0 ) &&
1142 ( fontId - 1u < mFontCache.size() ) )
1144 const FontFaceCacheItem& item = mFontCache[fontId - 1u];
1145 FT_Face ftFace = item.mFreeTypeFace;
1147 // Check to see if this is fixed size bitmap
1148 if( item.mHasColorTables )
1150 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
1155 return FT_Err_Ok == error;
1158 void FontClient::Plugin::InitSystemFonts()
1160 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::InitSystemFonts\n" );
1162 FcFontSet* fontSet = GetFcFontSet();
1166 DALI_LOG_INFO( gLogFilter, Debug::General, " number of system fonts : %d\n", fontSet->nfont );
1168 // Reserve some space to avoid reallocations.
1169 mSystemFonts.reserve( fontSet->nfont );
1171 for( int i = 0u; i < fontSet->nfont; ++i )
1173 FcPattern* fontPattern = fontSet->fonts[i];
1177 // Skip fonts with no path
1178 if( GetFcString( fontPattern, FC_FILE, path ) )
1180 FcCharSet* characterSet = NULL;
1181 FcPatternGetCharSet( fontPattern, FC_CHARSET, 0u, &characterSet );
1183 mSystemFontCharacterSets.PushBack( characterSet );
1185 mSystemFonts.push_back( FontDescription() );
1186 FontDescription& fontDescription = mSystemFonts.back();
1188 fontDescription.path = path;
1193 GetFcString( fontPattern, FC_FAMILY, fontDescription.family );
1194 GetFcInt( fontPattern, FC_WIDTH, width );
1195 GetFcInt( fontPattern, FC_WEIGHT, weight );
1196 GetFcInt( fontPattern, FC_SLANT, slant );
1197 fontDescription.width = IntToWidthType( width );
1198 fontDescription.weight = IntToWeightType( weight );
1199 fontDescription.slant = IntToSlantType( slant );
1201 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", fontDescription.family.c_str() );
1202 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1203 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1204 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1205 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1209 FcFontSetDestroy( fontSet );
1211 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::InitSystemFonts\n" );
1214 bool FontClient::Plugin::MatchFontDescriptionToPattern( FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, FcCharSet** characterSet )
1216 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::MatchFontDescriptionToPattern\n" );
1218 FcResult result = FcResultMatch;
1219 FcPattern* match = FcFontMatch( NULL /* use default configure */, pattern, &result );
1221 const bool matched = NULL != match;
1222 DALI_LOG_INFO( gLogFilter, Debug::General, " pattern matched : %s\n", ( matched ? "true" : "false" ) );
1229 GetFcString( match, FC_FILE, fontDescription.path );
1230 GetFcString( match, FC_FAMILY, fontDescription.family );
1231 GetFcInt( match, FC_WIDTH, width );
1232 GetFcInt( match, FC_WEIGHT, weight );
1233 GetFcInt( match, FC_SLANT, slant );
1234 fontDescription.width = IntToWidthType( width );
1235 fontDescription.weight = IntToWeightType( weight );
1236 fontDescription.slant = IntToSlantType( slant );
1238 // Cache the character ranges.
1239 FcPatternGetCharSet( match, FC_CHARSET, 0u, characterSet );
1241 // destroyed the matched pattern
1242 FcPatternDestroy( match );
1244 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1245 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1246 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1247 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1248 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1251 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::MatchFontDescriptionToPattern\n" );
1255 FcPattern* FontClient::Plugin::CreateFontFamilyPattern( const FontDescription& fontDescription )
1257 // create the cached font family lookup pattern
1258 // a pattern holds a set of names, each name refers to a property of the font
1259 FcPattern* fontFamilyPattern = FcPatternCreate();
1261 if( !fontFamilyPattern )
1266 // add a property to the pattern for the font family
1267 FcPatternAddString( fontFamilyPattern, FC_FAMILY, reinterpret_cast<const FcChar8*>( fontDescription.family.c_str() ) );
1269 int width = FONT_WIDTH_TYPE_TO_INT[fontDescription.width];
1273 width = DEFAULT_FONT_WIDTH;
1276 int weight = FONT_WEIGHT_TYPE_TO_INT[fontDescription.weight];
1280 weight = DEFAULT_FONT_WEIGHT;
1283 int slant = FONT_SLANT_TYPE_TO_INT[fontDescription.slant];
1287 slant = DEFAULT_FONT_SLANT;
1290 FcPatternAddInteger( fontFamilyPattern, FC_WIDTH, width );
1291 FcPatternAddInteger( fontFamilyPattern, FC_WEIGHT, weight );
1292 FcPatternAddInteger( fontFamilyPattern, FC_SLANT, slant );
1294 // Add a property of the pattern, to say we want to match TrueType fonts
1295 FcPatternAddString( fontFamilyPattern , FC_FONTFORMAT, reinterpret_cast<const FcChar8*>( FONT_FORMAT.c_str() ) );
1297 // modify the config, with the mFontFamilyPatterm
1298 FcConfigSubstitute( NULL /* use default configure */, fontFamilyPattern, FcMatchPattern );
1300 // provide default values for unspecified properties in the font pattern
1301 // e.g. patterns without a specified style or weight are set to Medium
1302 FcDefaultSubstitute( fontFamilyPattern );
1304 return fontFamilyPattern;
1307 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
1309 // create a new pattern.
1310 // a pattern holds a set of names, each name refers to a property of the font
1311 FcPattern* pattern = FcPatternCreate();
1313 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
1314 FcObjectSet* objectSet = FcObjectSetCreate();
1316 // build an object set from a list of property names
1317 FcObjectSetAdd( objectSet, FC_FILE );
1318 FcObjectSetAdd( objectSet, FC_FAMILY );
1319 FcObjectSetAdd( objectSet, FC_WIDTH );
1320 FcObjectSetAdd( objectSet, FC_WEIGHT );
1321 FcObjectSetAdd( objectSet, FC_SLANT );
1323 // get a list of fonts
1324 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
1325 FcFontSet* fontset = FcFontList( NULL /* the default configuration is checked to be up to date, and used */, pattern, objectSet );
1327 // clear up the object set
1330 FcObjectSetDestroy( objectSet );
1332 // clear up the pattern
1335 FcPatternDestroy( pattern );
1341 bool FontClient::Plugin::GetFcString( const FcPattern* const pattern,
1342 const char* const n,
1343 std::string& string )
1345 FcChar8* file = NULL;
1346 const FcResult retVal = FcPatternGetString( pattern, n, 0u, &file );
1348 if( FcResultMatch == retVal )
1350 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1351 string.assign( reinterpret_cast<const char*>( file ) );
1359 bool FontClient::Plugin::GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal )
1361 const FcResult retVal = FcPatternGetInteger( pattern, n, 0u, &intVal );
1363 if( FcResultMatch == retVal )
1371 FontId FontClient::Plugin::CreateFont( const FontPath& path,
1372 PointSize26Dot6 requestedPointSize,
1373 FaceIndex faceIndex,
1374 bool cacheDescription )
1376 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::CreateFont\n" );
1377 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
1378 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1382 // Create & cache new font face
1384 int error = FT_New_Face( mFreeTypeLibrary,
1389 if( FT_Err_Ok == error )
1391 // Check if a font is scalable.
1392 const bool isScalable = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_SCALABLE ) );
1393 const bool hasFixedSizedBitmaps = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_FIXED_SIZES ) ) && ( 0 != ftFace->num_fixed_sizes );
1394 const bool hasColorTables = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_COLOR ) );
1396 DALI_LOG_INFO( gLogFilter, Debug::General, " isScalable : [%s]\n", ( isScalable ? "true" : "false" ) );
1397 DALI_LOG_INFO( gLogFilter, Debug::General, " hasFixedSizedBitmaps : [%s]\n", ( hasFixedSizedBitmaps ? "true" : "false" ) );
1398 DALI_LOG_INFO( gLogFilter, Debug::General, " hasColorTables : [%s]\n", ( hasColorTables ? "true" : "false" ) );
1400 // Check to see if the font contains fixed sizes?
1401 if( !isScalable && hasFixedSizedBitmaps )
1403 PointSize26Dot6 actualPointSize = 0u;
1404 int fixedSizeIndex = 0;
1405 for( ; fixedSizeIndex < ftFace->num_fixed_sizes; ++fixedSizeIndex )
1407 const PointSize26Dot6 fixedSize = ftFace->available_sizes[fixedSizeIndex].size;
1408 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " size index : %d, size : %d\n", fixedSizeIndex, fixedSize );
1410 if( fixedSize >= requestedPointSize )
1412 actualPointSize = fixedSize;
1417 if( 0u == actualPointSize )
1419 // The requested point size is bigger than the bigest fixed size.
1420 fixedSizeIndex = ftFace->num_fixed_sizes - 1;
1421 actualPointSize = ftFace->available_sizes[fixedSizeIndex].size;
1424 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " size index : %d, actual size : %d\n", fixedSizeIndex, actualPointSize );
1426 // Tell Freetype to use this size
1427 error = FT_Select_Size( ftFace, fixedSizeIndex );
1428 if ( FT_Err_Ok != error )
1430 DALI_LOG_INFO( gLogFilter, Debug::General, "FreeType Select_Size error: %d\n", error );
1434 float fixedWidth = static_cast< float >( ftFace->available_sizes[ fixedSizeIndex ].width );
1435 float fixedHeight = static_cast< float >( ftFace->available_sizes[ fixedSizeIndex ].height );
1437 // Indicate that the font is a fixed sized bitmap
1438 FontMetrics metrics( fixedHeight, // The ascender in pixels.
1440 fixedHeight, // The height in pixels.
1444 mFontCache.push_back( FontFaceCacheItem( ftFace, path, requestedPointSize, faceIndex, metrics, fixedWidth, fixedHeight, hasColorTables ) );
1445 id = mFontCache.size();
1450 error = FT_Set_Char_Size( ftFace,
1456 if( FT_Err_Ok == error )
1459 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1461 FontMetrics metrics( static_cast< float >( ftMetrics.ascender ) * FROM_266,
1462 static_cast< float >( ftMetrics.descender ) * FROM_266,
1463 static_cast< float >( ftMetrics.height ) * FROM_266,
1464 static_cast< float >( ftFace->underline_position ) * FROM_266,
1465 static_cast< float >( ftFace->underline_thickness ) * FROM_266 );
1467 mFontCache.push_back( FontFaceCacheItem( ftFace, path, requestedPointSize, faceIndex, metrics ) );
1468 id = mFontCache.size();
1472 DALI_LOG_INFO( gLogFilter, Debug::General, " FreeType Set_Char_Size error: %d for pointSize %d\n", error, requestedPointSize );
1478 if( cacheDescription )
1480 CacheFontPath( ftFace, id, requestedPointSize, path );
1486 DALI_LOG_INFO( gLogFilter, Debug::General, " FreeType New_Face error: %d for [%s]\n", error, path.c_str() );
1489 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
1490 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::CreateFont\n" );
1495 void FontClient::Plugin::ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap )
1497 if( srcBitmap.width*srcBitmap.rows > 0 )
1499 switch( srcBitmap.pixel_mode )
1501 case FT_PIXEL_MODE_GRAY:
1503 if( srcBitmap.pitch == static_cast<int>( srcBitmap.width ) )
1505 const unsigned int bufferSize = srcBitmap.width * srcBitmap.rows;
1506 data.buffer = new unsigned char[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
1507 data.width = srcBitmap.width;
1508 data.height = srcBitmap.rows;
1509 data.format = Pixel::L8;
1510 memcpy( data.buffer, srcBitmap.buffer, bufferSize );
1515 #ifdef FREETYPE_BITMAP_SUPPORT
1516 case FT_PIXEL_MODE_BGRA:
1518 if( srcBitmap.pitch == static_cast<int>( srcBitmap.width << 2u ) )
1520 // Set the input dimensions.
1521 const ImageDimensions inputDimensions( srcBitmap.width, srcBitmap.rows );
1523 // Set the output dimensions.
1524 // If the output dimension is not given, the input dimension is set
1525 // and won't be downscaling.
1526 data.width = ( data.width == 0 ) ? srcBitmap.width : data.width;
1527 data.height = ( data.height == 0 ) ? srcBitmap.rows : data.height;
1528 const ImageDimensions desiredDimensions( data.width, data.height );
1530 // Creates the output buffer
1531 const unsigned int bufferSize = data.width * data.height * 4u;
1532 data.buffer = new unsigned char[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
1534 if( inputDimensions == desiredDimensions )
1536 // There isn't downscaling.
1537 memcpy( data.buffer, srcBitmap.buffer, bufferSize );
1541 Dali::Internal::Platform::LanczosSample4BPP( srcBitmap.buffer,
1544 desiredDimensions );
1546 data.format = Pixel::BGRA8888;
1553 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::ConvertBitmap. FontClient Unable to create Bitmap of this PixelType\n" );
1560 bool FontClient::Plugin::FindFont( const FontPath& path,
1561 PointSize26Dot6 requestedPointSize,
1562 FaceIndex faceIndex,
1563 FontId& fontId ) const
1565 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFont\n" );
1566 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
1567 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1568 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of fonts in the cache : %d\n", mFontCache.size() );
1571 for( std::vector<FontFaceCacheItem>::const_iterator it = mFontCache.begin(),
1572 endIt = mFontCache.end();
1576 const FontFaceCacheItem& cacheItem = *it;
1578 if( cacheItem.mRequestedPointSize == requestedPointSize &&
1579 cacheItem.mFaceIndex == faceIndex &&
1580 cacheItem.mPath == path )
1584 DALI_LOG_INFO( gLogFilter, Debug::General, " font found, id : %d\n", fontId );
1585 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1591 DALI_LOG_INFO( gLogFilter, Debug::General, " font not found\n" );
1592 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1597 bool FontClient::Plugin::FindValidatedFont( const FontDescription& fontDescription,
1598 FontDescriptionId& validatedFontId )
1600 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindValidatedFont\n" );
1601 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1602 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1603 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1604 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1605 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1606 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of validated fonts in the cache : %d\n", mValidatedFontCache.size() );
1608 validatedFontId = 0u;
1610 for( std::vector<FontDescriptionCacheItem>::const_iterator it = mValidatedFontCache.begin(),
1611 endIt = mValidatedFontCache.end();
1615 const FontDescriptionCacheItem& item = *it;
1617 if( !fontDescription.family.empty() &&
1618 ( fontDescription.family == item.fontDescription.family ) &&
1619 ( fontDescription.width == item.fontDescription.width ) &&
1620 ( fontDescription.weight == item.fontDescription.weight ) &&
1621 ( fontDescription.slant == item.fontDescription.slant ) )
1623 validatedFontId = item.index;
1625 DALI_LOG_INFO( gLogFilter, Debug::General, " validated font found, id : %d\n", validatedFontId );
1626 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindValidatedFont\n" );
1631 DALI_LOG_INFO( gLogFilter, Debug::General, " validated font not found\n" );
1632 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindValidatedFont\n" );
1636 bool FontClient::Plugin::FindFallbackFontList( const FontDescription& fontDescription,
1637 FontList*& fontList,
1638 CharacterSetList*& characterSetList )
1640 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFallbackFontList\n" );
1641 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1642 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1643 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1644 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1645 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1646 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of fallback font lists in the cache : %d\n", mFallbackCache.size() );
1650 for( std::vector<FallbackCacheItem>::const_iterator it = mFallbackCache.begin(), endIt = mFallbackCache.end();
1654 const FallbackCacheItem& item = *it;
1656 if( !fontDescription.family.empty() &&
1657 ( fontDescription.family == item.fontDescription.family ) &&
1658 ( fontDescription.width == item.fontDescription.width ) &&
1659 ( fontDescription.weight == item.fontDescription.weight ) &&
1660 ( fontDescription.slant == item.fontDescription.slant ) )
1662 fontList = item.fallbackFonts;
1663 characterSetList = item.characterSets;
1665 DALI_LOG_INFO( gLogFilter, Debug::General, " fallback font list found.\n" );
1666 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFontList\n" );
1671 DALI_LOG_INFO( gLogFilter, Debug::General, " fallback font list not found.\n" );
1672 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFontList\n" );
1676 bool FontClient::Plugin::FindFont( FontDescriptionId validatedFontId,
1677 PointSize26Dot6 requestedPointSize,
1680 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFont\n" );
1681 DALI_LOG_INFO( gLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId );
1682 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1686 for( std::vector<FontIdCacheItem>::const_iterator it = mFontIdCache.begin(),
1687 endIt = mFontIdCache.end();
1691 const FontIdCacheItem& item = *it;
1693 if( ( validatedFontId == item.validatedFontId ) &&
1694 ( requestedPointSize == item.requestedPointSize ) )
1696 fontId = item.fontId;
1698 DALI_LOG_INFO( gLogFilter, Debug::General, " font found, id : %d\n", fontId );
1699 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1704 DALI_LOG_INFO( gLogFilter, Debug::General, " font not found.\n" );
1705 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1709 bool FontClient::Plugin::IsScalable( const FontPath& path )
1711 bool isScalable = false;
1714 int error = FT_New_Face( mFreeTypeLibrary,
1718 if( FT_Err_Ok != error )
1720 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: %s\n", path.c_str() );
1724 isScalable = ftFace->face_flags & FT_FACE_FLAG_SCALABLE;
1730 bool FontClient::Plugin::IsScalable( const FontDescription& fontDescription )
1732 // Create a font pattern.
1733 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1735 FcResult result = FcResultMatch;
1737 // match the pattern
1738 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1739 bool isScalable = false;
1743 // Get the path to the font file name.
1745 GetFcString( match, FC_FILE, path );
1746 isScalable = IsScalable( path );
1750 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str() );
1752 FcPatternDestroy( fontFamilyPattern );
1753 FcPatternDestroy( match );
1757 void FontClient::Plugin::GetFixedSizes( const FontPath& path, Vector< PointSize26Dot6 >& sizes )
1759 // Empty the caller container
1763 int error = FT_New_Face( mFreeTypeLibrary,
1767 if( FT_Err_Ok != error )
1769 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font path : [%s]\n", path.c_str() );
1772 // Fetch the number of fixed sizes available
1773 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1775 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1777 sizes.PushBack( ftFace->available_sizes[ i ].size );
1782 void FontClient::Plugin::GetFixedSizes( const FontDescription& fontDescription,
1783 Vector< PointSize26Dot6 >& sizes )
1785 // Create a font pattern.
1786 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1788 FcResult result = FcResultMatch;
1790 // match the pattern
1791 FcPattern* match = FcFontMatch( NULL /* use default configure */, fontFamilyPattern, &result );
1795 // Get the path to the font file name.
1797 GetFcString( match, FC_FILE, path );
1798 GetFixedSizes( path, sizes );
1802 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str() );
1804 FcPatternDestroy( match );
1805 FcPatternDestroy( fontFamilyPattern );
1808 void FontClient::Plugin::CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path )
1810 FontDescription description;
1811 description.path = path;
1812 description.family = FontFamily( ftFace->family_name );
1813 description.weight = FontWeight::NONE;
1814 description.width = FontWidth::NONE;
1815 description.slant = FontSlant::NONE;
1817 // Note FreeType doesn't give too much info to build a proper font style.
1818 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1820 description.slant = FontSlant::ITALIC;
1822 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1824 description.weight = FontWeight::BOLD;
1827 FontDescriptionId validatedFontId = 0u;
1828 if( !FindValidatedFont( description,
1831 // Set the index to the vector of paths to font file names.
1832 validatedFontId = mFontDescriptionCache.size();
1834 FcPattern* pattern = CreateFontFamilyPattern( description );
1836 FcResult result = FcResultMatch;
1837 FcPattern* match = FcFontMatch( NULL, pattern, &result );
1839 FcCharSet* characterSet = NULL;
1840 FcPatternGetCharSet( match, FC_CHARSET, 0u, &characterSet );
1842 FcPatternDestroy( match );
1843 FcPatternDestroy( pattern );
1845 mFontCache[id-1u].mCharacterSet = characterSet;
1847 // Add the path to the cache.
1848 mFontDescriptionCache.push_back( description );
1849 mCharacterSetCache.PushBack( characterSet );
1851 // Cache the index and the font's description.
1852 FontDescriptionCacheItem item( description,
1855 mValidatedFontCache.push_back( item );
1857 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
1858 mFontIdCache.push_back( FontIdCacheItem( validatedFontId,
1864 } // namespace Internal
1866 } // namespace TextAbstraction