FastUIDraw
font_database.hpp
Go to the documentation of this file.
1 /*!
2  * \file font_database.hpp
3  * \brief file font_database.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_FONT_DATABASE_HPP
21 #define FASTUIDRAW_FONT_DATABASE_HPP
22 
24 
25 namespace fastuidraw
26 {
27 /*!\addtogroup Glyph
28  * @{
29  */
30 
31  /*!
32  * \brief
33  * A FontDatabase performs the act of font selection and glyph
34  * selection. It uses the values of \ref FontProperties (except
35  * for FontProperties::source_label()) to select suitable font
36  * or fonts.
37  */
38  class FontDatabase:public reference_counted<FontDatabase>::concurrent
39  {
40  public:
41 
42  /*!
43  * \brief
44  * A FontGeneratorBase is a means to create a font. Adding a font
45  * via a FontGenerator allows one to avoid opening and creating
46  * the font until the font is actually needed.
47  */
48  class FontGeneratorBase:public reference_counted<FontGeneratorBase>::concurrent
49  {
50  public:
51  /*!
52  * To be implemented by a derived class to generate a font.
53  */
54  virtual
56  generate_font(void) const = 0;
57 
58  /*!
59  * To be implemented by a derived class to return the FontProperties
60  * of the font that would be generated by generate_font().
61  */
62  virtual
63  const FontProperties&
64  font_properties(void) const = 0;
65  };
66 
67  /*!
68  * \brief
69  * A FontGroup represents a group of fonts which is selected
70  * from a FontProperties value. The accessors for FontGroup
71  * are methods of FontDatabase::parent_group(FontGroup),
72  * FontDatabase::fetch_font(FontGroup, unsigned int)
73  * and FontDatabase::number_fonts(FontGroup).
74  */
75  class FontGroup
76  {
77  public:
78  FontGroup(void):
79  m_d(nullptr)
80  {}
81 
82  private:
83  friend class FontDatabase;
84  void *m_d;
85  };
86 
87  /*!
88  * Enumeration to define bits for how fonts and glyphs
89  * are selected.
90  */
92  {
93  /*!
94  * Require an exact match when selecting a font
95  */
97 
98  /*!
99  * Ignore FontProperties::style() field when selecting
100  * a font
101  */
103 
104  /*!
105  * Ignore FontProperties::bold() and FontProperties::italic()
106  * when selecing font.
107  */
109  };
110 
111  /*!
112  * Ctor
113  */
114  explicit
115  FontDatabase(void);
116 
117  ~FontDatabase();
118 
119  /*!
120  * Add a font to this FontDatabase; the value of
121  * FontBase::properties().source_label()
122  * will be used as a key to uniquely identify the
123  * font. If a font is already present with the
124  * the same value, will return \ref routine_fail
125  * and not add the font.
126  * \param h font to add
127  */
128  enum return_code
130 
131  /*!
132  * Add a font to this FontDatabase; the value of
133  * FontGeneratorBase::font_properties().source_label()
134  * will be used as a key to uniquely identify the
135  * font. If a font is already present with the
136  * the same value, will return \ref routine_fail
137  * and not add the font.
138  * \param h font to add
139  */
140  enum return_code
142 
143  /*!
144  * If the font named by a generator is not yet part of the
145  * FontDatabase, add it the FontDatabase, otherwise use
146  * the existing generator. From the generator, return the
147  * font it generates.
148  */
151 
152  /*!
153  * Fetch a font using FontProperties::source_label()
154  * as the key to find the font added with add_font()
155  * or add_font_generator().
156  * \param source_label value of FontProperties::source_label()
157  * to hunt for a font to have.
158  */
160  fetch_font(c_string source_label);
161 
162  /*!
163  * Provided as a conveniance, equivalent to
164  * \code
165  * std::ostringstream str;
166  * str << filename << ":" << face_index;
167  * return fetch_font(str.str().c_str());
168  * \endcode
169  * \param filename file source of font to hunt for
170  * \param face_index face index source of font to hunt for
171  */
173  fetch_font(c_string filename, int face_index);
174 
175  /*!
176  * Returns the number of fonts in a FontGroup
177  * \param G FontGroup to query
178  */
179  unsigned int
181 
182  /*!
183  * Returns a font of a FontGroup
184  * \param G FontGroup to query
185  * \param N index of font with 0 <= N < number_fonts(G)
186  */
188  fetch_font(FontGroup G, unsigned int N);
189 
190  /*!
191  * FontGroup objects are grouped into hierarchies for
192  * selection. Returns the parent FontGroup of a FontGroup.
193  * \param G FontGroup to query
194  */
195  FontGroup
197 
198  /*!
199  * Fetch a font from a FontProperties description. The return
200  * value will be the closest matched font added with add_font().
201  * \param props FontProperties by which to search
202  * \param selection_strategy using bit-wise ors of values of
203  * \ref selection_bits_t to choose the
204  * matching criteria of selecting a font.
205  */
207  fetch_font(const FontProperties &props, uint32_t selection_strategy);
208 
209  /*!
210  * Fetch a FontGroup from a FontProperties value
211  * \param props font properties used to generate group.
212  * \param selection_strategy using bit-wise ors of values of
213  * \ref selection_bits_t to choose the
214  * matching criteria of selecting a font.
215  */
216  FontGroup
217  fetch_group(const FontProperties &props, uint32_t selection_strategy);
218 
219  /*!
220  * Returns the root FontGroup for all fonts added to this FontDatabase.
221  */
222  FontGroup
223  root_group(void);
224 
225  /*!
226  * Fetch a GlyphSource with font merging from a glyph rendering type,
227  * font properties and character code.
228  * \param props font properties used to fetch font
229  * \param character_code character code of glyph to fetch
230  * \param selection_strategy using bit-wise ors of values of
231  * \ref selection_bits_t to choose the
232  * matching criteria of selecting a font.
233  */
235  fetch_glyph(const FontProperties &props, uint32_t character_code,
236  uint32_t selection_strategy = 0u);
237 
238  /*!
239  * Fetch a GlyphSource with font merging from a glyph rendering type, font properties
240  * and character code.
241  * \param group FontGroup used to fetch font
242  * \param character_code character code of glyph to fetch
243  * \param selection_strategy using bit-wise ors of values of
244  * \ref selection_bits_t to choose the
245  * matching criteria of selecting a font.
246  */
248  fetch_glyph(FontGroup group, uint32_t character_code,
249  uint32_t selection_strategy = 0u);
250 
251  /*!
252  * Fetch a GlyphSource with font merging from a glyph rendering type,
253  * font preference and character code.
254  * \param h pointer to font from which to fetch the glyph, if the glyph
255  * is not present in the font attempt to get the glyph from
256  * a font of similiar properties
257  * \param character_code character code of glyph to fetch
258  * \param selection_strategy using bit-wise ors of values of
259  * \ref selection_bits_t to choose the
260  * matching criteria of selecting a font.
261  */
263  fetch_glyph(const FontBase *h, uint32_t character_code,
264  uint32_t selection_strategy = 0u);
265 
266  /*!
267  * Fetch a GlyphSource without font merging from a glyph rendering type,
268  * font and character code.
269  * \param h pointer to font from which to fetch the glyph, if the glyph
270  * is not present in the font, then return an invalid Glyph.
271  * \param character_code character code of glyph to fetch
272  */
274  fetch_glyph_no_merging(const FontBase *h, uint32_t character_code);
275 
276  /*!
277  * Fill Glyph values from an iterator range of character code values.
278  * \tparam input_iterator read iterator to type that is castable to uint32_t
279  * \tparam output_iterator write iterator to Glyph
280  * \param group FontGroup to choose what font
281  * \param character_codes_begin iterator to 1st character code
282  * \param character_codes_end iterator to one past last character code
283  * \param output_begin begin iterator to output
284  * \param selection_strategy using bit-wise ors of values of
285  * \ref selection_bits_t to choose the
286  * matching criteria of selecting a font.
287  */
288  template<typename input_iterator,
289  typename output_iterator>
290  void
292  input_iterator character_codes_begin,
293  input_iterator character_codes_end,
294  output_iterator output_begin,
295  uint32_t selection_strategy = 0u);
296 
297  /*!
298  * Fill Glyph values from an iterator range of character code values.
299  * \tparam input_iterator read iterator to type that is castable to uint32_t
300  * \tparam output_iterator write iterator to Glyph
301  * \param h pointer to font from which to fetch the glyph, if the glyph
302  * is not present in the font attempt to get the glyph from
303  * a font of similiar properties
304  * \param character_codes_begin iterator to 1st character code
305  * \param character_codes_end iterator to one past last character code
306  * \param output_begin begin iterator to output
307  * \param selection_strategy using bit-wise ors of values of
308  * \ref selection_bits_t to choose the
309  * matching criteria of selecting a font.
310  */
311  template<typename input_iterator,
312  typename output_iterator>
313  void
315  input_iterator character_codes_begin,
316  input_iterator character_codes_end,
317  output_iterator output_begin,
318  uint32_t selection_strategy = 0u);
319 
320  /*!
321  * Fill an array of Glyph values from an array of character code values.
322  * \tparam input_iterator read iterator to type that is castable to uint32_t
323  * \tparam output_iterator write iterator to Glyph
324  * \param h pointer to font from which to fetch the glyph, if the glyph
325  * is not present in the font attempt to get the glyph from
326  * a font of similiar properties
327  * \param character_codes_begin iterator to first character code
328  * \param character_codes_end iterator to one pash last character code
329  * \param output_begin begin iterator to output
330  */
331  template<typename input_iterator,
332  typename output_iterator>
333  void
335  input_iterator character_codes_begin,
336  input_iterator character_codes_end,
337  output_iterator output_begin);
338 
339  private:
340  void
341  lock_mutex(void);
342 
343  void
344  unlock_mutex(void);
345 
347  fetch_glyph_no_lock(FontGroup group, uint32_t character_code,
348  uint32_t selection_strategy);
349 
351  fetch_glyph_no_lock(const FontBase *h,
352  uint32_t character_code,
353  uint32_t selection_strategy);
354 
356  fetch_glyph_no_merging_no_lock(const FontBase *h, uint32_t character_code);
357 
358  void *m_d;
359  };
360 
361  template<typename input_iterator,
362  typename output_iterator>
363  void
366  input_iterator character_codes_begin,
367  input_iterator character_codes_end,
368  output_iterator output_begin,
369  uint32_t selection_strategy)
370  {
371  lock_mutex();
372  for(;character_codes_begin != character_codes_end; ++character_codes_begin, ++output_begin)
373  {
374  uint32_t v;
375  v = static_cast<uint32_t>(*character_codes_begin);
376  *output_begin = fetch_glyph_no_lock(group, v, selection_strategy);
377  }
378  unlock_mutex();
379  }
380 
381  template<typename input_iterator,
382  typename output_iterator>
383  void
386  input_iterator character_codes_begin,
387  input_iterator character_codes_end,
388  output_iterator output_begin,
389  uint32_t selection_strategy)
390  {
391  lock_mutex();
392  for(;character_codes_begin != character_codes_end; ++character_codes_begin, ++output_begin)
393  {
394  uint32_t v;
395  v = static_cast<uint32_t>(*character_codes_begin);
396  *output_begin = fetch_glyph_no_lock(h, v, selection_strategy);
397  }
398  unlock_mutex();
399  }
400 
401  template<typename input_iterator,
402  typename output_iterator>
403  void
406  input_iterator character_codes_begin,
407  input_iterator character_codes_end,
408  output_iterator output_begin)
409  {
410  lock_mutex();
411  for(;character_codes_begin != character_codes_end; ++character_codes_begin, ++output_begin)
412  {
413  uint32_t v;
414  v = static_cast<uint32_t>(*character_codes_begin);
415  *output_begin = fetch_glyph_no_merging_no_lock(h, v);
416  }
417  unlock_mutex();
418  }
419 
420 /*! @} */
421 }
422 
423 #endif
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
void create_glyph_sequence_no_merging(const FontBase *h, input_iterator character_codes_begin, input_iterator character_codes_end, output_iterator output_begin)
virtual reference_counted_ptr< const FontBase > generate_font(void) const =0
FontBase provides an interface for a font to generate glyph rendering data.
Definition: font.hpp:47
A wrapper over a pointer to implement reference counting.
reference_counted_ptr< const FontBase > fetch_font(c_string source_label)
return_code
Enumeration for simple return codes for functions for success or failure.
Definition: util.hpp:142
A FontGeneratorBase is a means to create a font. Adding a font via a FontGenerator allows one to avoi...
Represents defining properties of a font used by FontDatabase to perform font merging.
reference_counted_ptr< const FontBase > fetch_or_generate_font(const reference_counted_ptr< const FontGeneratorBase > &h)
enum return_code add_font(const reference_counted_ptr< const FontBase > &h)
virtual const FontProperties & font_properties(void) const =0
A FontGroup represents a group of fonts which is selected from a FontProperties value. The accessors for FontGroup are methods of FontDatabase::parent_group(FontGroup), FontDatabase::fetch_font(FontGroup, unsigned int) and FontDatabase::number_fonts(FontGroup).
FontGroup fetch_group(const FontProperties &props, uint32_t selection_strategy)
void create_glyph_sequence(FontGroup group, input_iterator character_codes_begin, input_iterator character_codes_end, output_iterator output_begin, uint32_t selection_strategy=0u)
FontGroup parent_group(FontGroup G)
unsigned int number_fonts(FontGroup G)
FontGroup root_group(void)
file glyph_source.hpp
Defines default reference counting base classes.
GlyphSource fetch_glyph_no_merging(const FontBase *h, uint32_t character_code)
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
enum return_code add_font_generator(const reference_counted_ptr< const FontGeneratorBase > &h)
GlyphSource fetch_glyph(const FontProperties &props, uint32_t character_code, uint32_t selection_strategy=0u)
A FontDatabase performs the act of font selection and glyph selection. It uses the values of FontProp...