1 #ifndef __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__
2 #define __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__
5 * Copyright (c) 2015 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 <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>
28 #include FT_FREETYPE_H
31 // forward declarations of font config types.
38 namespace TextAbstraction
45 *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
47 typedef uint32_t FontDescriptionId;
50 * @brief FontClient implementation.
52 struct FontClient::Plugin
55 * @brief Caches an list of fallback fonts for a given font-description
57 struct FallbackCacheItem
59 FallbackCacheItem( const FontDescription& fontDescription, FontList* fallbackFonts );
61 FontDescription fontDescription; ///< The font description.
62 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
66 * @brief Caches an index to the vector of font descriptions for a given font.
68 struct FontDescriptionCacheItem
70 FontDescriptionCacheItem( const FontDescription& fontDescription,
71 FontDescriptionId index );
73 FontDescription fontDescription; ///< The font description.
74 FontDescriptionId index; ///< Index to the vector of font descriptions.
78 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
80 struct FontIdCacheItem
82 FontIdCacheItem( FontDescriptionId validatedFontId,
83 PointSize26Dot6 pointSize,
86 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
87 PointSize26Dot6 pointSize; ///< The font point size.
88 FontId fontId; ///< The font id.
92 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
96 CacheItem( FT_Face ftFace,
98 PointSize26Dot6 pointSize,
100 const FontMetrics& metrics );
102 CacheItem( FT_Face ftFace,
103 const FontPath& path,
104 PointSize26Dot6 pointSize,
106 const FontMetrics& metrics,
110 FT_Face mFreeTypeFace; ///< The FreeType face.
111 FontPath mPath; ///< The path to the font file name.
112 PointSize26Dot6 mPointSize; ///< The font point size.
113 FaceIndex mFaceIndex; ///< The face index.
114 FontMetrics mMetrics; ///< The font metrics.
115 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
116 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
117 bool mIsFixedSizeBitmap; ///< Whether the font has fixed size bitmaps.
122 PointSize26Dot6 size;
129 * Initializes the FreeType library.
130 * Initializes the dpi values.
132 * @param[in] horizontalDpi The horizontal dpi.
133 * @param[in] verticalDpi The vertical dpi.
135 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
138 * Default destructor.
140 * Frees any allocated resource.
145 * @copydoc Dali::FontClient::SetDpi()
147 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
150 * @copydoc Dali::FontClient::ResetSystemDefaults()
152 void ResetSystemDefaults();
155 * @copydoc Dali::FontClient::SetDefaultFont()
157 void SetDefaultFont( const FontDescription& fontDescription );
160 * @copydoc Dali::FontClient::GetDefaultPlatformFontDescription()
162 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
165 * @copydoc Dali::FontClient::GetDefaultFonts()
167 void GetDefaultFonts( FontList& defaultFonts );
170 * @copydoc Dali::FontClient::GetSystemFonts()
172 void GetSystemFonts( FontList& systemFonts );
175 * @copydoc Dali::FontClient::GetDescription()
177 void GetDescription( FontId id, FontDescription& fontDescription ) const;
180 * @copydoc Dali::FontClient::GetPointSize()
182 PointSize26Dot6 GetPointSize( FontId id );
185 * @copydoc Dali::FontClient::FindFontForCharacter()
187 FontId FindFontForCharacter( const FontList& fontList,
189 PointSize26Dot6 requestedSize,
193 * @copydoc Dali::FontClient::FindDefaultFont()
195 FontId FindDefaultFont( Character charcode, PointSize26Dot6 pointSize, bool preferColor );
198 * @copydoc Dali::FontClient::FindFallbackFont()
200 FontId FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 requestedSize, bool preferColor );
203 * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex )
205 * @param[in] cacheDescription Whether to cache the font description.
207 FontId GetFontId( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription = true );
210 * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex )
212 FontId GetFontId( const FontDescription& fontDescription,
213 PointSize26Dot6 pointSize,
214 FaceIndex faceIndex );
217 * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
219 bool IsScalable( const FontPath& path );
222 * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
224 bool IsScalable( const FontDescription& fontDescription );
227 * @copydoc Dali::FontClient::GetFixedSizes()
229 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
232 * @copydoc Dali::FontClient::GetFixedSizes()
234 void GetFixedSizes( const FontDescription& fontDescription,
235 Dali::Vector< PointSize26Dot6 >& sizes );
238 * @copydoc Dali::FontClient::GetFontMetrics()
240 void GetFontMetrics( FontId fontId, FontMetrics& metrics, int desiredFixedSize );
243 * @copydoc Dali::FontClient::GetGlyphIndex()
245 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
248 * @copydoc Dali::FontClient::GetGlyphMetrics()
250 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int desiredFixedSize );
253 * @copydoc Dali::FontClient::CreateBitmap()
255 BufferImage CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
258 * @copydoc Dali::FontClient::GetEllipsisGlyph()
260 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 pointSize );
265 * Caches the fonts present in the platform.
267 * Calls GetFcFontSet() to retrieve the fonts.
269 void InitSystemFonts();
272 * Gets the FontDescription which matches the given pattern
273 * @param[in] pattern pattern to match against
274 * @param[out] fontDescription the resultant fontDescription that matched
275 * @return true if match found
277 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription );
280 * @brief Creates a font family pattern used to match fonts.
282 * @param[in] fontDescription The font to cache.
284 * @return The pattern.
286 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
289 * Retrieves the fonts present in the platform.
291 * @return A font fonfig data structure with the platform's fonts.
293 _FcFontSet* GetFcFontSet() const;
296 * Retrieves a font config object's value from a pattern.
298 * @param[in] pattern The font config pattern.
299 * @param[in] n The object.
300 * @param[out] string The object's value.
302 * @return @e true if the operation is successful.
304 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
307 * Retrieves a font config object's value from a pattern.
309 * @param[in] pattern The font config pattern.
310 * @param[in] n The object.
311 * @param[out] intVal The object's value.
313 * @return @e true if the operation is successful.
315 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
318 * @brief Creates a font.
320 * @param[in] path The path to the font file name.
321 * @param[in] pointSize The font point size.
322 * @param[in] faceIndex A face index.
323 * @param[in] cacheDescription Whether to cache the font description.
325 * @return The font id.
327 FontId CreateFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
330 * @brief Creates a fixed size font
332 * @param[in] path The path to the font file name.
333 * @param[in] pointSize The font point size( must be an available size ).
334 * @param[in] faceIndex A face index.
335 * @param[in] cacheDescription Whether to cache the font description.
337 * @return The font id.
339 FontId CreateFixedSizeFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, bool cacheDescription );
343 * @param[in] destBitmap
344 * @param[in] srcBitmap
346 void ConvertBitmap( BufferImage& destBitmap, FT_Bitmap srcBitmap );
349 * @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.
350 * If there is one , if writes the font id in the param @p fontId.
352 * @param[in] path Path to the font file name.
353 * @param[in] pointSize The font point size.
354 * @param[in] faceIndex The face index.
355 * @param[out] fontId The font id.
357 * @return @e true if there triplet is found.
359 bool FindFont( const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex, FontId& fontId ) const;
362 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
363 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
365 * @param[in] fontDescription The font to validate.
366 * @param[out] validatedFontId The index to the vector with font descriptions.
368 * @return @e true if the pair is found.
370 bool FindValidatedFont( const FontDescription& fontDescription,
371 FontDescriptionId& validatedFontId );
374 * @brief Finds a fallback font list from the cache for a given font-description
376 * @param[in] fontDescription The font to validate.
377 * @param[out] A valid pointer to a font list, or NULL if not found.
379 bool FindFallbackFontList( const FontDescription& fontDescription,
380 FontList*& fontList );
383 * @brief Finds in the cache a pair 'validated font id and font point size'.
384 * If there is one it writes the font id in the param @p fontId.
386 * @param[in] validatedFontId Index to the vector with font descriptions.
387 * @param[in] pointSize The font point size.
388 * @param[out] fontId The font id.
390 * @return @e true if the pair is found.
392 bool FindFont( FontDescriptionId validatedFontId,
393 PointSize26Dot6 pointSize,
397 * @brief Validate a font description.
399 * @param[in] fontDescription The font to validate.
400 * @param[out] validatedFontId Result of validation
402 void ValidateFont( const FontDescription& fontDescription,
403 FontDescriptionId& validatedFontId );
406 * Helper for GetDefaultFonts etc.
408 * @param[in] fontDescription A font description.
409 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
411 void SetFontList( const FontDescription& fontDescription, FontList& fontList );
414 * Caches a font path.
416 * @param[in] ftFace The FreeType face.
417 * @param[in] id The font identifier.
418 * @param[in] pointSize The font point size.
419 * @param[in] path Path to the font file name.
421 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 pointSize, const FontPath& path );
423 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
425 unsigned int mDpiHorizontal; ///< Horizontal dpi.
426 unsigned int mDpiVertical; ///< Vertical dpi.
428 FontDescription mDefaultFontDescription; ///< The cached default font from the system
430 FontList mSystemFonts; ///< Cached system fonts.
431 FontList mDefaultFonts; ///< Cached default fonts.
433 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
435 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'.
436 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
437 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
438 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.
440 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
442 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
445 } // namespace Internal
447 } // namespace TextAbstraction
451 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__