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) 2017 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
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( const 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 );
84 FontDescription fontDescription; ///< The font description.
85 FontDescriptionId index; ///< Index to the vector of font descriptions.
89 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
91 struct FontIdCacheItem
93 FontIdCacheItem( FontDescriptionId validatedFontId,
94 PointSize26Dot6 requestedPointSize,
97 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
98 PointSize26Dot6 requestedPointSize; ///< The font point size.
99 FontId fontId; ///< The font identifier.
103 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
105 struct FontFaceCacheItem
107 FontFaceCacheItem( FT_Face ftFace,
108 const FontPath& path,
109 PointSize26Dot6 requestedPointSize,
111 const FontMetrics& metrics );
113 FontFaceCacheItem( FT_Face ftFace,
114 const FontPath& path,
115 PointSize26Dot6 requestedPointSize,
117 const FontMetrics& metrics,
120 bool hasColorTables );
122 FT_Face mFreeTypeFace; ///< The FreeType face.
123 FontPath mPath; ///< The path to the font file name.
124 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
125 FaceIndex mFaceIndex; ///< The face index.
126 FontMetrics mMetrics; ///< The font metrics.
127 _FcCharSet* mCharacterSet; ///< Pointer with the range of characters.
128 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
129 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
130 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
131 bool mIsFixedSizeBitmap : 1; ///< Whether the font has fixed size bitmaps.
132 bool mHasColorTables : 1; ///< Whether the font has color tables.
137 PointSize26Dot6 requestedPointSize;
144 * Initializes the FreeType library.
145 * Initializes the dpi values.
147 * @param[in] horizontalDpi The horizontal dpi.
148 * @param[in] verticalDpi The vertical dpi.
150 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
153 * Default destructor.
155 * Frees any allocated resource.
160 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
162 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
165 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
167 void ResetSystemDefaults();
170 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
172 void SetDefaultFont( const FontDescription& preferredFontDescription );
175 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
177 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
180 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
182 void GetDefaultFonts( FontList& defaultFonts );
185 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
187 void GetSystemFonts( FontList& systemFonts );
190 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
192 void GetDescription( FontId id, FontDescription& fontDescription ) const;
195 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
197 PointSize26Dot6 GetPointSize( FontId id );
200 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
202 bool IsCharacterSupportedByFont( FontId fontId, Character character );
205 * @brief Finds within the @p fontList a font which support the @p carcode.
207 * @param[in] fontList A list of font paths, family, width, weight and slant.
208 * @param[in] characterSetList A list that contains a character set for each description of the font list.
209 * @param[in] charcode The character for which a font is needed.
210 * @param[in] requestedPointSize The point size in 26.6 fractional points.
211 * @param[in] preferColor @e true if a color font is preferred.
213 * @return A valid font identifier, or zero if no font is found.
215 FontId FindFontForCharacter( const FontList& fontList,
216 const CharacterSetList& characterSetList,
218 PointSize26Dot6 requestedPointSize,
222 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
224 FontId FindDefaultFont( Character charcode,
225 PointSize26Dot6 requestedPointSize,
229 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
231 FontId FindFallbackFont( Character charcode,
232 const FontDescription& preferredFontDescription,
233 PointSize26Dot6 requestedPointSize,
237 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
239 * @param[in] cacheDescription Whether to cache the font description.
241 FontId GetFontId( const FontPath& path,
242 PointSize26Dot6 requestedPointSize,
244 bool cacheDescription );
247 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
249 FontId GetFontId( const FontDescription& fontDescription,
250 PointSize26Dot6 requestedPointSize,
251 FaceIndex faceIndex );
254 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
256 bool IsScalable( const FontPath& path );
259 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
261 bool IsScalable( const FontDescription& fontDescription );
264 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
266 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
269 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
271 void GetFixedSizes( const FontDescription& fontDescription,
272 Dali::Vector< PointSize26Dot6 >& sizes );
275 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
277 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
280 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
282 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
285 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
287 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
290 * Helper for GetGlyphMetrics when using bitmaps
292 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
295 * Helper for GetGlyphMetrics when using vectors
297 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
300 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
302 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth );
305 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
307 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
310 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
312 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
315 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
317 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
320 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
322 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
327 * @brief Caches the fonts present in the platform.
329 * Calls GetFcFontSet() to retrieve the fonts.
331 void InitSystemFonts();
334 * @brief Gets the FontDescription which matches the given pattern.
335 * @param[in] pattern pattern to match against.
336 * @param[out] fontDescription the resultant fontDescription that matched.
337 * @param[out] characterSet The character set for that pattern.
338 * @return true if match found.
340 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
343 * @brief Creates a font family pattern used to match fonts.
345 * @param[in] fontDescription The font to cache.
347 * @return The pattern.
349 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
352 * Retrieves the fonts present in the platform.
354 * @return A font fonfig data structure with the platform's fonts.
356 _FcFontSet* GetFcFontSet() const;
359 * Retrieves a font config object's value from a pattern.
361 * @param[in] pattern The font config pattern.
362 * @param[in] n The object.
363 * @param[out] string The object's value.
365 * @return @e true if the operation is successful.
367 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
370 * Retrieves a font config object's value from a pattern.
372 * @param[in] pattern The font config pattern.
373 * @param[in] n The object.
374 * @param[out] intVal The object's value.
376 * @return @e true if the operation is successful.
378 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
381 * @brief Creates a font.
383 * @param[in] path The path to the font file name.
384 * @param[in] requestedPointSize The requested point size.
385 * @param[in] faceIndex A face index.
386 * @param[in] cacheDescription Whether to cache the font description.
388 * @return The font identifier.
390 FontId CreateFont( const FontPath& path,
391 PointSize26Dot6 requestedPointSize,
393 bool cacheDescription );
396 * @brief Copy the FreeType bitmap to the given buffer.
398 * @param[out] data The bitmap data.
399 * @param[in] srcBitmap The FreeType bitmap.
401 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
404 * @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.
405 * If there is one , if writes the font identifier in the param @p fontId.
407 * @param[in] path Path to the font file name.
408 * @param[in] requestedPointSize The font point size.
409 * @param[in] faceIndex The face index.
410 * @param[out] fontId The font identifier.
412 * @return @e true if there triplet is found.
414 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
417 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
418 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
420 * @param[in] fontDescription The font to validate.
421 * @param[out] validatedFontId The index to the vector with font descriptions.
423 * @return @e true if the pair is found.
425 bool FindValidatedFont( const FontDescription& fontDescription,
426 FontDescriptionId& validatedFontId );
429 * @brief Finds a fallback font list from the cache for a given font-description
431 * @param[in] fontDescription The font to validate.
432 * @param[out] A valid pointer to a font list, or NULL if not found.
433 * @param[out] characterSetList A valid pointer to a character set list, or NULL if not found.
435 bool FindFallbackFontList( const FontDescription& fontDescription,
437 CharacterSetList*& characterSetList );
440 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
441 * If there is one it writes the font identifier in the param @p fontId.
443 * @param[in] validatedFontId Index to the vector with font descriptions.
444 * @param[in] requestedPointSize The font point size.
445 * @param[out] fontId The font identifier.
447 * @return @e true if the pair is found.
449 bool FindFont( FontDescriptionId validatedFontId,
450 PointSize26Dot6 requestedPointSize,
454 * @brief Validate a font description.
456 * @param[in] fontDescription The font to validate.
457 * @param[out] validatedFontId Result of validation
459 void ValidateFont( const FontDescription& fontDescription,
460 FontDescriptionId& validatedFontId );
463 * Helper for GetDefaultFonts etc.
465 * @param[in] fontDescription A font description.
466 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
467 * @param[out] characterSetList A list of the character sets which are a close match for fontDescription.
469 void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
472 * Caches a font path.
474 * @param[in] ftFace The FreeType face.
475 * @param[in] id The font identifier.
476 * @param[in] requestedPointSize The font point size.
477 * @param[in] path Path to the font file name.
479 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
482 * @brief Creates a character set from a given font's @p description.
484 * @param[in] description The font's description.
486 * @return A character set.
488 _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description ) const;
492 // Declared private and left undefined to avoid copies.
493 Plugin( const Plugin& );
494 // Declared private and left undefined to avoid copies.
495 Plugin& operator=( const Plugin& );
499 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
501 unsigned int mDpiHorizontal; ///< Horizontal dpi.
502 unsigned int mDpiVertical; ///< Vertical dpi.
504 FontDescription mDefaultFontDescription; ///< The cached default font from the system
506 FontList mSystemFonts; ///< Cached system fonts.
507 FontList mDefaultFonts; ///< Cached default fonts.
508 CharacterSetList mDefaultFontCharacterSets;
510 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
512 std::vector<FontFaceCacheItem> mFontCache; ///< Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
513 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
514 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
515 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
516 std::vector<FontIdCacheItem> mFontIdCache; ///< Caches font identifiers for the pairs of font point size and the index to the vector with font descriptions of the validated fonts.
518 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
520 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
522 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
525 } // namespace Internal
527 } // namespace TextAbstraction
531 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H