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