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