1.11
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/geometry.h>
16#include <egt/serialize.h>
17#include <egt/types.h>
18#include <iosfwd>
19#include <map>
20#include <string>
21
22namespace egt
23{
24inline namespace v1
25{
26
27namespace detail
28{
29class InternalFont;
30}
31
40class EGT_API Font
41{
42public:
43
44 using Extent = float;
45
57
70
72 using Size = float;
73
77 enum class Weight
78 {
79 normal = 0,
80 bold = 1,
81 };
82
86 enum class Slant
87 {
88 normal = 0,
89 italic = 1,
90 oblique = 2,
91 };
92
94 static constexpr const char* DEFAULT_FACE = "Free Sans";
96 static constexpr Font::Weight DEFAULT_WEIGHT = Font::Weight::normal;
98 static constexpr Font::Size DEFAULT_SIZE = 18.f;
100 static constexpr Font::Slant DEFAULT_SLANT = Font::Slant::normal;
101
103
109 explicit Font(const std::string& face);
110
118 explicit Font(const unsigned char* data, size_t len,
119 Font::Size size);
120
129 explicit Font(const std::string& face, Font::Size size,
130 Font::Weight weight = Weight::normal,
131 Font::Slant slant = Slant::normal);
132
139 explicit Font(Font::Size size);
140
148 explicit Font(Font::Size size, Font::Weight weight);
149
156 explicit Font(Font::Weight weight);
157
164 explicit Font(Font::Slant slant);
165
169 EGT_NODISCARD const std::string& face() const { return m_face; }
170
174 void face(const std::string& face) { m_face = face; direct_allocate(); }
175
179 EGT_NODISCARD Font::Size size() const { return m_size; }
180
184 void size(const Font::Size& s) { m_use_default_size = false; m_size = s; direct_allocate(); }
185
189 EGT_NODISCARD Font::Weight weight() const { return m_weight; }
190
194 void weight(Font::Weight w) { m_weight = w; direct_allocate(); }
195
199 EGT_NODISCARD Font::Slant slant() const { return m_slant; }
200
204 void slant(Font::Slant s) { m_slant = s; }
205
215 EGT_NODISCARD Font::FontExtents extents() const;
216
229 EGT_NODISCARD Font::TextExtents extents(const std::string& text) const;
230
244 EGT_NODISCARD egt::Size text_size(const std::string& text) const;
245
252 EGT_NODISCARD const detail::InternalFont& scaled_font() const;
253
257 void serialize(const std::string& name, Serializer& serializer) const;
258
262 void deserialize(const std::string& name, const std::string& value,
263 const Serializer::Attributes& attrs);
264
273
279 static void reset_font_cache();
280
290 static void shutdown_fonts();
291
292protected:
293
295
297 std::string m_face{DEFAULT_FACE};
298
301
303 Font::Weight m_weight{DEFAULT_WEIGHT};
304
306 Font::Slant m_slant{DEFAULT_SLANT};
307
309 mutable std::shared_ptr<detail::InternalFont> m_scaled_font;
310 const unsigned char* m_data{nullptr};
311 size_t m_len{0};
312
314 bool m_use_default_size{true};
315
316private:
317 Font::Size default_font_size();
318};
319
321static_assert(detail::rule_of_5<Font>(), "must fulfill rule of 5");
322
324EGT_API std::ostream& operator<<(std::ostream& os, const Font& font);
325
327inline bool EGT_API operator==(const Font& lhs, const Font& rhs)
328{
329 return lhs.face() == rhs.face() &&
330 detail::float_equal(lhs.size(), rhs.size()) &&
331 lhs.weight() == rhs.weight() &&
332 lhs.slant() == rhs.slant();
333}
334
336inline bool EGT_API operator!=(const Font& lhs, const Font& rhs)
337{
338 return !(lhs == rhs);
339}
340
346EGT_API const Font* global_font();
347
351EGT_API void global_font(std::unique_ptr<Font>&& font);
352
356EGT_API void reset_global_font();
357
358}
359}
360
361#endif
Manages a font and properties of a font.
Definition font.h:41
EGT_NODISCARD Font::Slant slant() const
Get the slant of the font.
Definition font.h:199
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:184
EGT_NODISCARD Font::FontExtents extents() const
Get the font extents based on a default context.
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.
EGT_NODISCARD egt::Size text_size(const std::string &text) const
Get the size of a rectangle containing the text, based on a default context.
Font(Font::Slant slant)
Create a font based on the global default font, but with the specified slant.
Extent ascent
Definition font.h:51
void face(const std::string &face)
Set the face of the font.
Definition font.h:174
Extent descent
Definition font.h:52
void deserialize(const std::string &name, const std::string &value, const Serializer::Attributes &attrs)
Deserialize.
Font::Size m_size
Font size.
Definition font.h:300
EGT_NODISCARD Font::Size size() const
Get the size of the font.
Definition font.h:179
Extent max_y_advance
Definition font.h:55
void serialize(const std::string &name, Serializer &serializer) const
Serialize to the specified serializer.
Extent y_advance
Definition font.h:68
Extent x_advance
Definition font.h:67
float Extent
Definition font.h:44
EGT_NODISCARD Font::Weight weight() const
Get the weight of the font.
Definition font.h:189
static void shutdown_fonts()
Basically, this will clear the font cache and shutdown FontConfig which will release all memory alloc...
EGT_NODISCARD const detail::InternalFont & scaled_font() const
Generates a FontConfig scaled font instance.
void direct_allocate()
Font(Font::Size size)
Create a font based on the global default font, but with the specified size.
Extent x_bearing
Definition font.h:63
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:87
float Size
Scalar used for font size.
Definition font.h:72
Extent y_bearing
Definition font.h:64
Extent height
Definition font.h:53
EGT_NODISCARD Font::TextExtents extents(const std::string &text) const
Get the text extents based on a default context.
Weight
Font weights.
Definition font.h:78
void weight(Font::Weight w)
Set the weight of the font.
Definition font.h:194
Extent width
Definition font.h:65
Extent max_x_advance
Definition font.h:54
void slant(Font::Slant s)
Set the slant of the font.
Definition font.h:204
std::shared_ptr< detail::InternalFont > m_scaled_font
Only used when an in-memory font is created.
Definition font.h:309
EGT_NODISCARD const std::string & face() const
Get the face name the font.
Definition font.h:169
Font extent.
Definition font.h:50
Text extent.
Definition font.h:62
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
EGT framework namespace.
Definition animation.h:24