neoGFX
C++ GPU-oriented GUI library and app/game creation framework.
font.hpp
Go to the documentation of this file.
1 // font.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2015 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <neogfx/neogfx.hpp>
23 #include <neolib/variant.hpp>
25 
26 namespace neogfx
27 {
28  class i_native_font_face;
29  class glyph;
30  class i_glyph_texture;
31 
32  class font_info
33  {
34  // exceptions
35  public:
36  struct unknown_style : std::logic_error { unknown_style() : std::logic_error("neogfx::font_info::unknown_style") {} };
37  struct unknown_style_name : std::logic_error { unknown_style_name() : std::logic_error("neogfx::font_info::unknown_style_name") {} };
38  // types
39  public:
40  enum style_e
41  {
42  Invalid = 0x00,
43  Normal = 0x01,
44  Bold = 0x02,
45  Italic = 0x04,
46  Underline = 0x08,
51  };
52  enum weight_e
53  {
54  WeightThin = 100,
57  WeightLight = 300,
58  WeightNormal = 400,
60  WeightMedium = 500,
63  WeightBold = 700,
66  WeightHeavy = 900,
68  };
69  typedef double point_size;
70  private:
71  typedef boost::optional<style_e> optional_style;
72  typedef boost::optional<std::string> optional_style_name;
73  private:
74  class instance;
75  public:
76  font_info();
77  font_info(const std::string& aFamilyName, style_e aStyle, point_size aSize);
78  font_info(const std::string& aFamilyName, const std::string& aStyleName, point_size aSize);
79  font_info(const std::string& aFamilyName, style_e aStyle, const std::string& aStyleName, point_size aSize);
80  font_info(const font_info& aOther);
81  virtual ~font_info();
82  font_info& operator=(const font_info& aOther);
83  private:
84  font_info(const std::string& aFamilyName, const optional_style& aStyle, const optional_style_name& aStyleName, point_size aSize);
85  public:
86  virtual const std::string& family_name() const;
87  virtual bool style_available() const;
88  virtual style_e style() const;
89  virtual bool style_name_available() const;
90  virtual const std::string& style_name() const;
91  virtual bool underline() const;
92  virtual void set_underline(bool aUnderline);
93  virtual weight_e weight() const;
94  virtual point_size size() const;
95  virtual bool kerning() const;
96  virtual void enable_kerning();
97  virtual void disable_kerning();
98  public:
99  font_info with_style(style_e aStyle) const;
100  font_info with_size(point_size aSize) const;
101  public:
102  bool operator==(const font_info& aRhs) const;
103  bool operator!=(const font_info& aRhs) const;
104  bool operator<(const font_info& aRhs) const;
105  public:
107  static weight_e weight_from_style_name(std::string aStyleName);
108  private:
109  mutable std::shared_ptr<instance> iInstance;
110  };
111 
112  class font : public font_info
113  {
114  friend class graphics_context;
115  // exceptions
116  public:
117  struct no_fallback_font : std::logic_error { no_fallback_font() : std::logic_error("neogfx::font::no_fallback_font") {} };
118  // types
119  public:
120  typedef uint16_t token;
122  {
123  public:
124  scoped_token();
125  scoped_token(token aToken);
126  scoped_token(const font& aFont);
127  scoped_token(const scoped_token& aOther);
128  ~scoped_token();
129  public:
130  scoped_token& operator=(const scoped_token& aOther);
131  public:
132  token operator*() const
133  {
134  return get();
135  }
136  bool operator==(const scoped_token& aOther) const
137  {
138  return get() == aOther.get();
139  }
140  public:
141  token get() const
142  {
143  return iToken;
144  }
145  private:
146  void add_ref();
147  void release();
148  private:
149  token iToken;
150  };
151  private:
152  class instance;
153  // construction
154  public:
155  font();
156  font(const std::string& aFamilyName, style_e aStyle, point_size aSize);
157  font(const std::string& aFamilyName, const std::string& aStyleName, point_size aSize);
158  font(const font_info& aFontInfo);
159  font(const font& aOther);
160  font(const font& aOther, style_e aStyle, point_size aSize);
161  font(const font& aOther, const std::string& aStyleName, point_size aSize);
162  static font load_from_file(const std::string& aFileName);
163  static font load_from_file(const std::string& aFileName, style_e aStyle, point_size aSize);
164  static font load_from_file(const std::string& aFileName, const std::string& aStyleName, point_size aSize);
165  static font load_from_memory(const void* aData, std::size_t aSizeInBytes);
166  static font load_from_memory(const void* aData, std::size_t aSizeInBytes, style_e aStyle, point_size aSize);
167  static font load_from_memory(const void* aData, std::size_t aSizeInBytes, const std::string& aStyleName, point_size aSize);
168  ~font();
169  font& operator=(const font& aOther);
170  private:
171  font(std::unique_ptr<i_native_font_face> aNativeFontFace);
172  font(std::unique_ptr<i_native_font_face> aNativeFontFace, style_e aStyle);
173  public:
174  bool has_fallback() const;
175  font fallback() const;
176  // operations
177  public:
178  const std::string& family_name() const override;
179  style_e style() const override;
180  const std::string& style_name() const override;
181  point_size size() const override;
182  dimension height() const;
183  dimension descender() const;
184  dimension line_spacing() const;
185  using font_info::kerning;
186  dimension kerning(uint32_t aLeftGlyphIndex, uint32_t aRightGlyphIndex) const;
187  bool is_bitmap_font() const;
188  uint32_t num_fixed_sizes() const;
189  point_size fixed_size(uint32_t aFixedSizeIndex) const;
190  public:
191  token get_token() const;
192  void return_token(token aToken) const;
193  static const font& from_token(token aToken);
194  public:
195  const i_glyph_texture& glyph_texture(const glyph& aGlyph) const;
196  public:
197  bool operator==(const font& aRhs) const;
198  bool operator!=(const font& aRhs) const;
199  bool operator<(const font& aRhs) const;
200  public:
201  i_native_font_face& native_font_face() const;
202  // attributes
203  private:
204  mutable std::shared_ptr<instance> iInstance;
205  };
206 
207  typedef boost::optional<font> optional_font;
208 }
virtual style_e style() const
bool operator==(const font_info &aRhs) const
virtual bool kerning() const
virtual void set_underline(bool aUnderline)
virtual void enable_kerning()
bool operator==(const scoped_token &aOther) const
Definition: font.hpp:136
coordinate_value_type dimension
Definition: geometrical.hpp:33
virtual bool style_name_available() const
font_info & operator=(const font_info &aOther)
virtual const std::string & family_name() const
token operator*() const
Definition: font.hpp:132
static weight_e weight_from_style(font_info::style_e aStyle)
virtual point_size size() const
double point_size
Definition: font.hpp:69
token get() const
Definition: font.hpp:141
virtual void disable_kerning()
font_info with_size(point_size aSize) const
std::string string
Definition: glyph.hpp:31
boost::optional< font > optional_font
Definition: font.hpp:207
virtual bool underline() const
uint16_t token
Definition: font.hpp:120
virtual ~font_info()
font_info with_style(style_e aStyle) const
virtual weight_e weight() const
static weight_e weight_from_style_name(std::string aStyleName)
bool operator<(const font_info &aRhs) const
virtual const std::string & style_name() const
bool operator!=(const font_info &aRhs) const
virtual bool style_available() const