Revert "[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 private:
325
326   /**
327    * @brief Caches the fonts present in the platform.
328    *
329    * Calls GetFcFontSet() to retrieve the fonts.
330    */
331   void InitSystemFonts();
332
333   /**
334    * @brief Gets the FontDescription which matches the given pattern.
335    * @param[in] pattern pattern to match against.
336    * @param[out] fontDescription the resultant fontDescription that matched.
337    * @param[out] characterSet The character set for that pattern.
338    * @return true if match found.
339    */
340   bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
341
342   /**
343    * @brief Creates a font family pattern used to match fonts.
344    *
345    * @param[in] fontDescription The font to cache.
346    *
347    * @return The pattern.
348    */
349   _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
350
351   /**
352    * Retrieves the fonts present in the platform.
353    *
354    * @return A font fonfig data structure with the platform's fonts.
355    */
356   _FcFontSet* GetFcFontSet() const;
357
358   /**
359    * Retrieves a font config object's value from a pattern.
360    *
361    * @param[in] pattern The font config pattern.
362    * @param[in] n The object.
363    * @param[out] string The object's value.
364    *
365    * @return @e true if the operation is successful.
366    */
367   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
368
369   /**
370    * Retrieves a font config object's value from a pattern.
371    *
372    * @param[in] pattern The font config pattern.
373    * @param[in] n The object.
374    * @param[out] intVal The object's value.
375    *
376    * @return @e true if the operation is successful.
377    */
378   bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
379
380   /**
381    * @brief Creates a font.
382    *
383    * @param[in] path The path to the font file name.
384    * @param[in] requestedPointSize The requested point size.
385    * @param[in] faceIndex A face index.
386    * @param[in] cacheDescription Whether to cache the font description.
387    *
388    * @return The font identifier.
389    */
390   FontId CreateFont( const FontPath& path,
391                      PointSize26Dot6 requestedPointSize,
392                      FaceIndex faceIndex,
393                      bool cacheDescription );
394
395   /**
396    * @brief Copy the FreeType bitmap to the given buffer.
397    *
398    * @param[out] data The bitmap data.
399    * @param[in] srcBitmap The FreeType bitmap.
400    */
401   void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
402
403   /**
404    * @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.
405    * If there is one , if writes the font identifier in the param @p fontId.
406    *
407    * @param[in] path Path to the font file name.
408    * @param[in] requestedPointSize The font point size.
409    * @param[in] faceIndex The face index.
410    * @param[out] fontId The font identifier.
411    *
412    * @return @e true if there triplet is found.
413    */
414   bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
415
416   /**
417    * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
418    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
419    *
420    * @param[in] fontDescription The font to validate.
421    * @param[out] validatedFontId The index to the vector with font descriptions.
422    *
423    * @return @e true if the pair is found.
424    */
425   bool FindValidatedFont( const FontDescription& fontDescription,
426                           FontDescriptionId& validatedFontId );
427
428   /**
429    * @brief Finds a fallback font list from the cache for a given font-description
430    *
431    * @param[in] fontDescription The font to validate.
432    * @param[out] A valid pointer to a font list, or NULL if not found.
433    * @param[out] characterSetList A valid pointer to a character set list, or NULL if not found.
434    */
435   bool FindFallbackFontList( const FontDescription& fontDescription,
436                              FontList*& fontList,
437                              CharacterSetList*& characterSetList );
438
439   /**
440    * @brief Finds in the cache a pair 'validated font identifier and font point size'.
441    * If there is one it writes the font identifier in the param @p fontId.
442    *
443    * @param[in] validatedFontId Index to the vector with font descriptions.
444    * @param[in] requestedPointSize The font point size.
445    * @param[out] fontId The font identifier.
446    *
447    * @return @e true if the pair is found.
448    */
449   bool FindFont( FontDescriptionId validatedFontId,
450                  PointSize26Dot6 requestedPointSize,
451                  FontId& fontId );
452
453   /**
454    * @brief Validate a font description.
455    *
456    * @param[in] fontDescription The font to validate.
457    * @param[out] validatedFontId Result of validation
458    */
459   void ValidateFont( const FontDescription& fontDescription,
460                      FontDescriptionId& validatedFontId );
461
462   /**
463    * Helper for GetDefaultFonts etc.
464    *
465    * @param[in] fontDescription A font description.
466    * @param[out] fontList A list of the fonts which are a close match for fontDescription.
467    * @param[out] characterSetList A list of the character sets which are a close match for fontDescription.
468    */
469   void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
470
471   /**
472    * Caches a font path.
473    *
474    * @param[in] ftFace The FreeType face.
475    * @param[in] id The font identifier.
476    * @param[in] requestedPointSize The font point size.
477    * @param[in] path Path to the font file name.
478    */
479   void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize,  const FontPath& path );
480
481   /**
482    * @brief Creates a character set from a given font's @p description.
483    *
484    * @param[in] description The font's description.
485    *
486    * @return A character set.
487    */
488   _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description ) const;
489
490 private:
491
492   // Declared private and left undefined to avoid copies.
493   Plugin( const Plugin& );
494   // Declared private and left undefined to avoid copies.
495   Plugin& operator=( const Plugin& );
496
497 private:
498
499   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
500
501   unsigned int mDpiHorizontal; ///< Horizontal dpi.
502   unsigned int mDpiVertical;   ///< Vertical dpi.
503
504   FontDescription mDefaultFontDescription; ///< The cached default font from the system
505
506   FontList mSystemFonts;       ///< Cached system fonts.
507   FontList mDefaultFonts;      ///< Cached default fonts.
508   CharacterSetList mDefaultFontCharacterSets;
509
510   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
511
512   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'.
513   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given font.
514   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font.
515   CharacterSetList                      mCharacterSetCache;    ///< Caches character set lists for the validated font.
516   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.
517
518   VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
519
520   Vector<EllipsisItem> mEllipsisCache;      ///< Caches ellipsis glyphs for a particular point size.
521
522   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
523 };
524
525 } // namespace Internal
526
527 } // namespace TextAbstraction
528
529 } // namespace Dali
530
531 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H