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) 2022 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();
157 * @brief Crate the charset resouces by default font and Fallback caches.
158 * @pre We should call this API only one times after ClearCharacterSet().
160 void CreateCharacterSet();
162 public: // Find & Cache
166 * @brief Caches the fonts present in the platform.
168 * Calls GetFcFontSet() to retrieve the fonts.
170 void InitSystemFonts();
173 * @brief Retrieve the list of default fonts supported by the system.
175 void InitDefaultFonts();
178 * @brief Retrieve the active default font from the system.
180 void InitDefaultFontDescription();
185 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
186 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
188 * @param[in] fontDescription The font to validate.
189 * @param[out] validatedFontId The index to the vector with font descriptions.
191 * @return @e true if the pair is found.
193 bool FindValidatedFont(const FontDescription& fontDescription,
194 FontDescriptionId& validatedFontId);
197 * @brief Validate a font description.
199 * @param[in] fontDescription The font to validate.
200 * @param[out] fontDescriptionId Result of validation
202 void ValidateFont(const FontDescription& fontDescription,
203 FontDescriptionId& fontDescriptionId);
206 * @brief Cache in the descrption and validate id information
207 * @note We use std::move operation to fontDescription.
209 * @param[in] fontDescription The font to validate.
210 * @param[in] validatedFontId The index to the vector with font descriptions.
212 void CacheValidateFont(FontDescription&& fontDescription,
213 FontDescriptionId validatedFontId);
218 * @brief Finds a fallback font list from the cache for a given font-description
220 * @param[in] fontDescription The font to validate.
221 * @param[out] fontList A valid pointer to a font list, or @e nullptr if not found.
222 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
224 * @return Whether the fallback font list has been found.
226 bool FindFallbackFontList(const FontDescription& fontDescription,
228 CharacterSetList*& characterSetList) const;
231 * @brief Cache a fallback font list for a given font-description
232 * @note We use std::move operation to fontDescription.
234 * @param[in] fontDescription The font to validate.
235 * @param[out] fontListA valid pointer to a font list.
236 * @param[out] characterSetList A valid pointer to a character set list.
238 void CacheFallbackFontList(FontDescription&& fontDescription,
240 CharacterSetList*& characterSetList);
245 * @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.
246 * If there is one , if writes the font identifier in the param @p fontId.
248 * @param[in] path Path to the font file name.
249 * @param[in] requestedPointSize The font point size.
250 * @param[in] faceIndex The face index.
251 * @param[out] fontId The font identifier.
253 * @return @e true if there triplet is found.
255 bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
258 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
259 * If there is one it writes the font identifier in the param @p fontCacheIndex.
261 * @param[in] validatedFontId Index to the vector with font descriptions.
262 * @param[in] requestedPointSize The font point size.
263 * @param[out] fontCacheIndex The index of font cache identifier.
265 * @return @e true if the pair is found.
267 bool FindFont(FontDescriptionId validatedFontId,
268 PointSize26Dot6 requestedPointSize,
269 FontCacheIndex& fontCacheIndex);
272 * @brief Cache the font descpription size item.
274 * @param[in] fontDescriptionId FontDescriptionId of current font.
275 * @param[in] requestedPointSize Size of current font.
276 * @param[in] fontCacheIndex Index of this font's cache.
278 void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
281 * @brief Cache the font face cache item.
282 * @note We use std::move operation to cache item.
284 * @param[in] fontFaceCacheItem Font face cache item.
285 * @return FontId of newly inserted font cache item.
287 FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
290 * @brief Caches a font path.
292 * @param[in] ftFace The FreeType face.
293 * @param[in] fontId The font identifier.
294 * @param[in] requestedPointSize The font point size.
295 * @param[in] path Path to the font file name.
297 void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
302 * @brief Finds an ellipsis cache for a given point size
304 * @param[in] requestedPointSize Requested point size.
305 * @param[out] ellipsisCacheIndex The index of cached ellipsis.
307 * @return Whether the ellipsis has been found.
309 bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
312 * @brief Cache an ellipsis item
313 * @note We use std::move operation to cache item.
315 * @param[in] ellipsisItem Ellipsis item.
316 * @return The index of cached ellipsis.
318 EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
323 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
325 * @param[in] bitmapFontFamily The font's family name.
326 * @param[out] fontId The id of the font.
328 * @return Whether the font has been found.
330 bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
333 * @brief Cache the bitmap font cache item.
334 * @note We use std::move operation to cache item.
336 * @param[in] bitmapFontCacheItem Bitmap font cache item.
337 * @return FontId of newly inserted font cache item.
339 FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
344 * @brief Finds in the cache a pixel buffer for embedded font.
346 * @param[in] url The embedded image's url.
347 * @param[out] pixelBufferId The id of the loaded pixel buffer.
349 * @return Whether the embedded pixel buffer has been found.
351 bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
354 * @brief Cache the pixel buffer
355 * @note We load image syncronously.
357 * @param[in] url The url of embedded pixel buffer.
358 * @return PixelBufferId of newly inserted pixel buffer. Or 0 if we fail to be load.
360 PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
363 * @brief Finds in the cache a embedded item.
365 * @param pixelBufferId The id of embedded item's pixel buffer.
366 * @param width The width of embedded item.
367 * @param height The height of embedded item.
368 * @param[out] index GlyphIndex of embedded item.
369 * @return Whether the embedded item has been found.
371 bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
374 * @brief Cache the embedded item.
375 * @note We use std::move operation to cache item.
377 * @param[in] embeddedItem The url of embedded pixel buffer.
378 * @return GlyphIndex of newly inserted embedded item.
380 GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
382 public: // Other public API
383 GlyphCacheManager* GetGlyphCacheManager() const
385 return mGlyphCacheManager.get();
389 CacheHandler(const CacheHandler&) = delete;
390 CacheHandler& operator=(const CacheHandler&) = delete;
392 public: // Cache container list
393 FontDescription mDefaultFontDescription; ///< Cached default font from the system
395 FontList mSystemFonts; ///< Cached system fonts.
396 FontList mDefaultFonts; ///< Cached default fonts.
397 CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
399 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
401 std::vector<FontIdCacheItem> mFontIdCache; ///< Caches from FontId to FontCacheIndex.
402 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'.
403 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
404 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
405 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
407 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.
409 std::vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
410 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
411 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
412 std::vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
414 private: // Member value
415 std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
417 FontDescription mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
418 FontDescriptionId mLatestFoundFontDescriptionId;
420 FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
421 FontCacheIndex mLatestFoundCacheIndex;
423 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
426 } // namespace Dali::TextAbstraction::Internal
428 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H