Emscripten workarounds and llvm syntax fixes
[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 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
10 //
11 //     http://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <string>
22
23 // INTERNAL INCLUDES
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>
33
34 #include <dali/internal/common/message.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 std::string& text);
86
87  /**
88    * @copydoc Dali::Font::GetFamilyForText()
89    */
90   static const std::string GetFamilyForText(const Dali::Text& text);
91
92  /**
93    * @copydoc Dali::Font::GetFamilyForText()
94    */
95   static const std::string GetFamilyForText(const Dali::Character& text);
96
97   /**
98     * @copydoc Dali::Font::GetFamilyForText()
99     */
100   static const std::string GetFamilyForText(const TextArray& text);
101
102   /**
103    * @copydoc Dali::Font::GetLineHeightFromCapsHeight()
104    */
105   static PixelSize GetLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight);
106
107   /**
108    * @copydoc Dali::Font::GetInstalledFonts()
109    */
110   static std::vector<std::string> GetInstalledFonts( Dali::Font::FontListMode mode );
111
112   /**
113    * @copydoc Dali::Font::AllGlyphsSupported()
114    */
115   bool AllGlyphsSupported(const std::string& text) const;
116
117   /**
118    * @copydoc Dali::Font::AllGlyphsSupported()
119    */
120   bool AllGlyphsSupported(const Dali::Text& text) const;
121
122   /**
123    * @copydoc Dali::Font::AllGlyphsSupported(const Character& character) const
124    */
125   bool AllGlyphsSupported(const Dali::Character& character) const;
126
127   /**
128    * @copydoc Dali::Font::AllGlyphsSupported()
129    */
130   bool AllGlyphsSupported(const TextArray& text) const;
131
132   /**
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.
135    */
136   unsigned int GetResourceId() const;
137
138   /**
139    * @copydoc Dali::Font::MeasureTextWidth()
140    */
141   float MeasureTextWidth(const std::string& text, float textHeightPx) const;
142
143   /**
144    * @copydoc Dali::Font::MeasureTextWidth()
145    */
146   float MeasureTextWidth(const Dali::Text& text, float textHeightPx) const;
147
148   /**
149    * @copydoc Dali::Font::MeasureTextWidth(const Character& character, float textWidthPx) const
150    */
151   float MeasureTextWidth(const Dali::Character& character, float textHeightPx) const;
152
153   /**
154    * @copydoc Dali::Font::MeasureTextWidth()
155    */
156   float MeasureTextWidth(const TextArray& text, float textHeightPx) const;
157
158   /**
159    * @copydoc Dali::Font::MeasureTextHeight()
160    */
161   float MeasureTextHeight(const std::string& text, float textWidthPx) const;
162
163   /**
164    * @copydoc Dali::Font::MeasureTextHeight()
165    */
166   float MeasureTextHeight(const Dali::Text& text, float textWidthPx) const;
167
168   /**
169    * @copydoc Dali::Font::MeasureTextHeight(const Character& character, float textWidthPx) const
170    */
171   float MeasureTextHeight(const Dali::Character& character, float textWidthPx) const;
172
173   /**
174    * @copydoc Dali::Font::MeasureTextHeight()
175    */
176   float MeasureTextHeight(const TextArray& text, float textWidthPx) const;
177
178   /**
179    * @copydoc Dali::Font::MeasureText(const std::string& text) const
180    */
181   Vector3 MeasureText(const std::string& text) const;
182
183   /**
184    * @copydoc Dali::Font::MeasureText(const Text& text) const
185    */
186   Vector3 MeasureText(const Dali::Text& text) const;
187
188   /**
189    * @copydoc Dali::Font::MeasureText(const Character& character) const
190    */
191   Vector3 MeasureText(const Dali::Character& character) const;
192
193   /**
194    * @copydoc Dali::Font::MeasureText(const Text& text) const
195    */
196   Vector3 MeasureText(const TextArray& text) const;
197
198   /**
199    * @copydoc Dali::Font::IsDefaultSystemFont()
200    */
201   bool IsDefaultSystemFont() const;
202
203   /**
204    * @copydoc Dali::Font::IsDefaultSystemSize()
205    */
206   bool IsDefaultSystemSize() const;
207
208   /**
209    * @copydoc Dali::Font::GetName()
210    */
211   const std::string& GetName() const;
212
213   /**
214    * @copydoc Dali::Font::GetStyle()
215    */
216   const std::string& GetStyle() const;
217
218   /**
219    * @copydoc Dali::Font::GetPointSize()
220    */
221   float GetPointSize() const;
222
223   /**
224    * @copydoc Dali::Font::GetPixelSize()
225    */
226   unsigned int GetPixelSize() const;
227
228   /**
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
232    */
233   float GetLineHeight() const;
234
235   /**
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
239    */
240   float GetAscender() const;
241
242   /**
243    * @copydoc Dali::Font::GetUnderlineThickness()
244    */
245   float GetUnderlineThickness() const;
246
247   /**
248    * @copydoc Dali::Font::GetUnderlinePosition()
249    */
250   float GetUnderlinePosition() const;
251
252   /**
253    * Returns the scale factor to convert font units to pixels
254    * @return The scale factor to convert font units to pixels
255    */
256   float GetUnitsToPixels() const;
257
258   /**
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 .
262    */
263   void GetMetrics(const Dali::Character& character, Dali::Font::Metrics::Impl& metrics) const;
264
265   /**
266    * @copydoc Dali::PointsToPixels()
267    */
268   static unsigned int PointsToPixels(float pointSize);
269
270   /**
271    * @copydoc Dali::PixelsToPointsSize()
272    */
273   static float PixelsToPoints(int pixelSize);
274
275   /**
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.
284    */
285   static void ValidateFontRequest(std::string& fontFamily, std::string& fontStyle, float& fontPointSize, bool& fontFamilyDefault, bool& fontPointSizeDefault );
286
287   /**
288    * @copydoc GlyphAtlasManagerInterface::AddObserver()
289    */
290   void AddObserver(TextObserver& observer);
291
292   /**
293    * @copydoc GlyphAtlasManagerInterface::RemoveObserver()
294    */
295   void RemoveObserver(TextObserver& observer);
296
297   /**
298    * @copydoc GlyphAtlasManagerInterface::TextRequired()
299    */
300   TextVertexBuffer* TextRequired( const TextArray& text, const TextFormat& format );
301
302   /**
303    * @copydoc GlyphAtlasManagerInterface::TextNotRequired()
304    */
305   void TextNotRequired( const TextArray& text, const TextFormat& format, unsigned int textureId );
306
307   /**
308    * Add a glyph texture observer
309    * @param observer atlas observer
310    */
311   void AddTextureObserver(GlyphTextureObserver& observer );
312
313   /**
314    * Remove a glyph texture observer
315    * @param observer atlas observer
316    */
317   void RemoveTextureObserver(GlyphTextureObserver& observer );
318
319   /**
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
325    */
326   bool IsTextLoaded( const TextArray& text, const TextFormat& format, unsigned int textureId ) const;
327
328
329 private:
330
331   /**
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
341    */
342   Font(const std::string& fontFamily,
343        const std::string& fontStyle,
344        float size,
345        Integration::PlatformAbstraction& platform,
346        ResourceClient& resourceClient,
347        FontFactory& fontfactory,
348        GlyphAtlasManagerInterface& atlasInterface);
349
350 protected:
351
352   /**
353    * A reference counted object may only be deleted by calling Unreference()
354    */
355   virtual ~Font();
356
357 protected:
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
369 };
370
371 } // namespace Internal
372
373 inline const Internal::Font& GetImplementation(const Dali::Font& font)
374 {
375   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
376
377   const BaseObject& handle = font.GetBaseObject();
378
379   return static_cast<const Internal::Font&>(handle);
380 }
381
382
383 inline Internal::Font& GetImplementation(Dali::Font& font)
384 {
385   DALI_ASSERT_ALWAYS( font && "Font handle is empty" );
386
387   BaseObject& handle = font.GetBaseObject();
388
389   return static_cast<Internal::Font&>(handle);
390 }
391
392 } // namespace Dali
393
394 #endif // __DALI_INTERNAL_FONT_H__
395