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) 2018 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/text-abstraction/font-client-impl.h>
26 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
27 #include <dali/internal/text/glyphy/vector-font-cache.h>
29 class VectorFontCache;
34 #include FT_FREETYPE_H
39 // forward declarations of font config types.
47 namespace TextAbstraction
54 *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
56 typedef uint32_t FontDescriptionId;
57 typedef Vector<_FcCharSet*> CharacterSetList;
60 * @brief FontClient implementation.
62 struct FontClient::Plugin
65 * @brief Caches an list of fallback fonts for a given font-description
67 struct FallbackCacheItem
69 FallbackCacheItem( FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets );
71 FontDescription fontDescription; ///< The font description.
72 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
73 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
77 * @brief Caches an index to the vector of font descriptions for a given font.
79 struct FontDescriptionCacheItem
81 FontDescriptionCacheItem( const FontDescription& fontDescription,
82 FontDescriptionId index );
83 FontDescriptionCacheItem( FontDescription&& fontDescription,
84 FontDescriptionId index );
86 FontDescription fontDescription; ///< The font description.
87 FontDescriptionId index; ///< Index to the vector of font descriptions.
91 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
93 struct FontDescriptionSizeCacheItem
95 FontDescriptionSizeCacheItem( FontDescriptionId validatedFontId,
96 PointSize26Dot6 requestedPointSize,
99 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
100 PointSize26Dot6 requestedPointSize; ///< The font point size.
101 FontId fontId; ///< The font identifier.
105 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
107 struct FontFaceCacheItem
109 FontFaceCacheItem( FT_Face ftFace,
110 const FontPath& path,
111 PointSize26Dot6 requestedPointSize,
113 const FontMetrics& metrics );
115 FontFaceCacheItem( FT_Face ftFace,
116 const FontPath& path,
117 PointSize26Dot6 requestedPointSize,
119 const FontMetrics& metrics,
122 bool hasColorTables );
124 FT_Face mFreeTypeFace; ///< The FreeType face.
125 FontPath mPath; ///< The path to the font file name.
126 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
127 FaceIndex mFaceIndex; ///< The face index.
128 FontMetrics mMetrics; ///< The font metrics.
129 _FcCharSet* mCharacterSet; ///< Pointer with the range of characters.
130 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
131 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
132 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
133 bool mIsFixedSizeBitmap : 1; ///< Whether the font has fixed size bitmaps.
134 bool mHasColorTables : 1; ///< Whether the font has color tables.
139 PointSize26Dot6 requestedPointSize;
146 * Initializes the FreeType library.
147 * Initializes the dpi values.
149 * @param[in] horizontalDpi The horizontal dpi.
150 * @param[in] verticalDpi The vertical dpi.
152 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
155 * Default destructor.
157 * Frees any allocated resource.
162 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
164 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
167 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
169 void ResetSystemDefaults();
172 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
174 void SetDefaultFont( const FontDescription& preferredFontDescription );
177 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
179 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
182 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
184 void GetDefaultFonts( FontList& defaultFonts );
187 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
189 void GetSystemFonts( FontList& systemFonts );
192 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
194 void GetDescription( FontId id, FontDescription& fontDescription ) const;
197 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
199 PointSize26Dot6 GetPointSize( FontId id );
202 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
204 bool IsCharacterSupportedByFont( FontId fontId, Character character );
207 * @brief Finds within the @p fontList a font which support the @p carcode.
209 * @param[in] fontList A list of font paths, family, width, weight and slant.
210 * @param[in] characterSetList A list that contains a character set for each description of the font list.
211 * @param[in] charcode The character for which a font is needed.
212 * @param[in] requestedPointSize The point size in 26.6 fractional points.
213 * @param[in] preferColor @e true if a color font is preferred.
215 * @return A valid font identifier, or zero if no font is found.
217 FontId FindFontForCharacter( const FontList& fontList,
218 const CharacterSetList& characterSetList,
220 PointSize26Dot6 requestedPointSize,
224 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
226 FontId FindDefaultFont( Character charcode,
227 PointSize26Dot6 requestedPointSize,
231 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
233 FontId FindFallbackFont( Character charcode,
234 const FontDescription& preferredFontDescription,
235 PointSize26Dot6 requestedPointSize,
239 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
241 * @param[in] cacheDescription Whether to cache the font description.
243 FontId GetFontId( const FontPath& path,
244 PointSize26Dot6 requestedPointSize,
246 bool cacheDescription );
249 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
251 FontId GetFontId( const FontDescription& fontDescription,
252 PointSize26Dot6 requestedPointSize,
253 FaceIndex faceIndex );
256 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
258 bool IsScalable( const FontPath& path );
261 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
263 bool IsScalable( const FontDescription& fontDescription );
266 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
268 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
271 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
273 void GetFixedSizes( const FontDescription& fontDescription,
274 Dali::Vector< PointSize26Dot6 >& sizes );
277 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
279 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
282 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
284 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
287 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
289 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
292 * Helper for GetGlyphMetrics when using bitmaps
294 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
297 * Helper for GetGlyphMetrics when using vectors
299 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
302 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
304 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth );
307 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
309 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
312 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
314 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
317 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
319 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
322 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
324 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
327 * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
329 bool AddCustomFontDirectory( const FontPath& path );
334 * @brief Caches the fonts present in the platform.
336 * Calls GetFcFontSet() to retrieve the fonts.
338 void InitSystemFonts();
341 * @brief Gets the FontDescription which matches the given pattern.
342 * @param[in] pattern pattern to match against.
343 * @param[out] fontDescription the resultant fontDescription that matched.
344 * @param[out] characterSet The character set for that pattern.
345 * @return true if match found.
347 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
350 * @brief Creates a font family pattern used to match fonts.
352 * @param[in] fontDescription The font to cache.
354 * @return The pattern.
356 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
359 * Retrieves the fonts present in the platform.
361 * @return A font fonfig data structure with the platform's fonts.
363 _FcFontSet* GetFcFontSet() const;
366 * Retrieves a font config object's value from a pattern.
368 * @param[in] pattern The font config pattern.
369 * @param[in] n The object.
370 * @param[out] string The object's value.
372 * @return @e true if the operation is successful.
374 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
377 * Retrieves a font config object's value from a pattern.
379 * @param[in] pattern The font config pattern.
380 * @param[in] n The object.
381 * @param[out] intVal The object's value.
383 * @return @e true if the operation is successful.
385 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
388 * @brief Creates a font.
390 * @param[in] path The path to the font file name.
391 * @param[in] requestedPointSize The requested point size.
392 * @param[in] faceIndex A face index.
393 * @param[in] cacheDescription Whether to cache the font description.
395 * @return The font identifier.
397 FontId CreateFont( const FontPath& path,
398 PointSize26Dot6 requestedPointSize,
400 bool cacheDescription );
403 * @brief Copy the FreeType bitmap to the given buffer.
405 * @param[out] data The bitmap data.
406 * @param[in] srcBitmap The FreeType bitmap.
408 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
411 * @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.
412 * If there is one , if writes the font identifier in the param @p fontId.
414 * @param[in] path Path to the font file name.
415 * @param[in] requestedPointSize The font point size.
416 * @param[in] faceIndex The face index.
417 * @param[out] fontId The font identifier.
419 * @return @e true if there triplet is found.
421 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
424 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
425 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
427 * @param[in] fontDescription The font to validate.
428 * @param[out] validatedFontId The index to the vector with font descriptions.
430 * @return @e true if the pair is found.
432 bool FindValidatedFont( const FontDescription& fontDescription,
433 FontDescriptionId& validatedFontId );
436 * @brief Finds a fallback font list from the cache for a given font-description
438 * @param[in] fontDescription The font to validate.
439 * @param[out] A valid pointer to a font list, or @e nullptr if not found.
440 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
442 bool FindFallbackFontList( const FontDescription& fontDescription,
444 CharacterSetList*& characterSetList );
447 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
448 * If there is one it writes the font identifier in the param @p fontId.
450 * @param[in] validatedFontId Index to the vector with font descriptions.
451 * @param[in] requestedPointSize The font point size.
452 * @param[out] fontId The font identifier.
454 * @return @e true if the pair is found.
456 bool FindFont( FontDescriptionId validatedFontId,
457 PointSize26Dot6 requestedPointSize,
461 * @brief Validate a font description.
463 * @param[in] fontDescription The font to validate.
464 * @param[out] validatedFontId Result of validation
466 void ValidateFont( const FontDescription& fontDescription,
467 FontDescriptionId& validatedFontId );
470 * Helper for GetDefaultFonts etc.
472 * @param[in] fontDescription A font description.
473 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
474 * @param[out] characterSetList A list of the character sets which are a close match for fontDescription.
476 void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
479 * Caches a font path.
481 * @param[in] ftFace The FreeType face.
482 * @param[in] id The font identifier.
483 * @param[in] requestedPointSize The font point size.
484 * @param[in] path Path to the font file name.
486 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
489 * @brief Creates a character set from a given font's @p description.
491 * @param[in] description The font's description.
493 * @return A character set.
495 _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description );
498 * @brief Destroy all matched Patterns.
501 void DestroyMatchedPatterns();
505 // Declared private and left undefined to avoid copies.
506 Plugin( const Plugin& );
507 // Declared private and left undefined to avoid copies.
508 Plugin& operator=( const Plugin& );
512 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
514 unsigned int mDpiHorizontal; ///< Horizontal dpi.
515 unsigned int mDpiVertical; ///< Vertical dpi.
517 FontDescription mDefaultFontDescription; ///< The cached default font from the system
519 FontList mSystemFonts; ///< Cached system fonts.
520 FontList mDefaultFonts; ///< Cached default fonts.
521 CharacterSetList mDefaultFontCharacterSets;
523 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
525 std::vector<FontFaceCacheItem> mFontFaceCache; ///< Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
526 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
527 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
528 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
529 std::vector<FontDescriptionSizeCacheItem> mFontDescriptionSizeCache; ///< Caches font identifiers for the pairs of font point size and the index to the vector with font descriptions of the validated fonts.
531 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
532 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
533 Vector<_FcPattern*> mMatchedFcPatternCache; ///< Contain matched FcPattern pointer.
535 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
538 } // namespace Internal
540 } // namespace TextAbstraction
544 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H