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