HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
FontVariant.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Text/FontWeight.hpp"
7
8namespace tt {
9
16 uint8_t value;
17
18public:
19 constexpr static int max() { return 20; }
20 constexpr static int half() { return max() / 2; }
21
22 constexpr FontVariant(FontWeight weight, bool italic) noexcept : value(static_cast<uint8_t>(static_cast<int>(weight) + (italic ? half() : 0))) {}
23 constexpr FontVariant() noexcept : FontVariant(FontWeight::Regular, false) {}
24 constexpr FontVariant(FontWeight weight) noexcept : FontVariant(weight, false) {}
25 constexpr FontVariant(bool italic) noexcept : FontVariant(FontWeight::Regular, italic) {}
26
27 constexpr FontWeight weight() const noexcept {
28 tt_assume(value < max());
29 return static_cast<FontWeight>(value % half());
30 }
31
32 [[nodiscard]] constexpr bool italic() const noexcept {
33 tt_assume(value < max());
34 return value >= half();
35 }
36
37 constexpr FontVariant &set_weight(FontWeight rhs) noexcept {
38 value = static_cast<uint8_t>(static_cast<int>(rhs) + (italic() ? half() : 0));
39 tt_assume(value < max());
40 return *this;
41 }
42
43 constexpr FontVariant &set_italic(bool rhs) noexcept {
44 value = static_cast<uint8_t>(static_cast<int>(weight()) + (rhs ? half() : 0));
45 tt_assume(value < max());
46 return *this;
47 }
48
49 constexpr operator int () const noexcept {
50 tt_assume(value < max());
51 return value;
52 }
53
57 constexpr FontVariant alternative(int i) const noexcept {
58 tt_assume(i >= 0 && i < max());
59 ttlet w = FontWeight_alterative(weight(), i % half());
60 ttlet it = italic() == (i < half());
61 return {w, it};
62 }
63
64 [[nodiscard]] friend std::string to_string(FontVariant const &rhs) noexcept {
65 return fmt::format("{}", rhs.weight(), rhs.italic() ? "/italic" : "");
66 }
67
68 friend std::ostream &operator<<(std::ostream &lhs, FontVariant const &rhs) {
69 return lhs << to_string(rhs);
70 }
71};
72
73}
A font variant is one of 16 different fonts that can be part of a family.
Definition FontVariant.hpp:15
constexpr FontVariant alternative(int i) const noexcept
Get an alternative font variant.
Definition FontVariant.hpp:57