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 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
40 * Used to return metrics for a single character in public API
42 struct Font::Metrics::Impl
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.
59 class PlatformAbstraction;
61 } // namespace Integration
66 // 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 * @return An intrusive-pointer to the new instance.
93 static FontMetricsIntrusivePtr New( const Vector2& dpi,
94 const std::size_t hashValue,
96 const std::string& fontFamily,
97 const std::string& fontStyle );
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.
106 void LoadGlobalMetrics();
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.
113 Vector3 MeasureText(const Integration::TextArray& text);
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
120 bool TextAvailable (const Integration::TextArray& text) const;
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
129 unsigned int GetMissingText(const Integration::TextArray& text, CharacterList& missingText ) const;
131 public: // for FontMetricsInterface
134 * @copydoc FontMetricsInterface::LoadMetricsSynchronously
136 virtual void LoadMetricsSynchronously( const Integration::TextArray& text );
139 * @copydoc FontMetricsInterface::GetGlyph()
141 virtual const GlyphMetric* GetGlyph( uint32_t characterCode) const;
144 * @copydoc FontMetricsInterface::GetFontId()
146 virtual FontId GetFontId() const;
149 * @copydoc FontMetricsInterface::GetFontFamilyName()
151 virtual const std::string& GetFontFamilyName() const;
154 * @copydoc FontMetricsInterface::GetFontStyleName()
156 virtual const std::string& GetFontStyleName() const;
159 * @copydoc FontMetricsInterface::GetMaximumGylphSize()
161 virtual void GetMaximumGylphSize( float& width, float& height ) const;
164 * @copydoc FontMetricsInterface::GetUnitsToPixels()
166 virtual float GetUnitsToPixels(const float pointSize) const;
169 * @copydoc FontMetricsInterface::GetLineHeight()
171 virtual float GetLineHeight() const;
174 * @copydoc FontMetricsInterface::GetAscender()
176 virtual float GetAscender() const;
179 * @copydoc FontMetricsInterface::GetUnderlinePosition()
181 virtual float GetUnderlinePosition() const;
184 * @copydoc FontMetricsInterface::GetUnderlineThickness()
186 virtual float GetUnderlineThickness() const;
189 * @copydoc FontMetricsInterface::GetMaxWidth()
191 virtual float GetMaxWidth() const;
194 * @copydoc FontMetricsInterface::GetMaxHeight()
196 virtual float GetMaxHeight() const;
199 * @copydoc FontMetricsInterface::GetPadAdjustX()
201 virtual float GetPadAdjustX() const;
204 * @copydoc FontMetricsInterface::GetPadAdjustY()
206 virtual float GetPadAdjustY() const;
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 .
216 void GetMetrics( const Dali::Character& character, Dali::Font::Metrics::Impl& metrics );
219 * Increase the number of fonts using the metrics object
220 * Used by the font-factory to decided whether the metrics should be removed
222 * This does not determine the life time of the object.
224 void IncreaseFontCount();
227 * Decrease the number of fonts using the metrics object
228 * Used by the font-factory to decided whether the metrics should be removed
230 * This does not determine the life time of the object.
232 void DecreaseFontCount();
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
239 unsigned int GetFontUsageCount() const;
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
249 void AddGlyphSet( Integration::ResourceId id, const Integration::GlyphSet& glyphSet );
252 * Checks that the glyph metrics have been loaded
253 * if they haven't, then they are loaded.
255 void CheckMetricsLoaded();
258 * Reads global glyph metrics from glyph cache.
259 * @return \e true if global metrics have been read correctly.
261 bool ReadGlobalMetricsFromCache();
264 * Reads Glyph metrics from Glyph cache.
265 * @return \e true if global metrics have been read correctly.
267 bool ReadMetricsFromCache();
270 * Writes global glyph metrics to the cache.
272 void WriteGlobalMetricsToCache();
275 * Writes glyph metrics to the cache.
276 * @param[in] glyphSet The set of glyphs to insert into the cache.
278 void WriteMetricsToCache( const Integration::GlyphSet& glyphSet );
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
288 FontMetrics( const Vector2& dpi,
289 const std::size_t hashValue,
291 const std::string& fontFamily,
292 const std::string& fontStyle );
295 * Virtual destructor.
296 * Relies on default destructors.
298 virtual ~FontMetrics();
301 * Adds glyph metrics to the cache
302 * @param glyphMetric the glyph metric
304 void AddGlyphMetricToCache(const Integration::GlyphMetrics& glyphMetric);
306 typedef std::map<uint32_t, GlyphMetric> TCharMap;
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
319 } // namespace Internal
323 #endif // __DALI_INTERNAL_FONT_METRICS_H__