Emscripten workarounds and llvm syntax fixes
[platform/core/uifw/dali-core.git] / dali / internal / event / text / font-metrics.h
1 #ifndef __DALI_INTERNAL_FONT_METRICS_H__
2 #define __DALI_INTERNAL_FONT_METRICS_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
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/map-wrapper.h>
26 #include <dali/public-api/object/ref-object.h>
27 #include <dali/public-api/text/font.h>
28 #include <dali/internal/event/resources/resource-ticket.h>
29 #include <dali/internal/common/text-array.h>
30 #include <dali/internal/event/text/font-metrics-interface.h>
31 #include <dali/internal/event/text/resource/font-id.h>
32 #include <dali/internal/event/text/font-layout.h>
33 #include <dali/integration-api/glyph-set.h>
34
35 namespace Dali
36 {
37
38 /**
39  * Used to return metrics for a single character in public API
40  */
41 struct Font::Metrics::Impl
42 {
43   Impl()
44   : advance( 0.f ),
45     bearing( 0.f ),
46     width( 0.f ),
47     height( 0.f )
48   {}
49
50   float advance; ///< The distance between the glyph's current pen position and the pen's position of the next glyph.
51   float bearing; ///< The horizontal top side bearing. Is the distance between the baseline and the top of the glyph.
52   float width;   ///< The glyph's width.
53   float height;  ///< The glyph's height.
54 };
55
56 namespace Integration
57 {
58 class PlatformAbstraction;
59
60 } // namespace Integration
61
62 namespace Internal
63 {
64
65 // Forward declarations.
66
67 class FontMetrics;
68 class ResourceClient;
69
70 typedef IntrusivePtr<FontMetrics> FontMetricsIntrusivePtr;
71
72 typedef std::map< std::size_t, FontMetricsIntrusivePtr >  FontMetricsMap;
73 typedef FontMetricsMap::iterator                          FontMetricsIter;
74 typedef std::pair< std::size_t, FontMetricsIntrusivePtr > FontMetricsPair;
75
76 /**
77  * Class for storing glyph metrics. Only to be accessed from the event thread.
78  */
79 class FontMetrics : public FontMetricsInterface, public Dali::RefObject
80 {
81 public:
82   typedef Integration::TextResourceType::CharacterList CharacterList;
83
84   /**
85    * Creates a new font metrics object.
86    * @param [in] dpi System dpi.
87    * @param [in] hashValue Unique identifier for these metrics.
88    * @param [in] fontId font id.
89    * @param [in] fontFamily the font family
90    * @param [in] fontStyle the font style
91    * @param [in] resourceClient resource client
92    * @return An intrusive-pointer to the new instance.
93    */
94   static FontMetricsIntrusivePtr New( const Vector2& dpi,
95                                       const std::size_t hashValue,
96                                       const FontId fontId,
97                                       const std::string& fontFamily,
98                                       const std::string& fontStyle,
99                                       ResourceClient& resourceClient );
100
101
102   /**
103    * Loads the global metrics for the font.
104    * The metrics will either be read from a cache, or from Freetype
105    * and then saved to the cache.
106    *
107    */
108   void LoadGlobalMetrics();
109
110   /**
111    * Measure the natural size of a text string, as displayed in this font.
112    * @param[in] text              The text string to measure.
113    * @return The natural size of the text.
114    */
115   Vector3 MeasureText(const TextArray& text);
116
117   /**
118    * Check if all characters in a string are currently in the font
119    * @param[in] text          The string to check
120    * @return true if all characters are currently in the font
121    */
122   bool TextAvailable (const TextArray& text) const;
123
124   /**
125    * Given a text array, checks which characters have their metrics loaded.
126    * Characters which are not loaded are added to the missingText parameter
127    * @param[in]  text         The original string.
128    * @param[out] missingText  Characters from text not contained in this font.
129    * @return number of characters that have not had their metrics loaded
130    */
131   unsigned int GetMissingText(const TextArray& text, CharacterList& missingText ) const;
132
133 public: // for FontMetricsInterface
134
135   /**
136    * @copydoc FontMetricsInterface::LoadMetricsSynchronously
137    */
138   virtual void LoadMetricsSynchronously( const TextArray& text );
139
140   /**
141    * @copydoc FontMetricsInterface::GetGlyph()
142    */
143   virtual const GlyphMetric* GetGlyph( uint32_t characterCode) const;
144
145   /**
146    * @copydoc FontMetricsInterface::GetFontId()
147    */
148   virtual FontId GetFontId() const;
149
150   /**
151    * @copydoc FontMetricsInterface::GetFontFamilyName()
152    */
153   virtual const std::string& GetFontFamilyName() const;
154
155   /**
156    * @copydoc FontMetricsInterface::GetFontStyleName()
157    */
158   virtual const std::string&  GetFontStyleName() const;
159
160   /**
161    * @copydoc FontMetricsInterface::GetMaximumGylphSize()
162    */
163   virtual void GetMaximumGylphSize( float& width, float& height ) const;
164
165   /**
166    * @copydoc FontMetricsInterface::GetUnitsToPixels()
167    */
168    virtual float GetUnitsToPixels(const float pointSize) const;
169
170    /**
171     * @copydoc FontMetricsInterface::GetLineHeight()
172     */
173    virtual float GetLineHeight() const;
174
175    /**
176     * @copydoc FontMetricsInterface::GetAscender()
177     */
178    virtual float GetAscender() const;
179
180    /**
181     * @copydoc FontMetricsInterface::GetUnderlinePosition()
182     */
183    virtual float GetUnderlinePosition() const;
184
185    /**
186     * @copydoc FontMetricsInterface::GetUnderlineThickness()
187     */
188    virtual float GetUnderlineThickness() const;
189
190    /**
191     * @copydoc FontMetricsInterface::GetMaxWidth()
192     */
193    virtual float GetMaxWidth() const;
194
195    /**
196     * @copydoc FontMetricsInterface::GetMaxHeight()
197     */
198    virtual float GetMaxHeight() const;
199
200    /**
201     * @copydoc FontMetricsInterface::GetPadAdjustX()
202     */
203    virtual float GetPadAdjustX() const;
204
205    /**
206     * @copydoc FontMetricsInterface::GetPadAdjustY()
207     */
208    virtual float GetPadAdjustY() const;
209
210
211 public:
212
213   /**
214    * Get the glyph metrics for a character
215    * @param[in] character the character to get glyph metrics for
216    * @param[out] metrics used to store the glyph metrics .
217    */
218   void GetMetrics( const Dali::Character& character, Dali::Font::Metrics::Impl& metrics );
219
220   /**
221    * Increase the number of fonts using the metrics object
222    * Used by the font-factory to decided whether the metrics should be removed
223    * from the cache.
224    * This does not determine the life time of the object.
225    */
226   void IncreaseFontCount();
227
228   /**
229    * Decrease the number of fonts using the metrics object
230    * Used by the font-factory to decided whether the metrics should be removed
231    * from the cache.
232    * This does not determine the life time of the object.
233    */
234   void DecreaseFontCount();
235
236   /**
237    * Used by font-factory to remove the metrics from its cache when
238    * the font usage count reaches zero.
239    * @return the number of fonts using this metrics object
240    */
241   unsigned int GetFontUsageCount() const;
242
243 private:
244
245
246   /**
247    * Add glyphs to font
248    * @param[in] id resource id, used to find the ticket use for the request
249    * @param[in] glyphSet  The set of glyphs to insert into this font
250    */
251   void AddGlyphSet( Integration::ResourceId id, const Integration::GlyphSet& glyphSet );
252
253   /**
254    * Checks that the glyph metrics have been loaded
255    * if they haven't, then they are loaded.
256    */
257   void CheckMetricsLoaded();
258
259   /**
260    * Reads global glyph metrics from glyph cache.
261    * @return \e true if global metrics have been read correctly.
262    */
263   bool ReadGlobalMetricsFromCache();
264
265   /**
266    * Reads Glyph metrics from Glyph cache.
267    * @return \e true if global metrics have been read correctly.
268    */
269   bool ReadMetricsFromCache();
270
271   /**
272    * Writes global glyph metrics to the cache.
273    */
274   void WriteGlobalMetricsToCache();
275
276   /**
277    * Writes glyph metrics to the cache.
278    * @param[in] glyphSet The set of glyphs to insert into the cache.
279    */
280   void WriteMetricsToCache( const Integration::GlyphSet& glyphSet );
281
282   /**
283    * Private contructor use FontMetrics::New()
284    * @param[in] dpi       System dpi
285    * @param[in] hashValue Unique identifier for these metrics
286    * @param [in] fontId font id.
287    * @param [in] fontFamily the font family
288    * @param [in] fontStyle the font style
289    */
290   FontMetrics( const Vector2& dpi,
291                const std::size_t hashValue,
292                const FontId fontId,
293                const std::string& fontFamily,
294                const std::string& fontStyle,
295                ResourceClient& resourceClient );
296
297   /**
298    * Virtual destructor.
299    * Relies on default destructors.
300    */
301   virtual ~FontMetrics();
302
303   /**
304    * Adds glyph metrics to the cache
305    * @param glyphMetric the glyph metric
306    */
307   void AddGlyphMetricToCache(const Integration::GlyphMetrics& glyphMetric);
308
309   typedef std::map<uint32_t, GlyphMetric> TCharMap;
310
311   std::string           mFontFamily;            ///< font family name
312   std::string           mFontStyle;             ///< font style
313   TCharMap              mCharMap;               ///< Cache of GlyphMetric objects.
314   FontLayout            mFontLayout;            ///< font layout information (metrics, padding, dpi etc).
315   std::size_t           mHash;                  ///< Unique identifier for these metrics.
316   FontId                mFontId;                ///< Unique identifier for the font
317   unsigned int          mFontCount;             ///< How many font objects are using this font, used to know when it should be deleted from font factory
318   bool                  mMetricsLoaded;         ///< Whether the metrics cache has been loaded
319   Integration::PlatformAbstraction& mPlatform;  ///< platform abstraction
320 };
321
322 } // namespace Internal
323
324 } // namespace Dali
325
326 #endif // __DALI_INTERNAL_FONT_METRICS_H__