1 #ifndef __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__
2 #define __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "../../interfaces/font-controller.h"
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/common/map-wrapper.h>
27 #include <boost/thread.hpp>
30 // forward declarations of font config types
42 typedef Dali::Platform::FontController::FontList FontList;
45 * concrete interface for the SLP font controller.
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
51 * When searching for a font that can display a string of text, the follow occurs:
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.
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.
61 class FontController : public Platform::FontController
73 virtual ~FontController();
76 * Internally caches the font file name for every font family passed in
77 * @copydoc Dali::Platform::FontController::GetFontPath()
79 virtual const std::string& GetFontPath( const StyledFontFamily& styledFontFamily );
82 * Internally caches the font list the first time it is called.
83 * @copydoc Dali::Platform::FontController::GetFontList()
85 virtual void GetFontList( FontListMode fontListMode, FontList& fontList );
88 * @copydoc Dali::Platform::FontController::ValidateFontFamilyName()
90 virtual bool ValidateFontFamilyName( const StyledFontFamily& styledFontFamily,
91 bool& isDefaultSystemFontFamily,
92 bool& isDefaultSystemFontStyle,
93 StyledFontFamily& closestStyledFontFamilyMatch );
96 * @copydoc Dali::Platform::FontController::GetFontFamilyForChars()
98 virtual const StyledFontFamily& GetFontFamilyForChars( const Integration::TextArray& charsRequested);
101 * @copydoc Dali::Platform::FontController::AllGlyphsSupported()
103 virtual bool AllGlyphsSupported( const StyledFontFamily& styledFontFamily, const Integration::TextArray& text );
106 * @copydoc Dali::Platform::FontController::SetDefaultFontFamily()
108 virtual void SetDefaultFontFamily( const StyledFontFamily& styledFontFamily );
113 * Gets a cached font list
114 * @param[in] fontListMode the font list mode
117 void GetCachedFontList( FontListMode fontListMode, FontList& fontList ) const;
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.
124 const std::string& GetCachedFontPath( const StyledFontFamily& styledFontFamily ) const;
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.
132 _FcCharSet* GetCachedFontCharacterSet( const StyledFontFamily& styledFontFamily ) const;
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).
140 void AddCachedFont( const StyledFontFamily& styledFontFamily, const std::string& fontPath, _FcCharSet *characterSet );
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.
149 void CacheFontInfo( _FcPattern* pattern, const StyledFontFamily& inputStyledFontFamily, StyledFontFamily& closestStyledFontFamilyMatch );
152 * Create a pattern for font lookups.
153 * @param[in] styledFontFamily The name of the font's family and the font's style.
155 _FcPattern* CreateFontFamilyPattern( const StyledFontFamily& styledFontFamily );
158 * Checks whether a character is a control character.
160 bool IsAControlCharacter( uint32_t character ) const;
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.
168 bool FontFamilySupportsText( const StyledFontFamily& styledFontFamily, const Integration::TextArray& text );
171 * Clear the font family cache.
172 * Should only be called by the destructor.
174 void ClearFontFamilyCache();
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.
182 void AddToFontList( const std::string& fileName, const StyledFontFamily& styledFontFamily );
185 * Returns a FontConfig font set, which has a list of fonts.
186 * @return pointer to _FcFontSet struct.
188 _FcFontSet* GetFontSet() const;
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.
195 _FcCharSet* CreateCharacterSet( const Integration::TextArray& charsRequested );
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.
202 void AddMatchedFont( const StyledFontFamily& missingStyledFontFamily, StyledFontFamily& closestStyledFontFamilyMatch );
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.
209 const StyledFontFamily& GetMatchedFont( const StyledFontFamily& styledFontFamily ) const;
212 * Create a preferred list of fonts to use for when GetFontFamilyForChars() is called.
214 void CreatePreferedFontList();
217 * Deletes all preferred fonts.
219 void ClearPreferredFontList();
226 std::string FontFileName; ///< font file name
227 _FcCharSet* FcCharSet; ///< font config character set, used to test if a character is supported
230 typedef std::map<StyledFontFamily, FontCacheItem> FontFamilyLookup; ///< lookup for font names and font cache itmes
232 typedef std::map<StyledFontFamily, StyledFontFamily> MatchedFontLookup; ///< lookup for fonts that don't exist, and their nearest match return by FontConfig
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
238 StyledFontFamily mDefaultStyledFont; ///< default font
240 FontList mFontSystemList; ///< cached list of system fonts
241 FontList mFontApplicationList; ///< cached list of application fonts
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.
250 } // namespace SlpPlatform
254 #endif // __DALI_SLP_PLATFORM_FONT_CONTROLLER_H__