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