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