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::GetGlyphMetrics()
291 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal);
294 * Helper for GetGlyphMetrics when using bitmaps
296 bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
299 * Helper for GetGlyphMetrics when using vectors
301 bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal);
304 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
306 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth);
309 * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
311 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
314 * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
316 void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight);
319 * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
321 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
324 * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
326 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
329 * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
331 GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
334 * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
336 void EnableAtlasLimitation(bool enabled);
339 * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
341 bool IsAtlasLimitationEnabled() const;
344 * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
346 Size GetMaximumTextAtlasSize() const;
349 * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
351 Size GetDefaultTextAtlasSize() const;
354 * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
356 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
359 * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
361 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
364 * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
366 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
369 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
371 FT_FaceRec_* GetFreetypeFace(FontId fontId);
374 * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
376 FontDescription::Type GetFontType(FontId fontId);
379 * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
381 bool AddCustomFontDirectory(const FontPath& path);
385 * @brief Caches the fonts present in the platform.
387 * Calls GetFcFontSet() to retrieve the fonts.
389 void InitSystemFonts();
392 * @brief Gets the FontDescription which matches the given pattern.
394 * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
396 * @param[in] pattern pattern to match against.
397 * @param[out] fontDescription the resultant fontDescription that matched.
398 * @param[out] characterSet The character set for that pattern.
399 * @return true if match found.
401 bool MatchFontDescriptionToPattern(_FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, _FcCharSet** characterSet);
404 * @brief Retrieves the fonts present in the platform.
406 * @note Need to call FcFontSetDestroy to free the allocated resources.
408 * @return A font fonfig data structure with the platform's fonts.
410 _FcFontSet* GetFcFontSet() const;
413 * @brief Retrieves a font config object's value from a pattern.
415 * @param[in] pattern The font config pattern.
416 * @param[in] n The object.
417 * @param[out] string The object's value.
419 * @return @e true if the operation is successful.
421 bool GetFcString(const _FcPattern* const pattern, const char* const n, std::string& string);
424 * @brief Retrieves a font config object's value from a pattern.
426 * @param[in] pattern The font config pattern.
427 * @param[in] n The object.
428 * @param[out] intVal The object's value.
430 * @return @e true if the operation is successful.
432 bool GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal);
435 * @brief Creates a font.
437 * @param[in] path The path to the font file name.
438 * @param[in] requestedPointSize The requested point size.
439 * @param[in] faceIndex A face index.
440 * @param[in] cacheDescription Whether to cache the font description.
442 * @return The font identifier.
444 FontId CreateFont(const FontPath& path,
445 PointSize26Dot6 requestedPointSize,
447 bool cacheDescription);
450 * @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.
451 * If there is one , if writes the font identifier in the param @p fontId.
453 * @param[in] path Path to the font file name.
454 * @param[in] requestedPointSize The font point size.
455 * @param[in] faceIndex The face index.
456 * @param[out] fontId The font identifier.
458 * @return @e true if there triplet is found.
460 bool FindFont(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex, FontId& fontId) const;
463 * @brief Finds in the cache a cluster 'font family, font width, font weight, font slant'
464 * If there is one, it writes the index to the vector with font descriptions in the param @p validatedFontId.
466 * @param[in] fontDescription The font to validate.
467 * @param[out] validatedFontId The index to the vector with font descriptions.
469 * @return @e true if the pair is found.
471 bool FindValidatedFont(const FontDescription& fontDescription,
472 FontDescriptionId& validatedFontId);
475 * @brief Finds a fallback font list from the cache for a given font-description
477 * @param[in] fontDescription The font to validate.
478 * @param[out] A valid pointer to a font list, or @e nullptr if not found.
479 * @param[out] characterSetList A valid pointer to a character set list, or @e nullptr if not found.
481 bool FindFallbackFontList(const FontDescription& fontDescription,
483 CharacterSetList*& characterSetList);
486 * @brief Finds in the cache a pair 'validated font identifier and font point size'.
487 * If there is one it writes the font identifier in the param @p fontId.
489 * @param[in] validatedFontId Index to the vector with font descriptions.
490 * @param[in] requestedPointSize The font point size.
491 * @param[out] fontId The font identifier.
493 * @return @e true if the pair is found.
495 bool FindFont(FontDescriptionId validatedFontId,
496 PointSize26Dot6 requestedPointSize,
500 * @brief Finds in the cache a bitmap font with the @p bitmapFont family name.
502 * @param[in] bitmapFont The font's family name.
503 * @param[out] fontId The id of the font.
505 * @return Whether the font has been found.
507 bool FindBitmapFont(const FontFamily& bitmapFont, FontId& fontId) const;
510 * @brief Validate a font description.
512 * @param[in] fontDescription The font to validate.
513 * @param[out] validatedFontId Result of validation
515 void ValidateFont(const FontDescription& fontDescription,
516 FontDescriptionId& validatedFontId);
519 * @brief Helper for GetDefaultFonts etc.
521 * @note CharacterSetList is a vector of FcCharSet that are reference counted. It's needed to call FcCharSetDestroy to decrease the reference counter.
523 * @param[in] fontDescription A font description.
524 * @param[out] fontList A list of the fonts which are a close match for fontDescription.
525 * @param[out] characterSetList A list of character sets which are a close match for fontDescription.
527 void SetFontList(const FontDescription& fontDescription, FontList& fontList, CharacterSetList& characterSetList);
530 * Caches a font path.
532 * @param[in] ftFace The FreeType face.
533 * @param[in] id The font identifier.
534 * @param[in] requestedPointSize The font point size.
535 * @param[in] path Path to the font file name.
537 void CacheFontPath(FT_Face ftFace, FontId id, PointSize26Dot6 requestedPointSize, const FontPath& path);
540 * @brief Free the resources allocated in the fallback cache.
542 * @param[in] fallbackCache The fallback cache.
544 void ClearFallbackCache(std::vector<FallbackCacheItem>& fallbackCache);
547 * @brief Free the resources allocated by the FcCharSet objects.
549 void ClearCharacterSetFromFontFaceCache();
552 Plugin(const Plugin&) = delete;
553 Plugin& operator=(const Plugin&) = delete;
556 FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
558 unsigned int mDpiHorizontal; ///< Horizontal dpi.
559 unsigned int mDpiVertical; ///< Vertical dpi.
561 FontDescription mDefaultFontDescription; ///< The cached default font from the system
563 FontList mSystemFonts; ///< Cached system fonts.
564 FontList mDefaultFonts; ///< Cached default fonts.
565 CharacterSetList mDefaultFontCharacterSets;
567 std::vector<FallbackCacheItem> mFallbackCache; ///< Cached fallback font lists.
569 Vector<FontIdCacheItem> mFontIdCache;
570 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'.
571 std::vector<FontDescriptionCacheItem> mValidatedFontCache; ///< Caches indices to the vector of font descriptions for a given font.
572 FontList mFontDescriptionCache; ///< Caches font descriptions for the validated font.
573 CharacterSetList mCharacterSetCache; ///< Caches character set lists for the validated font.
574 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.
576 VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
578 Vector<EllipsisItem> mEllipsisCache; ///< Caches ellipsis glyphs for a particular point size.
579 std::vector<PixelBufferCacheItem> mPixelBufferCache; ///< Caches the pixel buffer of a url.
580 Vector<EmbeddedItem> mEmbeddedItemCache; ///< Cache embedded items.
581 std::vector<BitmapFontCacheItem> mBitmapFontCache; ///< Stores bitmap fonts.
583 bool mDefaultFontDescriptionCached : 1; ///< Whether the default font is cached or not
585 bool mIsAtlasLimitationEnabled : 1; ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
586 Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
589 } // namespace Internal
591 } // namespace TextAbstraction
595 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H