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