1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
5 * Copyright (c) 2023 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-list.h>
23 #include <dali/devel-api/text-abstraction/glyph-buffer-data.h>
24 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
25 #include <dali/public-api/common/dali-vector.h>
26 #include <dali/public-api/dali-adaptor-common.h>
27 #include <dali/public-api/images/pixel-data.h>
28 #include <dali/public-api/object/base-handle.h>
32 namespace TextAbstraction
38 namespace Internal DALI_INTERNAL
44 * @brief FontClient provides access to font information and resources.
46 * <h3>Querying the System Fonts</h3>
48 * A "system font" is described by a "path" to a font file on the native filesystem, along with a "family" and "style".
49 * For example on the Ubuntu system a "Regular" style font from the "Ubuntu Mono" family can be accessed from "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf".
51 * <h3>Accessing Fonts</h3>
53 * A "font" is created from the system for a specific point size in 26.6 fractional points. A "FontId" is used to identify each font.
54 * For example two different fonts with point sizes 10 & 12 can be created from the "Ubuntu Mono" family:
56 * FontClient fontClient = FontClient::Get();
57 * FontId ubuntuMonoTen = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 10*64 );
58 * FontId ubuntuMonoTwelve = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 12*64 );
60 * Glyph metrics and bitmap resources can then be retrieved using the FontId.
62 class DALI_ADAPTOR_API FontClient : public BaseHandle
65 static const PointSize26Dot6 DEFAULT_POINT_SIZE; ///< The default point size.
66 static const float DEFAULT_ITALIC_ANGLE; ///< The default software italic angle in radians.
68 static const bool DEFAULT_ATLAS_LIMITATION_ENABLED; ///< The default behavior of whether atlas limitation is enabled in dali.
69 static const uint32_t DEFAULT_TEXT_ATLAS_WIDTH; ///< The default width of text-atlas-block.
70 static const uint32_t DEFAULT_TEXT_ATLAS_HEIGHT; ///< The default height of text-atlas-block.
71 static const Size DEFAULT_TEXT_ATLAS_SIZE; ///< The default size(width, height) of text-atlas-block.
73 static const uint32_t MAX_TEXT_ATLAS_WIDTH; ///< The maximum width of text-atlas-block.
74 static const uint32_t MAX_TEXT_ATLAS_HEIGHT; ///< The maximum height of text-atlas-block.
75 static const Size MAX_TEXT_ATLAS_SIZE; ///< The maximum height of text-atlas-block.
77 static const uint16_t PADDING_TEXT_ATLAS_BLOCK; ///< Padding per edge. How much the block size (width, height) less than the text-atlas-block size (width, height).
78 static const Size MAX_SIZE_FIT_IN_ATLAS; ///< The maximum block's size fit into text-atlas-block.
80 static const uint32_t NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE; ///< Factor multiply point-size in toolkit.
83 static uint32_t GetPerformanceLogThresholdTime(); ///< Return performance log threshold time in miliseconds for debug.
84 static bool IsPerformanceLogEnabled(); ///< Whether performance log is enabled.
87 * @brief Used to load an embedded item into the font client.
89 struct EmbeddedItemDescription
91 std::string url; ///< The url path of the image.
92 unsigned int width; ///< The width of the item.
93 unsigned int height; ///< The height of the item.
94 ColorBlendingMode colorblendingMode; ///< Whether the color of the image is multiplied by the color of the text.
99 * @brief Retrieve a handle to the FontClient instance.
101 * @return A handle to the FontClient
102 * @remarks A reference to the singleton instance of FontClient.
104 static FontClient Get();
107 * @brief Create a handle to the new FontClient instance.
109 * @return A handle to the FontClient
110 * @remarks All functions of this are not thread-safe,
111 * so create new handles for each worker thread to utilize them.
113 static FontClient New();
116 * @brief Create a handle to the new FontClient instance.
118 * @param[in] horizontalDpi The horizontal resolution in DPI.
119 * @param[in] verticalDpi The vertical resolution in DPI.
120 * @return A handle to the FontClient
121 * @remarks All functions of this are not thread-safe,
122 * so create new handles for each worker thread to utilize them.
124 static FontClient New(uint32_t horizontalDpi, uint32_t verticalDpi);
127 * @brief Create an uninitialized TextAbstraction handle.
134 * This is non-virtual since derived Handle types must not contain data or virtual methods.
139 * @brief This copy constructor is required for (smart) pointer semantics.
141 * @param[in] handle A reference to the copied handle.
143 FontClient(const FontClient& handle);
146 * @brief This assignment operator is required for (smart) pointer semantics.
148 * @param [in] handle A reference to the copied handle.
149 * @return A reference to this.
151 FontClient& operator=(const FontClient& handle);
154 * @brief This move constructor is required for (smart) pointer semantics.
156 * @param[in] handle A reference to the moved handle.
158 FontClient(FontClient&& handle);
161 * @brief This move assignment operator is required for (smart) pointer semantics.
163 * @param [in] handle A reference to the moved handle.
164 * @return A reference to this.
166 FontClient& operator=(FontClient&& handle);
168 ////////////////////////////////////////
169 // Font management and validation.
170 ////////////////////////////////////////
173 * @brief Clear all caches in FontClient
179 * @brief Clear caches in FontClient when Locale Changed
181 * @note This function only clears caches that needs to be cleared.
183 void ClearCacheOnLocaleChanged();
186 * @brief Set the DPI of the target window.
188 * @note Multiple windows are not currently supported.
189 * @param[in] horizontalDpi The horizontal resolution in DPI.
190 * @param[in] verticalDpi The vertical resolution in DPI.
192 void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
195 * @brief Sets the DPI of the FontClient based on the current window's DPI.
197 * @note Multiple windows are not currently supported.
199 void SetDpiFromWindowSystem();
202 * @brief Retrieves the DPI previously set to the target window.
204 * @note Multiple windows are not currently supported.
205 * @param[out] horizontalDpi The horizontal resolution in DPI.
206 * @param[out] verticalDpi The vertical resolution in DPI.
208 void GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi);
211 * @brief Called by Dali to retrieve the default font size for the platform.
213 * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
214 * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
215 * @return The default font size.
217 int GetDefaultFontSize();
220 * @brief Called when the user changes the system defaults.
222 * @post Previously cached system defaults are removed.
224 void ResetSystemDefaults();
227 * @brief Retrieve the list of default fonts supported by the system.
229 * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
231 void GetDefaultFonts(FontList& defaultFonts);
234 * @brief Initializes and caches default font from the system.
236 void InitDefaultFontDescription();
239 * @brief Retrieve the active default font from the system.
241 * @param[out] fontDescription font structure describing the default font.
243 void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
246 * @brief Retrieve the list of fonts supported by the system.
248 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
250 void GetSystemFonts(FontList& systemFonts);
253 * @brief Retrieves the font description of a given font @p fontId.
255 * @param[in] fontId The font identifier.
256 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
258 void GetDescription(FontId fontId, FontDescription& fontDescription);
261 * @brief Retrieves the font point size of a given font @p fontId.
263 * @param[in] fontId The font identifier.
265 * @return The point size in 26.6 fractional points.
267 PointSize26Dot6 GetPointSize(FontId fontId);
270 * @brief Whether the given @p character is supported by the font.
272 * @param[in] fontId The id of the font.
273 * @param[in] character The character.
275 * @return @e true if the character is supported by the font.
277 bool IsCharacterSupportedByFont(FontId fontId, Character character);
280 * @brief Find the default font for displaying a UTF-32 character.
282 * This is useful when localised strings are provided for multiple languages
283 * i.e. when a single default font does not work for all languages.
285 * @param[in] charcode The character for which a font is needed.
286 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
287 * @param[in] preferColor @e true if a color font is preferred.
289 * @return A valid font identifier, or zero if the font does not exist.
291 FontId FindDefaultFont(Character charcode,
292 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
293 bool preferColor = false);
296 * @brief Find a fallback-font for displaying a UTF-32 character.
298 * This is useful when localised strings are provided for multiple languages
299 * i.e. when a single default font does not work for all languages.
301 * @param[in] charcode The character for which a font is needed.
302 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
303 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
304 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
305 * @param[in] preferColor @e true if a color font is preferred.
307 * @return A valid font identifier, or zero if the font does not exist.
309 FontId FindFallbackFont(Character charcode,
310 const FontDescription& preferredFontDescription,
311 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
312 bool preferColor = false);
315 * @brief Retrieve the unique identifier for a font.
317 * @param[in] path The path to a font file.
318 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
319 * @param[in] faceIndex The index of the font face (optional).
321 * @return A valid font identifier, or zero if the font does not exist.
323 FontId GetFontId(const FontPath& path,
324 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
325 FaceIndex faceIndex = 0);
328 * @brief Retrieves a unique font identifier for a given description.
330 * @param[in] preferredFontDescription Description of the preferred font.
331 * The font will be the closest match to @p preferredFontDescription.
332 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
333 * @param[in] faceIndex The index of the font face (optional).
335 * @return A valid font identifier, or zero if no font is found.
337 FontId GetFontId(const FontDescription& preferredFontDescription,
338 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
339 FaceIndex faceIndex = 0);
342 * @brief Retrieves a unique font identifier for a given bitmap font.
343 * If the font is not present, it will cache the given font, and give it a new font id.
345 * @param[in] bitmapFont A bitmap font.
347 * @return A valid font identifier.
349 FontId GetFontId(const BitmapFont& bitmapFont);
352 * @brief Check to see if a font is scalable.
354 * @param[in] path The path to a font file.
355 * @return true if scalable.
357 bool IsScalable(const FontPath& path);
360 * @brief Check to see if a font is scalable.
362 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
364 * @param[in] fontDescription A font description.
366 * @return true if scalable
368 bool IsScalable(const FontDescription& fontDescription);
371 * @brief Get a list of sizes available for a fixed size font.
373 * @param[in] path The path to a font file.
374 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
376 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
379 * @brief Get a list of sizes available for a fixed size font.
381 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
383 * @param[in] fontDescription A font description.
384 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
386 void GetFixedSizes(const FontDescription& fontDescription,
387 Dali::Vector<PointSize26Dot6>& sizes);
390 * @brief Whether the font has Italic style.
392 * @param[in] fontId The font identifier.
394 * @return true if the font has italic style.
396 bool HasItalicStyle(FontId fontId) const;
398 ////////////////////////////////////////
399 // Font metrics, glyphs and bitmaps.
400 ////////////////////////////////////////
403 * @brief Query the metrics for a font.
405 * @param[in] fontId The identifier of the font for the required glyph.
406 * @param[out] metrics The font metrics.
408 void GetFontMetrics(FontId fontId, FontMetrics& metrics);
411 * @brief Retrieve the glyph index for a UTF-32 character code.
413 * @param[in] fontId The identifier of the font for the required glyph.
414 * @param[in] charcode The UTF-32 character code.
416 * @return The glyph index, or zero if the character code is undefined.
418 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
421 * @brief Return the glyph index of a given character code as modified by the variation selector.
423 * @param[in] fontId The identifier of the font for the required glyph.
424 * @param[in] charcode The UTF-32 character code.
425 * @param[in] variantSelector The UTF-32 character code point of the variation selector.
427 * @return The glyph index, or zero if the character code is undefined.
429 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
432 * @brief Retrieve the metrics for a series of glyphs.
434 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
435 * It may contain the advance and an offset set into the bearing from the shaping tool.
436 * On return, the glyph's size value will be initialized. The bearing value will be updated by adding the font's glyph bearing to the one set by the shaping tool.
437 * @param[in] size The size of the array.
438 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
439 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
441 * @return @e true if all of the requested metrics were found.
443 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true);
446 * @brief Create a bitmap representation of a glyph.
448 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
450 * @param[in] fontId The identifier of the font.
451 * @param[in] glyphIndex The index of a glyph within the specified font.
452 * @param[in] isItalicRequired Whether the glyph requires italic style.
453 * @param[in] isBoldRequired Whether the glyph requires bold style.
454 * @param[out] data The bitmap data.
455 * @param[in] outlineWidth The width of the glyph outline in pixels.
457 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth);
460 * @brief Create a bitmap representation of a glyph.
462 * @param[in] fontId The identifier of the font.
463 * @param[in] glyphIndex The index of a glyph within the specified font.
464 * @param[in] outlineWidth The width of the glyph outline in pixels.
466 * @return A valid PixelData, or an empty handle if the glyph could not be rendered.
468 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
471 * @brief Create a vector representation of a glyph.
473 * @note This feature requires highp shader support and is not available on all platforms
474 * @param[in] fontId The identifier of the font.
475 * @param[in] glyphIndex The index of a glyph within the specified font.
476 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
477 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
478 * @param[out] nominalWidth The width of the blob.
479 * @param[out] nominalHeight The height of the blob.
481 void CreateVectorBlob(FontId fontId,
482 GlyphIndex glyphIndex,
484 unsigned int& blobLength,
485 unsigned int& nominalWidth,
486 unsigned int& nominalHeight);
489 * @brief Retrieves the ellipsis glyph for a requested point size.
491 * @param[in] requestedPointSize The requested point size.
493 * @return The ellipsis glyph.
495 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
498 * @brief Whether the given glyph @p glyphIndex is a color glyph.
500 * @param[in] fontId The font id.
501 * @param[in] glyphIndex The glyph index.
503 * @return @e true if the glyph is a color one.
505 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
508 * @brief Add custom fonts directory
510 * @param[in] path to the fonts directory
512 * @return true if the fonts can be added.
514 bool AddCustomFontDirectory(const FontPath& path);
517 * @brief Apply custom fonts directories
519 void ApplyCustomFontDirectories();
522 * @brief Creates and stores an embedded item and it's metrics.
524 * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
525 * If in the @p description there is a url and @e width or @e height are zero it stores the default size. Otherwise the image is resized.
526 * If the url in the @p description is empty it stores the size.
528 * @param[in] description The description of the embedded item.
529 * @param[out] pixelFormat The pixel format of the image.
531 * return The index within the vector of embedded items.
533 GlyphIndex CreateEmbeddedItem(const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
536 * @brief true to enable Atlas-Limitation.
538 * @note Used default configuration.
539 * @param[in] enabled The on/off value to enable/disable Atlas-Limitation.
541 void EnableAtlasLimitation(bool enabled);
544 * @brief Check Atlas-Limitation is enabled or disabled.
546 * @note Used default configuration.
547 * return true if Atlas-Limitation is enabled, otherwise false.
549 bool IsAtlasLimitationEnabled() const;
552 * @brief retrieve the maximum allowed width and height for text-atlas-block.
554 * @note Used default configuration.
555 * return the maximum width and height of text-atlas-block.
557 Size GetMaximumTextAtlasSize() const;
560 * @brief retrieve the default width and height for text-atlas-block.
562 * @note Used default configuration.
563 * return the default width and height of text-atlas-block.
565 Size GetDefaultTextAtlasSize() const;
568 * @brief retrieve the current maximum width and height for text-atlas-block.
570 * @note Used default configuration.
571 * return the current maximum width and height of text-atlas-block.
573 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
576 * @brief set the achieved size (width and height) for text-atlas-block.
577 * If @p currentMaximumBlockSizeFitInAtlas larger than the current maximum text atlas then store, otherwise ignore.
579 * @note Used default configuration.
580 * return true if the current maximum text atlas size is changed, otherwise false.
582 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
585 * @brief retrieve the number of points to scale-up one unit of point-size.
587 * @note Used default configuration.
588 * return the number of points per one unit of point-size
590 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
592 public: // Not intended for application developers
594 * @brief This constructor is used by FontClient::Get().
596 * @param[in] fontClient A pointer to the internal fontClient object.
598 explicit DALI_INTERNAL FontClient(Internal::FontClient* fontClient);
602 * @brief This is used to improve application launch performance
604 * @return A pre-initialized FontClient
606 DALI_ADAPTOR_API FontClient FontClientPreInitialize();
609 * @brief This is used to pre-cache FontConfig in order to improve the runtime performance of the application.
611 * @param[in] fallbackFamilyList A list of fallback font families to be pre-cached.
612 * @param[in] extraFamilyList A list of additional font families to be pre-cached.
613 * @param[in] localeFamily A locale font family to be pre-cached.
614 * @param[in] useThread True if the font client should create thread and perform pre-caching, false otherwise.
615 * @param[in] syncCreation True if thread creation guarantees syncronization with the main thread, false async creation.
617 DALI_ADAPTOR_API void FontClientPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily, bool useThread, bool syncCreation);
620 * @brief This is used to pre-load FreeType font face in order to improve the runtime performance of the application.
622 * @param[in] fontPathList A list of font paths to be pre-loaded.
623 * @param[in] memoryFontPathList A list of memory font paths to be pre-loaded.
624 * @param[in] useThread True if the font client should create thread and perform font pre-loading, false otherwise.
625 * @param[in] syncCreation True if thread creation guarantees syncronization with the main thread, false async creation.
628 * The fonts in the fontPathList perform FT_New_Face during pre-loading,
629 * which can provide some performace benefits.
631 * The fonts in the memoryFontPathList read the font file and cache the buffer in memory during pre-load.
632 * This enables the use of FT_New_Memory_Face during runtime and provides a performance boost.
633 * It requires memory equivalent to the size of each font file.
635 DALI_ADAPTOR_API void FontClientFontPreLoad(const FontPathList& fontPathList, const FontPathList& memoryFontPathList, bool useThread, bool syncCreation);
638 * @brief Joins font threads, waiting for their execution to complete.
640 DALI_ADAPTOR_API void FontClientJoinFontThreads();
642 } // namespace TextAbstraction
646 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H