[dali_1.0.31] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / event / text / font-impl.h
1 #ifndef __DALI_INTERNAL_FONT_H__
2 #define __DALI_INTERNAL_FONT_H__
3
4 /*
5  * Copyright (c) 2014 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 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
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-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>
33 #include <dali/internal/common/message.h>
34 #include <dali/integration-api/text-array.h>
35
36 namespace Dali
37 {
38
39 namespace Integration
40 {
41 class PlatformAbstraction;
42 }
43
44 namespace Internal
45 {
46
47 // Forward declarations.
48
49 struct TextFormat;
50 class ResourceClient;
51 class FontFactory;
52
53 /**
54  *  Responsible for loading and rendering fonts using the Freetype library.
55  */
56 class Font : public BaseObject
57 {
58 public:
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.
61
62   /**
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
68    */
69   static Font* New(const std::string& fontFamily, const std::string& fontStyle, float size);
70
71   /**
72    * Equality operator.
73    *
74    * Two fonts are considered equal if they are created with the same family, style and size.
75    *
76    * @param[in] font The font to be compared.
77    *
78    * @return \e true if the given font is equal to current one, otherwise it returns \e false.
79    */
80   bool operator==( const Font& font ) const;
81
82   /**
83     * @copydoc Dali::Font::GetFamilyForText()
84     */
85   static const std::string GetFamilyForText(const Integration::TextArray& text);
86
87   /**
88    * @copydoc Dali::Font::GetLineHeightFromCapsHeight()
89    */
90   static PixelSize GetLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight);
91
92   /**
93    * @copydoc Dali::Font::GetInstalledFonts()
94    */
95   static void GetInstalledFonts( Dali::Font::FontListMode mode, std::vector<std::string>& fontList );
96
97   /**
98    * @copydoc Dali::Font::AllGlyphsSupported()
99    */
100   bool AllGlyphsSupported(const Integration::TextArray& text) const;
101
102   /**
103    * returns the Id used for lookups
104    * @return the unique ID of the font. This is actually also the same as FontMetrics and FontAtlas Id.
105    */
106   unsigned int GetResourceId() const;
107
108   /**
109    * @copydoc Dali::Font::MeasureTextWidth()
110    */
111   float MeasureTextWidth(const Integration::TextArray& text, float textHeightPx) const;
112
113   /**
114    * @copydoc Dali::Font::MeasureTextHeight()
115    */
116   float MeasureTextHeight(const Integration::TextArray& text, float textWidthPx) const;
117
118   /**
119    * @copydoc Dali::Font::MeasureText(const Text& text) const
120    */
121   Vector3 MeasureText(const Integration::TextArray& text) const;
122
123   /**
124    * @copydoc Dali::Font::IsDefaultSystemFont()
125    */
126   bool IsDefaultSystemFont() const;
127
128   /**
129    * @copydoc Dali::Font::IsDefaultSystemSize()
130    */
131   bool IsDefaultSystemSize() const;
132
133   /**
134    * @copydoc Dali::Font::GetName()
135    */
136   const std::string& GetName() const;
137
138   /**
139    * @copydoc Dali::Font::GetStyle()
140    */
141   const std::string& GetStyle() const;
142
143   /**
144    * @copydoc Dali::Font::GetPointSize()
145    */
146   float GetPointSize() const;
147
148   /**
149    * @copydoc Dali::Font::GetPixelSize()
150    */
151   unsigned int GetPixelSize() const;
152
153   /**
154    * The line height is the vertical distance between the top of the highest character
155    * to the bottom of the lowest character
156    * @return the line height of the font in pixels
157    */
158   float GetLineHeight() const;
159
160   /**
161    * The ascender is the vertical distance from the
162    * baseline to the highest character coordinate in a font face.
163    * @return the ascender in pixels
164    */
165   float GetAscender() const;
166
167   /**
168    * @copydoc Dali::Font::GetUnderlineThickness()
169    */
170   float GetUnderlineThickness() const;
171
172   /**
173    * @copydoc Dali::Font::GetUnderlinePosition()
174    */
175   float GetUnderlinePosition() const;
176
177   /**
178    * Returns the scale factor to convert font units to pixels
179    * @return The scale factor to convert font units to pixels
180    */
181   float GetUnitsToPixels() const;
182
183   /**
184    * Get the glyph metrics for a character
185    * @param[in] character the character to get glyph metrics for
186    * @param[out] metrics used to store the glyph metrics .
187    */
188   void GetMetrics(const Dali::Character& character, Dali::Font::Metrics::Impl& metrics) const;
189
190   /**
191    * @copydoc Dali::PointsToPixels()
192    */
193   static unsigned int PointsToPixels(float pointSize);
194
195   /**
196    * @copydoc Dali::PixelsToPointsSize()
197    */
198   static float PixelsToPoints(int pixelSize);
199
200   /**
201    * Validates a font request.
202    * Provide a requested fontFamily, fontStyle, and point size.
203    * Will update with valid values for these.
204    * @param[in,out] fontFamily The font family name to be validated
205    * @param[in,out] fontStyle The font style to be validated
206    * @param[in,out] fontPointSize The font point size to be validated
207    * @param[out] fontFamilyDefault Whether the requested font family is default or not.
208    * @param[out] fontPointSizeDefault Whether the requested point size is default or not.
209    */
210   static void ValidateFontRequest(std::string& fontFamily, std::string& fontStyle, float& fontPointSize, bool& fontFamilyDefault, bool& fontPointSizeDefault );
211
212   /**
213    * @copydoc GlyphAtlasManagerInterface::AddObserver()
214    */
215   void AddObserver(TextObserver& observer);
216
217   /**
218    * @copydoc GlyphAtlasManagerInterface::RemoveObserver()
219    */
220   void RemoveObserver(TextObserver& observer);
221
222   /**
223    * @copydoc GlyphAtlasManagerInterface::TextRequired()
224    */
225   TextVertexBuffer* TextRequired( const Integration::TextArray& text, const TextFormat& format );
226
227   /**
228    * @copydoc GlyphAtlasManagerInterface::TextNotRequired()
229    */
230   void TextNotRequired( const Integration::TextArray& text, const TextFormat& format, unsigned int textureId );
231
232   /**
233    * Add a glyph texture observer
234    * @param observer atlas observer
235    */
236   void AddTextureObserver(GlyphTextureObserver& observer );
237
238   /**
239    * Remove a glyph texture observer
240    * @param observer atlas observer
241    */
242   void RemoveTextureObserver(GlyphTextureObserver& observer );
243
244   /**
245    * Check if the characters are loaded into a texture (atlas).
246    * @param[in] text text array
247    * @paran[in] format text format
248    * @param[in] textureId texture ID of the atlas
249    * @return true if all characters are available, false if not
250    */
251   bool IsTextLoaded( const Integration::TextArray& text, const TextFormat& format, unsigned int textureId ) const;
252
253
254 private:
255
256   /**
257    * Create a new font object using a font name and size
258    * @param [in] fontFamily The family's name of the font requested
259    * @param [in] fontStyle The style of the font requested
260    * @param [in] size The size of the font in points
261    * @param [in] platform platform abstraction
262    * @param [in] resourceClient resourceClient
263    * @param [in] fontfactory font factory
264    * @param [in] atlasInterface  reference to the atlas manager interface
265    * @return A newly allocated Font
266    */
267   Font(const std::string& fontFamily,
268        const std::string& fontStyle,
269        float size,
270        Integration::PlatformAbstraction& platform,
271        ResourceClient& resourceClient,
272        FontFactory& fontfactory,
273        GlyphAtlasManagerInterface& atlasInterface);
274
275 protected:
276
277   /**
278    * A reference counted object may only be deleted by calling Unreference()
279    */
280   virtual ~Font();
281
282 protected:
283   bool                              mIsDefault;       ///< Whether the font is a system default font.
284   bool                              mIsDefaultSize;   ///< Whether the font is a system default size.
285   std::string                       mName;            ///< Name of the font's family
286   std::string                       mStyle;           ///< Font's style
287   FontMetricsIntrusivePtr           mMetrics;         ///< Pointer to font metrics object
288   float                             mPointSize;       ///< Point size
289   float                             mUnitsToPixels;   ///< Used to scale from font metrics to pixels
290   Integration::PlatformAbstraction& mPlatform;        ///< platform abstraction
291   ResourceClient&                   mResourceClient;  ///< resource client
292   FontFactory&                      mFontFactory;     ///< font factory
293   GlyphAtlasManagerInterface&       mAtlasManager;    ///< glyph atlas manager interface
294 };
295
296 } // namespace Internal
297
298 inline const Internal::Font& GetImplementation(const Dali::Font& font)
299 {
300   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
301
302   const BaseObject& handle = font.GetBaseObject();
303
304   return static_cast<const Internal::Font&>(handle);
305 }
306
307
308 inline Internal::Font& GetImplementation(Dali::Font& font)
309 {
310   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
311
312   BaseObject& handle = font.GetBaseObject();
313
314   return static_cast<Internal::Font&>(handle);
315 }
316
317 } // namespace Dali
318
319 #endif // __DALI_INTERNAL_FONT_H__
320