FontClient Match API used to get default platform font without parsing file.
[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::SetDefaultFont()
151    */
152   void SetDefaultFont( const FontDescription& fontDescription );
153
154   /**
155    * @copydoc Dali::FontClient::GetDefaultPlatformFontDescription()
156    */
157   void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
158
159   /**
160    * @copydoc Dali::FontClient::GetDefaultFonts()
161    */
162   void GetDefaultFonts( FontList& defaultFonts );
163
164   /**
165    * @copydoc Dali::FontClient::GetSystemFonts()
166    */
167   void GetSystemFonts( FontList& systemFonts );
168
169   /**
170    * @copydoc Dali::FontClient::GetDescription()
171    */
172   void GetDescription( FontId id, FontDescription& fontDescription ) const;
173
174   /**
175    * @copydoc Dali::FontClient::GetPointSize()
176    */
177   PointSize26Dot6 GetPointSize( FontId id );
178
179   /**
180    * @copydoc Dali::FontClient::FindFontForCharacter()
181    */
182   FontId FindFontForCharacter( const FontList& fontList,
183                                Character charcode,
184                                PointSize26Dot6 requestedSize,
185                                bool preferColor );
186
187   /**
188    * @copydoc Dali::FontClient::FindDefaultFont()
189    */
190   FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor );
191
192   /**
193    * @copydoc Dali::FontClient::FindFallbackFont()
194    */
195   FontId FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 requestedSize, bool preferColor );
196
197   /**
198    * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
199    *
200    * @param[in] cacheDescription Whether to cache the font description.
201    */
202   FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
203
204   /**
205    * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex )
206    */
207   FontId GetFontId( const FontDescription& fontDescription,
208                     PointSize26Dot6 pointSize,
209                     FaceIndex faceIndex );
210
211   /**
212    * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
213    */
214   bool IsScalable( const FontPath& path );
215
216   /**
217    * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
218    */
219   bool IsScalable( const FontDescription& fontDescription );
220
221   /**
222    * @copydoc Dali::FontClient::GetFixedSizes()
223    */
224   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
225
226   /**
227    * @copydoc Dali::FontClient::GetFixedSizes()
228    */
229   void GetFixedSizes( const FontDescription& fontDescription,
230                       Dali::Vector< PointSize26Dot6 >& sizes );
231
232   /**
233    * @copydoc Dali::FontClient::GetFontMetrics()
234    */
235   void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize );
236
237   /**
238    * @copydoc Dali::FontClient::GetGlyphIndex()
239    */
240   GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
241
242   /**
243    * @copydoc Dali::FontClient::GetGlyphMetrics()
244    */
245   bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize );
246
247   /**
248    * @copydoc Dali::FontClient::CreateBitmap()
249    */
250   BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
251
252   /**
253    * @copydoc Dali::FontClient::GetEllipsisGlyph()
254    */
255   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize );
256
257 private:
258
259   /**
260    * Caches the fonts present in the platform.
261    *
262    * Calls GetFcFontSet() to retrieve the fonts.
263    */
264   void InitSystemFonts();
265
266   /**
267    * Gets the FontDescription which matches the given pattern
268    * @param[in] pattern pattern to match against
269    * @param[out] fontDescription the resultant fontDescription that matched
270    * @return true if match found
271    */
272   bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription );
273
274   /**
275    * @brief Creates a font family pattern used to match fonts.
276    *
277    * @param[in] fontDescription The font to cache.
278    *
279    * @return The pattern.
280    */
281   _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
282
283   /**
284    * Retrieves the fonts present in the platform.
285    *
286    * @return A font fonfig data structure with the platform's fonts.
287    */
288   _FcFontSet* GetFcFontSet() const;
289
290   /**
291    * Retrieves a font config object's value from a pattern.
292    *
293    * @param[in] pattern The font config pattern.
294    * @param[in] n The object.
295    * @param[out] string The object's value.
296    *
297    * @return @e true if the operation is successful.
298    */
299   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
300
301   /**
302    * Retrieves a font config object's value from a pattern.
303    *
304    * @param[in] pattern The font config pattern.
305    * @param[in] n The object.
306    * @param[out] intVal The object's value.
307    *
308    * @return @e true if the operation is successful.
309    */
310   bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
311
312   /**
313    * @brief Creates a font.
314    *
315    * @param[in] path The path to the font file name.
316    * @param[in] pointSize The font point size.
317    * @param[in] faceIndex A face index.
318    * @param[in] cacheDescription Whether to cache the font description.
319    *
320    * @return The font id.
321    */
322   FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
323
324   /**
325    * @brief Creates a fixed size font
326    *
327    * @param[in] path The path to the font file name.
328    * @param[in] pointSize The font point size( must be an available size ).
329    * @param[in] faceIndex A face index.
330    * @param[in] cacheDescription Whether to cache the font description.
331    *
332    * @return The font id.
333    */
334   FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
335
336   /**
337    *
338    * @param[in] destBitmap
339    * @param[in] srcBitmap
340    */
341   void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
342
343   /**
344    * @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.
345    * If there is one , if writes the font id in the param @p fontId.
346    *
347    * @param[in] path Path to the font file name.
348    * @param[in] pointSize The font point size.
349    * @param[in] faceIndex The face index.
350    * @param[out] fontId The font id.
351    *
352    * @return @e true if there triplet is found.
353    */
354   bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
355
356   /**
357    * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
358    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
359    *
360    * @param[in] fontDescription The font to validate.
361    * @param[out] validatedFontId The index to the vector with font descriptions.
362    *
363    * @return @e true if the pair is found.
364    */
365   bool FindValidatedFont( const FontDescription& fontDescription,
366                           FontDescriptionId& validatedFontId );
367
368   /**
369    * @brief Finds a fallback font list from the cache for a given font-description
370    *
371    * @param[in] fontDescription The font to validate.
372    * @param[out] A valid pointer to a font list, or NULL if not found.
373    */
374   bool FindFallbackFontList( const FontDescription& fontDescription,
375                              FontList*& fontList );
376
377   /**
378    * @brief Finds in the cache a pair 'validated font id and font point size'.
379    * If there is one it writes the font id in the param @p fontId.
380    *
381    * @param[in] validatedFontId Index to the vector with font descriptions.
382    * @param[in] pointSize The font point size.
383    * @param[out] fontId The font id.
384    *
385    * @return @e true if the pair is found.
386    */
387   bool FindFont( FontDescriptionId validatedFontId,
388                  PointSize26Dot6 pointSize,
389                  FontId& fontId );
390
391   /**
392    * @brief Validate a font description.
393    *
394    * @param[in] fontDescription The font to validate.
395    * @param[out] validatedFontId Result of validation
396    */
397   void ValidateFont( const FontDescription& fontDescription,
398                      FontDescriptionId& validatedFontId );
399
400   /**
401    * Helper for SetDefaultFont etc.
402    *
403    * @param[in] fontDescription A font description.
404    * @param[out] fontList A list of the fonts which are a close match for fontDescription.
405    */
406   void SetFontList( const FontDescription& fontDescription, FontList& fontList );
407
408   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
409
410   unsigned int mDpiHorizontal; ///< Horizontal dpi.
411   unsigned int mDpiVertical;   ///< Vertical dpi.
412
413   FontList mSystemFonts;       ///< Cached system fonts.
414   FontList mDefaultFonts;      ///< Cached default fonts.
415
416   std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
417
418   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'.
419   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given font.
420   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font.
421   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.
422
423   Vector<EllipsisItem> mEllipsisCache;      ///< Caches ellipsis glyphs for a particular point size.
424 };
425
426 } // namespace Internal
427
428 } // namespace TextAbstraction
429
430 } // namespace Dali
431
432 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__