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 Retrieve the active default font from the system.
289 * @param[out] fontDescription font structure describing the default font.
291 void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
294 * @brief Retrieve the list of fonts supported by the system.
296 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
298 void GetSystemFonts(FontList& systemFonts);
301 * @brief Retrieves the font description of a given font @p fontId.
303 * @param[in] fontId The font identifier.
304 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
306 void GetDescription(FontId fontId, FontDescription& fontDescription);
309 * @brief Retrieves the font point size of a given font @p fontId.
311 * @param[in] fontId The font identifier.
313 * @return The point size in 26.6 fractional points.
315 PointSize26Dot6 GetPointSize(FontId fontId);
318 * @brief Whether the given @p character is supported by the font.
320 * @param[in] fontId The id of the font.
321 * @param[in] character The character.
323 * @return @e true if the character is supported by the font.
325 bool IsCharacterSupportedByFont(FontId fontId, Character character);
328 * @brief Find the default font for displaying a UTF-32 character.
330 * This is useful when localised strings are provided for multiple languages
331 * i.e. when a single default font does not work for all languages.
333 * @param[in] charcode The character for which a font is needed.
334 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
335 * @param[in] preferColor @e true if a color font is preferred.
337 * @return A valid font identifier, or zero if the font does not exist.
339 FontId FindDefaultFont(Character charcode,
340 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
341 bool preferColor = false);
344 * @brief Find a fallback-font for displaying a UTF-32 character.
346 * This is useful when localised strings are provided for multiple languages
347 * i.e. when a single default font does not work for all languages.
349 * @param[in] charcode The character for which a font is needed.
350 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
351 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
352 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
353 * @param[in] preferColor @e true if a color font is preferred.
355 * @return A valid font identifier, or zero if the font does not exist.
357 FontId FindFallbackFont(Character charcode,
358 const FontDescription& preferredFontDescription,
359 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
360 bool preferColor = false);
363 * @brief Retrieve the unique identifier for a font.
365 * @param[in] path The path to a font file.
366 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
367 * @param[in] faceIndex The index of the font face (optional).
369 * @return A valid font identifier, or zero if the font does not exist.
371 FontId GetFontId(const FontPath& path,
372 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
373 FaceIndex faceIndex = 0);
376 * @brief Retrieves a unique font identifier for a given description.
378 * @param[in] preferredFontDescription Description of the preferred font.
379 * The font will be the closest match to @p preferredFontDescription.
380 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
381 * @param[in] faceIndex The index of the font face (optional).
383 * @return A valid font identifier, or zero if no font is found.
385 FontId GetFontId(const FontDescription& preferredFontDescription,
386 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
387 FaceIndex faceIndex = 0);
390 * @brief Retrieves a unique font identifier for a given bitmap font.
391 * If the font is not present, it will cache the given font, and give it a new font id.
393 * @param[in] bitmapFont A bitmap font.
395 * @return A valid font identifier.
397 FontId GetFontId(const BitmapFont& bitmapFont);
400 * @brief Check to see if a font is scalable.
402 * @param[in] path The path to a font file.
403 * @return true if scalable.
405 bool IsScalable(const FontPath& path);
408 * @brief Check to see if a font is scalable.
410 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
412 * @param[in] fontDescription A font description.
414 * @return true if scalable
416 bool IsScalable(const FontDescription& fontDescription);
419 * @brief Get a list of sizes available for a fixed size font.
421 * @param[in] path The path to a font file.
422 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
424 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
427 * @brief Get a list of sizes available for a fixed size font.
429 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
431 * @param[in] fontDescription A font description.
432 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
434 void GetFixedSizes(const FontDescription& fontDescription,
435 Dali::Vector<PointSize26Dot6>& sizes);
438 * @brief Whether the font has Italic style.
440 * @param[in] fontId The font identifier.
442 * @return true if the font has italic style.
444 bool HasItalicStyle(FontId fontId) const;
446 ////////////////////////////////////////
447 // Font metrics, glyphs and bitmaps.
448 ////////////////////////////////////////
451 * @brief Query the metrics for a font.
453 * @param[in] fontId The identifier of the font for the required glyph.
454 * @param[out] metrics The font metrics.
456 void GetFontMetrics(FontId fontId, FontMetrics& metrics);
459 * @brief Retrieve the glyph index for a UTF-32 character code.
461 * @param[in] fontId The identifier of the font for the required glyph.
462 * @param[in] charcode The UTF-32 character code.
464 * @return The glyph index, or zero if the character code is undefined.
466 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
469 * @brief Return the glyph index of a given character code as modified by the variation selector.
471 * @param[in] fontId The identifier of the font for the required glyph.
472 * @param[in] charcode The UTF-32 character code.
473 * @param[in] variantSelector The UTF-32 character code point of the variation selector.
475 * @return The glyph index, or zero if the character code is undefined.
477 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
480 * @brief Retrieve the metrics for a series of glyphs.
482 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
483 * It may contain the advance and an offset set into the bearing from the shaping tool.
484 * 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.
485 * @param[in] size The size of the array.
486 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
487 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
489 * @return @e true if all of the requested metrics were found.
491 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true);
494 * @brief Create a bitmap representation of a glyph.
496 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
498 * @param[in] fontId The identifier of the font.
499 * @param[in] glyphIndex The index of a glyph within the specified font.
500 * @param[in] isItalicRequired Whether the glyph requires italic style.
501 * @param[in] isBoldRequired Whether the glyph requires bold style.
502 * @param[out] data The bitmap data.
503 * @param[in] outlineWidth The width of the glyph outline in pixels.
505 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth);
508 * @brief Create a bitmap representation of a glyph.
510 * @param[in] fontId The identifier of the font.
511 * @param[in] glyphIndex The index of a glyph within the specified font.
512 * @param[in] outlineWidth The width of the glyph outline in pixels.
514 * @return A valid PixelData, or an empty handle if the glyph could not be rendered.
516 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
519 * @brief Create a vector representation of a glyph.
521 * @note This feature requires highp shader support and is not available on all platforms
522 * @param[in] fontId The identifier of the font.
523 * @param[in] glyphIndex The index of a glyph within the specified font.
524 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
525 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
526 * @param[out] nominalWidth The width of the blob.
527 * @param[out] nominalHeight The height of the blob.
529 void CreateVectorBlob(FontId fontId,
530 GlyphIndex glyphIndex,
532 unsigned int& blobLength,
533 unsigned int& nominalWidth,
534 unsigned int& nominalHeight);
537 * @brief Retrieves the ellipsis glyph for a requested point size.
539 * @param[in] requestedPointSize The requested point size.
541 * @return The ellipsis glyph.
543 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
546 * @brief Whether the given glyph @p glyphIndex is a color glyph.
548 * @param[in] fontId The font id.
549 * @param[in] glyphIndex The glyph index.
551 * @return @e true if the glyph is a color one.
553 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
556 * @brief Add custom fonts directory
558 * @param[in] path to the fonts directory
560 * @return true if the fonts can be added.
562 bool AddCustomFontDirectory(const FontPath& path);
565 * @brief Creates and stores an embedded item and it's metrics.
567 * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
568 * 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.
569 * If the url in the @p description is empty it stores the size.
571 * @param[in] description The description of the embedded item.
572 * @param[out] pixelFormat The pixel format of the image.
574 * return The index within the vector of embedded items.
576 GlyphIndex CreateEmbeddedItem(const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
579 * @brief true to enable Atlas-Limitation.
581 * @note Used default configuration.
582 * @param[in] enabled The on/off value to enable/disable Atlas-Limitation.
584 void EnableAtlasLimitation(bool enabled);
587 * @brief Check Atlas-Limitation is enabled or disabled.
589 * @note Used default configuration.
590 * return true if Atlas-Limitation is enabled, otherwise false.
592 bool IsAtlasLimitationEnabled() const;
595 * @brief retrieve the maximum allowed width and height for text-atlas-block.
597 * @note Used default configuration.
598 * return the maximum width and height of text-atlas-block.
600 Size GetMaximumTextAtlasSize() const;
603 * @brief retrieve the default width and height for text-atlas-block.
605 * @note Used default configuration.
606 * return the default width and height of text-atlas-block.
608 Size GetDefaultTextAtlasSize() const;
611 * @brief retrieve the current maximum width and height for text-atlas-block.
613 * @note Used default configuration.
614 * return the current maximum width and height of text-atlas-block.
616 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
619 * @brief set the achieved size (width and height) for text-atlas-block.
620 * If @p currentMaximumBlockSizeFitInAtlas larger than the current maximum text atlas then store, otherwise ignore.
622 * @note Used default configuration.
623 * return true if the current maximum text atlas size is changed, otherwise false.
625 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
628 * @brief retrieve the number of points to scale-up one unit of point-size.
630 * @note Used default configuration.
631 * return the number of points per one unit of point-size
633 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
635 public: // Not intended for application developers
637 * @brief This constructor is used by FontClient::Get().
639 * @param[in] fontClient A pointer to the internal fontClient object.
641 explicit DALI_INTERNAL FontClient(Internal::FontClient* fontClient);
645 * @brief This is used to improve application launch performance
647 * @return A pre-initialized FontClient
649 DALI_ADAPTOR_API FontClient FontClientPreInitialize();
652 * @brief This is used to pre-cache fonts in order to improve the runtime performance of the application.
654 * @param[in] fallbackFamilyList A list of fallback font families to be pre-cached.
655 * @param[in] extraFamilyList A list of additional font families to be pre-cached.
656 * @param[in] localeFamily A locale font family to be pre-cached.
657 * @param[in] useThread True if the font client should create thread and perform pre-caching, false otherwise.
659 DALI_ADAPTOR_API void FontClientPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily, bool useThread);
661 } // namespace TextAbstraction
665 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H