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 tt {
10
17 uint8_t value;
18
19public:
20 constexpr static int max() { return 20; }
21 constexpr static int half() { return max() / 2; }
22
23 constexpr font_variant(font_weight weight, bool italic) noexcept : value(static_cast<uint8_t>(static_cast<int>(weight) + (italic ? half() : 0))) {}
24 constexpr font_variant() noexcept : font_variant(font_weight::Regular, false) {}
25 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, false) {}
26 constexpr font_variant(bool italic) noexcept : font_variant(font_weight::Regular, italic) {}
27
28 constexpr font_weight weight() const noexcept {
29 tt_axiom(value < max());
30 return static_cast<font_weight>(value % half());
31 }
32
33 [[nodiscard]] constexpr bool italic() const noexcept {
34 tt_axiom(value < max());
35 return value >= half();
36 }
37
38 constexpr font_variant &set_weight(font_weight rhs) noexcept {
39 value = static_cast<uint8_t>(static_cast<int>(rhs) + (italic() ? half() : 0));
40 tt_axiom(value < max());
41 return *this;
42 }
43
44 constexpr font_variant &set_italic(bool rhs) noexcept {
45 value = static_cast<uint8_t>(static_cast<int>(weight()) + (rhs ? half() : 0));
46 tt_axiom(value < max());
47 return *this;
48 }
49
50 constexpr operator int () const noexcept {
51 tt_axiom(value < max());
52 return value;
53 }
54
58 constexpr font_variant alternative(int i) const noexcept {
59 tt_axiom(i >= 0 && i < max());
60 ttlet w = font_weight_alterative(weight(), i % half());
61 ttlet it = italic() == (i < half());
62 return {w, it};
63 }
64
65 [[nodiscard]] friend std::string to_string(font_variant const &rhs) noexcept {
66 return std::format("{}", rhs.weight(), rhs.italic() ? "/italic" : "");
67 }
68
69 friend std::ostream &operator<<(std::ostream &lhs, font_variant const &rhs) {
70 return lhs << to_string(rhs);
71 }
72};
73
74}
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:58