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 identifier.
98 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
100 struct FontFaceCacheItem
102 FontFaceCacheItem( FT_Face ftFace,
103 const FontPath& path,
104 PointSize26Dot6 requestedPointSize,
106 const FontMetrics& metrics );
108 FontFaceCacheItem( 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::TextAbstraction::FontClient::SetDpi()
154 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
157 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
159 void ResetSystemDefaults();
162 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
164 void SetDefaultFont( const FontDescription& preferredFontDescription );
167 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
169 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
172 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
174 void GetDefaultFonts( FontList& defaultFonts );
177 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
179 void GetSystemFonts( FontList& systemFonts );
182 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
184 void GetDescription( FontId id, FontDescription& fontDescription ) const;
187 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
189 PointSize26Dot6 GetPointSize( FontId id );
192 * @brief Finds within the @p fontList a font which support the @p carcode.
194 * @param[in] fontList A list of font paths, family, width, weight and slant.
195 * @param[in] charcode The character for which a font is needed.
196 * @param[in] requestedPointSize The point size in 26.6 fractional points.
197 * @param[in] preferColor @e true if a color font is preferred.
199 * @return A valid font identifier, or zero if no font is found.
201 FontId FindFontForCharacter( const FontList& fontList,
203 PointSize26Dot6 requestedPointSize,
207 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
209 FontId FindDefaultFont( Character charcode,
210 PointSize26Dot6 requestedPointSize,
214 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
216 FontId FindFallbackFont( Character charcode,
217 const FontDescription& preferredFontDescription,
218 PointSize26Dot6 requestedPointSize,
222 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
224 * @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.
225 * @param[in] cacheDescription Whether to cache the font description.
227 FontId GetFontId( const FontPath& path,
228 PointSize26Dot6 requestedPointSize,
229 PointSize26Dot6 actualPointSize,
231 bool cacheDescription = true );
234 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
236 * @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.
238 FontId GetFontId( const FontDescription& fontDescription,
239 PointSize26Dot6 requestedPointSize,
240 PointSize26Dot6 actualPointSize,
241 FaceIndex faceIndex );
244 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
246 bool IsScalable( const FontPath& path );
249 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
251 bool IsScalable( const FontDescription& fontDescription );
254 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
256 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
259 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
261 void GetFixedSizes( const FontDescription& fontDescription,
262 Dali::Vector< PointSize26Dot6 >& sizes );
265 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
267 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
270 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
272 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
275 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
277 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
280 * Helper for GetGlyphMetrics when using bitmaps
282 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
285 * Helper for GetGlyphMetrics when using vectors
287 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
290 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data )
292 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data );
295 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
297 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
300 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
302 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
305 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
307 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
310 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
312 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
317 * Caches the fonts present in the platform.
319 * Calls GetFcFontSet() to retrieve the fonts.
321 void InitSystemFonts();
324 * Gets the FontDescription which matches the given pattern
325 * @param[in] pattern pattern to match against
326 * @param[out] fontDescription the resultant fontDescription that matched
327 * @return true if match found
329 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription );
332 * @brief Creates a font family pattern used to match fonts.
334 * @param[in] fontDescription The font to cache.
336 * @return The pattern.
338 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription );
341 * Retrieves the fonts present in the platform.
343 * @return A font fonfig data structure with the platform's fonts.
345 _FcFontSet* GetFcFontSet() const;
348 * Retrieves a font config object's value from a pattern.
350 * @param[in] pattern The font config pattern.
351 * @param[in] n The object.
352 * @param[out] string The object's value.
354 * @return @e true if the operation is successful.
356 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
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] intVal The object's value.
365 * @return @e true if the operation is successful.
367 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
370 * @brief Creates a font.
372 * @param[in] path The path to the font file name.
373 * @param[in] requestedPointSize The requested point size.
374 * @param[in] actualPointSize The actual point size (for color emojis).
375 * @param[in] faceIndex A face index.
376 * @param[in] cacheDescription Whether to cache the font description.
378 * @return The font identifier.
380 FontId CreateFont( const FontPath& path,
381 PointSize26Dot6 requestedPointSize,
382 PointSize26Dot6 actualPointSize,
384 bool cacheDescription );
387 * @brief Copy the FreeType bitmap to the given buffer.
389 * @param[out] data The bitmap data.
390 * @param[in] srcBitmap The FreeType bitmap.
392 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap );
395 * @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.
396 * If there is one , if writes the font identifier in the param @p fontId.
398 * @param[in] path Path to the font file name.
399 * @param[in] requestedPointSize The font point size.
400 * @param[in] faceIndex The face index.
401 * @param[out] fontId The font identifier.
403 * @return @e true if there triplet is found.
405 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
408 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
409 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
411 * @param[in] fontDescription The font to validate.
412 * @param[out] validatedFontId The index to the vector with font descriptions.
414 * @return @e true if the pair is found.
416 bool FindValidatedFont( const FontDescription& fontDescription,
417 FontDescriptionId& validatedFontId );
420 * @brief Finds a fallback font list from the cache for a given font-description
422 * @param[in] fontDescription The font to validate.
423 * @param[out] A valid pointer to a font list, or NULL if not found.
425 bool FindFallbackFontList( const FontDescription& fontDescription,
426 FontList*& fontList );
429 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
430 * If there is one it writes the font identifier in the param @p fontId.
432 * @param[in] validatedFontId Index to the vector with font descriptions.
433 * @param[in] requestedPointSize The font point size.
434 * @param[out] fontId The font identifier.
436 * @return @e true if the pair is found.
438 bool FindFont( FontDescriptionId validatedFontId,
439 PointSize26Dot6 requestedPointSize,
443 * @brief Validate a font description.
445 * @param[in] fontDescription The font to validate.
446 * @param[out] validatedFontId Result of validation
448 void ValidateFont( const FontDescription& fontDescription,
449 FontDescriptionId& validatedFontId );
452 * Helper for GetDefaultFonts etc.
454 * @param[in] fontDescription A font description.
455 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
457 void SetFontList( const FontDescription& fontDescription, FontList& fontList );
460 * Caches a font path.
462 * @param[in] ftFace The FreeType face.
463 * @param[in] id The font identifier.
464 * @param[in] requestedPointSize The font point size.
465 * @param[in] path Path to the font file name.
467 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
471 // Declared private and left undefined to avoid copies.
472 Plugin( const Plugin& );
473 // Declared private and left undefined to avoid copies.
474 Plugin& operator=( const Plugin& );
478 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
480 unsigned int mDpiHorizontal; ///< Horizontal dpi.
481 unsigned int mDpiVertical; ///< Vertical dpi.
483 FontDescription mDefaultFontDescription; ///< The cached default font from the system
485 FontList mSystemFonts; ///< Cached system fonts.
486 FontList mDefaultFonts; ///< Cached default fonts.
488 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
490 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'.
491 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
492 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
493 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.
495 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
497 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
499 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
502 } // namespace Internal
504 } // namespace TextAbstraction
508 #endif // __DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H__