1 #ifndef __DALI_INTERNAL_FONT_METRICS_H__
2 #define __DALI_INTERNAL_FONT_METRICS_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
39 * Used to return metrics for a single character in public API
41 struct Font::Metrics::Impl
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.
58 class PlatformAbstraction;
60 } // namespace Integration
65 // Forward declarations.
70 typedef IntrusivePtr<FontMetrics> FontMetricsIntrusivePtr;
72 typedef std::map< std::size_t, FontMetricsIntrusivePtr > FontMetricsMap;
73 typedef FontMetricsMap::iterator FontMetricsIter;
74 typedef std::pair< std::size_t, FontMetricsIntrusivePtr > FontMetricsPair;
77 * Class for storing glyph metrics. Only to be accessed from the event thread.
79 class FontMetrics : public FontMetricsInterface, public Dali::RefObject
82 typedef Integration::TextResourceType::CharacterList CharacterList;
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.
94 static FontMetricsIntrusivePtr New( const Vector2& dpi,
95 const std::size_t hashValue,
97 const std::string& fontFamily,
98 const std::string& fontStyle,
99 ResourceClient& resourceClient );
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.
108 void LoadGlobalMetrics();
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.
115 Vector3 MeasureText(const TextArray& text);
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
122 bool TextAvailable (const TextArray& text) const;
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
131 unsigned int GetMissingText(const TextArray& text, CharacterList& missingText ) const;
133 public: // for FontMetricsInterface
136 * @copydoc FontMetricsInterface::LoadMetricsSynchronously
138 virtual void LoadMetricsSynchronously( const TextArray& text );
141 * @copydoc FontMetricsInterface::GetGlyph()
143 virtual const GlyphMetric* GetGlyph( uint32_t characterCode) const;
146 * @copydoc FontMetricsInterface::GetFontId()
148 virtual FontId GetFontId() const;
151 * @copydoc FontMetricsInterface::GetFontFamilyName()
153 virtual const std::string& GetFontFamilyName() const;
156 * @copydoc FontMetricsInterface::GetFontStyleName()
158 virtual const std::string& GetFontStyleName() const;
161 * @copydoc FontMetricsInterface::GetMaximumGylphSize()
163 virtual void GetMaximumGylphSize( float& width, float& height ) const;
166 * @copydoc FontMetricsInterface::GetUnitsToPixels()
168 virtual float GetUnitsToPixels(const float pointSize) const;
171 * @copydoc FontMetricsInterface::GetLineHeight()
173 virtual float GetLineHeight() const;
176 * @copydoc FontMetricsInterface::GetAscender()
178 virtual float GetAscender() const;
181 * @copydoc FontMetricsInterface::GetUnderlinePosition()
183 virtual float GetUnderlinePosition() const;
186 * @copydoc FontMetricsInterface::GetUnderlineThickness()
188 virtual float GetUnderlineThickness() const;
191 * @copydoc FontMetricsInterface::GetMaxWidth()
193 virtual float GetMaxWidth() const;
196 * @copydoc FontMetricsInterface::GetMaxHeight()
198 virtual float GetMaxHeight() const;
201 * @copydoc FontMetricsInterface::GetPadAdjustX()
203 virtual float GetPadAdjustX() const;
206 * @copydoc FontMetricsInterface::GetPadAdjustY()
208 virtual float GetPadAdjustY() const;
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 .
218 void GetMetrics( const Dali::Character& character, Dali::Font::Metrics::Impl& metrics );
221 * Increase the number of fonts using the metrics object
222 * Used by the font-factory to decided whether the metrics should be removed
224 * This does not determine the life time of the object.
226 void IncreaseFontCount();
229 * Decrease the number of fonts using the metrics object
230 * Used by the font-factory to decided whether the metrics should be removed
232 * This does not determine the life time of the object.
234 void DecreaseFontCount();
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
241 unsigned int GetFontUsageCount() const;
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
251 void AddGlyphSet( Integration::ResourceId id, const Integration::GlyphSet& glyphSet );
254 * Checks that the glyph metrics have been loaded
255 * if they haven't, then they are loaded.
257 void CheckMetricsLoaded();
260 * Reads global glyph metrics from glyph cache.
261 * @return \e true if global metrics have been read correctly.
263 bool ReadGlobalMetricsFromCache();
266 * Reads Glyph metrics from Glyph cache.
267 * @return \e true if global metrics have been read correctly.
269 bool ReadMetricsFromCache();
272 * Writes global glyph metrics to the cache.
274 void WriteGlobalMetricsToCache();
277 * Writes glyph metrics to the cache.
278 * @param[in] glyphSet The set of glyphs to insert into the cache.
280 void WriteMetricsToCache( const Integration::GlyphSet& glyphSet );
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
290 FontMetrics( const Vector2& dpi,
291 const std::size_t hashValue,
293 const std::string& fontFamily,
294 const std::string& fontStyle,
295 ResourceClient& resourceClient );
298 * Virtual destructor.
299 * Relies on default destructors.
301 virtual ~FontMetrics();
304 * Adds glyph metrics to the cache
305 * @param glyphMetric the glyph metric
307 void AddGlyphMetricToCache(const Integration::GlyphMetrics& glyphMetric);
309 typedef std::map<uint32_t, GlyphMetric> TCharMap;
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
322 } // namespace Internal
326 #endif // __DALI_INTERNAL_FONT_METRICS_H__