2 * Copyright (c) 2018 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/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/text-abstraction/font-client-helper.h>
28 #include <dali/internal/imaging/common/image-operations.h>
29 #include <dali/internal/adaptor/common/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( FontDescription&& font, FontList* fallbackFonts, CharacterSetList* characterSets )
142 : fontDescription{ std::move( font ) },
143 fallbackFonts{ fallbackFonts },
144 characterSets{ characterSets }
148 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem( const FontDescription& fontDescription,
149 FontDescriptionId index )
150 : fontDescription{ fontDescription },
155 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem( FontDescription&& fontDescription,
156 FontDescriptionId index )
157 : fontDescription{ std::move( fontDescription ) },
162 FontClient::Plugin::FontDescriptionSizeCacheItem::FontDescriptionSizeCacheItem( FontDescriptionId validatedFontId,
163 PointSize26Dot6 requestedPointSize,
165 : validatedFontId( validatedFontId ),
166 requestedPointSize( requestedPointSize ),
171 FontClient::Plugin::FontFaceCacheItem::FontFaceCacheItem( FT_Face ftFace,
172 const FontPath& path,
173 PointSize26Dot6 requestedPointSize,
175 const FontMetrics& metrics )
176 : mFreeTypeFace( ftFace ),
178 mRequestedPointSize( requestedPointSize ),
181 mCharacterSet( nullptr ),
182 mFixedWidthPixels( 0.0f ),
183 mFixedHeightPixels( 0.0f ),
185 mIsFixedSizeBitmap( false ),
186 mHasColorTables( false )
190 FontClient::Plugin::FontFaceCacheItem::FontFaceCacheItem( FT_Face ftFace,
191 const FontPath& path,
192 PointSize26Dot6 requestedPointSize,
194 const FontMetrics& metrics,
197 bool hasColorTables )
198 : mFreeTypeFace( ftFace ),
200 mRequestedPointSize( requestedPointSize ),
203 mCharacterSet( nullptr ),
204 mFixedWidthPixels( fixedWidth ),
205 mFixedHeightPixels( fixedHeight ),
207 mIsFixedSizeBitmap( true ),
208 mHasColorTables( hasColorTables )
212 FontClient::Plugin::Plugin( unsigned int horizontalDpi,
213 unsigned int verticalDpi )
214 : mFreeTypeLibrary( nullptr ),
215 mDpiHorizontal( horizontalDpi ),
216 mDpiVertical( verticalDpi ),
217 mDefaultFontDescription(),
221 mValidatedFontCache(),
222 mFontDescriptionCache( 1u ),
223 mCharacterSetCache(),
224 mFontDescriptionSizeCache(),
225 mVectorFontCache( nullptr ),
227 mDefaultFontDescriptionCached( false )
229 mCharacterSetCache.Resize( 1u );
231 int error = FT_Init_FreeType( &mFreeTypeLibrary );
232 if( FT_Err_Ok != error )
234 DALI_LOG_INFO( gLogFilter, Debug::General, "FreeType Init error: %d\n", error );
237 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
238 mVectorFontCache = new VectorFontCache( mFreeTypeLibrary );
242 FontClient::Plugin::~Plugin()
244 for( auto& item : mFallbackCache )
246 if( item.fallbackFonts )
248 delete item.fallbackFonts;
249 delete item.characterSets;
250 item.fallbackFonts = nullptr;
251 item.characterSets = nullptr;
255 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
256 delete mVectorFontCache;
258 DestroyMatchedPatterns();
259 FT_Done_FreeType( mFreeTypeLibrary );
262 void FontClient::Plugin::SetDpi( unsigned int horizontalDpi,
263 unsigned int verticalDpi )
265 mDpiHorizontal = horizontalDpi;
266 mDpiVertical = verticalDpi;
269 void FontClient::Plugin::ResetSystemDefaults()
271 mDefaultFontDescriptionCached = false;
274 void FontClient::Plugin::SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList )
276 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::SetFontList\n" );
277 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
278 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
279 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
280 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
284 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
286 FcResult result = FcResultMatch;
288 // Match the pattern.
289 FcFontSet* fontSet = FcFontSort( nullptr /* use default configure */,
291 false /* don't trim */,
295 if( nullptr != fontSet )
297 DALI_LOG_INFO( gLogFilter, Debug::General, " number of fonts found : [%d]\n", fontSet->nfont );
298 // Reserve some space to avoid reallocations.
299 fontList.reserve( fontSet->nfont );
301 for( int i = 0u; i < fontSet->nfont; ++i )
303 FcPattern* fontPattern = fontSet->fonts[i];
307 // Skip fonts with no path
308 if( GetFcString( fontPattern, FC_FILE, path ) )
310 FcCharSet* characterSet = nullptr;
311 FcPatternGetCharSet( fontPattern, FC_CHARSET, 0u, &characterSet );
313 characterSetList.PushBack( characterSet );
314 fontList.push_back( FontDescription() );
315 FontDescription& newFontDescription = fontList.back();
317 newFontDescription.path = std::move( path );
322 GetFcString( fontPattern, FC_FAMILY, newFontDescription.family );
323 GetFcInt( fontPattern, FC_WIDTH, width );
324 GetFcInt( fontPattern, FC_WEIGHT, weight );
325 GetFcInt( fontPattern, FC_SLANT, slant );
326 newFontDescription.width = IntToWidthType( width );
327 newFontDescription.weight = IntToWeightType( weight );
328 newFontDescription.slant = IntToSlantType( slant );
330 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", newFontDescription.family.c_str() );
331 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", newFontDescription.path.c_str() );
332 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[newFontDescription.width] );
333 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[newFontDescription.weight] );
334 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[newFontDescription.slant] );
338 FcFontSetDestroy( fontSet );
342 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " No fonts found.\n" );
345 FcPatternDestroy( fontFamilyPattern );
346 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::SetFontList\n" );
349 void FontClient::Plugin::GetDefaultFonts( FontList& defaultFonts )
351 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDefaultFonts\n" );
353 if( mDefaultFonts.empty() )
355 FontDescription fontDescription;
356 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
357 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
358 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
359 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
360 SetFontList( fontDescription, mDefaultFonts, mDefaultFontCharacterSets );
363 defaultFonts = mDefaultFonts;
365 DALI_LOG_INFO( gLogFilter, Debug::General, " number of default fonts : [%d]\n", mDefaultFonts.size() );
366 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDefaultFonts\n" );
369 void FontClient::Plugin::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
371 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDefaultPlatformFontDescription\n");
373 if( !mDefaultFontDescriptionCached )
375 // Clear any font config stored info in the caches.
376 mDefaultFontCharacterSets.Clear();
377 mCharacterSetCache.Clear();
379 for( auto& item : mFallbackCache )
381 item.characterSets->Clear();
384 for( auto& item : mFontFaceCache )
386 // Set the character set pointer as null. Will be created again the next time IsCharacterSupportedByFont()
387 item.mCharacterSet = nullptr;
390 // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
391 FcInitReinitialize();
393 FcPattern* matchPattern = FcPatternCreate();
397 FcConfigSubstitute( nullptr, matchPattern, FcMatchPattern );
398 FcDefaultSubstitute( matchPattern );
400 FcCharSet* characterSet = nullptr;
401 MatchFontDescriptionToPattern( matchPattern, mDefaultFontDescription, &characterSet );
402 FcPatternDestroy( matchPattern );
405 // Create again the character sets as they are not valid after FcInitReinitialize()
407 for( const auto& description : mDefaultFonts )
409 mDefaultFontCharacterSets.PushBack( CreateCharacterSetFromDescription( description ) );
412 for( const auto& description : mFontDescriptionCache )
414 mCharacterSetCache.PushBack( CreateCharacterSetFromDescription( description ) );
417 for( auto& item : mFallbackCache )
419 if( nullptr != item.fallbackFonts )
421 if( nullptr == item.characterSets )
423 item.characterSets = new CharacterSetList;
426 for( const auto& description : *( item.fallbackFonts ) )
428 item.characterSets->PushBack( CreateCharacterSetFromDescription( description ) );
433 mDefaultFontDescriptionCached = true;
436 fontDescription.path = mDefaultFontDescription.path;
437 fontDescription.family = mDefaultFontDescription.family;
438 fontDescription.width = mDefaultFontDescription.width;
439 fontDescription.weight = mDefaultFontDescription.weight;
440 fontDescription.slant = mDefaultFontDescription.slant;
442 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
443 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
444 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
445 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
446 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
447 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDefaultPlatformFontDescription\n");
450 void FontClient::Plugin::GetSystemFonts( FontList& systemFonts )
452 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetSystemFonts\n");
454 if( mSystemFonts.empty() )
459 systemFonts = mSystemFonts;
460 DALI_LOG_INFO( gLogFilter, Debug::General, " number of system fonts : [%d]\n", mSystemFonts.size() );
461 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetSystemFonts\n");
464 void FontClient::Plugin::GetDescription( FontId id,
465 FontDescription& fontDescription ) const
467 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetDescription\n");
468 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
470 for( const auto& item : mFontDescriptionSizeCache )
472 if( item.fontId == id )
474 fontDescription = *( mFontDescriptionCache.begin() + item.validatedFontId );
476 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
477 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
478 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
479 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
480 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
481 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDescription\n");
486 DALI_LOG_INFO( gLogFilter, Debug::General, " No description found for the font ID %d\n", id );
487 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetDescription\n");
490 PointSize26Dot6 FontClient::Plugin::GetPointSize( FontId id )
492 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetPointSize\n");
493 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
494 const FontId index = id - 1u;
497 ( index < mFontFaceCache.size() ) )
499 DALI_LOG_INFO( gLogFilter, Debug::General, " point size : %d\n", ( *( mFontFaceCache.begin() + index ) ).mRequestedPointSize );
500 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetPointSize\n");
501 return ( *( mFontFaceCache.begin() + index ) ).mRequestedPointSize;
505 DALI_LOG_INFO( gLogFilter, Debug::General, " Invalid font ID %d\n", id );
508 DALI_LOG_INFO( gLogFilter, Debug::General, " default point size : %d\n", TextAbstraction::FontClient::DEFAULT_POINT_SIZE );
509 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetPointSize\n");
510 return TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
513 bool FontClient::Plugin::IsCharacterSupportedByFont( FontId fontId, Character character )
515 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::IsCharacterSupportedByFont\n");
516 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
517 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", character );
519 if( ( fontId < 1u ) || ( fontId > mFontFaceCache.size() ) )
521 DALI_LOG_INFO( gLogFilter, Debug::General, " Invalid font id. Number of items in the cache: %d\n",mFontFaceCache.size());
522 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::IsCharacterSupportedByFont\n");
528 bool isSupported = false;
530 FontFaceCacheItem& cacheItem = mFontFaceCache[fontId];
532 if( nullptr == cacheItem.mCharacterSet )
534 // Create again the character set.
535 // It can be null if the ResetSystemDefaults() method has been called.
537 FontDescription description;
538 description.path = cacheItem.mPath;
539 description.family = std::move( FontFamily( cacheItem.mFreeTypeFace->family_name ) );
540 description.weight = FontWeight::NONE;
541 description.width = FontWidth::NONE;
542 description.slant = FontSlant::NONE;
544 // Note FreeType doesn't give too much info to build a proper font style.
545 if( cacheItem.mFreeTypeFace->style_flags & FT_STYLE_FLAG_ITALIC )
547 description.slant = FontSlant::ITALIC;
549 if( cacheItem.mFreeTypeFace->style_flags & FT_STYLE_FLAG_BOLD )
551 description.weight = FontWeight::BOLD;
554 cacheItem.mCharacterSet = CreateCharacterSetFromDescription( description );
557 isSupported = FcCharSetHasChar( cacheItem.mCharacterSet, character );
559 DALI_LOG_INFO( gLogFilter, Debug::General, " is supported : %s\n", (isSupported ? "true" : "false") );
560 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::IsCharacterSupportedByFont\n");
564 FontId FontClient::Plugin::FindFontForCharacter( const FontList& fontList,
565 const CharacterSetList& characterSetList,
567 PointSize26Dot6 requestedPointSize,
570 DALI_ASSERT_DEBUG( ( fontList.size() == characterSetList.Count() ) && "FontClient::Plugin::FindFontForCharacter. Different number of fonts and character sets." );
572 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFontForCharacter\n" );
573 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", character );
574 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
575 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
578 bool foundColor = false;
580 DALI_LOG_INFO( gLogFilter, Debug::General, " number of fonts : %d\n", fontList.size() );
582 // Traverse the list of fonts.
583 // Check for each font if supports the character.
584 for( unsigned int index = 0u, numberOfFonts = fontList.size(); index < numberOfFonts; ++index )
586 const FontDescription& description = fontList[index];
587 const FcCharSet* const characterSet = characterSetList[index];
589 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", description.family.c_str() );
590 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", description.path.c_str() );
591 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[description.width] );
592 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[description.weight] );
593 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[description.slant] );
595 bool foundInRanges = false;
596 if( nullptr != characterSet )
598 foundInRanges = FcCharSetHasChar( characterSet, character );
603 fontId = GetFontId( description,
607 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " font id : %d\n", fontId );
611 if( ( fontId > 0 ) &&
612 ( fontId - 1u < mFontFaceCache.size() ) )
614 const FontFaceCacheItem& item = mFontFaceCache[fontId - 1u];
616 foundColor = item.mHasColorTables;
619 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " foundColor : %s\n", ( foundColor ? "true" : "false" ) );
622 // Keep going unless we prefer a different (color) font.
623 if( !preferColor || foundColor )
630 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
631 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFontForCharacter\n" );
635 FontId FontClient::Plugin::FindDefaultFont( Character charcode,
636 PointSize26Dot6 requestedPointSize,
639 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindDefaultFont\n" );
640 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", charcode );
641 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
642 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
646 // Create the list of default fonts if it has not been created.
647 if( mDefaultFonts.empty() )
649 FontDescription fontDescription;
650 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
651 fontDescription.width = IntToWidthType( DEFAULT_FONT_WIDTH );
652 fontDescription.weight = IntToWeightType( DEFAULT_FONT_WEIGHT );
653 fontDescription.slant = IntToSlantType( DEFAULT_FONT_SLANT );
655 SetFontList( fontDescription, mDefaultFonts, mDefaultFontCharacterSets );
657 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of default fonts : %d\n", mDefaultFonts.size() );
660 // Traverse the list of default fonts.
661 // Check for each default font if supports the character.
662 fontId = FindFontForCharacter( mDefaultFonts, mDefaultFontCharacterSets, charcode, requestedPointSize, preferColor );
664 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
665 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindDefaultFont\n" );
670 FontId FontClient::Plugin::FindFallbackFont( Character charcode,
671 const FontDescription& preferredFontDescription,
672 PointSize26Dot6 requestedPointSize,
675 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFallbackFont\n" );
676 DALI_LOG_INFO( gLogFilter, Debug::General, " character : %p\n", charcode );
677 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
678 DALI_LOG_INFO( gLogFilter, Debug::General, " preferColor : %s\n", ( preferColor ? "true" : "false" ) );
680 // The font id to be returned.
683 FontDescription fontDescription;
685 // Fill the font description with the preferred font description and complete with the defaults.
686 fontDescription.family = preferredFontDescription.family.empty() ? DEFAULT_FONT_FAMILY_NAME : preferredFontDescription.family;
687 fontDescription.weight = ( ( FontWeight::NONE == preferredFontDescription.weight ) ? IntToWeightType( DEFAULT_FONT_WEIGHT ) : preferredFontDescription.weight );
688 fontDescription.width = ( ( FontWidth::NONE == preferredFontDescription.width ) ? IntToWidthType( DEFAULT_FONT_WIDTH ) : preferredFontDescription.width );
689 fontDescription.slant = ( ( FontSlant::NONE == preferredFontDescription.slant ) ? IntToSlantType( DEFAULT_FONT_SLANT ) : preferredFontDescription.slant );
691 DALI_LOG_INFO( gLogFilter, Debug::General, " preferredFontDescription --> fontDescription\n" );
692 DALI_LOG_INFO( gLogFilter, Debug::General, " [%s] --> [%s]\n", preferredFontDescription.family.c_str(), fontDescription.family.c_str() );
693 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWeight::Name[preferredFontDescription.weight], FontWeight::Name[fontDescription.weight] );
694 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWidth::Name[preferredFontDescription.width], FontWidth::Name[fontDescription.width] );
695 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontSlant::Name[preferredFontDescription.slant], FontSlant::Name[fontDescription.slant] );
697 // Check first if the font's description has been queried before.
698 FontList* fontList = nullptr;
699 CharacterSetList* characterSetList = nullptr;
701 if( !FindFallbackFontList( fontDescription, fontList, characterSetList ) )
703 fontList = new FontList;
704 characterSetList = new CharacterSetList;
706 SetFontList( fontDescription, *fontList, *characterSetList );
708 // Add the font-list to the cache.
709 mFallbackCache.push_back( std::move( FallbackCacheItem( std::move( fontDescription ), fontList, characterSetList ) ) );
712 if( fontList && characterSetList )
714 fontId = FindFontForCharacter( *fontList, *characterSetList, charcode, requestedPointSize, preferColor );
717 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
718 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFont\n");
722 FontId FontClient::Plugin::GetFontId( const FontPath& path,
723 PointSize26Dot6 requestedPointSize,
725 bool cacheDescription )
727 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetFontId\n" );
728 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
729 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
733 if( nullptr != mFreeTypeLibrary )
736 if( FindFont( path, requestedPointSize, faceIndex, foundId ) )
742 id = CreateFont( path, requestedPointSize, faceIndex, cacheDescription );
746 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
747 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetFontId\n" );
752 FontId FontClient::Plugin::GetFontId( const FontDescription& fontDescription,
753 PointSize26Dot6 requestedPointSize,
754 FaceIndex faceIndex )
756 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetFontId\n" );
757 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
758 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
759 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
760 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
761 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
762 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
764 // This method uses three vectors which caches:
765 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
766 // * The path to font file names.
767 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
769 // 1) Checks in the cache if the font's description has been validated before.
770 // If it was it gets an index to the vector with paths to font file names. Otherwise,
771 // retrieves using font config a path to a font file name which matches with the
772 // font's description. The path is stored in the cache.
774 // 2) Checks in the cache if the pair 'font point size, index to the vector with paths to
775 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
776 // the GetFontId() method with the path to the font file name and the point size to
779 // The font id to be returned.
782 // Check first if the font's description have been validated before.
783 FontDescriptionId validatedFontId = 0u;
785 if( !FindValidatedFont( fontDescription,
788 // Use font config to validate the font's description.
789 ValidateFont( fontDescription,
793 // Check if exists a pair 'validatedFontId, requestedPointSize' in the cache.
794 if( !FindFont( validatedFontId, requestedPointSize, fontId ) )
796 // Retrieve the font file name path.
797 const FontDescription& description = *( mFontDescriptionCache.begin() + validatedFontId );
799 // Retrieve the font id. Do not cache the description as it has been already cached.
800 fontId = GetFontId( description.path,
805 mFontFaceCache[fontId-1u].mCharacterSet = mCharacterSetCache[validatedFontId];
807 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
808 mFontDescriptionSizeCache.push_back( FontDescriptionSizeCacheItem( validatedFontId,
813 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", fontId );
814 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetFontId\n" );
819 void FontClient::Plugin::ValidateFont( const FontDescription& fontDescription,
820 FontDescriptionId& validatedFontId )
822 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::ValidateFont\n" );
823 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
824 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
825 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
826 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
827 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
829 // Create a font pattern.
830 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
832 FontDescription description;
834 FcCharSet* characterSet = nullptr;
835 bool matched = MatchFontDescriptionToPattern( fontFamilyPattern, description, &characterSet );
836 FcPatternDestroy( fontFamilyPattern );
838 if( matched && ( nullptr != characterSet ) )
840 // Set the index to the vector of paths to font file names.
841 validatedFontId = mFontDescriptionCache.size();
843 DALI_LOG_INFO( gLogFilter, Debug::General, " matched description; family : [%s]\n", description.family.c_str() );
844 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", description.path.c_str() );
845 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[description.width] );
846 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[description.weight] );
847 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[description.slant] );
848 DALI_LOG_INFO( gLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId );
850 // Add the path to the cache.
851 mFontDescriptionCache.push_back( description );
852 mCharacterSetCache.PushBack( characterSet );
854 // Cache the index and the matched font's description.
855 FontDescriptionCacheItem item( description,
858 mValidatedFontCache.push_back( std::move( item ) );
860 if( ( fontDescription.family != description.family ) ||
861 ( fontDescription.width != description.width ) ||
862 ( fontDescription.weight != description.weight ) ||
863 ( fontDescription.slant != description.slant ) )
865 // Cache the given font's description if it's different than the matched.
866 FontDescriptionCacheItem item( fontDescription,
869 mValidatedFontCache.push_back( std::move( item ) );
874 DALI_LOG_INFO( gLogFilter, Debug::General, " font validation failed for font [%s]\n", fontDescription.family.c_str() );
877 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::ValidateFont\n" );
880 void FontClient::Plugin::GetFontMetrics( FontId fontId,
881 FontMetrics& metrics )
883 if( ( fontId > 0 ) &&
884 ( fontId - 1u < mFontFaceCache.size() ) )
886 const FontFaceCacheItem& font = mFontFaceCache[fontId-1];
888 metrics = font.mMetrics;
890 // Adjust the metrics if the fixed-size font should be down-scaled
891 if( font.mIsFixedSizeBitmap )
893 const float desiredFixedSize = static_cast<float>( font.mRequestedPointSize ) * FROM_266 / POINTS_PER_INCH * mDpiVertical;
895 if( desiredFixedSize > 0.f )
897 const float scaleFactor = desiredFixedSize / static_cast<float>( font.mFixedHeightPixels );
899 metrics.ascender = floorf( metrics.ascender * scaleFactor );
900 metrics.descender = floorf( metrics.descender * scaleFactor );
901 metrics.height = floorf( metrics.height * scaleFactor );
902 metrics.underlinePosition = floorf( metrics.underlinePosition * scaleFactor );
903 metrics.underlineThickness = floorf( metrics.underlineThickness * scaleFactor );
909 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFontMetrics. Invalid font id : %d\n", fontId );
913 GlyphIndex FontClient::Plugin::GetGlyphIndex( FontId fontId,
916 GlyphIndex index = 0u;
918 if( ( fontId > 0u ) &&
919 ( fontId - 1u < mFontFaceCache.size() ) )
921 FT_Face ftFace = mFontFaceCache[fontId-1u].mFreeTypeFace;
923 index = FT_Get_Char_Index( ftFace, charcode );
929 bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
934 if( VECTOR_GLYPH == type )
936 return GetVectorMetrics( array, size, horizontal );
939 return GetBitmapMetrics( array, size, horizontal );
942 bool FontClient::Plugin::GetBitmapMetrics( GlyphInfo* array,
946 bool success( true );
948 for( unsigned int i=0; i<size; ++i )
950 GlyphInfo& glyph = array[i];
952 FontId fontId = glyph.fontId;
955 fontId-1 < mFontFaceCache.size() )
957 const FontFaceCacheItem& font = mFontFaceCache[fontId-1];
959 FT_Face ftFace = font.mFreeTypeFace;
961 #ifdef FREETYPE_BITMAP_SUPPORT
962 // Check to see if we should be loading a Fixed Size bitmap?
963 if ( font.mIsFixedSizeBitmap )
965 int error = FT_Load_Glyph( ftFace, glyph.index, FT_LOAD_COLOR );
966 if ( FT_Err_Ok == error )
968 glyph.width = font.mFixedWidthPixels;
969 glyph.height = font.mFixedHeightPixels;
970 glyph.advance = font.mFixedWidthPixels;
971 glyph.xBearing = 0.0f;
972 glyph.yBearing = font.mFixedHeightPixels;
974 // Adjust the metrics if the fixed-size font should be down-scaled
975 const float desiredFixedSize = static_cast<float>( font.mRequestedPointSize ) * FROM_266 / POINTS_PER_INCH * mDpiVertical;
977 if( desiredFixedSize > 0.f )
979 const float scaleFactor = desiredFixedSize / static_cast<float>( font.mFixedHeightPixels );
981 glyph.width = floorf( glyph.width * scaleFactor );
982 glyph.height = floorf( glyph.height * scaleFactor );
983 glyph.advance = floorf( glyph.advance * scaleFactor );
984 glyph.xBearing = floorf( glyph.xBearing * scaleFactor );
985 glyph.yBearing = floorf( glyph.yBearing * scaleFactor );
987 glyph.scaleFactor = scaleFactor;
992 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetBitmapMetrics. FreeType Bitmap Load_Glyph error %d\n", error );
999 int error = FT_Load_Glyph( ftFace, glyph.index, FT_LOAD_NO_AUTOHINT );
1001 if( FT_Err_Ok == error )
1003 glyph.width = static_cast< float >( ftFace->glyph->metrics.width ) * FROM_266;
1004 glyph.height = static_cast< float >( ftFace->glyph->metrics.height ) * FROM_266 ;
1007 glyph.xBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingX ) * FROM_266;
1008 glyph.yBearing += static_cast< float >( ftFace->glyph->metrics.horiBearingY ) * FROM_266;
1012 glyph.xBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingX ) * FROM_266;
1013 glyph.yBearing += static_cast< float >( ftFace->glyph->metrics.vertBearingY ) * FROM_266;
1031 bool FontClient::Plugin::GetVectorMetrics( GlyphInfo* array,
1035 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1036 bool success( true );
1038 for( unsigned int i=0; i<size; ++i )
1040 FontId fontId = array[i].fontId;
1043 fontId-1 < mFontFaceCache.size() )
1045 FontFaceCacheItem& font = mFontFaceCache[fontId-1];
1047 if( ! font.mVectorFontId )
1049 font.mVectorFontId = mVectorFontCache->GetFontId( font.mPath );
1052 mVectorFontCache->GetGlyphMetrics( font.mVectorFontId, array[i] );
1054 // Vector metrics are in EMs, convert to pixels
1055 const float scale = ( static_cast<float>( font.mRequestedPointSize ) * FROM_266 ) * static_cast<float>( mDpiVertical ) / POINTS_PER_INCH;
1056 array[i].width *= scale;
1057 array[i].height *= scale;
1058 array[i].xBearing *= scale;
1059 array[i].yBearing *= scale;
1060 array[i].advance *= scale;
1074 void FontClient::Plugin::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
1076 if( ( fontId > 0 ) &&
1077 ( fontId - 1u < mFontFaceCache.size() ) )
1079 FT_Face ftFace = mFontFaceCache[fontId - 1u].mFreeTypeFace;
1083 #ifdef FREETYPE_BITMAP_SUPPORT
1084 // Check to see if this is fixed size bitmap
1085 if ( mFontFaceCache[fontId - 1u].mIsFixedSizeBitmap )
1087 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
1092 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_NO_AUTOHINT );
1094 if( FT_Err_Ok == error )
1097 error = FT_Get_Glyph( ftFace->glyph, &glyph );
1099 // Convert to bitmap if necessary
1100 if ( FT_Err_Ok == error )
1102 if( glyph->format != FT_GLYPH_FORMAT_BITMAP )
1104 // Check whether we should create a bitmap for the outline
1105 if( glyph->format == FT_GLYPH_FORMAT_OUTLINE && outlineWidth > 0 )
1109 error = FT_Stroker_New(mFreeTypeLibrary, &stroker );
1111 if ( FT_Err_Ok == error )
1113 FT_Stroker_Set( stroker, outlineWidth * 64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0 );
1114 error = FT_Glyph_StrokeBorder( &glyph, stroker, 0, 1 );
1116 if ( FT_Err_Ok == error )
1118 FT_Stroker_Done( stroker );
1122 DALI_LOG_ERROR( "FT_Glyph_StrokeBorder Failed with error: %d\n", error );
1127 DALI_LOG_ERROR( "FT_Stroker_New Failed with error: %d\n", error );
1131 error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
1132 if ( FT_Err_Ok == error )
1134 FT_BitmapGlyph bitmapGlyph = reinterpret_cast< FT_BitmapGlyph >( glyph );
1135 ConvertBitmap( data, bitmapGlyph->bitmap );
1139 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::CreateBitmap. FT_Get_Glyph Failed with error: %d\n", error );
1144 ConvertBitmap( data, ftFace->glyph->bitmap );
1147 // Created FT_Glyph object must be released with FT_Done_Glyph
1148 FT_Done_Glyph( glyph );
1153 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::CreateBitmap. FT_Load_Glyph Failed with error: %d\n", error );
1158 PixelData FontClient::Plugin::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
1160 TextAbstraction::FontClient::GlyphBufferData data;
1162 CreateBitmap( fontId, glyphIndex, data, outlineWidth );
1164 return PixelData::New( data.buffer,
1165 data.width * data.height * Pixel::GetBytesPerPixel( data.format ),
1169 PixelData::DELETE_ARRAY );
1172 void FontClient::Plugin::CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
1177 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1179 fontId-1 < mFontFaceCache.size() )
1181 FontFaceCacheItem& font = mFontFaceCache[fontId-1];
1183 if( ! font.mVectorFontId )
1185 font.mVectorFontId = mVectorFontCache->GetFontId( font.mPath );
1188 mVectorFontCache->GetVectorBlob( font.mVectorFontId, fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
1193 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph( PointSize26Dot6 requestedPointSize )
1195 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::GetEllipsisGlyph\n" );
1196 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize %d.\n", requestedPointSize );
1198 // First look into the cache if there is an ellipsis glyph for the requested point size.
1199 for( const auto& item : mEllipsisCache )
1201 if( fabsf( item.requestedPointSize - requestedPointSize ) < Math::MACHINE_EPSILON_1000 )
1203 // Use the glyph in the cache.
1205 DALI_LOG_INFO( gLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index );
1206 DALI_LOG_INFO( gLogFilter, Debug::General, " font %d.\n", item.glyph.fontId );
1207 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetEllipsisGlyph\n" );
1213 // No glyph has been found. Create one.
1214 mEllipsisCache.PushBack( EllipsisItem() );
1215 EllipsisItem& item = *( mEllipsisCache.End() - 1u );
1217 item.requestedPointSize = requestedPointSize;
1219 // Find a font for the ellipsis glyph.
1220 item.glyph.fontId = FindDefaultFont( ELLIPSIS_CHARACTER,
1224 // Set the character index to access the glyph inside the font.
1225 item.glyph.index = FT_Get_Char_Index( mFontFaceCache[item.glyph.fontId-1].mFreeTypeFace,
1226 ELLIPSIS_CHARACTER );
1228 GetBitmapMetrics( &item.glyph, 1u, true );
1230 DALI_LOG_INFO( gLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index );
1231 DALI_LOG_INFO( gLogFilter, Debug::General, " font %d.\n", item.glyph.fontId );
1232 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::GetEllipsisGlyph\n" );
1237 bool FontClient::Plugin::IsColorGlyph( FontId fontId, GlyphIndex glyphIndex )
1239 FT_Error error = -1;
1241 #ifdef FREETYPE_BITMAP_SUPPORT
1242 if( ( fontId > 0 ) &&
1243 ( fontId - 1u < mFontFaceCache.size() ) )
1245 const FontFaceCacheItem& item = mFontFaceCache[fontId - 1u];
1246 FT_Face ftFace = item.mFreeTypeFace;
1248 // Check to see if this is fixed size bitmap
1249 if( item.mHasColorTables )
1251 error = FT_Load_Glyph( ftFace, glyphIndex, FT_LOAD_COLOR );
1256 return FT_Err_Ok == error;
1259 void FontClient::Plugin::InitSystemFonts()
1261 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::InitSystemFonts\n" );
1263 FcFontSet* fontSet = GetFcFontSet();
1267 DALI_LOG_INFO( gLogFilter, Debug::General, " number of system fonts : %d\n", fontSet->nfont );
1269 // Reserve some space to avoid reallocations.
1270 mSystemFonts.reserve( fontSet->nfont );
1272 for( int i = 0u; i < fontSet->nfont; ++i )
1274 FcPattern* fontPattern = fontSet->fonts[i];
1278 // Skip fonts with no path
1279 if( GetFcString( fontPattern, FC_FILE, path ) )
1281 mSystemFonts.push_back( FontDescription() );
1282 FontDescription& fontDescription = mSystemFonts.back();
1284 fontDescription.path = std::move( path );
1289 GetFcString( fontPattern, FC_FAMILY, fontDescription.family );
1290 GetFcInt( fontPattern, FC_WIDTH, width );
1291 GetFcInt( fontPattern, FC_WEIGHT, weight );
1292 GetFcInt( fontPattern, FC_SLANT, slant );
1293 fontDescription.width = IntToWidthType( width );
1294 fontDescription.weight = IntToWeightType( weight );
1295 fontDescription.slant = IntToSlantType( slant );
1297 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " description; family : [%s]\n", fontDescription.family.c_str() );
1298 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1299 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1300 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1301 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1305 FcFontSetDestroy( fontSet );
1307 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::InitSystemFonts\n" );
1310 bool FontClient::Plugin::MatchFontDescriptionToPattern( FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, FcCharSet** characterSet )
1312 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::MatchFontDescriptionToPattern\n" );
1314 FcResult result = FcResultMatch;
1315 FcPattern* match = FcFontMatch( nullptr /* use default configure */, pattern, &result );
1317 const bool matched = nullptr != match;
1318 DALI_LOG_INFO( gLogFilter, Debug::General, " pattern matched : %s\n", ( matched ? "true" : "false" ) );
1325 GetFcString( match, FC_FILE, fontDescription.path );
1326 GetFcString( match, FC_FAMILY, fontDescription.family );
1327 GetFcInt( match, FC_WIDTH, width );
1328 GetFcInt( match, FC_WEIGHT, weight );
1329 GetFcInt( match, FC_SLANT, slant );
1330 fontDescription.width = IntToWidthType( width );
1331 fontDescription.weight = IntToWeightType( weight );
1332 fontDescription.slant = IntToSlantType( slant );
1334 // Cache the character ranges.
1335 FcPatternGetCharSet( match, FC_CHARSET, 0u, characterSet );
1337 // destroyed the matched pattern
1338 FcPatternDestroy( match );
1340 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1341 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1342 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1343 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1344 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1347 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::MatchFontDescriptionToPattern\n" );
1351 FcPattern* FontClient::Plugin::CreateFontFamilyPattern( const FontDescription& fontDescription ) const
1353 // create the cached font family lookup pattern
1354 // a pattern holds a set of names, each name refers to a property of the font
1355 FcPattern* fontFamilyPattern = FcPatternCreate();
1357 if( !fontFamilyPattern )
1362 // add a property to the pattern for the font family
1363 FcPatternAddString( fontFamilyPattern, FC_FAMILY, reinterpret_cast<const FcChar8*>( fontDescription.family.c_str() ) );
1365 int width = FONT_WIDTH_TYPE_TO_INT[fontDescription.width];
1369 width = DEFAULT_FONT_WIDTH;
1372 int weight = FONT_WEIGHT_TYPE_TO_INT[fontDescription.weight];
1376 weight = DEFAULT_FONT_WEIGHT;
1379 int slant = FONT_SLANT_TYPE_TO_INT[fontDescription.slant];
1383 slant = DEFAULT_FONT_SLANT;
1386 FcPatternAddInteger( fontFamilyPattern, FC_WIDTH, width );
1387 FcPatternAddInteger( fontFamilyPattern, FC_WEIGHT, weight );
1388 FcPatternAddInteger( fontFamilyPattern, FC_SLANT, slant );
1390 // Add a property of the pattern, to say we want to match TrueType fonts
1391 FcPatternAddString( fontFamilyPattern , FC_FONTFORMAT, reinterpret_cast<const FcChar8*>( FONT_FORMAT.c_str() ) );
1393 // modify the config, with the mFontFamilyPatterm
1394 FcConfigSubstitute( nullptr /* use default configure */, fontFamilyPattern, FcMatchPattern );
1396 // provide default values for unspecified properties in the font pattern
1397 // e.g. patterns without a specified style or weight are set to Medium
1398 FcDefaultSubstitute( fontFamilyPattern );
1400 return fontFamilyPattern;
1403 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
1405 // create a new pattern.
1406 // a pattern holds a set of names, each name refers to a property of the font
1407 FcPattern* pattern = FcPatternCreate();
1409 FcFontSet* fontset = NULL;
1411 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
1412 FcObjectSet* objectSet = FcObjectSetCreate();
1416 // build an object set from a list of property names
1417 FcObjectSetAdd( objectSet, FC_FILE );
1418 FcObjectSetAdd( objectSet, FC_FAMILY );
1419 FcObjectSetAdd( objectSet, FC_WIDTH );
1420 FcObjectSetAdd( objectSet, FC_WEIGHT );
1421 FcObjectSetAdd( objectSet, FC_SLANT );
1423 // get a list of fonts
1424 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
1425 fontset = FcFontList( NULL /* the default configuration is checked to be up to date, and used */, pattern, objectSet );
1427 // clear up the object set
1428 FcObjectSetDestroy( objectSet );
1430 // clear up the pattern
1433 FcPatternDestroy( pattern );
1439 bool FontClient::Plugin::GetFcString( const FcPattern* const pattern,
1440 const char* const n,
1441 std::string& string )
1443 FcChar8* file = nullptr;
1444 const FcResult retVal = FcPatternGetString( pattern, n, 0u, &file );
1446 if( FcResultMatch == retVal )
1448 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1449 string.assign( reinterpret_cast<const char*>( file ) );
1457 bool FontClient::Plugin::GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal )
1459 const FcResult retVal = FcPatternGetInteger( pattern, n, 0u, &intVal );
1461 if( FcResultMatch == retVal )
1469 FontId FontClient::Plugin::CreateFont( const FontPath& path,
1470 PointSize26Dot6 requestedPointSize,
1471 FaceIndex faceIndex,
1472 bool cacheDescription )
1474 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::CreateFont\n" );
1475 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
1476 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1480 // Create & cache new font face
1482 int error = FT_New_Face( mFreeTypeLibrary,
1487 if( FT_Err_Ok == error )
1489 // Check if a font is scalable.
1490 const bool isScalable = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_SCALABLE ) );
1491 const bool hasFixedSizedBitmaps = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_FIXED_SIZES ) ) && ( 0 != ftFace->num_fixed_sizes );
1492 const bool hasColorTables = ( 0 != ( ftFace->face_flags & FT_FACE_FLAG_COLOR ) );
1494 DALI_LOG_INFO( gLogFilter, Debug::General, " isScalable : [%s]\n", ( isScalable ? "true" : "false" ) );
1495 DALI_LOG_INFO( gLogFilter, Debug::General, " hasFixedSizedBitmaps : [%s]\n", ( hasFixedSizedBitmaps ? "true" : "false" ) );
1496 DALI_LOG_INFO( gLogFilter, Debug::General, " hasColorTables : [%s]\n", ( hasColorTables ? "true" : "false" ) );
1498 // Check to see if the font contains fixed sizes?
1499 if( !isScalable && hasFixedSizedBitmaps )
1501 PointSize26Dot6 actualPointSize = 0u;
1502 int fixedSizeIndex = 0;
1503 for( ; fixedSizeIndex < ftFace->num_fixed_sizes; ++fixedSizeIndex )
1505 const PointSize26Dot6 fixedSize = ftFace->available_sizes[fixedSizeIndex].size;
1506 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " size index : %d, size : %d\n", fixedSizeIndex, fixedSize );
1508 if( fixedSize >= requestedPointSize )
1510 actualPointSize = fixedSize;
1515 if( 0u == actualPointSize )
1517 // The requested point size is bigger than the bigest fixed size.
1518 fixedSizeIndex = ftFace->num_fixed_sizes - 1;
1519 actualPointSize = ftFace->available_sizes[fixedSizeIndex].size;
1522 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " size index : %d, actual size : %d\n", fixedSizeIndex, actualPointSize );
1524 // Tell Freetype to use this size
1525 error = FT_Select_Size( ftFace, fixedSizeIndex );
1526 if ( FT_Err_Ok != error )
1528 DALI_LOG_INFO( gLogFilter, Debug::General, "FreeType Select_Size error: %d\n", error );
1532 float fixedWidth = static_cast< float >( ftFace->available_sizes[ fixedSizeIndex ].width );
1533 float fixedHeight = static_cast< float >( ftFace->available_sizes[ fixedSizeIndex ].height );
1535 // Indicate that the font is a fixed sized bitmap
1536 FontMetrics metrics( fixedHeight, // The ascender in pixels.
1538 fixedHeight, // The height in pixels.
1542 mFontFaceCache.push_back( FontFaceCacheItem( ftFace, path, requestedPointSize, faceIndex, metrics, fixedWidth, fixedHeight, hasColorTables ) );
1543 id = mFontFaceCache.size();
1548 error = FT_Set_Char_Size( ftFace,
1554 if( FT_Err_Ok == error )
1557 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1559 FontMetrics metrics( static_cast< float >( ftMetrics.ascender ) * FROM_266,
1560 static_cast< float >( ftMetrics.descender ) * FROM_266,
1561 static_cast< float >( ftMetrics.height ) * FROM_266,
1562 static_cast< float >( ftFace->underline_position ) * FROM_266,
1563 static_cast< float >( ftFace->underline_thickness ) * FROM_266 );
1565 mFontFaceCache.push_back( FontFaceCacheItem( ftFace, path, requestedPointSize, faceIndex, metrics ) );
1566 id = mFontFaceCache.size();
1570 DALI_LOG_INFO( gLogFilter, Debug::General, " FreeType Set_Char_Size error: %d for pointSize %d\n", error, requestedPointSize );
1576 if( cacheDescription )
1578 CacheFontPath( ftFace, id, requestedPointSize, path );
1584 DALI_LOG_INFO( gLogFilter, Debug::General, " FreeType New_Face error: %d for [%s]\n", error, path.c_str() );
1587 DALI_LOG_INFO( gLogFilter, Debug::General, " font id : %d\n", id );
1588 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::CreateFont\n" );
1593 void FontClient::Plugin::ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap )
1595 if( srcBitmap.width*srcBitmap.rows > 0 )
1597 switch( srcBitmap.pixel_mode )
1599 case FT_PIXEL_MODE_GRAY:
1601 if( srcBitmap.pitch == static_cast<int>( srcBitmap.width ) )
1603 const unsigned int bufferSize = srcBitmap.width * srcBitmap.rows;
1604 data.buffer = new unsigned char[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
1605 data.width = srcBitmap.width;
1606 data.height = srcBitmap.rows;
1607 data.format = Pixel::L8;
1608 memcpy( data.buffer, srcBitmap.buffer, bufferSize );
1613 #ifdef FREETYPE_BITMAP_SUPPORT
1614 case FT_PIXEL_MODE_BGRA:
1616 if( srcBitmap.pitch == static_cast<int>( srcBitmap.width << 2u ) )
1618 // Set the input dimensions.
1619 const ImageDimensions inputDimensions( srcBitmap.width, srcBitmap.rows );
1621 // Set the output dimensions.
1622 // If the output dimension is not given, the input dimension is set
1623 // and won't be downscaling.
1624 data.width = ( data.width == 0 ) ? srcBitmap.width : data.width;
1625 data.height = ( data.height == 0 ) ? srcBitmap.rows : data.height;
1626 const ImageDimensions desiredDimensions( data.width, data.height );
1628 // Creates the output buffer
1629 const unsigned int bufferSize = data.width * data.height * 4u;
1630 data.buffer = new unsigned char[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
1632 if( inputDimensions == desiredDimensions )
1634 // There isn't downscaling.
1635 memcpy( data.buffer, srcBitmap.buffer, bufferSize );
1639 Dali::Internal::Platform::LanczosSample4BPP( srcBitmap.buffer,
1642 desiredDimensions );
1644 data.format = Pixel::BGRA8888;
1651 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::ConvertBitmap. FontClient Unable to create Bitmap of this PixelType\n" );
1658 bool FontClient::Plugin::FindFont( const FontPath& path,
1659 PointSize26Dot6 requestedPointSize,
1660 FaceIndex faceIndex,
1661 FontId& fontId ) const
1663 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFont\n" );
1664 DALI_LOG_INFO( gLogFilter, Debug::General, " path : [%s]\n", path.c_str() );
1665 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1666 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of fonts in the cache : %d\n", mFontFaceCache.size() );
1669 for( const auto& cacheItem : mFontFaceCache )
1672 if( cacheItem.mRequestedPointSize == requestedPointSize &&
1673 cacheItem.mFaceIndex == faceIndex &&
1674 cacheItem.mPath == path )
1677 DALI_LOG_INFO( gLogFilter, Debug::General, " font found, id : %d\n", fontId );
1678 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1684 DALI_LOG_INFO( gLogFilter, Debug::General, " font not found\n" );
1685 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1691 bool FontClient::Plugin::FindValidatedFont( const FontDescription& fontDescription,
1692 FontDescriptionId& validatedFontId )
1694 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindValidatedFont\n" );
1695 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1696 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1697 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1698 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1699 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1700 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of validated fonts in the cache : %d\n", mValidatedFontCache.size() );
1702 validatedFontId = 0u;
1704 for( const auto& item : mValidatedFontCache )
1706 if( !fontDescription.family.empty() &&
1707 ( fontDescription.family == item.fontDescription.family ) &&
1708 ( fontDescription.width == item.fontDescription.width ) &&
1709 ( fontDescription.weight == item.fontDescription.weight ) &&
1710 ( fontDescription.slant == item.fontDescription.slant ) )
1712 validatedFontId = item.index;
1714 DALI_LOG_INFO( gLogFilter, Debug::General, " validated font found, id : %d\n", validatedFontId );
1715 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindValidatedFont\n" );
1720 DALI_LOG_INFO( gLogFilter, Debug::General, " validated font not found\n" );
1721 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindValidatedFont\n" );
1725 bool FontClient::Plugin::FindFallbackFontList( const FontDescription& fontDescription,
1726 FontList*& fontList,
1727 CharacterSetList*& characterSetList )
1729 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFallbackFontList\n" );
1730 DALI_LOG_INFO( gLogFilter, Debug::General, " description; family : [%s]\n", fontDescription.family.c_str() );
1731 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " path : [%s]\n", fontDescription.path.c_str() );
1732 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " width : [%s]\n", FontWidth::Name[fontDescription.width] );
1733 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " weight : [%s]\n", FontWeight::Name[fontDescription.weight] );
1734 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " slant : [%s]\n\n", FontSlant::Name[fontDescription.slant] );
1735 DALI_LOG_INFO( gLogFilter, Debug::Verbose, " number of fallback font lists in the cache : %d\n", mFallbackCache.size() );
1739 for( const auto& item : mFallbackCache )
1741 if( !fontDescription.family.empty() &&
1742 ( fontDescription.family == item.fontDescription.family ) &&
1743 ( fontDescription.width == item.fontDescription.width ) &&
1744 ( fontDescription.weight == item.fontDescription.weight ) &&
1745 ( fontDescription.slant == item.fontDescription.slant ) )
1747 fontList = item.fallbackFonts;
1748 characterSetList = item.characterSets;
1750 DALI_LOG_INFO( gLogFilter, Debug::General, " fallback font list found.\n" );
1751 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFontList\n" );
1756 DALI_LOG_INFO( gLogFilter, Debug::General, " fallback font list not found.\n" );
1757 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFallbackFontList\n" );
1761 bool FontClient::Plugin::FindFont( FontDescriptionId validatedFontId,
1762 PointSize26Dot6 requestedPointSize,
1765 DALI_LOG_INFO( gLogFilter, Debug::General, "-->FontClient::Plugin::FindFont\n" );
1766 DALI_LOG_INFO( gLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId );
1767 DALI_LOG_INFO( gLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize );
1771 for( const auto& item : mFontDescriptionSizeCache )
1773 if( ( validatedFontId == item.validatedFontId ) &&
1774 ( requestedPointSize == item.requestedPointSize ) )
1776 fontId = item.fontId;
1778 DALI_LOG_INFO( gLogFilter, Debug::General, " font found, id : %d\n", fontId );
1779 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1784 DALI_LOG_INFO( gLogFilter, Debug::General, " font not found.\n" );
1785 DALI_LOG_INFO( gLogFilter, Debug::General, "<--FontClient::Plugin::FindFont\n" );
1789 bool FontClient::Plugin::IsScalable( const FontPath& path )
1791 bool isScalable = false;
1794 int error = FT_New_Face( mFreeTypeLibrary,
1798 if( FT_Err_Ok != error )
1800 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: %s\n", path.c_str() );
1804 isScalable = ftFace->face_flags & FT_FACE_FLAG_SCALABLE;
1810 bool FontClient::Plugin::IsScalable( const FontDescription& fontDescription )
1812 // Create a font pattern.
1813 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1815 FcResult result = FcResultMatch;
1817 // match the pattern
1818 FcPattern* match = FcFontMatch( nullptr /* use default configure */, fontFamilyPattern, &result );
1819 bool isScalable = false;
1823 // Get the path to the font file name.
1825 GetFcString( match, FC_FILE, path );
1826 isScalable = IsScalable( path );
1830 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str() );
1832 FcPatternDestroy( fontFamilyPattern );
1833 FcPatternDestroy( match );
1837 void FontClient::Plugin::GetFixedSizes( const FontPath& path, Vector< PointSize26Dot6 >& sizes )
1839 // Empty the caller container
1843 int error = FT_New_Face( mFreeTypeLibrary,
1847 if( FT_Err_Ok != error )
1849 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font path : [%s]\n", path.c_str() );
1852 // Fetch the number of fixed sizes available
1853 if ( ftFace->num_fixed_sizes && ftFace->available_sizes )
1855 for ( int i = 0; i < ftFace->num_fixed_sizes; ++i )
1857 sizes.PushBack( ftFace->available_sizes[ i ].size );
1862 void FontClient::Plugin::GetFixedSizes( const FontDescription& fontDescription,
1863 Vector< PointSize26Dot6 >& sizes )
1865 // Create a font pattern.
1866 FcPattern* fontFamilyPattern = CreateFontFamilyPattern( fontDescription );
1868 FcResult result = FcResultMatch;
1870 // match the pattern
1871 FcPattern* match = FcFontMatch( nullptr /* use default configure */, fontFamilyPattern, &result );
1875 // Get the path to the font file name.
1877 GetFcString( match, FC_FILE, path );
1878 GetFixedSizes( path, sizes );
1882 DALI_LOG_INFO( gLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str() );
1884 FcPatternDestroy( match );
1885 FcPatternDestroy( fontFamilyPattern );
1888 void FontClient::Plugin::CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path )
1890 FontDescription description;
1891 description.path = path;
1892 description.family = std::move( FontFamily( ftFace->family_name ) );
1893 description.weight = FontWeight::NONE;
1894 description.width = FontWidth::NONE;
1895 description.slant = FontSlant::NONE;
1897 // Note FreeType doesn't give too much info to build a proper font style.
1898 if( ftFace->style_flags & FT_STYLE_FLAG_ITALIC )
1900 description.slant = FontSlant::ITALIC;
1902 if( ftFace->style_flags & FT_STYLE_FLAG_BOLD )
1904 description.weight = FontWeight::BOLD;
1907 FontDescriptionId validatedFontId = 0u;
1908 if( !FindValidatedFont( description,
1911 // Set the index to the vector of paths to font file names.
1912 validatedFontId = mFontDescriptionCache.size();
1914 FcPattern* pattern = CreateFontFamilyPattern( description );
1916 FcResult result = FcResultMatch;
1917 FcPattern* match = FcFontMatch( nullptr, pattern, &result );
1919 FcCharSet* characterSet = nullptr;
1920 FcPatternGetCharSet( match, FC_CHARSET, 0u, &characterSet );
1922 FcPatternDestroy( pattern );
1924 mMatchedFcPatternCache.PushBack( match );
1926 mFontFaceCache[id-1u].mCharacterSet = characterSet;
1928 // Add the path to the cache.
1929 mFontDescriptionCache.push_back( description );
1930 mCharacterSetCache.PushBack( characterSet );
1932 // Cache the index and the font's description.
1933 mValidatedFontCache.push_back( std::move( FontDescriptionCacheItem( std::move( description ),
1934 validatedFontId) ) );
1936 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
1937 mFontDescriptionSizeCache.push_back( FontDescriptionSizeCacheItem( validatedFontId,
1943 FcCharSet* FontClient::Plugin::CreateCharacterSetFromDescription( const FontDescription& description )
1945 FcCharSet* characterSet = nullptr;
1947 FcPattern* pattern = CreateFontFamilyPattern( description );
1949 if( nullptr != pattern )
1951 FcResult result = FcResultMatch;
1952 FcPattern* match = FcFontMatch( nullptr, pattern, &result );
1954 FcPatternGetCharSet( match, FC_CHARSET, 0u, &characterSet );
1955 mMatchedFcPatternCache.PushBack( match );
1957 FcPatternDestroy( pattern );
1960 return characterSet;
1963 void FontClient::Plugin::DestroyMatchedPatterns()
1965 for (auto & object : mMatchedFcPatternCache)
1967 FcPatternDestroy(reinterpret_cast<FcPattern*>(object));
1969 mMatchedFcPatternCache.Clear();
1972 } // namespace Internal
1974 } // namespace TextAbstraction