1 #ifndef DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
2 #define DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 namespace TextAbstraction
38 namespace Internal DALI_INTERNAL
44 * @brief FontClient provides access to font information and resources.
46 * <h3>Querying the System Fonts</h3>
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".
51 * <h3>Accessing Fonts</h3>
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:
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 );
60 * Glyph metrics and bitmap resources can then be retrieved using the FontId.
62 class DALI_IMPORT_API FontClient : public BaseHandle
65 static const PointSize26Dot6 DEFAULT_POINT_SIZE; ///< The default point size.
68 * @brief Struct used to retrieve the glyph's bitmap.
70 struct GlyphBufferData
75 * Initializes struct members to their defaults.
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.
93 * @brief Retrieve a handle to the FontClient instance.
95 * @return A handle to the FontClient
97 static FontClient Get();
100 * @brief Create an uninitialized TextAbstraction handle.
107 * This is non-virtual since derived Handle types must not contain data or virtual methods.
112 * @brief This copy constructor is required for (smart) pointer semantics.
114 * @param[in] handle A reference to the copied handle.
116 FontClient( const FontClient& handle );
119 * @brief This assignment operator is required for (smart) pointer semantics.
121 * @param [in] handle A reference to the copied handle.
122 * @return A reference to this.
124 FontClient& operator=( const FontClient& handle );
126 ////////////////////////////////////////
127 // Font management and validation.
128 ////////////////////////////////////////
131 * @brief Set the DPI of the target window.
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.
137 void SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi );
140 * @brief Retrieves the DPI previously set to the target window.
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.
146 void GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi );
149 * @brief Called when the user changes the system defaults.
151 * @post Previously cached system defaults are removed.
153 void ResetSystemDefaults();
156 * @brief Retrieve the list of default fonts supported by the system.
158 * @param[out] defaultFonts A list of default font paths, family, width, weight and slant.
160 void GetDefaultFonts( FontList& defaultFonts );
163 * @brief Retrieve the active default font from the system.
165 * @param[out] fontDescription font structure describing the default font.
167 void GetDefaultPlatformFontDescription( FontDescription& fontDescription );
170 * @brief Retrieve the list of fonts supported by the system.
172 * @param[out] systemFonts A list of font paths, family, width, weight and slant.
174 void GetSystemFonts( FontList& systemFonts );
177 * @brief Retrieves the font description of a given font @p id.
179 * @param[in] id The font identifier.
180 * @param[out] fontDescription The path, family & style (width, weight and slant) describing the font.
182 void GetDescription( FontId id, FontDescription& fontDescription );
185 * @brief Retrieves the font point size of a given font @p id.
187 * @param[in] id The font identifier.
189 * @return The point size in 26.6 fractional points.
191 PointSize26Dot6 GetPointSize( FontId id );
194 * @brief Find the default font for displaying a UTF-32 character.
196 * This is useful when localised strings are provided for multiple languages
197 * i.e. when a single default font does not work for all languages.
199 * @param[in] charcode The character for which a font is needed.
200 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
201 * @param[in] preferColor @e true if a color font is preferred.
203 * @return A valid font identifier, or zero if the font does not exist.
205 FontId FindDefaultFont( Character charcode,
206 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
207 bool preferColor = false );
210 * @brief Find a fallback-font for displaying a UTF-32 character.
212 * This is useful when localised strings are provided for multiple languages
213 * i.e. when a single default font does not work for all languages.
215 * @param[in] charcode The character for which a font is needed.
216 * @param[in] preferredFontDescription Description of the preferred font which may not provide a glyph for @p charcode.
217 * The fallback-font will be the closest match to @p preferredFontDescription, which does support the required glyph.
218 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
219 * @param[in] preferColor @e true if a color font is preferred.
221 * @return A valid font identifier, or zero if the font does not exist.
223 FontId FindFallbackFont( Character charcode,
224 const FontDescription& preferredFontDescription,
225 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
226 bool preferColor = false );
229 * @brief Retrieve the unique identifier for a font.
231 * @param[in] path The path to a font file.
232 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
233 * @param[in] faceIndex The index of the font face (optional).
235 * @return A valid font identifier, or zero if the font does not exist.
237 FontId GetFontId( const FontPath& path,
238 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
239 FaceIndex faceIndex = 0 );
242 * @brief Retrieves a unique font identifier for a given description.
244 * @param[in] preferredFontDescription Description of the preferred font.
245 * The font will be the closest match to @p preferredFontDescription.
246 * @param[in] requestedPointSize The point size in 26.6 fractional points; the default point size is 12*64.
247 * @param[in] faceIndex The index of the font face (optional).
249 * @return A valid font identifier, or zero if no font is found.
251 FontId GetFontId( const FontDescription& preferredFontDescription,
252 PointSize26Dot6 requestedPointSize = DEFAULT_POINT_SIZE,
253 FaceIndex faceIndex = 0 );
256 * @brief Check to see if a font is scalable.
258 * @param[in] path The path to a font file.
259 * @return true if scalable.
261 bool IsScalable( const FontPath& path );
264 * @brief Check to see if a font is scalable.
266 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
268 * @param[in] fontDescription A font description.
270 * @return true if scalable
272 bool IsScalable( const FontDescription& fontDescription );
275 * @brief Get a list of sizes available for a fixed size font.
277 * @param[in] path The path to a font file.
278 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
280 void GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes );
283 * @brief Get a list of sizes available for a fixed size font.
285 * @note It the font style is not empty, it will be used instead the font weight and font slant slant.
287 * @param[in] fontDescription A font description.
288 * @param[out] sizes A list of the available sizes, if no sizes available will return empty.
290 void GetFixedSizes( const FontDescription& fontDescription,
291 Dali::Vector< PointSize26Dot6 >& sizes );
293 ////////////////////////////////////////
294 // Font metrics, glyphs and bitmaps.
295 ////////////////////////////////////////
298 * @brief Query the metrics for a font.
300 * @param[in] fontId The identifier of the font for the required glyph.
301 * @param[out] metrics The font metrics.
303 void GetFontMetrics( FontId fontId, FontMetrics& metrics );
306 * @brief Retrieve the glyph index for a UTF-32 character code.
308 * @param[in] fontId The identifier of the font for the required glyph.
309 * @param[in] charcode The UTF-32 character code.
311 * @return The glyph index, or zero if the character code is undefined.
313 GlyphIndex GetGlyphIndex( FontId fontId, Character charcode );
316 * @brief Retrieve the metrics for a series of glyphs.
318 * @param[in,out] array An array of glyph-info structures with initialized FontId & GlyphIndex values.
319 * It may contain the advance and an offset set into the bearing from the shaping tool.
320 * 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.
321 * @param[in] size The size of the array.
322 * @param[in] type The type of glyphs used for rendering; either bitmaps or vectors.
323 * @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
325 * @return @e true if all of the requested metrics were found.
327 bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal = true );
330 * @brief Create a bitmap representation of a glyph.
332 * @note The caller is responsible for deallocating the bitmap data @p data.buffer using delete[].
334 * @param[in] fontId The identifier of the font.
335 * @param[in] glyphIndex The index of a glyph within the specified font.
336 * @param[out] data The bitmap data.
338 void CreateBitmap( FontId fontId, GlyphIndex glyphIndex, GlyphBufferData& data );
341 * @brief Create a bitmap representation of a glyph.
343 * @param[in] fontId The identifier of the font.
344 * @param[in] glyphIndex The index of a glyph within the specified font.
346 * @return A valid BufferImage, or an empty handle if the glyph could not be rendered.
348 PixelData CreateBitmap( FontId fontId, GlyphIndex glyphIndex );
351 * @brief Create a vector representation of a glyph.
353 * @note This feature requires highp shader support and is not available on all platforms
354 * @param[in] fontId The identifier of the font.
355 * @param[in] glyphIndex The index of a glyph within the specified font.
356 * @param[out] blob A blob of data; this is owned by FontClient and should be copied by the caller of CreateVectorData().
357 * @param[out] blobLength The length of the blob data, or zero if the blob creation failed.
358 * @param[out] nominalWidth The width of the blob.
359 * @param[out] nominalHeight The height of the blob.
361 void CreateVectorBlob( FontId fontId,
362 GlyphIndex glyphIndex,
364 unsigned int& blobLength,
365 unsigned int& nominalWidth,
366 unsigned int& nominalHeight );
369 * @brief Retrieves the ellipsis glyph for a requested point size.
371 * @param[in] requestedPointSize The requested point size.
373 * @return The ellipsis glyph.
375 const GlyphInfo& GetEllipsisGlyph( PointSize26Dot6 requestedPointSize );
378 * @brief Whether the given glyph @p glyphIndex is a color glyph.
380 * @param[in] fontId The font id.
381 * @param[in] glyphIndex The glyph index.
383 * @return @e true if the glyph is a color one.
385 bool IsColorGlyph( FontId fontId, GlyphIndex glyphIndex );
387 public: // Not intended for application developers
389 * @brief This constructor is used by FontClient::Get().
391 * @param[in] fontClient A pointer to the internal fontClient object.
393 explicit DALI_INTERNAL FontClient( Internal::FontClient* fontClient );
396 } // namespace TextAbstraction
400 #endif // DALI_PLATFORM_TEXT_ABSTRACTION_FONT_CLIENT_H