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