1 #ifndef __DALI_INTERNAL_FONT_H__
2 #define __DALI_INTERNAL_FONT_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/text/font.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/internal/event/text/font-metrics.h>
28 #include <dali/internal/common/text-array.h>
29 #include <dali/internal/common/text-vertex-buffer.h>
30 #include <dali/internal/event/text/text-observer.h>
31 #include <dali/internal/event/text/resource/glyph-texture-observer.h>
32 #include <dali/internal/event/text/atlas/glyph-atlas-manager-interface.h>
34 #include <dali/internal/common/message.h>
41 class PlatformAbstraction;
47 // Forward declarations.
54 * Responsible for loading and rendering fonts using the Freetype library.
56 class Font : public BaseObject
59 static const float MIN_FONT_POINT_SIZE; ///< Minimum font point size supported.
60 static const float MAX_FONT_POINT_SIZE; ///< Maximum font point size supported.
63 * Create a new font object using a font name and size
64 * @param [in] fontFamily The family's name of the font requested
65 * @param [in] fontStyle The style of the font requested
66 * @param [in] size size of the font in points.
67 * @return A newly allocated Font
69 static Font* New(const std::string& fontFamily, const std::string& fontStyle, float size);
74 * Two fonts are considered equal if they are created with the same family, style and size.
76 * @param[in] font The font to be compared.
78 * @return \e true if the given font is equal to current one, otherwise it returns \e false.
80 bool operator==( const Font& font ) const;
83 * @copydoc Dali::Font::GetFamilyForText()
85 static const std::string GetFamilyForText(const std::string& text);
88 * @copydoc Dali::Font::GetFamilyForText()
90 static const std::string GetFamilyForText(const Dali::Text& text);
93 * @copydoc Dali::Font::GetFamilyForText()
95 static const std::string GetFamilyForText(const Dali::Character& text);
98 * @copydoc Dali::Font::GetFamilyForText()
100 static const std::string GetFamilyForText(const TextArray& text);
103 * @copydoc Dali::Font::GetLineHeightFromCapsHeight()
105 static PixelSize GetLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight);
108 * @copydoc Dali::Font::GetInstalledFonts()
110 static std::vector<std::string> GetInstalledFonts( Dali::Font::FontListMode mode );
113 * @copydoc Dali::Font::AllGlyphsSupported()
115 bool AllGlyphsSupported(const std::string& text) const;
118 * @copydoc Dali::Font::AllGlyphsSupported()
120 bool AllGlyphsSupported(const Dali::Text& text) const;
123 * @copydoc Dali::Font::AllGlyphsSupported(const Character& character) const
125 bool AllGlyphsSupported(const Dali::Character& character) const;
128 * @copydoc Dali::Font::AllGlyphsSupported()
130 bool AllGlyphsSupported(const TextArray& text) const;
133 * returns the Id used for lookups
134 * @return the unique ID of the font. This is actually also the same as FontMetrics and FontAtlas Id.
136 unsigned int GetResourceId() const;
139 * @copydoc Dali::Font::MeasureTextWidth()
141 float MeasureTextWidth(const std::string& text, float textHeightPx) const;
144 * @copydoc Dali::Font::MeasureTextWidth()
146 float MeasureTextWidth(const Dali::Text& text, float textHeightPx) const;
149 * @copydoc Dali::Font::MeasureTextWidth(const Character& character, float textWidthPx) const
151 float MeasureTextWidth(const Dali::Character& character, float textHeightPx) const;
154 * @copydoc Dali::Font::MeasureTextWidth()
156 float MeasureTextWidth(const TextArray& text, float textHeightPx) const;
159 * @copydoc Dali::Font::MeasureTextHeight()
161 float MeasureTextHeight(const std::string& text, float textWidthPx) const;
164 * @copydoc Dali::Font::MeasureTextHeight()
166 float MeasureTextHeight(const Dali::Text& text, float textWidthPx) const;
169 * @copydoc Dali::Font::MeasureTextHeight(const Character& character, float textWidthPx) const
171 float MeasureTextHeight(const Dali::Character& character, float textWidthPx) const;
174 * @copydoc Dali::Font::MeasureTextHeight()
176 float MeasureTextHeight(const TextArray& text, float textWidthPx) const;
179 * @copydoc Dali::Font::MeasureText(const std::string& text) const
181 Vector3 MeasureText(const std::string& text) const;
184 * @copydoc Dali::Font::MeasureText(const Text& text) const
186 Vector3 MeasureText(const Dali::Text& text) const;
189 * @copydoc Dali::Font::MeasureText(const Character& character) const
191 Vector3 MeasureText(const Dali::Character& character) const;
194 * @copydoc Dali::Font::MeasureText(const Text& text) const
196 Vector3 MeasureText(const TextArray& text) const;
199 * @copydoc Dali::Font::IsDefaultSystemFont()
201 bool IsDefaultSystemFont() const;
204 * @copydoc Dali::Font::IsDefaultSystemSize()
206 bool IsDefaultSystemSize() const;
209 * @copydoc Dali::Font::GetName()
211 const std::string& GetName() const;
214 * @copydoc Dali::Font::GetStyle()
216 const std::string& GetStyle() const;
219 * @copydoc Dali::Font::GetPointSize()
221 float GetPointSize() const;
224 * @copydoc Dali::Font::GetPixelSize()
226 unsigned int GetPixelSize() const;
229 * The line height is the vertical distance between the top of the highest character
230 * to the bottom of the lowest character
231 * @return the line height of the font in pixels
233 float GetLineHeight() const;
236 * The ascender is the vertical distance from the
237 * baseline to the highest character coordinate in a font face.
238 * @return the ascender in pixels
240 float GetAscender() const;
243 * @copydoc Dali::Font::GetUnderlineThickness()
245 float GetUnderlineThickness() const;
248 * @copydoc Dali::Font::GetUnderlinePosition()
250 float GetUnderlinePosition() const;
253 * Returns the scale factor to convert font units to pixels
254 * @return The scale factor to convert font units to pixels
256 float GetUnitsToPixels() const;
259 * Get the glyph metrics for a character
260 * @param[in] character the character to get glyph metrics for
261 * @param[out] metrics used to store the glyph metrics .
263 void GetMetrics(const Dali::Character& character, Dali::Font::Metrics::Impl& metrics) const;
266 * @copydoc Dali::PointsToPixels()
268 static unsigned int PointsToPixels(float pointSize);
271 * @copydoc Dali::PixelsToPointsSize()
273 static float PixelsToPoints(int pixelSize);
276 * Validates a font request.
277 * Provide a requested fontFamily, fontStyle, and point size.
278 * Will update with valid values for these.
279 * @param[in,out] fontFamily The font family name to be validated
280 * @param[in,out] fontStyle The font style to be validated
281 * @param[in,out] fontPointSize The font point size to be validated
282 * @param[out] fontFamilyDefault Whether the requested font family is default or not.
283 * @param[out] fontPointSizeDefault Whether the requested point size is default or not.
285 static void ValidateFontRequest(std::string& fontFamily, std::string& fontStyle, float& fontPointSize, bool& fontFamilyDefault, bool& fontPointSizeDefault );
288 * @copydoc GlyphAtlasManagerInterface::AddObserver()
290 void AddObserver(TextObserver& observer);
293 * @copydoc GlyphAtlasManagerInterface::RemoveObserver()
295 void RemoveObserver(TextObserver& observer);
298 * @copydoc GlyphAtlasManagerInterface::TextRequired()
300 TextVertexBuffer* TextRequired( const TextArray& text, const TextFormat& format );
303 * @copydoc GlyphAtlasManagerInterface::TextNotRequired()
305 void TextNotRequired( const TextArray& text, const TextFormat& format, unsigned int textureId );
308 * Add a glyph texture observer
309 * @param observer atlas observer
311 void AddTextureObserver(GlyphTextureObserver& observer );
314 * Remove a glyph texture observer
315 * @param observer atlas observer
317 void RemoveTextureObserver(GlyphTextureObserver& observer );
320 * Check if the characters are loaded into a texture (atlas).
321 * @param[in] text text array
322 * @paran[in] format text format
323 * @param[in] textureId texture ID of the atlas
324 * @return true if all characters are available, false if not
326 bool IsTextLoaded( const TextArray& text, const TextFormat& format, unsigned int textureId ) const;
332 * Create a new font object using a font name and size
333 * @param [in] fontFamily The family's name of the font requested
334 * @param [in] fontStyle The style of the font requested
335 * @param [in] size The size of the font in points
336 * @param [in] platform platform abstraction
337 * @param [in] resourceClient resourceClient
338 * @param [in] fontfactory font factory
339 * @param [in] atlasInterface reference to the atlas manager interface
340 * @return A newly allocated Font
342 Font(const std::string& fontFamily,
343 const std::string& fontStyle,
345 Integration::PlatformAbstraction& platform,
346 ResourceClient& resourceClient,
347 FontFactory& fontfactory,
348 GlyphAtlasManagerInterface& atlasInterface);
353 * A reference counted object may only be deleted by calling Unreference()
358 bool mIsDefault; ///< Whether the font is a system default font.
359 bool mIsDefaultSize; ///< Whether the font is a system default size.
360 std::string mName; ///< Name of the font's family
361 std::string mStyle; ///< Font's style
362 FontMetricsIntrusivePtr mMetrics; ///< Pointer to font metrics object
363 float mPointSize; ///< Point size
364 float mUnitsToPixels; ///< Used to scale from font metrics to pixels
365 Integration::PlatformAbstraction& mPlatform; ///< platform abstraction
366 ResourceClient& mResourceClient; ///< resource client
367 FontFactory& mFontFactory; ///< font factory
368 GlyphAtlasManagerInterface& mAtlasManager; ///< glyph atlas manager interface
371 } // namespace Internal
373 inline const Internal::Font& GetImplementation(const Dali::Font& font)
375 DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
377 const BaseObject& handle = font.GetBaseObject();
379 return static_cast<const Internal::Font&>(handle);
383 inline Internal::Font& GetImplementation(Dali::Font& font)
385 DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
387 BaseObject& handle = font.GetBaseObject();
389 return static_cast<Internal::Font&>(handle);
394 #endif // __DALI_INTERNAL_FONT_H__