HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_weight.hpp
1// Copyright Take Vos 2021-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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <string>
10#include <string_view>
11#include <unordered_map>
12#include <ostream>
13#include <array>
14#include <coroutine>
15#include <format>
16
17hi_export_module(hikogui.font.font_weight);
18
19hi_export namespace hi::inline v1 {
20
21enum class font_weight {
22 thin,
24 light,
25 regular,
26 medium,
27 semi_bold,
28 bold,
30 black,
32};
33
34constexpr font_weight& operator++(font_weight& rhs) noexcept
35{
36 hi_axiom(rhs < font_weight::extra_black);
37 return rhs = static_cast<font_weight>(std::to_underlying(rhs) + 1);
38}
39
40constexpr font_weight& operator--(font_weight& rhs) noexcept
41{
42 hi_axiom(rhs > font_weight::thin);
43 return rhs = static_cast<font_weight>(std::to_underlying(rhs) - 1);
44}
45
46// clang-format off
47constexpr auto font_weight_metadata = enum_metadata{
48 font_weight::thin, "thin",
49 font_weight::extra_light, "extra-light",
50 font_weight::light, "light",
51 font_weight::regular, "regular",
52 font_weight::medium, "medium",
53 font_weight::semi_bold, "semi-bold",
54 font_weight::bold, "bold",
55 font_weight::extra_bold, "extra-bold",
56 font_weight::black, "black",
57 font_weight::extra_black, "extra-black",
58};
59// clang-format on
60
63[[nodiscard]] constexpr font_weight font_weight_from_int(numeric_integral auto rhs)
64{
65 if (rhs < 50 or rhs > 1000) {
66 throw parse_error(std::format("Unknown font-weight {}", rhs));
67 }
68 return static_cast<font_weight>(((rhs + 50) / 100) - 1);
69}
70
71[[nodiscard]] constexpr font_weight font_weight_from_string(std::string_view rhs)
72{
73 if (auto weight = font_weight_metadata.at_if(rhs)) {
74 return *weight;
75 } else {
76 throw parse_error(std::format("Unknown font-weight {}", rhs));
77 }
78}
79
80[[nodiscard]] constexpr std::string_view to_string_view(font_weight const& x) noexcept
81{
82 return font_weight_metadata[x];
83}
84
85[[nodiscard]] constexpr std::string to_string(font_weight const& x) noexcept
86{
87 return std::string{to_string_view(x)};
88}
89
90[[nodiscard]] constexpr char to_char(font_weight const& x) noexcept
91{
92 auto const x_ = static_cast<int>(x);
93 hi_axiom(x_ >= 0 && x_ <= 9);
94 return char_cast<char>('0' + x_);
95}
96
97[[nodiscard]] constexpr int to_int(font_weight const& x) noexcept
98{
99 auto const x_ = (static_cast<int>(x) + 1) * 100;
100 return (x_ == 1000) ? 950 : x_;
101}
102
103inline std::ostream& operator<<(std::ostream& lhs, font_weight const& rhs)
104{
105 return lhs << to_string(rhs);
106}
107
108constexpr bool almost_equal(font_weight const& lhs, font_weight const& rhs) noexcept
109{
110 // Check only if it is bold or not.
111 return (lhs > font_weight::medium) == (rhs > font_weight::medium);
112}
113
119[[nodiscard]] inline generator<font_weight> alternatives(font_weight start) noexcept
120{
121 co_yield start;
122
123 auto min = start;
124 auto max = start;
125 auto forward = false;
126 while (min > font_weight::thin or max < font_weight::extra_black) {
127 if ((forward and max == font_weight::extra_black) or (not forward and min == font_weight::thin)) {
128 // Change direction to not overflow.
129 forward = not forward;
130 }
131
132 if (forward) {
133 co_yield ++max;
134 } else {
135 co_yield --min;
136 }
137
138 // Zig-zag through each weight.
139 forward = not forward;
140 }
141}
142
143} // namespace hi::inline v1
144
145// XXX #617 MSVC bug does not handle partial specialization in modules.
146hi_export template<>
147struct std::formatter<hi::font_weight, char> : std::formatter<std::string_view, char> {
148 auto format(hi::font_weight const& t, auto& fc) const
149 {
150 return std::formatter<std::string_view, char>::format(hi::to_string_view(t), fc);
151 }
152};
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr font_weight font_weight_from_int(numeric_integral auto rhs)
Convert a font weight value between 50 and 1000 to a font weight.
Definition font_weight.hpp:63
font_weight
Definition font_weight.hpp:21
@ medium
500: Medium
@ black
900: Heavy / Black
@ light
300: Light
@ semi_bold
600: Semi-bold / Demi-bold
@ thin
100: Thin / Hairline
@ bold
700: Bold
@ extra_light
200: Ultra-light / Extra-light
@ regular
400: Normal / Regular
@ extra_bold
800: Extra-bold / Ultra-bold
@ extra_black
950: Extra-black / Ultra-black
A return value for a generator-function.
Definition generator.hpp:31
T to_string(T... args)