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#include "font_style.hpp"
9
10namespace hi::inline v1 {
11
18 uint8_t value;
19
20public:
21 constexpr static int max()
22 {
23 return 20;
24 }
25 constexpr static int half()
26 {
27 return max() / 2;
28 }
29
30 constexpr font_variant(font_weight weight, font_style style) noexcept :
31 value(narrow_cast<uint8_t>(static_cast<int>(weight) + (style == font_style::italic ? half() : 0)))
32 {
33 }
34 constexpr font_variant() noexcept : font_variant(font_weight::regular, font_style::normal) {}
35 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, font_style::normal) {}
36 constexpr font_variant(font_style style) noexcept : font_variant(font_weight::regular, style) {}
37
38 [[nodiscard]] size_t hash() const noexcept
39 {
40 return std::hash<uint8_t>{}(value);
41 }
42
43 constexpr font_weight weight() const noexcept
44 {
45 hi_axiom(value < max());
46 return static_cast<font_weight>(value % half());
47 }
48
49 [[nodiscard]] constexpr font_style style() const noexcept
50 {
51 hi_axiom(value < max());
52 return static_cast<font_style>(value >= half());
53 }
54
55 constexpr font_variant& set_weight(font_weight rhs) noexcept
56 {
57 value = narrow_cast<uint8_t>(static_cast<int>(rhs) + (style() == font_style::italic ? half() : 0));
58 hi_axiom(value < max());
59 return *this;
60 }
61
62 constexpr font_variant& set_style(font_style rhs) noexcept
63 {
64 value = narrow_cast<uint8_t>(static_cast<int>(weight()) + (rhs == font_style::italic ? half() : 0));
65 hi_axiom(value < max());
66 return *this;
67 }
68
69 constexpr operator int() const noexcept
70 {
71 hi_axiom(value < max());
72 return value;
73 }
74
78 constexpr font_variant alternative(int i) const noexcept
79 {
80 hi_axiom(i >= 0 && i < max());
81 hilet w = font_weight_alterative(weight(), i % half());
82 hilet it = (style() == font_style::italic) == (i < half());
83 return {w, static_cast<font_style>(it)};
84 }
85
86 [[nodiscard]] friend std::string to_string(font_variant const& rhs) noexcept
87 {
88 return std::format("{}", rhs.weight(), rhs.style() == font_style::italic ? "/italic" : "");
89 }
90
91 friend std::ostream& operator<<(std::ostream& lhs, font_variant const& rhs)
92 {
93 return lhs << to_string(rhs);
94 }
95};
96
97} // namespace hi::inline v1
98
99template<>
100struct std::hash<hi::font_variant> {
101 [[nodiscard]] size_t operator()(hi::font_variant const& rhs) const noexcept
102 {
103 return rhs.hash();
104 }
105};
Defines the `font_style` type.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
font_weight
Definition font_weight.hpp:16
geometry/margins.hpp
Definition cache.hpp:11
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:17
constexpr font_variant alternative(int i) const noexcept
Get an alternative font variant.
Definition font_variant.hpp:78
T operator()(T... args)