1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
5 * Copyright (c) 2020 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.
84 * @brief Struct used to retrieve the glyph's bitmap.
86 struct DALI_ADAPTOR_API GlyphBufferData
91 * Initializes struct members to their defaults.
100 unsigned char* buffer; ///< The glyph's bitmap buffer data.
101 unsigned int width; ///< The width of the bitmap.
102 unsigned int height; ///< The height of the bitmap.
103 int outlineOffsetX; ///< The additional horizontal offset to be added for the glyph's position for outline.
104 int outlineOffsetY; ///< The additional vertical offset to be added for the glyph's position for outline.
105 Pixel::Format format; ///< The pixel's format of the bitmap.
106 bool isColorEmoji : 1; ///< Whether the glyph is an emoji.
107 bool isColorBitmap : 1; ///< Whether the glyph is a color bitmap.
111 * @brief Used to load an embedded item into the font client.
113 struct EmbeddedItemDescription
115 std::string url; ///< The url path of the image.
116 unsigned int width; ///< The width of the item.
117 unsigned int height; ///< The height of the item.
118 ColorBlendingMode colorblendingMode; ///< Whether the color of the image is multiplied by the color of the text.
123 * @brief Retrieve a handle to the FontClient instance.
125 * @return A handle to the FontClient
127 static FontClient Get();
130 * @brief Create an uninitialized TextAbstraction handle.
137 * This is non-virtual since derived Handle types must not contain data or virtual methods.
142 * @brief This copy constructor is required for (smart) pointer semantics.
144 * @param[in] handle A reference to the copied handle.
146 FontClient(const FontClient& handle);
149 * @brief This assignment operator is required for (smart) pointer semantics.
151 * @param [in] handle A reference to the copied handle.
152 * @return A reference to this.
154 FontClient& operator=(const FontClient& handle);
156 ////////////////////////////////////////
157 // Font management and validation.
158 ////////////////////////////////////////
161 * @brief Clear all caches in FontClient
167 * @brief Set the DPI of the target window.
169 * @note Multiple windows are not currently supported.
170 * @param[in] horizontalDpi The horizontal resolution in DPI.
171 * @param[in] verticalDpi The vertical resolution in DPI.
173 void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
176 * @brief Retrieves the DPI previously set to the target window.
178 * @note Multiple windows are not currently supported.
179 * @param[out] horizontalDpi The horizontal resolution in DPI.
180 * @param[out] verticalDpi The vertical resolution in DPI.
182 void GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi);
185 * @brief Called by Dali to retrieve the default font size for the platform.
187 * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
188 * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
189 * @return The default font size.
191 int GetDefaultFontSize();
194 * @brief Called when the user changes the system defaults.
196 * @post Previously cached system defaults are removed.
198 void ResetSystemDefaults();
201 * @brief Retrieve the list of default fonts supported by the system.
203 * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
205 void GetDefaultFonts(FontList& defaultFonts);
208 * @brief Retrieve the active default font from the system.
210 * @param[out] fontDescription font structure describing the default font.
212 void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
215 * @brief Retrieve the list of fonts supported by the system.
217 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
219 void GetSystemFonts(FontList& systemFonts);
222 * @brief Retrieves the font description of a given font @p id.
224 * @param[in] id The font identifier.
225 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
227 void GetDescription(FontId id, FontDescription& fontDescription);
230 * @brief Retrieves the font point size of a given font @p id.
232 * @param[in] id The font identifier.
234 * @return The point size in 26.6 fractional points.
236 PointSize26Dot6 GetPointSize(FontId id);
239 * @brief Whether the given @p character is supported by the font.
241 * @param[in] fontId The id of the font.
242 * @param[in] character The character.
244 * @return @e true if the character is supported by the font.
246 bool IsCharacterSupportedByFont(FontId fontId, Character character);
249 * @brief Find the default font for displaying a UTF-32 character.
251 * This is useful when localised strings are provided for multiple languages
252 * i.e. when a single default font does not work for all languages.
254 * @param[in] charcode The character for which a font is needed.
255 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
256 * @param[in] preferColor @e true if a color font is preferred.
258 * @return A valid font identifier, or zero if the font does not exist.
260 FontId FindDefaultFont(Character charcode,
261 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
262 bool preferColor = false);
265 * @brief Find a fallback-font for displaying a UTF-32 character.
267 * This is useful when localised strings are provided for multiple languages
268 * i.e. when a single default font does not work for all languages.
270 * @param[in] charcode The character for which a font is needed.
271 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
272 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
273 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
274 * @param[in] preferColor @e true if a color font is preferred.
276 * @return A valid font identifier, or zero if the font does not exist.
278 FontId FindFallbackFont(Character charcode,
279 const FontDescription& preferredFontDescription,
280 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
281 bool preferColor = false);
284 * @brief Retrieve the unique identifier for a font.
286 * @param[in] path The path to a font file.
287 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
288 * @param[in] faceIndex The index of the font face (optional).
290 * @return A valid font identifier, or zero if the font does not exist.
292 FontId GetFontId(const FontPath& path,
293 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
294 FaceIndex faceIndex = 0);
297 * @brief Retrieves a unique font identifier for a given description.
299 * @param[in] preferredFontDescription Description of the preferred font.
300 * The font will be the closest match to @p preferredFontDescription.
301 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
302 * @param[in] faceIndex The index of the font face (optional).
304 * @return A valid font identifier, or zero if no font is found.
306 FontId GetFontId(const FontDescription& preferredFontDescription,
307 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
308 FaceIndex faceIndex = 0);
311 * @brief Retrieves a unique font identifier for a given bitmap font.
313 * @param[in] bitmapFont A bitmap font.
315 * @return A valid font identifier, or zero if no bitmap font is created.
317 FontId GetFontId(const BitmapFont& bitmapFont);
320 * @brief Check to see if a font is scalable.
322 * @param[in] path The path to a font file.
323 * @return true if scalable.
325 bool IsScalable(const FontPath& path);
328 * @brief Check to see if a font is scalable.
330 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
332 * @param[in] fontDescription A font description.
334 * @return true if scalable
336 bool IsScalable(const FontDescription& fontDescription);
339 * @brief Get a list of sizes available for a fixed size font.
341 * @param[in] path The path to a font file.
342 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
344 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
347 * @brief Get a list of sizes available for a fixed size font.
349 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
351 * @param[in] fontDescription A font description.
352 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
354 void GetFixedSizes(const FontDescription& fontDescription,
355 Dali::Vector<PointSize26Dot6>& sizes);
358 * @brief Whether the font has Italic style.
360 * @param[in] fontId The font identifier.
362 * @return true if the font has italic style.
364 bool HasItalicStyle(FontId fontId) const;
366 ////////////////////////////////////////
367 // Font metrics, glyphs and bitmaps.
368 ////////////////////////////////////////
371 * @brief Query the metrics for a font.
373 * @param[in] fontId The identifier of the font for the required glyph.
374 * @param[out] metrics The font metrics.
376 void GetFontMetrics(FontId fontId, FontMetrics& metrics);
379 * @brief Retrieve the glyph index for a UTF-32 character code.
381 * @param[in] fontId The identifier of the font for the required glyph.
382 * @param[in] charcode The UTF-32 character code.
384 * @return The glyph index, or zero if the character code is undefined.
386 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
389 * @brief Retrieve the metrics for a series of glyphs.
391 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
392 * It may contain the advance and an offset set into the bearing from the shaping tool.
393 * 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.
394 * @param[in] size The size of the array.
395 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
396 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
398 * @return @e true if all of the requested metrics were found.
400 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true);
403 * @brief Create a bitmap representation of a glyph.
405 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
407 * @param[in] fontId The identifier of the font.
408 * @param[in] glyphIndex The index of a glyph within the specified font.
409 * @param[in] isItalicRequired Whether the glyph requires italic style.
410 * @param[in] isBoldRequired Whether the glyph requires bold style.
411 * @param[out] data The bitmap data.
412 * @param[in] outlineWidth The width of the glyph outline in pixels.
414 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth);
417 * @brief Create a bitmap representation of a glyph.
419 * @param[in] fontId The identifier of the font.
420 * @param[in] glyphIndex The index of a glyph within the specified font.
421 * @param[in] outlineWidth The width of the glyph outline in pixels.
423 * @return A valid PixelData, or an empty handle if the glyph could not be rendered.
425 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
428 * @brief Create a vector representation of a glyph.
430 * @note This feature requires highp shader support and is not available on all platforms
431 * @param[in] fontId The identifier of the font.
432 * @param[in] glyphIndex The index of a glyph within the specified font.
433 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
434 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
435 * @param[out] nominalWidth The width of the blob.
436 * @param[out] nominalHeight The height of the blob.
438 void CreateVectorBlob(FontId fontId,
439 GlyphIndex glyphIndex,
441 unsigned int& blobLength,
442 unsigned int& nominalWidth,
443 unsigned int& nominalHeight);
446 * @brief Retrieves the ellipsis glyph for a requested point size.
448 * @param[in] requestedPointSize The requested point size.
450 * @return The ellipsis glyph.
452 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
455 * @brief Whether the given glyph @p glyphIndex is a color glyph.
457 * @param[in] fontId The font id.
458 * @param[in] glyphIndex The glyph index.
460 * @return @e true if the glyph is a color one.
462 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
465 * @brief Add custom fonts directory
467 * @param[in] path to the fonts directory
469 * @return true if the fonts can be added.
471 bool AddCustomFontDirectory(const FontPath& path);
474 * @brief Creates and stores an embedded item and it's metrics.
476 * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
477 * 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.
478 * If the url in the @p description is empty it stores the size.
480 * @param[in] description The description of the embedded item.
481 * @param[out] pixelFormat The pixel format of the image.
483 * return The index within the vector of embedded items.
485 GlyphIndex CreateEmbeddedItem(const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
488 * @brief true to enable Atlas-Limitation.
490 * @note Used default configuration.
491 * @param[in] enabled The on/off value to enable/disable Atlas-Limitation.
493 void EnableAtlasLimitation(bool enabled);
496 * @brief Check Atlas-Limitation is enabled or disabled.
498 * @note Used default configuration.
499 * return true if Atlas-Limitation is enabled, otherwise false.
501 bool IsAtlasLimitationEnabled() const;
504 * @brief retrieve the maximum allowed width and height for text-atlas-block.
506 * @note Used default configuration.
507 * return the maximum width and height of text-atlas-block.
509 Size GetMaximumTextAtlasSize() const;
512 * @brief retrieve the default width and height for text-atlas-block.
514 * @note Used default configuration.
515 * return the default width and height of text-atlas-block.
517 Size GetDefaultTextAtlasSize() const;
520 * @brief retrieve the current maximum width and height for text-atlas-block.
522 * @note Used default configuration.
523 * return the current maximum width and height of text-atlas-block.
525 Size GetCurrentMaximumBlockSizeFitInAtlas() const;
528 * @brief set the achieved size (width and height) for text-atlas-block.
529 * If @p currentMaximumBlockSizeFitInAtlas larger than the current maximum text atlas then store, otherwise ignore.
531 * @note Used default configuration.
532 * return true if the current maximum text atlas size is changed, otherwise false.
534 bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
537 * @brief retrieve the number of points to scale-up one unit of point-size.
539 * @note Used default configuration.
540 * return the number of points per one unit of point-size
542 uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
544 public: // Not intended for application developers
546 * @brief This constructor is used by FontClient::Get().
548 * @param[in] fontClient A pointer to the internal fontClient object.
550 explicit DALI_INTERNAL FontClient(Internal::FontClient* fontClient);
554 * @brief This is used to improve application launch performance
556 * @return A pre-initialized FontClient
558 DALI_ADAPTOR_API FontClient FontClientPreInitialize();
560 } // namespace TextAbstraction
564 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H