Merge "Pixmap render surface synchronization moved to thread-synchronization" into...
[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 index to the vector of font descriptions for a given font.
56    */
57   struct FontDescriptionCacheItem
58   {
59     FontDescriptionCacheItem( const FontDescription& fontDescription,
60                               FontDescriptionId index );
61
62     FontDescription fontDescription; ///< The font description.
63     FontDescriptionId index;         ///< Index to the vector of font descriptions.
64   };
65
66   /**
67    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
68    */
69   struct FontIdCacheItem
70   {
71     FontIdCacheItem( FontDescriptionId validatedFontId,
72                      PointSize26Dot6 pointSize,
73                      FontId fontId );
74
75     FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
76     PointSize26Dot6   pointSize;       ///< The font point size.
77     FontId            fontId;          ///< The font id.
78   };
79
80   /**
81    * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
82    */
83   struct CacheItem
84   {
85     CacheItem( FT_Face ftFace,
86                const FontPath& path,
87                PointSize26Dot6 pointSize,
88                FaceIndex face,
89                const FontMetrics& metrics );
90
91     CacheItem( FT_Face ftFace,
92                const FontPath& path,
93                PointSize26Dot6 pointSize,
94                FaceIndex face,
95                const FontMetrics& metrics,
96                float fixedWidth,
97                float fixedHeight );
98
99     FT_Face mFreeTypeFace;       ///< The FreeType face.
100     FontPath mPath;              ///< The path to the font file name.
101     PointSize26Dot6 mPointSize;  ///< The font point size.
102     FaceIndex mFaceIndex;        ///< The face index.
103     FontMetrics mMetrics;        ///< The font metrics.
104     FT_Short mFixedWidthPixels;  ///< The height in pixels (fixed size bitmaps only)
105     FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
106     bool mIsFixedSizeBitmap;     ///< Whether the font has fixed size bitmaps.
107   };
108
109   struct EllipsisItem
110   {
111     PointSize26Dot6 size;
112     GlyphInfo glyph;
113   };
114
115   /**
116    * Constructor.
117    *
118    * Initializes the FreeType library.
119    * Initializes the dpi values.
120    *
121    * @param[in] horizontalDpi The horizontal dpi.
122    * @param[in] verticalDpi The vertical dpi.
123    */
124   Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
125
126   /**
127    * Default destructor.
128    *
129    * Frees any allocated resource.
130    */
131   ~Plugin();
132
133   /**
134    * @copydoc Dali::FontClient::SetDpi()
135    */
136   void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
137
138   /**
139    * @copydoc Dali::FontClient::SetDefaultFont()
140    */
141   void SetDefaultFont( const FontDescription& fontDescription );
142
143   /**
144    * @copydoc Dali::FontClient::GetDefaultFonts()
145    */
146   void GetDefaultFonts( FontList& defaultFonts );
147
148   /**
149    * @copydoc Dali::FontClient::GetSystemFonts()
150    */
151   void GetSystemFonts( FontList& systemFonts );
152
153   /**
154    * @copydoc Dali::FontClient::GetDescription()
155    */
156   void GetDescription( FontId id, FontDescription& fontDescription ) const;
157
158   /**
159    * @copydoc Dali::FontClient::GetPointSize()
160    */
161   PointSize26Dot6 GetPointSize( FontId id );
162
163   /**
164    * @copydoc Dali::FontClient::FindDefaultFont()
165    */
166   FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor );
167
168   /**
169    * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
170    *
171    * @param[in] cacheDescription Whether to cache the font description.
172    */
173   FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
174
175   /**
176    * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex )
177    */
178   FontId GetFontId( const FontDescription& fontDescription,
179                     PointSize26Dot6 pointSize,
180                     FaceIndex faceIndex );
181
182   /**
183    * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
184    */
185   bool IsScalable( const FontPath& path );
186
187   /**
188    * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
189    */
190   bool IsScalable( const FontDescription& fontDescription );
191
192   /**
193    * @copydoc Dali::FontClient::GetFixedSizes()
194    */
195   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
196
197   /**
198    * @copydoc Dali::FontClient::GetFixedSizes()
199    */
200   void GetFixedSizes( const FontDescription& fontDescription,
201                       Dali::Vector< PointSize26Dot6 >& sizes );
202
203   /**
204    * @copydoc Dali::FontClient::GetFontMetrics()
205    */
206   void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize );
207
208   /**
209    * @copydoc Dali::FontClient::GetGlyphIndex()
210    */
211   GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
212
213   /**
214    * @copydoc Dali::FontClient::GetGlyphMetrics()
215    */
216   bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize );
217
218   /**
219    * @copydoc Dali::FontClient::CreateBitmap()
220    */
221   BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
222
223   /**
224    * @copydoc Dali::FontClient::GetEllipsisGlyph()
225    */
226   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize );
227
228 private:
229
230   /**
231    * Caches the fonts present in the platform.
232    *
233    * Calls GetFcFontSet() to retrieve the fonts.
234    */
235   void InitSystemFonts();
236
237   /**
238    * @brief Creates a font family pattern used to match fonts.
239    *
240    * @param[in] fontDescription The font to cache.
241    *
242    * @return The pattern.
243    */
244   _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
245
246   /**
247    * Retrieves the fonts present in the platform.
248    *
249    * @return A font fonfig data structure with the platform's fonts.
250    */
251   _FcFontSet* GetFcFontSet() const;
252
253   /**
254    * Retrieves a font config object's value from a pattern.
255    *
256    * @param[in] pattern The font config pattern.
257    * @param[in] n The object.
258    * @param[out] string The object's value.
259    *
260    * @return @e true if the operation is successful.
261    */
262   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
263
264   /**
265    * Retrieves a font config object's value from a pattern.
266    *
267    * @param[in] pattern The font config pattern.
268    * @param[in] n The object.
269    * @param[out] intVal The object's value.
270    *
271    * @return @e true if the operation is successful.
272    */
273   bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
274
275   /**
276    * @brief Creates a font.
277    *
278    * @param[in] path The path to the font file name.
279    * @param[in] pointSize The font point size.
280    * @param[in] faceIndex A face index.
281    * @param[in] cacheDescription Whether to cache the font description.
282    *
283    * @return The font id.
284    */
285   FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
286
287   /**
288    * @brief Creates a fixed size font
289    *
290    * @param[in] path The path to the font file name.
291    * @param[in] pointSize The font point size( must be an available size ).
292    * @param[in] faceIndex A face index.
293    * @param[in] cacheDescription Whether to cache the font description.
294    *
295    * @return The font id.
296    */
297   FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
298
299   /**
300    *
301    * @param[in] destBitmap
302    * @param[in] srcBitmap
303    */
304   void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
305
306   /**
307    * @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.
308    * If there is one , if writes the font id in the param @p fontId.
309    *
310    * @param[in] path Path to the font file name.
311    * @param[in] pointSize The font point size.
312    * @param[in] faceIndex The face index.
313    * @param[out] fontId The font id.
314    *
315    * @return @e true if there triplet is found.
316    */
317   bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
318
319   /**
320    * @brief Finds in the cahce a cluster 'font family, font width, font weight, font slant'
321    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
322    *
323    * @param[in] fontDescription The font to validate.
324    * @param[out] validatedFontId The index to the vector with font descriptions.
325    *
326    * @return @e true if the pair is found.
327    */
328   bool FindValidatedFont( const FontDescription& fontDescription,
329                           FontDescriptionId& validatedFontId );
330
331   /**
332    * @brief Finds in the cache a pair 'validated font id and font point size'.
333    * If there is one it writes the font id in the param @p fontId.
334    *
335    * @param[in] validatedFontId Index to the vector with font descriptions.
336    * @param[in] pointSize The font point size.
337    * @param[out] fontId The font id.
338    *
339    * @return @e true if the pair is found.
340    */
341   bool FindFont( FontDescriptionId validatedFontId,
342                  PointSize26Dot6 pointSize,
343                  FontId& fontId );
344
345   /**
346    * @brief Validate a font description.
347    *
348    * @param[in] fontDescription The font to validate.
349    * @param[out] validatedFontId Result of validation
350    */
351   void ValidateFont( const FontDescription& fontDescription,
352                      FontDescriptionId& validatedFontId );
353
354   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
355
356   unsigned int mDpiHorizontal; ///< Horizontal dpi.
357   unsigned int mDpiVertical;   ///< Vertical dpi.
358
359   FontList mSystemFonts;       ///< Cached system fonts.
360   FontList mDefaultFonts;      ///< Cached default fonts.
361
362   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'.
363   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given font.
364   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font.
365   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.
366
367   Vector<EllipsisItem> mEllipsisCache;      ///< Caches ellipsis glyphs for a particular point size.
368 };
369
370 } // namespace Internal
371
372 } // namespace TextAbstraction
373
374 } // namespace Dali
375
376 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__