Merge "Ensure invlid-syncs are handled in thread-sync" into devel/master
[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::GetDefaultPlatformFontDescription()
145    */
146   void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
147
148   /**
149    * @copydoc Dali::FontClient::GetDefaultFonts()
150    */
151   void GetDefaultFonts( FontList& defaultFonts );
152
153   /**
154    * @copydoc Dali::FontClient::GetSystemFonts()
155    */
156   void GetSystemFonts( FontList& systemFonts );
157
158   /**
159    * @copydoc Dali::FontClient::GetDescription()
160    */
161   void GetDescription( FontId id, FontDescription& fontDescription ) const;
162
163   /**
164    * @copydoc Dali::FontClient::GetPointSize()
165    */
166   PointSize26Dot6 GetPointSize( FontId id );
167
168   /**
169    * @copydoc Dali::FontClient::FindDefaultFont()
170    */
171   FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor );
172
173   /**
174    * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
175    *
176    * @param[in] cacheDescription Whether to cache the font description.
177    */
178   FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
179
180   /**
181    * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex )
182    */
183   FontId GetFontId( const FontDescription& fontDescription,
184                     PointSize26Dot6 pointSize,
185                     FaceIndex faceIndex );
186
187   /**
188    * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
189    */
190   bool IsScalable( const FontPath& path );
191
192   /**
193    * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
194    */
195   bool IsScalable( const FontDescription& fontDescription );
196
197   /**
198    * @copydoc Dali::FontClient::GetFixedSizes()
199    */
200   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
201
202   /**
203    * @copydoc Dali::FontClient::GetFixedSizes()
204    */
205   void GetFixedSizes( const FontDescription& fontDescription,
206                       Dali::Vector< PointSize26Dot6 >& sizes );
207
208   /**
209    * @copydoc Dali::FontClient::GetFontMetrics()
210    */
211   void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize );
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, int maxFixedSize );
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] fontDescription The font to cache.
246    *
247    * @return The pattern.
248    */
249   _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
250
251   /**
252    * Retrieves the fonts present in the platform.
253    *
254    * @return A font fonfig data structure with the platform's fonts.
255    */
256   _FcFontSet* GetFcFontSet() const;
257
258   /**
259    * Retrieves a font config object's value from a pattern.
260    *
261    * @param[in] pattern The font config pattern.
262    * @param[in] n The object.
263    * @param[out] string The object's value.
264    *
265    * @return @e true if the operation is successful.
266    */
267   bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
268
269   /**
270    * Retrieves a font config object's value from a pattern.
271    *
272    * @param[in] pattern The font config pattern.
273    * @param[in] n The object.
274    * @param[out] intVal The object's value.
275    *
276    * @return @e true if the operation is successful.
277    */
278   bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
279
280   /**
281    * @brief Creates a font.
282    *
283    * @param[in] path The path to the font file name.
284    * @param[in] pointSize The font point size.
285    * @param[in] faceIndex A face index.
286    * @param[in] cacheDescription Whether to cache the font description.
287    *
288    * @return The font id.
289    */
290   FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
291
292   /**
293    * @brief Creates a fixed size font
294    *
295    * @param[in] path The path to the font file name.
296    * @param[in] pointSize The font point size( must be an available size ).
297    * @param[in] faceIndex A face index.
298    * @param[in] cacheDescription Whether to cache the font description.
299    *
300    * @return The font id.
301    */
302   FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
303
304   /**
305    *
306    * @param[in] destBitmap
307    * @param[in] srcBitmap
308    */
309   void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
310
311   /**
312    * @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.
313    * If there is one , if writes the font id in the param @p fontId.
314    *
315    * @param[in] path Path to the font file name.
316    * @param[in] pointSize The font point size.
317    * @param[in] faceIndex The face index.
318    * @param[out] fontId The font id.
319    *
320    * @return @e true if there triplet is found.
321    */
322   bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
323
324   /**
325    * @brief Finds in the cahce a cluster 'font family, font width, font weight, font slant'
326    * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
327    *
328    * @param[in] fontDescription The font to validate.
329    * @param[out] validatedFontId The index to the vector with font descriptions.
330    *
331    * @return @e true if the pair is found.
332    */
333   bool FindValidatedFont( const FontDescription& fontDescription,
334                           FontDescriptionId& validatedFontId );
335
336   /**
337    * @brief Finds in the cache a pair 'validated font id and font point size'.
338    * If there is one it writes the font id in the param @p fontId.
339    *
340    * @param[in] validatedFontId Index to the vector with font descriptions.
341    * @param[in] pointSize The font point size.
342    * @param[out] fontId The font id.
343    *
344    * @return @e true if the pair is found.
345    */
346   bool FindFont( FontDescriptionId validatedFontId,
347                  PointSize26Dot6 pointSize,
348                  FontId& fontId );
349
350   /**
351    * @brief Validate a font description.
352    *
353    * @param[in] fontDescription The font to validate.
354    * @param[out] validatedFontId Result of validation
355    */
356   void ValidateFont( const FontDescription& fontDescription,
357                      FontDescriptionId& validatedFontId );
358
359   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
360
361   unsigned int mDpiHorizontal; ///< Horizontal dpi.
362   unsigned int mDpiVertical;   ///< Vertical dpi.
363
364   FontList mSystemFonts;       ///< Cached system fonts.
365   FontList mDefaultFonts;      ///< Cached default fonts.
366
367   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'.
368   std::vector<FontDescriptionCacheItem> mValidatedFontCache;   ///< Caches indices to the vector of font descriptions for a given font.
369   FontList                              mFontDescriptionCache; ///< Caches font descriptions for the validated font.
370   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.
371
372   Vector<EllipsisItem> mEllipsisCache;      ///< Caches ellipsis glyphs for a particular point size.
373 };
374
375 } // namespace Internal
376
377 } // namespace TextAbstraction
378
379 } // namespace Dali
380
381 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__