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