Add set of APIs for emoji-character-properties
[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::GetGlyphIndex()
290    */
291   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
292
293   /**
294    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
295    */
296   bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal);
297
298   /**
299    * Helper for GetGlyphMetrics when using bitmaps
300    */
301   bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
302
303   /**
304    * Helper for GetGlyphMetrics when using vectors
305    */
306   bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
307
308   /**
309    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
310    */
311   void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth);
312
313   /**
314    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
315    */
316   PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
317
318   /**
319    * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
320    */
321   void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight);
322
323   /**
324    * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
325    */
326   const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
327
328   /**
329    * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
330    */
331   bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
332
333   /**
334    * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
335    */
336   GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
337
338   /**
339    * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
340    */
341   void EnableAtlasLimitation(bool enabled);
342
343   /**
344    * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
345    */
346   bool IsAtlasLimitationEnabled() const;
347
348   /**
349    * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
350    */
351   Size GetMaximumTextAtlasSize() const;
352
353   /**
354    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
355    */
356   Size GetDefaultTextAtlasSize() const;
357
358   /**
359    * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
360    */
361   Size GetCurrentMaximumBlockSizeFitInAtlas() const;
362
363   /**
364    * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
365    */
366   bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
367
368   /**
369    * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
370    */
371   uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
372
373   /**
374    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
375    */
376   FT_FaceRec_* GetFreetypeFace(FontId fontId);
377
378   /**
379    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
380    */
381   FontDescription::Type GetFontType(FontId fontId);
382
383   /**
384    * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
385    */
386   bool AddCustomFontDirectory(const FontPath& path);
387
388 private:
389   /**
390    * @brief Caches the fonts present in the platform.
391    *
392    * Calls GetFcFontSet() to retrieve the fonts.
393    */
394   void InitSystemFonts();
395
396   /**
397    * @brief Gets the FontDescription which matches the given pattern.
398    *
399    * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
400    *
401    * @param[in] pattern pattern to match against.
402    * @param[out] fontDescription the resultant fontDescription that matched.
403    * @param[out] characterSet The character set for that pattern.
404    * @return true if match found.
405    */
406   bool MatchFontDescriptionToPattern(_FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet);
407
408   /**
409    * @brief Retrieves the fonts present in the platform.
410    *
411    * @note Need to call FcFontSetDestroy to free the allocated resources.
412    *
413    * @return A font fonfig data structure with the platform's fonts.
414    */
415   _FcFontSet* GetFcFontSet() const;
416
417   /**
418    * @brief Retrieves a font config object's value from a pattern.
419    *
420    * @param[in] pattern The font config pattern.
421    * @param[in] n The object.
422    * @param[out] string The object's value.
423    *
424    * @return @e true if the operation is successful.
425    */
426   bool GetFcString(const _FcPattern* const pattern, const char* const n, std::string& string);
427
428   /**
429    * @brief Retrieves a font config object's value from a pattern.
430    *
431    * @param[in] pattern The font config pattern.
432    * @param[in] n The object.
433    * @param[out] intVal The object's value.
434    *
435    * @return @e true if the operation is successful.
436    */
437   bool GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal);
438
439   /**
440    * @brief Creates a font.
441    *
442    * @param[in] path The path to the font file name.
443    * @param[in] requestedPointSize The requested point size.
444    * @param[in] faceIndex A face index.
445    * @param[in] cacheDescription Whether to cache the font description.
446    *
447    * @return The font identifier.
448    */
449   FontId CreateFont(const FontPath& path,
450                     PointSize26Dot6 requestedPointSize,
451                     FaceIndex       faceIndex,
452                     bool            cacheDescription);
453
454   /**
455    * @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.
456    * If there is one , if writes the font identifier in the param @p fontId.
457    *
458    * @param[in] path Path to the font file name.
459    * @param[in] requestedPointSize The font point size.
460    * @param[in] faceIndex The face index.
461    * @param[out] fontId The font identifier.
462    *
463    * @return @e true if there triplet is found.
464    */
465   bool FindFont(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
466
467   /**
468    * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
469    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
470    *
471    * @param[in] fontDescription The font to validate.
472    * @param[out] validatedFontId The index to the vector with font descriptions.
473    *
474    * @return @e true if the pair is found.
475    */
476   bool FindValidatedFont(const FontDescription& fontDescription,
477                          FontDescriptionId&     validatedFontId);
478
479   /**
480    * @brief Finds a fallback font list from the cache for a given font-description
481    *
482    * @param[in] fontDescription The font to validate.
483    * @param[out] A valid pointer to a font list, or @e nullptr if not found.
484    * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
485    */
486   bool FindFallbackFontList(const FontDescription& fontDescription,
487                             FontList*&             fontList,
488                             CharacterSetList*&     characterSetList);
489
490   /**
491    * @brief Finds in the cache a pair 'validated font identifier and font point size'.
492    * If there is one it writes the font identifier in the param @p fontId.
493    *
494    * @param[in] validatedFontId Index to the vector with font descriptions.
495    * @param[in] requestedPointSize The font point size.
496    * @param[out] fontId The font identifier.
497    *
498    * @return @e true if the pair is found.
499    */
500   bool FindFont(FontDescriptionId validatedFontId,
501                 PointSize26Dot6   requestedPointSize,
502                 FontId&           fontId);
503
504   /**
505    * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
506    *
507    * @param[in] bitmapFont The font's family name.
508    * @param[out] fontId The id of the font.
509    *
510    * @return Whether the font has been found.
511    */
512   bool FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const;
513
514   /**
515    * @brief Validate a font description.
516    *
517    * @param[in] fontDescription The font to validate.
518    * @param[out] validatedFontId Result of validation
519    */
520   void ValidateFont(const FontDescription& fontDescription,
521                     FontDescriptionId&     validatedFontId);
522
523   /**
524    * @brief Helper for GetDefaultFonts etc.
525    *
526    * @note CharacterSetList is a vector of FcCharSet that are reference counted. It's needed to call FcCharSetDestroy to decrease the reference counter.
527    *
528    * @param[in] fontDescription A font description.
529    * @param[out] fontList A list of the fonts which are a close match for fontDescription.
530    * @param[out] characterSetList A list of character sets which are a close match for fontDescription.
531    */
532   void SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList);
533
534   /**
535    * Caches a font path.
536    *
537    * @param[in] ftFace The FreeType face.
538    * @param[in] id The font identifier.
539    * @param[in] requestedPointSize The font point size.
540    * @param[in] path Path to the font file name.
541    */
542   void CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path);
543
544   /**
545    * @brief Free the resources allocated in the fallback cache.
546    *
547    * @param[in] fallbackCache The fallback cache.
548    */
549   void ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache);
550
551   /**
552    * @brief Free the resources allocated by the FcCharSet objects.
553    */
554   void ClearCharacterSetFromFontFaceCache();
555
556 private:
557   Plugin(const Plugin&) = delete;
558   Plugin& operator=(const Plugin&) = delete;
559
560 private:
561   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
562
563   unsigned int mDpiHorizontal; ///< Horizontal dpi.
564   unsigned int mDpiVertical;   ///< Vertical dpi.
565
566   FontDescription mDefaultFontDescription; ///< The cached default font from the system
567
568   FontList         mSystemFonts;  ///< Cached system fonts.
569   FontList         mDefaultFonts; ///< Cached default fonts.
570   CharacterSetList mDefaultFontCharacterSets;
571
572   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
573
574   Vector<FontIdCacheItem>                   mFontIdCache;
575   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'.
576   std::vector<FontDescriptionCacheItem>     mValidatedFontCache;       ///< Caches indices to the vector of font descriptions for a given font.
577   FontList                                  mFontDescriptionCache;     ///< Caches font descriptions for the validated font.
578   CharacterSetList                          mCharacterSetCache;        ///< Caches character set lists for the validated font.
579   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.
580
581   VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
582
583   Vector<EllipsisItem>              mEllipsisCache;     ///< Caches ellipsis glyphs for a particular point size.
584   std::vector<PixelBufferCacheItem> mPixelBufferCache;  ///< Caches the pixel buffer of a url.
585   Vector<EmbeddedItem>              mEmbeddedItemCache; ///< Cache embedded items.
586   std::vector<BitmapFontCacheItem>  mBitmapFontCache;   ///< Stores bitmap fonts.
587
588   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
589
590   bool    mIsAtlasLimitationEnabled : 1;      ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
591   Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
592 };
593
594 } // namespace Internal
595
596 } // namespace TextAbstraction
597
598 } // namespace Dali
599
600 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H