[Tizen] Add support for FontClientFontPreLoad API
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / font-client-plugin-cache-handler.h
1 #ifndef DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H
2 #define DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
24
25 namespace Dali::TextAbstraction::Internal
26 {
27 /**
28  * @brief FontClient Plugin cache item handler.
29  */
30 struct FontClient::Plugin::CacheHandler
31 {
32 public:
33   /**
34    * Constructor.
35    */
36   CacheHandler();
37
38   /**
39    * Default destructor.
40    *
41    * Frees any allocated resource.
42    */
43   ~CacheHandler();
44
45 public: // Public struct
46   /// Redefine FontId name to specifiy the value's usage
47   using FontCacheIndex     = FontId;
48   using EllipsisCacheIndex = FontId;
49
50   /**
51    * @brief Index of FontCache container.
52    */
53   struct FontIdCacheItem
54   {
55     FontDescription::Type type;  ///< The type of font.
56     FontCacheIndex        index; ///< Index to the cache of fonts for the specified type. Face or Bitmap
57   };
58
59   /**
60    * @brief Caches an list of fallback fonts for a given font-description
61    */
62   struct FallbackCacheItem
63   {
64     FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
65
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.
69   };
70
71   /**
72    * @brief Caches an glyph informations of ellipsis character per each point size.
73    */
74   struct EllipsisItem
75   {
76     PointSize26Dot6    requestedPointSize;
77     EllipsisCacheIndex index;
78     GlyphInfo          glyph;
79   };
80
81   /**
82    * @brief Caches an index to the vector of font descriptions for a given font.
83    */
84   struct FontDescriptionCacheItem
85   {
86     FontDescriptionCacheItem(const FontDescription& fontDescription,
87                              FontDescriptionId      index);
88     FontDescriptionCacheItem(FontDescription&& fontDescription,
89                              FontDescriptionId index);
90
91     FontDescription   fontDescription; ///< The font description.
92     FontDescriptionId index;           ///< Index to the vector of font descriptions.
93   };
94
95   /**
96    * @brief Pair of FontDescriptionId and PointSize. It will be used to find cached validate font.
97    */
98   struct FontDescriptionSizeCacheKey
99   {
100     FontDescriptionSizeCacheKey(FontDescriptionId fontDescriptionId,
101                                 PointSize26Dot6   requestedPointSize);
102
103     FontDescriptionId fontDescriptionId;  ///< Index to the vector with font descriptions.
104     PointSize26Dot6   requestedPointSize; ///< The font point size.
105
106     bool operator==(FontDescriptionSizeCacheKey const& rhs) const noexcept
107     {
108       return fontDescriptionId == rhs.fontDescriptionId && requestedPointSize == rhs.requestedPointSize;
109     }
110   };
111
112   /**
113    * @brief Custom hash functions for FontDescriptionSizeCacheKey.
114    */
115   struct FontDescriptionSizeCacheKeyHash
116   {
117     std::size_t operator()(FontDescriptionSizeCacheKey const& key) const noexcept
118     {
119       return key.fontDescriptionId ^ key.requestedPointSize;
120     }
121   };
122
123   /**
124    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
125    */
126   using FontDescriptionSizeCacheContainer = std::unordered_map<FontDescriptionSizeCacheKey, FontCacheIndex, FontDescriptionSizeCacheKeyHash>;
127
128 public: // Clear cache public
129   /**
130    * @copydoc Dali::TextAbstraction::FontClient::Plugin::ClearCache()
131    */
132   void ClearCache();
133
134   /**
135    * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
136    */
137   void ResetSystemDefaults();
138
139 private: // Clear cache private
140   /**
141    * @brief Free the resources allocated by the FcCharSet objects.
142    */
143   void ClearCharacterSetFromFontFaceCache();
144
145   /**
146    * @brief Free the resources allocated in the fallback cache.
147    */
148   void ClearFallbackCache();
149
150   /**
151    * @brief Free the resources allocated in charset cache.
152    */
153   void ClearCharacterSet();
154
155   /**
156    * @brief Free the resources allocated in FreeType face cache.
157    */
158   void ClearFTFaceFromFontFTFaceCache();
159
160 private:
161   /**
162    * @brief Crate the charset resouces by default font and Fallback caches.
163    * @pre We should call this API only one times after ClearCharacterSet().
164    */
165   void CreateCharacterSet();
166
167 public: // Find & Cache
168   // System / Default
169
170   /**
171    * @brief Caches the fonts present in the platform.
172    *
173    * Calls GetFcFontSet() to retrieve the fonts.
174    */
175   void InitSystemFonts();
176
177   /**
178    * @brief Retrieve the list of default fonts supported by the system.
179    */
180   void InitDefaultFonts();
181
182   /**
183    * @brief Retrieve the active default font from the system.
184    */
185   void InitDefaultFontDescription();
186
187   // Font
188
189   /**
190    * @brief Checks if font data for the specified font path is cached.
191    *
192    * @param[in] fontPath The font path to check for cached data.
193    *
194    * @return @e true if the font data is cached, otherwise false.
195    */
196   bool FindFontData(const std::string& fontPath) const;
197
198   /**
199    * @brief Retrieves font data for the specified font path if it is cached.
200    *
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.
204    *
205    * @return @e true if the font data is cached and retrieved successfully, otherwise false.
206    */
207   bool FindFontData(const std::string& fontPath, uint8_t*& fontDataPtr, std::streampos& dataSize) const;
208
209   /**
210    * @brief Loads font data from the specified file path.
211    *
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.
215    *
216    * @return @e true if the font data was loaded successfully, otherwise false.
217    */
218   bool LoadFontDataFromFile(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize) const;
219
220   /**
221    * @brief Caches font data for the specified font path.
222    *
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.
226    */
227   void CacheFontData(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize);
228
229   /**
230    * @brief Checks if FreeType face for the specified font path is cached.
231    *
232    * @param[in] fontPath The font path to check for cached face.
233    *
234    * @return @e true if the font face is cached, otherwise false.
235    */
236   bool FindFontFace(const std::string& fontPath) const;
237
238   /**
239    * @brief Caches FreeType face for the specified font path.
240    *
241    * @param[in] fontPath The font path to cache the face for.
242    * @param[in] ftFace The freetype font face to cache.
243    */
244   void CacheFontFace(const std::string& fontPath, FT_Face ftFace);
245
246   // Validate
247
248   /**
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.
251    *
252    * @param[in] fontDescription The font to validate.
253    * @param[out] validatedFontId The index to the vector with font descriptions.
254    *
255    * @return @e true if the pair is found.
256    */
257   bool FindValidatedFont(const FontDescription& fontDescription,
258                          FontDescriptionId&     validatedFontId);
259
260   /**
261    * @brief Validate a font description.
262    *
263    * @param[in] fontDescription The font to validate.
264    * @param[out] fontDescriptionId Result of validation
265    */
266   void ValidateFont(const FontDescription& fontDescription,
267                     FontDescriptionId&     fontDescriptionId);
268
269   /**
270    * @brief Cache in the descrption and validate id information
271    * @note We use std::move operation to fontDescription.
272    *
273    * @param[in] fontDescription The font to validate.
274    * @param[in] validatedFontId The index to the vector with font descriptions.
275    */
276   void CacheValidateFont(FontDescription&& fontDescription,
277                          FontDescriptionId validatedFontId);
278
279   // Fallback
280
281   /**
282    * @brief Finds a fallback font list from the cache for a given font-description
283    *
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.
287    *
288    * @return Whether the fallback font list has been found.
289    */
290   bool FindFallbackFontList(const FontDescription& fontDescription,
291                             FontList*&             fontList,
292                             CharacterSetList*&     characterSetList) const;
293
294   /**
295    * @brief Cache a fallback font list for a given font-description
296    * @note We use std::move operation to fontDescription.
297    *
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.
301    */
302   void CacheFallbackFontList(FontDescription&&  fontDescription,
303                              FontList*&         fontList,
304                              CharacterSetList*& characterSetList);
305
306   // Font / FontFace
307
308   /**
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.
311    *
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.
316    *
317    * @return @e true if there triplet is found.
318    */
319   bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
320
321   /**
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.
324    *
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.
328    *
329    * @return @e true if the pair is found.
330    */
331   bool FindFont(FontDescriptionId validatedFontId,
332                 PointSize26Dot6   requestedPointSize,
333                 FontCacheIndex&   fontCacheIndex);
334
335   /**
336    * @brief Cache the font descpription size item.
337    *
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.
341    */
342   void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
343
344   /**
345    * @brief Cache the font face cache item.
346    * @note We use std::move operation to cache item.
347    *
348    * @param[in] fontFaceCacheItem Font face cache item.
349    * @return FontId of newly inserted font cache item.
350    */
351   FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
352
353   /**
354    * @brief Caches a font path.
355    *
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.
360    */
361   void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
362
363   // Ellipsis
364
365   /**
366    * @brief Finds an ellipsis cache for a given point size
367    *
368    * @param[in] requestedPointSize Requested point size.
369    * @param[out] ellipsisCacheIndex The index of cached ellipsis.
370    *
371    * @return Whether the ellipsis has been found.
372    */
373   bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
374
375   /**
376    * @brief Cache an ellipsis item
377    * @note We use std::move operation to cache item.
378    *
379    * @param[in] ellipsisItem Ellipsis item.
380    * @return The index of cached ellipsis.
381    */
382   EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
383
384   // Bitmap font
385
386   /**
387    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
388    *
389    * @param[in] bitmapFontFamily The font's family name.
390    * @param[out] fontId The id of the font.
391    *
392    * @return Whether the font has been found.
393    */
394   bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
395
396   /**
397    * @brief Cache the bitmap font cache item.
398    * @note We use std::move operation to cache item.
399    *
400    * @param[in] bitmapFontCacheItem Bitmap font cache item.
401    * @return FontId of newly inserted font cache item.
402    */
403   FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
404
405   // Embedded
406
407   /**
408    * @brief Finds in the cache a pixel buffer for embedded font.
409    *
410    * @param[in] url The embedded image's url.
411    * @param[out] pixelBufferId The id of the loaded pixel buffer.
412    *
413    * @return Whether the embedded pixel buffer has been found.
414    */
415   bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
416
417   /**
418    * @brief Cache the pixel buffer
419    * @note We load image syncronously.
420    *
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.
423    */
424   PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
425
426   /**
427    * @brief Finds in the cache a embedded item.
428    *
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.
434    */
435   bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
436
437   /**
438    * @brief Cache the embedded item.
439    * @note We use std::move operation to cache item.
440    *
441    * @param[in] embeddedItem The url of embedded pixel buffer.
442    * @return GlyphIndex of newly inserted embedded item.
443    */
444   GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
445
446 public: // Other public API
447   GlyphCacheManager* GetGlyphCacheManager() const
448   {
449     return mGlyphCacheManager.get();
450   }
451
452 private:
453   CacheHandler(const CacheHandler&) = delete;
454   CacheHandler& operator=(const CacheHandler&) = delete;
455
456 public:                                    // Cache container list
457   FontDescription mDefaultFontDescription; ///< Cached default font from the system
458
459   FontList         mSystemFonts;              ///< Cached system fonts.
460   FontList         mDefaultFonts;             ///< Cached default fonts.
461   CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
462
463   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
464
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.
470
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.
472
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.
475
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.
480
481 private:                                                 // Member value
482   std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
483
484   FontDescription   mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
485   FontDescriptionId mLatestFoundFontDescriptionId;
486
487   FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
488   FontCacheIndex              mLatestFoundCacheIndex;
489
490   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
491 };
492
493 } // namespace Dali::TextAbstraction::Internal
494
495 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H