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/bitmap-font.h>
23 #include <dali/devel-api/text-abstraction/font-metrics.h>
24 #include <dali/devel-api/text-abstraction/glyph-info.h>
25 #include <dali/internal/text/text-abstraction/font-client-impl.h>
26 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
29 #include <dali/internal/text/glyphy/vector-font-cache.h>
31 class VectorFontCache;
36 #include FT_FREETYPE_H
40 #include FT_SYNTHESIS_H
42 // forward declarations of font config types.
50 namespace TextAbstraction
57 * @brief Type used for indices addressing the vector with front descriptions of validated fonts.
59 typedef uint32_t FontDescriptionId;
62 * @brief Type used for indices addressing the vector with pixel buffers.
64 typedef uint32_t PixelBufferId;
67 * @brief Vector of character sets.
69 typedef Vector<_FcCharSet*> CharacterSetList;
72 * @brief FontClient implementation.
74 struct FontClient::Plugin
76 struct FontIdCacheItem
78 FontDescription::Type type; ///< The type of font.
79 FontId id; ///< Index to the cache of fonts for the specified type.
83 * @brief Caches an list of fallback fonts for a given font-description
85 struct FallbackCacheItem
87 FallbackCacheItem( FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets );
89 FontDescription fontDescription; ///< The font description.
90 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
91 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
95 * @brief Caches an index to the vector of font descriptions for a given font.
97 struct FontDescriptionCacheItem
99 FontDescriptionCacheItem( const FontDescription& fontDescription,
100 FontDescriptionId index );
101 FontDescriptionCacheItem( FontDescription&& fontDescription,
102 FontDescriptionId index );
104 FontDescription fontDescription; ///< The font description.
105 FontDescriptionId index; ///< Index to the vector of font descriptions.
109 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
111 struct FontDescriptionSizeCacheItem
113 FontDescriptionSizeCacheItem( FontDescriptionId validatedFontId,
114 PointSize26Dot6 requestedPointSize,
117 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
118 PointSize26Dot6 requestedPointSize; ///< The font point size.
119 FontId fontId; ///< The font identifier.
123 * @brief Caches the FreeType face and font metrics of the triplet 'path to the font file name, font point size and face index'.
125 struct FontFaceCacheItem
127 FontFaceCacheItem( FT_Face ftFace,
128 const FontPath& path,
129 PointSize26Dot6 requestedPointSize,
131 const FontMetrics& metrics );
133 FontFaceCacheItem( FT_Face ftFace,
134 const FontPath& path,
135 PointSize26Dot6 requestedPointSize,
137 const FontMetrics& metrics,
141 bool hasColorTables );
143 FT_Face mFreeTypeFace; ///< The FreeType face.
144 FontPath mPath; ///< The path to the font file name.
145 PointSize26Dot6 mRequestedPointSize; ///< The font point size.
146 FaceIndex mFaceIndex; ///< The face index.
147 FontMetrics mMetrics; ///< The font metrics.
148 _FcCharSet* mCharacterSet; ///< Pointer with the range of characters.
149 int mFixedSizeIndex; ///< Index to the fixed size table for the requested size.
150 float mFixedWidthPixels; ///< The height in pixels (fixed size bitmaps only)
151 float mFixedHeightPixels; ///< The height in pixels (fixed size bitmaps only)
152 unsigned int mVectorFontId; ///< The ID of the equivalent vector-based font
153 FontId mFontId; ///< Index to the vector with the cache of font's ids.
154 bool mIsFixedSizeBitmap : 1; ///< Whether the font has fixed size bitmaps.
155 bool mHasColorTables : 1; ///< Whether the font has color tables.
160 PointSize26Dot6 requestedPointSize;
165 * @brief Caches pixel buffers.
167 struct PixelBufferCacheItem
169 Devel::PixelBuffer pixelBuffer; ///< The pixel buffer loaded from the url.
170 std::string url; ///< The url.
174 * @brief Caches embedded items.
178 PixelBufferId pixelBufferId; ///< Index to the vector of pixel buffers
179 unsigned int width; ///< The desired width.
180 unsigned int height; ///< The desired height.
184 * @brief Stores a bitmap font and its pixel buffers per glyph.
186 struct BitmapFontCacheItem
188 BitmapFont font; ///< The bitmap font.
189 std::vector<Devel::PixelBuffer> pixelBuffers; ///< The pixel buffers of the glyphs.
190 FontId id; ///< Index to the vector with the cache of font's ids.
196 * Initializes the FreeType library.
197 * Initializes the dpi values.
199 * @param[in] horizontalDpi The horizontal dpi.
200 * @param[in] verticalDpi The vertical dpi.
202 Plugin( unsigned int horizontalDpi, unsigned int verticalDpi );
205 * Default destructor.
207 * Frees any allocated resource.
212 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
214 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
217 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
219 void ResetSystemDefaults();
222 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
224 void SetDefaultFont( const FontDescription& preferredFontDescription );
227 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
229 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
232 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
234 void GetDefaultFonts( FontList& defaultFonts );
237 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
239 void GetSystemFonts( FontList& systemFonts );
242 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
244 void GetDescription( FontId id, FontDescription& fontDescription ) const;
247 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
249 PointSize26Dot6 GetPointSize( FontId id );
252 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
254 bool IsCharacterSupportedByFont( FontId fontId, Character character );
257 * @brief Finds within the @p fontList a font which support the @p carcode.
259 * @param[in] fontList A list of font paths, family, width, weight and slant.
260 * @param[in] characterSetList A list that contains a character set for each description of the font list.
261 * @param[in] charcode The character for which a font is needed.
262 * @param[in] requestedPointSize The point size in 26.6 fractional points.
263 * @param[in] preferColor @e true if a color font is preferred.
265 * @return A valid font identifier, or zero if no font is found.
267 FontId FindFontForCharacter( const FontList& fontList,
268 const CharacterSetList& characterSetList,
270 PointSize26Dot6 requestedPointSize,
274 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
276 FontId FindDefaultFont( Character charcode,
277 PointSize26Dot6 requestedPointSize,
281 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
283 FontId FindFallbackFont( Character charcode,
284 const FontDescription& preferredFontDescription,
285 PointSize26Dot6 requestedPointSize,
289 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
291 * @param[in] cacheDescription Whether to cache the font description.
293 FontId GetFontId( const FontPath& path,
294 PointSize26Dot6 requestedPointSize,
296 bool cacheDescription );
299 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
301 FontId GetFontId( const FontDescription& fontDescription,
302 PointSize26Dot6 requestedPointSize,
303 FaceIndex faceIndex );
306 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const BitmapFont& bitmapFont )
308 FontId GetFontId( const BitmapFont& bitmapFont );
311 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
313 bool IsScalable( const FontPath& path );
316 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
318 bool IsScalable( const FontDescription& fontDescription );
321 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
323 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
326 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
328 void GetFixedSizes( const FontDescription& fontDescription,
329 Dali::Vector< PointSize26Dot6 >& sizes );
332 * @copydoc Dali::TextAbstraction::FontClient::HasItalicStyle()
334 bool HasItalicStyle( FontId fontId ) const;
337 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
339 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
342 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
344 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
347 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
349 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal );
352 * Helper for GetGlyphMetrics when using bitmaps
354 bool GetBitmapMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
357 * Helper for GetGlyphMetrics when using vectors
359 bool GetVectorMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
362 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
364 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth );
367 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
369 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
372 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
374 void CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight );
377 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
379 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
382 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
384 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
387 * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
389 GlyphIndex CreateEmbeddedItem( const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat );
392 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
394 FT_FaceRec_* GetFreetypeFace( FontId fontId );
397 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
399 FontDescription::Type GetFontType( FontId fontId );
402 * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
404 bool AddCustomFontDirectory( const FontPath& path );
409 * @brief Caches the fonts present in the platform.
411 * Calls GetFcFontSet() to retrieve the fonts.
413 void InitSystemFonts();
416 * @brief Gets the FontDescription which matches the given pattern.
418 * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
420 * @param[in] pattern pattern to match against.
421 * @param[out] fontDescription the resultant fontDescription that matched.
422 * @param[out] characterSet The character set for that pattern.
423 * @return true if match found.
425 bool MatchFontDescriptionToPattern( _FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet );
428 * @brief Creates a font family pattern used to match fonts.
430 * @note Need to call FcPatternDestroy to free the resources.
432 * @param[in] fontDescription The font to cache.
434 * @return The pattern.
436 _FcPattern* CreateFontFamilyPattern( const FontDescription& fontDescription ) const;
439 * @brief Retrieves the fonts present in the platform.
441 * @note Need to call FcFontSetDestroy to free the allocated resources.
443 * @return A font fonfig data structure with the platform's fonts.
445 _FcFontSet* GetFcFontSet() const;
448 * @brief Retrieves a font config object's value from a pattern.
450 * @param[in] pattern The font config pattern.
451 * @param[in] n The object.
452 * @param[out] string The object's value.
454 * @return @e true if the operation is successful.
456 bool GetFcString( const _FcPattern* const pattern, const char* const n, std::string& string );
459 * @brief Retrieves a font config object's value from a pattern.
461 * @param[in] pattern The font config pattern.
462 * @param[in] n The object.
463 * @param[out] intVal The object's value.
465 * @return @e true if the operation is successful.
467 bool GetFcInt( const _FcPattern* const pattern, const char* const n, int& intVal );
470 * @brief Creates a font.
472 * @param[in] path The path to the font file name.
473 * @param[in] requestedPointSize The requested point size.
474 * @param[in] faceIndex A face index.
475 * @param[in] cacheDescription Whether to cache the font description.
477 * @return The font identifier.
479 FontId CreateFont( const FontPath& path,
480 PointSize26Dot6 requestedPointSize,
482 bool cacheDescription );
485 * @brief Copy the color bitmap given in @p srcBuffer to @p data.
487 * @param[out] data The bitmap data.
488 * @param[in] srcWidth The width of the bitmap.
489 * @param[in] srcHeight The height of the bitmap.
490 * @param[in] srcBuffer The buffer of the bitmap.
492 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, unsigned int srcWidth, unsigned int srcHeight, const unsigned char* const srcBuffer );
495 * @brief Copy the FreeType bitmap to the given buffer.
497 * @param[out] data The bitmap data.
498 * @param[in] srcBitmap The FreeType bitmap.
499 * @param[in] isShearRequired Whether the bitmap needs a shear transform (for software italics).
501 void ConvertBitmap( TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap, bool isShearRequired );
504 * @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.
505 * If there is one , if writes the font identifier in the param @p fontId.
507 * @param[in] path Path to the font file name.
508 * @param[in] requestedPointSize The font point size.
509 * @param[in] faceIndex The face index.
510 * @param[out] fontId The font identifier.
512 * @return @e true if there triplet is found.
514 bool FindFont( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId ) const;
517 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
518 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
520 * @param[in] fontDescription The font to validate.
521 * @param[out] validatedFontId The index to the vector with font descriptions.
523 * @return @e true if the pair is found.
525 bool FindValidatedFont( const FontDescription& fontDescription,
526 FontDescriptionId& validatedFontId );
529 * @brief Finds a fallback font list from the cache for a given font-description
531 * @param[in] fontDescription The font to validate.
532 * @param[out] A valid pointer to a font list, or @e nullptr if not found.
533 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
535 bool FindFallbackFontList( const FontDescription& fontDescription,
537 CharacterSetList*& characterSetList );
540 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
541 * If there is one it writes the font identifier in the param @p fontId.
543 * @param[in] validatedFontId Index to the vector with font descriptions.
544 * @param[in] requestedPointSize The font point size.
545 * @param[out] fontId The font identifier.
547 * @return @e true if the pair is found.
549 bool FindFont( FontDescriptionId validatedFontId,
550 PointSize26Dot6 requestedPointSize,
554 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
556 * @param[in] bitmapFont The font's family name.
557 * @param[out] fontId The id of the font.
559 * @return Whether the font has been found.
561 bool FindBitmapFont( const FontFamily& bitmapFont, FontId& fontId ) const;
564 * @brief Validate a font description.
566 * @param[in] fontDescription The font to validate.
567 * @param[out] validatedFontId Result of validation
569 void ValidateFont( const FontDescription& fontDescription,
570 FontDescriptionId& validatedFontId );
573 * @brief Helper for GetDefaultFonts etc.
575 * @note CharacterSetList is a vector of FcCharSet that are reference counted. It's needed to call FcCharSetDestroy to decrease the reference counter.
577 * @param[in] fontDescription A font description.
578 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
579 * @param[out] characterSetList A list of character sets which are a close match for fontDescription.
581 void SetFontList( const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList );
584 * Caches a font path.
586 * @param[in] ftFace The FreeType face.
587 * @param[in] id The font identifier.
588 * @param[in] requestedPointSize The font point size.
589 * @param[in] path Path to the font file name.
591 void CacheFontPath( FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path );
594 * @brief Creates a character set from a given font's @p description.
596 * @note Need to call FcCharSetDestroy to free the resources.
598 * @param[in] description The font's description.
600 * @return A character set.
602 _FcCharSet* CreateCharacterSetFromDescription( const FontDescription& description );
605 * @brief Free the resources allocated in the fallback cache.
607 * @param[in] fallbackCache The fallback cache.
609 void ClearFallbackCache( std::vector<FallbackCacheItem>& fallbackCache );
612 * @brief Free the resources allocated by the FcCharSet objects.
614 void ClearCharacterSetFromFontFaceCache();
618 // Declared private and left undefined to avoid copies.
619 Plugin( const Plugin& );
620 // Declared private and left undefined to avoid copies.
621 Plugin& operator=( const Plugin& );
625 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
627 unsigned int mDpiHorizontal; ///< Horizontal dpi.
628 unsigned int mDpiVertical; ///< Vertical dpi.
630 FontDescription mDefaultFontDescription; ///< The cached default font from the system
632 FontList mSystemFonts; ///< Cached system fonts.
633 FontList mDefaultFonts; ///< Cached default fonts.
634 CharacterSetList mDefaultFontCharacterSets;
636 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
638 Vector<FontIdCacheItem> mFontIdCache;
639 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'.
640 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
641 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
642 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
643 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.
645 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
647 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
648 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
649 Vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
650 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
652 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
655 } // namespace Internal
657 } // namespace TextAbstraction
661 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H