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