2 * Copyright (c) 2021 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::FontDescriptionSizeCacheItem::FontDescriptionSizeCacheItem(FontDescriptionId validatedFontId,
252 PointSize26Dot6 requestedPointSize,
254 : validatedFontId(validatedFontId),
255 requestedPointSize(requestedPointSize),
260 FontClient::Plugin::Plugin(unsigned int horizontalDpi,
261 unsigned int verticalDpi)
262 : mFreeTypeLibrary(nullptr),
263 mDpiHorizontal(horizontalDpi),
264 mDpiVertical(verticalDpi),
265 mDefaultFontDescription(),
270 mValidatedFontCache(),
271 mFontDescriptionCache(),
272 mCharacterSetCache(),
273 mFontDescriptionSizeCache(),
274 mVectorFontCache(nullptr),
276 mEmbeddedItemCache(),
277 mDefaultFontDescriptionCached(false),
278 mIsAtlasLimitationEnabled(TextAbstraction::FontClient::DEFAULT_ATLAS_LIMITATION_ENABLED),
279 mCurrentMaximumBlockSizeFitInAtlas(TextAbstraction::FontClient::MAX_SIZE_FIT_IN_ATLAS)
282 int error = FT_Init_FreeType(&mFreeTypeLibrary);
283 if(FT_Err_Ok != error)
285 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Init error: %d\n", error);
288 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
289 mVectorFontCache = new VectorFontCache(mFreeTypeLibrary);
293 FontClient::Plugin::~Plugin()
295 ClearFallbackCache(mFallbackCache);
297 // Free the resources allocated by the FcCharSet objects.
298 DestroyCharacterSets(mDefaultFontCharacterSets);
299 DestroyCharacterSets(mCharacterSetCache);
300 ClearCharacterSetFromFontFaceCache();
302 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
303 delete mVectorFontCache;
305 FT_Done_FreeType(mFreeTypeLibrary);
308 void FontClient::Plugin::ClearCache()
310 mDefaultFontDescription = FontDescription();
312 mSystemFonts.clear();
313 mDefaultFonts.clear();
315 DestroyCharacterSets(mDefaultFontCharacterSets);
316 mDefaultFontCharacterSets.Clear();
318 ClearFallbackCache(mFallbackCache);
319 mFallbackCache.clear();
321 mFontIdCache.Clear();
323 ClearCharacterSetFromFontFaceCache();
324 mFontFaceCache.clear();
326 mValidatedFontCache.clear();
327 mFontDescriptionCache.clear();
329 DestroyCharacterSets(mCharacterSetCache);
330 mCharacterSetCache.Clear();
332 mFontDescriptionSizeCache.clear();
334 mEllipsisCache.Clear();
335 mPixelBufferCache.clear();
336 mEmbeddedItemCache.Clear();
337 mBitmapFontCache.clear();
339 mDefaultFontDescriptionCached = false;
342 void FontClient::Plugin::SetDpi(unsigned int horizontalDpi,
343 unsigned int verticalDpi)
345 mDpiHorizontal = horizontalDpi;
346 mDpiVertical = verticalDpi;
349 void FontClient::Plugin::ResetSystemDefaults()
351 mDefaultFontDescriptionCached = false;
354 void FontClient::Plugin::SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList)
356 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
357 FONT_LOG_DESCRIPTION(fontDescription, "");
360 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a pattern that needs to be destroyed by calling FcPatternDestroy.
362 FcResult result = FcResultMatch;
364 // Match the pattern.
365 FcFontSet* fontSet = FcFontSort(nullptr /* use default configure */,
367 false /* don't trim */,
369 &result); // FcFontSort creates a font set that needs to be destroyed by calling FcFontSetDestroy.
371 if(nullptr != fontSet)
373 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of fonts found : [%d]\n", fontSet->nfont);
374 // Reserve some space to avoid reallocations.
375 fontList.reserve(fontSet->nfont);
377 for(int i = 0u; i < fontSet->nfont; ++i)
379 FcPattern* fontPattern = fontSet->fonts[i];
383 // Skip fonts with no path
384 if(GetFcString(fontPattern, FC_FILE, path))
386 // Retrieve the character set. Need to call FcCharSetDestroy to free the resources.
387 FcCharSet* characterSet = nullptr;
388 FcPatternGetCharSet(fontPattern, FC_CHARSET, 0u, &characterSet);
390 // Increase the reference counter of the character set.
391 characterSetList.PushBack(FcCharSetCopy(characterSet));
393 fontList.push_back(FontDescription());
394 FontDescription& newFontDescription = fontList.back();
396 newFontDescription.path = std::move(path);
401 GetFcString(fontPattern, FC_FAMILY, newFontDescription.family);
402 GetFcInt(fontPattern, FC_WIDTH, width);
403 GetFcInt(fontPattern, FC_WEIGHT, weight);
404 GetFcInt(fontPattern, FC_SLANT, slant);
405 newFontDescription.width = IntToWidthType(width);
406 newFontDescription.weight = IntToWeightType(weight);
407 newFontDescription.slant = IntToSlantType(slant);
409 FONT_LOG_DESCRIPTION(newFontDescription, "");
413 // Destroys the font set created by FcFontSort.
414 FcFontSetDestroy(fontSet);
418 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " No fonts found.\n");
421 // Destroys the pattern created by FcPatternCreate in CreateFontFamilyPattern.
422 FcPatternDestroy(fontFamilyPattern);
425 void FontClient::Plugin::GetDefaultFonts(FontList& defaultFonts)
427 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
429 if(mDefaultFonts.empty())
431 FontDescription fontDescription;
432 fontDescription.family = DEFAULT_FONT_FAMILY_NAME; // todo This could be set to the Platform font
433 fontDescription.width = DefaultFontWidth();
434 fontDescription.weight = DefaultFontWeight();
435 fontDescription.slant = DefaultFontSlant();
436 SetFontList(fontDescription, mDefaultFonts, mDefaultFontCharacterSets);
439 defaultFonts = mDefaultFonts;
441 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of default fonts : [%d]\n", mDefaultFonts.size());
444 void FontClient::Plugin::GetDefaultPlatformFontDescription(FontDescription& fontDescription)
446 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
448 if(!mDefaultFontDescriptionCached)
450 // Clear any font config stored info in the caches.
452 // Decrease the reference counter and eventually free the resources allocated by FcCharSet objects.
453 DestroyCharacterSets(mDefaultFontCharacterSets);
454 DestroyCharacterSets(mCharacterSetCache);
455 mDefaultFontCharacterSets.Clear();
456 mCharacterSetCache.Clear();
458 for(auto& item : mFallbackCache)
460 // Decrease the reference counter and eventually free the resources allocated by FcCharSet objects.
461 DestroyCharacterSets(*item.characterSets);
463 delete item.characterSets;
464 item.characterSets = nullptr;
467 // Set the character set pointer as null. Will be created again the next time IsCharacterSupportedByFont()
468 ClearCharacterSetFromFontFaceCache();
470 // FcInitBringUptoDate did not seem to reload config file as was still getting old default font.
471 FcInitReinitialize();
473 FcPattern* matchPattern = FcPatternCreate(); // Creates a pattern that needs to be destroyed by calling FcPatternDestroy.
475 if(nullptr != matchPattern)
477 FcConfigSubstitute(nullptr, matchPattern, FcMatchPattern);
478 FcDefaultSubstitute(matchPattern);
480 FcCharSet* characterSet = nullptr;
481 MatchFontDescriptionToPattern(matchPattern, mDefaultFontDescription, &characterSet);
482 // Decrease the reference counter of the character set as it's not stored.
483 FcCharSetDestroy(characterSet);
485 // Destroys the pattern created.
486 FcPatternDestroy(matchPattern);
489 // Create again the character sets as they are not valid after FcInitReinitialize()
491 for(const auto& description : mDefaultFonts)
493 mDefaultFontCharacterSets.PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
496 for(const auto& description : mFontDescriptionCache)
498 mCharacterSetCache.PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
501 for(auto& item : mFallbackCache)
503 if(nullptr != item.fallbackFonts)
505 if(nullptr == item.characterSets)
507 item.characterSets = new CharacterSetList;
510 for(const auto& description : *(item.fallbackFonts))
512 item.characterSets->PushBack(FcCharSetCopy(CreateCharacterSetFromDescription(description)));
517 mDefaultFontDescriptionCached = true;
520 fontDescription.path = mDefaultFontDescription.path;
521 fontDescription.family = mDefaultFontDescription.family;
522 fontDescription.width = mDefaultFontDescription.width;
523 fontDescription.weight = mDefaultFontDescription.weight;
524 fontDescription.slant = mDefaultFontDescription.slant;
526 FONT_LOG_DESCRIPTION(fontDescription, "");
529 void FontClient::Plugin::GetSystemFonts(FontList& systemFonts)
531 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
533 if(mSystemFonts.empty())
538 systemFonts = mSystemFonts;
539 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of system fonts : [%d]\n", mSystemFonts.size());
542 void FontClient::Plugin::GetDescription(FontId id,
543 FontDescription& fontDescription) const
545 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
546 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
547 const FontId index = id - 1u;
549 if((id > 0u) && (index < mFontIdCache.Count()))
551 const FontIdCacheItem& fontIdCacheItem = mFontIdCache[index];
552 switch(fontIdCacheItem.type)
554 case FontDescription::FACE_FONT:
556 for(const auto& item : mFontDescriptionSizeCache)
558 if(item.fontId == fontIdCacheItem.id)
560 fontDescription = *(mFontDescriptionCache.begin() + item.validatedFontId - 1u);
562 FONT_LOG_DESCRIPTION(fontDescription, "");
568 case FontDescription::BITMAP_FONT:
570 fontDescription.type = FontDescription::BITMAP_FONT;
571 fontDescription.family = mBitmapFontCache[fontIdCacheItem.id].font.name;
576 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
577 fontDescription.type = FontDescription::INVALID;
578 fontDescription.family.clear();
583 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " No description found for the font ID %d\n", id);
586 PointSize26Dot6 FontClient::Plugin::GetPointSize(FontId id)
588 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
589 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
591 PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
592 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(id);
593 if(fontCacheItem != nullptr)
595 pointSize = fontCacheItem->GetPointSize();
597 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " point size : %d\n", pointSize);
602 bool FontClient::Plugin::IsCharacterSupportedByFont(FontId fontId, Character character)
604 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
605 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
606 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
608 bool isSupported = false;
609 auto fontCacheItem = const_cast<FontCacheItemInterface*>(GetCachedFontItem(fontId));
610 if(fontCacheItem != nullptr)
612 isSupported = fontCacheItem->IsCharacterSupported(character); // May cache
615 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " is supported : %s\n", (isSupported ? "true" : "false"));
619 const FontCacheItemInterface* FontClient::Plugin::GetCachedFontItem(FontId id) const
621 const FontId index = id - 1u;
622 if((id > 0u) && (index < mFontIdCache.Count()))
624 const FontIdCacheItem& fontIdCacheItem = mFontIdCache[index];
625 switch(fontIdCacheItem.type)
627 case FontDescription::FACE_FONT:
629 return &mFontFaceCache[fontIdCacheItem.id];
631 case FontDescription::BITMAP_FONT:
633 return &mBitmapFontCache[fontIdCacheItem.id];
637 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
644 FontId FontClient::Plugin::FindFontForCharacter(const FontList& fontList,
645 const CharacterSetList& characterSetList,
647 PointSize26Dot6 requestedPointSize,
650 DALI_ASSERT_DEBUG((fontList.size() == characterSetList.Count()) && "FontClient::Plugin::FindFontForCharacter. Different number of fonts and character sets.");
651 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
652 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
653 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
654 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferColor : %s\n", (preferColor ? "true" : "false"));
657 bool foundColor = false;
659 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of fonts : %d\n", fontList.size());
661 // Traverse the list of fonts.
662 // Check for each font if supports the character.
663 for(unsigned int index = 0u, numberOfFonts = fontList.size(); index < numberOfFonts; ++index)
665 const FontDescription& description = fontList[index];
666 const FcCharSet* const characterSet = characterSetList[index];
668 FONT_LOG_DESCRIPTION(description, "");
670 bool foundInRanges = false;
671 if(nullptr != characterSet)
673 foundInRanges = FcCharSetHasChar(characterSet, character);
678 fontId = GetFontId(description, requestedPointSize, 0u);
680 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " font id : %d\n", fontId);
685 (fontId - 1u < mFontIdCache.Count()))
687 const FontFaceCacheItem& item = mFontFaceCache[mFontIdCache[fontId - 1u].id];
689 foundColor = item.mHasColorTables;
692 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " foundColor : %s\n", (foundColor ? "true" : "false"));
695 // Keep going unless we prefer a different (color) font.
696 if(!preferColor || foundColor)
703 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
707 FontId FontClient::Plugin::FindDefaultFont(Character charcode,
708 PointSize26Dot6 requestedPointSize,
711 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
712 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
716 // Create the list of default fonts if it has not been created.
717 if(mDefaultFonts.empty())
719 FontDescription fontDescription;
720 fontDescription.family = DEFAULT_FONT_FAMILY_NAME;
721 fontDescription.width = DefaultFontWidth();
722 fontDescription.weight = DefaultFontWeight();
723 fontDescription.slant = DefaultFontSlant();
725 SetFontList(fontDescription, mDefaultFonts, mDefaultFontCharacterSets);
727 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of default fonts : %d\n", mDefaultFonts.size());
729 // Traverse the list of default fonts.
730 // Check for each default font if supports the character.
731 fontId = FindFontForCharacter(mDefaultFonts, mDefaultFontCharacterSets, charcode, requestedPointSize, preferColor);
733 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
737 FontId FontClient::Plugin::FindFallbackFont(Character charcode,
738 const FontDescription& preferredFontDescription,
739 PointSize26Dot6 requestedPointSize,
742 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
743 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
745 // The font id to be returned.
748 FontDescription fontDescription;
750 // Fill the font description with the preferred font description and complete with the defaults.
751 fontDescription.family = preferredFontDescription.family.empty() ? DEFAULT_FONT_FAMILY_NAME : preferredFontDescription.family;
752 fontDescription.weight = ((FontWeight::NONE == preferredFontDescription.weight) ? DefaultFontWeight() : preferredFontDescription.weight);
753 fontDescription.width = ((FontWidth::NONE == preferredFontDescription.width) ? DefaultFontWidth() : preferredFontDescription.width);
754 fontDescription.slant = ((FontSlant::NONE == preferredFontDescription.slant) ? DefaultFontSlant() : preferredFontDescription.slant);
756 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferredFontDescription --> fontDescription\n");
757 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " [%s] --> [%s]\n", preferredFontDescription.family.c_str(), fontDescription.family.c_str());
758 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWeight::Name[preferredFontDescription.weight], FontWeight::Name[fontDescription.weight]);
759 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWidth::Name[preferredFontDescription.width], FontWidth::Name[fontDescription.width]);
760 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontSlant::Name[preferredFontDescription.slant], FontSlant::Name[fontDescription.slant]);
762 // Check first if the font's description has been queried before.
763 FontList* fontList = nullptr;
764 CharacterSetList* characterSetList = nullptr;
766 if(!FindFallbackFontList(fontDescription, fontList, characterSetList))
768 fontList = new FontList;
769 characterSetList = new CharacterSetList;
771 SetFontList(fontDescription, *fontList, *characterSetList);
773 FontDescription appleColorEmoji;
774 appleColorEmoji.family = "Apple Color Emoji";
775 appleColorEmoji.width = fontDescription.width;
776 appleColorEmoji.weight = fontDescription.weight;
777 appleColorEmoji.slant = fontDescription.slant;
778 FontList emojiFontList;
779 CharacterSetList emojiCharSetList;
780 SetFontList(appleColorEmoji, emojiFontList, emojiCharSetList);
782 std::move(fontList->begin(), fontList->end(), std::back_inserter(emojiFontList));
783 emojiCharSetList.Insert(emojiCharSetList.End(), characterSetList->Begin(), characterSetList->End());
784 *fontList = std::move(emojiFontList);
785 *characterSetList = std::move(emojiCharSetList);
788 // Add the font-list to the cache.
789 mFallbackCache.push_back(std::move(FallbackCacheItem(std::move(fontDescription), fontList, characterSetList)));
792 if(fontList && characterSetList)
794 fontId = FindFontForCharacter(*fontList, *characterSetList, charcode, requestedPointSize, preferColor);
797 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
801 FontId FontClient::Plugin::GetFontId(const FontPath& path,
802 PointSize26Dot6 requestedPointSize,
804 bool cacheDescription)
806 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
807 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
808 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
812 if(nullptr != mFreeTypeLibrary)
815 if(FindFont(path, requestedPointSize, faceIndex, foundId))
821 id = CreateFont(path, requestedPointSize, faceIndex, cacheDescription);
825 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
829 FontId FontClient::Plugin::GetFontId(const FontDescription& fontDescription,
830 PointSize26Dot6 requestedPointSize,
833 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
834 FONT_LOG_DESCRIPTION(fontDescription, "");
835 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
837 // This method uses three vectors which caches:
838 // * The bitmap font cache
839 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
840 // * The path to font file names.
841 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
843 // 1) Checks if the font description matches with a previously loaded bitmap font.
844 // Returns if a font is found.
845 // 2) Checks in the cache if the font's description has been validated before.
846 // If it was it gets an index to the vector with paths to font file names. Otherwise,
847 // retrieves using font config a path to a font file name which matches with the
848 // font's description. The path is stored in the cache.
850 // 3) Checks in the cache if the pair 'font point size, index to the vector with paths to
851 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
852 // the GetFontId() method with the path to the font file name and the point size to
855 // The font id to be returned.
858 // Check first if the font description matches with a previously loaded bitmap font.
859 if(FindBitmapFont(fontDescription.family, fontId))
864 // Check if the font's description have been validated before.
865 FontDescriptionId validatedFontId = 0u;
867 if(!FindValidatedFont(fontDescription,
870 // Use font config to validate the font's description.
871 ValidateFont(fontDescription,
875 FontId fontFaceId = 0u;
876 // Check if exists a pair 'validatedFontId, requestedPointSize' in the cache.
877 if(!FindFont(validatedFontId, requestedPointSize, fontFaceId))
879 // Retrieve the font file name path.
880 const FontDescription& description = *(mFontDescriptionCache.begin() + validatedFontId - 1u);
882 // Retrieve the font id. Do not cache the description as it has been already cached.
883 fontId = GetFontId(description.path,
888 fontFaceId = mFontIdCache[fontId - 1u].id;
889 mFontFaceCache[fontFaceId].mCharacterSet = FcCharSetCopy(mCharacterSetCache[validatedFontId - 1u]);
891 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
892 mFontDescriptionSizeCache.push_back(FontDescriptionSizeCacheItem(validatedFontId,
898 fontId = mFontFaceCache[fontFaceId].mFontId + 1u;
901 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
905 FontId FontClient::Plugin::GetFontId(const BitmapFont& bitmapFont)
907 for(const auto& item : mBitmapFontCache)
909 if(bitmapFont.name == item.font.name)
915 BitmapFontCacheItem bitmapFontCacheItem(bitmapFont, mFontIdCache.Count());
917 FontIdCacheItem fontIdCacheItem;
918 fontIdCacheItem.type = FontDescription::BITMAP_FONT;
919 fontIdCacheItem.id = mBitmapFontCache.size();
921 mBitmapFontCache.push_back(std::move(bitmapFontCacheItem));
922 mFontIdCache.PushBack(fontIdCacheItem);
924 return bitmapFontCacheItem.id + 1u;
927 void FontClient::Plugin::ValidateFont(const FontDescription& fontDescription,
928 FontDescriptionId& validatedFontId)
930 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
931 FONT_LOG_DESCRIPTION(fontDescription, "");
933 // Create a font pattern.
934 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription);
936 FontDescription description;
938 FcCharSet* characterSet = nullptr;
939 bool matched = MatchFontDescriptionToPattern(fontFamilyPattern, description, &characterSet);
940 FcPatternDestroy(fontFamilyPattern);
942 if(matched && (nullptr != characterSet))
944 // Add the path to the cache.
945 description.type = FontDescription::FACE_FONT;
946 mFontDescriptionCache.push_back(description);
948 // Set the index to the vector of paths to font file names.
949 validatedFontId = mFontDescriptionCache.size();
951 FONT_LOG_DESCRIPTION(description, "matched");
952 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId);
954 // The reference counter of the character set has already been increased in MatchFontDescriptionToPattern.
955 mCharacterSetCache.PushBack(characterSet);
957 // Cache the index and the matched font's description.
958 FontDescriptionCacheItem item(description,
961 mValidatedFontCache.push_back(std::move(item));
963 if((fontDescription.family != description.family) ||
964 (fontDescription.width != description.width) ||
965 (fontDescription.weight != description.weight) ||
966 (fontDescription.slant != description.slant))
968 // Cache the given font's description if it's different than the matched.
969 FontDescriptionCacheItem item(fontDescription,
972 mValidatedFontCache.push_back(std::move(item));
977 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font validation failed for font [%s]\n", fontDescription.family.c_str());
981 void FontClient::Plugin::GetFontMetrics(FontId fontId,
982 FontMetrics& metrics)
984 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
985 if(fontCacheItem != nullptr)
987 fontCacheItem->GetFontMetrics(metrics, mDpiVertical);
991 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
994 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
995 if(fontCacheItem != nullptr)
997 return fontCacheItem->GetGlyphIndex(charcode);
1003 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
1005 Character variantSelector)
1007 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1008 if(fontCacheItem != nullptr)
1010 return fontCacheItem->GetGlyphIndex(charcode, variantSelector);
1016 bool FontClient::Plugin::GetGlyphMetrics(GlyphInfo* array,
1021 if(VECTOR_GLYPH == type)
1023 return GetVectorMetrics(array, size, horizontal);
1026 return GetBitmapMetrics(array, size, horizontal);
1029 bool FontClient::Plugin::GetBitmapMetrics(GlyphInfo* array,
1033 bool success(false);
1035 for(unsigned int i = 0; i < size; ++i)
1037 GlyphInfo& glyph = array[i];
1039 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(glyph.fontId);
1040 if(fontCacheItem != nullptr)
1042 success = fontCacheItem->GetGlyphMetrics(glyph, mDpiVertical, horizontal);
1044 // Check if it's an embedded image.
1045 else if((0u == glyph.fontId) && (0u != glyph.index) && (glyph.index <= mEmbeddedItemCache.Count()))
1047 mEmbeddedItemCache[glyph.index - 1u].GetGlyphMetrics(glyph);
1055 bool FontClient::Plugin::GetVectorMetrics(GlyphInfo* array,
1059 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1062 for(unsigned int i = 0u; i < size; ++i)
1064 FontId fontId = array[i].fontId;
1067 (fontId - 1u) < mFontIdCache.Count())
1069 FontFaceCacheItem& font = mFontFaceCache[mFontIdCache[fontId - 1u].id];
1071 if(!font.mVectorFontId)
1073 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
1076 mVectorFontCache->GetGlyphMetrics(font.mVectorFontId, array[i]);
1078 // Vector metrics are in EMs, convert to pixels
1079 const float scale = (static_cast<float>(font.mRequestedPointSize) * FROM_266) * static_cast<float>(mDpiVertical) / POINTS_PER_INCH;
1080 array[i].width *= scale;
1081 array[i].height *= scale;
1082 array[i].xBearing *= scale;
1083 array[i].yBearing *= scale;
1084 array[i].advance *= scale;
1098 void FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth)
1100 data.isColorBitmap = false;
1101 data.isColorEmoji = false;
1102 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1103 if(fontCacheItem != nullptr)
1105 fontCacheItem->CreateBitmap(glyphIndex, data, outlineWidth, isItalicRequired, isBoldRequired);
1107 else if((0u != glyphIndex) && (glyphIndex <= mEmbeddedItemCache.Count()))
1109 // It's an embedded item.
1110 mEmbeddedItemCache[glyphIndex - 1u].CreateBitmap(mPixelBufferCache, data);
1114 PixelData FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth)
1116 TextAbstraction::FontClient::GlyphBufferData data;
1118 CreateBitmap(fontId, glyphIndex, false, false, data, outlineWidth);
1120 return PixelData::New(data.buffer,
1121 data.width * data.height * Pixel::GetBytesPerPixel(data.format),
1125 PixelData::DELETE_ARRAY);
1128 void FontClient::Plugin::CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight)
1133 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
1135 (fontId - 1u < mFontIdCache.Count()))
1137 const FontId fontFaceId = mFontIdCache[fontId - 1u].id;
1138 FontFaceCacheItem& font = mFontFaceCache[fontFaceId];
1140 if(!font.mVectorFontId)
1142 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
1145 mVectorFontCache->GetVectorBlob(font.mVectorFontId, fontFaceId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight);
1150 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph(PointSize26Dot6 requestedPointSize)
1152 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1153 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize %d.\n", requestedPointSize);
1155 // First look into the cache if there is an ellipsis glyph for the requested point size.
1156 for(const auto& item : mEllipsisCache)
1158 if(item.requestedPointSize == requestedPointSize)
1160 // Use the glyph in the cache.
1161 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index);
1162 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font %d.\n", item.glyph.fontId);
1167 // No glyph has been found. Create one.
1168 mEllipsisCache.PushBack(EllipsisItem());
1169 EllipsisItem& item = *(mEllipsisCache.End() - 1u);
1171 item.requestedPointSize = requestedPointSize;
1173 // Find a font for the ellipsis glyph.
1174 item.glyph.fontId = FindDefaultFont(ELLIPSIS_CHARACTER,
1178 // Set the character index to access the glyph inside the font.
1179 item.glyph.index = FT_Get_Char_Index(mFontFaceCache[mFontIdCache[item.glyph.fontId - 1u].id].mFreeTypeFace,
1180 ELLIPSIS_CHARACTER);
1182 GetBitmapMetrics(&item.glyph, 1u, true);
1184 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index);
1185 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font %d.\n", item.glyph.fontId);
1189 bool FontClient::Plugin::IsColorGlyph(FontId fontId, GlyphIndex glyphIndex)
1191 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1192 return fontCacheItem && fontCacheItem->IsColorGlyph(glyphIndex);
1195 FT_FaceRec_* FontClient::Plugin::GetFreetypeFace(FontId fontId)
1197 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1198 if(fontCacheItem != nullptr)
1200 return fontCacheItem->GetTypeface();
1205 FontDescription::Type FontClient::Plugin::GetFontType(FontId fontId)
1207 const FontId index = fontId - 1u;
1209 (index < mFontIdCache.Count()))
1211 return mFontIdCache[index].type;
1213 return FontDescription::INVALID;
1216 bool FontClient::Plugin::AddCustomFontDirectory(const FontPath& path)
1218 // nullptr as first parameter means the current configuration is used.
1219 return FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>(path.c_str()));
1222 GlyphIndex FontClient::Plugin::CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat)
1224 EmbeddedItem embeddedItem;
1226 embeddedItem.pixelBufferId = 0u;
1227 embeddedItem.width = description.width;
1228 embeddedItem.height = description.height;
1230 pixelFormat = Pixel::A8;
1232 if(!description.url.empty())
1234 // Check if the url is in the cache.
1235 PixelBufferId index = 0u;
1237 for(const auto& cacheItem : mPixelBufferCache)
1240 if(cacheItem.url == description.url)
1242 // The url is in the pixel buffer cache.
1243 // Set the index +1 to the vector.
1244 embeddedItem.pixelBufferId = index;
1249 Devel::PixelBuffer pixelBuffer;
1250 if(0u == embeddedItem.pixelBufferId)
1252 // The pixel buffer is not in the cache. Create one and cache it.
1254 // Load the image from the url.
1255 pixelBuffer = LoadImageFromFile(description.url);
1257 // Create the cache item.
1258 PixelBufferCacheItem pixelBufferCacheItem;
1259 pixelBufferCacheItem.pixelBuffer = pixelBuffer;
1260 pixelBufferCacheItem.url = description.url;
1262 // Store the cache item in the cache.
1263 mPixelBufferCache.push_back(std::move(pixelBufferCacheItem));
1265 // Set the pixel buffer id to the embedded item.
1266 embeddedItem.pixelBufferId = mPixelBufferCache.size();
1270 // Retrieve the pixel buffer from the cache to set the pixel format.
1271 pixelBuffer = mPixelBufferCache[embeddedItem.pixelBufferId - 1u].pixelBuffer;
1276 // Set the size of the embedded item if it has not been set.
1277 if(0u == embeddedItem.width)
1279 embeddedItem.width = static_cast<unsigned int>(pixelBuffer.GetWidth());
1282 if(0u == embeddedItem.height)
1284 embeddedItem.height = static_cast<unsigned int>(pixelBuffer.GetHeight());
1287 // Set the pixel format.
1288 pixelFormat = pixelBuffer.GetPixelFormat();
1292 // Find if the same embeddedItem has already been created.
1293 unsigned int index = 0u;
1294 for(const auto& item : mEmbeddedItemCache)
1297 if((item.pixelBufferId == embeddedItem.pixelBufferId) &&
1298 (item.width == embeddedItem.width) &&
1299 (item.height == embeddedItem.height))
1305 // Cache the embedded item.
1306 mEmbeddedItemCache.PushBack(embeddedItem);
1308 return mEmbeddedItemCache.Count();
1311 void FontClient::Plugin::EnableAtlasLimitation(bool enabled)
1313 mIsAtlasLimitationEnabled = enabled;
1316 bool FontClient::Plugin::IsAtlasLimitationEnabled() const
1318 return mIsAtlasLimitationEnabled;
1321 Size FontClient::Plugin::GetMaximumTextAtlasSize() const
1323 return TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
1326 Size FontClient::Plugin::GetDefaultTextAtlasSize() const
1328 return TextAbstraction::FontClient::DEFAULT_TEXT_ATLAS_SIZE;
1331 Size FontClient::Plugin::GetCurrentMaximumBlockSizeFitInAtlas() const
1333 return mCurrentMaximumBlockSizeFitInAtlas;
1336 bool FontClient::Plugin::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
1338 bool isChanged = false;
1339 const Size& maxTextAtlasSize = TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
1340 const uint16_t& padding = TextAbstraction::FontClient::PADDING_TEXT_ATLAS_BLOCK;
1342 if(currentMaximumBlockSizeFitInAtlas.width <= maxTextAtlasSize.width - padding && currentMaximumBlockSizeFitInAtlas.height <= maxTextAtlasSize.height - padding)
1344 mCurrentMaximumBlockSizeFitInAtlas = currentMaximumBlockSizeFitInAtlas;
1351 uint32_t FontClient::Plugin::GetNumberOfPointsPerOneUnitOfPointSize() const
1353 return TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
1357 void FontClient::Plugin::InitSystemFonts()
1359 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1361 FcFontSet* fontSet = GetFcFontSet(); // Creates a FcFontSet that needs to be destroyed by calling FcFontSetDestroy.
1365 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of system fonts : %d\n", fontSet->nfont);
1367 // Reserve some space to avoid reallocations.
1368 mSystemFonts.reserve(fontSet->nfont);
1370 for(int i = 0u; i < fontSet->nfont; ++i)
1372 FcPattern* fontPattern = fontSet->fonts[i];
1376 // Skip fonts with no path
1377 if(GetFcString(fontPattern, FC_FILE, path))
1379 mSystemFonts.push_back(FontDescription());
1380 FontDescription& fontDescription = mSystemFonts.back();
1382 fontDescription.path = std::move(path);
1387 GetFcString(fontPattern, FC_FAMILY, fontDescription.family);
1388 GetFcInt(fontPattern, FC_WIDTH, width);
1389 GetFcInt(fontPattern, FC_WEIGHT, weight);
1390 GetFcInt(fontPattern, FC_SLANT, slant);
1391 fontDescription.width = IntToWidthType(width);
1392 fontDescription.weight = IntToWeightType(weight);
1393 fontDescription.slant = IntToSlantType(slant);
1395 FONT_LOG_DESCRIPTION(fontDescription, "");
1399 // Destroys the font set created.
1400 FcFontSetDestroy(fontSet);
1404 bool FontClient::Plugin::MatchFontDescriptionToPattern(FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, FcCharSet** characterSet)
1406 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1408 FcResult result = FcResultMatch;
1409 FcPattern* match = FcFontMatch(nullptr /* use default configure */, pattern, &result); // Creates a new font pattern that needs to be destroyed by calling FcPatternDestroy.
1411 const bool matched = nullptr != match;
1412 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " pattern matched : %s\n", (matched ? "true" : "false"));
1419 GetFcString(match, FC_FILE, fontDescription.path);
1420 GetFcString(match, FC_FAMILY, fontDescription.family);
1421 GetFcInt(match, FC_WIDTH, width);
1422 GetFcInt(match, FC_WEIGHT, weight);
1423 GetFcInt(match, FC_SLANT, slant);
1424 fontDescription.width = IntToWidthType(width);
1425 fontDescription.weight = IntToWeightType(weight);
1426 fontDescription.slant = IntToSlantType(slant);
1428 // Retrieve the character set and increase the reference counter.
1429 FcPatternGetCharSet(match, FC_CHARSET, 0u, characterSet);
1430 *characterSet = FcCharSetCopy(*characterSet);
1432 // destroyed the matched pattern
1433 FcPatternDestroy(match);
1434 FONT_LOG_DESCRIPTION(fontDescription, "");
1439 _FcFontSet* FontClient::Plugin::GetFcFontSet() const
1441 FcFontSet* fontset = nullptr;
1443 // create a new pattern.
1444 // a pattern holds a set of names, each name refers to a property of the font
1445 FcPattern* pattern = FcPatternCreate();
1447 if(nullptr != pattern)
1449 // create an object set used to define which properties are to be returned in the patterns from FcFontList.
1450 FcObjectSet* objectSet = FcObjectSetCreate();
1452 if(nullptr != objectSet)
1454 // build an object set from a list of property names
1455 FcObjectSetAdd(objectSet, FC_FILE);
1456 FcObjectSetAdd(objectSet, FC_FAMILY);
1457 FcObjectSetAdd(objectSet, FC_WIDTH);
1458 FcObjectSetAdd(objectSet, FC_WEIGHT);
1459 FcObjectSetAdd(objectSet, FC_SLANT);
1461 // get a list of fonts
1462 // creates patterns from those fonts containing only the objects in objectSet and returns the set of unique such patterns
1463 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.
1465 // clear up the object set
1466 FcObjectSetDestroy(objectSet);
1469 // clear up the pattern
1470 FcPatternDestroy(pattern);
1476 bool FontClient::Plugin::GetFcString(const FcPattern* const pattern,
1477 const char* const n,
1478 std::string& string)
1480 FcChar8* file = nullptr;
1481 const FcResult retVal = FcPatternGetString(pattern, n, 0u, &file);
1483 if(FcResultMatch == retVal)
1485 // Have to use reinterpret_cast because FcChar8 is unsigned char*, not a const char*.
1486 string.assign(reinterpret_cast<const char*>(file));
1494 bool FontClient::Plugin::GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal)
1496 const FcResult retVal = FcPatternGetInteger(pattern, n, 0u, &intVal);
1498 if(FcResultMatch == retVal)
1506 FontId FontClient::Plugin::CreateFont(const FontPath& path,
1507 PointSize26Dot6 requestedPointSize,
1508 FaceIndex faceIndex,
1509 bool cacheDescription)
1511 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1512 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
1513 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1517 // Create & cache new font face
1519 int error = FT_New_Face(mFreeTypeLibrary,
1524 if(FT_Err_Ok == error)
1526 // Check if a font is scalable.
1527 const bool isScalable = (0 != (ftFace->face_flags & FT_FACE_FLAG_SCALABLE));
1528 const bool hasFixedSizedBitmaps = (0 != (ftFace->face_flags & FT_FACE_FLAG_FIXED_SIZES)) && (0 != ftFace->num_fixed_sizes);
1529 const bool hasColorTables = (0 != (ftFace->face_flags & FT_FACE_FLAG_COLOR));
1530 FontId fontFaceId = 0u;
1532 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " isScalable : [%s]\n", (isScalable ? "true" : "false"));
1533 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasFixedSizedBitmaps : [%s]\n", (hasFixedSizedBitmaps ? "true" : "false"));
1534 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasColorTables : [%s]\n", (hasColorTables ? "true" : "false"));
1536 // Check to see if the font contains fixed sizes?
1537 if(!isScalable && hasFixedSizedBitmaps)
1539 PointSize26Dot6 actualPointSize = 0u;
1540 int fixedSizeIndex = 0;
1541 for(; fixedSizeIndex < ftFace->num_fixed_sizes; ++fixedSizeIndex)
1543 const PointSize26Dot6 fixedSize = ftFace->available_sizes[fixedSizeIndex].size;
1544 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, size : %d\n", fixedSizeIndex, fixedSize);
1546 if(fixedSize >= requestedPointSize)
1548 actualPointSize = fixedSize;
1553 if(0u == actualPointSize)
1555 // The requested point size is bigger than the bigest fixed size.
1556 fixedSizeIndex = ftFace->num_fixed_sizes - 1;
1557 actualPointSize = ftFace->available_sizes[fixedSizeIndex].size;
1560 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, actual size : %d\n", fixedSizeIndex, actualPointSize);
1562 // Tell Freetype to use this size
1563 error = FT_Select_Size(ftFace, fixedSizeIndex);
1564 if(FT_Err_Ok != error)
1566 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Select_Size error: %d\n", error);
1570 const float fixedWidth = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].width);
1571 const float fixedHeight = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].height);
1573 // Indicate that the font is a fixed sized bitmap
1574 FontMetrics metrics(fixedHeight, // The ascender in pixels.
1576 fixedHeight, // The height in pixels.
1580 // Create the FreeType font face item to cache.
1581 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, path, requestedPointSize, faceIndex, metrics, fixedSizeIndex, fixedWidth, fixedHeight, hasColorTables);
1583 // Set the index to the font's id cache.
1584 fontFaceCacheItem.mFontId = mFontIdCache.Count();
1586 // Create the font id item to cache.
1587 FontIdCacheItem fontIdCacheItem;
1588 fontIdCacheItem.type = FontDescription::FACE_FONT;
1590 // Set the index to the FreeType font face cache.
1591 fontIdCacheItem.id = mFontFaceCache.size();
1592 fontFaceId = fontIdCacheItem.id + 1u;
1595 mFontFaceCache.push_back(fontFaceCacheItem);
1596 mFontIdCache.PushBack(fontIdCacheItem);
1598 // Set the font id to be returned.
1599 id = mFontIdCache.Count();
1604 if(mIsAtlasLimitationEnabled)
1606 //There is limitation on block size to fit in predefined atlas size.
1607 //If the block size cannot fit into atlas size, then the system cannot draw block.
1608 //This is workaround to avoid issue in advance
1609 //Decrementing point-size until arriving to maximum allowed block size.
1610 auto requestedPointSizeBackup = requestedPointSize;
1611 const Size& maxSizeFitInAtlas = GetCurrentMaximumBlockSizeFitInAtlas();
1612 error = SearchOnProperPointSize(ftFace, mDpiHorizontal, mDpiVertical, maxSizeFitInAtlas, requestedPointSize);
1614 if(requestedPointSize != requestedPointSizeBackup)
1616 DALI_LOG_WARNING(" The requested-point-size : %d, is reduced to point-size : %d\n", requestedPointSizeBackup, requestedPointSize);
1621 error = FT_Set_Char_Size(ftFace,
1628 if(FT_Err_Ok == error)
1630 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1632 FontMetrics metrics(static_cast<float>(ftMetrics.ascender) * FROM_266,
1633 static_cast<float>(ftMetrics.descender) * FROM_266,
1634 static_cast<float>(ftMetrics.height) * FROM_266,
1635 static_cast<float>(ftFace->underline_position) * FROM_266,
1636 static_cast<float>(ftFace->underline_thickness) * FROM_266);
1638 // Create the FreeType font face item to cache.
1639 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, path, requestedPointSize, faceIndex, metrics);
1641 // Set the index to the font's id cache.
1642 fontFaceCacheItem.mFontId = mFontIdCache.Count();
1644 // Create the font id item to cache.
1645 FontIdCacheItem fontIdCacheItem;
1646 fontIdCacheItem.type = FontDescription::FACE_FONT;
1648 // Set the index to the FreeType font face cache.
1649 fontIdCacheItem.id = mFontFaceCache.size();
1650 fontFaceId = fontIdCacheItem.id + 1u;
1653 mFontFaceCache.push_back(fontFaceCacheItem);
1654 mFontIdCache.PushBack(fontIdCacheItem);
1656 // Set the font id to be returned.
1657 id = mFontIdCache.Count();
1661 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType Set_Char_Size error: %d for pointSize %d\n", error, requestedPointSize);
1665 if(0u != fontFaceId)
1667 if(cacheDescription)
1669 CacheFontPath(ftFace, fontFaceId, requestedPointSize, path);
1675 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType New_Face error: %d for [%s]\n", error, path.c_str());
1678 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
1682 bool FontClient::Plugin::FindFont(const FontPath& path,
1683 PointSize26Dot6 requestedPointSize,
1684 FaceIndex faceIndex,
1685 FontId& fontId) const
1687 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1688 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
1689 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1690 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of fonts in the cache : %d\n", mFontFaceCache.size());
1693 for(const auto& cacheItem : mFontFaceCache)
1695 if(cacheItem.mRequestedPointSize == requestedPointSize &&
1696 cacheItem.mFaceIndex == faceIndex &&
1697 cacheItem.mPath == path)
1699 fontId = cacheItem.mFontId + 1u;
1701 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font found, id : %d\n", fontId);
1706 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font not found\n");
1710 bool FontClient::Plugin::FindValidatedFont(const FontDescription& fontDescription,
1711 FontDescriptionId& validatedFontId)
1713 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1714 FONT_LOG_DESCRIPTION(fontDescription, "");
1715 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of validated fonts in the cache : %d\n", mValidatedFontCache.size());
1717 validatedFontId = 0u;
1719 for(const auto& item : mValidatedFontCache)
1721 if(!fontDescription.family.empty() &&
1722 (fontDescription.family == item.fontDescription.family) &&
1723 (fontDescription.width == item.fontDescription.width) &&
1724 (fontDescription.weight == item.fontDescription.weight) &&
1725 (fontDescription.slant == item.fontDescription.slant))
1727 validatedFontId = item.index;
1729 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validated font found, id : %d\n", validatedFontId);
1734 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validated font not found\n");
1738 bool FontClient::Plugin::FindFallbackFontList(const FontDescription& fontDescription,
1739 FontList*& fontList,
1740 CharacterSetList*& characterSetList)
1742 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1743 FONT_LOG_DESCRIPTION(fontDescription, "");
1744 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of fallback font lists in the cache : %d\n", mFallbackCache.size());
1748 for(const auto& item : mFallbackCache)
1750 if(!fontDescription.family.empty() &&
1751 (fontDescription.family == item.fontDescription.family) &&
1752 (fontDescription.width == item.fontDescription.width) &&
1753 (fontDescription.weight == item.fontDescription.weight) &&
1754 (fontDescription.slant == item.fontDescription.slant))
1756 fontList = item.fallbackFonts;
1757 characterSetList = item.characterSets;
1759 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fallback font list found.\n");
1764 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " fallback font list not found.\n");
1768 bool FontClient::Plugin::FindFont(FontDescriptionId validatedFontId,
1769 PointSize26Dot6 requestedPointSize,
1772 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1773 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " validatedFontId : %d\n", validatedFontId);
1774 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1778 for(const auto& item : mFontDescriptionSizeCache)
1780 if((validatedFontId == item.validatedFontId) &&
1781 (requestedPointSize == item.requestedPointSize))
1783 fontId = item.fontId;
1785 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font found, id : %d\n", fontId);
1790 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font not found.\n");
1794 bool FontClient::Plugin::FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const
1798 for(const auto& item : mBitmapFontCache)
1800 if(bitmapFont == item.font.name)
1802 fontId = item.id + 1u;
1810 bool FontClient::Plugin::IsScalable(const FontPath& path)
1812 bool isScalable = false;
1815 int error = FT_New_Face(mFreeTypeLibrary,
1819 if(FT_Err_Ok != error)
1821 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: %s\n", path.c_str());
1825 isScalable = ftFace->face_flags & FT_FACE_FLAG_SCALABLE;
1831 bool FontClient::Plugin::IsScalable(const FontDescription& fontDescription)
1833 // Create a font pattern.
1834 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1836 FcResult result = FcResultMatch;
1838 // match the pattern
1839 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1840 bool isScalable = false;
1844 // Get the path to the font file name.
1846 GetFcString(match, FC_FILE, path);
1847 isScalable = IsScalable(path);
1851 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1854 // Destroys the created patterns.
1855 FcPatternDestroy(match);
1856 FcPatternDestroy(fontFamilyPattern);
1861 void FontClient::Plugin::GetFixedSizes(const FontPath& path, Vector<PointSize26Dot6>& sizes)
1863 // Empty the caller container
1867 int error = FT_New_Face(mFreeTypeLibrary,
1871 if(FT_Err_Ok != error)
1873 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font path : [%s]\n", path.c_str());
1876 // Fetch the number of fixed sizes available
1877 if(ftFace->num_fixed_sizes && ftFace->available_sizes)
1879 for(int i = 0; i < ftFace->num_fixed_sizes; ++i)
1881 sizes.PushBack(ftFace->available_sizes[i].size);
1886 void FontClient::Plugin::GetFixedSizes(const FontDescription& fontDescription,
1887 Vector<PointSize26Dot6>& sizes)
1889 // Create a font pattern.
1890 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1892 FcResult result = FcResultMatch;
1894 // match the pattern
1895 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1899 // Get the path to the font file name.
1901 GetFcString(match, FC_FILE, path);
1902 GetFixedSizes(path, sizes);
1906 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1909 // Destroys the created patterns.
1910 FcPatternDestroy(match);
1911 FcPatternDestroy(fontFamilyPattern);
1914 bool FontClient::Plugin::HasItalicStyle(FontId fontId) const
1916 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1917 if(fontCacheItem != nullptr)
1919 return fontCacheItem->HasItalicStyle();
1924 void FontClient::Plugin::CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path)
1926 FontDescription description;
1927 description.path = path;
1928 description.family = std::move(FontFamily(ftFace->family_name));
1929 description.weight = FontWeight::NONE;
1930 description.width = FontWidth::NONE;
1931 description.slant = FontSlant::NONE;
1933 // Note FreeType doesn't give too much info to build a proper font style.
1934 if(ftFace->style_flags & FT_STYLE_FLAG_ITALIC)
1936 description.slant = FontSlant::ITALIC;
1938 if(ftFace->style_flags & FT_STYLE_FLAG_BOLD)
1940 description.weight = FontWeight::BOLD;
1943 FontDescriptionId validatedFontId = 0u;
1944 if(!FindValidatedFont(description,
1947 FcPattern* pattern = CreateFontFamilyPattern(description); // Creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
1949 FcResult result = FcResultMatch;
1950 FcPattern* match = FcFontMatch(nullptr, pattern, &result); // FcFontMatch creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
1952 FcCharSet* characterSet = nullptr;
1953 FcPatternGetCharSet(match, FC_CHARSET, 0u, &characterSet);
1955 const FontId fontFaceId = id - 1u;
1956 mFontFaceCache[fontFaceId].mCharacterSet = FcCharSetCopy(characterSet); // Increases the reference counter.
1958 // Destroys the created patterns.
1959 FcPatternDestroy(match);
1960 FcPatternDestroy(pattern);
1962 // Add the path to the cache.
1963 description.type = FontDescription::FACE_FONT;
1964 mFontDescriptionCache.push_back(description);
1966 // Set the index to the vector of paths to font file names.
1967 validatedFontId = mFontDescriptionCache.size();
1969 // Increase the reference counter and add the character set to the cache.
1970 mCharacterSetCache.PushBack(FcCharSetCopy(characterSet));
1972 // Cache the index and the font's description.
1973 mValidatedFontCache.push_back(std::move(FontDescriptionCacheItem(std::move(description),
1976 // Cache the pair 'validatedFontId, requestedPointSize' to improve the following queries.
1977 mFontDescriptionSizeCache.push_back(FontDescriptionSizeCacheItem(validatedFontId,
1983 void FontClient::Plugin::ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache)
1985 for(auto& item : fallbackCache)
1987 if(nullptr != item.fallbackFonts)
1989 delete item.fallbackFonts;
1992 if(nullptr != item.characterSets)
1994 // Free the resources allocated by the FcCharSet objects in the 'characterSets' vector.
1995 DestroyCharacterSets(*item.characterSets);
1996 delete item.characterSets;
2001 void FontClient::Plugin::ClearCharacterSetFromFontFaceCache()
2003 for(auto& item : mFontFaceCache)
2005 FcCharSetDestroy(item.mCharacterSet);
2006 item.mCharacterSet = nullptr;
2010 } // namespace Dali::TextAbstraction::Internal