[dali_1.4.10] Merge branch '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) 2019 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/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>
29
30 namespace Dali
31 {
32
33 namespace TextAbstraction
34 {
35
36 struct FontMetrics;
37 struct GlyphInfo;
38 struct BitmapFont;
39
40 namespace Internal DALI_INTERNAL
41 {
42 class FontClient;
43 }
44
45 /**
46  * @brief FontClient provides access to font information and resources.
47  *
48  * <h3>Querying the System Fonts</h3>
49  *
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".
52  *
53  * <h3>Accessing Fonts</h3>
54  *
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:
57  * @code
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 );
61  * @endcode
62  * Glyph metrics and bitmap resources can then be retrieved using the FontId.
63  */
64 class DALI_ADAPTOR_API FontClient : public BaseHandle
65 {
66 public:
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.
69
70   /**
71    * @brief Struct used to retrieve the glyph's bitmap.
72    */
73   struct DALI_ADAPTOR_API GlyphBufferData
74   {
75     /**
76      * @brief Constructor.
77      *
78      * Initializes struct members to their defaults.
79      */
80     GlyphBufferData();
81
82     /**
83      * @brief Destructor.
84      */
85     ~GlyphBufferData();
86
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.
91   };
92
93   /**
94    * @brief Used to load an embedded item into the font client.
95    */
96   struct EmbeddedItemDescription
97   {
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.
102   };
103
104 public:
105
106   /**
107    * @brief Retrieve a handle to the FontClient instance.
108    *
109    * @return A handle to the FontClient
110    */
111   static FontClient Get();
112
113   /**
114    * @brief Create an uninitialized TextAbstraction handle.
115    */
116   FontClient();
117
118   /**
119    * @brief Destructor
120    *
121    * This is non-virtual since derived Handle types must not contain data or virtual methods.
122    */
123   ~FontClient();
124
125   /**
126    * @brief This copy constructor is required for (smart) pointer semantics.
127    *
128    * @param[in] handle A reference to the copied handle.
129    */
130   FontClient( const FontClient& handle );
131
132   /**
133    * @brief This assignment operator is required for (smart) pointer semantics.
134    *
135    * @param [in] handle  A reference to the copied handle.
136    * @return A reference to this.
137    */
138   FontClient& operator=( const FontClient& handle );
139
140   ////////////////////////////////////////
141   // Font management and validation.
142   ////////////////////////////////////////
143
144   /**
145    * @brief Clear all caches in FontClient
146    *
147    */
148   void ClearCache();
149
150   /**
151    * @brief Set the DPI of the target window.
152    *
153    * @note Multiple windows are not currently supported.
154    * @param[in] horizontalDpi The horizontal resolution in DPI.
155    * @param[in] verticalDpi The vertical resolution in DPI.
156    */
157   void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
158
159   /**
160    * @brief Retrieves the DPI previously set to the target window.
161    *
162    * @note Multiple windows are not currently supported.
163    * @param[out] horizontalDpi The horizontal resolution in DPI.
164    * @param[out] verticalDpi The vertical resolution in DPI.
165    */
166   void GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi );
167
168   /**
169    * @brief Called by Dali to retrieve the default font size for the platform.
170    *
171    * This is an accessibility size, which is mapped to a UI Control specific point-size in stylesheets.
172    * For example if zero the smallest size, this could potentially map to TextLabel point-size 8.
173    * @return The default font size.
174    */
175   int GetDefaultFontSize();
176
177   /**
178    * @brief Called when the user changes the system defaults.
179    *
180    * @post Previously cached system defaults are removed.
181    */
182   void ResetSystemDefaults();
183
184   /**
185    * @brief Retrieve the list of default fonts supported by the system.
186    *
187    * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
188    */
189   void GetDefaultFonts( FontList& defaultFonts );
190
191   /**
192    * @brief Retrieve the active default font from the system.
193    *
194    * @param[out] fontDescription font structure describing the default font.
195    */
196   void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
197
198   /**
199    * @brief Retrieve the list of fonts supported by the system.
200    *
201    * @param[out] systemFonts A list of font paths, family, width, weight and slant.
202    */
203   void GetSystemFonts( FontList& systemFonts );
204
205   /**
206    * @brief Retrieves the font description of a given font @p id.
207    *
208    * @param[in] id The font identifier.
209    * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
210    */
211   void GetDescription( FontId id, FontDescription& fontDescription );
212
213   /**
214    * @brief Retrieves the font point size of a given font @p id.
215    *
216    * @param[in] id The font identifier.
217    *
218    * @return The point size in 26.6 fractional points.
219    */
220   PointSize26Dot6 GetPointSize( FontId id );
221
222   /**
223    * @brief Whether the given @p character is supported by the font.
224    *
225    * @param[in] fontId The id of the font.
226    * @param[in] character The character.
227    *
228    * @return @e true if the character is supported by the font.
229    */
230   bool IsCharacterSupportedByFont( FontId fontId, Character character );
231
232   /**
233    * @brief Find the default font for displaying a UTF-32 character.
234    *
235    * This is useful when localised strings are provided for multiple languages
236    * i.e. when a single default font does not work for all languages.
237    *
238    * @param[in] charcode The character for which a font is needed.
239    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
240    * @param[in] preferColor @e true if a color font is preferred.
241    *
242    * @return A valid font identifier, or zero if the font does not exist.
243    */
244   FontId FindDefaultFont( Character charcode,
245                           PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
246                           bool preferColor = false );
247
248   /**
249    * @brief Find a fallback-font for displaying a UTF-32 character.
250    *
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.
253    *
254    * @param[in] charcode The character for which a font is needed.
255    * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
256    *                                     The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
257    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
258    * @param[in] preferColor @e true if a color font is preferred.
259    *
260    * @return A valid font identifier, or zero if the font does not exist.
261    */
262   FontId FindFallbackFont( Character charcode,
263                            const FontDescription& preferredFontDescription,
264                            PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
265                            bool preferColor = false );
266
267   /**
268    * @brief Retrieve the unique identifier for a font.
269    *
270    * @param[in] path The path to a font file.
271    * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
272    * @param[in] faceIndex The index of the font face (optional).
273    *
274    * @return A valid font identifier, or zero if the font does not exist.
275    */
276   FontId GetFontId( const FontPath& path,
277                     PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
278                     FaceIndex faceIndex = 0 );
279
280   /**
281    * @brief Retrieves a unique font identifier for a given description.
282    *
283    * @param[in] preferredFontDescription Description of the preferred font.
284    *                                     The font will be the closest match to @p preferredFontDescription.
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 no font is found.
289    */
290   FontId GetFontId( const FontDescription& preferredFontDescription,
291                     PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
292                     FaceIndex faceIndex = 0 );
293
294   /**
295    * @brief Retrieves a unique font identifier for a given bitmap font.
296    *
297    * @param[in] bitmapFont A bitmap font.
298    *
299    * @return A valid font identifier, or zero if no bitmap font is created.
300    */
301   FontId GetFontId( const BitmapFont& bitmapFont );
302
303   /**
304    * @brief Check to see if a font is scalable.
305    *
306    * @param[in] path The path to a font file.
307    * @return true if scalable.
308    */
309   bool IsScalable( const FontPath& path );
310
311   /**
312    * @brief Check to see if a font is scalable.
313    *
314    * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
315    *
316    * @param[in] fontDescription A font description.
317    *
318    * @return true if scalable
319    */
320   bool IsScalable( const FontDescription& fontDescription );
321
322   /**
323    * @brief Get a list of sizes available for a fixed size font.
324    *
325    * @param[in] path The path to a font file.
326    * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
327    */
328   void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
329
330   /**
331    * @brief Get a list of sizes available for a fixed size font.
332    *
333    * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
334    *
335    * @param[in] fontDescription A font description.
336    * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
337    */
338   void GetFixedSizes( const FontDescription& fontDescription,
339                       Dali::Vector< PointSize26Dot6 >& sizes );
340
341   /**
342    * @brief Whether the font has Italic style.
343    *
344    * @param[in] fontId The font identifier.
345    *
346    * @return true if the font has italic style.
347    */
348   bool HasItalicStyle( FontId fontId ) const;
349
350   ////////////////////////////////////////
351   // Font metrics, glyphs and bitmaps.
352   ////////////////////////////////////////
353
354   /**
355    * @brief Query the metrics for a font.
356    *
357    * @param[in] fontId The identifier of the font for the required glyph.
358    * @param[out] metrics The font metrics.
359    */
360   void GetFontMetrics( FontId fontId, FontMetrics& metrics );
361
362   /**
363    * @brief Retrieve the glyph index for a UTF-32 character code.
364    *
365    * @param[in] fontId The identifier of the font for the required glyph.
366    * @param[in] charcode The UTF-32 character code.
367    *
368    * @return The glyph index, or zero if the character code is undefined.
369    */
370   GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
371
372   /**
373    * @brief Retrieve the metrics for a series of glyphs.
374    *
375    * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
376    *                      It may contain the advance and an offset set into the bearing from the shaping tool.
377    *                      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.
378    * @param[in] size The size of the array.
379    * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
380    * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
381    *
382    * @return @e true if all of the requested metrics were found.
383    */
384   bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true );
385
386   /**
387    * @brief Create a bitmap representation of a glyph.
388    *
389    * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
390    *
391    * @param[in]  fontId           The identifier of the font.
392    * @param[in]  glyphIndex       The index of a glyph within the specified font.
393    * @param[in]  isItalicRequired Whether the glyph requires italic style.
394    * @param[in]  isBoldRequired   Whether the glyph requires bold style.
395    * @param[out] data             The bitmap data.
396    * @param[in]  outlineWidth     The width of the glyph outline in pixels.
397    */
398   void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, GlyphBufferData& data, int outlineWidth );
399
400   /**
401    * @brief Create a bitmap representation of a glyph.
402    *
403    * @param[in] fontId The identifier of the font.
404    * @param[in] glyphIndex The index of a glyph within the specified font.
405    * @param[in] outlineWidth The width of the glyph outline in pixels.
406    *
407    * @return A valid BufferImage, or an empty handle if the glyph could not be rendered.
408    */
409   PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth );
410
411   /**
412    * @brief Create a vector representation of a glyph.
413    *
414    * @note This feature requires highp shader support and is not available on all platforms
415    * @param[in] fontId The identifier of the font.
416    * @param[in] glyphIndex The index of a glyph within the specified font.
417    * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
418    * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
419    * @param[out] nominalWidth The width of the blob.
420    * @param[out] nominalHeight The height of the blob.
421    */
422   void CreateVectorBlob( FontId fontId,
423                          GlyphIndex glyphIndex,
424                          VectorBlob*& blob,
425                          unsigned int& blobLength,
426                          unsigned int& nominalWidth,
427                          unsigned int& nominalHeight );
428
429   /**
430    * @brief Retrieves the ellipsis glyph for a requested point size.
431    *
432    * @param[in] requestedPointSize The requested point size.
433    *
434    * @return The ellipsis glyph.
435    */
436   const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
437
438   /**
439    * @brief Whether the given glyph @p glyphIndex is a color glyph.
440    *
441    * @param[in] fontId The font id.
442    * @param[in] glyphIndex The glyph index.
443    *
444    * @return @e true if the glyph is a color one.
445    */
446   bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
447
448   /**
449    * @brief  Add custom fonts directory
450    *
451    * @param[in] path to the fonts directory
452    *
453    * @return true if the fonts can be added.
454    */
455   bool AddCustomFontDirectory( const FontPath& path );
456
457   /**
458    * @brief Creates and stores an embedded item and it's metrics.
459    *
460    * If in the @p description there is a non empty url, it calls Dali::LoadImageFromFile() internally.
461    * 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.
462    * If the url in the @p description is empty it stores the size.
463    *
464    * @param[in] description The description of the embedded item.
465    * @param[out] pixelFormat The pixel format of the image.
466    *
467    * return The index within the vector of embedded items.
468    */
469   GlyphIndex CreateEmbeddedItem( const EmbeddedItemDescription& description, Pixel::Format& pixelFormat);
470
471
472 public: // Not intended for application developers
473   /**
474    * @brief This constructor is used by FontClient::Get().
475    *
476    * @param[in] fontClient  A pointer to the internal fontClient object.
477    */
478   explicit DALI_INTERNAL FontClient( Internal::FontClient* fontClient );
479 };
480
481 } // namespace TextAbstraction
482
483 } // namespace Dali
484
485 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H