FastUIDraw
glyph_cache.hpp
Go to the documentation of this file.
1 /*!
2  * \file glyph_cache.hpp
3  * \brief file glyph_cache.hpp
4  *
5  * Copyright 2016 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 
20 #ifndef FASTUIDRAW_GLYPH_CACHE_HPP
21 #define FASTUIDRAW_GLYPH_CACHE_HPP
22 
25 #include <fastuidraw/text/font.hpp>
29 
30 namespace fastuidraw
31 {
32 /*!\addtogroup Glyph
33  * @{
34  */
35 
36  /*!
37  * \brief
38  * A GlyphCache represents a cache of glyphs and manages the uploading
39  * of the data to a GlyphAtlas. The methods of GlyphAtlas are thread
40  * safe because it maintains an internal mutex lock for the durations
41  * of its methods.
42  */
43  class GlyphCache:public reference_counted<GlyphCache>::concurrent
44  {
45  public:
46  /*!
47  * An AllocationHandle represents a handle to data allocated
48  * on the underlying GlyphAtlas of a GlyphCache. The handle
49  * is to be used to deallocate from the GlyphAtlas. Note that
50  * all data on the GlyphCache is deallocated when \ref
51  * GlyphCache::clear_atlas() or \ref GlyphCache::clear_cache()
52  * is called.
53  */
55  {
56  public:
57  AllocationHandle(void):
58  m_location(0),
59  m_size(0)
60  {}
61 
62  /*!
63  * Returns true if the AllocationHandle refers
64  * to a successful allocation.
65  */
66  bool
67  valid(void) const
68  {
69  return m_size > 0;
70  }
71 
72  /*!
73  * Returns the location within the GlyphAtlas of the
74  * allocated data.
75  */
76  unsigned int
77  location(void) const
78  {
79  return m_location;
80  }
81 
82  private:
83  friend class GlyphCache;
84  unsigned int m_location, m_size;
85  };
86 
87  /*!
88  * Ctor
89  * \param patlas GlyphAtlas to store glyph data
90  */
91  explicit
93 
94  ~GlyphCache();
95 
96  /*!
97  * Fetch, and if necessay create and store, the metrics
98  * of given a glyph code of a font.
99  * \param font font from which to take the glyph
100  * \param glyph_code glyph code
101  */
103  fetch_glyph_metrics(const FontBase *font, uint32_t glyph_code);
104 
105  /*!
106  * Fetch, and if necessay create and store, the metrics
107  * of given a set of glyph codes of a font.
108  * \param font font from which to take the glyph
109  * \param glyph_codes glyph codes to fetch
110  * \param out_metrics location to which to write the Glyph
111  */
112  void
113  fetch_glyph_metrics(const FontBase *font,
114  c_array<const uint32_t> glyph_codes,
115  c_array<GlyphMetrics> out_metrics);
116 
117  /*!
118  * Fetch, and if necessay create and store, the metrics
119  * of given a set of glyph codes of a font.
120  * \param glyph_sources sequence of \ref GlyphSource values
121  * \param out_metrics location to which to write the Glyph
122  */
123  void
125  c_array<GlyphMetrics> out_metrics);
126 
127  /*!
128  * Fetch, and if necessay create and store, a glyph given a
129  * glyph code of a font and a GlyphRenderer specifying how
130  * to render the glyph.
131  * \param render renderer of fetched Glyph
132  * \param font font from which to take the glyph
133  * \param glyph_code glyph code
134  * \param upload_to_atlas if true, upload to atlas
135  */
136  Glyph
137  fetch_glyph(GlyphRenderer render, const FontBase *font,
138  uint32_t glyph_code, bool upload_to_atlas = true);
139 
140  /*!
141  * Fetch, and if necessay create and store, a sequence of
142  * glyphs given a sequence of glyph codes of a font and a
143  * GlyphRenderer specifying how to render the glyph.
144  * \param render renderer of fetched Glyph
145  * \param font font from which to take the glyph
146  * \param glyph_codes sequence of glyph codes
147  * \param[out] out_glyphs location to which to write the glyphs;
148  * the size must be the same as glyph_codes
149  * \param upload_to_atlas if true, upload glyphs to atlas
150  */
151  void
152  fetch_glyphs(GlyphRenderer render, const FontBase *font,
153  c_array<const uint32_t> glyph_codes,
154  c_array<Glyph> out_glyphs,
155  bool upload_to_atlas = true);
156 
157  /*!
158  * Fetch, and if necessay create and store, a sequence of
159  * glyphs given a sequence of glyph codes of a font and a
160  * GlyphRenderer specifying how to render the glyph.
161  * \param render renderer of fetched Glyph
162  * \param glyph_sources sequence of \ref GlyphSource values
163  * \param[out] out_glyphs location to which to write the glyphs;
164  * the size must be the same as glyph_codes
165  * \param upload_to_atlas if true, upload glyphs to atlas
166  */
167  void
169  c_array<const GlyphSource> glyph_sources,
170  c_array<Glyph> out_glyphs,
171  bool upload_to_atlas = true);
172 
173  /*!
174  * Fetch, and if necessay create and store, a sequence of
175  * glyphs given a sequence of \ref GlyphMetrics values and
176  * a \ref GlyphRenderer specifying how to render the glyph.
177  * \param render renderer of fetched Glyph
178  * \param glyph_metrics sequence of \ref GlyphMetrics values
179  * \param[out] out_glyphs location to which to write the glyphs;
180  * the size must be the same as glyph_codes
181  * \param upload_to_atlas if true, upload glyphs to atlas
182  */
183  void
185  c_array<const GlyphMetrics> glyph_metrics,
186  c_array<Glyph> out_glyphs,
187  bool upload_to_atlas = true);
188 
189  /*!
190  * Add a Glyph created with Glyph::create_glyph() to
191  * this GlyphCache. Will fail if a Glyph with the
192  * same glyph_code (GlyphMetrics::glyph_code()),
193  * font (GlyphMetrics::font()) and renderer
194  * (Glyph::renderer()) is already present in the
195  * GlyphCache.
196  * \param glyph Glyph to add to cache
197  * \param upload_to_atlas if true, upload to atlas
198  */
199  enum return_code
200  add_glyph(Glyph glyph, bool upload_to_atlas = true);
201 
202  /*!
203  * Deletes and removes a glyph from the GlyphCache,
204  * thus to use that glyph again requires calling fetch_glyph()
205  * (and thus fetching a new value for Glyph). The underlying
206  * memory of the Glyph will be reused by a later glyph,
207  * thus the Glyph value passed should be discarded.
208  * \param glyph Glyph to delete and remove from cache
209  */
210  void
211  delete_glyph(Glyph glyph);
212 
213  /*!
214  * Call to clear the backing GlyphAtlas. In doing so, the glyphs
215  * will lose their backing store in the GlyphAtlas and will need
216  * to be re-uploaded (see Glyph::upload_to_atlas()). The glyphs
217  * however are NOT removed from this GlyphCache. Thus, the return
218  * values of previous calls to create_glyph() are still valie, but
219  * they need to be re-uploaded to the GlyphAtlas with
220  * Glyph::upload_to_atlas().
221  */
222  void
223  clear_atlas(void);
224 
225  /*!
226  * Returns the number of times that this GlyphCache cleared
227  * its GlyphAtlas (i.e. the number of times clear_atlas() or
228  * clear_cache() have been called).
229  */
230  unsigned int
232 
233  /*!
234  * Clear this GlyphCache and the GlyphAtlas backing the glyphs.
235  * Thus all previous \ref Glyph and \ref GlyphMetrics values
236  * returned are no longer valid. In addition, as a side-effect
237  * of clearing all Glyph and GlyphMetrics values, all references
238  * to \ref FontBase objects are also released.
239  */
240  void
241  clear_cache(void);
242 
243  /*!
244  * Allocate and set data in the GlyphAtlas of this GlyphCache.
245  */
248 
249  /*!
250  * Deallocate data in the GlyphAtlas of this GlyphCache
251  * previously allocated with allocate_data().
252  */
253  void
255 
256  private:
257  void *m_d;
258  };
259 /*! @} */
260 } //namespace fastuidraw
261 
262 #endif
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
FontBase provides an interface for a font to generate glyph rendering data.
Definition: font.hpp:47
unsigned int number_times_atlas_cleared(void)
A wrapper over a pointer to implement reference counting.
return_code
Enumeration for simple return codes for functions for success or failure.
Definition: util.hpp:142
A GlyphCache represents a cache of glyphs and manages the uploading of the data to a GlyphAtlas...
Definition: glyph_cache.hpp:43
AllocationHandle allocate_data(c_array< const uint32_t > pdata)
Glyph fetch_glyph(GlyphRenderer render, const FontBase *font, uint32_t glyph_code, bool upload_to_atlas=true)
file glyph_atlas.hpp
void delete_glyph(Glyph glyph)
void fetch_glyphs(GlyphRenderer render, const FontBase *font, c_array< const uint32_t > glyph_codes, c_array< Glyph > out_glyphs, bool upload_to_atlas=true)
GlyphMetrics fetch_glyph_metrics(const FontBase *font, uint32_t glyph_code)
file glyph_source.hpp
Defines default reference counting base classes.
A GlyphMetrics provides information on the metrics of a glyph, all the values are in units of the fon...
A Glyph is essentially an opaque pointer to data for rendering and performing layout of a glyph...
Definition: glyph.hpp:48
void deallocate_data(AllocationHandle h)
file glyph_metrics.hpp
file glyph.hpp
enum return_code add_glyph(Glyph glyph, bool upload_to_atlas=true)
Specifies how to render a glyph.
file font.hpp
file reference_counted.hpp