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) 2021 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/adaptor-framework/pixel-buffer.h>
23 #include <dali/devel-api/text-abstraction/bitmap-font.h>
24 #include <dali/devel-api/text-abstraction/font-metrics.h>
25 #include <dali/devel-api/text-abstraction/glyph-info.h>
26 #include <dali/internal/text/text-abstraction/font-client-impl.h>
27 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
28 #include <dali/internal/text/text-abstraction/plugin/embedded-item.h>
29 #include <dali/internal/text/text-abstraction/plugin/font-face-cache-item.h>
30 #include <dali/internal/text/text-abstraction/plugin/pixel-buffer-cache-item.h>
32 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
33 #include <third-party/glyphy/vector-font-cache.h>
35 class VectorFontCache;
40 #include FT_FREETYPE_H
43 #include FT_SYNTHESIS_H
45 // forward declarations of font config types.
52 namespace TextAbstraction
57 * @brief Type used for indices addressing the vector with front descriptions of validated fonts.
59 typedef uint32_t FontDescriptionId;
62 * @brief Vector of character sets.
64 typedef Vector<_FcCharSet*> CharacterSetList;
67 * @brief FontClient implementation.
69 struct FontClient::Plugin
71 struct FontIdCacheItem
73 FontDescription::Type type; ///< The type of font.
74 FontId id; ///< Index to the cache of fonts for the specified type.
78 * @brief Caches an list of fallback fonts for a given font-description
80 struct FallbackCacheItem
82 FallbackCacheItem(FontDescription&& fontDescription, FontList* fallbackFonts, CharacterSetList* characterSets);
84 FontDescription fontDescription; ///< The font description.
85 FontList* fallbackFonts; ///< The list of fallback fonts for the given font-description.
86 CharacterSetList* characterSets; ///< The list of character sets for the given font-description.
90 * @brief Caches an index to the vector of font descriptions for a given font.
92 struct FontDescriptionCacheItem
94 FontDescriptionCacheItem(const FontDescription& fontDescription,
95 FontDescriptionId index);
96 FontDescriptionCacheItem(FontDescription&& fontDescription,
97 FontDescriptionId index);
99 FontDescription fontDescription; ///< The font description.
100 FontDescriptionId index; ///< Index to the vector of font descriptions.
104 * @brief Caches the font id of the pair font point size and the index to the vector of font descriptions of validated fonts.
106 struct FontDescriptionSizeCacheItem
108 FontDescriptionSizeCacheItem(FontDescriptionId validatedFontId,
109 PointSize26Dot6 requestedPointSize,
112 FontDescriptionId validatedFontId; ///< Index to the vector with font descriptions.
113 PointSize26Dot6 requestedPointSize; ///< The font point size.
114 FontId fontId; ///< The font identifier.
119 PointSize26Dot6 requestedPointSize;
126 * Initializes the FreeType library.
127 * Initializes the dpi values.
129 * @param[in] horizontalDpi The horizontal dpi.
130 * @param[in] verticalDpi The vertical dpi.
132 Plugin(unsigned int horizontalDpi, unsigned int verticalDpi);
135 * Default destructor.
137 * Frees any allocated resource.
142 * @copydoc Dali::TextAbstraction::FontClient::ClearCache()
147 * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
149 void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
152 * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
154 void ResetSystemDefaults();
157 * @copydoc Dali::TextAbstraction::FontClient::SetDefaultFont()
159 void SetDefaultFont(const FontDescription& preferredFontDescription);
162 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
164 void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
167 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
169 void GetDefaultFonts(FontList& defaultFonts);
172 * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
174 void GetSystemFonts(FontList& systemFonts);
177 * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
179 void GetDescription(FontId id, FontDescription& fontDescription) const;
182 * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
184 PointSize26Dot6 GetPointSize(FontId id);
187 * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
189 bool IsCharacterSupportedByFont(FontId fontId, Character character);
192 * Get the cached font item for the given font
193 * @param[in] id The font id to search for
194 * @return the matching cached font item
196 const FontCacheItemInterface* GetCachedFontItem(FontId id) const;
199 * @brief Finds within the @p fontList a font which support the @p carcode.
201 * @param[in] fontList A list of font paths, family, width, weight and slant.
202 * @param[in] characterSetList A list that contains a character set for each description of the font list.
203 * @param[in] charcode The character for which a font is needed.
204 * @param[in] requestedPointSize The point size in 26.6 fractional points.
205 * @param[in] preferColor @e true if a color font is preferred.
207 * @return A valid font identifier, or zero if no font is found.
209 FontId FindFontForCharacter(const FontList& fontList,
210 const CharacterSetList& characterSetList,
212 PointSize26Dot6 requestedPointSize,
216 * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
218 FontId FindDefaultFont(Character charcode,
219 PointSize26Dot6 requestedPointSize,
223 * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
225 FontId FindFallbackFont(Character charcode,
226 const FontDescription& preferredFontDescription,
227 PointSize26Dot6 requestedPointSize,
231 * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
233 * @param[in] cacheDescription Whether to cache the font description.
235 FontId GetFontId(const FontPath& path,
236 PointSize26Dot6 requestedPointSize,
238 bool cacheDescription);
241 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
243 FontId GetFontId(const FontDescription& fontDescription,
244 PointSize26Dot6 requestedPointSize,
245 FaceIndex faceIndex);
248 * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const BitmapFont& bitmapFont )
250 FontId GetFontId(const BitmapFont& bitmapFont);
253 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
255 bool IsScalable(const FontPath& path);
258 * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
260 bool IsScalable(const FontDescription& fontDescription);
263 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
265 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
268 * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
270 void GetFixedSizes(const FontDescription& fontDescription,
271 Dali::Vector<PointSize26Dot6>& sizes);
274 * @copydoc Dali::TextAbstraction::FontClient::HasItalicStyle()
276 bool HasItalicStyle(FontId fontId) const;
279 * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
281 void GetFontMetrics(FontId fontId, FontMetrics& metrics);
284 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
286 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
289 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
291 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
294 * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
296 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal);
299 * Helper for GetGlyphMetrics when using bitmaps
301 bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
304 * Helper for GetGlyphMetrics when using vectors
306 bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
309 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
311 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth);
314 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
316 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
319 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
321 void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight);
324 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
326 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
329 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
331 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
334 * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
336 GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
339 * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
341 void EnableAtlasLimitation(bool enabled);
344 * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
346 bool IsAtlasLimitationEnabled() const;
349 * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
351 Size GetMaximumTextAtlasSize() const;
354 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
356 Size GetDefaultTextAtlasSize() const;
359 * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
361 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
364 * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
366 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
369 * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
371 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
374 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
376 FT_FaceRec_* GetFreetypeFace(FontId fontId);
379 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
381 FontDescription::Type GetFontType(FontId fontId);
384 * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
386 bool AddCustomFontDirectory(const FontPath& path);
390 * @brief Caches the fonts present in the platform.
392 * Calls GetFcFontSet() to retrieve the fonts.
394 void InitSystemFonts();
397 * @brief Gets the FontDescription which matches the given pattern.
399 * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
401 * @param[in] pattern pattern to match against.
402 * @param[out] fontDescription the resultant fontDescription that matched.
403 * @param[out] characterSet The character set for that pattern.
404 * @return true if match found.
406 bool MatchFontDescriptionToPattern(_FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet);
409 * @brief Retrieves the fonts present in the platform.
411 * @note Need to call FcFontSetDestroy to free the allocated resources.
413 * @return A font fonfig data structure with the platform's fonts.
415 _FcFontSet* GetFcFontSet() const;
418 * @brief Retrieves a font config object's value from a pattern.
420 * @param[in] pattern The font config pattern.
421 * @param[in] n The object.
422 * @param[out] string The object's value.
424 * @return @e true if the operation is successful.
426 bool GetFcString(const _FcPattern* const pattern, const char* const n, std::string& string);
429 * @brief Retrieves a font config object's value from a pattern.
431 * @param[in] pattern The font config pattern.
432 * @param[in] n The object.
433 * @param[out] intVal The object's value.
435 * @return @e true if the operation is successful.
437 bool GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal);
440 * @brief Creates a font.
442 * @param[in] path The path to the font file name.
443 * @param[in] requestedPointSize The requested point size.
444 * @param[in] faceIndex A face index.
445 * @param[in] cacheDescription Whether to cache the font description.
447 * @return The font identifier.
449 FontId CreateFont(const FontPath& path,
450 PointSize26Dot6 requestedPointSize,
452 bool cacheDescription);
455 * @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.
456 * If there is one , if writes the font identifier in the param @p fontId.
458 * @param[in] path Path to the font file name.
459 * @param[in] requestedPointSize The font point size.
460 * @param[in] faceIndex The face index.
461 * @param[out] fontId The font identifier.
463 * @return @e true if there triplet is found.
465 bool FindFont(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
468 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
469 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
471 * @param[in] fontDescription The font to validate.
472 * @param[out] validatedFontId The index to the vector with font descriptions.
474 * @return @e true if the pair is found.
476 bool FindValidatedFont(const FontDescription& fontDescription,
477 FontDescriptionId& validatedFontId);
480 * @brief Finds a fallback font list from the cache for a given font-description
482 * @param[in] fontDescription The font to validate.
483 * @param[out] A valid pointer to a font list, or @e nullptr if not found.
484 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
486 bool FindFallbackFontList(const FontDescription& fontDescription,
488 CharacterSetList*& characterSetList);
491 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
492 * If there is one it writes the font identifier in the param @p fontId.
494 * @param[in] validatedFontId Index to the vector with font descriptions.
495 * @param[in] requestedPointSize The font point size.
496 * @param[out] fontId The font identifier.
498 * @return @e true if the pair is found.
500 bool FindFont(FontDescriptionId validatedFontId,
501 PointSize26Dot6 requestedPointSize,
505 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
507 * @param[in] bitmapFont The font's family name.
508 * @param[out] fontId The id of the font.
510 * @return Whether the font has been found.
512 bool FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const;
515 * @brief Validate a font description.
517 * @param[in] fontDescription The font to validate.
518 * @param[out] validatedFontId Result of validation
520 void ValidateFont(const FontDescription& fontDescription,
521 FontDescriptionId& validatedFontId);
524 * @brief Helper for GetDefaultFonts etc.
526 * @note CharacterSetList is a vector of FcCharSet that are reference counted. It's needed to call FcCharSetDestroy to decrease the reference counter.
528 * @param[in] fontDescription A font description.
529 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
530 * @param[out] characterSetList A list of character sets which are a close match for fontDescription.
532 void SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList);
535 * Caches a font path.
537 * @param[in] ftFace The FreeType face.
538 * @param[in] id The font identifier.
539 * @param[in] requestedPointSize The font point size.
540 * @param[in] path Path to the font file name.
542 void CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path);
545 * @brief Free the resources allocated in the fallback cache.
547 * @param[in] fallbackCache The fallback cache.
549 void ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache);
552 * @brief Free the resources allocated by the FcCharSet objects.
554 void ClearCharacterSetFromFontFaceCache();
557 Plugin(const Plugin&) = delete;
558 Plugin& operator=(const Plugin&) = delete;
561 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
563 unsigned int mDpiHorizontal; ///< Horizontal dpi.
564 unsigned int mDpiVertical; ///< Vertical dpi.
566 FontDescription mDefaultFontDescription; ///< The cached default font from the system
568 FontList mSystemFonts; ///< Cached system fonts.
569 FontList mDefaultFonts; ///< Cached default fonts.
570 CharacterSetList mDefaultFontCharacterSets;
572 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
574 Vector<FontIdCacheItem> mFontIdCache;
575 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'.
576 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
577 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
578 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
579 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.
581 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
583 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
584 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
585 Vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
586 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
588 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
590 bool mIsAtlasLimitationEnabled : 1; ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
591 Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
594 } // namespace Internal
596 } // namespace TextAbstraction
600 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H