Seperate font client plugin file.
[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
24 namespace Dali::TextAbstraction::Internal
25 {
26 /**
27  * @brief FontClient Plugin cache item handler.
28  */
29 struct FontClient::Plugin::CacheHandler
30 {
31 public:
32   /**
33    * Constructor.
34    */
35   CacheHandler();
36
37   /**
38    * Default destructor.
39    *
40    * Frees any allocated resource.
41    */
42   ~CacheHandler();
43
44 public: // Public struct
45   /// Redefine FontId name to specifiy the value's usage
46   using FontCacheIndex     = FontId;
47   using EllipsisCacheIndex = FontId;
48
49   /**
50    * @brief Index of FontCache container.
51    */
52   struct FontIdCacheItem
53   {
54     FontDescription::Type type;  ///< The type of font.
55     FontCacheIndex        index; ///< Index to the cache of fonts for the specified type. Face or Bitmap
56   };
57
58   /**
59    * @brief Caches an list of fallback fonts for a given font-description
60    */
61   struct FallbackCacheItem
62   {
63     FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
64
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.
68   };
69
70   /**
71    * @brief Caches an glyph informations of ellipsis character per each point size.
72    */
73   struct EllipsisItem
74   {
75     PointSize26Dot6    requestedPointSize;
76     EllipsisCacheIndex index;
77     GlyphInfo          glyph;
78   };
79
80   /**
81    * @brief Caches an index to the vector of font descriptions for a given font.
82    */
83   struct FontDescriptionCacheItem
84   {
85     FontDescriptionCacheItem(const FontDescription& fontDescription,
86                              FontDescriptionId      index);
87     FontDescriptionCacheItem(FontDescription&& fontDescription,
88                              FontDescriptionId index);
89
90     FontDescription   fontDescription; ///< The font description.
91     FontDescriptionId index;           ///< Index to the vector of font descriptions.
92   };
93
94   /**
95    * @brief Pair of FontDescriptionId and PointSize. It will be used to find cached validate font.
96    */
97   struct FontDescriptionSizeCacheKey
98   {
99     FontDescriptionSizeCacheKey(FontDescriptionId fontDescriptionId,
100                                 PointSize26Dot6   requestedPointSize);
101
102     FontDescriptionId fontDescriptionId;  ///< Index to the vector with font descriptions.
103     PointSize26Dot6   requestedPointSize; ///< The font point size.
104
105     bool operator==(FontDescriptionSizeCacheKey const& rhs) const noexcept
106     {
107       return fontDescriptionId == rhs.fontDescriptionId && requestedPointSize == rhs.requestedPointSize;
108     }
109   };
110
111   /**
112    * @brief Custom hash functions for FontDescriptionSizeCacheKey.
113    */
114   struct FontDescriptionSizeCacheKeyHash
115   {
116     std::size_t operator()(FontDescriptionSizeCacheKey const& key) const noexcept
117     {
118       return key.fontDescriptionId ^ key.requestedPointSize;
119     }
120   };
121
122   /**
123    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
124    */
125   using FontDescriptionSizeCacheContainer = std::unordered_map<FontDescriptionSizeCacheKey, FontCacheIndex, FontDescriptionSizeCacheKeyHash>;
126
127 public: // Clear cache public
128   /**
129    * @copydoc Dali::TextAbstraction::FontClient::Plugin::ClearCache()
130    */
131   void ClearCache();
132
133   /**
134    * @copydoc Dali::TextAbstraction::FontClient::Plugin::ResetSystemDefaults()
135    */
136   void ResetSystemDefaults();
137
138 private: // Clear cache private
139   /**
140    * @brief Free the resources allocated by the FcCharSet objects.
141    */
142   void ClearCharacterSetFromFontFaceCache();
143
144   /**
145    * @brief Free the resources allocated in the fallback cache.
146    */
147   void ClearFallbackCache();
148
149   /**
150    * @brief Free the resources allocated in charset cache.
151    */
152   void ClearCharacterSet();
153
154 private:
155   /**
156    * @brief Crate the charset resouces by default font and Fallback caches.
157    * @pre We should call this API only one times after ClearCharacterSet().
158    */
159   void CreateCharacterSet();
160
161 public: // Find & Cache
162   // System / Default
163
164   /**
165    * @brief Caches the fonts present in the platform.
166    *
167    * Calls GetFcFontSet() to retrieve the fonts.
168    */
169   void InitSystemFonts();
170
171   /**
172    * @brief Retrieve the list of default fonts supported by the system.
173    */
174   void InitDefaultFonts();
175
176   /**
177    * @brief Retrieve the active default font from the system.
178    */
179   void InitDefaultFontDescription();
180
181   // Validate
182
183   /**
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.
186    *
187    * @param[in] fontDescription The font to validate.
188    * @param[out] validatedFontId The index to the vector with font descriptions.
189    *
190    * @return @e true if the pair is found.
191    */
192   bool FindValidatedFont(const FontDescription& fontDescription,
193                          FontDescriptionId&     validatedFontId);
194
195   /**
196    * @brief Validate a font description.
197    *
198    * @param[in] fontDescription The font to validate.
199    * @param[out] fontDescriptionId Result of validation
200    */
201   void ValidateFont(const FontDescription& fontDescription,
202                     FontDescriptionId&     fontDescriptionId);
203
204   /**
205    * @brief Cache in the descrption and validate id information
206    * @note We use std::move operation to fontDescription.
207    *
208    * @param[in] fontDescription The font to validate.
209    * @param[in] validatedFontId The index to the vector with font descriptions.
210    */
211   void CacheValidateFont(FontDescription&& fontDescription,
212                          FontDescriptionId validatedFontId);
213
214   // Fallback
215
216   /**
217    * @brief Finds a fallback font list from the cache for a given font-description
218    *
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.
222    *
223    * @return Whether the fallback font list has been found.
224    */
225   bool FindFallbackFontList(const FontDescription& fontDescription,
226                             FontList*&             fontList,
227                             CharacterSetList*&     characterSetList) const;
228
229   /**
230    * @brief Cache a fallback font list for a given font-description
231    * @note We use std::move operation to fontDescription.
232    *
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.
236    */
237   void CacheFallbackFontList(FontDescription&&  fontDescription,
238                              FontList*&         fontList,
239                              CharacterSetList*& characterSetList);
240
241   // Font / FontFace
242
243   /**
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.
246    *
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.
251    *
252    * @return @e true if there triplet is found.
253    */
254   bool FindFontByPath(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
255
256   /**
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.
259    *
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.
263    *
264    * @return @e true if the pair is found.
265    */
266   bool FindFont(FontDescriptionId validatedFontId,
267                 PointSize26Dot6   requestedPointSize,
268                 FontCacheIndex&   fontCacheIndex);
269
270   /**
271    * @brief Cache the font descpription size item.
272    *
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.
276    */
277   void CacheFontDescriptionSize(FontDescriptionId fontDescriptionId, PointSize26Dot6 requestedPointSize, FontCacheIndex fontCacheIndex);
278
279   /**
280    * @brief Cache the font face cache item.
281    * @note We use std::move operation to cache item.
282    *
283    * @param[in] fontFaceCacheItem Font face cache item.
284    * @return FontId of newly inserted font cache item.
285    */
286   FontId CacheFontFaceCacheItem(FontFaceCacheItem&& fontFaceCacheItem);
287
288   /**
289    * @brief Caches a font path.
290    *
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.
295    */
296   void CacheFontPath(FT_Face ftFace, FontId fontId, PointSize26Dot6 requestedPointSize, const FontPath& path);
297
298   // Ellipsis
299
300   /**
301    * @brief Finds an ellipsis cache for a given point size
302    *
303    * @param[in] requestedPointSize Requested point size.
304    * @param[out] ellipsisCacheIndex The index of cached ellipsis.
305    *
306    * @return Whether the ellipsis has been found.
307    */
308   bool FindEllipsis(PointSize26Dot6 requestedPointSize, EllipsisCacheIndex& ellipsisCacheIndex) const;
309
310   /**
311    * @brief Cache an ellipsis item
312    * @note We use std::move operation to cache item.
313    *
314    * @param[in] ellipsisItem Ellipsis item.
315    * @return The index of cached ellipsis.
316    */
317   EllipsisCacheIndex CacheEllipsis(EllipsisItem&& ellipsisItem);
318
319   // Bitmap font
320
321   /**
322    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
323    *
324    * @param[in] bitmapFontFamily The font's family name.
325    * @param[out] fontId The id of the font.
326    *
327    * @return Whether the font has been found.
328    */
329   bool FindBitmapFont(const FontFamily& bitmapFontFamily, FontId& fontId) const;
330
331   /**
332    * @brief Cache the bitmap font cache item.
333    * @note We use std::move operation to cache item.
334    *
335    * @param[in] bitmapFontCacheItem Bitmap font cache item.
336    * @return FontId of newly inserted font cache item.
337    */
338   FontId CacheBitmapFontCacheItem(BitmapFontCacheItem&& bitmapFontCacheItem);
339
340   // Embedded
341
342   /**
343    * @brief Finds in the cache a pixel buffer for embedded font.
344    *
345    * @param[in] url The embedded image's url.
346    * @param[out] pixelBufferId The id of the loaded pixel buffer.
347    *
348    * @return Whether the embedded pixel buffer has been found.
349    */
350   bool FindEmbeddedPixelBufferId(const std::string& url, PixelBufferId& pixelBufferId) const;
351
352   /**
353    * @brief Cache the pixel buffer
354    * @note We load image syncronously.
355    *
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.
358    */
359   PixelBufferId CacheEmbeddedPixelBuffer(const std::string& url);
360
361   /**
362    * @brief Finds in the cache a embedded item.
363    *
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.
369    */
370   bool FindEmbeddedItem(PixelBufferId pixelBufferId, uint32_t width, uint32_t height, GlyphIndex& index) const;
371
372   /**
373    * @brief Cache the embedded item.
374    * @note We use std::move operation to cache item.
375    *
376    * @param[in] embeddedItem The url of embedded pixel buffer.
377    * @return GlyphIndex of newly inserted embedded item.
378    */
379   GlyphIndex CacheEmbeddedItem(EmbeddedItem&& embeddedItem);
380
381 private:
382   CacheHandler(const CacheHandler&) = delete;
383   CacheHandler& operator=(const CacheHandler&) = delete;
384
385 public:                                    // Cache container list
386   FontDescription mDefaultFontDescription; ///< Cached default font from the system
387
388   FontList         mSystemFonts;              ///< Cached system fonts.
389   FontList         mDefaultFonts;             ///< Cached default fonts.
390   CharacterSetList mDefaultFontCharacterSets; ///< Cached default fonts character set.
391
392   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
393
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.
399
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.
401
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.
406
407 private:                                         // Member value
408   FontDescription   mLatestFoundFontDescription; ///< Latest found font description and id in FindValidatedFont()
409   FontDescriptionId mLatestFoundFontDescriptionId;
410
411   FontDescriptionSizeCacheKey mLatestFoundCacheKey; ///< Latest found font description and id in FindFont()
412   FontCacheIndex              mLatestFoundCacheIndex;
413
414   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
415 };
416
417 } // namespace Dali::TextAbstraction::Internal
418
419 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_CACHE_HANDLER_H