1 #ifndef __DALI_INTERNAL_FONT_H__
2 #define __DALI_INTERNAL_FONT_H__
5 * Copyright (c) 2014 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.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/text/font.h>
27 #include <dali/public-api/object/base-object.h>
28 #include <dali/internal/event/text/font-metrics.h>
29 #include <dali/internal/common/text-array.h>
30 #include <dali/internal/common/text-vertex-buffer.h>
31 #include <dali/internal/event/text/text-observer.h>
32 #include <dali/internal/event/text/resource/glyph-texture-observer.h>
33 #include <dali/internal/event/text/atlas/glyph-atlas-manager-interface.h>
35 #include <dali/internal/common/message.h>
42 class PlatformAbstraction;
48 // Forward declarations.
55 * Responsible for loading and rendering fonts using the Freetype library.
57 class Font : public BaseObject
60 static const float MIN_FONT_POINT_SIZE; ///< Minimum font point size supported.
61 static const float MAX_FONT_POINT_SIZE; ///< Maximum font point size supported.
64 * Create a new font object using a font name and size
65 * @param [in] fontFamily The family's name of the font requested
66 * @param [in] fontStyle The style of the font requested
67 * @param [in] size size of the font in points.
68 * @return A newly allocated Font
70 static Font* New(const std::string& fontFamily, const std::string& fontStyle, float size);
75 * Two fonts are considered equal if they are created with the same family, style and size.
77 * @param[in] font The font to be compared.
79 * @return \e true if the given font is equal to current one, otherwise it returns \e false.
81 bool operator==( const Font& font ) const;
84 * @copydoc Dali::Font::GetFamilyForText()
86 static const std::string GetFamilyForText(const std::string& text);
89 * @copydoc Dali::Font::GetFamilyForText()
91 static const std::string GetFamilyForText(const Dali::Text& text);
94 * @copydoc Dali::Font::GetFamilyForText()
96 static const std::string GetFamilyForText(const Dali::Character& text);
99 * @copydoc Dali::Font::GetFamilyForText()
101 static const std::string GetFamilyForText(const TextArray& text);
104 * @copydoc Dali::Font::GetLineHeightFromCapsHeight()
106 static PixelSize GetLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight);
109 * @copydoc Dali::Font::GetInstalledFonts()
111 static void GetInstalledFonts( Dali::Font::FontListMode mode, std::vector<std::string>& fontList );
114 * @copydoc Dali::Font::AllGlyphsSupported()
116 bool AllGlyphsSupported(const std::string& text) const;
119 * @copydoc Dali::Font::AllGlyphsSupported()
121 bool AllGlyphsSupported(const Dali::Text& text) const;
124 * @copydoc Dali::Font::AllGlyphsSupported(const Character& character) const
126 bool AllGlyphsSupported(const Dali::Character& character) const;
129 * @copydoc Dali::Font::AllGlyphsSupported()
131 bool AllGlyphsSupported(const TextArray& text) const;
134 * returns the Id used for lookups
135 * @return the unique ID of the font. This is actually also the same as FontMetrics and FontAtlas Id.
137 unsigned int GetResourceId() const;
140 * @copydoc Dali::Font::MeasureTextWidth()
142 float MeasureTextWidth(const std::string& text, float textHeightPx) const;
145 * @copydoc Dali::Font::MeasureTextWidth()
147 float MeasureTextWidth(const Dali::Text& text, float textHeightPx) const;
150 * @copydoc Dali::Font::MeasureTextWidth(const Character& character, float textWidthPx) const
152 float MeasureTextWidth(const Dali::Character& character, float textHeightPx) const;
155 * @copydoc Dali::Font::MeasureTextWidth()
157 float MeasureTextWidth(const TextArray& text, float textHeightPx) const;
160 * @copydoc Dali::Font::MeasureTextHeight()
162 float MeasureTextHeight(const std::string& text, float textWidthPx) const;
165 * @copydoc Dali::Font::MeasureTextHeight()
167 float MeasureTextHeight(const Dali::Text& text, float textWidthPx) const;
170 * @copydoc Dali::Font::MeasureTextHeight(const Character& character, float textWidthPx) const
172 float MeasureTextHeight(const Dali::Character& character, float textWidthPx) const;
175 * @copydoc Dali::Font::MeasureTextHeight()
177 float MeasureTextHeight(const TextArray& text, float textWidthPx) const;
180 * @copydoc Dali::Font::MeasureText(const std::string& text) const
182 Vector3 MeasureText(const std::string& text) const;
185 * @copydoc Dali::Font::MeasureText(const Text& text) const
187 Vector3 MeasureText(const Dali::Text& text) const;
190 * @copydoc Dali::Font::MeasureText(const Character& character) const
192 Vector3 MeasureText(const Dali::Character& character) const;
195 * @copydoc Dali::Font::MeasureText(const Text& text) const
197 Vector3 MeasureText(const TextArray& text) const;
200 * @copydoc Dali::Font::IsDefaultSystemFont()
202 bool IsDefaultSystemFont() const;
205 * @copydoc Dali::Font::IsDefaultSystemSize()
207 bool IsDefaultSystemSize() const;
210 * @copydoc Dali::Font::GetName()
212 const std::string& GetName() const;
215 * @copydoc Dali::Font::GetStyle()
217 const std::string& GetStyle() const;
220 * @copydoc Dali::Font::GetPointSize()
222 float GetPointSize() const;
225 * @copydoc Dali::Font::GetPixelSize()
227 unsigned int GetPixelSize() const;
230 * The line height is the vertical distance between the top of the highest character
231 * to the bottom of the lowest character
232 * @return the line height of the font in pixels
234 float GetLineHeight() const;
237 * The ascender is the vertical distance from the
238 * baseline to the highest character coordinate in a font face.
239 * @return the ascender in pixels
241 float GetAscender() const;
244 * @copydoc Dali::Font::GetUnderlineThickness()
246 float GetUnderlineThickness() const;
249 * @copydoc Dali::Font::GetUnderlinePosition()
251 float GetUnderlinePosition() const;
254 * Returns the scale factor to convert font units to pixels
255 * @return The scale factor to convert font units to pixels
257 float GetUnitsToPixels() const;
260 * Get the glyph metrics for a character
261 * @param[in] character the character to get glyph metrics for
262 * @param[out] metrics used to store the glyph metrics .
264 void GetMetrics(const Dali::Character& character, Dali::Font::Metrics::Impl& metrics) const;
267 * @copydoc Dali::PointsToPixels()
269 static unsigned int PointsToPixels(float pointSize);
272 * @copydoc Dali::PixelsToPointsSize()
274 static float PixelsToPoints(int pixelSize);
277 * Validates a font request.
278 * Provide a requested fontFamily, fontStyle, and point size.
279 * Will update with valid values for these.
280 * @param[in,out] fontFamily The font family name to be validated
281 * @param[in,out] fontStyle The font style to be validated
282 * @param[in,out] fontPointSize The font point size to be validated
283 * @param[out] fontFamilyDefault Whether the requested font family is default or not.
284 * @param[out] fontPointSizeDefault Whether the requested point size is default or not.
286 static void ValidateFontRequest(std::string& fontFamily, std::string& fontStyle, float& fontPointSize, bool& fontFamilyDefault, bool& fontPointSizeDefault );
289 * @copydoc GlyphAtlasManagerInterface::AddObserver()
291 void AddObserver(TextObserver& observer);
294 * @copydoc GlyphAtlasManagerInterface::RemoveObserver()
296 void RemoveObserver(TextObserver& observer);
299 * @copydoc GlyphAtlasManagerInterface::TextRequired()
301 TextVertexBuffer* TextRequired( const TextArray& text, const TextFormat& format );
304 * @copydoc GlyphAtlasManagerInterface::TextNotRequired()
306 void TextNotRequired( const TextArray& text, const TextFormat& format, unsigned int textureId );
309 * Add a glyph texture observer
310 * @param observer atlas observer
312 void AddTextureObserver(GlyphTextureObserver& observer );
315 * Remove a glyph texture observer
316 * @param observer atlas observer
318 void RemoveTextureObserver(GlyphTextureObserver& observer );
321 * Check if the characters are loaded into a texture (atlas).
322 * @param[in] text text array
323 * @paran[in] format text format
324 * @param[in] textureId texture ID of the atlas
325 * @return true if all characters are available, false if not
327 bool IsTextLoaded( const TextArray& text, const TextFormat& format, unsigned int textureId ) const;
333 * Create a new font object using a font name and size
334 * @param [in] fontFamily The family's name of the font requested
335 * @param [in] fontStyle The style of the font requested
336 * @param [in] size The size of the font in points
337 * @param [in] platform platform abstraction
338 * @param [in] resourceClient resourceClient
339 * @param [in] fontfactory font factory
340 * @param [in] atlasInterface reference to the atlas manager interface
341 * @return A newly allocated Font
343 Font(const std::string& fontFamily,
344 const std::string& fontStyle,
346 Integration::PlatformAbstraction& platform,
347 ResourceClient& resourceClient,
348 FontFactory& fontfactory,
349 GlyphAtlasManagerInterface& atlasInterface);
354 * A reference counted object may only be deleted by calling Unreference()
359 bool mIsDefault; ///< Whether the font is a system default font.
360 bool mIsDefaultSize; ///< Whether the font is a system default size.
361 std::string mName; ///< Name of the font's family
362 std::string mStyle; ///< Font's style
363 FontMetricsIntrusivePtr mMetrics; ///< Pointer to font metrics object
364 float mPointSize; ///< Point size
365 float mUnitsToPixels; ///< Used to scale from font metrics to pixels
366 Integration::PlatformAbstraction& mPlatform; ///< platform abstraction
367 ResourceClient& mResourceClient; ///< resource client
368 FontFactory& mFontFactory; ///< font factory
369 GlyphAtlasManagerInterface& mAtlasManager; ///< glyph atlas manager interface
372 } // namespace Internal
374 inline const Internal::Font& GetImplementation(const Dali::Font& font)
376 DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
378 const BaseObject& handle = font.GetBaseObject();
380 return static_cast<const Internal::Font&>(handle);
384 inline Internal::Font& GetImplementation(Dali::Font& font)
386 DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
388 BaseObject& handle = font.GetBaseObject();
390 return static_cast<Internal::Font&>(handle);
395 #endif // __DALI_INTERNAL_FONT_H__