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