Font.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2010 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_FONT_H
32 #define PT_GFX_FONT_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/System/Path.h>
36 #include <Pt/SmartPtr.h>
37 
38 #include <cstddef>
39 #include <string>
40 
41 namespace Pt {
42 
43 namespace Gfx {
44 
45 class FontData;
46 
55 class PT_GFX_API Font
56 {
57  public:
60  enum class Weight
61  {
62  Thin = 100,
63  ExtraLight = 200,
64  Light = 300,
65  Normal = 400,
66  Medium = 500,
67  SemiBold = 600,
68  Bold = 700,
69  ExtraBold = 800,
70  Black = 900
71  };
72 
75  enum class Slant
76  {
77  Normal,
78  Italic,
79  Oblique
80  };
81 
84  enum class Stretch
85  {
86  UltraCondensed = 1,
87  ExtraCondensed = 2,
88  Condensed = 3,
89  SemiCondensed = 4,
90  Normal = 5,
91  SemiExpanded = 6,
92  Expanded = 7,
93  ExtraExpanded = 8,
94  UltraExpanded = 9
95  };
96 
99  enum class Category
100  {
101  None,
102  Serif,
103  SansSerif,
104  Monospace,
105  Cursive,
106  Fantasy
107  };
108 
111  Font();
112 
115  Font(const std::string& family, std::size_t size,
116  const std::string& styleName,
117  Weight weight = Weight::Normal,
118  Slant slant = Slant::Normal,
119  Stretch stretch = Stretch::Normal);
120 
123  Font(const std::string& family, std::size_t size,
124  Weight weight = Weight::Normal,
125  Slant slant = Slant::Normal,
126  Stretch stretch = Stretch::Normal);
127 
130  Font(Category category, std::size_t size,
131  Weight weight = Weight::Normal,
132  Slant slant = Slant::Normal,
133  Stretch stretch = Stretch::Normal);
134 
137  const std::string& family() const;
138 
141  std::size_t size() const;
142 
145  const std::string& styleName() const;
146 
149  bool hasStyleName() const;
150 
153  Weight weight() const;
154 
157  Slant slant() const;
158 
161  Stretch stretch() const;
162 
166 
169  Font withSize(std::size_t size) const;
170 
173  Font withWeight(Weight weight) const;
174 
177  Font withSlant(Slant slant) const;
178 
181  Font withStretch(Stretch stretch) const;
182 
183  public:
186  static void addFonts(const System::Path& path);
187 
190  static bool addFont(const System::Path& path);
191 
194  static bool removeFont(const System::Path& path);
195 
198  static const std::vector<System::Path>& fontFiles();
199 
200  private:
201  SmartPtr<FontData> _fontData;
202 };
203 
204 
208 inline bool operator==(const Font& a, const Font& b)
209 {
210  return a.family() == b.family() &&
211  a.weight() == b.weight() &&
212  a.slant() == b.slant() &&
213  a.stretch() == b.stretch() &&
214  a.category() == b.category() &&
215  a.size() == b.size() &&
216  a.styleName() == b.styleName();
217 }
218 
219 
223 inline bool operator!=(const Font& a, const Font& b)
224 {
225  return a.family() != b.family() ||
226  a.weight() != b.weight() ||
227  a.slant() != b.slant() ||
228  a.stretch() != b.stretch() ||
229  a.category() != b.category() ||
230  a.size() != b.size() ||
231  a.styleName() != b.styleName();
232 }
233 
234 
238 inline bool operator<(const Font& a, const Font& b)
239 {
240  if(a.family() != b.family())
241  return a.family() < b.family();
242 
243  if(a.weight() != b.weight())
244  return a.weight() < b.weight();
245 
246  if(a.slant() != b.slant())
247  return a.slant() < b.slant();
248 
249  if(a.stretch() != b.stretch())
250  return a.stretch() < b.stretch();
251 
252  if(a.category() != b.category())
253  return a.category() < b.category();
254 
255  if(a.size() != b.size())
256  return a.size() < b.size();
257 
258  return a.styleName() < b.styleName();
259 }
260 
261 
262 inline Font Font::withSize(std::size_t size) const
263 {
264  if(category() != Category::None)
265  return Font(category(), size, weight(), slant(), stretch());
266 
267  if(hasStyleName())
268  return Font(family(), size, styleName(), weight(), slant(), stretch());
269 
270  return Font(family(), size, weight(), slant(), stretch());
271 }
272 
273 
274 inline Font Font::withWeight(Weight weight) const
275 {
276  if(category() != Category::None)
277  return Font(category(), size(), weight, slant(), stretch());
278 
279  if(hasStyleName())
280  return Font(family(), size(), styleName(), weight, slant(), stretch());
281 
282  return Font(family(), size(), weight, slant(), stretch());
283 }
284 
285 
286 inline Font Font::withSlant(Slant slant) const
287 {
288  if(category() != Category::None)
289  return Font(category(), size(), weight(), slant, stretch());
290 
291  if(hasStyleName())
292  return Font(family(), size(), styleName(), weight(), slant, stretch());
293 
294  return Font(family(), size(), weight(), slant, stretch());
295 }
296 
297 
298 inline Font Font::withStretch(Stretch stretch) const
299 {
300  if(category() != Category::None)
301  return Font(category(), size(), weight(), slant(), stretch);
302 
303  if(hasStyleName())
304  return Font(family(), size(), styleName(), weight(), slant(), stretch);
305 
306  return Font(family(), size(), weight(), slant(), stretch);
307 }
308 
309 
310 class FontData
311 {
312  public:
313  FontData()
314  : _family()
315  , _size(0)
316  , _styleName()
317  , _weight(Font::Weight::Normal)
318  , _slant(Font::Slant::Normal)
319  , _stretch(Font::Stretch::Normal)
320  , _category(Font::Category::None)
321  {
322  }
323 
324  FontData(const std::string& family, std::size_t size,
325  Font::Weight weight = Font::Weight::Normal,
326  Font::Slant slant = Font::Slant::Normal,
327  Font::Stretch stretch = Font::Stretch::Normal,
328  const std::string& styleName = std::string())
329  : _family(family)
330  , _size(size)
331  , _styleName(styleName)
332  , _weight(weight)
333  , _slant(slant)
334  , _stretch(stretch)
335  , _category(Font::Category::None)
336  {
337  }
338 
339  FontData(Font::Category category, std::size_t size,
340  Font::Weight weight = Font::Weight::Normal,
341  Font::Slant slant = Font::Slant::Normal,
342  Font::Stretch stretch = Font::Stretch::Normal)
343  : _family()
344  , _size(size)
345  , _styleName()
346  , _weight(weight)
347  , _slant(slant)
348  , _stretch(stretch)
349  , _category(category)
350  {
351  }
352 
353  FontData(const std::string& family, const FontData& font)
354  : _family(family)
355  , _size(font._size)
356  , _styleName(font._styleName)
357  , _weight(font._weight)
358  , _slant(font._slant)
359  , _stretch(font._stretch)
360  , _category(font._category)
361  {
362  }
363 
364  const std::string& family() const
365  {
366  return _family;
367  }
368 
369  std::size_t size() const
370  {
371  return _size;
372  }
373 
374  const std::string& styleName() const
375  {
376  return _styleName;
377  }
378 
379  bool hasStyleName() const
380  {
381  return ! _styleName.empty();
382  }
383 
384  Font::Weight weight() const
385  {
386  return _weight;
387  }
388 
389  Font::Slant slant() const
390  {
391  return _slant;
392  }
393 
394  Font::Stretch stretch() const
395  {
396  return _stretch;
397  }
398 
399  Font::Category category() const
400  {
401  return _category;
402  }
403 
404  private:
405  std::string _family;
406  std::size_t _size;
407  std::string _styleName;
408  Font::Weight _weight;
409  Font::Slant _slant;
410  Font::Stretch _stretch;
411  Font::Category _category;
412 };
413 
414 } //namespace
415 
416 } //namespace
417 
418 #endif
Core module.
Definition: pt-gfx-images.dox:14
Category category() const
Returns the generic font category.
Slant slant() const
Returns the slant of the font request.
Slant
Describes the requested font slant.
Definition: Font.h:76
bool operator==(const Font &a, const Font &b)
Returns true if two font requests are equal.
Definition: Font.h:208
Stretch stretch() const
Returns the stretch of the font request.
Font()
Default constructor.
Weight weight() const
Returns the weight of the font request.
static bool addFont(const System::Path &path)
Adds one font file.
bool operator<(const Font &a, const Font &b)
Returns true if one font request sorts before another.
Definition: Font.h:238
Font withSize(std::size_t size) const
Returns a copy of this font with a different size.
Definition: Font.h:262
Weight
Describes the requested font weight.
Definition: Font.h:61
std::size_t size() const
Returns the size of the font.
static const std::vector< System::Path > & fontFiles()
Returns the registered font files.
const std::string & family() const
Returns the family of the font.
Font(const std::string &family, std::size_t size, Weight weight=Weight::Normal, Slant slant=Slant::Normal, Stretch stretch=Stretch::Normal)
Construct a font with explicit weight and slant.
const std::string & styleName() const
Returns the optional exact style name hint.
static void addFonts(const System::Path &path)
Adds all fonts found at the given path.
Category
Describes a generic fallback font category.
Definition: Font.h:100
Font request used for text drawing and measurement.
Definition: Font.h:56
Represents a path in the file-system.
Definition: Path.h:48
bool operator!=(const Font &a, const Font &b)
Returns true if two font requests are different.
Definition: Font.h:223
Font withSlant(Slant slant) const
Returns a copy of this font with a different slant.
Definition: Font.h:286
Font withWeight(Weight weight) const
Returns a copy of this font with a different weight.
Definition: Font.h:274
Font withStretch(Stretch stretch) const
Returns a copy of this font with a different stretch.
Definition: Font.h:298
static bool removeFont(const System::Path &path)
Removes one font file.
Font(Category category, std::size_t size, Weight weight=Weight::Normal, Slant slant=Slant::Normal, Stretch stretch=Stretch::Normal)
Construct a font from a generic category.
Stretch
Describes the requested font stretch.
Definition: Font.h:85
bool hasStyleName() const
Returns true if an exact style name hint is set.
Font(const std::string &family, std::size_t size, const std::string &styleName, Weight weight=Weight::Normal, Slant slant=Slant::Normal, Stretch stretch=Stretch::Normal)
Construct a font with an exact style name hint.