Cache font description when a font is created from the file path.
[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) 2015 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 // EXTERNAL INCLUDES
27 #include <ft2build.h>
28 #include FT_FREETYPE_H
29 #include FT_GLYPH_H
30
31 // forward declarations of font config types.
32 struct _FcFontSet;
33 struct _FcPattern;
34
35 namespace Dali
36 {
37
38 namespace TextAbstraction
39 {
40
41 namespace Internal
42 {
43
44 /**
45  *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
46  */
47 typedef uint32_t FontDescriptionId;
48
49 /**
50  * @brief FontClient implementation.
51  */
52 struct FontClient::Plugin
53 {
54   /**
55    * @brief Caches an list of fallback fonts for a given font-description
56    */
57   struct FallbackCacheItem
58   {
59     FallbackCacheItem( const FontDescription& fontDescription, FontList* fallbackFonts );
60
61     FontDescription fontDescription; ///< The font description.
62     FontList* fallbackFonts;         ///< The list of fallback fonts for the given font-description.
63   };
64
65   /**
66    * @brief Caches an index to the vector of font descriptions for a given font.
67    */
68   struct FontDescriptionCacheItem
69   {
70     FontDescriptionCacheItem( const FontDescription& fontDescription,
71                               FontDescriptionId index );
72
73     FontDescription fontDescription; ///< The font description.
74     FontDescriptionId index;         ///< Index to the vector of font descriptions.
75   };
76
77   /**
78    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
79    */
80   struct FontIdCacheItem
81   {
82     FontIdCacheItem( FontDescriptionId validatedFontId,
83                      PointSize26Dot6 pointSize,
84                      FontId fontId );
85
86     FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
87     PointSize26Dot6   pointSize;       ///< The font point size.
88     FontId            fontId;          ///< The font id.
89   };
90
91   /**
92    * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
93    */
94   struct CacheItem
95   {
96     CacheItem( FT_Face ftFace,
97                const FontPath& path,
98                PointSize26Dot6 pointSize,
99                FaceIndex face,
100                const FontMetrics& metrics );
101
102     CacheItem( FT_Face ftFace,
103                const FontPath& path,
104                PointSize26Dot6 pointSize,
105                FaceIndex face,
106                const FontMetrics& metrics,
107                float fixedWidth,
108                float fixedHeight );
109
110     FT_Face mFreeTypeFace;       ///< The FreeType face.
111     FontPath mPath;              ///< The path to the font file name.
112     PointSize26Dot6 mPointSize;  ///< The font point size.
113     FaceIndex mFaceIndex;        ///< The face index.
114     FontMetrics mMetrics;        ///< The font metrics.
115     FT_Short mFixedWidthPixels;  ///< The height in pixels (fixed size bitmaps only)
116     FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
117     bool mIsFixedSizeBitmap;     ///< Whether the font has fixed size bitmaps.
118   };
119
120   struct EllipsisItem
121   {
122     PointSize26Dot6 size;
123     GlyphInfo glyph;
124   };
125
126   /**
127    * Constructor.
128    *
129    * Initializes the FreeType library.
130    * Initializes the dpi values.
131    *
132    * @param[in] horizontalDpi The horizontal dpi.
133    * @param[in] verticalDpi The vertical dpi.
134    */
135   Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
136
137   /**
138    * Default destructor.
139    *
140    * Frees any allocated resource.
141    */
142   ~Plugin();
143
144   /**
145    * @copydoc Dali::FontClient::SetDpi()
146    */
147   void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
148
149   /**
150    * @copydoc Dali::FontClient::ResetSystemDefaults()
151    */
152   void ResetSystemDefaults();
153
154   /**
155    * @copydoc Dali::FontClient::SetDefaultFont()
156    */
157   void SetDefaultFont( const FontDescription& fontDescription );
158
159   /**
160    * @copydoc Dali::FontClient::GetDefaultPlatformFontDescription()
161    */
162   void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
163
164   /**
165    * @copydoc Dali::FontClient::GetDefaultFonts()
166    */
167   void GetDefaultFonts( FontList& defaultFonts );
168
169   /**
170    * @copydoc Dali::FontClient::GetSystemFonts()
171    */
172   void GetSystemFonts( FontList& systemFonts );
173
174   /**
175    * @copydoc Dali::FontClient::GetDescription()
176    */
177   void GetDescription( FontId id, FontDescription& fontDescription ) const;
178
179   /**
180    * @copydoc Dali::FontClient::GetPointSize()
181    */
182   PointSize26Dot6 GetPointSize( FontId id );
183
184   /**
185    * @copydoc Dali::FontClient::FindFontForCharacter()
186    */
187   FontId FindFontForCharacter( const FontList& fontList,
188                                Character charcode,
189                                PointSize26Dot6 requestedSize,
190                                bool preferColor );
191
192   /**
193    * @copydoc Dali::FontClient::FindDefaultFont()
194    */
195   FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor );
196
197   /**
198    * @copydoc Dali::FontClient::FindFallbackFont()
199    */
200   FontId FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 requestedSize, bool preferColor );
201
202   /**
203    * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
204    *
205    * @param[in] cacheDescription Whether to cache the font description.
206    */
207   FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
208
209   /**
210    * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex )
211    */
212   FontId GetFontId( const FontDescription& fontDescription,
213                     PointSize26Dot6 pointSize,
214                     FaceIndex faceIndex );
215
216   /**
217    * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
218    */
219   bool IsScalable( const FontPath& path );
220
221   /**
222    * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
223    */
224   bool IsScalable( const FontDescription& fontDescription );
225
226   /**
227    * @copydoc Dali::FontClient::GetFixedSizes()
228    */
229   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
230
231   /**
232    * @copydoc Dali::FontClient::GetFixedSizes()
233    */
234   void GetFixedSizes( const FontDescription& fontDescription,
235                       Dali::Vector< PointSize26Dot6 >& sizes );
236
237   /**
238    * @copydoc Dali::FontClient::GetFontMetrics()
239    */
240   void GetFontMetrics( FontId fontId, FontMetrics& metrics, int desiredFixedSize );
241
242   /**
243    * @copydoc Dali::FontClient::GetGlyphIndex()
244    */
245   GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
246
247   /**
248    * @copydoc Dali::FontClient::GetGlyphMetrics()
249    */
250   bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int desiredFixedSize );
251
252   /**
253    * @copydoc Dali::FontClient::CreateBitmap()
254    */
255   BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
256
257   /**
258    * @copydoc Dali::FontClient::GetEllipsisGlyph()
259    */
260   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize );
261
262 private:
263
264   /**
265    * Caches the fonts present in the platform.
266    *
267    * Calls GetFcFontSet() to retrieve the fonts.
268    */
269   void InitSystemFonts();
270
271   /**
272    * Gets the FontDescription which matches the given pattern
273    * @param[in] pattern pattern to match against
274    * @param[out] fontDescription the resultant fontDescription that matched
275    * @return true if match found
276    */
277   bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription );
278
279   /**
280    * @brief Creates a font family pattern used to match fonts.
281    *
282    * @param[in] fontDescription The font to cache.
283    *
284    * @return The pattern.
285    */
286   _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
287
288   /**
289    * Retrieves the fonts present in the platform.
290    *
291    * @return A font fonfig data structure with the platform's fonts.
292    */
293   _FcFontSet* GetFcFontSet() const;
294
295   /**
296    * Retrieves a font config object's value from a pattern.
297    *
298    * @param[in] pattern The font config pattern.
299    * @param[in] n The object.
300    * @param[out] string The object's value.
301    *
302    * @return @e true if the operation is successful.
303    */
304   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
305
306   /**
307    * Retrieves a font config object's value from a pattern.
308    *
309    * @param[in] pattern The font config pattern.
310    * @param[in] n The object.
311    * @param[out] intVal The object's value.
312    *
313    * @return @e true if the operation is successful.
314    */
315   bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
316
317   /**
318    * @brief Creates a font.
319    *
320    * @param[in] path The path to the font file name.
321    * @param[in] pointSize The font point size.
322    * @param[in] faceIndex A face index.
323    * @param[in] cacheDescription Whether to cache the font description.
324    *
325    * @return The font id.
326    */
327   FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
328
329   /**
330    * @brief Creates a fixed size font
331    *
332    * @param[in] path The path to the font file name.
333    * @param[in] pointSize The font point size( must be an available size ).
334    * @param[in] faceIndex A face index.
335    * @param[in] cacheDescription Whether to cache the font description.
336    *
337    * @return The font id.
338    */
339   FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
340
341   /**
342    *
343    * @param[in] destBitmap
344    * @param[in] srcBitmap
345    */
346   void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
347
348   /**
349    * @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.
350    * If there is one , if writes the font id in the param @p fontId.
351    *
352    * @param[in] path Path to the font file name.
353    * @param[in] pointSize The font point size.
354    * @param[in] faceIndex The face index.
355    * @param[out] fontId The font id.
356    *
357    * @return @e true if there triplet is found.
358    */
359   bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
360
361   /**
362    * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
363    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
364    *
365    * @param[in] fontDescription The font to validate.
366    * @param[out] validatedFontId The index to the vector with font descriptions.
367    *
368    * @return @e true if the pair is found.
369    */
370   bool FindValidatedFont( const FontDescription& fontDescription,
371                           FontDescriptionId& validatedFontId );
372
373   /**
374    * @brief Finds a fallback font list from the cache for a given font-description
375    *
376    * @param[in] fontDescription The font to validate.
377    * @param[out] A valid pointer to a font list, or NULL if not found.
378    */
379   bool FindFallbackFontList( const FontDescription& fontDescription,
380                              FontList*& fontList );
381
382   /**
383    * @brief Finds in the cache a pair 'validated font id and font point size'.
384    * If there is one it writes the font id in the param @p fontId.
385    *
386    * @param[in] validatedFontId Index to the vector with font descriptions.
387    * @param[in] pointSize The font point size.
388    * @param[out] fontId The font id.
389    *
390    * @return @e true if the pair is found.
391    */
392   bool FindFont( FontDescriptionId validatedFontId,
393                  PointSize26Dot6 pointSize,
394                  FontId& fontId );
395
396   /**
397    * @brief Validate a font description.
398    *
399    * @param[in] fontDescription The font to validate.
400    * @param[out] validatedFontId Result of validation
401    */
402   void ValidateFont( const FontDescription& fontDescription,
403                      FontDescriptionId& validatedFontId );
404
405   /**
406    * Helper for GetDefaultFonts etc.
407    *
408    * @param[in] fontDescription A font description.
409    * @param[out] fontList A list of the fonts which are a close match for fontDescription.
410    */
411   void SetFontList( const FontDescription& fontDescription, FontList& fontList );
412
413   /**
414    * Caches a font path.
415    *
416    * @param[in] ftFace The FreeType face.
417    * @param[in] id The font identifier.
418    * @param[in] pointSize The font point size.
419    * @param[in] path Path to the font file name.
420    */
421   void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 pointSize,  const FontPath& path );
422
423   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
424
425   unsigned int mDpiHorizontal; ///< Horizontal dpi.
426   unsigned int mDpiVertical;   ///< Vertical dpi.
427
428   FontDescription mDefaultFontDescription; ///< The cached default font from the system
429
430   FontList mSystemFonts;       ///< Cached system fonts.
431   FontList mDefaultFonts;      ///< Cached default fonts.
432
433   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
434
435   std::vector<CacheItem>                mFontCache;            ///< Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
436   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given font.
437   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font.
438   std::vector<FontIdCacheItem>          mFontIdCache;          ///< Caches font ids for the pairs of font point size and the index to the vector with font descriptions of the validated fonts.
439
440   Vector<EllipsisItem> mEllipsisCache;      ///< Caches ellipsis glyphs for a particular point size.
441
442   bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
443 };
444
445 } // namespace Internal
446
447 } // namespace TextAbstraction
448
449 } // namespace Dali
450
451 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__