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