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
10hi_export_module(hikogui.font.font_variant);
11
12namespace hi::inline v1 {
13
19hi_export class font_variant {
20public:
21 [[nodiscard]] constexpr static size_t size() noexcept
22 {
23 return font_weight_metadata.size() * font_style_metadata.size();
24 }
25
26 constexpr font_variant(font_weight weight, font_style style) noexcept :
27 _value(narrow_cast<uint8_t>((std::to_underlying(weight) << 1) | std::to_underlying(style)))
28 {
29 }
30
31 constexpr font_variant() noexcept : font_variant(font_weight::regular, font_style::normal) {}
32 constexpr font_variant(font_weight weight) noexcept : font_variant(weight, font_style::normal) {}
33 constexpr font_variant(font_style style) noexcept : font_variant(font_weight::regular, style) {}
34
35 [[nodiscard]] constexpr operator size_t() const noexcept
36 {
37 return std::to_underlying(style()) + std::to_underlying(weight()) * font_style_metadata.size();
38 }
39
40 [[nodiscard]] size_t hash() const noexcept
41 {
42 return std::hash<uint8_t>{}(_value);
43 }
44
45 constexpr font_weight weight() const noexcept
46 {
47 return static_cast<font_weight>(_value >> 1);
48 }
49
50 [[nodiscard]] constexpr font_style style() const noexcept
51 {
52 return static_cast<font_style>(_value & 1);
53 }
54
55 constexpr font_variant& set_weight(font_weight rhs) noexcept
56 {
57 _value &= 1;
58 _value |= std::to_underlying(rhs) << 1;
59 return *this;
60 }
61
62 constexpr font_variant& set_style(font_style rhs) noexcept
63 {
64 _value &= 0x1e;
65 _value |= std::to_underlying(rhs);
66 return *this;
67 }
68
75 [[nodiscard]] friend generator<font_variant> alternatives(font_variant const& start) noexcept
76 {
77 for (hilet s : alternatives(start.style())) {
78 for (hilet w : alternatives(start.weight())) {
79 co_yield font_variant{w, s};
80 }
81 }
82 }
83
84 [[nodiscard]] friend std::string to_string(font_variant const& rhs) noexcept
85 {
86 return std::format("{}", rhs.weight(), rhs.style() == font_style::italic ? "/italic" : "");
87 }
88
89 friend std::ostream& operator<<(std::ostream& lhs, font_variant const& rhs)
90 {
91 return lhs << to_string(rhs);
92 }
93
94private:
100 uint8_t _value;
101};
102
103} // namespace hi::inline v1
104
105hi_export template<>
106struct std::hash<hi::font_variant> {
107 [[nodiscard]] size_t operator()(hi::font_variant const& rhs) const noexcept
108 {
109 return rhs.hash();
110 }
111};
Defines the `font_style` type.
DOXYGEN BUG.
Definition algorithm.hpp:16
font_weight
Definition font_weight.hpp:18
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A return value for a generator-function.
Definition generator.hpp:31
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:19
friend generator< font_variant > alternatives(font_variant const &start) noexcept
Get an alternative font variant.
Definition font_variant.hpp:75
T operator()(T... args)