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::ClearCacheOnLocaleChanged()
137 void ClearCacheOnLocaleChanged();
140 * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
142 void ResetSystemDefaults();
144 private: // Clear cache private
146 * @brief Free the resources allocated by the FcCharSet objects.
148 void ClearCharacterSetFromFontFaceCache();
151 * @brief Free the resources allocated in the fallback cache.
153 void ClearFallbackCache();
156 * @brief Free the resources allocated in charset cache.
158 void ClearCharacterSet();
161 * @brief Free the resources allocated in FreeType face cache.
163 void ClearFTFaceFromFontFTFaceCache();
167 * @brief Crate the charset resouces by default font and Fallback caches.
168 * @pre We should call this API only one times after ClearCharacterSet().
170 void CreateCharacterSet();
172 public: // Find & Cache
176 * @brief Caches the fonts present in the platform.
178 * Calls GetFcFontSet() to retrieve the fonts.
180 void InitSystemFonts();
183 * @brief Retrieve the list of default fonts supported by the system.
185 void InitDefaultFonts();
188 * @brief Retrieve the active default font from the system.
190 void InitDefaultFontDescription();
195 * @brief Checks if font data for the specified font path is cached.
197 * @param[in] fontPath The font path to check for cached data.
199 * @return @e true if the font data is cached, otherwise false.
201 bool FindFontData(const std::string& fontPath) const;
204 * @brief Retrieves font data for the specified font path if it is cached.
206 * @param[in] fontPath The font path to retrieve the cached data for.
207 * @param[out] fontDataPtr A pointer to the cached font data.
208 * @param[out] dataSize The size of the cached font data.
210 * @return @e true if the font data is cached and retrieved successfully, otherwise false.
212 bool FindFontData(const std::string& fontPath, uint8_t*& fontDataPtr, std::streampos& dataSize) const;
215 * @brief Loads font data from the specified file path.
217 * @param[in] fontPath The file path to load the font data from.
218 * @param[out] fontDataBuffer A vector containing the loaded font data.
219 * @param[out] dataSize The size of the loaded font data.
221 * @return @e true if the font data was loaded successfully, otherwise false.
223 bool LoadFontDataFromFile(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize) const;
226 * @brief Caches font data for the specified font path.
228 * @param[in] fontPath The font path to cache the data for.
229 * @param[in] fontDataBuffer A vector containing the font data to cache.
230 * @param[in] dataSize The size of the font data to cache.
232 void CacheFontData(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize);
235 * @brief Checks if FreeType face for the specified font path is cached.
237 * @param[in] fontPath The font path to check for cached face.
239 * @return @e true if the font face is cached, otherwise false.
241 bool FindFontFace(const std::string& fontPath) const;
244 * @brief Caches FreeType face for the specified font path.
246 * @param[in] fontPath The font path to cache the face for.
247 * @param[in] ftFace The freetype font face to cache.
249 void CacheFontFace(const std::string& fontPath, FT_Face ftFace);
254 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
255 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
257 * @param[in] fontDescription The font to validate.
258 * @param[out] validatedFontId The index to the vector with font descriptions.
260 * @return @e true if the pair is found.
262 bool FindValidatedFont(const FontDescription& fontDescription,
263 FontDescriptionId& validatedFontId);
266 * @brief Validate a font description.
268 * @param[in] fontDescription The font to validate.
269 * @param[out] fontDescriptionId Result of validation
271 void ValidateFont(const FontDescription& fontDescription,
272 FontDescriptionId& fontDescriptionId);
275 * @brief Cache in the descrption and validate id information
276 * @note We use std::move operation to fontDescription.
278 * @param[in] fontDescription The font to validate.
279 * @param[in] validatedFontId The index to the vector with font descriptions.
281 void CacheValidateFont(FontDescription&& fontDescription,
282 FontDescriptionId validatedFontId);
287 * @brief Finds a fallback font list from the cache for a given font-description
289 * @param[in] fontDescription The font to validate.
290 * @param[out] fontList A valid pointer to a font list, or @e nullptr if not found.
291 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
293 * @return Whether the fallback font list has been found.
295 bool FindFallbackFontList(const FontDescription& fontDescription,
297 CharacterSetList*& characterSetList) const;
300 * @brief Cache a fallback font list for a given font-description
301 * @note We use std::move operation to fontDescription.
303 * @param[in] fontDescription The font to validate.
304 * @param[out] fontListA valid pointer to a font list.
305 * @param[out] characterSetList A valid pointer to a character set list.
307 void CacheFallbackFontList(FontDescription&& fontDescription,
309 CharacterSetList*& characterSetList);
314 * @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.
315 * If there is one , if writes the font identifier in the param @p fontId.
317 * @param[in] path Path to the font file name.
318 * @param[in] requestedPointSize The font point size.
319 * @param[in] faceIndex The face index.
320 * @param[out] fontId The font identifier.
322 * @return @e true if there triplet is found.
324 bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
327 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
328 * If there is one it writes the font identifier in the param @p fontCacheIndex.
330 * @param[in] validatedFontId Index to the vector with font descriptions.
331 * @param[in] requestedPointSize The font point size.
332 * @param[out] fontCacheIndex The index of font cache identifier.
334 * @return @e true if the pair is found.
336 bool FindFont(FontDescriptionId validatedFontId,
337 PointSize26Dot6 requestedPointSize,
338 FontCacheIndex& fontCacheIndex);
341 * @brief Cache the font descpription size item.
343 * @param[in] fontDescriptionId FontDescriptionId of current font.
344 * @param[in] requestedPointSize Size of current font.
345 * @param[in] fontCacheIndex Index of this font's cache.
347 void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
350 * @brief Cache the font face cache item.
351 * @note We use std::move operation to cache item.
353 * @param[in] fontFaceCacheItem Font face cache item.
354 * @return FontId of newly inserted font cache item.
356 FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
359 * @brief Caches a font path.
361 * @param[in] ftFace The FreeType face.
362 * @param[in] fontId The font identifier.
363 * @param[in] requestedPointSize The font point size.
364 * @param[in] path Path to the font file name.
366 void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
371 * @brief Finds an ellipsis cache for a given point size
373 * @param[in] requestedPointSize Requested point size.
374 * @param[out] ellipsisCacheIndex The index of cached ellipsis.
376 * @return Whether the ellipsis has been found.
378 bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
381 * @brief Cache an ellipsis item
382 * @note We use std::move operation to cache item.
384 * @param[in] ellipsisItem Ellipsis item.
385 * @return The index of cached ellipsis.
387 EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
392 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
394 * @param[in] bitmapFontFamily The font's family name.
395 * @param[out] fontId The id of the font.
397 * @return Whether the font has been found.
399 bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
402 * @brief Cache the bitmap font cache item.
403 * @note We use std::move operation to cache item.
405 * @param[in] bitmapFontCacheItem Bitmap font cache item.
406 * @return FontId of newly inserted font cache item.
408 FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
413 * @brief Finds in the cache a pixel buffer for embedded font.
415 * @param[in] url The embedded image's url.
416 * @param[out] pixelBufferId The id of the loaded pixel buffer.
418 * @return Whether the embedded pixel buffer has been found.
420 bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
423 * @brief Cache the pixel buffer
424 * @note We load image syncronously.
426 * @param[in] url The url of embedded pixel buffer.
427 * @return PixelBufferId of newly inserted pixel buffer. Or 0 if we fail to be load.
429 PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
432 * @brief Finds in the cache a embedded item.
434 * @param pixelBufferId The id of embedded item's pixel buffer.
435 * @param width The width of embedded item.
436 * @param height The height of embedded item.
437 * @param[out] index GlyphIndex of embedded item.
438 * @return Whether the embedded item has been found.
440 bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
443 * @brief Cache the embedded item.
444 * @note We use std::move operation to cache item.
446 * @param[in] embeddedItem The url of embedded pixel buffer.
447 * @return GlyphIndex of newly inserted embedded item.
449 GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
451 public: // Other public API
452 GlyphCacheManager* GetGlyphCacheManager() const
454 return mGlyphCacheManager.get();
458 CacheHandler(const CacheHandler&) = delete;
459 CacheHandler& operator=(const CacheHandler&) = delete;
461 public: // Cache container list
462 FontDescription mDefaultFontDescription; ///< Cached default font from the system
464 FontList mSystemFonts; ///< Cached system fonts.
465 FontList mDefaultFonts; ///< Cached default fonts.
466 CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
468 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
470 std::vector<FontIdCacheItem> mFontIdCache; ///< Caches from FontId to FontCacheIndex.
471 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'.
472 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
473 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
474 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
476 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.
478 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.
479 std::unordered_map<std::string, FT_Face> mFontFTFaceCache; ///< Caches freetype font face for font pre-load.
481 std::vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
482 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
483 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
484 std::vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
486 private: // Member value
487 std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
489 FontDescription mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
490 FontDescriptionId mLatestFoundFontDescriptionId;
492 FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
493 FontCacheIndex mLatestFoundCacheIndex;
495 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
498 } // namespace Dali::TextAbstraction::Internal
500 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H