Add LocaleChangedSignal
[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::ClearCacheOnLocaleChanged()
136    */
137   void ClearCacheOnLocaleChanged();
138
139   /**
140    * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
141    */
142   void ResetSystemDefaults();
143
144 private: // Clear cache private
145   /**
146    * @brief Free the resources allocated by the FcCharSet objects.
147    */
148   void ClearCharacterSetFromFontFaceCache();
149
150   /**
151    * @brief Free the resources allocated in the fallback cache.
152    */
153   void ClearFallbackCache();
154
155   /**
156    * @brief Free the resources allocated in charset cache.
157    */
158   void ClearCharacterSet();
159
160   /**
161    * @brief Free the resources allocated in FreeType face cache.
162    */
163   void ClearFTFaceFromFontFTFaceCache();
164
165 private:
166   /**
167    * @brief Crate the charset resouces by default font and Fallback caches.
168    * @pre We should call this API only one times after ClearCharacterSet().
169    */
170   void CreateCharacterSet();
171
172 public: // Find & Cache
173   // System / Default
174
175   /**
176    * @brief Caches the fonts present in the platform.
177    *
178    * Calls GetFcFontSet() to retrieve the fonts.
179    */
180   void InitSystemFonts();
181
182   /**
183    * @brief Retrieve the list of default fonts supported by the system.
184    */
185   void InitDefaultFonts();
186
187   /**
188    * @brief Retrieve the active default font from the system.
189    */
190   void InitDefaultFontDescription();
191
192   // Font
193
194   /**
195    * @brief Checks if font data for the specified font path is cached.
196    *
197    * @param[in] fontPath The font path to check for cached data.
198    *
199    * @return @e true if the font data is cached, otherwise false.
200    */
201   bool FindFontData(const std::string& fontPath) const;
202
203   /**
204    * @brief Retrieves font data for the specified font path if it is cached.
205    *
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.
209    *
210    * @return @e true if the font data is cached and retrieved successfully, otherwise false.
211    */
212   bool FindFontData(const std::string& fontPath, uint8_t*& fontDataPtr, std::streampos& dataSize) const;
213
214   /**
215    * @brief Loads font data from the specified file path.
216    *
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.
220    *
221    * @return @e true if the font data was loaded successfully, otherwise false.
222    */
223   bool LoadFontDataFromFile(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize) const;
224
225   /**
226    * @brief Caches font data for the specified font path.
227    *
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.
231    */
232   void CacheFontData(const std::string& fontPath, Dali::Vector<uint8_t>& fontDataBuffer, std::streampos& dataSize);
233
234   /**
235    * @brief Checks if FreeType face for the specified font path is cached.
236    *
237    * @param[in] fontPath The font path to check for cached face.
238    *
239    * @return @e true if the font face is cached, otherwise false.
240    */
241   bool FindFontFace(const std::string& fontPath) const;
242
243   /**
244    * @brief Caches FreeType face for the specified font path.
245    *
246    * @param[in] fontPath The font path to cache the face for.
247    * @param[in] ftFace The freetype font face to cache.
248    */
249   void CacheFontFace(const std::string& fontPath, FT_Face ftFace);
250
251   // Validate
252
253   /**
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.
256    *
257    * @param[in] fontDescription The font to validate.
258    * @param[out] validatedFontId The index to the vector with font descriptions.
259    *
260    * @return @e true if the pair is found.
261    */
262   bool FindValidatedFont(const FontDescription& fontDescription,
263                          FontDescriptionId&     validatedFontId);
264
265   /**
266    * @brief Validate a font description.
267    *
268    * @param[in] fontDescription The font to validate.
269    * @param[out] fontDescriptionId Result of validation
270    */
271   void ValidateFont(const FontDescription& fontDescription,
272                     FontDescriptionId&     fontDescriptionId);
273
274   /**
275    * @brief Cache in the descrption and validate id information
276    * @note We use std::move operation to fontDescription.
277    *
278    * @param[in] fontDescription The font to validate.
279    * @param[in] validatedFontId The index to the vector with font descriptions.
280    */
281   void CacheValidateFont(FontDescription&& fontDescription,
282                          FontDescriptionId validatedFontId);
283
284   // Fallback
285
286   /**
287    * @brief Finds a fallback font list from the cache for a given font-description
288    *
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.
292    *
293    * @return Whether the fallback font list has been found.
294    */
295   bool FindFallbackFontList(const FontDescription& fontDescription,
296                             FontList*&             fontList,
297                             CharacterSetList*&     characterSetList) const;
298
299   /**
300    * @brief Cache a fallback font list for a given font-description
301    * @note We use std::move operation to fontDescription.
302    *
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.
306    */
307   void CacheFallbackFontList(FontDescription&&  fontDescription,
308                              FontList*&         fontList,
309                              CharacterSetList*& characterSetList);
310
311   // Font / FontFace
312
313   /**
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.
316    *
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.
321    *
322    * @return @e true if there triplet is found.
323    */
324   bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
325
326   /**
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.
329    *
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.
333    *
334    * @return @e true if the pair is found.
335    */
336   bool FindFont(FontDescriptionId validatedFontId,
337                 PointSize26Dot6   requestedPointSize,
338                 FontCacheIndex&   fontCacheIndex);
339
340   /**
341    * @brief Cache the font descpription size item.
342    *
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.
346    */
347   void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
348
349   /**
350    * @brief Cache the font face cache item.
351    * @note We use std::move operation to cache item.
352    *
353    * @param[in] fontFaceCacheItem Font face cache item.
354    * @return FontId of newly inserted font cache item.
355    */
356   FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
357
358   /**
359    * @brief Caches a font path.
360    *
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.
365    */
366   void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
367
368   // Ellipsis
369
370   /**
371    * @brief Finds an ellipsis cache for a given point size
372    *
373    * @param[in] requestedPointSize Requested point size.
374    * @param[out] ellipsisCacheIndex The index of cached ellipsis.
375    *
376    * @return Whether the ellipsis has been found.
377    */
378   bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
379
380   /**
381    * @brief Cache an ellipsis item
382    * @note We use std::move operation to cache item.
383    *
384    * @param[in] ellipsisItem Ellipsis item.
385    * @return The index of cached ellipsis.
386    */
387   EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
388
389   // Bitmap font
390
391   /**
392    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
393    *
394    * @param[in] bitmapFontFamily The font's family name.
395    * @param[out] fontId The id of the font.
396    *
397    * @return Whether the font has been found.
398    */
399   bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
400
401   /**
402    * @brief Cache the bitmap font cache item.
403    * @note We use std::move operation to cache item.
404    *
405    * @param[in] bitmapFontCacheItem Bitmap font cache item.
406    * @return FontId of newly inserted font cache item.
407    */
408   FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
409
410   // Embedded
411
412   /**
413    * @brief Finds in the cache a pixel buffer for embedded font.
414    *
415    * @param[in] url The embedded image's url.
416    * @param[out] pixelBufferId The id of the loaded pixel buffer.
417    *
418    * @return Whether the embedded pixel buffer has been found.
419    */
420   bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
421
422   /**
423    * @brief Cache the pixel buffer
424    * @note We load image syncronously.
425    *
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.
428    */
429   PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
430
431   /**
432    * @brief Finds in the cache a embedded item.
433    *
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.
439    */
440   bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
441
442   /**
443    * @brief Cache the embedded item.
444    * @note We use std::move operation to cache item.
445    *
446    * @param[in] embeddedItem The url of embedded pixel buffer.
447    * @return GlyphIndex of newly inserted embedded item.
448    */
449   GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
450
451 public: // Other public API
452   GlyphCacheManager* GetGlyphCacheManager() const
453   {
454     return mGlyphCacheManager.get();
455   }
456
457 private:
458   CacheHandler(const CacheHandler&) = delete;
459   CacheHandler& operator=(const CacheHandler&) = delete;
460
461 public:                                    // Cache container list
462   FontDescription mDefaultFontDescription; ///< Cached default font from the system
463
464   FontList         mSystemFonts;              ///< Cached system fonts.
465   FontList         mDefaultFonts;             ///< Cached default fonts.
466   CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
467
468   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
469
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.
475
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.
477
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.
480
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.
485
486 private:                                                 // Member value
487   std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
488
489   FontDescription   mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
490   FontDescriptionId mLatestFoundFontDescriptionId;
491
492   FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
493   FontCacheIndex              mLatestFoundCacheIndex;
494
495   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
496 };
497
498 } // namespace Dali::TextAbstraction::Internal
499
500 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H