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