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#include "../utility/utility.hpp"
10#include "../macros.hpp"
11#include <coroutine>
12#include <string>
13#include <ostream>
14#include <format>
15#include <utility>
16#include <cstddef>
17
18hi_export_module(hikogui.font.font_variant);
19
20hi_export namespace hi::inline v1 {
21
28public:
29 [[nodiscard]] constexpr static size_t size() noexcept
30 {
31 return font_weight_metadata.size() * font_style_metadata.size();
32 }
33
34 constexpr font_variant(font_weight weight, font_style style) noexcept :
35 _value(narrow_cast<uint8_t>((std::to_underlying(weight) << 1) | std::to_underlying(style)))
36 {
37 }
38
39 constexpr font_variant() noexcept : font_variant(font_weight::regular, font_style::normal) {}
40 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, font_style::normal) {}
41 constexpr font_variant(font_style style) noexcept : font_variant(font_weight::regular, style) {}
42
43 [[nodiscard]] constexpr operator size_t() const noexcept
44 {
45 return std::to_underlying(style()) + std::to_underlying(weight()) * font_style_metadata.size();
46 }
47
48 [[nodiscard]] size_t hash() const noexcept
49 {
50 return std::hash<uint8_t>{}(_value);
51 }
52
53 constexpr font_weight weight() const noexcept
54 {
55 return static_cast<font_weight>(_value >> 1);
56 }
57
58 [[nodiscard]] constexpr font_style style() const noexcept
59 {
60 return static_cast<font_style>(_value & 1);
61 }
62
63 constexpr font_variant& set_weight(font_weight rhs) noexcept
64 {
65 _value &= 1;
66 _value |= std::to_underlying(rhs) << 1;
67 return *this;
68 }
69
70 constexpr font_variant& set_style(font_style rhs) noexcept
71 {
72 _value &= 0x1e;
73 _value |= std::to_underlying(rhs);
74 return *this;
75 }
76
83 [[nodiscard]] friend generator<font_variant> alternatives(font_variant start) noexcept
84 {
85 for (auto const s : alternatives(start.style())) {
86 for (auto const w : alternatives(start.weight())) {
87 co_yield font_variant{w, s};
88 }
89 }
90 }
91
92 [[nodiscard]] friend std::string to_string(font_variant const& rhs) noexcept
93 {
94 return std::format("{}", rhs.weight(), rhs.style() == font_style::italic ? "/italic" : "");
95 }
96
97 friend std::ostream& operator<<(std::ostream& lhs, font_variant const& rhs)
98 {
99 return lhs << to_string(rhs);
100 }
101
102private:
108 uint8_t _value;
109};
110
111} // namespace hi::inline v1
112
113hi_export template<>
114struct std::hash<hi::font_variant> {
115 [[nodiscard]] size_t operator()(hi::font_variant const& rhs) const noexcept
116 {
117 return rhs.hash();
118 }
119};
Defines the `font_style` type.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
font_weight
Definition font_weight.hpp:21
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:27
friend generator< font_variant > alternatives(font_variant start) noexcept
Get an alternative font variant.
Definition font_variant.hpp:83
A return value for a generator-function.
Definition generator.hpp:31
T operator()(T... args)