1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
5 * Copyright (c) 2022 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/text-abstraction-definitions.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/dali-adaptor-common.h>
26 #include <dali/public-api/images/pixel-data.h>
27 #include <dali/public-api/object/base-handle.h>
31 namespace TextAbstraction
37 namespace Internal DALI_INTERNAL
43 * @brief FontClient provides access to font information and resources.
45 * <h3>Querying the System Fonts</h3>
47 * A "system font" is described by a "path" to a font file on the native filesystem, along with a "family" and "style".
48 * 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".
50 * <h3>Accessing Fonts</h3>
52 * 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.
53 * For example two different fonts with point sizes 10 & 12 can be created from the "Ubuntu Mono" family:
55 * FontClient fontClient = FontClient::Get();
56 * FontId ubuntuMonoTen = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 10*64 );
57 * FontId ubuntuMonoTwelve = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 12*64 );
59 * Glyph metrics and bitmap resources can then be retrieved using the FontId.
61 class DALI_ADAPTOR_API FontClient : public BaseHandle
64 static const PointSize26Dot6 DEFAULT_POINT_SIZE; ///< The default point size.
65 static const float DEFAULT_ITALIC_ANGLE; ///< The default software italic angle in radians.
67 static const bool DEFAULT_ATLAS_LIMITATION_ENABLED; ///< The default behavior of whether atlas limitation is enabled in dali.
68 static const uint32_t DEFAULT_TEXT_ATLAS_WIDTH; ///< The default width of text-atlas-block.
69 static const uint32_t DEFAULT_TEXT_ATLAS_HEIGHT; ///< The default height of text-atlas-block.
70 static const Size DEFAULT_TEXT_ATLAS_SIZE; ///< The default size(width, height) of text-atlas-block.
72 static const uint32_t MAX_TEXT_ATLAS_WIDTH; ///< The maximum width of text-atlas-block.
73 static const uint32_t MAX_TEXT_ATLAS_HEIGHT; ///< The maximum height of text-atlas-block.
74 static const Size MAX_TEXT_ATLAS_SIZE; ///< The maximum height of text-atlas-block.
76 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).
77 static const Size MAX_SIZE_FIT_IN_ATLAS; ///< The maximum block's size fit into text-atlas-block.
79 static const uint32_t NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE; ///< Factor multiply point-size in toolkit.
82 * @brief Struct used to retrieve the glyph's bitmap.
84 struct DALI_ADAPTOR_API GlyphBufferData
89 * Initializes struct members to their defaults.
99 * @brief Move constructor.
101 * @param[in] rhs moved data.
103 GlyphBufferData(GlyphBufferData&& rhs) noexcept;
106 * @brief Move assign operator.
108 * @param[in] rhs moved data.
109 * @return A reference to this.
111 GlyphBufferData& operator=(GlyphBufferData&& rhs) noexcept;
113 // Compression method of buffer. Each buffer compressed line by line
114 enum class CompressionType
116 NO_COMPRESSION = 0, // No compression
117 BPP_4 = 1, // Compress as 4 bit. Color become value * 17 (0x00, 0x11, 0x22, ... 0xee, 0xff).
118 // Only works for Pixel::L8 format
119 RLE_4 = 2, // Compress as 4 bit, and Run-Length-Encode. For more high compress rate, we store difference between previous scanline.
120 // Only works for Pixel::L8 format
124 * @brief Helper static function to compress raw buffer from inBuffer to outBufferData.buffer.
125 * outBufferData will have it's own buffer.
127 * @pre outBufferData must not have it's own buffer.
128 * @param[in] inBuffer The input raw data.
129 * @param[in, out] outBufferData The output glyph buffer data.
130 * @return Size of compressed out buffer, Or 0 if compress failed.
132 static size_t Compress(const uint8_t* const inBuffer, GlyphBufferData& outBufferData);
135 * @brief Helper static function to decompress raw buffer from inBuffer to outBufferPtr.
136 * If outBuffer is nullptr, Do nothing.
138 * @pre outBuffer memory should be allocated.
139 * @param[in] inBufferData The input glyph buffer data.
140 * @param[in, out] outBuffer The output pointer of raw buffer data.
142 static void Decompress(const GlyphBufferData& inBufferData, uint8_t* outBuffer);
145 * @brief Special Helper static function to decompress raw buffer from inBuffer to outBuffer one scanline.
146 * After decompress one scanline successed, offset will be changed.
148 * @pre outBuffer memory should be allocated.
149 * @pre if inBufferData's compression type is RLE4, outBuffer memory should store the previous scanline data.
150 * @param[in] inBufferData The input glyph buffer data.
151 * @param[in, out] outBuffer The output pointer of raw buffer data.
152 * @param[in, out] offset The offset of input. It will be changed as next scanline's offset.
154 static void DecompressScanline(const GlyphBufferData& inBufferData, uint8_t* outBuffer, uint32_t& offset);
157 // Delete copy operation.
158 GlyphBufferData(const GlyphBufferData& rhs) = delete;
159 GlyphBufferData& operator=(const GlyphBufferData& rhs) = delete;
162 uint8_t* buffer; ///< The glyph's bitmap buffer data.
163 uint32_t width; ///< The width of the bitmap.
164 uint32_t height; ///< The height of the bitmap.
165 int outlineOffsetX; ///< The additional horizontal offset to be added for the glyph's position for outline.
166 int outlineOffsetY; ///< The additional vertical offset to be added for the glyph's position for outline.
167 Pixel::Format format; ///< The pixel's format of the bitmap.
168 CompressionType compressionType; ///< The type of buffer compression.
169 bool isColorEmoji : 1; ///< Whether the glyph is an emoji.
170 bool isColorBitmap : 1; ///< Whether the glyph is a color bitmap.
171 bool isBufferOwned : 1; ///< Whether the glyph's bitmap buffer data owned by this class or not. Becareful when you use non-owned buffer data.
175 * @brief Used to load an embedded item into the font client.
177 struct EmbeddedItemDescription
179 std::string url; ///< The url path of the image.
180 unsigned int width; ///< The width of the item.
181 unsigned int height; ///< The height of the item.
182 ColorBlendingMode colorblendingMode; ///< Whether the color of the image is multiplied by the color of the text.
187 * @brief Retrieve a handle to the FontClient instance.
189 * @return A handle to the FontClient
191 static FontClient Get();
194 * @brief Create an uninitialized TextAbstraction handle.
201 * This is non-virtual since derived Handle types must not contain data or virtual methods.
206 * @brief This copy constructor is required for (smart) pointer semantics.
208 * @param[in] handle A reference to the copied handle.
210 FontClient(const FontClient& handle);
213 * @brief This assignment operator is required for (smart) pointer semantics.
215 * @param [in] handle A reference to the copied handle.
216 * @return A reference to this.
218 FontClient& operator=(const FontClient& handle);
221 * @brief This move constructor is required for (smart) pointer semantics.
223 * @param[in] handle A reference to the moved handle.
225 FontClient(FontClient&& handle);
228 * @brief This move assignment operator is required for (smart) pointer semantics.
230 * @param [in] handle A reference to the moved handle.
231 * @return A reference to this.
233 FontClient& operator=(FontClient&& handle);
235 ////////////////////////////////////////
236 // Font management and validation.
237 ////////////////////////////////////////
240 * @brief Clear all caches in FontClient
246 * @brief Set the DPI of the target window.
248 * @note Multiple windows are not currently supported.
249 * @param[in] horizontalDpi The horizontal resolution in DPI.
250 * @param[in] verticalDpi The vertical resolution in DPI.
252 void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
255 * @brief Retrieves the DPI previously set to the target window.
257 * @note Multiple windows are not currently supported.
258 * @param[out] horizontalDpi The horizontal resolution in DPI.
259 * @param[out] verticalDpi The vertical resolution in DPI.
261 void GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi);
264 * @brief Called by Dali to retrieve the default font size for the platform.
266 * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
267 * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
268 * @return The default font size.
270 int GetDefaultFontSize();
273 * @brief Called when the user changes the system defaults.
275 * @post Previously cached system defaults are removed.
277 void ResetSystemDefaults();
280 * @brief Retrieve the list of default fonts supported by the system.
282 * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
284 void GetDefaultFonts(FontList& defaultFonts);
287 * @brief Initializes and caches default font from the system.
289 void InitDefaultFontDescription();
292 * @brief Retrieve the active default font from the system.
294 * @param[out] fontDescription font structure describing the default font.
296 void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
299 * @brief Retrieve the list of fonts supported by the system.
301 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
303 void GetSystemFonts(FontList& systemFonts);
306 * @brief Retrieves the font description of a given font @p fontId.
308 * @param[in] fontId The font identifier.
309 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
311 void GetDescription(FontId fontId, FontDescription& fontDescription);
314 * @brief Retrieves the font point size of a given font @p fontId.
316 * @param[in] fontId The font identifier.
318 * @return The point size in 26.6 fractional points.
320 PointSize26Dot6 GetPointSize(FontId fontId);
323 * @brief Whether the given @p character is supported by the font.
325 * @param[in] fontId The id of the font.
326 * @param[in] character The character.
328 * @return @e true if the character is supported by the font.
330 bool IsCharacterSupportedByFont(FontId fontId, Character character);
333 * @brief Find the default font for displaying a UTF-32 character.
335 * This is useful when localised strings are provided for multiple languages
336 * i.e. when a single default font does not work for all languages.
338 * @param[in] charcode The character for which a font is needed.
339 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
340 * @param[in] preferColor @e true if a color font is preferred.
342 * @return A valid font identifier, or zero if the font does not exist.
344 FontId FindDefaultFont(Character charcode,
345 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
346 bool preferColor = false);
349 * @brief Find a fallback-font for displaying a UTF-32 character.
351 * This is useful when localised strings are provided for multiple languages
352 * i.e. when a single default font does not work for all languages.
354 * @param[in] charcode The character for which a font is needed.
355 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
356 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
357 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
358 * @param[in] preferColor @e true if a color font is preferred.
360 * @return A valid font identifier, or zero if the font does not exist.
362 FontId FindFallbackFont(Character charcode,
363 const FontDescription& preferredFontDescription,
364 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
365 bool preferColor = false);
368 * @brief Retrieve the unique identifier for a font.
370 * @param[in] path The path to a font file.
371 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
372 * @param[in] faceIndex The index of the font face (optional).
374 * @return A valid font identifier, or zero if the font does not exist.
376 FontId GetFontId(const FontPath& path,
377 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
378 FaceIndex faceIndex = 0);
381 * @brief Retrieves a unique font identifier for a given description.
383 * @param[in] preferredFontDescription Description of the preferred font.
384 * The font will be the closest match to @p preferredFontDescription.
385 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
386 * @param[in] faceIndex The index of the font face (optional).
388 * @return A valid font identifier, or zero if no font is found.
390 FontId GetFontId(const FontDescription& preferredFontDescription,
391 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
392 FaceIndex faceIndex = 0);
395 * @brief Retrieves a unique font identifier for a given bitmap font.
396 * If the font is not present, it will cache the given font, and give it a new font id.
398 * @param[in] bitmapFont A bitmap font.
400 * @return A valid font identifier.
402 FontId GetFontId(const BitmapFont& bitmapFont);
405 * @brief Check to see if a font is scalable.
407 * @param[in] path The path to a font file.
408 * @return true if scalable.
410 bool IsScalable(const FontPath& path);
413 * @brief Check to see if a font is scalable.
415 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
417 * @param[in] fontDescription A font description.
419 * @return true if scalable
421 bool IsScalable(const FontDescription& fontDescription);
424 * @brief Get a list of sizes available for a fixed size font.
426 * @param[in] path The path to a font file.
427 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
429 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
432 * @brief Get a list of sizes available for a fixed size font.
434 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
436 * @param[in] fontDescription A font description.
437 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
439 void GetFixedSizes(const FontDescription& fontDescription,
440 Dali::Vector<PointSize26Dot6>& sizes);
443 * @brief Whether the font has Italic style.
445 * @param[in] fontId The font identifier.
447 * @return true if the font has italic style.
449 bool HasItalicStyle(FontId fontId) const;
451 ////////////////////////////////////////
452 // Font metrics, glyphs and bitmaps.
453 ////////////////////////////////////////
456 * @brief Query the metrics for a font.
458 * @param[in] fontId The identifier of the font for the required glyph.
459 * @param[out] metrics The font metrics.
461 void GetFontMetrics(FontId fontId, FontMetrics& metrics);
464 * @brief Retrieve the glyph index for a UTF-32 character code.
466 * @param[in] fontId The identifier of the font for the required glyph.
467 * @param[in] charcode The UTF-32 character code.
469 * @return The glyph index, or zero if the character code is undefined.
471 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
474 * @brief Return the glyph index of a given character code as modified by the variation selector.
476 * @param[in] fontId The identifier of the font for the required glyph.
477 * @param[in] charcode The UTF-32 character code.
478 * @param[in] variantSelector The UTF-32 character code point of the variation selector.
480 * @return The glyph index, or zero if the character code is undefined.
482 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
485 * @brief Retrieve the metrics for a series of glyphs.
487 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
488 * It may contain the advance and an offset set into the bearing from the shaping tool.
489 * 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.
490 * @param[in] size The size of the array.
491 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
492 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
494 * @return @e true if all of the requested metrics were found.
496 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true);
499 * @brief Create a bitmap representation of a glyph.
501 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
503 * @param[in] fontId The identifier of the font.
504 * @param[in] glyphIndex The index of a glyph within the specified font.
505 * @param[in] isItalicRequired Whether the glyph requires italic style.
506 * @param[in] isBoldRequired Whether the glyph requires bold style.
507 * @param[out] data The bitmap data.
508 * @param[in] outlineWidth The width of the glyph outline in pixels.
510 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth);
513 * @brief Create a bitmap representation of a glyph.
515 * @param[in] fontId The identifier of the font.
516 * @param[in] glyphIndex The index of a glyph within the specified font.
517 * @param[in] outlineWidth The width of the glyph outline in pixels.
519 * @return A valid PixelData, or an empty handle if the glyph could not be rendered.
521 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
524 * @brief Create a vector representation of a glyph.
526 * @note This feature requires highp shader support and is not available on all platforms
527 * @param[in] fontId The identifier of the font.
528 * @param[in] glyphIndex The index of a glyph within the specified font.
529 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
530 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
531 * @param[out] nominalWidth The width of the blob.
532 * @param[out] nominalHeight The height of the blob.
534 void CreateVectorBlob(FontId fontId,
535 GlyphIndex glyphIndex,
537 unsigned int& blobLength,
538 unsigned int& nominalWidth,
539 unsigned int& nominalHeight);
542 * @brief Retrieves the ellipsis glyph for a requested point size.
544 * @param[in] requestedPointSize The requested point size.
546 * @return The ellipsis glyph.
548 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
551 * @brief Whether the given glyph @p glyphIndex is a color glyph.
553 * @param[in] fontId The font id.
554 * @param[in] glyphIndex The glyph index.
556 * @return @e true if the glyph is a color one.
558 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
561 * @brief Add custom fonts directory
563 * @param[in] path to the fonts directory
565 * @return true if the fonts can be added.
567 bool AddCustomFontDirectory(const FontPath& path);
570 * @brief Creates and stores an embedded item and it's metrics.
572 * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
573 * 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.
574 * If the url in the @p description is empty it stores the size.
576 * @param[in] description The description of the embedded item.
577 * @param[out] pixelFormat The pixel format of the image.
579 * return The index within the vector of embedded items.
581 GlyphIndex CreateEmbeddedItem(const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
584 * @brief true to enable Atlas-Limitation.
586 * @note Used default configuration.
587 * @param[in] enabled The on/off value to enable/disable Atlas-Limitation.
589 void EnableAtlasLimitation(bool enabled);
592 * @brief Check Atlas-Limitation is enabled or disabled.
594 * @note Used default configuration.
595 * return true if Atlas-Limitation is enabled, otherwise false.
597 bool IsAtlasLimitationEnabled() const;
600 * @brief retrieve the maximum allowed width and height for text-atlas-block.
602 * @note Used default configuration.
603 * return the maximum width and height of text-atlas-block.
605 Size GetMaximumTextAtlasSize() const;
608 * @brief retrieve the default width and height for text-atlas-block.
610 * @note Used default configuration.
611 * return the default width and height of text-atlas-block.
613 Size GetDefaultTextAtlasSize() const;
616 * @brief retrieve the current maximum width and height for text-atlas-block.
618 * @note Used default configuration.
619 * return the current maximum width and height of text-atlas-block.
621 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
624 * @brief set the achieved size (width and height) for text-atlas-block.
625 * If @p currentMaximumBlockSizeFitInAtlas larger than the current maximum text atlas then store, otherwise ignore.
627 * @note Used default configuration.
628 * return true if the current maximum text atlas size is changed, otherwise false.
630 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
633 * @brief retrieve the number of points to scale-up one unit of point-size.
635 * @note Used default configuration.
636 * return the number of points per one unit of point-size
638 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
640 public: // Not intended for application developers
642 * @brief This constructor is used by FontClient::Get().
644 * @param[in] fontClient A pointer to the internal fontClient object.
646 explicit DALI_INTERNAL FontClient(Internal::FontClient* fontClient);
650 * @brief This is used to improve application launch performance
652 * @return A pre-initialized FontClient
654 DALI_ADAPTOR_API FontClient FontClientPreInitialize();
657 * @brief This is used to pre-cache FontConfig in order to improve the runtime performance of the application.
659 * @param[in] fallbackFamilyList A list of fallback font families to be pre-cached.
660 * @param[in] extraFamilyList A list of additional font families to be pre-cached.
661 * @param[in] localeFamily A locale font family to be pre-cached.
662 * @param[in] useThread True if the font client should create thread and perform pre-caching, false otherwise.
664 DALI_ADAPTOR_API void FontClientPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily, bool useThread);
667 * @brief This is used to pre-load FreeType font face in order to improve the runtime performance of the application.
669 * @param[in] fontPathList A list of font paths to be pre-loaded.
670 * @param[in] memoryFontPathList A list of memory font paths to be pre-loaded.
671 * @param[in] useThread True if the font client should create thread and perform font pre-loading, false otherwise.
674 * The fonts in the fontPathList perform FT_New_Face during pre-loading,
675 * which can provide some performace benefits.
677 * The fonts in the memoryFontPathList read the font file and cache the buffer in memory during pre-load.
678 * This enables the use of FT_New_Memory_Face during runtime and provides a performance boost.
679 * It requires memory equivalent to the size of each font file.
681 DALI_ADAPTOR_API void FontClientFontPreLoad(const FontPathList& fontPathList, const FontPathList& memoryFontPathList, bool useThread);
683 } // namespace TextAbstraction
687 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H