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>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/internal/adaptor/common/adaptor-impl.h>
26 #include <dali/internal/imaging/common/image-operations.h>
27 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
28 #include <dali/internal/text/text-abstraction/plugin/embedded-item.h>
29 #include <dali/internal/text/text-abstraction/plugin/font-client-plugin-cache-handler.h>
30 #include <dali/internal/text/text-abstraction/plugin/font-client-utils.h>
31 #include <dali/internal/text/text-abstraction/plugin/font-face-cache-item.h>
32 #include <dali/public-api/common/dali-vector.h>
33 #include <dali/public-api/common/vector-wrapper.h>
36 #include <fontconfig/fontconfig.h>
40 #if defined(DEBUG_ENABLED)
42 // Note, to turn on trace and verbose logging, use "export LOG_FONT_CLIENT=3,true"
43 // Or re-define the following filter using Verbose,true instead of NoLogging,false,
44 // Or, add DALI_LOG_FILTER_ENABLE_TRACE(gFontClientLogFilter) in the code below.
46 Dali::Integration::Log::Filter* gFontClientLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_FONT_CLIENT");
48 #define FONT_LOG_DESCRIPTION(fontDescription, prefix) \
49 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, #prefix " description; family : [%s]\n", fontDescription.family.c_str()); \
50 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, \
54 " slant : [%s]\n\n", \
55 fontDescription.path.c_str(), \
56 FontWidth::Name[fontDescription.width], \
57 FontWeight::Name[fontDescription.weight], \
58 FontSlant::Name[fontDescription.slant])
60 #define FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor) \
61 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, \
63 " requestedPointSize : %d\n" \
64 " preferColor : %s\n", \
67 (preferColor ? "true" : "false"))
71 #define FONT_LOG_DESCRIPTION(fontDescription, prefix)
72 #define FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor)
79 * Conversion from Fractional26.6 to float
81 const float FROM_266 = 1.0f / 64.0f;
82 const float POINTS_PER_INCH = 72.f;
84 const uint32_t ELLIPSIS_CHARACTER = 0x2026;
90 namespace Dali::TextAbstraction::Internal
95 * @brief Check if @p ftFace and @p requestedPointSize produces block that fit into atlas block
97 * @param[in/out] ftFace Face type object.
98 * @param[in] horizontalDpi The horizontal dpi.
99 * @param[in] verticalDpi The vertical dpi.
100 * @param[in] maxSizeFitInAtlas The maximum size of block to fit into atlas
101 * @param[in] requestedPointSize The requested point-size.
102 * @return whether the ftFace's block can fit into atlas
104 bool IsFitIntoAtlas(FT_Face& ftFace, int& error, const unsigned int& horizontalDpi, const unsigned int& verticalDpi, const Size& maxSizeFitInAtlas, const uint32_t& requestedPointSize)
108 error = FT_Set_Char_Size(ftFace,
114 if(error == FT_Err_Ok)
116 //Check width and height of block for requestedPointSize
117 //If the width or height is greater than the maximum-size then decrement by one unit of point-size.
118 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)
128 * @brief Search on proper @p requestedPointSize that produces block that fit into atlas block considering on @p ftFace, @p horizontalDpi, and @p verticalDpi
130 * @param[in/out] ftFace Face type object.
131 * @param[in] horizontalDpi The horizontal dpi.
132 * @param[in] verticalDpi The vertical dpi.
133 * @param[in] maxSizeFitInAtlas The maximum size of block to fit into atlas
134 * @param[in/out] requestedPointSize The requested point-size.
135 * @return FreeType error code. 0 means success when requesting the nominal size (in points).
137 int SearchOnProperPointSize(FT_Face& ftFace, const unsigned int& horizontalDpi, const unsigned int& verticalDpi, const Size& maxSizeFitInAtlas, uint32_t& requestedPointSize)
139 //To improve performance of sequential search. This code is applying Exponential search then followed by Binary search.
140 const uint32_t& pointSizePerOneUnit = TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
142 int error; // FreeType error code.
144 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
145 if(FT_Err_Ok != error)
153 uint32_t exponentialDecrement = 1;
155 while(!canFitInAtlas && requestedPointSize > pointSizePerOneUnit * exponentialDecrement)
157 requestedPointSize -= (pointSizePerOneUnit * exponentialDecrement);
158 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
159 if(FT_Err_Ok != error)
164 exponentialDecrement *= 2;
168 uint32_t minPointSize;
169 uint32_t maxPointSize;
173 exponentialDecrement /= 2;
174 minPointSize = requestedPointSize;
175 maxPointSize = requestedPointSize + (pointSizePerOneUnit * exponentialDecrement);
180 maxPointSize = requestedPointSize;
183 while(minPointSize < maxPointSize)
185 requestedPointSize = ((maxPointSize / pointSizePerOneUnit - minPointSize / pointSizePerOneUnit) / 2) * pointSizePerOneUnit + minPointSize;
186 canFitInAtlas = IsFitIntoAtlas(ftFace, error, horizontalDpi, verticalDpi, maxSizeFitInAtlas, requestedPointSize);
187 if(FT_Err_Ok != error)
194 if(minPointSize == requestedPointSize)
196 //Found targeted point-size
200 minPointSize = requestedPointSize;
204 maxPointSize = requestedPointSize;
214 FontClient::Plugin::Plugin(unsigned int horizontalDpi,
215 unsigned int verticalDpi)
216 : mFreeTypeLibrary(nullptr),
217 mDpiHorizontal(horizontalDpi),
218 mDpiVertical(verticalDpi),
219 mIsAtlasLimitationEnabled(TextAbstraction::FontClient::DEFAULT_ATLAS_LIMITATION_ENABLED),
220 mCurrentMaximumBlockSizeFitInAtlas(TextAbstraction::FontClient::MAX_SIZE_FIT_IN_ATLAS),
221 mVectorFontCache(nullptr),
222 mCacheHandler(new CacheHandler())
224 int error = FT_Init_FreeType(&mFreeTypeLibrary);
225 if(FT_Err_Ok != error)
227 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Init error: %d\n", error);
230 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
231 mVectorFontCache = new VectorFontCache(mFreeTypeLibrary);
235 FontClient::Plugin::~Plugin()
237 // Delete cache hanlder before remove mFreeTypeLibrary
238 delete mCacheHandler;
240 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
241 delete mVectorFontCache;
244 FT_Done_FreeType(mFreeTypeLibrary);
247 void FontClient::Plugin::ClearCache() const
249 mCacheHandler->ClearCache();
252 void FontClient::Plugin::SetDpi(unsigned int horizontalDpi,
253 unsigned int verticalDpi)
255 mDpiHorizontal = horizontalDpi;
256 mDpiVertical = verticalDpi;
259 void FontClient::Plugin::ResetSystemDefaults() const
261 mCacheHandler->ResetSystemDefaults();
264 void FontClient::Plugin::GetDefaultPlatformFontDescription(FontDescription& fontDescription) const
266 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
268 mCacheHandler->InitDefaultFontDescription();
269 fontDescription = mCacheHandler->mDefaultFontDescription;
271 FONT_LOG_DESCRIPTION(fontDescription, "");
274 void FontClient::Plugin::GetDefaultFonts(FontList& defaultFonts) const
276 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
278 mCacheHandler->InitDefaultFonts();
279 defaultFonts = mCacheHandler->mDefaultFonts;
281 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of default fonts : [%d]\n", mCacheHandler->mDefaultFonts.size());
284 void FontClient::Plugin::GetSystemFonts(FontList& systemFonts) const
286 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
288 mCacheHandler->InitSystemFonts();
289 systemFonts = mCacheHandler->mSystemFonts;
291 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of system fonts : [%d]\n", mCacheHandler->mSystemFonts.size());
294 void FontClient::Plugin::GetDescription(FontId fontId,
295 FontDescription& fontDescription) const
297 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
298 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
300 if((fontId > 0u) && (fontId - 1u < mCacheHandler->mFontIdCache.size()))
302 const auto& fontIdCacheItem = mCacheHandler->mFontIdCache[fontId - 1u];
303 switch(fontIdCacheItem.type)
305 case FontDescription::FACE_FONT:
307 for(const auto& item : mCacheHandler->mFontDescriptionSizeCache)
309 if(item.second == fontIdCacheItem.index)
311 fontDescription = *(mCacheHandler->mFontDescriptionCache.begin() + item.first.fontDescriptionId - 1u);
313 FONT_LOG_DESCRIPTION(fontDescription, "");
319 case FontDescription::BITMAP_FONT:
321 fontDescription.type = FontDescription::BITMAP_FONT;
322 fontDescription.family = mCacheHandler->mBitmapFontCache[fontIdCacheItem.index].font.name;
327 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
328 fontDescription.type = FontDescription::INVALID;
329 fontDescription.family.clear();
334 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " No description found for the font id %d\n", fontId);
337 PointSize26Dot6 FontClient::Plugin::GetPointSize(FontId fontId) const
339 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
340 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
342 PointSize26Dot6 pointSize = TextAbstraction::FontClient::DEFAULT_POINT_SIZE;
343 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
344 if(fontCacheItem != nullptr)
346 pointSize = fontCacheItem->GetPointSize();
348 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " point size : %d\n", pointSize);
353 bool FontClient::Plugin::IsCharacterSupportedByFont(FontId fontId, Character character) const
355 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
356 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
357 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
359 bool isSupported = false;
360 auto fontCacheItem = const_cast<FontCacheItemInterface*>(GetCachedFontItem(fontId));
361 if(fontCacheItem != nullptr)
363 isSupported = fontCacheItem->IsCharacterSupported(character); // May cache
366 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " is supported : %s\n", (isSupported ? "true" : "false"));
370 const FontCacheItemInterface* FontClient::Plugin::GetCachedFontItem(FontId fontId) const
372 if((fontId > 0u) && (fontId - 1u < mCacheHandler->mFontIdCache.size()))
374 const auto& fontIdCacheItem = mCacheHandler->mFontIdCache[fontId - 1u];
375 switch(fontIdCacheItem.type)
377 case FontDescription::FACE_FONT:
379 return &mCacheHandler->mFontFaceCache[fontIdCacheItem.index];
381 case FontDescription::BITMAP_FONT:
383 return &mCacheHandler->mBitmapFontCache[fontIdCacheItem.index];
387 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Invalid type of font\n");
394 FontId FontClient::Plugin::FindFontForCharacter(const FontList& fontList,
395 const CharacterSetList& characterSetList,
397 PointSize26Dot6 requestedPointSize,
398 bool preferColor) const
400 DALI_ASSERT_DEBUG((fontList.size() == characterSetList.Count()) && "FontClient::Plugin::FindFontForCharacter. Different number of fonts and character sets.");
401 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
402 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " character : %p\n", character);
403 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
404 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferColor : %s\n", (preferColor ? "true" : "false"));
407 bool foundColor = false;
409 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " number of fonts : %d\n", fontList.size());
411 // Traverse the list of fonts.
412 // Check for each font if supports the character.
413 for(unsigned int index = 0u, numberOfFonts = fontList.size(); index < numberOfFonts; ++index)
415 const FontDescription& description = fontList[index];
416 const FcCharSet* const characterSet = characterSetList[index];
418 FONT_LOG_DESCRIPTION(description, "");
420 bool foundInRanges = false;
421 if(nullptr != characterSet)
423 foundInRanges = FcCharSetHasChar(characterSet, character);
428 fontId = GetFontId(description, requestedPointSize, 0u);
430 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " font id : %d\n", fontId);
434 if((fontId > 0) && (fontId - 1u < mCacheHandler->mFontIdCache.size()))
436 const FontFaceCacheItem& item = mCacheHandler->mFontFaceCache[mCacheHandler->mFontIdCache[fontId - 1u].index];
438 foundColor = item.mHasColorTables;
441 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " foundColor : %s\n", (foundColor ? "true" : "false"));
444 // Keep going unless we prefer a different (color) font.
445 if(!preferColor || foundColor)
452 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
456 FontId FontClient::Plugin::FindDefaultFont(Character charcode,
457 PointSize26Dot6 requestedPointSize,
458 bool preferColor) const
460 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
461 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
465 // Create the list of default fonts if it has not been created.
466 mCacheHandler->InitDefaultFonts();
467 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " number of default fonts : %d\n", mCacheHandler->mDefaultFonts.size());
469 // Traverse the list of default fonts.
470 // Check for each default font if supports the character.
471 fontId = FindFontForCharacter(mCacheHandler->mDefaultFonts, mCacheHandler->mDefaultFontCharacterSets, charcode, requestedPointSize, preferColor);
473 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
477 FontId FontClient::Plugin::FindFallbackFont(Character charcode,
478 const FontDescription& preferredFontDescription,
479 PointSize26Dot6 requestedPointSize,
480 bool preferColor) const
482 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
483 FONT_LOG_REQUEST(charcode, requestedPointSize, preferColor);
485 // The font id to be returned.
488 FontDescription fontDescription;
490 // Fill the font description with the preferred font description and complete with the defaults.
491 fontDescription.family = preferredFontDescription.family.empty() ? DefaultFontFamily() : preferredFontDescription.family;
492 fontDescription.weight = ((FontWeight::NONE == preferredFontDescription.weight) ? DefaultFontWeight() : preferredFontDescription.weight);
493 fontDescription.width = ((FontWidth::NONE == preferredFontDescription.width) ? DefaultFontWidth() : preferredFontDescription.width);
494 fontDescription.slant = ((FontSlant::NONE == preferredFontDescription.slant) ? DefaultFontSlant() : preferredFontDescription.slant);
496 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " preferredFontDescription --> fontDescription\n");
497 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " [%s] --> [%s]\n", preferredFontDescription.family.c_str(), fontDescription.family.c_str());
498 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWeight::Name[preferredFontDescription.weight], FontWeight::Name[fontDescription.weight]);
499 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontWidth::Name[preferredFontDescription.width], FontWidth::Name[fontDescription.width]);
500 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " [%s] --> [%s]\n", FontSlant::Name[preferredFontDescription.slant], FontSlant::Name[fontDescription.slant]);
502 // Check first if the font's description has been queried before.
503 FontList* fontList = nullptr;
504 CharacterSetList* characterSetList = nullptr;
506 if(!mCacheHandler->FindFallbackFontList(fontDescription, fontList, characterSetList))
508 mCacheHandler->CacheFallbackFontList(std::move(fontDescription), fontList, characterSetList);
511 if(fontList && characterSetList)
513 fontId = FindFontForCharacter(*fontList, *characterSetList, charcode, requestedPointSize, preferColor);
516 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
520 FontId FontClient::Plugin::GetFontIdByPath(const FontPath& path,
521 PointSize26Dot6 requestedPointSize,
523 bool cacheDescription) const
525 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
526 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
527 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
531 if(nullptr != mFreeTypeLibrary)
534 if(mCacheHandler->FindFontByPath(path, requestedPointSize, faceIndex, foundId))
540 id = CreateFont(path, requestedPointSize, faceIndex, cacheDescription);
544 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", id);
548 FontId FontClient::Plugin::GetFontId(const FontDescription& fontDescription,
549 PointSize26Dot6 requestedPointSize,
550 FaceIndex faceIndex) const
552 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
553 FONT_LOG_DESCRIPTION(fontDescription, "");
555 // Special case when font Description don't have family information.
556 // In this case, we just use default description family and path.
557 const FontDescription& realFontDescription = fontDescription.family.empty() ? FontDescription(mCacheHandler->mDefaultFontDescription.path,
558 mCacheHandler->mDefaultFontDescription.family,
559 fontDescription.width,
560 fontDescription.weight,
561 fontDescription.slant,
562 fontDescription.type)
565 FONT_LOG_DESCRIPTION(realFontDescription, "");
566 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
568 // This method uses three vectors which caches:
569 // * The bitmap font cache
570 // * Pairs of non validated font descriptions and an index to a vector with paths to font file names.
571 // * The path to font file names.
572 // * The font ids of pairs 'font point size, index to the vector with paths to font file names'.
574 // 1) Checks if the font description matches with a previously loaded bitmap font.
575 // Returns if a font is found.
576 // 2) Checks in the cache if the font's description has been validated before.
577 // If it was it gets an index to the vector with paths to font file names. Otherwise,
578 // retrieves using font config a path to a font file name which matches with the
579 // font's description. The path is stored in the cache.
581 // 3) Checks in the cache if the pair 'font point size, index to the vector with paths to
582 // font file names' exists. If exists, it gets the font id. If it doesn't it calls
583 // the GetFontId() method with the path to the font file name and the point size to
586 // The font id to be returned.
589 // Check first if the font description matches with a previously loaded bitmap font.
590 if(mCacheHandler->FindBitmapFont(realFontDescription.family, fontId))
595 // Check if the font's description have been validated before.
596 FontDescriptionId fontDescriptionId = 0u;
598 if(!mCacheHandler->FindValidatedFont(realFontDescription, fontDescriptionId))
600 // Use font config to validate the font's description.
601 mCacheHandler->ValidateFont(realFontDescription, fontDescriptionId);
604 using FontCacheIndex = CacheHandler::FontCacheIndex;
605 FontCacheIndex fontCacheIndex = 0u;
606 // Check if exists a pair 'fontDescriptionId, requestedPointSize' in the cache.
607 if(!mCacheHandler->FindFont(fontDescriptionId, requestedPointSize, fontCacheIndex))
609 // Retrieve the font file name path.
610 const FontDescription& description = *(mCacheHandler->mFontDescriptionCache.begin() + fontDescriptionId - 1u);
612 // Retrieve the font id. Do not cache the description as it has been already cached.
613 // Note : CacheFontPath() API call ValidateFont() + setup CharacterSet + cache the font description.
614 // So set cacheDescription=false, that we don't call CacheFontPath().
615 fontId = GetFontIdByPath(description.path, requestedPointSize, faceIndex, false);
617 if((fontId > 0u) && (fontId - 1u < mCacheHandler->mFontIdCache.size()))
619 fontCacheIndex = mCacheHandler->mFontIdCache[fontId - 1u].index;
620 mCacheHandler->mFontFaceCache[fontCacheIndex].mCharacterSet = FcCharSetCopy(mCacheHandler->mCharacterSetCache[fontDescriptionId - 1u]);
622 // Cache the pair 'fontDescriptionId, requestedPointSize' to improve the following queries.
623 mCacheHandler->CacheFontDescriptionSize(fontDescriptionId, requestedPointSize, fontCacheIndex);
628 fontId = mCacheHandler->mFontFaceCache[fontCacheIndex].mFontId + 1u;
631 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
635 FontId FontClient::Plugin::GetFontId(const BitmapFont& bitmapFont) const
637 // The font id to be returned.
639 if(!mCacheHandler->FindBitmapFont(bitmapFont.name, fontId))
641 BitmapFontCacheItem bitmapFontCacheItem(bitmapFont);
643 fontId = mCacheHandler->CacheBitmapFontCacheItem(std::move(bitmapFontCacheItem));
648 void FontClient::Plugin::GetFontMetrics(FontId fontId,
649 FontMetrics& metrics) const
651 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
652 if(fontCacheItem != nullptr)
654 fontCacheItem->GetFontMetrics(metrics, mDpiVertical);
658 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
659 Character charcode) const
661 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
662 if(fontCacheItem != nullptr)
664 return fontCacheItem->GetGlyphIndex(charcode);
670 GlyphIndex FontClient::Plugin::GetGlyphIndex(FontId fontId,
672 Character variantSelector) const
674 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
675 if(fontCacheItem != nullptr)
677 return fontCacheItem->GetGlyphIndex(charcode, variantSelector);
683 bool FontClient::Plugin::GetGlyphMetrics(GlyphInfo* array,
686 bool horizontal) const
688 if(VECTOR_GLYPH == type)
690 return GetVectorMetrics(array, size, horizontal);
693 return GetBitmapMetrics(array, size, horizontal);
696 bool FontClient::Plugin::GetBitmapMetrics(GlyphInfo* array,
698 bool horizontal) const
700 bool success(size > 0u);
702 for(unsigned int i = 0; i < size; ++i)
704 GlyphInfo& glyph = array[i];
706 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(glyph.fontId);
707 if(fontCacheItem != nullptr)
709 success &= fontCacheItem->GetGlyphMetrics(glyph, mDpiVertical, horizontal);
711 // Check if it's an embedded image.
712 else if((0u == glyph.fontId) && (0u != glyph.index) && (glyph.index <= mCacheHandler->mEmbeddedItemCache.size()))
714 mCacheHandler->mEmbeddedItemCache[glyph.index - 1u].GetGlyphMetrics(glyph);
725 bool FontClient::Plugin::GetVectorMetrics(GlyphInfo* array,
727 bool horizontal) const
729 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
732 for(unsigned int i = 0u; i < size; ++i)
734 FontId fontId = array[i].fontId;
737 (fontId - 1u) < mCacheHandler->mFontIdCache.size())
739 FontFaceCacheItem& font = mCacheHandler->mFontFaceCache[mCacheHandler->mFontIdCache[fontId - 1u].index];
741 if(!font.mVectorFontId)
743 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
746 mVectorFontCache->GetGlyphMetrics(font.mVectorFontId, array[i]);
748 // Vector metrics are in EMs, convert to pixels
749 const float scale = (static_cast<float>(font.mRequestedPointSize) * FROM_266) * static_cast<float>(mDpiVertical) / POINTS_PER_INCH;
750 array[i].width *= scale;
751 array[i].height *= scale;
752 array[i].xBearing *= scale;
753 array[i].yBearing *= scale;
754 array[i].advance *= scale;
768 void FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth) const
770 data.isColorBitmap = false;
771 data.isColorEmoji = false;
772 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
773 if(fontCacheItem != nullptr)
775 fontCacheItem->CreateBitmap(glyphIndex, data, outlineWidth, isItalicRequired, isBoldRequired);
777 else if((0u != glyphIndex) && (glyphIndex <= mCacheHandler->mEmbeddedItemCache.size()))
779 // It's an embedded item.
780 mCacheHandler->mEmbeddedItemCache[glyphIndex - 1u].CreateBitmap(mCacheHandler->mPixelBufferCache, data);
784 PixelData FontClient::Plugin::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth) const
786 TextAbstraction::FontClient::GlyphBufferData data;
788 CreateBitmap(fontId, glyphIndex, false, false, data, outlineWidth);
790 // If data is compressed or not owned buffer, copy this.
791 if(!data.isBufferOwned || data.compressionType != TextAbstraction::FontClient::GlyphBufferData::CompressionType::NO_COMPRESSION)
793 uint8_t* newBuffer = (uint8_t*)malloc(data.width * data.height * Pixel::GetBytesPerPixel(data.format));
794 TextAbstraction::FontClient::GlyphBufferData::Decompress(data, newBuffer);
795 if(data.isBufferOwned)
800 data.buffer = newBuffer;
801 data.isBufferOwned = true;
802 data.compressionType = TextAbstraction::FontClient::GlyphBufferData::CompressionType::NO_COMPRESSION;
805 return PixelData::New(data.buffer,
806 data.width * data.height * Pixel::GetBytesPerPixel(data.format),
813 void FontClient::Plugin::CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight) const
818 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
820 (fontId - 1u < mCacheHandler->mFontIdCache.size()))
822 using FontCacheIndex = CacheHandler::FontCacheIndex;
823 const FontCacheIndex fontCacheIndex = mCacheHandler->mFontIdCache[fontId - 1u].index;
824 FontFaceCacheItem& font = mCacheHandler->mFontFaceCache[fontCacheIndex];
826 if(!font.mVectorFontId)
828 font.mVectorFontId = mVectorFontCache->GetFontId(font.mPath);
831 mVectorFontCache->GetVectorBlob(font.mVectorFontId, fontCacheIndex, glyphIndex, blob, blobLength, nominalWidth, nominalHeight);
836 const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph(PointSize26Dot6 requestedPointSize) const
838 using EllipsisCacheIndex = CacheHandler::EllipsisCacheIndex;
839 using EllipsisItem = CacheHandler::EllipsisItem;
840 EllipsisCacheIndex ellipsisCacheIndex = 0u;
842 if(!mCacheHandler->FindEllipsis(requestedPointSize, ellipsisCacheIndex))
844 // No glyph has been found. Create one.
847 item.requestedPointSize = requestedPointSize;
848 item.index = ellipsisCacheIndex;
850 // Find a font for the ellipsis glyph.
851 item.glyph.fontId = FindDefaultFont(ELLIPSIS_CHARACTER,
855 // Set the character index to access the glyph inside the font.
856 item.glyph.index = GetGlyphIndex(item.glyph.fontId, ELLIPSIS_CHARACTER);
858 // Get glyph informations.
859 GetBitmapMetrics(&item.glyph, 1u, true);
861 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " glyph id %d found in the cache.\n", item.glyph.index);
862 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font %d.\n", item.glyph.fontId);
864 ellipsisCacheIndex = mCacheHandler->CacheEllipsis(std::move(item));
866 return mCacheHandler->mEllipsisCache[ellipsisCacheIndex].glyph;
869 bool FontClient::Plugin::IsColorGlyph(FontId fontId, GlyphIndex glyphIndex) const
871 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
872 return fontCacheItem && fontCacheItem->IsColorGlyph(glyphIndex);
875 FT_FaceRec_* FontClient::Plugin::GetFreetypeFace(FontId fontId) const
877 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
878 if(fontCacheItem != nullptr)
880 return fontCacheItem->GetTypeface();
885 FontDescription::Type FontClient::Plugin::GetFontType(FontId fontId) const
887 const FontId index = fontId - 1u;
888 if((fontId > 0u) && (index < mCacheHandler->mFontIdCache.size()))
890 return mCacheHandler->mFontIdCache[index].type;
892 return FontDescription::INVALID;
895 bool FontClient::Plugin::AddCustomFontDirectory(const FontPath& path)
897 // nullptr as first parameter means the current configuration is used.
898 return FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>(path.c_str()));
901 HarfBuzzFontHandle FontClient::Plugin::GetHarfBuzzFont(FontId fontId) const
903 FontCacheItemInterface* fontCacheItem = const_cast<FontCacheItemInterface*>(GetCachedFontItem(fontId));
904 if(fontCacheItem != nullptr)
906 return fontCacheItem->GetHarfBuzzFont(mDpiHorizontal, mDpiVertical); // May cache
911 GlyphIndex FontClient::Plugin::CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat) const
913 EmbeddedItem embeddedItem;
915 embeddedItem.pixelBufferId = 0u;
916 embeddedItem.width = description.width;
917 embeddedItem.height = description.height;
919 pixelFormat = Pixel::A8;
921 if(!description.url.empty())
923 // Check if the url is in the cache.
924 Devel::PixelBuffer pixelBuffer;
925 if(!mCacheHandler->FindEmbeddedPixelBufferId(description.url, embeddedItem.pixelBufferId))
927 // The pixel buffer is not in the cache. Create one and cache it.
928 embeddedItem.pixelBufferId = mCacheHandler->CacheEmbeddedPixelBuffer(description.url);
931 if((embeddedItem.pixelBufferId > 0u) && (embeddedItem.pixelBufferId - 1u) < mCacheHandler->mPixelBufferCache.size())
933 // Retrieve the pixel buffer from the cache to set the pixel format.
934 pixelBuffer = mCacheHandler->mPixelBufferCache[embeddedItem.pixelBufferId - 1u].pixelBuffer;
939 // Set the size of the embedded item if it has not been set.
940 if(0u == embeddedItem.width)
942 embeddedItem.width = static_cast<unsigned int>(pixelBuffer.GetWidth());
945 if(0u == embeddedItem.height)
947 embeddedItem.height = static_cast<unsigned int>(pixelBuffer.GetHeight());
950 // Set the pixel format.
951 pixelFormat = pixelBuffer.GetPixelFormat();
955 // Find if the same embeddedItem has already been created.
956 GlyphIndex index = 0u;
957 if(!mCacheHandler->FindEmbeddedItem(embeddedItem.pixelBufferId, embeddedItem.width, embeddedItem.height, index))
959 index = mCacheHandler->CacheEmbeddedItem(std::move(embeddedItem));
964 void FontClient::Plugin::EnableAtlasLimitation(bool enabled)
966 mIsAtlasLimitationEnabled = enabled;
969 bool FontClient::Plugin::IsAtlasLimitationEnabled() const
971 return mIsAtlasLimitationEnabled;
974 Size FontClient::Plugin::GetMaximumTextAtlasSize() const
976 return TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
979 Size FontClient::Plugin::GetDefaultTextAtlasSize() const
981 return TextAbstraction::FontClient::DEFAULT_TEXT_ATLAS_SIZE;
984 Size FontClient::Plugin::GetCurrentMaximumBlockSizeFitInAtlas() const
986 return mCurrentMaximumBlockSizeFitInAtlas;
989 bool FontClient::Plugin::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
991 bool isChanged = false;
992 const Size& maxTextAtlasSize = TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
993 const uint16_t& padding = TextAbstraction::FontClient::PADDING_TEXT_ATLAS_BLOCK;
995 if(currentMaximumBlockSizeFitInAtlas.width <= maxTextAtlasSize.width - padding && currentMaximumBlockSizeFitInAtlas.height <= maxTextAtlasSize.height - padding)
997 mCurrentMaximumBlockSizeFitInAtlas = currentMaximumBlockSizeFitInAtlas;
1004 uint32_t FontClient::Plugin::GetNumberOfPointsPerOneUnitOfPointSize() const
1006 return TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
1010 FontId FontClient::Plugin::CreateFont(const FontPath& path,
1011 PointSize26Dot6 requestedPointSize,
1012 FaceIndex faceIndex,
1013 bool cacheDescription) const
1015 DALI_LOG_TRACE_METHOD(gFontClientLogFilter);
1016 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " path : [%s]\n", path.c_str());
1017 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " requestedPointSize : %d\n", requestedPointSize);
1021 // Create & cache new font face
1023 int error = FT_New_Face(mFreeTypeLibrary,
1028 if(FT_Err_Ok == error)
1030 // Check if a font is scalable.
1031 const bool isScalable = (0 != (ftFace->face_flags & FT_FACE_FLAG_SCALABLE));
1032 const bool hasFixedSizedBitmaps = (0 != (ftFace->face_flags & FT_FACE_FLAG_FIXED_SIZES)) && (0 != ftFace->num_fixed_sizes);
1033 const bool hasColorTables = (0 != (ftFace->face_flags & FT_FACE_FLAG_COLOR));
1035 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " isScalable : [%s]\n", (isScalable ? "true" : "false"));
1036 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasFixedSizedBitmaps : [%s]\n", (hasFixedSizedBitmaps ? "true" : "false"));
1037 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " hasColorTables : [%s]\n", (hasColorTables ? "true" : "false"));
1039 // Check to see if the font contains fixed sizes?
1040 if(!isScalable && hasFixedSizedBitmaps)
1042 PointSize26Dot6 actualPointSize = 0u;
1043 int fixedSizeIndex = 0;
1044 for(; fixedSizeIndex < ftFace->num_fixed_sizes; ++fixedSizeIndex)
1046 const PointSize26Dot6 fixedSize = static_cast<PointSize26Dot6>(ftFace->available_sizes[fixedSizeIndex].size);
1047 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, size : %d\n", fixedSizeIndex, fixedSize);
1049 if(fixedSize >= requestedPointSize)
1051 actualPointSize = fixedSize;
1056 if(0u == actualPointSize)
1058 // The requested point size is bigger than the bigest fixed size.
1059 fixedSizeIndex = ftFace->num_fixed_sizes - 1;
1060 actualPointSize = static_cast<PointSize26Dot6>(ftFace->available_sizes[fixedSizeIndex].size);
1063 DALI_LOG_INFO(gFontClientLogFilter, Debug::Verbose, " size index : %d, actual size : %d\n", fixedSizeIndex, actualPointSize);
1065 // Tell Freetype to use this size
1066 error = FT_Select_Size(ftFace, fixedSizeIndex);
1067 if(FT_Err_Ok != error)
1069 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FreeType Select_Size error: %d\n", error);
1073 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1075 FontMetrics metrics(static_cast<float>(ftMetrics.ascender) * FROM_266,
1076 static_cast<float>(ftMetrics.descender) * FROM_266,
1077 static_cast<float>(ftMetrics.height) * FROM_266,
1078 static_cast<float>(ftFace->underline_position) * FROM_266,
1079 static_cast<float>(ftFace->underline_thickness) * FROM_266);
1081 const float fixedWidth = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].width);
1082 const float fixedHeight = static_cast<float>(ftFace->available_sizes[fixedSizeIndex].height);
1084 // Create the FreeType font face item to cache.
1085 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, mCacheHandler->GetGlyphCacheManager(), path, requestedPointSize, faceIndex, metrics, fixedSizeIndex, fixedWidth, fixedHeight, hasColorTables);
1087 fontId = mCacheHandler->CacheFontFaceCacheItem(std::move(fontFaceCacheItem));
1092 if(mIsAtlasLimitationEnabled)
1094 //There is limitation on block size to fit in predefined atlas size.
1095 //If the block size cannot fit into atlas size, then the system cannot draw block.
1096 //This is workaround to avoid issue in advance
1097 //Decrementing point-size until arriving to maximum allowed block size.
1098 auto requestedPointSizeBackup = requestedPointSize;
1099 const Size& maxSizeFitInAtlas = GetCurrentMaximumBlockSizeFitInAtlas();
1100 error = SearchOnProperPointSize(ftFace, mDpiHorizontal, mDpiVertical, maxSizeFitInAtlas, requestedPointSize);
1102 if(requestedPointSize != requestedPointSizeBackup)
1104 DALI_LOG_WARNING(" The requested-point-size : %d, is reduced to point-size : %d\n", requestedPointSizeBackup, requestedPointSize);
1109 error = FT_Set_Char_Size(ftFace,
1116 if(FT_Err_Ok == error)
1118 FT_Size_Metrics& ftMetrics = ftFace->size->metrics;
1120 FontMetrics metrics(static_cast<float>(ftMetrics.ascender) * FROM_266,
1121 static_cast<float>(ftMetrics.descender) * FROM_266,
1122 static_cast<float>(ftMetrics.height) * FROM_266,
1123 static_cast<float>(ftFace->underline_position) * FROM_266,
1124 static_cast<float>(ftFace->underline_thickness) * FROM_266);
1126 // Create the FreeType font face item to cache.
1127 FontFaceCacheItem fontFaceCacheItem(mFreeTypeLibrary, ftFace, mCacheHandler->GetGlyphCacheManager(), path, requestedPointSize, faceIndex, metrics);
1129 fontId = mCacheHandler->CacheFontFaceCacheItem(std::move(fontFaceCacheItem));
1133 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType Set_Char_Size error: %d for pointSize %d\n", error, requestedPointSize);
1139 if(cacheDescription)
1141 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " Cache Font Path at font id : %d [%s]\n", fontId, path.c_str());
1142 mCacheHandler->CacheFontPath(ftFace, fontId, requestedPointSize, path);
1148 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " FreeType New_Face error: %d for [%s]\n", error, path.c_str());
1151 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, " font id : %d\n", fontId);
1155 bool FontClient::Plugin::IsScalable(const FontPath& path) const
1157 bool isScalable = false;
1159 FT_Face ftFace = nullptr;
1160 int error = FT_New_Face(mFreeTypeLibrary,
1164 if(FT_Err_Ok != error)
1166 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: %s\n", path.c_str());
1170 isScalable = ftFace->face_flags & FT_FACE_FLAG_SCALABLE;
1175 FT_Done_Face(ftFace);
1181 bool FontClient::Plugin::IsScalable(const FontDescription& fontDescription) const
1183 // Create a font pattern.
1184 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1186 FcResult result = FcResultMatch;
1188 // match the pattern
1189 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1190 bool isScalable = false;
1194 // Get the path to the font file name.
1196 GetFcString(match, FC_FILE, path);
1197 isScalable = IsScalable(path);
1201 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::IsScalable. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1204 // Destroys the created patterns.
1205 FcPatternDestroy(match);
1206 FcPatternDestroy(fontFamilyPattern);
1211 void FontClient::Plugin::GetFixedSizes(const FontPath& path, Vector<PointSize26Dot6>& sizes) const
1213 // Empty the caller container
1216 FT_Face ftFace = nullptr;
1217 int error = FT_New_Face(mFreeTypeLibrary,
1221 if(FT_Err_Ok != error)
1223 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font path : [%s]\n", path.c_str());
1228 // Fetch the number of fixed sizes available
1229 if(ftFace->num_fixed_sizes && ftFace->available_sizes)
1231 for(int i = 0; i < ftFace->num_fixed_sizes; ++i)
1233 sizes.PushBack(ftFace->available_sizes[i].size);
1237 FT_Done_Face(ftFace);
1241 void FontClient::Plugin::GetFixedSizes(const FontDescription& fontDescription,
1242 Vector<PointSize26Dot6>& sizes) const
1244 // Create a font pattern.
1245 FcPattern* fontFamilyPattern = CreateFontFamilyPattern(fontDescription); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1247 FcResult result = FcResultMatch;
1249 // match the pattern
1250 FcPattern* match = FcFontMatch(nullptr /* use default configure */, fontFamilyPattern, &result); // Creates a font pattern that needs to be destroyed by calling FcPatternDestroy.
1254 // Get the path to the font file name.
1256 GetFcString(match, FC_FILE, path);
1257 GetFixedSizes(path, sizes);
1261 DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::GetFixedSizes. FreeType Cannot check font: [%s]\n", fontDescription.family.c_str());
1264 // Destroys the created patterns.
1265 FcPatternDestroy(match);
1266 FcPatternDestroy(fontFamilyPattern);
1269 bool FontClient::Plugin::HasItalicStyle(FontId fontId) const
1271 const FontCacheItemInterface* fontCacheItem = GetCachedFontItem(fontId);
1272 if(fontCacheItem != nullptr)
1274 return fontCacheItem->HasItalicStyle();
1279 } // namespace Dali::TextAbstraction::Internal