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