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>
24 namespace Dali::TextAbstraction::Internal
27 * @brief FontClient Plugin cache item handler.
29 struct FontClient::Plugin::CacheHandler
40 * Frees any allocated resource.
44 public: // Public struct
45 /// Redefine FontId name to specifiy the value's usage
46 using FontCacheIndex = FontId;
47 using EllipsisCacheIndex = FontId;
50 * @brief Index of FontCache container.
52 struct FontIdCacheItem
54 FontDescription::Type type; ///< The type of font.
55 FontCacheIndex index; ///< Index to the cache of fonts for the specified type. Face or Bitmap
59 * @brief Caches an list of fallback fonts for a given font-description
61 struct FallbackCacheItem
63 FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
65 FontDescription fontDescription; ///< The font description.
66 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
67 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
71 * @brief Caches an glyph informations of ellipsis character per each point size.
75 PointSize26Dot6 requestedPointSize;
76 EllipsisCacheIndex index;
81 * @brief Caches an index to the vector of font descriptions for a given font.
83 struct FontDescriptionCacheItem
85 FontDescriptionCacheItem(const FontDescription& fontDescription,
86 FontDescriptionId index);
87 FontDescriptionCacheItem(FontDescription&& fontDescription,
88 FontDescriptionId index);
90 FontDescription fontDescription; ///< The font description.
91 FontDescriptionId index; ///< Index to the vector of font descriptions.
95 * @brief Pair of FontDescriptionId and PointSize. It will be used to find cached validate font.
97 struct FontDescriptionSizeCacheKey
99 FontDescriptionSizeCacheKey(FontDescriptionId fontDescriptionId,
100 PointSize26Dot6 requestedPointSize);
102 FontDescriptionId fontDescriptionId; ///< Index to the vector with font descriptions.
103 PointSize26Dot6 requestedPointSize; ///< The font point size.
105 bool operator==(FontDescriptionSizeCacheKey const& rhs) const noexcept
107 return fontDescriptionId == rhs.fontDescriptionId && requestedPointSize == rhs.requestedPointSize;
112 * @brief Custom hash functions for FontDescriptionSizeCacheKey.
114 struct FontDescriptionSizeCacheKeyHash
116 std::size_t operator()(FontDescriptionSizeCacheKey const& key) const noexcept
118 return key.fontDescriptionId ^ key.requestedPointSize;
123 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
125 using FontDescriptionSizeCacheContainer = std::unordered_map<FontDescriptionSizeCacheKey, FontCacheIndex, FontDescriptionSizeCacheKeyHash>;
127 public: // Clear cache public
129 * @copydoc Dali::TextAbstraction::FontClient::Plugin::ClearCache()
134 * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
136 void ResetSystemDefaults();
138 private: // Clear cache private
140 * @brief Free the resources allocated by the FcCharSet objects.
142 void ClearCharacterSetFromFontFaceCache();
145 * @brief Free the resources allocated in the fallback cache.
147 void ClearFallbackCache();
150 * @brief Free the resources allocated in charset cache.
152 void ClearCharacterSet();
156 * @brief Crate the charset resouces by default font and Fallback caches.
157 * @pre We should call this API only one times after ClearCharacterSet().
159 void CreateCharacterSet();
161 public: // Find & Cache
165 * @brief Caches the fonts present in the platform.
167 * Calls GetFcFontSet() to retrieve the fonts.
169 void InitSystemFonts();
172 * @brief Retrieve the list of default fonts supported by the system.
174 void InitDefaultFonts();
177 * @brief Retrieve the active default font from the system.
179 void InitDefaultFontDescription();
184 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
185 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
187 * @param[in] fontDescription The font to validate.
188 * @param[out] validatedFontId The index to the vector with font descriptions.
190 * @return @e true if the pair is found.
192 bool FindValidatedFont(const FontDescription& fontDescription,
193 FontDescriptionId& validatedFontId);
196 * @brief Validate a font description.
198 * @param[in] fontDescription The font to validate.
199 * @param[out] fontDescriptionId Result of validation
201 void ValidateFont(const FontDescription& fontDescription,
202 FontDescriptionId& fontDescriptionId);
205 * @brief Cache in the descrption and validate id information
206 * @note We use std::move operation to fontDescription.
208 * @param[in] fontDescription The font to validate.
209 * @param[in] validatedFontId The index to the vector with font descriptions.
211 void CacheValidateFont(FontDescription&& fontDescription,
212 FontDescriptionId validatedFontId);
217 * @brief Finds a fallback font list from the cache for a given font-description
219 * @param[in] fontDescription The font to validate.
220 * @param[out] fontList A valid pointer to a font list, or @e nullptr if not found.
221 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
223 * @return Whether the fallback font list has been found.
225 bool FindFallbackFontList(const FontDescription& fontDescription,
227 CharacterSetList*& characterSetList) const;
230 * @brief Cache a fallback font list for a given font-description
231 * @note We use std::move operation to fontDescription.
233 * @param[in] fontDescription The font to validate.
234 * @param[out] fontListA valid pointer to a font list.
235 * @param[out] characterSetList A valid pointer to a character set list.
237 void CacheFallbackFontList(FontDescription&& fontDescription,
239 CharacterSetList*& characterSetList);
244 * @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.
245 * If there is one , if writes the font identifier in the param @p fontId.
247 * @param[in] path Path to the font file name.
248 * @param[in] requestedPointSize The font point size.
249 * @param[in] faceIndex The face index.
250 * @param[out] fontId The font identifier.
252 * @return @e true if there triplet is found.
254 bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
257 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
258 * If there is one it writes the font identifier in the param @p fontCacheIndex.
260 * @param[in] validatedFontId Index to the vector with font descriptions.
261 * @param[in] requestedPointSize The font point size.
262 * @param[out] fontCacheIndex The index of font cache identifier.
264 * @return @e true if the pair is found.
266 bool FindFont(FontDescriptionId validatedFontId,
267 PointSize26Dot6 requestedPointSize,
268 FontCacheIndex& fontCacheIndex);
271 * @brief Cache the font descpription size item.
273 * @param[in] fontDescriptionId FontDescriptionId of current font.
274 * @param[in] requestedPointSize Size of current font.
275 * @param[in] fontCacheIndex Index of this font's cache.
277 void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
280 * @brief Cache the font face cache item.
281 * @note We use std::move operation to cache item.
283 * @param[in] fontFaceCacheItem Font face cache item.
284 * @return FontId of newly inserted font cache item.
286 FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
289 * @brief Caches a font path.
291 * @param[in] ftFace The FreeType face.
292 * @param[in] fontId The font identifier.
293 * @param[in] requestedPointSize The font point size.
294 * @param[in] path Path to the font file name.
296 void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
301 * @brief Finds an ellipsis cache for a given point size
303 * @param[in] requestedPointSize Requested point size.
304 * @param[out] ellipsisCacheIndex The index of cached ellipsis.
306 * @return Whether the ellipsis has been found.
308 bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
311 * @brief Cache an ellipsis item
312 * @note We use std::move operation to cache item.
314 * @param[in] ellipsisItem Ellipsis item.
315 * @return The index of cached ellipsis.
317 EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
322 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
324 * @param[in] bitmapFontFamily The font's family name.
325 * @param[out] fontId The id of the font.
327 * @return Whether the font has been found.
329 bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
332 * @brief Cache the bitmap font cache item.
333 * @note We use std::move operation to cache item.
335 * @param[in] bitmapFontCacheItem Bitmap font cache item.
336 * @return FontId of newly inserted font cache item.
338 FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
343 * @brief Finds in the cache a pixel buffer for embedded font.
345 * @param[in] url The embedded image's url.
346 * @param[out] pixelBufferId The id of the loaded pixel buffer.
348 * @return Whether the embedded pixel buffer has been found.
350 bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
353 * @brief Cache the pixel buffer
354 * @note We load image syncronously.
356 * @param[in] url The url of embedded pixel buffer.
357 * @return PixelBufferId of newly inserted pixel buffer. Or 0 if we fail to be load.
359 PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
362 * @brief Finds in the cache a embedded item.
364 * @param pixelBufferId The id of embedded item's pixel buffer.
365 * @param width The width of embedded item.
366 * @param height The height of embedded item.
367 * @param[out] index GlyphIndex of embedded item.
368 * @return Whether the embedded item has been found.
370 bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
373 * @brief Cache the embedded item.
374 * @note We use std::move operation to cache item.
376 * @param[in] embeddedItem The url of embedded pixel buffer.
377 * @return GlyphIndex of newly inserted embedded item.
379 GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
382 CacheHandler(const CacheHandler&) = delete;
383 CacheHandler& operator=(const CacheHandler&) = delete;
385 public: // Cache container list
386 FontDescription mDefaultFontDescription; ///< Cached default font from the system
388 FontList mSystemFonts; ///< Cached system fonts.
389 FontList mDefaultFonts; ///< Cached default fonts.
390 CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
392 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
394 std::vector<FontIdCacheItem> mFontIdCache; ///< Caches from FontId to FontCacheIndex.
395 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'.
396 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
397 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
398 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
400 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.
402 std::vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
403 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
404 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
405 std::vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
407 private: // Member value
408 FontDescription mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
409 FontDescriptionId mLatestFoundFontDescriptionId;
411 FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
412 FontCacheIndex mLatestFoundCacheIndex;
414 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
417 } // namespace Dali::TextAbstraction::Internal
419 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H