2 * Copyright (c) 2022 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/plugin/font-client-plugin-impl.h>
22 #include <dali/devel-api/text-abstraction/font-list.h>
24 #include <dali/devel-api/adaptor-framework/image-loading.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/platform-abstraction.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/imaging/common/image-operations.h>
29 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
30 #include <dali/internal/text/text-abstraction/plugin/embedded-item.h>
31 #include <dali/internal/text/text-abstraction/plugin/font-client-utils.h>
32 #include <dali/internal/text/text-abstraction/plugin/font-face-cache-item.h>
33 #include <dali/public-api/common/dali-vector.h>
34 #include <dali/public-api/common/vector-wrapper.h>
37 #include <fontconfig/fontconfig.h>
41 #if defined(DEBUG_ENABLED)
43 // Note, to turn on trace and verbose logging, use "export LOG_FONT_CLIENT=3,true"
44 // Or re-define the following filter using Verbose,true instead of NoLogging,false,
45 // Or, add DALI_LOG_FILTER_ENABLE_TRACE(gFontClientLogFilter) in the code below.
47 Dali::Integration::Log::Filter* gFontClientLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_FONT_CLIENT");
49 #define FONT_LOG_DESCRIPTION(fontDescription, prefix) \
50 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, #prefix " description; family : [%s]\n", fontDescription.family.c_str()); \
51 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, \
55 " slant : [%s]\n\n", \
56 fontDescription.path.c_str(), \
57 FontWidth::Name[fontDescription.width], \
58 FontWeight::Name[fontDescription.weight], \
59 FontSlant::Name[fontDescription.slant])
61 #define FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor) \
62 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, \
64 " requestedPointSize : %d\n" \
65 " preferColor : %s\n", \
68 (preferColor ? "true" : "false"))
72 #define FONT_LOG_DESCRIPTION(fontDescription, prefix)
73 #define FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor)
80 * Conversion from Fractional26.6 to float
82 const float FROM_266 = 1.0f / 64.0f;
83 const float POINTS_PER_INCH = 72.f;
85 const std::string DEFAULT_FONT_FAMILY_NAME("Tizen");
87 const uint32_t ELLIPSIS_CHARACTER = 0x2026;
94 namespace Dali::TextAbstraction::Internal
97 * @brief Free the resources allocated by the FcCharSet objects.
99 * @param[in] characterSets The vector of character sets.
101 void DestroyCharacterSets(CharacterSetList& characterSets)
103 for(auto& item : characterSets)
107 FcCharSetDestroy(item);
113 * @brief Check if @p ftFace and @p requestedPointSize produces block that fit into atlas block
115 * @param[in/out] ftFace Face type object.
116 * @param[in] horizontalDpi The horizontal dpi.
117 * @param[in] verticalDpi The vertical dpi.
118 * @param[in] maxSizeFitInAtlas The maximum size of block to fit into atlas
119 * @param[in] requestedPointSize The requested point-size.
120 * @return whether the ftFace's block can fit into atlas
122 bool IsFitIntoAtlas(FT_Face& ftFace, int& error, const unsigned int& horizontalDpi, const unsigned int& verticalDpi, const Size& maxSizeFitInAtlas, const uint32_t& requestedPointSize)
126 error = FT_Set_Char_Size(ftFace,
132 if(error == FT_Err_Ok)
134 //Check width and height of block for requestedPointSize
135 //If the width or height is greater than the maximum-size then decrement by one unit of point-size.
136 if(static_cast<float>(ftFace->size->metrics.height) * FROM_266 <= maxSizeFitInAtlas.height && (static_cast<float>(ftFace->size->metrics.ascender) - static_cast<float>(ftFace->size->metrics.descender)) * FROM_266 <= maxSizeFitInAtlas.width)
146 * @brief Search on proper @p requestedPointSize that produces block that fit into atlas block considering on @p ftFace, @p horizontalDpi, and @p verticalDpi
148 * @param[in/out] ftFace Face type object.
149 * @param[in] horizontalDpi The horizontal dpi.
150 * @param[in] verticalDpi The vertical dpi.
151 * @param[in] maxSizeFitInAtlas The maximum size of block to fit into atlas
152 * @param[in/out] requestedPointSize The requested point-size.
153 * @return FreeType error code. 0 means success when requesting the nominal size (in points).
155 int SearchOnProperPointSize(FT_Face& ftFace, const unsigned int& horizontalDpi, const unsigned int& verticalDpi, const Size& maxSizeFitInAtlas, uint32_t& requestedPointSize)
157 //To improve performance of sequential search. This code is applying Exponential search then followed by Binary search.
158 const uint32_t& pointSizePerOneUnit = TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
160 int error; // FreeType error code.
162 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
163 if(FT_Err_Ok != error)
171 uint32_t exponentialDecrement = 1;
173 while(!canFitInAtlas && requestedPointSize > pointSizePerOneUnit * exponentialDecrement)
175 requestedPointSize -= (pointSizePerOneUnit * exponentialDecrement);
176 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
177 if(FT_Err_Ok != error)
182 exponentialDecrement *= 2;
186 uint32_t minPointSize;
187 uint32_t maxPointSize;
191 exponentialDecrement /= 2;
192 minPointSize = requestedPointSize;
193 maxPointSize = requestedPointSize + (pointSizePerOneUnit * exponentialDecrement);
198 maxPointSize = requestedPointSize;
201 while(minPointSize < maxPointSize)
203 requestedPointSize = ((maxPointSize / pointSizePerOneUnit - minPointSize / pointSizePerOneUnit) / 2) * pointSizePerOneUnit + minPointSize;
204 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
205 if(FT_Err_Ok != error)
212 if(minPointSize == requestedPointSize)
214 //Found targeted point-size
218 minPointSize = requestedPointSize;
222 maxPointSize = requestedPointSize;
230 FontClient::Plugin::FallbackCacheItem::FallbackCacheItem(FontDescription&& font, FontList* fallbackFonts, CharacterSetList* characterSets)
231 : fontDescription{std::move(font)},
232 fallbackFonts{fallbackFonts},
233 characterSets{characterSets}
237 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem(const FontDescription& fontDescription,
238 FontDescriptionId index)
239 : fontDescription{fontDescription},
244 FontClient::Plugin::FontDescriptionCacheItem::FontDescriptionCacheItem(FontDescription&& fontDescription,
245 FontDescriptionId index)
246 : fontDescription{std::move(fontDescription)},
251 FontClient::Plugin::FontDescriptionSizeCacheKey::FontDescriptionSizeCacheKey(FontDescriptionId fontDescriptionId,
252 PointSize26Dot6 requestedPointSize)
253 : fontDescriptionId(fontDescriptionId),
254 requestedPointSize(requestedPointSize)
258 FontClient::Plugin::Plugin(unsigned int horizontalDpi,
259 unsigned int verticalDpi)
260 : mFreeTypeLibrary(nullptr),
261 mDpiHorizontal(horizontalDpi),
262 mDpiVertical(verticalDpi),
263 mDefaultFontDescription(),
268 mValidatedFontCache(),
269 mFontDescriptionCache(),
270 mCharacterSetCache(),
271 mFontDescriptionSizeCache(),
272 mVectorFontCache(nullptr),
274 mEmbeddedItemCache(),
275 mDefaultFontDescriptionCached(false),
276 mIsAtlasLimitationEnabled(TextAbstraction::FontClient::DEFAULT_ATLAS_LIMITATION_ENABLED),
277 mCurrentMaximumBlockSizeFitInAtlas(TextAbstraction::FontClient::MAX_SIZE_FIT_IN_ATLAS)
280 int error = FT_Init_FreeType(&mFreeTypeLibrary);
281 if(FT_Err_Ok != error)
283 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Init error: %d\n", error);
286 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
287 mVectorFontCache = new VectorFontCache(mFreeTypeLibrary);
291 FontClient::Plugin::~Plugin()
293 ClearFallbackCache(mFallbackCache);
295 // Free the resources allocated by the FcCharSet objects.
296 DestroyCharacterSets(mDefaultFontCharacterSets);
297 DestroyCharacterSets(mCharacterSetCache);
298 ClearCharacterSetFromFontFaceCache();
300 // Clear FontFaceCache here. Due to we sould deallocate FT_Faces before done freetype library
301 mFontFaceCache.clear();
303 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
304 delete mVectorFontCache;
306 FT_Done_FreeType(mFreeTypeLibrary);
309 void FontClient::Plugin::ClearCache()
311 mDefaultFontDescription = FontDescription();
313 mSystemFonts.clear();
314 mDefaultFonts.clear();
316 DestroyCharacterSets(mDefaultFontCharacterSets);
317 mDefaultFontCharacterSets.Clear();
319 ClearFallbackCache(mFallbackCache);
320 mFallbackCache.clear();
322 mFontIdCache.Clear();
324 ClearCharacterSetFromFontFaceCache();
325 mFontFaceCache.clear();
327 mValidatedFontCache.clear();
328 mFontDescriptionCache.clear();
330 DestroyCharacterSets(mCharacterSetCache);
331 mCharacterSetCache.Clear();
333 mFontDescriptionSizeCache.clear();
334 mFontDescriptionSizeCache.rehash(0); // Note : unordered_map.clear() didn't deallocate memory
336 mEllipsisCache.Clear();
337 mPixelBufferCache.clear();
338 mEmbeddedItemCache.Clear();
339 mBitmapFontCache.clear();
341 mDefaultFontDescriptionCached = false;
344 void FontClient::Plugin::SetDpi(unsigned int horizontalDpi,
345 unsigned int verticalDpi)
347 mDpiHorizontal = horizontalDpi;
348 mDpiVertical = verticalDpi;
351 void FontClient::Plugin::ResetSystemDefaults()
353 mDefaultFontDescriptionCached = false;
356 void FontClient::Plugin::SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList)
358 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
359 FONT_LOG_DESCRIPTION(fontDescription, "");
362 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a pattern that needs to be destroyed by calling FcPatternDestroy.
364 FcResult result = FcResultMatch;
366 // Match the pattern.
367 FcFontSet* fontSet = FcFontSort(nullptr /* use default configure */,
369 false /* don't trim */,
371 &result); // FcFontSort creates a font set that needs to be destroyed by calling FcFontSetDestroy.
373 if(nullptr != fontSet)
375 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of fonts found : [%d]\n", fontSet->nfont);
376 // Reserve some space to avoid reallocations.
377 fontList.reserve(fontSet->nfont);
379 for(int i = 0u; i < fontSet->nfont; ++i)
381 FcPattern* fontPattern = fontSet->fonts[i];
385 // Skip fonts with no path
386 if(GetFcString(fontPattern, FC_FILE, path))
388 // Retrieve the character set. Need to call FcCharSetDestroy to free the resources.
389 FcCharSet* characterSet = nullptr;
390 FcPatternGetCharSet(fontPattern, FC_CHARSET, 0u, &characterSet);
392 // Increase the reference counter of the character set.
393 characterSetList.PushBack(FcCharSetCopy(characterSet));
395 fontList.push_back(FontDescription());
396 FontDescription& newFontDescription = fontList.back();
398 newFontDescription.path = std::move(path);
403 GetFcString(fontPattern, FC_FAMILY, newFontDescription.family);
404 GetFcInt(fontPattern, FC_WIDTH, width);
405 GetFcInt(fontPattern, FC_WEIGHT, weight);
406 GetFcInt(fontPattern, FC_SLANT, slant);
407 newFontDescription.width = IntToWidthType(width);
408 newFontDescription.weight = IntToWeightType(weight);
409 newFontDescription.slant = IntToSlantType(slant);
411 FONT_LOG_DESCRIPTION(newFontDescription, "");
415 // Destroys the font set created by FcFontSort.
416 FcFontSetDestroy(fontSet);
420 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " No fonts found.\n");
423 // Destroys the pattern created by FcPatternCreate in CreateFontFamilyPattern.
424 FcPatternDestroy(fontFamilyPattern);
427 void FontClient::Plugin::GetDefaultFonts(FontList& defaultFonts)
429 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
431 if(mDefaultFonts.empty())
433 FontDescription fontDescription;
434 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
435 fontDescription.width = DefaultFontWidth();
436 fontDescription.weight = DefaultFontWeight();
437 fontDescription.slant = DefaultFontSlant();
438 SetFontList(fontDescription, mDefaultFonts, mDefaultFontCharacterSets);
441 defaultFonts = mDefaultFonts;
443 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of default fonts : [%d]\n", mDefaultFonts.size());
446 void FontClient::Plugin::GetDefaultPlatformFontDescription(FontDescription& fontDescription)
448 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
450 if(!mDefaultFontDescriptionCached)
452 // Clear any font config stored info in the caches.
454 // Decrease the reference counter and eventually free the resources allocated by FcCharSet objects.
455 DestroyCharacterSets(mDefaultFontCharacterSets);
456 DestroyCharacterSets(mCharacterSetCache);
457 mDefaultFontCharacterSets.Clear();
458 mCharacterSetCache.Clear();
460 for(auto& item : mFallbackCache)
462 // Decrease the reference counter and eventually free the resources allocated by FcCharSet objects.
463 DestroyCharacterSets(*item.characterSets);
465 delete item.characterSets;
466 item.characterSets = nullptr;
469 // Set the character set pointer as null. Will be created again the next time IsCharacterSupportedByFont()
470 ClearCharacterSetFromFontFaceCache();
472 // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
473 FcInitReinitialize();
475 FcPattern* matchPattern = FcPatternCreate(); // Creates a pattern that needs to be destroyed by calling FcPatternDestroy.
477 if(nullptr != matchPattern)
479 FcConfigSubstitute(nullptr, matchPattern, FcMatchPattern);
480 FcDefaultSubstitute(matchPattern);
482 FcCharSet* characterSet = nullptr;
483 bool matched = MatchFontDescriptionToPattern(matchPattern, mDefaultFontDescription, &characterSet);
485 // Caching the default font description
488 // Copy default font description info.
489 // Due to the type changed, we need to make some temperal font description.
490 FontDescription tempFontDescription = mDefaultFontDescription;
492 // Add the path to the cache.
493 tempFontDescription.type = FontDescription::FACE_FONT;
494 mFontDescriptionCache.push_back(tempFontDescription);
496 // Set the index to the vector of paths to font file names.
497 const FontDescriptionId fontDescriptionId = mFontDescriptionCache.size();
499 FONT_LOG_DESCRIPTION(tempFontDescription, "default platform font");
500 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " default font fontDescriptionId : %d\n", fontDescriptionId);
502 // Cache the index and the matched font's description.
503 FontDescriptionCacheItem item(tempFontDescription,
506 mValidatedFontCache.push_back(std::move(item));
510 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " default font validation failed for font [%s]\n", mDefaultFontDescription.family.c_str());
513 // Decrease the reference counter of the character set as it's not stored.
514 // Note. the cached default font description will increase reference counter by
515 // mFontDescriptionCache. So we can decrease reference counter here.
516 FcCharSetDestroy(characterSet);
518 // Destroys the pattern created.
519 FcPatternDestroy(matchPattern);
522 // Create again the character sets as they are not valid after FcInitReinitialize()
524 for(const auto& description : mDefaultFonts)
526 mDefaultFontCharacterSets.PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
529 for(const auto& description : mFontDescriptionCache)
531 mCharacterSetCache.PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
534 for(auto& item : mFallbackCache)
536 if(nullptr != item.fallbackFonts)
538 if(nullptr == item.characterSets)
540 item.characterSets = new CharacterSetList;
543 for(const auto& description : *(item.fallbackFonts))
545 item.characterSets->PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
550 mDefaultFontDescriptionCached = true;
553 fontDescription.path = mDefaultFontDescription.path;
554 fontDescription.family = mDefaultFontDescription.family;
555 fontDescription.width = mDefaultFontDescription.width;
556 fontDescription.weight = mDefaultFontDescription.weight;
557 fontDescription.slant = mDefaultFontDescription.slant;
559 FONT_LOG_DESCRIPTION(fontDescription, "");
562 void FontClient::Plugin::GetSystemFonts(FontList& systemFonts)
564 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
566 if(mSystemFonts.empty())
571 systemFonts = mSystemFonts;
572 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of system fonts : [%d]\n", mSystemFonts.size());
575 void FontClient::Plugin::GetDescription(FontId id,
576 FontDescription& fontDescription) const
578 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
579 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
580 const FontId index = id - 1u;
582 if((id > 0u) && (index < mFontIdCache.Count()))
584 const FontIdCacheItem& fontIdCacheItem = mFontIdCache[index];
585 switch(fontIdCacheItem.type)
587 case FontDescription::FACE_FONT:
589 for(const auto& item : mFontDescriptionSizeCache)
591 if(item.second == fontIdCacheItem.index)
593 fontDescription = *(mFontDescriptionCache.begin() + item.first.fontDescriptionId - 1u);
595 FONT_LOG_DESCRIPTION(fontDescription, "");
601 case FontDescription::BITMAP_FONT:
603 fontDescription.type = FontDescription::BITMAP_FONT;
604 fontDescription.family = mBitmapFontCache[fontIdCacheItem.index].font.name;
609 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
610 fontDescription.type = FontDescription::INVALID;
611 fontDescription.family.clear();
616 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " No description found for the font ID %d\n", id);
619 PointSize26Dot6 FontClient::Plugin::GetPointSize(FontId id)
621 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
622 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
624 PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
625 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(id);
626 if(fontCacheItem != nullptr)
628 pointSize = fontCacheItem->GetPointSize();
630 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " point size : %d\n", pointSize);
635 bool FontClient::Plugin::IsCharacterSupportedByFont(FontId fontId, Character character)
637 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
638 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
639 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
641 bool isSupported = false;
642 auto fontCacheItem = const_cast<FontCacheItemInterface*>(GetCachedFontItem(fontId));
643 if(fontCacheItem != nullptr)
645 isSupported = fontCacheItem->IsCharacterSupported(character); // May cache
648 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " is supported : %s\n", (isSupported ? "true" : "false"));
652 const FontCacheItemInterface* FontClient::Plugin::GetCachedFontItem(FontId id) const
654 const FontCacheIndex index = id - 1u;
655 if((id > 0u) && (index < mFontIdCache.Count()))
657 const FontIdCacheItem& fontIdCacheItem = mFontIdCache[index];
658 switch(fontIdCacheItem.type)
660 case FontDescription::FACE_FONT:
662 return &mFontFaceCache[fontIdCacheItem.index];
664 case FontDescription::BITMAP_FONT:
666 return &mBitmapFontCache[fontIdCacheItem.index];
670 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
677 FontId FontClient::Plugin::FindFontForCharacter(const FontList& fontList,
678 const CharacterSetList& characterSetList,
680 PointSize26Dot6 requestedPointSize,
683 DALI_ASSERT_DEBUG((fontList.size() == characterSetList.Count()) && "FontClient::Plugin::FindFontForCharacter. Different number of fonts and character sets.");
684 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
685 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
686 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
687 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferColor : %s\n", (preferColor ? "true" : "false"));
690 bool foundColor = false;
692 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of fonts : %d\n", fontList.size());
694 // Traverse the list of fonts.
695 // Check for each font if supports the character.
696 for(unsigned int index = 0u, numberOfFonts = fontList.size(); index < numberOfFonts; ++index)
698 const FontDescription& description = fontList[index];
699 const FcCharSet* const characterSet = characterSetList[index];
701 FONT_LOG_DESCRIPTION(description, "");
703 bool foundInRanges = false;
704 if(nullptr != characterSet)
706 foundInRanges = FcCharSetHasChar(characterSet, character);
711 fontId = GetFontId(description, requestedPointSize, 0u);
713 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " font id : %d\n", fontId);
718 (fontId - 1u < mFontIdCache.Count()))
720 const FontFaceCacheItem& item = mFontFaceCache[mFontIdCache[fontId - 1u].index];
722 foundColor = item.mHasColorTables;
725 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " foundColor : %s\n", (foundColor ? "true" : "false"));
728 // Keep going unless we prefer a different (color) font.
729 if(!preferColor || foundColor)
736 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
740 FontId FontClient::Plugin::FindDefaultFont(Character charcode,
741 PointSize26Dot6 requestedPointSize,
744 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
745 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
749 // Create the list of default fonts if it has not been created.
750 if(mDefaultFonts.empty())
752 FontDescription fontDescription;
753 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
754 fontDescription.width = DefaultFontWidth();
755 fontDescription.weight = DefaultFontWeight();
756 fontDescription.slant = DefaultFontSlant();
758 SetFontList(fontDescription, mDefaultFonts, mDefaultFontCharacterSets);
760 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of default fonts : %d\n", mDefaultFonts.size());
762 // Traverse the list of default fonts.
763 // Check for each default font if supports the character.
764 fontId = FindFontForCharacter(mDefaultFonts, mDefaultFontCharacterSets, charcode, requestedPointSize, preferColor);
766 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
770 FontId FontClient::Plugin::FindFallbackFont(Character charcode,
771 const FontDescription& preferredFontDescription,
772 PointSize26Dot6 requestedPointSize,
775 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
776 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
778 // The font id to be returned.
781 FontDescription fontDescription;
783 // Fill the font description with the preferred font description and complete with the defaults.
784 fontDescription.family = preferredFontDescription.family.empty() ? DEFAULT_FONT_FAMILY_NAME : preferredFontDescription.family;
785 fontDescription.weight = ((FontWeight::NONE == preferredFontDescription.weight) ? DefaultFontWeight() : preferredFontDescription.weight);
786 fontDescription.width = ((FontWidth::NONE == preferredFontDescription.width) ? DefaultFontWidth() : preferredFontDescription.width);
787 fontDescription.slant = ((FontSlant::NONE == preferredFontDescription.slant) ? DefaultFontSlant() : preferredFontDescription.slant);
789 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferredFontDescription --> fontDescription\n");
790 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " [%s] --> [%s]\n", preferredFontDescription.family.c_str(), fontDescription.family.c_str());
791 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWeight::Name[preferredFontDescription.weight], FontWeight::Name[fontDescription.weight]);
792 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWidth::Name[preferredFontDescription.width], FontWidth::Name[fontDescription.width]);
793 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontSlant::Name[preferredFontDescription.slant], FontSlant::Name[fontDescription.slant]);
795 // Check first if the font's description has been queried before.
796 FontList* fontList = nullptr;
797 CharacterSetList* characterSetList = nullptr;
799 if(!FindFallbackFontList(fontDescription, fontList, characterSetList))
801 fontList = new FontList;
802 characterSetList = new CharacterSetList;
804 SetFontList(fontDescription, *fontList, *characterSetList);
806 FontDescription appleColorEmoji;
807 appleColorEmoji.family = "Apple Color Emoji";
808 appleColorEmoji.width = fontDescription.width;
809 appleColorEmoji.weight = fontDescription.weight;
810 appleColorEmoji.slant = fontDescription.slant;
811 FontList emojiFontList;
812 CharacterSetList emojiCharSetList;
813 SetFontList(appleColorEmoji, emojiFontList, emojiCharSetList);
815 std::move(fontList->begin(), fontList->end(), std::back_inserter(emojiFontList));
816 emojiCharSetList.Insert(emojiCharSetList.End(), characterSetList->Begin(), characterSetList->End());
817 *fontList = std::move(emojiFontList);
818 *characterSetList = std::move(emojiCharSetList);
821 // Add the font-list to the cache.
822 mFallbackCache.push_back(std::move(FallbackCacheItem(std::move(fontDescription), fontList, characterSetList)));
825 if(fontList && characterSetList)
827 fontId = FindFontForCharacter(*fontList, *characterSetList, charcode, requestedPointSize, preferColor);
830 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
834 FontId FontClient::Plugin::GetFontId(const FontPath& path,
835 PointSize26Dot6 requestedPointSize,
837 bool cacheDescription)
839 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
840 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
841 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
845 if(nullptr != mFreeTypeLibrary)
848 if(FindFont(path, requestedPointSize, faceIndex, foundId))
854 id = CreateFont(path, requestedPointSize, faceIndex, cacheDescription);
858 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
862 FontId FontClient::Plugin::GetFontId(const FontDescription& fontDescription,
863 PointSize26Dot6 requestedPointSize,
866 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
867 FONT_LOG_DESCRIPTION(fontDescription, "");
869 // Special case when font Description don't have family information.
870 // In this case, we just use default description family and path.
871 const FontDescription& realFontDescription = fontDescription.family.empty() ? FontDescription(mDefaultFontDescription.path,
872 mDefaultFontDescription.family,
873 fontDescription.width,
874 fontDescription.weight,
875 fontDescription.slant,
876 fontDescription.type)
879 FONT_LOG_DESCRIPTION(realFontDescription, "");
880 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
882 // This method uses three vectors which caches:
883 // * The bitmap font cache
884 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
885 // * The path to font file names.
886 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
888 // 1) Checks if the font description matches with a previously loaded bitmap font.
889 // Returns if a font is found.
890 // 2) Checks in the cache if the font's description has been validated before.
891 // If it was it gets an index to the vector with paths to font file names. Otherwise,
892 // retrieves using font config a path to a font file name which matches with the
893 // font's description. The path is stored in the cache.
895 // 3) Checks in the cache if the pair 'font point size, index to the vector with paths to
896 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
897 // the GetFontId() method with the path to the font file name and the point size to
900 // The font id to be returned.
903 // Check first if the font description matches with a previously loaded bitmap font.
904 if(FindBitmapFont(realFontDescription.family, fontId))
909 // Check if the font's description have been validated before.
910 FontDescriptionId fontDescriptionId = 0u;
912 if(!FindValidatedFont(realFontDescription,
915 // Use font config to validate the font's description.
916 ValidateFont(realFontDescription,
920 FontCacheIndex fontCacheIndex = 0u;
921 // Check if exists a pair 'fontDescriptionId, requestedPointSize' in the cache.
922 if(!FindFont(fontDescriptionId, requestedPointSize, fontCacheIndex))
924 // Retrieve the font file name path.
925 const FontDescription& description = *(mFontDescriptionCache.begin() + fontDescriptionId - 1u);
927 // Retrieve the font id. Do not cache the description as it has been already cached.
928 fontId = GetFontId(description.path,
933 fontCacheIndex = mFontIdCache[fontId - 1u].index;
934 mFontFaceCache[fontCacheIndex].mCharacterSet = FcCharSetCopy(mCharacterSetCache[fontDescriptionId - 1u]);
936 // Cache the pair 'fontDescriptionId, requestedPointSize' to improve the following queries.
937 mFontDescriptionSizeCache.emplace(FontDescriptionSizeCacheKey(fontDescriptionId, requestedPointSize), fontCacheIndex);
941 fontId = mFontFaceCache[fontCacheIndex].mFontId + 1u;
944 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
948 FontId FontClient::Plugin::GetFontId(const BitmapFont& bitmapFont)
950 for(const auto& item : mBitmapFontCache)
952 if(bitmapFont.name == item.font.name)
958 BitmapFontCacheItem bitmapFontCacheItem(bitmapFont, mFontIdCache.Count());
960 FontIdCacheItem fontIdCacheItem;
961 fontIdCacheItem.type = FontDescription::BITMAP_FONT;
962 fontIdCacheItem.index = mBitmapFontCache.size();
964 mBitmapFontCache.push_back(std::move(bitmapFontCacheItem));
965 mFontIdCache.PushBack(fontIdCacheItem);
967 return bitmapFontCacheItem.id + 1u;
970 void FontClient::Plugin::ValidateFont(const FontDescription& fontDescription,
971 FontDescriptionId& fontDescriptionId)
973 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
974 FONT_LOG_DESCRIPTION(fontDescription, "");
976 // Create a font pattern.
977 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription);
979 FontDescription description;
981 FcCharSet* characterSet = nullptr;
982 bool matched = MatchFontDescriptionToPattern(fontFamilyPattern, description, &characterSet);
983 FcPatternDestroy(fontFamilyPattern);
985 if(matched && (nullptr != characterSet))
987 // Add the path to the cache.
988 description.type = FontDescription::FACE_FONT;
989 mFontDescriptionCache.push_back(description);
991 // Set the index to the vector of paths to font file names.
992 fontDescriptionId = mFontDescriptionCache.size();
994 FONT_LOG_DESCRIPTION(description, "matched");
995 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fontDescriptionId : %d\n", fontDescriptionId);
997 // The reference counter of the character set has already been increased in MatchFontDescriptionToPattern.
998 mCharacterSetCache.PushBack(characterSet);
1000 // Cache the index and the matched font's description.
1001 FontDescriptionCacheItem item(description,
1004 mValidatedFontCache.push_back(std::move(item));
1006 if((fontDescription.family != description.family) ||
1007 (fontDescription.width != description.width) ||
1008 (fontDescription.weight != description.weight) ||
1009 (fontDescription.slant != description.slant))
1011 // Cache the given font's description if it's different than the matched.
1012 FontDescriptionCacheItem item(fontDescription,
1015 mValidatedFontCache.push_back(std::move(item));
1020 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font validation failed for font [%s]\n", fontDescription.family.c_str());
1024 void FontClient::Plugin::GetFontMetrics(FontId fontId,
1025 FontMetrics& metrics)
1027 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1028 if(fontCacheItem != nullptr)
1030 fontCacheItem->GetFontMetrics(metrics, mDpiVertical);
1034 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
1037 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1038 if(fontCacheItem != nullptr)
1040 return fontCacheItem->GetGlyphIndex(charcode);
1046 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
1048 Character variantSelector)
1050 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1051 if(fontCacheItem != nullptr)
1053 return fontCacheItem->GetGlyphIndex(charcode, variantSelector);
1059 bool FontClient::Plugin::GetGlyphMetrics(GlyphInfo* array,
1064 if(VECTOR_GLYPH == type)
1066 return GetVectorMetrics(array, size, horizontal);
1069 return GetBitmapMetrics(array, size, horizontal);
1072 bool FontClient::Plugin::GetBitmapMetrics(GlyphInfo* array,
1076 bool success(false);
1078 for(unsigned int i = 0; i < size; ++i)
1080 GlyphInfo& glyph = array[i];
1082 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(glyph.fontId);
1083 if(fontCacheItem != nullptr)
1085 success = fontCacheItem->GetGlyphMetrics(glyph, mDpiVertical, horizontal);
1087 // Check if it's an embedded image.
1088 else if((0u == glyph.fontId) && (0u != glyph.index) && (glyph.index <= mEmbeddedItemCache.Count()))
1090 mEmbeddedItemCache[glyph.index - 1u].GetGlyphMetrics(glyph);
1098 bool FontClient::Plugin::GetVectorMetrics(GlyphInfo* array,
1102 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1105 for(unsigned int i = 0u; i < size; ++i)
1107 FontId fontId = array[i].fontId;
1110 (fontId - 1u) < mFontIdCache.Count())
1112 FontFaceCacheItem& font = mFontFaceCache[mFontIdCache[fontId - 1u].index];
1114 if(!font.mVectorFontId)
1116 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
1119 mVectorFontCache->GetGlyphMetrics(font.mVectorFontId, array[i]);
1121 // Vector metrics are in EMs, convert to pixels
1122 const float scale = (static_cast<float>(font.mRequestedPointSize) * FROM_266) * static_cast<float>(mDpiVertical) / POINTS_PER_INCH;
1123 array[i].width *= scale;
1124 array[i].height *= scale;
1125 array[i].xBearing *= scale;
1126 array[i].yBearing *= scale;
1127 array[i].advance *= scale;
1141 void FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth)
1143 data.isColorBitmap = false;
1144 data.isColorEmoji = false;
1145 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1146 if(fontCacheItem != nullptr)
1148 fontCacheItem->CreateBitmap(glyphIndex, data, outlineWidth, isItalicRequired, isBoldRequired);
1150 else if((0u != glyphIndex) && (glyphIndex <= mEmbeddedItemCache.Count()))
1152 // It's an embedded item.
1153 mEmbeddedItemCache[glyphIndex - 1u].CreateBitmap(mPixelBufferCache, data);
1157 PixelData FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth)
1159 TextAbstraction::FontClient::GlyphBufferData data;
1161 CreateBitmap(fontId, glyphIndex, false, false, data, outlineWidth);
1163 return PixelData::New(data.buffer,
1164 data.width * data.height * Pixel::GetBytesPerPixel(data.format),
1171 void FontClient::Plugin::CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight)
1176 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1178 (fontId - 1u < mFontIdCache.Count()))
1180 const FontCacheIndex fontFaceId = mFontIdCache[fontId - 1u].index;
1181 FontFaceCacheItem& font = mFontFaceCache[fontFaceId];
1183 if(!font.mVectorFontId)
1185 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
1188 mVectorFontCache->GetVectorBlob(font.mVectorFontId, fontFaceId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight);
1193 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph(PointSize26Dot6 requestedPointSize)
1195 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1196 DALI_LOG_INFO(gFontClientLogFilter, 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(item.requestedPointSize == requestedPointSize)
1203 // Use the glyph in the cache.
1204 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index);
1205 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font %d.\n", item.glyph.fontId);
1210 // No glyph has been found. Create one.
1211 mEllipsisCache.PushBack(EllipsisItem());
1212 EllipsisItem& item = *(mEllipsisCache.End() - 1u);
1214 item.requestedPointSize = requestedPointSize;
1216 // Find a font for the ellipsis glyph.
1217 item.glyph.fontId = FindDefaultFont(ELLIPSIS_CHARACTER,
1221 // Set the character index to access the glyph inside the font.
1222 item.glyph.index = FT_Get_Char_Index(mFontFaceCache[mFontIdCache[item.glyph.fontId - 1u].index].mFreeTypeFace,
1223 ELLIPSIS_CHARACTER);
1225 GetBitmapMetrics(&item.glyph, 1u, true);
1227 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index);
1228 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font %d.\n", item.glyph.fontId);
1232 bool FontClient::Plugin::IsColorGlyph(FontId fontId, GlyphIndex glyphIndex)
1234 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1235 return fontCacheItem && fontCacheItem->IsColorGlyph(glyphIndex);
1238 FT_FaceRec_* FontClient::Plugin::GetFreetypeFace(FontId fontId)
1240 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1241 if(fontCacheItem != nullptr)
1243 return fontCacheItem->GetTypeface();
1248 FontDescription::Type FontClient::Plugin::GetFontType(FontId fontId)
1250 const FontId index = fontId - 1u;
1252 (index < mFontIdCache.Count()))
1254 return mFontIdCache[index].type;
1256 return FontDescription::INVALID;
1259 bool FontClient::Plugin::AddCustomFontDirectory(const FontPath& path)
1261 // nullptr as first parameter means the current configuration is used.
1262 return FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>(path.c_str()));
1265 GlyphIndex FontClient::Plugin::CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat)
1267 EmbeddedItem embeddedItem;
1269 embeddedItem.pixelBufferId = 0u;
1270 embeddedItem.width = description.width;
1271 embeddedItem.height = description.height;
1273 pixelFormat = Pixel::A8;
1275 if(!description.url.empty())
1277 // Check if the url is in the cache.
1278 PixelBufferId index = 0u;
1280 for(const auto& cacheItem : mPixelBufferCache)
1283 if(cacheItem.url == description.url)
1285 // The url is in the pixel buffer cache.
1286 // Set the index +1 to the vector.
1287 embeddedItem.pixelBufferId = index;
1292 Devel::PixelBuffer pixelBuffer;
1293 if(0u == embeddedItem.pixelBufferId)
1295 // The pixel buffer is not in the cache. Create one and cache it.
1297 // Load the image from the url.
1298 pixelBuffer = LoadImageFromFile(description.url);
1300 // Create the cache item.
1301 PixelBufferCacheItem pixelBufferCacheItem;
1302 pixelBufferCacheItem.pixelBuffer = pixelBuffer;
1303 pixelBufferCacheItem.url = description.url;
1305 // Store the cache item in the cache.
1306 mPixelBufferCache.push_back(std::move(pixelBufferCacheItem));
1308 // Set the pixel buffer id to the embedded item.
1309 embeddedItem.pixelBufferId = mPixelBufferCache.size();
1313 // Retrieve the pixel buffer from the cache to set the pixel format.
1314 pixelBuffer = mPixelBufferCache[embeddedItem.pixelBufferId - 1u].pixelBuffer;
1319 // Set the size of the embedded item if it has not been set.
1320 if(0u == embeddedItem.width)
1322 embeddedItem.width = static_cast<unsigned int>(pixelBuffer.GetWidth());
1325 if(0u == embeddedItem.height)
1327 embeddedItem.height = static_cast<unsigned int>(pixelBuffer.GetHeight());
1330 // Set the pixel format.
1331 pixelFormat = pixelBuffer.GetPixelFormat();
1335 // Find if the same embeddedItem has already been created.
1336 unsigned int index = 0u;
1337 for(const auto& item : mEmbeddedItemCache)
1340 if((item.pixelBufferId == embeddedItem.pixelBufferId) &&
1341 (item.width == embeddedItem.width) &&
1342 (item.height == embeddedItem.height))
1348 // Cache the embedded item.
1349 mEmbeddedItemCache.PushBack(embeddedItem);
1351 return mEmbeddedItemCache.Count();
1354 void FontClient::Plugin::EnableAtlasLimitation(bool enabled)
1356 mIsAtlasLimitationEnabled = enabled;
1359 bool FontClient::Plugin::IsAtlasLimitationEnabled() const
1361 return mIsAtlasLimitationEnabled;
1364 Size FontClient::Plugin::GetMaximumTextAtlasSize() const
1366 return TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
1369 Size FontClient::Plugin::GetDefaultTextAtlasSize() const
1371 return TextAbstraction::FontClient::DEFAULT_TEXT_ATLAS_SIZE;
1374 Size FontClient::Plugin::GetCurrentMaximumBlockSizeFitInAtlas() const
1376 return mCurrentMaximumBlockSizeFitInAtlas;
1379 bool FontClient::Plugin::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
1381 bool isChanged = false;
1382 const Size& maxTextAtlasSize = TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
1383 const uint16_t& padding = TextAbstraction::FontClient::PADDING_TEXT_ATLAS_BLOCK;
1385 if(currentMaximumBlockSizeFitInAtlas.width <= maxTextAtlasSize.width - padding && currentMaximumBlockSizeFitInAtlas.height <= maxTextAtlasSize.height - padding)
1387 mCurrentMaximumBlockSizeFitInAtlas = currentMaximumBlockSizeFitInAtlas;
1394 uint32_t FontClient::Plugin::GetNumberOfPointsPerOneUnitOfPointSize() const
1396 return TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
1400 void FontClient::Plugin::InitSystemFonts()
1402 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1404 FcFontSet* fontSet = GetFcFontSet(); // Creates a FcFontSet that needs to be destroyed by calling FcFontSetDestroy.
1408 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of system fonts : %d\n", fontSet->nfont);
1410 // Reserve some space to avoid reallocations.
1411 mSystemFonts.reserve(fontSet->nfont);
1413 for(int i = 0u; i < fontSet->nfont; ++i)
1415 FcPattern* fontPattern = fontSet->fonts[i];
1419 // Skip fonts with no path
1420 if(GetFcString(fontPattern, FC_FILE, path))
1422 mSystemFonts.push_back(FontDescription());
1423 FontDescription& fontDescription = mSystemFonts.back();
1425 fontDescription.path = std::move(path);
1430 GetFcString(fontPattern, FC_FAMILY, fontDescription.family);
1431 GetFcInt(fontPattern, FC_WIDTH, width);
1432 GetFcInt(fontPattern, FC_WEIGHT, weight);
1433 GetFcInt(fontPattern, FC_SLANT, slant);
1434 fontDescription.width = IntToWidthType(width);
1435 fontDescription.weight = IntToWeightType(weight);
1436 fontDescription.slant = IntToSlantType(slant);
1438 FONT_LOG_DESCRIPTION(fontDescription, "");
1442 // Destroys the font set created.
1443 FcFontSetDestroy(fontSet);
1447 bool FontClient::Plugin::MatchFontDescriptionToPattern(FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, FcCharSet** characterSet)
1449 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1451 FcResult result = FcResultMatch;
1452 FcPattern* match = FcFontMatch(nullptr /* use default configure */, pattern, &result); // Creates a new font pattern that needs to be destroyed by calling FcPatternDestroy.
1454 const bool matched = nullptr != match;
1455 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " pattern matched : %s\n", (matched ? "true" : "false"));
1462 GetFcString(match, FC_FILE, fontDescription.path);
1463 GetFcString(match, FC_FAMILY, fontDescription.family);
1464 GetFcInt(match, FC_WIDTH, width);
1465 GetFcInt(match, FC_WEIGHT, weight);
1466 GetFcInt(match, FC_SLANT, slant);
1467 fontDescription.width = IntToWidthType(width);
1468 fontDescription.weight = IntToWeightType(weight);
1469 fontDescription.slant = IntToSlantType(slant);
1471 // Retrieve the character set and increase the reference counter.
1472 FcPatternGetCharSet(match, FC_CHARSET, 0u, characterSet);
1473 *characterSet = FcCharSetCopy(*characterSet);
1475 // destroyed the matched pattern
1476 FcPatternDestroy(match);
1477 FONT_LOG_DESCRIPTION(fontDescription, "");
1482 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
1484 FcFontSet* fontset = nullptr;
1486 // create a new pattern.
1487 // a pattern holds a set of names, each name refers to a property of the font
1488 FcPattern* pattern = FcPatternCreate();
1490 if(nullptr != pattern)
1492 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
1493 FcObjectSet* objectSet = FcObjectSetCreate();
1495 if(nullptr != objectSet)
1497 // build an object set from a list of property names
1498 FcObjectSetAdd(objectSet, FC_FILE);
1499 FcObjectSetAdd(objectSet, FC_FAMILY);
1500 FcObjectSetAdd(objectSet, FC_WIDTH);
1501 FcObjectSetAdd(objectSet, FC_WEIGHT);
1502 FcObjectSetAdd(objectSet, FC_SLANT);
1504 // get a list of fonts
1505 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
1506 fontset = FcFontList(nullptr /* the default configuration is checked to be up to date, and used */, pattern, objectSet); // Creates a FcFontSet that needs to be destroyed by calling FcFontSetDestroy.
1508 // clear up the object set
1509 FcObjectSetDestroy(objectSet);
1512 // clear up the pattern
1513 FcPatternDestroy(pattern);
1519 bool FontClient::Plugin::GetFcString(const FcPattern* const pattern,
1520 const char* const n,
1521 std::string& string)
1523 FcChar8* file = nullptr;
1524 const FcResult retVal = FcPatternGetString(pattern, n, 0u, &file);
1526 if(FcResultMatch == retVal)
1528 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1529 string.assign(reinterpret_cast<const char*>(file));
1537 bool FontClient::Plugin::GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal)
1539 const FcResult retVal = FcPatternGetInteger(pattern, n, 0u, &intVal);
1541 if(FcResultMatch == retVal)
1549 FontId FontClient::Plugin::CreateFont(const FontPath& path,
1550 PointSize26Dot6 requestedPointSize,
1551 FaceIndex faceIndex,
1552 bool cacheDescription)
1554 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1555 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
1556 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1560 // Create & cache new font face
1562 int error = FT_New_Face(mFreeTypeLibrary,
1567 if(FT_Err_Ok == error)
1569 // Check if a font is scalable.
1570 const bool isScalable = (0 != (ftFace->face_flags & FT_FACE_FLAG_SCALABLE));
1571 const bool hasFixedSizedBitmaps = (0 != (ftFace->face_flags & FT_FACE_FLAG_FIXED_SIZES)) && (0 != ftFace->num_fixed_sizes);
1572 const bool hasColorTables = (0 != (ftFace->face_flags & FT_FACE_FLAG_COLOR));
1573 FontId fontFaceId = 0u;
1575 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " isScalable : [%s]\n", (isScalable ? "true" : "false"));
1576 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasFixedSizedBitmaps : [%s]\n", (hasFixedSizedBitmaps ? "true" : "false"));
1577 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasColorTables : [%s]\n", (hasColorTables ? "true" : "false"));
1579 // Check to see if the font contains fixed sizes?
1580 if(!isScalable && hasFixedSizedBitmaps)
1582 PointSize26Dot6 actualPointSize = 0u;
1583 int fixedSizeIndex = 0;
1584 for(; fixedSizeIndex < ftFace->num_fixed_sizes; ++fixedSizeIndex)
1586 const PointSize26Dot6 fixedSize = ftFace->available_sizes[fixedSizeIndex].size;
1587 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, size : %d\n", fixedSizeIndex, fixedSize);
1589 if(fixedSize >= requestedPointSize)
1591 actualPointSize = fixedSize;
1596 if(0u == actualPointSize)
1598 // The requested point size is bigger than the bigest fixed size.
1599 fixedSizeIndex = ftFace->num_fixed_sizes - 1;
1600 actualPointSize = ftFace->available_sizes[fixedSizeIndex].size;
1603 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, actual size : %d\n", fixedSizeIndex, actualPointSize);
1605 // Tell Freetype to use this size
1606 error = FT_Select_Size(ftFace, fixedSizeIndex);
1607 if(FT_Err_Ok != error)
1609 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Select_Size error: %d\n", error);
1613 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1615 FontMetrics metrics(static_cast<float>(ftMetrics.ascender) * FROM_266,
1616 static_cast<float>(ftMetrics.descender) * FROM_266,
1617 static_cast<float>(ftMetrics.height) * FROM_266,
1618 static_cast<float>(ftFace->underline_position) * FROM_266,
1619 static_cast<float>(ftFace->underline_thickness) * FROM_266);
1621 const float fixedWidth = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].width);
1622 const float fixedHeight = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].height);
1624 // Create the FreeType font face item to cache.
1625 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, path, requestedPointSize, faceIndex, metrics, fixedSizeIndex, fixedWidth, fixedHeight, hasColorTables);
1627 // Set the index to the font's id cache.
1628 fontFaceCacheItem.mFontId = mFontIdCache.Count();
1630 // Create the font id item to cache.
1631 FontIdCacheItem fontIdCacheItem;
1632 fontIdCacheItem.type = FontDescription::FACE_FONT;
1634 // Set the index to the FreeType font face cache.
1635 fontIdCacheItem.index = mFontFaceCache.size();
1636 fontFaceId = fontIdCacheItem.index + 1u;
1639 mFontFaceCache.emplace_back(std::move(fontFaceCacheItem));
1640 mFontIdCache.PushBack(fontIdCacheItem);
1642 // Set the font id to be returned.
1643 id = mFontIdCache.Count();
1648 if(mIsAtlasLimitationEnabled)
1650 //There is limitation on block size to fit in predefined atlas size.
1651 //If the block size cannot fit into atlas size, then the system cannot draw block.
1652 //This is workaround to avoid issue in advance
1653 //Decrementing point-size until arriving to maximum allowed block size.
1654 auto requestedPointSizeBackup = requestedPointSize;
1655 const Size& maxSizeFitInAtlas = GetCurrentMaximumBlockSizeFitInAtlas();
1656 error = SearchOnProperPointSize(ftFace, mDpiHorizontal, mDpiVertical, maxSizeFitInAtlas, requestedPointSize);
1658 if(requestedPointSize != requestedPointSizeBackup)
1660 DALI_LOG_WARNING(" The requested-point-size : %d, is reduced to point-size : %d\n", requestedPointSizeBackup, requestedPointSize);
1665 error = FT_Set_Char_Size(ftFace,
1672 if(FT_Err_Ok == error)
1674 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1676 FontMetrics metrics(static_cast<float>(ftMetrics.ascender) * FROM_266,
1677 static_cast<float>(ftMetrics.descender) * FROM_266,
1678 static_cast<float>(ftMetrics.height) * FROM_266,
1679 static_cast<float>(ftFace->underline_position) * FROM_266,
1680 static_cast<float>(ftFace->underline_thickness) * FROM_266);
1682 // Create the FreeType font face item to cache.
1683 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, path, requestedPointSize, faceIndex, metrics);
1685 // Set the index to the font's id cache.
1686 fontFaceCacheItem.mFontId = mFontIdCache.Count();
1688 // Create the font id item to cache.
1689 FontIdCacheItem fontIdCacheItem;
1690 fontIdCacheItem.type = FontDescription::FACE_FONT;
1692 // Set the index to the FreeType font face cache.
1693 fontIdCacheItem.index = mFontFaceCache.size();
1694 fontFaceId = fontIdCacheItem.index + 1u;
1697 mFontFaceCache.emplace_back(std::move(fontFaceCacheItem));
1698 mFontIdCache.PushBack(fontIdCacheItem);
1700 // Set the font id to be returned.
1701 id = mFontIdCache.Count();
1705 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType Set_Char_Size error: %d for pointSize %d\n", error, requestedPointSize);
1709 if(0u != fontFaceId)
1711 if(cacheDescription)
1713 CacheFontPath(ftFace, fontFaceId, requestedPointSize, path);
1719 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType New_Face error: %d for [%s]\n", error, path.c_str());
1722 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
1726 bool FontClient::Plugin::FindFont(const FontPath& path,
1727 PointSize26Dot6 requestedPointSize,
1728 FaceIndex faceIndex,
1729 FontId& fontId) const
1731 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1732 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
1733 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1734 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of fonts in the cache : %d\n", mFontFaceCache.size());
1737 for(const auto& cacheItem : mFontFaceCache)
1739 if(cacheItem.mRequestedPointSize == requestedPointSize &&
1740 cacheItem.mFaceIndex == faceIndex &&
1741 cacheItem.mPath == path)
1743 fontId = cacheItem.mFontId + 1u;
1745 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font found, id : %d\n", fontId);
1750 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font not found\n");
1754 bool FontClient::Plugin::FindValidatedFont(const FontDescription& fontDescription,
1755 FontDescriptionId& fontDescriptionId)
1757 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1758 FONT_LOG_DESCRIPTION(fontDescription, "");
1759 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of validated fonts in the cache : %d\n", mValidatedFontCache.size());
1761 fontDescriptionId = 0u;
1763 for(const auto& item : mValidatedFontCache)
1765 if(!fontDescription.family.empty() &&
1766 (fontDescription.family == item.fontDescription.family) &&
1767 (fontDescription.width == item.fontDescription.width) &&
1768 (fontDescription.weight == item.fontDescription.weight) &&
1769 (fontDescription.slant == item.fontDescription.slant))
1771 fontDescriptionId = item.index;
1773 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validated font description found, id : %d\n", fontDescriptionId);
1778 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validated font description not found\n");
1782 bool FontClient::Plugin::FindFallbackFontList(const FontDescription& fontDescription,
1783 FontList*& fontList,
1784 CharacterSetList*& characterSetList)
1786 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1787 FONT_LOG_DESCRIPTION(fontDescription, "");
1788 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of fallback font lists in the cache : %d\n", mFallbackCache.size());
1792 for(const auto& item : mFallbackCache)
1794 if(!fontDescription.family.empty() &&
1795 (fontDescription.family == item.fontDescription.family) &&
1796 (fontDescription.width == item.fontDescription.width) &&
1797 (fontDescription.weight == item.fontDescription.weight) &&
1798 (fontDescription.slant == item.fontDescription.slant))
1800 fontList = item.fallbackFonts;
1801 characterSetList = item.characterSets;
1803 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fallback font list found.\n");
1808 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fallback font list not found.\n");
1812 bool FontClient::Plugin::FindFont(FontDescriptionId fontDescriptionId,
1813 PointSize26Dot6 requestedPointSize,
1814 FontCacheIndex& fontCacheIndex)
1816 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1817 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fontDescriptionId : %d\n", fontDescriptionId);
1818 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1820 fontCacheIndex = 0u;
1822 FontDescriptionSizeCacheKey key(fontDescriptionId, requestedPointSize);
1824 const auto& iter = mFontDescriptionSizeCache.find(key);
1825 if(iter != mFontDescriptionSizeCache.cend())
1827 fontCacheIndex = iter->second;
1829 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font found, index of font cache : %d\n", fontCacheIndex);
1833 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font not found.\n");
1837 bool FontClient::Plugin::FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const
1841 for(const auto& item : mBitmapFontCache)
1843 if(bitmapFont == item.font.name)
1845 fontId = item.id + 1u;
1853 bool FontClient::Plugin::IsScalable(const FontPath& path)
1855 bool isScalable = false;
1858 int error = FT_New_Face(mFreeTypeLibrary,
1862 if(FT_Err_Ok != error)
1864 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: %s\n", path.c_str());
1868 isScalable = ftFace->face_flags & FT_FACE_FLAG_SCALABLE;
1874 bool FontClient::Plugin::IsScalable(const FontDescription& fontDescription)
1876 // Create a font pattern.
1877 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1879 FcResult result = FcResultMatch;
1881 // match the pattern
1882 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1883 bool isScalable = false;
1887 // Get the path to the font file name.
1889 GetFcString(match, FC_FILE, path);
1890 isScalable = IsScalable(path);
1894 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1897 // Destroys the created patterns.
1898 FcPatternDestroy(match);
1899 FcPatternDestroy(fontFamilyPattern);
1904 void FontClient::Plugin::GetFixedSizes(const FontPath& path, Vector<PointSize26Dot6>& sizes)
1906 // Empty the caller container
1910 int error = FT_New_Face(mFreeTypeLibrary,
1914 if(FT_Err_Ok != error)
1916 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font path : [%s]\n", path.c_str());
1919 // Fetch the number of fixed sizes available
1920 if(ftFace->num_fixed_sizes && ftFace->available_sizes)
1922 for(int i = 0; i < ftFace->num_fixed_sizes; ++i)
1924 sizes.PushBack(ftFace->available_sizes[i].size);
1929 void FontClient::Plugin::GetFixedSizes(const FontDescription& fontDescription,
1930 Vector<PointSize26Dot6>& sizes)
1932 // Create a font pattern.
1933 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1935 FcResult result = FcResultMatch;
1937 // match the pattern
1938 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1942 // Get the path to the font file name.
1944 GetFcString(match, FC_FILE, path);
1945 GetFixedSizes(path, sizes);
1949 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1952 // Destroys the created patterns.
1953 FcPatternDestroy(match);
1954 FcPatternDestroy(fontFamilyPattern);
1957 bool FontClient::Plugin::HasItalicStyle(FontId fontId) const
1959 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1960 if(fontCacheItem != nullptr)
1962 return fontCacheItem->HasItalicStyle();
1967 void FontClient::Plugin::CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path)
1969 FontDescription description;
1970 description.path = path;
1971 description.family = std::move(FontFamily(ftFace->family_name));
1972 description.weight = FontWeight::NONE;
1973 description.width = FontWidth::NONE;
1974 description.slant = FontSlant::NONE;
1976 // Note FreeType doesn't give too much info to build a proper font style.
1977 if(ftFace->style_flags & FT_STYLE_FLAG_ITALIC)
1979 description.slant = FontSlant::ITALIC;
1981 if(ftFace->style_flags & FT_STYLE_FLAG_BOLD)
1983 description.weight = FontWeight::BOLD;
1986 FontDescriptionId fontDescriptionId = 0u;
1987 if(!FindValidatedFont(description,
1990 FcPattern* pattern = CreateFontFamilyPattern(description); // Creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
1992 FcResult result = FcResultMatch;
1993 FcPattern* match = FcFontMatch(nullptr, pattern, &result); // FcFontMatch creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
1995 FcCharSet* characterSet = nullptr;
1996 FcPatternGetCharSet(match, FC_CHARSET, 0u, &characterSet);
1998 const FontId fontFaceId = id - 1u;
1999 mFontFaceCache[fontFaceId].mCharacterSet = FcCharSetCopy(characterSet); // Increases the reference counter.
2001 // Destroys the created patterns.
2002 FcPatternDestroy(match);
2003 FcPatternDestroy(pattern);
2005 // Add the path to the cache.
2006 description.type = FontDescription::FACE_FONT;
2007 mFontDescriptionCache.push_back(description);
2009 // Set the index to the vector of paths to font file names.
2010 fontDescriptionId = mFontDescriptionCache.size();
2012 // Increase the reference counter and add the character set to the cache.
2013 mCharacterSetCache.PushBack(FcCharSetCopy(characterSet));
2015 // Cache the index and the font's description.
2016 mValidatedFontCache.push_back(std::move(FontDescriptionCacheItem(std::move(description),
2017 fontDescriptionId)));
2019 // Cache the pair 'fontDescriptionId, requestedPointSize' to improve the following queries.
2020 mFontDescriptionSizeCache.emplace(FontDescriptionSizeCacheKey(fontDescriptionId, requestedPointSize), fontFaceId);
2024 void FontClient::Plugin::ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache)
2026 for(auto& item : fallbackCache)
2028 if(nullptr != item.fallbackFonts)
2030 delete item.fallbackFonts;
2033 if(nullptr != item.characterSets)
2035 // Free the resources allocated by the FcCharSet objects in the 'characterSets' vector.
2036 DestroyCharacterSets(*item.characterSets);
2037 delete item.characterSets;
2042 void FontClient::Plugin::ClearCharacterSetFromFontFaceCache()
2044 for(auto& item : mFontFaceCache)
2046 FcCharSetDestroy(item.mCharacterSet);
2047 item.mCharacterSet = nullptr;
2051 } // namespace Dali::TextAbstraction::Internal