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