1.10
font.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_FONT_H
7#define EGT_FONT_H
8
14#include <egt/detail/math.h>
15#include <egt/serialize.h>
16#include <egt/types.h>
17#include <iosfwd>
18#include <map>
19#include <string>
20
21namespace egt
22{
23inline namespace v1
24{
25
34class EGT_API Font
35{
36public:
37
39 using Size = float;
40
44 enum class Weight
45 {
46 normal = 0,
47 bold = 1,
48 };
49
53 enum class Slant
54 {
55 normal = 0,
56 italic = 1,
57 oblique = 2,
58 };
59
61 static constexpr const char* DEFAULT_FACE = "Free Sans";
63 static constexpr Font::Weight DEFAULT_WEIGHT = Font::Weight::normal;
65 static constexpr Font::Size DEFAULT_SIZE = 18.f;
67 static constexpr Font::Slant DEFAULT_SLANT = Font::Slant::normal;
68
70
76 explicit Font(const std::string& face);
77
85 explicit Font(const unsigned char* data, size_t len,
86 Font::Size size);
87
96 explicit Font(const std::string& face, Font::Size size,
97 Font::Weight weight = Weight::normal,
98 Font::Slant slant = Slant::normal);
99
106 explicit Font(Font::Size size);
107
115 explicit Font(Font::Size size, Font::Weight weight);
116
123 explicit Font(Font::Weight weight);
124
131 explicit Font(Font::Slant slant);
132
136 EGT_NODISCARD const std::string& face() const { return m_face; }
137
141 void face(const std::string& face) { m_face = face; direct_allocate(); }
142
146 EGT_NODISCARD Font::Size size() const { return m_size; }
147
151 void size(const Font::Size& s) { m_use_default_size = false; m_size = s; direct_allocate(); }
152
156 EGT_NODISCARD Font::Weight weight() const { return m_weight; }
157
161 void weight(Font::Weight w) { m_weight = w; direct_allocate(); }
162
166 EGT_NODISCARD Font::Slant slant() const { return m_slant; }
167
171 void slant(Font::Slant s) { m_slant = s; }
172
179 EGT_NODISCARD cairo_scaled_font_t* scaled_font() const;
180
184 void serialize(const std::string& name, Serializer& serializer) const;
185
189 void deserialize(const std::string& name, const std::string& value,
190 const Serializer::Attributes& attrs);
191
200
206 static void reset_font_cache();
207
217 static void shutdown_fonts();
218
219protected:
220
222
224 std::string m_face{DEFAULT_FACE};
225
228
230 Font::Weight m_weight{DEFAULT_WEIGHT};
231
233 Font::Slant m_slant{DEFAULT_SLANT};
234
237 const unsigned char* m_data{nullptr};
238 size_t m_len{0};
239
241 bool m_use_default_size{true};
242
243private:
244 Font::Size default_font_size();
245};
246
248static_assert(detail::rule_of_5<Font>(), "must fulfill rule of 5");
249
251EGT_API std::ostream& operator<<(std::ostream& os, const Font& font);
252
254inline bool EGT_API operator==(const Font& lhs, const Font& rhs)
255{
256 return lhs.face() == rhs.face() &&
257 detail::float_equal(lhs.size(), rhs.size()) &&
258 lhs.weight() == rhs.weight() &&
259 lhs.slant() == rhs.slant();
260}
261
263inline bool EGT_API operator!=(const Font& lhs, const Font& rhs)
264{
265 return !(lhs == rhs);
266}
267
273EGT_API const Font* global_font();
274
278EGT_API void global_font(std::unique_ptr<Font>&& font);
279
283EGT_API void reset_global_font();
284
285}
286}
287
288#endif
Manages a font and properties of a font.
Definition font.h:35
EGT_NODISCARD Font::Slant slant() const
Get the slant of the font.
Definition font.h:166
Font(const std::string &face)
Create a font based on the supplied parameters.
Font(const unsigned char *data, size_t len, Font::Size size)
Create a font from an in-memory font.
Font(Font::Weight weight)
Create a font based on the global default font, but with the specified weight.
void size(const Font::Size &s)
Set the size of the font.
Definition font.h:151
static void reset_font_cache()
Clears any internal font cache.
Font(Font::Size size, Font::Weight weight)
Create a font based on the global default font, but with the specified size and weight.
Font(Font::Slant slant)
Create a font based on the global default font, but with the specified slant.
void face(const std::string &face)
Set the face of the font.
Definition font.h:141
void deserialize(const std::string &name, const std::string &value, const Serializer::Attributes &attrs)
Deserialize.
Font::Size m_size
Font size.
Definition font.h:227
EGT_NODISCARD Font::Size size() const
Get the size of the font.
Definition font.h:146
void serialize(const std::string &name, Serializer &serializer) const
Serialize to the specified serializer.
EGT_NODISCARD cairo_scaled_font_t * scaled_font() const
Generates a FontConfig scaled font instance.
EGT_NODISCARD Font::Weight weight() const
Get the weight of the font.
Definition font.h:156
static void shutdown_fonts()
Basically, this will clear the font cache and shutdown FontConfig which will release all memory alloc...
void direct_allocate()
Font(Font::Size size)
Create a font based on the global default font, but with the specified size.
Font(const std::string &face, Font::Size size, Font::Weight weight=Weight::normal, Font::Slant slant=Slant::normal)
Create a font based on the supplied parameters.
void on_screen_resized()
Recompute the font size from the screen size but only if it has not been defined by the user.
Slant
Font slants.
Definition font.h:54
float Size
Scalar used for font size.
Definition font.h:39
Weight
Font weights.
Definition font.h:45
void weight(Font::Weight w)
Set the weight of the font.
Definition font.h:161
void slant(Font::Slant s)
Set the slant of the font.
Definition font.h:171
shared_cairo_scaled_font_t m_scaled_font
Only used when an in-memory font is created.
Definition font.h:236
EGT_NODISCARD const std::string & face() const
Get the face name the font.
Definition font.h:136
Abstract base serializer class.
Definition serialize.h:34
std::list< std::pair< std::string, std::string > > Attributes
Attributes array type.
Definition serialize.h:45
constexpr bool float_equal(const float f1, const float f2)
Safe equal comparison of float values.
Definition math.h:107
constexpr bool operator!=(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:509
EGT_API void reset_global_font()
Reset global Font.
EGT_API const Font * global_font()
Get the global Font.
EGT_API std::ostream & operator<<(std::ostream &os, const Color &color)
Overloaded std::ostream insertion operator.
constexpr bool operator==(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:500
std::shared_ptr< cairo_scaled_font_t > shared_cairo_scaled_font_t
Shared pointer for a cairo font.
Definition types.h:47
EGT framework namespace.
Definition animation.h:24