HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_variant.hpp
1// Copyright Take Vos 2020-2022.
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 [[nodiscard]] size_t hash() const noexcept
38 {
39 return std::hash<uint8_t>{}(value);
40 }
41
42 constexpr font_weight weight() const noexcept
43 {
44 hi_axiom(value < max());
45 return static_cast<font_weight>(value % half());
46 }
47
48 [[nodiscard]] constexpr bool italic() const noexcept
49 {
50 hi_axiom(value < max());
51 return value >= half();
52 }
53
54 constexpr font_variant &set_weight(font_weight rhs) noexcept
55 {
56 value = static_cast<uint8_t>(static_cast<int>(rhs) + (italic() ? half() : 0));
57 hi_axiom(value < max());
58 return *this;
59 }
60
61 constexpr font_variant &set_italic(bool rhs) noexcept
62 {
63 value = static_cast<uint8_t>(static_cast<int>(weight()) + (rhs ? half() : 0));
64 hi_axiom(value < max());
65 return *this;
66 }
67
68 constexpr operator int() const noexcept
69 {
70 hi_axiom(value < max());
71 return value;
72 }
73
77 constexpr font_variant alternative(int i) const noexcept
78 {
79 hi_axiom(i >= 0 && i < max());
80 hilet w = font_weight_alterative(weight(), i % half());
81 hilet it = italic() == (i < half());
82 return {w, it};
83 }
84
85 [[nodiscard]] friend std::string to_string(font_variant const &rhs) noexcept
86 {
87 return std::format("{}", rhs.weight(), rhs.italic() ? "/italic" : "");
88 }
89
90 friend std::ostream &operator<<(std::ostream &lhs, font_variant const &rhs)
91 {
92 return lhs << to_string(rhs);
93 }
94};
95
96} // namespace hi::inline v1
97
98template<>
99struct std::hash<hi::font_variant> {
100 [[nodiscard]] size_t operator()(hi::font_variant const &rhs) const noexcept
101 {
102 return rhs.hash();
103 }
104};
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
font_weight
Definition font_weight.hpp:17
The HikoGUI namespace.
Definition ascii.hpp:19
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:77
T operator()(T... args)