Merge remote-tracking branch 'origin/tizen' into new_text
[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/public-api/text-abstraction/font-metrics.h>
23 #include <dali/internal/text-abstraction/font-client-impl.h>
24
25 // EXTERNAL INCLUDES
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28 #include FT_GLYPH_H
29
30 // forward declarations of font config types.
31 struct _FcFontSet;
32 struct _FcPattern;
33
34 namespace Dali
35 {
36
37 namespace TextAbstraction
38 {
39
40 namespace Internal
41 {
42
43 /**
44  *@brief Type used for indices addressing the vector with front descriptions of validated pairs 'font family name, font style'.
45  */
46 typedef uint32_t FontDescriptionId;
47
48 /**
49  * @brief FontClient implementation.
50  */
51 struct FontClient::Plugin
52 {
53   /**
54    * @brief Caches an index to the vector of font descriptions for a given 'font family name, font style'.
55    */
56   struct FontDescriptionCacheItem
57   {
58     FontDescriptionCacheItem( const FontFamily& fontFamily,
59                               const FontStyle& fontStyle,
60                               FontDescriptionId index );
61
62     FontFamily        fontFamily; ///< The font family name.
63     FontStyle         fontStyle;  ///< The font style.
64     FontDescriptionId index;      ///< Index to the vector of font descriptions.
65   };
66
67   /**
68    * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
69    */
70   struct FontIdCacheItem
71   {
72     FontIdCacheItem( FontDescriptionId validatedFontId,
73                      PointSize26Dot6 pointSize,
74                      FontId fontId );
75
76     FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
77     PointSize26Dot6   pointSize;       ///< The font point size.
78     FontId            fontId;          ///< The font id.
79   };
80
81   /**
82    * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
83    */
84   struct CacheItem
85   {
86     CacheItem( FT_Face ftFace,
87                const FontPath& path,
88                PointSize26Dot6 pointSize,
89                FaceIndex face,
90                const FontMetrics& metrics,
91                bool isFixedSizeBitmap = false );
92
93     FT_Face mFreeTypeFace;      ///< The FreeType face.
94     FontPath mPath;             ///< The path to the font file name.
95     PointSize26Dot6 mPointSize; ///< The font point size.
96     FaceIndex mFaceIndex;       ///< The face index.
97     FontMetrics mMetrics;       ///< The font metrics.
98     bool mIsFixedSizeBitmap;    ///< Font is fixed size bitmap.
99   };
100
101   /**
102    * Constructor.
103    *
104    * Initializes the FreeType library.
105    * Initializes the dpi values.
106    *
107    * @param[in] horizontalDpi The horizontal dpi.
108    * @param[in] verticalDpi The vertical dpi.
109    */
110   Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
111
112   /**
113    * Default destructor.
114    *
115    * Frees any allocated resource.
116    */
117   ~Plugin();
118
119   /**
120    * @copydoc Dali::FontClient::SetDpi()
121    */
122   void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
123
124   /**
125    * @copydoc Dali::FontClient::SetDefaultFontFamily()
126    */
127   void SetDefaultFontFamily( const FontFamily& fontFamilyName,
128                              const FontStyle& fontStyle );
129
130   /**
131    * @copydoc Dali::FontClient::GetDefaultFonts()
132    */
133   void GetDefaultFonts( FontList& defaultFonts );
134
135   /**
136    * @copydoc Dali::FontClient::GetSystemFonts()
137    */
138   void GetSystemFonts( FontList& systemFonts );
139
140   /**
141    * @copydoc Dali::FontClient::GetDescription()
142    */
143   void GetDescription( FontId id, FontDescription& fontDescription ) const;
144
145   /**
146    * @copydoc Dali::FontClient::GetPointSize()
147    */
148   PointSize26Dot6 GetPointSize( FontId id );
149
150   /**
151    * @copydoc Dali::FontClient::FindDefaultFont()
152    */
153   FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize );
154
155   /**
156    * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
157    *
158    * @param[in] cacheDescription Whether to cache the font description.
159    */
160   FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
161
162   /**
163    * @copydoc Dali::FontClient::GetFontId(const FontFamily& fontFamily, const FontStyle& fontStyle, PointSize26Dot6 pointSize, FaceIndex faceIndex )
164    */
165   FontId GetFontId( const FontFamily& fontFamily,
166                     const FontStyle& fontStyle,
167                     PointSize26Dot6 pointSize,
168                     FaceIndex faceIndex );
169
170   /**
171    * @copydoc Dali::FontClient::IsScalable(const FontPath& path )
172    */
173   bool IsScalable( const FontPath& path );
174
175   /**
176    * @copydoc Dali::FontClient::IsScalable( const FontFamily& fontFamily, const FontStyle& fontStyle )
177    */
178   bool IsScalable( const FontFamily& fontFamily, const FontStyle& fontStyle );
179
180   /**
181    * @copydoc Dali::FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes )
182    */
183   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
184
185   /**
186    * @copydoc Dali::FontClient::GetFixedSizes( const FontFamily& fontFamily, const FontStyle& fontStyle, Dali::Vector< PointSize26Dot6>& sizes )
187    */
188   void GetFixedSizes( const FontFamily& fontFamily,
189                       const FontStyle& fontStyle,
190                       Dali::Vector< PointSize26Dot6 >& sizes );
191
192   /**
193    * @copydoc Dali::FontClient::GetFontMetrics()
194    */
195   void GetFontMetrics( FontId fontId, FontMetrics& metrics );
196
197   /**
198    * @copydoc Dali::FontClient::GetGlyphIndex()
199    */
200   GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
201
202   /**
203    * @copydoc Dali::FontClient::CreateMetrics()
204    */
205   bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
206
207   /**
208    * @copydoc Dali::FontClient::CreateBitmap()
209    */
210   BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
211
212 private:
213
214   /**
215    * Caches the fonts present in the platform.
216    *
217    * Calls GetFcFontSet() to retrieve the fonts.
218    */
219   void InitSystemFonts();
220
221   /**
222    * @brief Creates a font family pattern used to match fonts.
223    *
224    * @param[in] fontFamily The font family name.
225    * @param[in] fontStyle The font style.
226    *
227    * @return The pattern.
228    */
229   _FcPattern* CreateFontFamilyPattern( const FontFamily& fontFamily,
230                                        const FontStyle& fontStyle );
231
232   /**
233    * Retrieves the fonts present in the platform.
234    *
235    * @return A font fonfig data structure with the platform's fonts.
236    */
237   _FcFontSet* GetFcFontSet() const;
238
239   /**
240    * Retrieves a font config object's value from a pattern.
241    *
242    * @param[in] pattern The font config pattern.
243    * @param[in] n The object.
244    * @param[out] string The object's value.
245    *
246    * @return @e true if the operation is successful.
247    */
248   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
249
250   /**
251    * @brief Creates a font.
252    *
253    * @param[in] path The path to the font file name.
254    * @param[in] pointSize The font point size.
255    * @param[in] faceIndex A face index.
256    * @param[in] cacheDescription Whether to cache the font description.
257    *
258    * @return The font id.
259    */
260   FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
261
262   /**
263    * @brief Creates a fixed size font
264    *
265    * @param[in] path The path to the font file name.
266    * @param[in] pointSize The font point size( must be an available size ).
267    * @param[in] faceIndex A face index.
268    * @param[in] cacheDescription Whether to cache the font description.
269    *
270    * @return The font id.
271    */
272   FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
273
274   /**
275    *
276    * @param[in] destBitmap
277    * @param[in] srcBitmap
278    */
279   void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
280
281   /**
282    * @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.
283    * If there is one , if writes the font id in the param @p fontId.
284    *
285    * @param[in] path Path to the font file name.
286    * @param[in] pointSize The font point size.
287    * @param[in] faceIndex The face index.
288    * @param[out] fontId The font id.
289    *
290    * @return @e true if there triplet is found.
291    */
292   bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
293
294   /**
295    * @brief Finds in the cahce a pair 'font family, font style'.
296    * If there is one, it writes the index to the vector with font descriptions in the param @p  validatedFontId.
297    *
298    * @param[in] fontFamily The font family name.
299    * @param[in] fontStyle The font style.
300    * @param[out] validatedFontId The index to the vector with font descriptions.
301    *
302    * @return @e true if the pair is found.
303    */
304   bool FindValidatedFont( const FontFamily& fontFamily,
305                           const FontStyle& fontStyle,
306                           FontDescriptionId& validatedFontId );
307
308   /**
309    * @brief Finds in the cache a pair 'validated font id and font point size'.
310    * If there is one it writes the font id in the param @p fontId.
311    *
312    * @param[in] validatedFontId Index to the vector with font descriptions.
313    * @param[in] pointSize The font point size.
314    * @param[out] fontId The font id.
315    *
316    * @return @e true if the pair is found.
317    */
318   bool FindFont( FontDescriptionId validatedFontId,
319                  PointSize26Dot6 pointSize,
320                  FontId& fontId );
321
322   /**
323    * @brief Validate a font family and style
324    *
325    * @param[in] fontFamily Font Family to validate
326    * @param[in] fontStyle Font Style to validate
327    * @param[out] validatedFontId Result of validation
328    */
329   void ValidateFont( const FontFamily& fontFamily,
330                      const FontStyle& fontStyle,
331                      FontDescriptionId& validatedFontId );
332
333   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
334
335   unsigned int mDpiHorizontal; ///< Horizontal dpi.
336   unsigned int mDpiVertical;   ///< Vertical dpi.
337
338   FontList mSystemFonts;       ///< Cached system fonts.
339   FontList mDefaultFonts;      ///< Cached default fonts.
340
341   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'.
342   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given 'font family name, font style'.
343   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font family name and font style pairs.
344   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.
345 };
346
347 } // namespace Internal
348
349 } // namespace TextAbstraction
350
351 } // namespace Dali
352
353 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__