68ada2c54d300a43c77783463e14f1eb3b287ac7
[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-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>
34
35 #include <dali/internal/common/message.h>
36
37 namespace Dali
38 {
39
40 namespace Integration
41 {
42 class PlatformAbstraction;
43 }
44
45 namespace Internal
46 {
47
48 // Forward declarations.
49
50 struct TextFormat;
51 class ResourceClient;
52 class FontFactory;
53
54 /**
55  *  Responsible for loading and rendering fonts using the Freetype library.
56  */
57 class Font : public BaseObject
58 {
59 public:
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.
62
63   /**
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
69    */
70   static Font* New(const std::string& fontFamily, const std::string& fontStyle, float size);
71
72   /**
73    * Equality operator.
74    *
75    * Two fonts are considered equal if they are created with the same family, style and size.
76    *
77    * @param[in] font The font to be compared.
78    *
79    * @return \e true if the given font is equal to current one, otherwise it returns \e false.
80    */
81   bool operator==( const Font& font ) const;
82
83  /**
84    * @copydoc Dali::Font::GetFamilyForText()
85    */
86   static const std::string GetFamilyForText(const std::string& text);
87
88  /**
89    * @copydoc Dali::Font::GetFamilyForText()
90    */
91   static const std::string GetFamilyForText(const Dali::Text& text);
92
93  /**
94    * @copydoc Dali::Font::GetFamilyForText()
95    */
96   static const std::string GetFamilyForText(const Dali::Character& text);
97
98   /**
99     * @copydoc Dali::Font::GetFamilyForText()
100     */
101   static const std::string GetFamilyForText(const TextArray& text);
102
103   /**
104    * @copydoc Dali::Font::GetLineHeightFromCapsHeight()
105    */
106   static PixelSize GetLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight);
107
108   /**
109    * @copydoc Dali::Font::GetInstalledFonts()
110    */
111   static void GetInstalledFonts( Dali::Font::FontListMode mode, std::vector<std::string>& fontList );
112
113   /**
114    * @copydoc Dali::Font::AllGlyphsSupported()
115    */
116   bool AllGlyphsSupported(const std::string& text) const;
117
118   /**
119    * @copydoc Dali::Font::AllGlyphsSupported()
120    */
121   bool AllGlyphsSupported(const Dali::Text& text) const;
122
123   /**
124    * @copydoc Dali::Font::AllGlyphsSupported(const Character& character) const
125    */
126   bool AllGlyphsSupported(const Dali::Character& character) const;
127
128   /**
129    * @copydoc Dali::Font::AllGlyphsSupported()
130    */
131   bool AllGlyphsSupported(const TextArray& text) const;
132
133   /**
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.
136    */
137   unsigned int GetResourceId() const;
138
139   /**
140    * @copydoc Dali::Font::MeasureTextWidth()
141    */
142   float MeasureTextWidth(const std::string& text, float textHeightPx) const;
143
144   /**
145    * @copydoc Dali::Font::MeasureTextWidth()
146    */
147   float MeasureTextWidth(const Dali::Text& text, float textHeightPx) const;
148
149   /**
150    * @copydoc Dali::Font::MeasureTextWidth(const Character& character, float textWidthPx) const
151    */
152   float MeasureTextWidth(const Dali::Character& character, float textHeightPx) const;
153
154   /**
155    * @copydoc Dali::Font::MeasureTextWidth()
156    */
157   float MeasureTextWidth(const TextArray& text, float textHeightPx) const;
158
159   /**
160    * @copydoc Dali::Font::MeasureTextHeight()
161    */
162   float MeasureTextHeight(const std::string& text, float textWidthPx) const;
163
164   /**
165    * @copydoc Dali::Font::MeasureTextHeight()
166    */
167   float MeasureTextHeight(const Dali::Text& text, float textWidthPx) const;
168
169   /**
170    * @copydoc Dali::Font::MeasureTextHeight(const Character& character, float textWidthPx) const
171    */
172   float MeasureTextHeight(const Dali::Character& character, float textWidthPx) const;
173
174   /**
175    * @copydoc Dali::Font::MeasureTextHeight()
176    */
177   float MeasureTextHeight(const TextArray& text, float textWidthPx) const;
178
179   /**
180    * @copydoc Dali::Font::MeasureText(const std::string& text) const
181    */
182   Vector3 MeasureText(const std::string& text) const;
183
184   /**
185    * @copydoc Dali::Font::MeasureText(const Text& text) const
186    */
187   Vector3 MeasureText(const Dali::Text& text) const;
188
189   /**
190    * @copydoc Dali::Font::MeasureText(const Character& character) const
191    */
192   Vector3 MeasureText(const Dali::Character& character) const;
193
194   /**
195    * @copydoc Dali::Font::MeasureText(const Text& text) const
196    */
197   Vector3 MeasureText(const TextArray& text) const;
198
199   /**
200    * @copydoc Dali::Font::IsDefaultSystemFont()
201    */
202   bool IsDefaultSystemFont() const;
203
204   /**
205    * @copydoc Dali::Font::IsDefaultSystemSize()
206    */
207   bool IsDefaultSystemSize() const;
208
209   /**
210    * @copydoc Dali::Font::GetName()
211    */
212   const std::string& GetName() const;
213
214   /**
215    * @copydoc Dali::Font::GetStyle()
216    */
217   const std::string& GetStyle() const;
218
219   /**
220    * @copydoc Dali::Font::GetPointSize()
221    */
222   float GetPointSize() const;
223
224   /**
225    * @copydoc Dali::Font::GetPixelSize()
226    */
227   unsigned int GetPixelSize() const;
228
229   /**
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
233    */
234   float GetLineHeight() const;
235
236   /**
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
240    */
241   float GetAscender() const;
242
243   /**
244    * @copydoc Dali::Font::GetUnderlineThickness()
245    */
246   float GetUnderlineThickness() const;
247
248   /**
249    * @copydoc Dali::Font::GetUnderlinePosition()
250    */
251   float GetUnderlinePosition() const;
252
253   /**
254    * Returns the scale factor to convert font units to pixels
255    * @return The scale factor to convert font units to pixels
256    */
257   float GetUnitsToPixels() const;
258
259   /**
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 .
263    */
264   void GetMetrics(const Dali::Character& character, Dali::Font::Metrics::Impl& metrics) const;
265
266   /**
267    * @copydoc Dali::PointsToPixels()
268    */
269   static unsigned int PointsToPixels(float pointSize);
270
271   /**
272    * @copydoc Dali::PixelsToPointsSize()
273    */
274   static float PixelsToPoints(int pixelSize);
275
276   /**
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.
285    */
286   static void ValidateFontRequest(std::string& fontFamily, std::string& fontStyle, float& fontPointSize, bool& fontFamilyDefault, bool& fontPointSizeDefault );
287
288   /**
289    * @copydoc GlyphAtlasManagerInterface::AddObserver()
290    */
291   void AddObserver(TextObserver& observer);
292
293   /**
294    * @copydoc GlyphAtlasManagerInterface::RemoveObserver()
295    */
296   void RemoveObserver(TextObserver& observer);
297
298   /**
299    * @copydoc GlyphAtlasManagerInterface::TextRequired()
300    */
301   TextVertexBuffer* TextRequired( const TextArray& text, const TextFormat& format );
302
303   /**
304    * @copydoc GlyphAtlasManagerInterface::TextNotRequired()
305    */
306   void TextNotRequired( const TextArray& text, const TextFormat& format, unsigned int textureId );
307
308   /**
309    * Add a glyph texture observer
310    * @param observer atlas observer
311    */
312   void AddTextureObserver(GlyphTextureObserver& observer );
313
314   /**
315    * Remove a glyph texture observer
316    * @param observer atlas observer
317    */
318   void RemoveTextureObserver(GlyphTextureObserver& observer );
319
320   /**
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
326    */
327   bool IsTextLoaded( const TextArray& text, const TextFormat& format, unsigned int textureId ) const;
328
329
330 private:
331
332   /**
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
342    */
343   Font(const std::string& fontFamily,
344        const std::string& fontStyle,
345        float size,
346        Integration::PlatformAbstraction& platform,
347        ResourceClient& resourceClient,
348        FontFactory& fontfactory,
349        GlyphAtlasManagerInterface& atlasInterface);
350
351 protected:
352
353   /**
354    * A reference counted object may only be deleted by calling Unreference()
355    */
356   virtual ~Font();
357
358 protected:
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
370 };
371
372 } // namespace Internal
373
374 inline const Internal::Font& GetImplementation(const Dali::Font& font)
375 {
376   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
377
378   const BaseObject& handle = font.GetBaseObject();
379
380   return static_cast<const Internal::Font&>(handle);
381 }
382
383
384 inline Internal::Font& GetImplementation(Dali::Font& font)
385 {
386   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
387
388   BaseObject& handle = font.GetBaseObject();
389
390   return static_cast<Internal::Font&>(handle);
391 }
392
393 } // namespace Dali
394
395 #endif // __DALI_INTERNAL_FONT_H__
396