1 #ifndef DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H
2 #define DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/internal/text/text-abstraction/plugin/font-client-plugin-impl.h>
23 #include <dali/internal/text/text-abstraction/plugin/font-face-glyph-cache-manager.h>
25 namespace Dali::TextAbstraction::Internal
28 * @brief FontClient Plugin cache item handler.
30 struct FontClient::Plugin::CacheHandler
41 * Frees any allocated resource.
45 public: // Public struct
46 /// Redefine FontId name to specifiy the value's usage
47 using FontCacheIndex = FontId;
48 using EllipsisCacheIndex = FontId;
51 * @brief Index of FontCache container.
53 struct FontIdCacheItem
55 FontDescription::Type type; ///< The type of font.
56 FontCacheIndex index; ///< Index to the cache of fonts for the specified type. Face or Bitmap
60 * @brief Caches an list of fallback fonts for a given font-description
62 struct FallbackCacheItem
64 FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
66 FontDescription fontDescription; ///< The font description.
67 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
68 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
72 * @brief Caches an glyph informations of ellipsis character per each point size.
76 PointSize26Dot6 requestedPointSize;
77 EllipsisCacheIndex index;
82 * @brief Caches an index to the vector of font descriptions for a given font.
84 struct FontDescriptionCacheItem
86 FontDescriptionCacheItem(const FontDescription& fontDescription,
87 FontDescriptionId index);
88 FontDescriptionCacheItem(FontDescription&& fontDescription,
89 FontDescriptionId index);
91 FontDescription fontDescription; ///< The font description.
92 FontDescriptionId index; ///< Index to the vector of font descriptions.
96 * @brief Pair of FontDescriptionId and PointSize. It will be used to find cached validate font.
98 struct FontDescriptionSizeCacheKey
100 FontDescriptionSizeCacheKey(FontDescriptionId fontDescriptionId,
101 PointSize26Dot6 requestedPointSize);
103 FontDescriptionId fontDescriptionId; ///< Index to the vector with font descriptions.
104 PointSize26Dot6 requestedPointSize; ///< The font point size.
106 bool operator==(FontDescriptionSizeCacheKey const& rhs) const noexcept
108 return fontDescriptionId == rhs.fontDescriptionId && requestedPointSize == rhs.requestedPointSize;
113 * @brief Custom hash functions for FontDescriptionSizeCacheKey.
115 struct FontDescriptionSizeCacheKeyHash
117 std::size_t operator()(FontDescriptionSizeCacheKey const& key) const noexcept
119 return key.fontDescriptionId ^ key.requestedPointSize;
124 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
126 using FontDescriptionSizeCacheContainer = std::unordered_map<FontDescriptionSizeCacheKey, FontCacheIndex, FontDescriptionSizeCacheKeyHash>;
128 public: // Clear cache public
130 * @copydoc Dali::TextAbstraction::FontClient::Plugin::ClearCache()
135 * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
137 void ResetSystemDefaults();
139 private: // Clear cache private
141 * @brief Free the resources allocated by the FcCharSet objects.
143 void ClearCharacterSetFromFontFaceCache();
146 * @brief Free the resources allocated in the fallback cache.
148 void ClearFallbackCache();
151 * @brief Free the resources allocated in charset cache.
153 void ClearCharacterSet();
156 * @brief Free the resources allocated in FreeType face cache.
158 void ClearFTFaceFromFontFTFaceCache();
162 * @brief Crate the charset resouces by default font and Fallback caches.
163 * @pre We should call this API only one times after ClearCharacterSet().
165 void CreateCharacterSet();
167 public: // Find & Cache
171 * @brief Caches the fonts present in the platform.
173 * Calls GetFcFontSet() to retrieve the fonts.
175 void InitSystemFonts();
178 * @brief Retrieve the list of default fonts supported by the system.
180 void InitDefaultFonts();
183 * @brief Retrieve the active default font from the system.
185 void InitDefaultFontDescription();
190 * @brief Checks if font data for the specified font path is cached.
192 * @param[in] fontPath The font path to check for cached data.
194 * @return @e true if the font data is cached, otherwise false.
196 bool FindFontData(const std::string& fontPath) const;
199 * @brief Retrieves font data for the specified font path if it is cached.
201 * @param[in] fontPath The font path to retrieve the cached data for.
202 * @param[out] fontDataPtr A pointer to the cached font data.
203 * @param[out] dataSize The size of the cached font data.
205 * @return @e true if the font data is cached and retrieved successfully, otherwise false.
207 bool FindFontData(const std::string& fontPath, uint8_t*& fontDataPtr, std::streampos& dataSize) const;
210 * @brief Loads font data from the specified file path.
212 * @param[in] fontPath The file path to load the font data from.
213 * @param[out] fontDataBuffer A vector containing the loaded font data.
214 * @param[out] dataSize The size of the loaded font data.
216 * @return @e true if the font data was loaded successfully, otherwise false.
218 bool LoadFontDataFromFile(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize) const;
221 * @brief Caches font data for the specified font path.
223 * @param[in] fontPath The font path to cache the data for.
224 * @param[in] fontDataBuffer A vector containing the font data to cache.
225 * @param[in] dataSize The size of the font data to cache.
227 void CacheFontData(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize);
230 * @brief Checks if FreeType face for the specified font path is cached.
232 * @param[in] fontPath The font path to check for cached face.
234 * @return @e true if the font face is cached, otherwise false.
236 bool FindFontFace(const std::string& fontPath) const;
239 * @brief Caches FreeType face for the specified font path.
241 * @param[in] fontPath The font path to cache the face for.
242 * @param[in] ftFace The freetype font face to cache.
244 void CacheFontFace(const std::string& fontPath, FT_Face ftFace);
249 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
250 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
252 * @param[in] fontDescription The font to validate.
253 * @param[out] validatedFontId The index to the vector with font descriptions.
255 * @return @e true if the pair is found.
257 bool FindValidatedFont(const FontDescription& fontDescription,
258 FontDescriptionId& validatedFontId);
261 * @brief Validate a font description.
263 * @param[in] fontDescription The font to validate.
264 * @param[out] fontDescriptionId Result of validation
266 void ValidateFont(const FontDescription& fontDescription,
267 FontDescriptionId& fontDescriptionId);
270 * @brief Cache in the descrption and validate id information
271 * @note We use std::move operation to fontDescription.
273 * @param[in] fontDescription The font to validate.
274 * @param[in] validatedFontId The index to the vector with font descriptions.
276 void CacheValidateFont(FontDescription&& fontDescription,
277 FontDescriptionId validatedFontId);
282 * @brief Finds a fallback font list from the cache for a given font-description
284 * @param[in] fontDescription The font to validate.
285 * @param[out] fontList A valid pointer to a font list, or @e nullptr if not found.
286 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
288 * @return Whether the fallback font list has been found.
290 bool FindFallbackFontList(const FontDescription& fontDescription,
292 CharacterSetList*& characterSetList) const;
295 * @brief Cache a fallback font list for a given font-description
296 * @note We use std::move operation to fontDescription.
298 * @param[in] fontDescription The font to validate.
299 * @param[out] fontListA valid pointer to a font list.
300 * @param[out] characterSetList A valid pointer to a character set list.
302 void CacheFallbackFontList(FontDescription&& fontDescription,
304 CharacterSetList*& characterSetList);
309 * @brief Finds in the cache if there is a triplet with the path to the font file name, the font point size and the face index.
310 * If there is one , if writes the font identifier in the param @p fontId.
312 * @param[in] path Path to the font file name.
313 * @param[in] requestedPointSize The font point size.
314 * @param[in] faceIndex The face index.
315 * @param[out] fontId The font identifier.
317 * @return @e true if there triplet is found.
319 bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
322 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
323 * If there is one it writes the font identifier in the param @p fontCacheIndex.
325 * @param[in] validatedFontId Index to the vector with font descriptions.
326 * @param[in] requestedPointSize The font point size.
327 * @param[out] fontCacheIndex The index of font cache identifier.
329 * @return @e true if the pair is found.
331 bool FindFont(FontDescriptionId validatedFontId,
332 PointSize26Dot6 requestedPointSize,
333 FontCacheIndex& fontCacheIndex);
336 * @brief Cache the font descpription size item.
338 * @param[in] fontDescriptionId FontDescriptionId of current font.
339 * @param[in] requestedPointSize Size of current font.
340 * @param[in] fontCacheIndex Index of this font's cache.
342 void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
345 * @brief Cache the font face cache item.
346 * @note We use std::move operation to cache item.
348 * @param[in] fontFaceCacheItem Font face cache item.
349 * @return FontId of newly inserted font cache item.
351 FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
354 * @brief Caches a font path.
356 * @param[in] ftFace The FreeType face.
357 * @param[in] fontId The font identifier.
358 * @param[in] requestedPointSize The font point size.
359 * @param[in] path Path to the font file name.
361 void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
366 * @brief Finds an ellipsis cache for a given point size
368 * @param[in] requestedPointSize Requested point size.
369 * @param[out] ellipsisCacheIndex The index of cached ellipsis.
371 * @return Whether the ellipsis has been found.
373 bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
376 * @brief Cache an ellipsis item
377 * @note We use std::move operation to cache item.
379 * @param[in] ellipsisItem Ellipsis item.
380 * @return The index of cached ellipsis.
382 EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
387 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
389 * @param[in] bitmapFontFamily The font's family name.
390 * @param[out] fontId The id of the font.
392 * @return Whether the font has been found.
394 bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
397 * @brief Cache the bitmap font cache item.
398 * @note We use std::move operation to cache item.
400 * @param[in] bitmapFontCacheItem Bitmap font cache item.
401 * @return FontId of newly inserted font cache item.
403 FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
408 * @brief Finds in the cache a pixel buffer for embedded font.
410 * @param[in] url The embedded image's url.
411 * @param[out] pixelBufferId The id of the loaded pixel buffer.
413 * @return Whether the embedded pixel buffer has been found.
415 bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
418 * @brief Cache the pixel buffer
419 * @note We load image syncronously.
421 * @param[in] url The url of embedded pixel buffer.
422 * @return PixelBufferId of newly inserted pixel buffer. Or 0 if we fail to be load.
424 PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
427 * @brief Finds in the cache a embedded item.
429 * @param pixelBufferId The id of embedded item's pixel buffer.
430 * @param width The width of embedded item.
431 * @param height The height of embedded item.
432 * @param[out] index GlyphIndex of embedded item.
433 * @return Whether the embedded item has been found.
435 bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
438 * @brief Cache the embedded item.
439 * @note We use std::move operation to cache item.
441 * @param[in] embeddedItem The url of embedded pixel buffer.
442 * @return GlyphIndex of newly inserted embedded item.
444 GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
446 public: // Other public API
447 GlyphCacheManager* GetGlyphCacheManager() const
449 return mGlyphCacheManager.get();
453 CacheHandler(const CacheHandler&) = delete;
454 CacheHandler& operator=(const CacheHandler&) = delete;
456 public: // Cache container list
457 FontDescription mDefaultFontDescription; ///< Cached default font from the system
459 FontList mSystemFonts; ///< Cached system fonts.
460 FontList mDefaultFonts; ///< Cached default fonts.
461 CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
463 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
465 std::vector<FontIdCacheItem> mFontIdCache; ///< Caches from FontId to FontCacheIndex.
466 std::vector<FontFaceCacheItem> mFontFaceCache; ///< Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
467 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
468 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
469 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
471 FontDescriptionSizeCacheContainer mFontDescriptionSizeCache; ///< Caches font identifiers for the pairs of font point size and the index to the vector with font descriptions of the validated fonts.
473 std::unordered_map<std::string, std::pair<Dali::Vector<uint8_t>, std::streampos>> mFontDataCache; ///< Caches font data with each font path as the key, allowing faster loading of fonts later on.
474 std::unordered_map<std::string, FT_Face> mFontFTFaceCache; ///< Caches freetype font face for font pre-load.
476 std::vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
477 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
478 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
479 std::vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
481 private: // Member value
482 std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
484 FontDescription mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
485 FontDescriptionId mLatestFoundFontDescriptionId;
487 FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
488 FontCacheIndex mLatestFoundCacheIndex;
490 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
493 } // namespace Dali::TextAbstraction::Internal
495 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H