20c599532496327f239c0bf9fab29f9e9c2a6406
[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) 2022 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 private:
156   /**
157    * @brief Crate the charset resouces by default font and Fallback caches.
158    * @pre We should call this API only one times after ClearCharacterSet().
159    */
160   void CreateCharacterSet();
161
162 public: // Find & Cache
163   // System / Default
164
165   /**
166    * @brief Caches the fonts present in the platform.
167    *
168    * Calls GetFcFontSet() to retrieve the fonts.
169    */
170   void InitSystemFonts();
171
172   /**
173    * @brief Retrieve the list of default fonts supported by the system.
174    */
175   void InitDefaultFonts();
176
177   /**
178    * @brief Retrieve the active default font from the system.
179    */
180   void InitDefaultFontDescription();
181
182   // Validate
183
184   /**
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.
187    *
188    * @param[in] fontDescription The font to validate.
189    * @param[out] validatedFontId The index to the vector with font descriptions.
190    *
191    * @return @e true if the pair is found.
192    */
193   bool FindValidatedFont(const FontDescription& fontDescription,
194                          FontDescriptionId&     validatedFontId);
195
196   /**
197    * @brief Validate a font description.
198    *
199    * @param[in] fontDescription The font to validate.
200    * @param[out] fontDescriptionId Result of validation
201    */
202   void ValidateFont(const FontDescription& fontDescription,
203                     FontDescriptionId&     fontDescriptionId);
204
205   /**
206    * @brief Cache in the descrption and validate id information
207    * @note We use std::move operation to fontDescription.
208    *
209    * @param[in] fontDescription The font to validate.
210    * @param[in] validatedFontId The index to the vector with font descriptions.
211    */
212   void CacheValidateFont(FontDescription&& fontDescription,
213                          FontDescriptionId validatedFontId);
214
215   // Fallback
216
217   /**
218    * @brief Finds a fallback font list from the cache for a given font-description
219    *
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.
223    *
224    * @return Whether the fallback font list has been found.
225    */
226   bool FindFallbackFontList(const FontDescription& fontDescription,
227                             FontList*&             fontList,
228                             CharacterSetList*&     characterSetList) const;
229
230   /**
231    * @brief Cache a fallback font list for a given font-description
232    * @note We use std::move operation to fontDescription.
233    *
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.
237    */
238   void CacheFallbackFontList(FontDescription&&  fontDescription,
239                              FontList*&         fontList,
240                              CharacterSetList*& characterSetList);
241
242   // Font / FontFace
243
244   /**
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.
247    *
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.
252    *
253    * @return @e true if there triplet is found.
254    */
255   bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
256
257   /**
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.
260    *
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.
264    *
265    * @return @e true if the pair is found.
266    */
267   bool FindFont(FontDescriptionId validatedFontId,
268                 PointSize26Dot6   requestedPointSize,
269                 FontCacheIndex&   fontCacheIndex);
270
271   /**
272    * @brief Cache the font descpription size item.
273    *
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.
277    */
278   void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
279
280   /**
281    * @brief Cache the font face cache item.
282    * @note We use std::move operation to cache item.
283    *
284    * @param[in] fontFaceCacheItem Font face cache item.
285    * @return FontId of newly inserted font cache item.
286    */
287   FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
288
289   /**
290    * @brief Caches a font path.
291    *
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.
296    */
297   void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
298
299   // Ellipsis
300
301   /**
302    * @brief Finds an ellipsis cache for a given point size
303    *
304    * @param[in] requestedPointSize Requested point size.
305    * @param[out] ellipsisCacheIndex The index of cached ellipsis.
306    *
307    * @return Whether the ellipsis has been found.
308    */
309   bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
310
311   /**
312    * @brief Cache an ellipsis item
313    * @note We use std::move operation to cache item.
314    *
315    * @param[in] ellipsisItem Ellipsis item.
316    * @return The index of cached ellipsis.
317    */
318   EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
319
320   // Bitmap font
321
322   /**
323    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
324    *
325    * @param[in] bitmapFontFamily The font's family name.
326    * @param[out] fontId The id of the font.
327    *
328    * @return Whether the font has been found.
329    */
330   bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
331
332   /**
333    * @brief Cache the bitmap font cache item.
334    * @note We use std::move operation to cache item.
335    *
336    * @param[in] bitmapFontCacheItem Bitmap font cache item.
337    * @return FontId of newly inserted font cache item.
338    */
339   FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
340
341   // Embedded
342
343   /**
344    * @brief Finds in the cache a pixel buffer for embedded font.
345    *
346    * @param[in] url The embedded image's url.
347    * @param[out] pixelBufferId The id of the loaded pixel buffer.
348    *
349    * @return Whether the embedded pixel buffer has been found.
350    */
351   bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
352
353   /**
354    * @brief Cache the pixel buffer
355    * @note We load image syncronously.
356    *
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.
359    */
360   PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
361
362   /**
363    * @brief Finds in the cache a embedded item.
364    *
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.
370    */
371   bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
372
373   /**
374    * @brief Cache the embedded item.
375    * @note We use std::move operation to cache item.
376    *
377    * @param[in] embeddedItem The url of embedded pixel buffer.
378    * @return GlyphIndex of newly inserted embedded item.
379    */
380   GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
381
382 public: // Other public API
383   GlyphCacheManager* GetGlyphCacheManager() const
384   {
385     return mGlyphCacheManager.get();
386   }
387
388 private:
389   CacheHandler(const CacheHandler&) = delete;
390   CacheHandler& operator=(const CacheHandler&) = delete;
391
392 public:                                    // Cache container list
393   FontDescription mDefaultFontDescription; ///< Cached default font from the system
394
395   FontList         mSystemFonts;              ///< Cached system fonts.
396   FontList         mDefaultFonts;             ///< Cached default fonts.
397   CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
398
399   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
400
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.
406
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.
408
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.
413
414 private:                                                 // Member value
415   std::unique_ptr<GlyphCacheManager> mGlyphCacheManager; ///< The glyph cache manager. It will cache this face's glyphs.
416
417   FontDescription   mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
418   FontDescriptionId mLatestFoundFontDescriptionId;
419
420   FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
421   FontCacheIndex              mLatestFoundCacheIndex;
422
423   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
424 };
425
426 } // namespace Dali::TextAbstraction::Internal
427
428 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H