DALi Version 2.2.26
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / text-abstraction / font-client.h
1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32 namespace TextAbstraction
33 {
34 struct FontMetrics;
35 struct GlyphInfo;
36 struct BitmapFont;
37
38 namespace Internal DALI_INTERNAL
39 {
40 class FontClient;
41 }
42
43 /**
44  * @brief FontClient provides access to font information and resources.
45  *
46  * <h3>Querying the System Fonts</h3>
47  *
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".
50  *
51  * <h3>Accessing Fonts</h3>
52  *
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:
55  * @code
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 );
59  * @endcode
60  * Glyph metrics and bitmap resources can then be retrieved using the FontId.
61  */
62 class DALI_ADAPTOR_API FontClient : public BaseHandle
63 {
64 public:
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.
67
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.
72
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.
76
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.
79
80   static const uint32_t NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE; ///< Factor multiply point-size in toolkit.
81
82   /**
83    * @brief Used to load an embedded item into the font client.
84    */
85   struct EmbeddedItemDescription
86   {
87     std::string       url;               ///< The url path of the image.
88     unsigned int      width;             ///< The width of the item.
89     unsigned int      height;            ///< The height of the item.
90     ColorBlendingMode colorblendingMode; ///< Whether the color of the image is multiplied by the color of the text.
91   };
92
93 public:
94   /**
95    * @brief Retrieve a handle to the FontClient instance.
96    *
97    * @return A handle to the FontClient
98    */
99   static FontClient Get();
100
101   /**
102    * @brief Create a handle to the new FontClient instance.
103    *
104    * @param[in] horizontalDpi The horizontal resolution in DPI.
105    * @param[in] verticalDpi The vertical resolution in DPI.
106    * @return A handle to the FontClient
107    */
108   static FontClient New(uint32_t horizontalDpi, uint32_t verticalDpi);
109
110   /**
111    * @brief Create an uninitialized TextAbstraction handle.
112    */
113   FontClient();
114
115   /**
116    * @brief Destructor
117    *
118    * This is non-virtual since derived Handle types must not contain data or virtual methods.
119    */
120   ~FontClient();
121
122   /**
123    * @brief This copy constructor is required for (smart) pointer semantics.
124    *
125    * @param[in] handle A reference to the copied handle.
126    */
127   FontClient(const FontClient& handle);
128
129   /**
130    * @brief This assignment operator is required for (smart) pointer semantics.
131    *
132    * @param [in] handle  A reference to the copied handle.
133    * @return A reference to this.
134    */
135   FontClient& operator=(const FontClient& handle);
136
137   /**
138    * @brief This move constructor is required for (smart) pointer semantics.
139    *
140    * @param[in] handle A reference to the moved handle.
141    */
142   FontClient(FontClient&& handle);
143
144   /**
145    * @brief This move assignment operator is required for (smart) pointer semantics.
146    *
147    * @param [in] handle  A reference to the moved handle.
148    * @return A reference to this.
149    */
150   FontClient& operator=(FontClient&& handle);
151
152   ////////////////////////////////////////
153   // Font management and validation.
154   ////////////////////////////////////////
155
156   /**
157    * @brief Clear all caches in FontClient
158    *
159    */
160   void ClearCache();
161
162   /**
163    * @brief Set the DPI of the target window.
164    *
165    * @note Multiple windows are not currently supported.
166    * @param[in] horizontalDpi The horizontal resolution in DPI.
167    * @param[in] verticalDpi The vertical resolution in DPI.
168    */
169   void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
170
171   /**
172    * @brief Retrieves the DPI previously set to the target window.
173    *
174    * @note Multiple windows are not currently supported.
175    * @param[out] horizontalDpi The horizontal resolution in DPI.
176    * @param[out] verticalDpi The vertical resolution in DPI.
177    */
178   void GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi);
179
180   /**
181    * @brief Called by Dali to retrieve the default font size for the platform.
182    *
183    * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
184    * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
185    * @return The default font size.
186    */
187   int GetDefaultFontSize();
188
189   /**
190    * @brief Called when the user changes the system defaults.
191    *
192    * @post Previously cached system defaults are removed.
193    */
194   void ResetSystemDefaults();
195
196   /**
197    * @brief Retrieve the list of default fonts supported by the system.
198    *
199    * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
200    */
201   void GetDefaultFonts(FontList& defaultFonts);
202
203   /**
204    * @brief Retrieve the active default font from the system.
205    *
206    * @param[out] fontDescription font structure describing the default font.
207    */
208   void GetDefaultPlatformFontDescription(FontDescription& fontDescription);
209
210   /**
211    * @brief Retrieve the list of fonts supported by the system.
212    *
213    * @param[out] systemFonts A list of font paths, family, width, weight and slant.
214    */
215   void GetSystemFonts(FontList& systemFonts);
216
217   /**
218    * @brief Retrieves the font description of a given font @p fontId.
219    *
220    * @param[in] fontId The font identifier.
221    * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
222    */
223   void GetDescription(FontId fontId, FontDescription& fontDescription);
224
225   /**
226    * @brief Retrieves the font point size of a given font @p fontId.
227    *
228    * @param[in] fontId The font identifier.
229    *
230    * @return The point size in 26.6 fractional points.
231    */
232   PointSize26Dot6 GetPointSize(FontId fontId);
233
234   /**
235    * @brief Whether the given @p character is supported by the font.
236    *
237    * @param[in] fontId The id of the font.
238    * @param[in] character The character.
239    *
240    * @return @e true if the character is supported by the font.
241    */
242   bool IsCharacterSupportedByFont(FontId fontId, Character character);
243
244   /**
245    * @brief Find the default font for displaying a UTF-32 character.
246    *
247    * This is useful when localised strings are provided for multiple languages
248    * i.e. when a single default font does not work for all languages.
249    *
250    * @param[in] charcode The character for which a font is needed.
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.
253    *
254    * @return A valid font identifier, or zero if the font does not exist.
255    */
256   FontId FindDefaultFont(Character       charcode,
257                          PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
258                          bool            preferColor        = false);
259
260   /**
261    * @brief Find a fallback-font for displaying a UTF-32 character.
262    *
263    * This is useful when localised strings are provided for multiple languages
264    * i.e. when a single default font does not work for all languages.
265    *
266    * @param[in] charcode The character for which a font is needed.
267    * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
268    *                                     The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
269    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
270    * @param[in] preferColor @e true if a color font is preferred.
271    *
272    * @return A valid font identifier, or zero if the font does not exist.
273    */
274   FontId FindFallbackFont(Character              charcode,
275                           const FontDescription& preferredFontDescription,
276                           PointSize26Dot6        requestedPointSize = DEFAULT_POINT_SIZE,
277                           bool                   preferColor        = false);
278
279   /**
280    * @brief Retrieve the unique identifier for a font.
281    *
282    * @param[in] path The path to a font file.
283    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
284    * @param[in] faceIndex The index of the font face (optional).
285    *
286    * @return A valid font identifier, or zero if the font does not exist.
287    */
288   FontId GetFontId(const FontPath& path,
289                    PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
290                    FaceIndex       faceIndex          = 0);
291
292   /**
293    * @brief Retrieves a unique font identifier for a given description.
294    *
295    * @param[in] preferredFontDescription Description of the preferred font.
296    *                                     The font will be the closest match to @p preferredFontDescription.
297    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
298    * @param[in] faceIndex The index of the font face (optional).
299    *
300    * @return A valid font identifier, or zero if no font is found.
301    */
302   FontId GetFontId(const FontDescription& preferredFontDescription,
303                    PointSize26Dot6        requestedPointSize = DEFAULT_POINT_SIZE,
304                    FaceIndex              faceIndex          = 0);
305
306   /**
307    * @brief Retrieves a unique font identifier for a given bitmap font.
308    * If the font is not present, it will cache the given font, and give it a new font id.
309    *
310    * @param[in] bitmapFont A bitmap font.
311    *
312    * @return A valid font identifier.
313    */
314   FontId GetFontId(const BitmapFont& bitmapFont);
315
316   /**
317    * @brief Check to see if a font is scalable.
318    *
319    * @param[in] path The path to a font file.
320    * @return true if scalable.
321    */
322   bool IsScalable(const FontPath& path);
323
324   /**
325    * @brief Check to see if a font is scalable.
326    *
327    * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
328    *
329    * @param[in] fontDescription A font description.
330    *
331    * @return true if scalable
332    */
333   bool IsScalable(const FontDescription& fontDescription);
334
335   /**
336    * @brief Get a list of sizes available for a fixed size font.
337    *
338    * @param[in] path The path to a font file.
339    * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
340    */
341   void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes);
342
343   /**
344    * @brief Get a list of sizes available for a fixed size font.
345    *
346    * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
347    *
348    * @param[in] fontDescription A font description.
349    * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
350    */
351   void GetFixedSizes(const FontDescription&         fontDescription,
352                      Dali::Vector<PointSize26Dot6>& sizes);
353
354   /**
355    * @brief Whether the font has Italic style.
356    *
357    * @param[in] fontId The font identifier.
358    *
359    * @return true if the font has italic style.
360    */
361   bool HasItalicStyle(FontId fontId) const;
362
363   ////////////////////////////////////////
364   // Font metrics, glyphs and bitmaps.
365   ////////////////////////////////////////
366
367   /**
368    * @brief Query the metrics for a font.
369    *
370    * @param[in] fontId The identifier of the font for the required glyph.
371    * @param[out] metrics The font metrics.
372    */
373   void GetFontMetrics(FontId fontId, FontMetrics& metrics);
374
375   /**
376    * @brief Retrieve the glyph index for a UTF-32 character code.
377    *
378    * @param[in] fontId The identifier of the font for the required glyph.
379    * @param[in] charcode The UTF-32 character code.
380    *
381    * @return The glyph index, or zero if the character code is undefined.
382    */
383   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode);
384
385   /**
386    * @brief Return the glyph index of a given character code as modified by the variation selector.
387    *
388    * @param[in] fontId The identifier of the font for the required glyph.
389    * @param[in] charcode The UTF-32 character code.
390    * @param[in] variantSelector The UTF-32 character code point of the variation selector.
391    *
392    * @return The glyph index, or zero if the character code is undefined.
393    */
394   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector);
395
396   /**
397    * @brief Retrieve the metrics for a series of glyphs.
398    *
399    * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
400    *                      It may contain the advance and an offset set into the bearing from the shaping tool.
401    *                      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.
402    * @param[in] size The size of the array.
403    * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
404    * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
405    *
406    * @return @e true if all of the requested metrics were found.
407    */
408   bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true);
409
410   /**
411    * @brief Create a bitmap representation of a glyph.
412    *
413    * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
414    *
415    * @param[in]  fontId           The identifier of the font.
416    * @param[in]  glyphIndex       The index of a glyph within the specified font.
417    * @param[in]  isItalicRequired Whether the glyph requires italic style.
418    * @param[in]  isBoldRequired   Whether the glyph requires bold style.
419    * @param[out] data             The bitmap data.
420    * @param[in]  outlineWidth     The width of the glyph outline in pixels.
421    */
422   void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth);
423
424   /**
425    * @brief Create a bitmap representation of a glyph.
426    *
427    * @param[in] fontId The identifier of the font.
428    * @param[in] glyphIndex The index of a glyph within the specified font.
429    * @param[in] outlineWidth The width of the glyph outline in pixels.
430    *
431    * @return A valid PixelData, or an empty handle if the glyph could not be rendered.
432    */
433   PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth);
434
435   /**
436    * @brief Create a vector representation of a glyph.
437    *
438    * @note This feature requires highp shader support and is not available on all platforms
439    * @param[in] fontId The identifier of the font.
440    * @param[in] glyphIndex The index of a glyph within the specified font.
441    * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
442    * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
443    * @param[out] nominalWidth The width of the blob.
444    * @param[out] nominalHeight The height of the blob.
445    */
446   void CreateVectorBlob(FontId        fontId,
447                         GlyphIndex    glyphIndex,
448                         VectorBlob*&  blob,
449                         unsigned int& blobLength,
450                         unsigned int& nominalWidth,
451                         unsigned int& nominalHeight);
452
453   /**
454    * @brief Retrieves the ellipsis glyph for a requested point size.
455    *
456    * @param[in] requestedPointSize The requested point size.
457    *
458    * @return The ellipsis glyph.
459    */
460   const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize);
461
462   /**
463    * @brief Whether the given glyph @p glyphIndex is a color glyph.
464    *
465    * @param[in] fontId The font id.
466    * @param[in] glyphIndex The glyph index.
467    *
468    * @return @e true if the glyph is a color one.
469    */
470   bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex);
471
472   /**
473    * @brief  Add custom fonts directory
474    *
475    * @param[in] path to the fonts directory
476    *
477    * @return true if the fonts can be added.
478    */
479   bool AddCustomFontDirectory(const FontPath& path);
480
481   /**
482    * @brief Creates and stores an embedded item and it's metrics.
483    *
484    * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
485    * 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.
486    * If the url in the @p description is empty it stores the size.
487    *
488    * @param[in] description The description of the embedded item.
489    * @param[out] pixelFormat The pixel format of the image.
490    *
491    * return The index within the vector of embedded items.
492    */
493   GlyphIndex CreateEmbeddedItem(const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
494
495   /**
496    * @brief true to enable Atlas-Limitation.
497    *
498    * @note Used default configuration.
499    * @param[in] enabled The on/off value to enable/disable Atlas-Limitation.
500    */
501   void EnableAtlasLimitation(bool enabled);
502
503   /**
504    * @brief Check Atlas-Limitation is enabled or disabled.
505    *
506    * @note Used default configuration.
507    * return true if Atlas-Limitation is enabled, otherwise false.
508    */
509   bool IsAtlasLimitationEnabled() const;
510
511   /**
512    * @brief retrieve the maximum allowed width and height for text-atlas-block.
513    *
514    * @note Used default configuration.
515    * return the maximum width and height of text-atlas-block.
516    */
517   Size GetMaximumTextAtlasSize() const;
518
519   /**
520    * @brief retrieve the default width and height for text-atlas-block.
521    *
522    * @note Used default configuration.
523    * return the default width and height of text-atlas-block.
524    */
525   Size GetDefaultTextAtlasSize() const;
526
527   /**
528    * @brief retrieve the current maximum width and height for text-atlas-block.
529    *
530    * @note Used default configuration.
531    * return the current maximum width and height of text-atlas-block.
532    */
533   Size GetCurrentMaximumBlockSizeFitInAtlas() const;
534
535   /**
536    * @brief set the achieved size (width and height) for text-atlas-block.
537    * If @p currentMaximumBlockSizeFitInAtlas larger than the current maximum text atlas then store, otherwise ignore.
538    *
539    * @note Used default configuration.
540    * return true if the current maximum text atlas size is changed, otherwise false.
541    */
542   bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
543
544   /**
545    * @brief retrieve the number of points to scale-up one unit of point-size.
546    *
547    * @note Used default configuration.
548    * return the number of points per one unit of point-size
549    */
550   uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
551
552 public: // Not intended for application developers
553   /**
554    * @brief This constructor is used by FontClient::Get().
555    *
556    * @param[in] fontClient  A pointer to the internal fontClient object.
557    */
558   explicit DALI_INTERNAL FontClient(Internal::FontClient* fontClient);
559 };
560
561 /**
562  * @brief This is used to improve application launch performance
563  *
564  * @return A pre-initialized FontClient
565  */
566 DALI_ADAPTOR_API FontClient FontClientPreInitialize();
567
568 /**
569  * @brief This is used to pre-cache fonts in order to improve the runtime performance of the application.
570  *
571  * @param[in] fallbackFamilyList A list of fallback font families to be pre-cached.
572  * @param[in] extraFamilyList A list of additional font families to be pre-cached.
573  * @param[in] localeFamily A locale font family to be pre-cached.
574  * @param[in] useThread True if the font client should create thread and perform pre-caching, false otherwise.
575  */
576 DALI_ADAPTOR_API void FontClientPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily, bool useThread);
577
578 } // namespace TextAbstraction
579
580 } // namespace Dali
581
582 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H