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