HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_variant.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "font_weight.hpp"
8
9namespace hi::inline v1 {
10
17 uint8_t value;
18
19public:
20 constexpr static int max()
21 {
22 return 20;
23 }
24 constexpr static int half()
25 {
26 return max() / 2;
27 }
28
29 constexpr font_variant(font_weight weight, bool italic) noexcept :
30 value(static_cast<uint8_t>(static_cast<int>(weight) + (italic ? half() : 0)))
31 {
32 }
33 constexpr font_variant() noexcept : font_variant(font_weight::Regular, false) {}
34 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, false) {}
35 constexpr font_variant(bool italic) noexcept : font_variant(font_weight::Regular, italic) {}
36
37 constexpr font_weight weight() const noexcept
38 {
39 hi_axiom(value < max());
40 return static_cast<font_weight>(value % half());
41 }
42
43 [[nodiscard]] constexpr bool italic() const noexcept
44 {
45 hi_axiom(value < max());
46 return value >= half();
47 }
48
49 constexpr font_variant &set_weight(font_weight rhs) noexcept
50 {
51 value = static_cast<uint8_t>(static_cast<int>(rhs) + (italic() ? half() : 0));
52 hi_axiom(value < max());
53 return *this;
54 }
55
56 constexpr font_variant &set_italic(bool rhs) noexcept
57 {
58 value = static_cast<uint8_t>(static_cast<int>(weight()) + (rhs ? half() : 0));
59 hi_axiom(value < max());
60 return *this;
61 }
62
63 constexpr operator int() const noexcept
64 {
65 hi_axiom(value < max());
66 return value;
67 }
68
72 constexpr font_variant alternative(int i) const noexcept
73 {
74 hi_axiom(i >= 0 && i < max());
75 hilet w = font_weight_alterative(weight(), i % half());
76 hilet it = italic() == (i < half());
77 return {w, it};
78 }
79
80 [[nodiscard]] friend std::string to_string(font_variant const &rhs) noexcept
81 {
82 return std::format("{}", rhs.weight(), rhs.italic() ? "/italic" : "");
83 }
84
85 friend std::ostream &operator<<(std::ostream &lhs, font_variant const &rhs)
86 {
87 return lhs << to_string(rhs);
88 }
89};
90
91} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:16
constexpr font_variant alternative(int i) const noexcept
Get an alternative font variant.
Definition font_variant.hpp:72