1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
5 * Copyright (c) 2019 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/public-api/common/dali-vector.h>
23 #include <dali/public-api/images/buffer-image.h>
24 #include <dali/public-api/images/pixel-data.h>
25 #include <dali/public-api/object/base-handle.h>
26 #include <dali/devel-api/text-abstraction/font-list.h>
27 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
28 #include <dali/public-api/dali-adaptor-common.h>
33 namespace TextAbstraction
40 namespace Internal DALI_INTERNAL
46 * @brief FontClient provides access to font information and resources.
48 * <h3>Querying the System Fonts</h3>
50 * A "system font" is described by a "path" to a font file on the native filesystem, along with a "family" and "style".
51 * 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".
53 * <h3>Accessing Fonts</h3>
55 * 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.
56 * For example two different fonts with point sizes 10 & 12 can be created from the "Ubuntu Mono" family:
58 * FontClient fontClient = FontClient::Get();
59 * FontId ubuntuMonoTen = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 10*64 );
60 * FontId ubuntuMonoTwelve = fontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 12*64 );
62 * Glyph metrics and bitmap resources can then be retrieved using the FontId.
64 class DALI_ADAPTOR_API FontClient : public BaseHandle
67 static const PointSize26Dot6 DEFAULT_POINT_SIZE; ///< The default point size.
68 static const float DEFAULT_ITALIC_ANGLE; ///< The default software italic angle in radians.
71 * @brief Struct used to retrieve the glyph's bitmap.
73 struct DALI_ADAPTOR_API GlyphBufferData
78 * Initializes struct members to their defaults.
87 unsigned char* buffer; ///< The glyph's bitmap buffer data.
88 unsigned int width; ///< The width of the bitmap.
89 unsigned int height; ///< The height of the bitmap.
90 Pixel::Format format; ///< The pixel's format of the bitmap.
94 * @brief Used to load an embedded item into the font client.
96 struct EmbeddedItemDescription
98 std::string url; ///< The url path of the image.
99 unsigned int width; ///< The width of the item.
100 unsigned int height; ///< The height of the item.
101 ColorBlendingMode colorblendingMode; ///< Whether the color of the image is multiplied by the color of the text.
107 * @brief Retrieve a handle to the FontClient instance.
109 * @return A handle to the FontClient
111 static FontClient Get();
114 * @brief Create an uninitialized TextAbstraction handle.
121 * This is non-virtual since derived Handle types must not contain data or virtual methods.
126 * @brief This copy constructor is required for (smart) pointer semantics.
128 * @param[in] handle A reference to the copied handle.
130 FontClient( const FontClient& handle );
133 * @brief This assignment operator is required for (smart) pointer semantics.
135 * @param [in] handle A reference to the copied handle.
136 * @return A reference to this.
138 FontClient& operator=( const FontClient& handle );
140 ////////////////////////////////////////
141 // Font management and validation.
142 ////////////////////////////////////////
145 * @brief Set the DPI of the target window.
147 * @note Multiple windows are not currently supported.
148 * @param[in] horizontalDpi The horizontal resolution in DPI.
149 * @param[in] verticalDpi The vertical resolution in DPI.
151 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
154 * @brief Retrieves the DPI previously set to the target window.
156 * @note Multiple windows are not currently supported.
157 * @param[out] horizontalDpi The horizontal resolution in DPI.
158 * @param[out] verticalDpi The vertical resolution in DPI.
160 void GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi );
163 * @brief Called by Dali to retrieve the default font size for the platform.
165 * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
166 * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
167 * @return The default font size.
169 int GetDefaultFontSize();
172 * @brief Called when the user changes the system defaults.
174 * @post Previously cached system defaults are removed.
176 void ResetSystemDefaults();
179 * @brief Retrieve the list of default fonts supported by the system.
181 * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
183 void GetDefaultFonts( FontList& defaultFonts );
186 * @brief Retrieve the active default font from the system.
188 * @param[out] fontDescription font structure describing the default font.
190 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
193 * @brief Retrieve the list of fonts supported by the system.
195 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
197 void GetSystemFonts( FontList& systemFonts );
200 * @brief Retrieves the font description of a given font @p id.
202 * @param[in] id The font identifier.
203 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
205 void GetDescription( FontId id, FontDescription& fontDescription );
208 * @brief Retrieves the font point size of a given font @p id.
210 * @param[in] id The font identifier.
212 * @return The point size in 26.6 fractional points.
214 PointSize26Dot6 GetPointSize( FontId id );
217 * @brief Whether the given @p character is supported by the font.
219 * @param[in] fontId The id of the font.
220 * @param[in] character The character.
222 * @return @e true if the character is supported by the font.
224 bool IsCharacterSupportedByFont( FontId fontId, Character character );
227 * @brief Find the default font for displaying a UTF-32 character.
229 * This is useful when localised strings are provided for multiple languages
230 * i.e. when a single default font does not work for all languages.
232 * @param[in] charcode The character for which a font is needed.
233 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
234 * @param[in] preferColor @e true if a color font is preferred.
236 * @return A valid font identifier, or zero if the font does not exist.
238 FontId FindDefaultFont( Character charcode,
239 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
240 bool preferColor = false );
243 * @brief Find a fallback-font for displaying a UTF-32 character.
245 * This is useful when localised strings are provided for multiple languages
246 * i.e. when a single default font does not work for all languages.
248 * @param[in] charcode The character for which a font is needed.
249 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
250 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
251 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
252 * @param[in] preferColor @e true if a color font is preferred.
254 * @return A valid font identifier, or zero if the font does not exist.
256 FontId FindFallbackFont( Character charcode,
257 const FontDescription& preferredFontDescription,
258 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
259 bool preferColor = false );
262 * @brief Retrieve the unique identifier for a font.
264 * @param[in] path The path to a font file.
265 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
266 * @param[in] faceIndex The index of the font face (optional).
268 * @return A valid font identifier, or zero if the font does not exist.
270 FontId GetFontId( const FontPath& path,
271 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
272 FaceIndex faceIndex = 0 );
275 * @brief Retrieves a unique font identifier for a given description.
277 * @param[in] preferredFontDescription Description of the preferred font.
278 * The font will be the closest match to @p preferredFontDescription.
279 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
280 * @param[in] faceIndex The index of the font face (optional).
282 * @return A valid font identifier, or zero if no font is found.
284 FontId GetFontId( const FontDescription& preferredFontDescription,
285 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
286 FaceIndex faceIndex = 0 );
289 * @brief Retrieves a unique font identifier for a given bitmap font.
291 * @param[in] bitmapFont A bitmap font.
293 * @return A valid font identifier, or zero if no bitmap font is created.
295 FontId GetFontId( const BitmapFont& bitmapFont );
298 * @brief Check to see if a font is scalable.
300 * @param[in] path The path to a font file.
301 * @return true if scalable.
303 bool IsScalable( const FontPath& path );
306 * @brief Check to see if a font is scalable.
308 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
310 * @param[in] fontDescription A font description.
312 * @return true if scalable
314 bool IsScalable( const FontDescription& fontDescription );
317 * @brief Get a list of sizes available for a fixed size font.
319 * @param[in] path The path to a font file.
320 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
322 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
325 * @brief Get a list of sizes available for a fixed size font.
327 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
329 * @param[in] fontDescription A font description.
330 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
332 void GetFixedSizes( const FontDescription& fontDescription,
333 Dali::Vector< PointSize26Dot6 >& sizes );
336 * @brief Whether the font has Italic style.
338 * @param[in] fontId The font identifier.
340 * @return true if the font has italic style.
342 bool HasItalicStyle( FontId fontId ) const;
344 ////////////////////////////////////////
345 // Font metrics, glyphs and bitmaps.
346 ////////////////////////////////////////
349 * @brief Query the metrics for a font.
351 * @param[in] fontId The identifier of the font for the required glyph.
352 * @param[out] metrics The font metrics.
354 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
357 * @brief Retrieve the glyph index for a UTF-32 character code.
359 * @param[in] fontId The identifier of the font for the required glyph.
360 * @param[in] charcode The UTF-32 character code.
362 * @return The glyph index, or zero if the character code is undefined.
364 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
367 * @brief Retrieve the metrics for a series of glyphs.
369 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
370 * It may contain the advance and an offset set into the bearing from the shaping tool.
371 * 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.
372 * @param[in] size The size of the array.
373 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
374 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
376 * @return @e true if all of the requested metrics were found.
378 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true );
381 * @brief Create a bitmap representation of a glyph.
383 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
385 * @param[in] fontId The identifier of the font.
386 * @param[in] glyphIndex The index of a glyph within the specified font.
387 * @param[in] isItalicRequired Whether the glyph requires italic style.
388 * @param[in] isBoldRequired Whether the glyph requires bold style.
389 * @param[out] data The bitmap data.
390 * @param[in] outlineWidth The width of the glyph outline in pixels.
392 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth );
395 * @brief Create a bitmap representation of a glyph.
397 * @param[in] fontId The identifier of the font.
398 * @param[in] glyphIndex The index of a glyph within the specified font.
399 * @param[in] outlineWidth The width of the glyph outline in pixels.
401 * @return A valid BufferImage, or an empty handle if the glyph could not be rendered.
403 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
406 * @brief Create a vector representation of a glyph.
408 * @note This feature requires highp shader support and is not available on all platforms
409 * @param[in] fontId The identifier of the font.
410 * @param[in] glyphIndex The index of a glyph within the specified font.
411 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
412 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
413 * @param[out] nominalWidth The width of the blob.
414 * @param[out] nominalHeight The height of the blob.
416 void CreateVectorBlob( FontId fontId,
417 GlyphIndex glyphIndex,
419 unsigned int& blobLength,
420 unsigned int& nominalWidth,
421 unsigned int& nominalHeight );
424 * @brief Retrieves the ellipsis glyph for a requested point size.
426 * @param[in] requestedPointSize The requested point size.
428 * @return The ellipsis glyph.
430 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
433 * @brief Whether the given glyph @p glyphIndex is a color glyph.
435 * @param[in] fontId The font id.
436 * @param[in] glyphIndex The glyph index.
438 * @return @e true if the glyph is a color one.
440 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
443 * @brief Add custom fonts directory
445 * @param[in] path to the fonts directory
447 * @return true if the fonts can be added.
449 bool AddCustomFontDirectory( const FontPath& path );
452 * @brief Creates and stores an embedded item and it's metrics.
454 * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
455 * 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.
456 * If the url in the @p description is empty it stores the size.
458 * @param[in] description The description of the embedded item.
459 * @param[out] pixelFormat The pixel format of the image.
461 * return The index within the vector of embedded items.
463 GlyphIndex CreateEmbeddedItem( const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
466 public: // Not intended for application developers
468 * @brief This constructor is used by FontClient::Get().
470 * @param[in] fontClient A pointer to the internal fontClient object.
472 explicit DALI_INTERNAL FontClient( Internal::FontClient* fontClient );
475 } // namespace TextAbstraction
479 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H