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
37 // forward declarations of font config types.
45 namespace TextAbstraction
52 *@brief Type used for indices addressing the vector with front descriptions of validated fonts.
54 typedef uint32_t FontDescriptionId;
55 typedef Vector<_FcCharSet*> CharacterSetList;
58 * @brief FontClient implementation.
60 struct FontClient::Plugin
63 * @brief Caches an list of fallback fonts for a given font-description
65 struct FallbackCacheItem
67 FallbackCacheItem( const FontDescription& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets );
69 FontDescription fontDescription; ///< The font description.
70 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
71 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
75 * @brief Caches an index to the vector of font descriptions for a given font.
77 struct FontDescriptionCacheItem
79 FontDescriptionCacheItem( const FontDescription& fontDescription,
80 FontDescriptionId index );
82 FontDescription fontDescription; ///< The font description.
83 FontDescriptionId index; ///< Index to the vector of font descriptions.
87 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
89 struct FontIdCacheItem
91 FontIdCacheItem( FontDescriptionId validatedFontId,
92 PointSize26Dot6 requestedPointSize,
95 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
96 PointSize26Dot6 requestedPointSize; ///< The font point size.
97 FontId fontId; ///< The font identifier.
101 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
103 struct FontFaceCacheItem
105 FontFaceCacheItem( FT_Face ftFace,
106 const FontPath& path,
107 PointSize26Dot6 requestedPointSize,
109 const FontMetrics& metrics );
111 FontFaceCacheItem( FT_Face ftFace,
112 const FontPath& path,
113 PointSize26Dot6 requestedPointSize,
115 const FontMetrics& metrics,
118 bool hasColorTables );
120 FT_Face mFreeTypeFace; ///< The FreeType face.
121 FontPath mPath; ///< The path to the font file name.
122 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
123 FaceIndex mFaceIndex; ///< The face index.
124 FontMetrics mMetrics; ///< The font metrics.
125 _FcCharSet* mCharacterSet; ///< Pointer with the range of characters.
126 FT_Short mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
127 FT_Short mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
128 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
129 bool mIsFixedSizeBitmap : 1; ///< Whether the font has fixed size bitmaps.
130 bool mHasColorTables : 1; ///< Whether the font has color tables.
135 PointSize26Dot6 requestedPointSize;
142 * Initializes the FreeType library.
143 * Initializes the dpi values.
145 * @param[in] horizontalDpi The horizontal dpi.
146 * @param[in] verticalDpi The vertical dpi.
148 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
151 * Default destructor.
153 * Frees any allocated resource.
158 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
160 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
163 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
165 void ResetSystemDefaults();
168 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
170 void SetDefaultFont( const FontDescription& preferredFontDescription );
173 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
175 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
178 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
180 void GetDefaultFonts( FontList& defaultFonts );
183 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
185 void GetSystemFonts( FontList& systemFonts );
188 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
190 void GetDescription( FontId id, FontDescription& fontDescription ) const;
193 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
195 PointSize26Dot6 GetPointSize( FontId id );
198 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
200 bool IsCharacterSupportedByFont( FontId fontId, Character character );
203 * @brief Finds within the @p fontList a font which support the @p carcode.
205 * @param[in] fontList A list of font paths, family, width, weight and slant.
206 * @param[in] characterSetList A list that contains a character set for each description of the font list.
207 * @param[in] charcode The character for which a font is needed.
208 * @param[in] requestedPointSize The point size in 26.6 fractional points.
209 * @param[in] preferColor @e true if a color font is preferred.
211 * @return A valid font identifier, or zero if no font is found.
213 FontId FindFontForCharacter( const FontList& fontList,
214 const CharacterSetList& characterSetList,
216 PointSize26Dot6 requestedPointSize,
220 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
222 FontId FindDefaultFont( Character charcode,
223 PointSize26Dot6 requestedPointSize,
227 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
229 FontId FindFallbackFont( Character charcode,
230 const FontDescription& preferredFontDescription,
231 PointSize26Dot6 requestedPointSize,
235 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
237 * @param[in] cacheDescription Whether to cache the font description.
239 FontId GetFontId( const FontPath& path,
240 PointSize26Dot6 requestedPointSize,
242 bool cacheDescription );
245 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
247 FontId GetFontId( const FontDescription& fontDescription,
248 PointSize26Dot6 requestedPointSize,
249 FaceIndex faceIndex );
252 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
254 bool IsScalable( const FontPath& path );
257 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
259 bool IsScalable( const FontDescription& fontDescription );
262 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
264 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
267 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
269 void GetFixedSizes( const FontDescription& fontDescription,
270 Dali::Vector< PointSize26Dot6 >& sizes );
273 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
275 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
278 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
280 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
283 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
285 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
288 * Helper for GetGlyphMetrics when using bitmaps
290 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
293 * Helper for GetGlyphMetrics when using vectors
295 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
298 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data )
300 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data );
303 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
305 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
308 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
310 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
313 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
315 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
318 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
320 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
325 * @brief Caches the fonts present in the platform.
327 * Calls GetFcFontSet() to retrieve the fonts.
329 void InitSystemFonts();
332 * @brief Gets the FontDescription which matches the given pattern.
333 * @param[in] pattern pattern to match against.
334 * @param[out] fontDescription the resultant fontDescription that matched.
335 * @param[out] characterSet The character set for that pattern.
336 * @return true if match found.
338 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
341 * @brief Creates a font family pattern used to match fonts.
343 * @param[in] fontDescription The font to cache.
345 * @return The pattern.
347 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
350 * Retrieves the fonts present in the platform.
352 * @return A font fonfig data structure with the platform's fonts.
354 _FcFontSet* GetFcFontSet() const;
357 * Retrieves a font config object's value from a pattern.
359 * @param[in] pattern The font config pattern.
360 * @param[in] n The object.
361 * @param[out] string The object's value.
363 * @return @e true if the operation is successful.
365 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
368 * Retrieves a font config object's value from a pattern.
370 * @param[in] pattern The font config pattern.
371 * @param[in] n The object.
372 * @param[out] intVal The object's value.
374 * @return @e true if the operation is successful.
376 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
379 * @brief Creates a font.
381 * @param[in] path The path to the font file name.
382 * @param[in] requestedPointSize The requested point size.
383 * @param[in] faceIndex A face index.
384 * @param[in] cacheDescription Whether to cache the font description.
386 * @return The font identifier.
388 FontId CreateFont( const FontPath& path,
389 PointSize26Dot6 requestedPointSize,
391 bool cacheDescription );
394 * @brief Copy the FreeType bitmap to the given buffer.
396 * @param[out] data The bitmap data.
397 * @param[in] srcBitmap The FreeType bitmap.
399 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
402 * @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.
403 * If there is one , if writes the font identifier in the param @p fontId.
405 * @param[in] path Path to the font file name.
406 * @param[in] requestedPointSize The font point size.
407 * @param[in] faceIndex The face index.
408 * @param[out] fontId The font identifier.
410 * @return @e true if there triplet is found.
412 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
415 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
416 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
418 * @param[in] fontDescription The font to validate.
419 * @param[out] validatedFontId The index to the vector with font descriptions.
421 * @return @e true if the pair is found.
423 bool FindValidatedFont( const FontDescription& fontDescription,
424 FontDescriptionId& validatedFontId );
427 * @brief Finds a fallback font list from the cache for a given font-description
429 * @param[in] fontDescription The font to validate.
430 * @param[out] A valid pointer to a font list, or NULL if not found.
431 * @param[out] characterSetList A valid pointer to a character set list, or NULL if not found.
433 bool FindFallbackFontList( const FontDescription& fontDescription,
435 CharacterSetList*& characterSetList );
438 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
439 * If there is one it writes the font identifier in the param @p fontId.
441 * @param[in] validatedFontId Index to the vector with font descriptions.
442 * @param[in] requestedPointSize The font point size.
443 * @param[out] fontId The font identifier.
445 * @return @e true if the pair is found.
447 bool FindFont( FontDescriptionId validatedFontId,
448 PointSize26Dot6 requestedPointSize,
452 * @brief Validate a font description.
454 * @param[in] fontDescription The font to validate.
455 * @param[out] validatedFontId Result of validation
457 void ValidateFont( const FontDescription& fontDescription,
458 FontDescriptionId& validatedFontId );
461 * Helper for GetDefaultFonts etc.
463 * @param[in] fontDescription A font description.
464 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
465 * @param[out] characterSetList A list of the character sets which are a close match for fontDescription.
467 void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
470 * Caches a font path.
472 * @param[in] ftFace The FreeType face.
473 * @param[in] id The font identifier.
474 * @param[in] requestedPointSize The font point size.
475 * @param[in] path Path to the font file name.
477 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
480 * @brief Creates a character set from a given font's @p description.
482 * @param[in] description The font's description.
484 * @return A character set.
486 _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description ) const;
490 // Declared private and left undefined to avoid copies.
491 Plugin( const Plugin& );
492 // Declared private and left undefined to avoid copies.
493 Plugin& operator=( const Plugin& );
497 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
499 unsigned int mDpiHorizontal; ///< Horizontal dpi.
500 unsigned int mDpiVertical; ///< Vertical dpi.
502 FontDescription mDefaultFontDescription; ///< The cached default font from the system
504 FontList mSystemFonts; ///< Cached system fonts.
505 FontList mDefaultFonts; ///< Cached default fonts.
506 CharacterSetList mDefaultFontCharacterSets;
508 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
510 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'.
511 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
512 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
513 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
514 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.
516 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
518 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
520 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
523 } // namespace Internal
525 } // namespace TextAbstraction
529 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H