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
41namespace Pt {
42
43namespace Gfx {
44
45class FontData;
46
62class PT_GFX_API Font
63{
64 public:
67 enum class Weight
68 {
69 Thin = 100,
70 ExtraLight = 200,
71 Light = 300,
72 Normal = 400,
73 Medium = 500,
74 SemiBold = 600,
75 Bold = 700,
76 ExtraBold = 800,
77 Black = 900
78 };
79
82 enum class Slant
83 {
84 Normal,
85 Italic,
86 Oblique
87 };
88
91 enum class Stretch
92 {
93 UltraCondensed = 1,
94 ExtraCondensed = 2,
95 Condensed = 3,
96 SemiCondensed = 4,
97 Normal = 5,
98 SemiExpanded = 6,
99 Expanded = 7,
100 ExtraExpanded = 8,
101 UltraExpanded = 9
102 };
103
106 enum class Category
107 {
108 None,
109 Serif,
110 SansSerif,
111 Monospace,
112 Cursive,
113 Fantasy
114 };
115
119
122 Font(const std::string& family, std::size_t size,
123 const std::string& styleName,
124 Weight weight = Weight::Normal,
125 Slant slant = Slant::Normal,
126 Stretch stretch = Stretch::Normal);
127
130 Font(const std::string& family, std::size_t size,
131 Weight weight = Weight::Normal,
132 Slant slant = Slant::Normal,
133 Stretch stretch = Stretch::Normal);
134
138 Weight weight = Weight::Normal,
139 Slant slant = Slant::Normal,
140 Stretch stretch = Stretch::Normal);
141
144 const std::string& family() const;
145
148 std::size_t size() const;
149
152 const std::string& styleName() const;
153
156 bool hasStyleName() const;
157
160 Weight weight() const;
161
164 Slant slant() const;
165
169
173
176 Font withSize(std::size_t size) const;
177
181
184 Font withSlant(Slant slant) const;
185
189
190 public:
193 static void addFonts(const System::Path& path);
194
197 static bool addFont(const System::Path& path);
198
201 static bool removeFont(const System::Path& path);
202
205 static const std::vector<System::Path>& fontFiles();
206
207 private:
208 SmartPtr<FontData> _fontData;
209};
210
211
215inline bool operator==(const Font& a, const Font& b)
216{
217 return a.family() == b.family() &&
218 a.weight() == b.weight() &&
219 a.slant() == b.slant() &&
220 a.stretch() == b.stretch() &&
221 a.category() == b.category() &&
222 a.size() == b.size() &&
223 a.styleName() == b.styleName();
224}
225
226
230inline bool operator!=(const Font& a, const Font& b)
231{
232 return a.family() != b.family() ||
233 a.weight() != b.weight() ||
234 a.slant() != b.slant() ||
235 a.stretch() != b.stretch() ||
236 a.category() != b.category() ||
237 a.size() != b.size() ||
238 a.styleName() != b.styleName();
239}
240
241
245inline bool operator<(const Font& a, const Font& b)
246{
247 if(a.family() != b.family())
248 return a.family() < b.family();
249
250 if(a.weight() != b.weight())
251 return a.weight() < b.weight();
252
253 if(a.slant() != b.slant())
254 return a.slant() < b.slant();
255
256 if(a.stretch() != b.stretch())
257 return a.stretch() < b.stretch();
258
259 if(a.category() != b.category())
260 return a.category() < b.category();
261
262 if(a.size() != b.size())
263 return a.size() < b.size();
264
265 return a.styleName() < b.styleName();
266}
267
268
269inline Font Font::withSize(std::size_t size) const
270{
271 if(category() != Category::None)
272 return Font(category(), size, weight(), slant(), stretch());
273
274 if(hasStyleName())
275 return Font(family(), size, styleName(), weight(), slant(), stretch());
276
277 return Font(family(), size, weight(), slant(), stretch());
278}
279
280
282{
283 if(category() != Category::None)
284 return Font(category(), size(), weight, slant(), stretch());
285
286 if(hasStyleName())
287 return Font(family(), size(), styleName(), weight, slant(), stretch());
288
289 return Font(family(), size(), weight, slant(), stretch());
290}
291
292
294{
295 if(category() != Category::None)
296 return Font(category(), size(), weight(), slant, stretch());
297
298 if(hasStyleName())
299 return Font(family(), size(), styleName(), weight(), slant, stretch());
300
301 return Font(family(), size(), weight(), slant, stretch());
302}
303
304
306{
307 if(category() != Category::None)
308 return Font(category(), size(), weight(), slant(), stretch);
309
310 if(hasStyleName())
311 return Font(family(), size(), styleName(), weight(), slant(), stretch);
312
313 return Font(family(), size(), weight(), slant(), stretch);
314}
315
316
317class FontData
318{
319 public:
320 FontData()
321 : _family()
322 , _size(0)
323 , _styleName()
324 , _weight(Font::Weight::Normal)
325 , _slant(Font::Slant::Normal)
326 , _stretch(Font::Stretch::Normal)
327 , _category(Font::Category::None)
328 {
329 }
330
331 FontData(const std::string& family, std::size_t size,
332 Font::Weight weight = Font::Weight::Normal,
333 Font::Slant slant = Font::Slant::Normal,
334 Font::Stretch stretch = Font::Stretch::Normal,
335 const std::string& styleName = std::string())
336 : _family(family)
337 , _size(size)
338 , _styleName(styleName)
339 , _weight(weight)
340 , _slant(slant)
341 , _stretch(stretch)
342 , _category(Font::Category::None)
343 {
344 }
345
346 FontData(Font::Category category, std::size_t size,
347 Font::Weight weight = Font::Weight::Normal,
348 Font::Slant slant = Font::Slant::Normal,
349 Font::Stretch stretch = Font::Stretch::Normal)
350 : _family()
351 , _size(size)
352 , _styleName()
353 , _weight(weight)
354 , _slant(slant)
355 , _stretch(stretch)
356 , _category(category)
357 {
358 }
359
360 FontData(const std::string& family, const FontData& font)
361 : _family(family)
362 , _size(font._size)
363 , _styleName(font._styleName)
364 , _weight(font._weight)
365 , _slant(font._slant)
366 , _stretch(font._stretch)
367 , _category(font._category)
368 {
369 }
370
371 const std::string& family() const
372 {
373 return _family;
374 }
375
376 std::size_t size() const
377 {
378 return _size;
379 }
380
381 const std::string& styleName() const
382 {
383 return _styleName;
384 }
385
386 bool hasStyleName() const
387 {
388 return ! _styleName.empty();
389 }
390
391 Font::Weight weight() const
392 {
393 return _weight;
394 }
395
396 Font::Slant slant() const
397 {
398 return _slant;
399 }
400
401 Font::Stretch stretch() const
402 {
403 return _stretch;
404 }
405
406 Font::Category category() const
407 {
408 return _category;
409 }
410
411 private:
412 std::string _family;
413 std::size_t _size;
414 std::string _styleName;
415 Font::Weight _weight;
416 Font::Slant _slant;
417 Font::Stretch _stretch;
418 Font::Category _category;
419};
420
421} //namespace
422
423} //namespace
424
425#endif
Font family, size and style.
Definition Font.h:63
const std::string & family() const
Returns the family of the font.
Slant slant() const
Returns the slant of the font request.
Font withSlant(Slant slant) const
Returns a copy of this font with a different slant.
Definition Font.h:293
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.
static void addFonts(const System::Path &path)
Adds all fonts found at the given path.
static const std::vector< System::Path > & fontFiles()
Returns the registered font files.
bool operator==(const Font &a, const Font &b)
Returns true if two font requests are equal.
Definition Font.h:215
bool operator<(const Font &a, const Font &b)
Returns true if one font request sorts before another.
Definition Font.h:245
Weight weight() const
Returns the weight of the font request.
Font withStretch(Stretch stretch) const
Returns a copy of this font with a different stretch.
Definition Font.h:305
Category category() const
Returns the generic font category.
const std::string & styleName() const
Returns the optional exact style name hint.
Font()
Default constructor.
static bool addFont(const System::Path &path)
Adds one font file.
Font withWeight(Weight weight) const
Returns a copy of this font with a different weight.
Definition Font.h:281
Category
Describes a generic fallback font category.
Definition Font.h:107
Stretch
Describes the requested font stretch.
Definition Font.h:92
Slant
Describes the requested font slant.
Definition Font.h:83
bool operator!=(const Font &a, const Font &b)
Returns true if two font requests are different.
Definition Font.h:230
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.
static bool removeFont(const System::Path &path)
Removes one font file.
Weight
Describes the requested font weight.
Definition Font.h:68
std::size_t size() const
Returns the size 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.
bool hasStyleName() const
Returns true if an exact style name hint is set.
Stretch stretch() const
Returns the stretch of the font request.
Font withSize(std::size_t size) const
Returns a copy of this font with a different size.
Definition Font.h:269
Policy based smart pointer.
Definition SmartPtr.h:462
Represents a path in the file-system.
Definition Path.h:48
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33