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
18public:
19 [[nodiscard]] constexpr static size_t size() noexcept
20 {
21 return font_weight_metadata.size() * font_style_metadata.size();
22 }
23
24 constexpr font_variant(font_weight weight, font_style style) noexcept :
25 _value(narrow_cast<uint8_t>((to_underlying(weight) << 1) | to_underlying(style)))
26 {
27 }
28
29 constexpr font_variant() noexcept : font_variant(font_weight::regular, font_style::normal) {}
30 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, font_style::normal) {}
31 constexpr font_variant(font_style style) noexcept : font_variant(font_weight::regular, style) {}
32
33 [[nodiscard]] constexpr operator size_t() const noexcept
34 {
35 return to_underlying(style()) + to_underlying(weight()) * font_style_metadata.size();
36 }
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 return static_cast<font_weight>(_value >> 1);
46 }
47
48 [[nodiscard]] constexpr font_style style() const noexcept
49 {
50 return static_cast<font_style>(_value & 1);
51 }
52
53 constexpr font_variant& set_weight(font_weight rhs) noexcept
54 {
55 _value &= 1;
56 _value |= to_underlying(rhs) << 1;
57 return *this;
58 }
59
60 constexpr font_variant& set_style(font_style rhs) noexcept
61 {
62 _value &= 0x1e;
63 _value |= to_underlying(rhs);
64 return *this;
65 }
66
73 [[nodiscard]] friend generator<font_variant> alternatives(font_variant const& start) noexcept
74 {
75 for (hilet s : alternatives(start.style())) {
76 for (hilet w : alternatives(start.weight())) {
77 co_yield font_variant{w, s};
78 }
79 }
80 }
81
82 [[nodiscard]] friend std::string to_string(font_variant const& rhs) noexcept
83 {
84 return std::format("{}", rhs.weight(), rhs.style() == font_style::italic ? "/italic" : "");
85 }
86
87 friend std::ostream& operator<<(std::ostream& lhs, font_variant const& rhs)
88 {
89 return lhs << to_string(rhs);
90 }
91
92private:
98 uint8_t _value;
99};
100
101} // namespace hi::inline v1
102
103template<>
104struct std::hash<hi::font_variant> {
105 [[nodiscard]] size_t operator()(hi::font_variant const& rhs) const noexcept
106 {
107 return rhs.hash();
108 }
109};
Defines the `font_style` type.
#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:17
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
friend generator< font_variant > alternatives(font_variant const &start) noexcept
Get an alternative font variant.
Definition font_variant.hpp:73
A return value for a generator-function.
Definition generator.hpp:29
T operator()(T... args)