db2c86e49576b4e371657a02672d85807aa745fd
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / font-platform / font-controller-impl.h
1 #ifndef __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__
2 #define __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__
3
4 /*
5  * Copyright (c) 2014 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 "../../interfaces/font-controller.h"
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/common/map-wrapper.h>
25
26 // EXTERNAL INCLUDES
27 #include <boost/thread.hpp>
28
29
30 // forward declarations of font config types
31 struct _FcConfig;
32 struct _FcPattern;
33 struct _FcCharSet;
34 struct _FcFontSet;
35
36 namespace Dali
37 {
38
39 namespace SlpPlatform
40 {
41
42 typedef Dali::Platform::FontController::FontList FontList;
43
44 /**
45  * concrete interface for the SLP font controller.
46  *
47  * Font controller currently caches the following:
48  * - List of fonts on the system, if the user calls GetFontList()
49  * - List of fonts used by the application with both its filename and character set
50  *
51  * When searching for a font that can display a string of text, the follow occurs:
52  *
53  * - 1. Check the last used font to see if it supports the text
54  * - 2. If 1 fails, check all other Fonts in the cache to see if they support the text
55  * - 3. If 2 fails, use FontConfig to find a font which supports the text, then cache that font.
56  *
57  * Suggested future improvements:
58  * Stop using FontConfig library, instead use FreeType directly to query and cache font family names and
59  * character sets. Then parse the font config configuration file directly to get the system preferred fonts etc.
60  */
61 class FontController : public Platform::FontController
62 {
63 public:
64
65   /**
66    * Constructor
67    */
68   FontController();
69
70  /**
71    * Destructor
72    */
73   virtual ~FontController();
74
75   /**
76    * Internally caches the font file name for every font family passed in
77    * @copydoc Dali::Platform::FontController::GetFontPath()
78    */
79   virtual const std::string& GetFontPath( const StyledFontFamily& styledFontFamily );
80
81   /**
82    * Internally caches the font list the first time it is called.
83    * @copydoc Dali::Platform::FontController::GetFontList()
84    */
85   virtual void GetFontList( FontListMode fontListMode, FontList& fontList );
86
87   /**
88    * @copydoc Dali::Platform::FontController::ValidateFontFamilyName()
89    */
90   virtual bool ValidateFontFamilyName( const StyledFontFamily& styledFontFamily,
91                                        bool& isDefaultSystemFontFamily,
92                                        bool& isDefaultSystemFontStyle,
93                                        StyledFontFamily& closestStyledFontFamilyMatch );
94
95   /**
96    * @copydoc Dali::Platform::FontController::GetFontFamilyForChars()
97    */
98   virtual const StyledFontFamily& GetFontFamilyForChars( const TextArray& charsRequested);
99
100   /**
101    * @copydoc Dali::Platform::FontController::AllGlyphsSupported()
102    */
103   virtual bool AllGlyphsSupported( const StyledFontFamily& styledFontFamily, const TextArray& text );
104
105   /**
106    * @copydoc Dali::Platform::FontController::SetDefaultFontFamily()
107    */
108   virtual void SetDefaultFontFamily( const StyledFontFamily& styledFontFamily );
109
110 private:
111
112   /**
113    * Gets a cached font list
114    * @param[in] fontListMode the font list mode
115    * @return font list
116    */
117   void GetCachedFontList( FontListMode fontListMode, FontList& fontList ) const;
118
119   /**
120    * returns the font path given a font family.
121    * @param[in] styledFontFamily The name of the font's family and the font's style.
122    * @return The font's file name path.
123    */
124   const std::string& GetCachedFontPath( const StyledFontFamily& styledFontFamily ) const;
125
126   /**
127    * returns the cached character set for a font family.
128    * @param[in] styledFontFamily The name of the font's family and the font's style.
129    * @return pointer to font configs character set object.
130    *
131    */
132   _FcCharSet* GetCachedFontCharacterSet( const StyledFontFamily& styledFontFamily ) const;
133
134   /**
135    * Add a font path to the font path cache.
136    * @param[in] styledFontFamily The name of the font's family and the font's style.
137    * @param[in] fontPath font path.
138    * @param[in] characterSet font config character set object (not const because it's ref count is increased).
139    */
140   void AddCachedFont( const StyledFontFamily& styledFontFamily, const std::string& fontPath, _FcCharSet *characterSet );
141
142   /**
143    * Store information held in a fontconfig pattern, into the font cache.
144    * Stores font name, filename and character set.
145    * @param[in] pattern pointer to a font config pattern.
146    * @param[in] inputStyledFontFamily font family name with its style used to perform the pattern match.
147    * @param[out] closestStyledFontFamilyMatch matched family name and style.
148    */
149   void CacheFontInfo( _FcPattern* pattern, const StyledFontFamily& inputStyledFontFamily, StyledFontFamily& closestStyledFontFamilyMatch );
150
151   /**
152    * Create a pattern for font lookups.
153    * @param[in] styledFontFamily The name of the font's family and the font's style.
154    */
155   _FcPattern* CreateFontFamilyPattern( const StyledFontFamily& styledFontFamily );
156
157   /**
158    * Checks whether a character is a control character.
159    */
160   bool IsAControlCharacter( uint32_t character ) const;
161
162   /**
163    * Checks cached fonts to see if they support the text.
164    * @param styledFontFamily The name of the font's family and the font's style.
165    * @param text text array.
166    * @return true if the font family supports the text false if not.
167    */
168   bool FontFamilySupportsText( const StyledFontFamily& styledFontFamily, const TextArray& text );
169
170   /**
171    * Clear the font family cache.
172    * Should only be called by the destructor.
173    */
174   void ClearFontFamilyCache();
175
176   /**
177    * Adds a font to the cached font list.
178    * Checks for duplicates.
179    * @param fileName the font full filename with path
180    * @param styledFontFamily The name of the font's family and the font's style.
181    */
182   void AddToFontList( const std::string& fileName, const StyledFontFamily& styledFontFamily );
183
184   /**
185    * Returns a FontConfig font set, which has a list of fonts.
186    * @return pointer to _FcFontSet struct.
187    */
188   _FcFontSet* GetFontSet() const;
189
190   /**
191    * Create a font config character set object, using the text string.
192    * @param charsRequested array of characters.
193    * @return a pointer to _FcCharSet object on success, NULL on failure.
194    */
195   _FcCharSet* CreateCharacterSet( const TextArray& charsRequested );
196
197   /**
198    * Add a font that has not been found on the system but a match has been found.
199    * @param[in] missingStyledFontFamily the missing font and its style to add.
200    * @param[in] closestStyledFontFamilyMatch it's nearest font and its style match.
201    */
202   void AddMatchedFont( const StyledFontFamily& missingStyledFontFamily, StyledFontFamily& closestStyledFontFamilyMatch );
203
204   /**
205    * Check the cache to see if a font and its style has been added to the missing fonts list.
206    * @param styledFontFamily the font and its style to check.
207    * @return the closest styled font that the missing styled font has been matched with by font-config.
208    */
209    const StyledFontFamily& GetMatchedFont( const StyledFontFamily& styledFontFamily ) const;
210
211    /**
212     * Create a preferred list of fonts to use for when GetFontFamilyForChars() is called.
213     */
214   void CreatePreferedFontList();
215
216   /**
217    * Deletes all preferred fonts.
218    */
219   void ClearPreferredFontList();
220
221   /**
222    * Font cache item.
223    */
224   struct FontCacheItem
225   {
226     std::string FontFileName;     ///< font file name
227     _FcCharSet* FcCharSet;        ///< font config character set, used to test if a character is supported
228   };
229
230   typedef std::map<StyledFontFamily, FontCacheItem> FontFamilyLookup;     ///<  lookup for font names and font cache itmes
231
232   typedef std::map<StyledFontFamily, StyledFontFamily> MatchedFontLookup; ///< lookup for fonts that don't exist, and their nearest match return by FontConfig
233
234   boost::mutex      mFontConfigMutex;       ///< FontConfig needs serializing because it isn't thread safe
235   boost::mutex      mFontFamilyCacheMutex;  ///< to protect the FontFamilyCache data
236   boost::mutex      mFontListMutex;         ///< to prevent more than one thread creating the font list data
237
238   StyledFontFamily  mDefaultStyledFont;     ///< default font
239
240   FontList          mFontSystemList;        ///< cached list of system fonts
241   FontList          mFontApplicationList;   ///< cached list of application fonts
242
243   FontFamilyLookup  mFontFamilyCache;       ///< cache of font names and corresponding font cache items
244   MatchedFontLookup mMatchedFontsFound;     ///< lookup for fonts that haven't been found on the sytem, and the nearest matching font.
245   Vector<StyledFontFamily*> mPreferredFonts;          ///< Ordered list of preferred fonts.
246   Vector<bool>              mPreferredFontsValidated; ///< Stores which of the prefered fonts have been validated.
247
248 };
249
250 } // namespace SlpPlatform
251
252 } // namespace Dali
253
254 #endif // __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__