Refactored font-client-plugin-impl
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / font-client-plugin-impl.h
1 #ifndef DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H
2 #define DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H
3
4 /*
5  * Copyright (c) 2021 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/devel-api/adaptor-framework/pixel-buffer.h>
23 #include <dali/devel-api/text-abstraction/bitmap-font.h>
24 #include <dali/devel-api/text-abstraction/font-metrics.h>
25 #include <dali/devel-api/text-abstraction/glyph-info.h>
26 #include <dali/internal/text/text-abstraction/font-client-impl.h>
27 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
28 #include <dali/internal/text/text-abstraction/plugin/embedded-item.h>
29 #include <dali/internal/text/text-abstraction/plugin/font-face-cache-item.h>
30 #include <dali/internal/text/text-abstraction/plugin/pixel-buffer-cache-item.h>
31
32 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
33 #include <third-party/glyphy/vector-font-cache.h>
34 #else
35 class VectorFontCache;
36 #endif
37
38 // EXTERNAL INCLUDES
39 #include <ft2build.h>
40 #include FT_FREETYPE_H
41 #include FT_GLYPH_H
42 #include FT_STROKER_H
43 #include FT_SYNTHESIS_H
44
45 // forward declarations of font config types.
46 struct _FcCharSet;
47 struct _FcFontSet;
48 struct _FcPattern;
49
50 namespace Dali
51 {
52 namespace TextAbstraction
53 {
54 namespace Internal
55 {
56 /**
57  * @brief Type used for indices addressing the vector with front descriptions of validated fonts.
58  */
59 typedef uint32_t FontDescriptionId;
60
61 /**
62  * @brief Vector of character sets.
63  */
64 typedef Vector<_FcCharSet*> CharacterSetList;
65
66 /**
67  * @brief FontClient implementation.
68  */
69 struct FontClient::Plugin
70 {
71   struct FontIdCacheItem
72   {
73     FontDescription::Type type; ///< The type of font.
74     FontId                id;   ///< Index to the cache of fonts for the specified type.
75   };
76
77   /**
78    * @brief Caches an list of fallback fonts for a given font-description
79    */
80   struct FallbackCacheItem
81   {
82     FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
83
84     FontDescription   fontDescription; ///< The font description.
85     FontList*         fallbackFonts;   ///< The list of fallback fonts for the given font-description.
86     CharacterSetList* characterSets;   ///< The list of character sets for the given font-description.
87   };
88
89   /**
90    * @brief Caches an index to the vector of font descriptions for a given font.
91    */
92   struct FontDescriptionCacheItem
93   {
94     FontDescriptionCacheItem(const FontDescription& fontDescription,
95                              FontDescriptionId      index);
96     FontDescriptionCacheItem(FontDescription&& fontDescription,
97                              FontDescriptionId index);
98
99     FontDescription   fontDescription; ///< The font description.
100     FontDescriptionId index;           ///< Index to the vector of font descriptions.
101   };
102
103   /**
104    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
105    */
106   struct FontDescriptionSizeCacheItem
107   {
108     FontDescriptionSizeCacheItem(FontDescriptionId validatedFontId,
109                                  PointSize26Dot6   requestedPointSize,
110                                  FontId            fontId);
111
112     FontDescriptionId validatedFontId;    ///< Index to the vector with font descriptions.
113     PointSize26Dot6   requestedPointSize; ///< The font point size.
114     FontId            fontId;             ///< The font identifier.
115   };
116
117   struct EllipsisItem
118   {
119     PointSize26Dot6 requestedPointSize;
120     GlyphInfo       glyph;
121   };
122
123   /**
124    * Constructor.
125    *
126    * Initializes the FreeType library.
127    * Initializes the dpi values.
128    *
129    * @param[in] horizontalDpi The horizontal dpi.
130    * @param[in] verticalDpi The vertical dpi.
131    */
132   Plugin(unsigned int horizontalDpi, unsigned int verticalDpi);
133
134   /**
135    * Default destructor.
136    *
137    * Frees any allocated resource.
138    */
139   ~Plugin();
140
141   /**
142    * @copydoc Dali::TextAbstraction::FontClient::ClearCache()
143    */
144   void ClearCache();
145
146   /**
147    * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
148    */
149   void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
150
151   /**
152    * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
153    */
154   void ResetSystemDefaults();
155
156   /**
157    * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
158    */
159   void SetDefaultFont(const FontDescription& preferredFontDescription);
160
161   /**
162    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
163    */
164   void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
165
166   /**
167    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
168    */
169   void GetDefaultFonts(FontList& defaultFonts);
170
171   /**
172    * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
173    */
174   void GetSystemFonts(FontList& systemFonts);
175
176   /**
177    * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
178    */
179   void GetDescription(FontId id, FontDescription& fontDescription) const;
180
181   /**
182    * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
183    */
184   PointSize26Dot6 GetPointSize(FontId id);
185
186   /**
187    * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
188    */
189   bool IsCharacterSupportedByFont(FontId fontId, Character character);
190
191   /**
192    * Get the cached font item for the given font
193    * @param[in] id The font id to search for
194    * @return the matching cached font item
195    */
196   const FontCacheItemInterface* GetCachedFontItem(FontId id) const;
197
198   /**
199    * @brief Finds within the @p fontList a font which support the @p carcode.
200    *
201    * @param[in] fontList A list of font paths, family, width, weight and slant.
202    * @param[in] characterSetList A list that contains a character set for each description of the font list.
203    * @param[in] charcode The character for which a font is needed.
204    * @param[in] requestedPointSize The point size in 26.6 fractional points.
205    * @param[in] preferColor @e true if a color font is preferred.
206    *
207    * @return A valid font identifier, or zero if no font is found.
208    */
209   FontId FindFontForCharacter(const FontList&         fontList,
210                               const CharacterSetList& characterSetList,
211                               Character               charcode,
212                               PointSize26Dot6         requestedPointSize,
213                               bool                    preferColor);
214
215   /**
216    * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
217    */
218   FontId FindDefaultFont(Character       charcode,
219                          PointSize26Dot6 requestedPointSize,
220                          bool            preferColor);
221
222   /**
223    * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
224    */
225   FontId FindFallbackFont(Character              charcode,
226                           const FontDescription& preferredFontDescription,
227                           PointSize26Dot6        requestedPointSize,
228                           bool                   preferColor);
229
230   /**
231    * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
232    *
233    * @param[in] cacheDescription Whether to cache the font description.
234    */
235   FontId GetFontId(const FontPath& path,
236                    PointSize26Dot6 requestedPointSize,
237                    FaceIndex       faceIndex,
238                    bool            cacheDescription);
239
240   /**
241    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
242    */
243   FontId GetFontId(const FontDescription& fontDescription,
244                    PointSize26Dot6        requestedPointSize,
245                    FaceIndex              faceIndex);
246
247   /**
248    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const BitmapFont& bitmapFont )
249    */
250   FontId GetFontId(const BitmapFont& bitmapFont);
251
252   /**
253    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
254    */
255   bool IsScalable(const FontPath& path);
256
257   /**
258    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
259    */
260   bool IsScalable(const FontDescription& fontDescription);
261
262   /**
263    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
264    */
265   void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
266
267   /**
268    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
269    */
270   void GetFixedSizes(const FontDescription&         fontDescription,
271                      Dali::Vector<PointSize26Dot6>& sizes);
272
273   /**
274    * @copydoc Dali::TextAbstraction::FontClient::HasItalicStyle()
275    */
276   bool HasItalicStyle(FontId fontId) const;
277
278   /**
279    * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
280    */
281   void GetFontMetrics(FontId fontId, FontMetrics& metrics);
282
283   /**
284    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
285    */
286   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
287
288   /**
289    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
290    */
291   bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal);
292
293   /**
294    * Helper for GetGlyphMetrics when using bitmaps
295    */
296   bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
297
298   /**
299    * Helper for GetGlyphMetrics when using vectors
300    */
301   bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
302
303   /**
304    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
305    */
306   void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth);
307
308   /**
309    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
310    */
311   PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
312
313   /**
314    * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
315    */
316   void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight);
317
318   /**
319    * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
320    */
321   const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
322
323   /**
324    * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
325    */
326   bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
327
328   /**
329    * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
330    */
331   GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
332
333   /**
334    * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
335    */
336   void EnableAtlasLimitation(bool enabled);
337
338   /**
339    * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
340    */
341   bool IsAtlasLimitationEnabled() const;
342
343   /**
344    * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
345    */
346   Size GetMaximumTextAtlasSize() const;
347
348   /**
349    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
350    */
351   Size GetDefaultTextAtlasSize() const;
352
353   /**
354    * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
355    */
356   Size GetCurrentMaximumBlockSizeFitInAtlas() const;
357
358   /**
359    * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
360    */
361   bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
362
363   /**
364    * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
365    */
366   uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
367
368   /**
369    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
370    */
371   FT_FaceRec_* GetFreetypeFace(FontId fontId);
372
373   /**
374    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
375    */
376   FontDescription::Type GetFontType(FontId fontId);
377
378   /**
379    * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
380    */
381   bool AddCustomFontDirectory(const FontPath& path);
382
383 private:
384   /**
385    * @brief Caches the fonts present in the platform.
386    *
387    * Calls GetFcFontSet() to retrieve the fonts.
388    */
389   void InitSystemFonts();
390
391   /**
392    * @brief Gets the FontDescription which matches the given pattern.
393    *
394    * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
395    *
396    * @param[in] pattern pattern to match against.
397    * @param[out] fontDescription the resultant fontDescription that matched.
398    * @param[out] characterSet The character set for that pattern.
399    * @return true if match found.
400    */
401   bool MatchFontDescriptionToPattern(_FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet);
402
403   /**
404    * @brief Retrieves the fonts present in the platform.
405    *
406    * @note Need to call FcFontSetDestroy to free the allocated resources.
407    *
408    * @return A font fonfig data structure with the platform's fonts.
409    */
410   _FcFontSet* GetFcFontSet() const;
411
412   /**
413    * @brief Retrieves a font config object's value from a pattern.
414    *
415    * @param[in] pattern The font config pattern.
416    * @param[in] n The object.
417    * @param[out] string The object's value.
418    *
419    * @return @e true if the operation is successful.
420    */
421   bool GetFcString(const _FcPattern* const pattern, const char* const n, std::string& string);
422
423   /**
424    * @brief Retrieves a font config object's value from a pattern.
425    *
426    * @param[in] pattern The font config pattern.
427    * @param[in] n The object.
428    * @param[out] intVal The object's value.
429    *
430    * @return @e true if the operation is successful.
431    */
432   bool GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal);
433
434   /**
435    * @brief Creates a font.
436    *
437    * @param[in] path The path to the font file name.
438    * @param[in] requestedPointSize The requested point size.
439    * @param[in] faceIndex A face index.
440    * @param[in] cacheDescription Whether to cache the font description.
441    *
442    * @return The font identifier.
443    */
444   FontId CreateFont(const FontPath& path,
445                     PointSize26Dot6 requestedPointSize,
446                     FaceIndex       faceIndex,
447                     bool            cacheDescription);
448
449   /**
450    * @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.
451    * If there is one , if writes the font identifier in the param @p fontId.
452    *
453    * @param[in] path Path to the font file name.
454    * @param[in] requestedPointSize The font point size.
455    * @param[in] faceIndex The face index.
456    * @param[out] fontId The font identifier.
457    *
458    * @return @e true if there triplet is found.
459    */
460   bool FindFont(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
461
462   /**
463    * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
464    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
465    *
466    * @param[in] fontDescription The font to validate.
467    * @param[out] validatedFontId The index to the vector with font descriptions.
468    *
469    * @return @e true if the pair is found.
470    */
471   bool FindValidatedFont(const FontDescription& fontDescription,
472                          FontDescriptionId&     validatedFontId);
473
474   /**
475    * @brief Finds a fallback font list from the cache for a given font-description
476    *
477    * @param[in] fontDescription The font to validate.
478    * @param[out] A valid pointer to a font list, or @e nullptr if not found.
479    * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
480    */
481   bool FindFallbackFontList(const FontDescription& fontDescription,
482                             FontList*&             fontList,
483                             CharacterSetList*&     characterSetList);
484
485   /**
486    * @brief Finds in the cache a pair 'validated font identifier and font point size'.
487    * If there is one it writes the font identifier in the param @p fontId.
488    *
489    * @param[in] validatedFontId Index to the vector with font descriptions.
490    * @param[in] requestedPointSize The font point size.
491    * @param[out] fontId The font identifier.
492    *
493    * @return @e true if the pair is found.
494    */
495   bool FindFont(FontDescriptionId validatedFontId,
496                 PointSize26Dot6   requestedPointSize,
497                 FontId&           fontId);
498
499   /**
500    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
501    *
502    * @param[in] bitmapFont The font's family name.
503    * @param[out] fontId The id of the font.
504    *
505    * @return Whether the font has been found.
506    */
507   bool FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const;
508
509   /**
510    * @brief Validate a font description.
511    *
512    * @param[in] fontDescription The font to validate.
513    * @param[out] validatedFontId Result of validation
514    */
515   void ValidateFont(const FontDescription& fontDescription,
516                     FontDescriptionId&     validatedFontId);
517
518   /**
519    * @brief Helper for GetDefaultFonts etc.
520    *
521    * @note CharacterSetList is a vector of FcCharSet that are reference counted. It's needed to call FcCharSetDestroy to decrease the reference counter.
522    *
523    * @param[in] fontDescription A font description.
524    * @param[out] fontList A list of the fonts which are a close match for fontDescription.
525    * @param[out] characterSetList A list of character sets which are a close match for fontDescription.
526    */
527   void SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList);
528
529   /**
530    * Caches a font path.
531    *
532    * @param[in] ftFace The FreeType face.
533    * @param[in] id The font identifier.
534    * @param[in] requestedPointSize The font point size.
535    * @param[in] path Path to the font file name.
536    */
537   void CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path);
538
539   /**
540    * @brief Free the resources allocated in the fallback cache.
541    *
542    * @param[in] fallbackCache The fallback cache.
543    */
544   void ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache);
545
546   /**
547    * @brief Free the resources allocated by the FcCharSet objects.
548    */
549   void ClearCharacterSetFromFontFaceCache();
550
551 private:
552   Plugin(const Plugin&) = delete;
553   Plugin& operator=(const Plugin&) = delete;
554
555 private:
556   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
557
558   unsigned int mDpiHorizontal; ///< Horizontal dpi.
559   unsigned int mDpiVertical;   ///< Vertical dpi.
560
561   FontDescription mDefaultFontDescription; ///< The cached default font from the system
562
563   FontList         mSystemFonts;  ///< Cached system fonts.
564   FontList         mDefaultFonts; ///< Cached default fonts.
565   CharacterSetList mDefaultFontCharacterSets;
566
567   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
568
569   Vector<FontIdCacheItem>                   mFontIdCache;
570   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'.
571   std::vector<FontDescriptionCacheItem>     mValidatedFontCache;       ///< Caches indices to the vector of font descriptions for a given font.
572   FontList                                  mFontDescriptionCache;     ///< Caches font descriptions for the validated font.
573   CharacterSetList                          mCharacterSetCache;        ///< Caches character set lists for the validated font.
574   std::vector<FontDescriptionSizeCacheItem> mFontDescriptionSizeCache; ///< Caches font identifiers for the pairs of font point size and the index to the vector with font descriptions of the validated fonts.
575
576   VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
577
578   Vector<EllipsisItem>              mEllipsisCache;     ///< Caches ellipsis glyphs for a particular point size.
579   std::vector<PixelBufferCacheItem> mPixelBufferCache;  ///< Caches the pixel buffer of a url.
580   Vector<EmbeddedItem>              mEmbeddedItemCache; ///< Cache embedded items.
581   std::vector<BitmapFontCacheItem>  mBitmapFontCache;   ///< Stores bitmap fonts.
582
583   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
584
585   bool    mIsAtlasLimitationEnabled : 1;      ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
586   Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
587 };
588
589 } // namespace Internal
590
591 } // namespace TextAbstraction
592
593 } // namespace Dali
594
595 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H