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>
26 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
27 #include <dali/internal/glyphy/vector-font-cache.h>
29 class VectorFontCache;
34 #include FT_FREETYPE_H
37 // forward declarations of font config types.
44 namespace TextAbstraction
51 *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
53 typedef uint32_t FontDescriptionId;
56 * @brief FontClient implementation.
58 struct FontClient::Plugin
61 * @brief Caches an list of fallback fonts for a given font-description
63 struct FallbackCacheItem
65 FallbackCacheItem( const FontDescription& fontDescription, FontList* fallbackFonts );
67 FontDescription fontDescription; ///< The font description.
68 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
72 * @brief Caches an index to the vector of font descriptions for a given font.
74 struct FontDescriptionCacheItem
76 FontDescriptionCacheItem( const FontDescription& fontDescription,
77 FontDescriptionId index );
79 FontDescription fontDescription; ///< The font description.
80 FontDescriptionId index; ///< Index to the vector of font descriptions.
84 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
86 struct FontIdCacheItem
88 FontIdCacheItem( FontDescriptionId validatedFontId,
89 PointSize26Dot6 requestedPointSize,
92 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
93 PointSize26Dot6 requestedPointSize; ///< The font point size.
94 FontId fontId; ///< The font id.
98 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
102 CacheItem( FT_Face ftFace,
103 const FontPath& path,
104 PointSize26Dot6 requestedPointSize,
106 const FontMetrics& metrics );
108 CacheItem( FT_Face ftFace,
109 const FontPath& path,
110 PointSize26Dot6 requestedPointSize,
112 const FontMetrics& metrics,
116 FT_Face mFreeTypeFace; ///< The FreeType face.
117 FontPath mPath; ///< The path to the font file name.
118 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
119 FaceIndex mFaceIndex; ///< The face index.
120 FontMetrics mMetrics; ///< The font metrics.
121 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
122 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
123 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
124 bool mIsFixedSizeBitmap; ///< Whether the font has fixed size bitmaps.
129 PointSize26Dot6 requestedPointSize;
136 * Initializes the FreeType library.
137 * Initializes the dpi values.
139 * @param[in] horizontalDpi The horizontal dpi.
140 * @param[in] verticalDpi The vertical dpi.
142 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
145 * Default destructor.
147 * Frees any allocated resource.
152 * @copydoc Dali::FontClient::SetDpi()
154 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
157 * @copydoc Dali::FontClient::ResetSystemDefaults()
159 void ResetSystemDefaults();
162 * @copydoc Dali::FontClient::SetDefaultFont()
164 void SetDefaultFont( const FontDescription& fontDescription );
167 * @copydoc Dali::FontClient::GetDefaultPlatformFontDescription()
169 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
172 * @copydoc Dali::FontClient::GetDefaultFonts()
174 void GetDefaultFonts( FontList& defaultFonts );
177 * @copydoc Dali::FontClient::GetSystemFonts()
179 void GetSystemFonts( FontList& systemFonts );
182 * @copydoc Dali::FontClient::GetDescription()
184 void GetDescription( FontId id, FontDescription& fontDescription ) const;
187 * @copydoc Dali::FontClient::GetPointSize()
189 PointSize26Dot6 GetPointSize( FontId id );
192 * @copydoc Dali::FontClient::FindFontForCharacter()
194 FontId FindFontForCharacter( const FontList& fontList,
196 PointSize26Dot6 requestedPointSize,
200 * @copydoc Dali::FontClient::FindDefaultFont()
202 FontId FindDefaultFont( Character charcode, PointSize26Dot6 requestedPointSize, bool preferColor );
205 * @copydoc Dali::FontClient::FindFallbackFont()
207 FontId FindFallbackFont( FontId preferredFont, Character charcode, PointSize26Dot6 requestedPointSize, bool preferColor );
210 * @see Dali::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
212 * @param[in] actualPointSize The actual point size. In case of emojis the @p requestedPointSize is used to build the metrics and cache the font and the @p actualPointSize is used to load the glyph.
213 * @param[in] cacheDescription Whether to cache the font description.
215 FontId GetFontId( const FontPath& path,
216 PointSize26Dot6 requestedPointSize,
217 PointSize26Dot6 actualPointSize,
219 bool cacheDescription = true );
222 * @copydoc Dali::FontClient::GetFontId( const FontDescription& fontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
224 FontId GetFontId( const FontDescription& fontDescription,
225 PointSize26Dot6 requestedPointSize,
226 PointSize26Dot6 actualPointSize,
227 FaceIndex faceIndex );
230 * @copydoc Dali::FontClient::IsScalable( const FontPath& path )
232 bool IsScalable( const FontPath& path );
235 * @copydoc Dali::FontClient::IsScalable( const FontDescription& fontDescription )
237 bool IsScalable( const FontDescription& fontDescription );
240 * @copydoc Dali::FontClient::GetFixedSizes()
242 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
245 * @copydoc Dali::FontClient::GetFixedSizes()
247 void GetFixedSizes( const FontDescription& fontDescription,
248 Dali::Vector< PointSize26Dot6 >& sizes );
251 * @copydoc Dali::FontClient::GetFontMetrics()
253 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
256 * @copydoc Dali::FontClient::GetGlyphIndex()
258 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
261 * @copydoc Dali::FontClient::GetGlyphMetrics()
263 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
266 * Helper for GetGlyphMetrics when using bitmaps
268 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
271 * Helper for GetGlyphMetrics when using vectors
273 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
276 * @copydoc Dali::FontClient::CreateBitmap()
278 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
281 * @copydoc Dali::FontClient::CreateVectorBlob()
283 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
286 * @copydoc Dali::FontClient::GetEllipsisGlyph()
288 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
293 * Caches the fonts present in the platform.
295 * Calls GetFcFontSet() to retrieve the fonts.
297 void InitSystemFonts();
300 * Gets the FontDescription which matches the given pattern
301 * @param[in] pattern pattern to match against
302 * @param[out] fontDescription the resultant fontDescription that matched
303 * @return true if match found
305 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription );
308 * @brief Creates a font family pattern used to match fonts.
310 * @param[in] fontDescription The font to cache.
312 * @return The pattern.
314 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
317 * Retrieves the fonts present in the platform.
319 * @return A font fonfig data structure with the platform's fonts.
321 _FcFontSet* GetFcFontSet() const;
324 * Retrieves a font config object's value from a pattern.
326 * @param[in] pattern The font config pattern.
327 * @param[in] n The object.
328 * @param[out] string The object's value.
330 * @return @e true if the operation is successful.
332 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
335 * Retrieves a font config object's value from a pattern.
337 * @param[in] pattern The font config pattern.
338 * @param[in] n The object.
339 * @param[out] intVal The object's value.
341 * @return @e true if the operation is successful.
343 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
346 * @brief Creates a font.
348 * @param[in] path The path to the font file name.
349 * @param[in] requestedPointSize The requested point size.
350 * @param[in] actualPointSize The actual point size (for color emojis).
351 * @param[in] faceIndex A face index.
352 * @param[in] cacheDescription Whether to cache the font description.
354 * @return The font id.
356 FontId CreateFont( const FontPath& path,
357 PointSize26Dot6 requestedPointSize,
358 PointSize26Dot6 actualPointSize,
360 bool cacheDescription );
364 * @param[in] destBitmap
365 * @param[in] srcBitmap
367 void ConvertBitmap( PixelData& destBitmap, FT_Bitmap srcBitmap );
370 * @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.
371 * If there is one , if writes the font id in the param @p fontId.
373 * @param[in] path Path to the font file name.
374 * @param[in] requestedPointSize The font point size.
375 * @param[in] faceIndex The face index.
376 * @param[out] fontId The font id.
378 * @return @e true if there triplet is found.
380 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
383 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
384 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
386 * @param[in] fontDescription The font to validate.
387 * @param[out] validatedFontId The index to the vector with font descriptions.
389 * @return @e true if the pair is found.
391 bool FindValidatedFont( const FontDescription& fontDescription,
392 FontDescriptionId& validatedFontId );
395 * @brief Finds a fallback font list from the cache for a given font-description
397 * @param[in] fontDescription The font to validate.
398 * @param[out] A valid pointer to a font list, or NULL if not found.
400 bool FindFallbackFontList( const FontDescription& fontDescription,
401 FontList*& fontList );
404 * @brief Finds in the cache a pair 'validated font id and font point size'.
405 * If there is one it writes the font id in the param @p fontId.
407 * @param[in] validatedFontId Index to the vector with font descriptions.
408 * @param[in] requestedPointSize The font point size.
409 * @param[out] fontId The font id.
411 * @return @e true if the pair is found.
413 bool FindFont( FontDescriptionId validatedFontId,
414 PointSize26Dot6 requestedPointSize,
418 * @brief Validate a font description.
420 * @param[in] fontDescription The font to validate.
421 * @param[out] validatedFontId Result of validation
423 void ValidateFont( const FontDescription& fontDescription,
424 FontDescriptionId& validatedFontId );
427 * Helper for GetDefaultFonts etc.
429 * @param[in] fontDescription A font description.
430 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
432 void SetFontList( const FontDescription& fontDescription, FontList& fontList );
435 * Caches a font path.
437 * @param[in] ftFace The FreeType face.
438 * @param[in] id The font identifier.
439 * @param[in] requestedPointSize The font point size.
440 * @param[in] path Path to the font file name.
442 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
446 // Declared private and left undefined to avoid copies.
447 Plugin( const Plugin& );
448 // Declared private and left undefined to avoid copies.
449 Plugin& operator=( const Plugin& );
453 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
455 unsigned int mDpiHorizontal; ///< Horizontal dpi.
456 unsigned int mDpiVertical; ///< Vertical dpi.
458 FontDescription mDefaultFontDescription; ///< The cached default font from the system
460 FontList mSystemFonts; ///< Cached system fonts.
461 FontList mDefaultFonts; ///< Cached default fonts.
463 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
465 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'.
466 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
467 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
468 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.
470 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
472 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
474 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
477 } // namespace Internal
479 } // namespace TextAbstraction
483 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__