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-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
38 #include FT_SYNTHESIS_H
40 // forward declarations of font config types.
48 namespace TextAbstraction
55 *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
57 typedef uint32_t FontDescriptionId;
58 typedef Vector<_FcCharSet*> CharacterSetList;
61 * @brief FontClient implementation.
63 struct FontClient::Plugin
66 * @brief Caches an list of fallback fonts for a given font-description
68 struct FallbackCacheItem
70 FallbackCacheItem( FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets );
72 FontDescription fontDescription; ///< The font description.
73 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
74 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
78 * @brief Caches an index to the vector of font descriptions for a given font.
80 struct FontDescriptionCacheItem
82 FontDescriptionCacheItem( const FontDescription& fontDescription,
83 FontDescriptionId index );
84 FontDescriptionCacheItem( FontDescription&& fontDescription,
85 FontDescriptionId index );
87 FontDescription fontDescription; ///< The font description.
88 FontDescriptionId index; ///< Index to the vector of font descriptions.
92 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
94 struct FontDescriptionSizeCacheItem
96 FontDescriptionSizeCacheItem( FontDescriptionId validatedFontId,
97 PointSize26Dot6 requestedPointSize,
100 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
101 PointSize26Dot6 requestedPointSize; ///< The font point size.
102 FontId fontId; ///< The font identifier.
106 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
108 struct FontFaceCacheItem
110 FontFaceCacheItem( FT_Face ftFace,
111 const FontPath& path,
112 PointSize26Dot6 requestedPointSize,
114 const FontMetrics& metrics );
116 FontFaceCacheItem( FT_Face ftFace,
117 const FontPath& path,
118 PointSize26Dot6 requestedPointSize,
120 const FontMetrics& metrics,
123 bool hasColorTables );
125 FT_Face mFreeTypeFace; ///< The FreeType face.
126 FontPath mPath; ///< The path to the font file name.
127 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
128 FaceIndex mFaceIndex; ///< The face index.
129 FontMetrics mMetrics; ///< The font metrics.
130 _FcCharSet* mCharacterSet; ///< Pointer with the range of characters.
131 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
132 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
133 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
134 bool mIsFixedSizeBitmap : 1; ///< Whether the font has fixed size bitmaps.
135 bool mHasColorTables : 1; ///< Whether the font has color tables.
140 PointSize26Dot6 requestedPointSize;
147 * Initializes the FreeType library.
148 * Initializes the dpi values.
150 * @param[in] horizontalDpi The horizontal dpi.
151 * @param[in] verticalDpi The vertical dpi.
153 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
156 * Default destructor.
158 * Frees any allocated resource.
163 * @copydoc Dali::TextAbstraction::FontClient::ClearCache()
168 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
170 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
173 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
175 void ResetSystemDefaults();
178 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
180 void SetDefaultFont( const FontDescription& preferredFontDescription );
183 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
185 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
188 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
190 void GetDefaultFonts( FontList& defaultFonts );
193 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
195 void GetSystemFonts( FontList& systemFonts );
198 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
200 void GetDescription( FontId id, FontDescription& fontDescription ) const;
203 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
205 PointSize26Dot6 GetPointSize( FontId id );
208 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
210 bool IsCharacterSupportedByFont( FontId fontId, Character character );
213 * @brief Finds within the @p fontList a font which support the @p carcode.
215 * @param[in] fontList A list of font paths, family, width, weight and slant.
216 * @param[in] characterSetList A list that contains a character set for each description of the font list.
217 * @param[in] charcode The character for which a font is needed.
218 * @param[in] requestedPointSize The point size in 26.6 fractional points.
219 * @param[in] preferColor @e true if a color font is preferred.
221 * @return A valid font identifier, or zero if no font is found.
223 FontId FindFontForCharacter( const FontList& fontList,
224 const CharacterSetList& characterSetList,
226 PointSize26Dot6 requestedPointSize,
230 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
232 FontId FindDefaultFont( Character charcode,
233 PointSize26Dot6 requestedPointSize,
237 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
239 FontId FindFallbackFont( Character charcode,
240 const FontDescription& preferredFontDescription,
241 PointSize26Dot6 requestedPointSize,
245 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
247 * @param[in] cacheDescription Whether to cache the font description.
249 FontId GetFontId( const FontPath& path,
250 PointSize26Dot6 requestedPointSize,
252 bool cacheDescription );
255 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
257 FontId GetFontId( const FontDescription& fontDescription,
258 PointSize26Dot6 requestedPointSize,
259 FaceIndex faceIndex );
262 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
264 bool IsScalable( const FontPath& path );
267 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
269 bool IsScalable( const FontDescription& fontDescription );
272 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
274 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
277 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
279 void GetFixedSizes( const FontDescription& fontDescription,
280 Dali::Vector< PointSize26Dot6 >& sizes );
283 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
285 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
288 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
290 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
293 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
295 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
298 * Helper for GetGlyphMetrics when using bitmaps
300 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
303 * Helper for GetGlyphMetrics when using vectors
305 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
308 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool softwareItalic, bool softwareBold, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
310 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool softwareItalic, bool softwareBold, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth );
313 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
315 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
318 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
320 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
323 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
325 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
328 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
330 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
333 * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
335 bool AddCustomFontDirectory( const FontPath& path );
338 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
340 FT_FaceRec_* GetFreetypeFace( FontId fontId );
345 * @brief Caches the fonts present in the platform.
347 * Calls GetFcFontSet() to retrieve the fonts.
349 void InitSystemFonts();
352 * @brief Gets the FontDescription which matches the given pattern.
353 * @param[in] pattern pattern to match against.
354 * @param[out] fontDescription the resultant fontDescription that matched.
355 * @param[out] characterSet The character set for that pattern.
356 * @return true if match found.
358 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
361 * @brief Creates a font family pattern used to match fonts.
363 * @param[in] fontDescription The font to cache.
365 * @return The pattern.
367 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
370 * Retrieves the fonts present in the platform.
372 * @return A font fonfig data structure with the platform's fonts.
374 _FcFontSet* GetFcFontSet() const;
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] string The object's value.
383 * @return @e true if the operation is successful.
385 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
388 * Retrieves a font config object's value from a pattern.
390 * @param[in] pattern The font config pattern.
391 * @param[in] n The object.
392 * @param[out] intVal The object's value.
394 * @return @e true if the operation is successful.
396 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
399 * @brief Creates a font.
401 * @param[in] path The path to the font file name.
402 * @param[in] requestedPointSize The requested point size.
403 * @param[in] faceIndex A face index.
404 * @param[in] cacheDescription Whether to cache the font description.
406 * @return The font identifier.
408 FontId CreateFont( const FontPath& path,
409 PointSize26Dot6 requestedPointSize,
411 bool cacheDescription );
414 * @brief Copy the FreeType bitmap to the given buffer.
416 * @param[out] data The bitmap data.
417 * @param[in] srcBitmap The FreeType bitmap.
419 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
422 * @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.
423 * If there is one , if writes the font identifier in the param @p fontId.
425 * @param[in] path Path to the font file name.
426 * @param[in] requestedPointSize The font point size.
427 * @param[in] faceIndex The face index.
428 * @param[out] fontId The font identifier.
430 * @return @e true if there triplet is found.
432 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
435 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
436 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
438 * @param[in] fontDescription The font to validate.
439 * @param[out] validatedFontId The index to the vector with font descriptions.
441 * @return @e true if the pair is found.
443 bool FindValidatedFont( const FontDescription& fontDescription,
444 FontDescriptionId& validatedFontId );
447 * @brief Finds a fallback font list from the cache for a given font-description
449 * @param[in] fontDescription The font to validate.
450 * @param[out] A valid pointer to a font list, or @e nullptr if not found.
451 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
453 bool FindFallbackFontList( const FontDescription& fontDescription,
455 CharacterSetList*& characterSetList );
458 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
459 * If there is one it writes the font identifier in the param @p fontId.
461 * @param[in] validatedFontId Index to the vector with font descriptions.
462 * @param[in] requestedPointSize The font point size.
463 * @param[out] fontId The font identifier.
465 * @return @e true if the pair is found.
467 bool FindFont( FontDescriptionId validatedFontId,
468 PointSize26Dot6 requestedPointSize,
472 * @brief Validate a font description.
474 * @param[in] fontDescription The font to validate.
475 * @param[out] validatedFontId Result of validation
477 void ValidateFont( const FontDescription& fontDescription,
478 FontDescriptionId& validatedFontId );
481 * Helper for GetDefaultFonts etc.
483 * @param[in] fontDescription A font description.
484 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
485 * @param[out] characterSetList A list of the character sets which are a close match for fontDescription.
487 void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
490 * Caches a font path.
492 * @param[in] ftFace The FreeType face.
493 * @param[in] id The font identifier.
494 * @param[in] requestedPointSize The font point size.
495 * @param[in] path Path to the font file name.
497 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
500 * @brief Creates a character set from a given font's @p description.
502 * @param[in] description The font's description.
504 * @return A character set.
506 _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description );
509 * @brief Destroy all matched Patterns.
512 void DestroyMatchedPatterns();
516 // Declared private and left undefined to avoid copies.
517 Plugin( const Plugin& );
518 // Declared private and left undefined to avoid copies.
519 Plugin& operator=( const Plugin& );
523 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
525 unsigned int mDpiHorizontal; ///< Horizontal dpi.
526 unsigned int mDpiVertical; ///< Vertical dpi.
528 FontDescription mDefaultFontDescription; ///< The cached default font from the system
530 FontList mSystemFonts; ///< Cached system fonts.
531 FontList mDefaultFonts; ///< Cached default fonts.
532 CharacterSetList mDefaultFontCharacterSets;
534 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
536 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'.
537 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
538 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
539 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
540 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.
542 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
543 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
544 Vector<_FcPattern*> mMatchedFcPatternCache; ///< Contain matched FcPattern pointer.
546 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
549 } // namespace Internal
551 } // namespace TextAbstraction
555 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H