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 "../coroutine/module.hpp"
9#include <string>
10#include <unordered_map>
11#include <ostream>
12#include <array>
13
14hi_export_module(hikogui.font.font_weight);
15
16namespace hi::inline v1 {
17
18hi_export enum class font_weight {
19 thin,
21 light,
22 regular,
23 medium,
24 semi_bold,
25 bold,
27 black,
29};
30
31hi_export constexpr font_weight& operator++(font_weight& rhs) noexcept
32{
33 hi_axiom(rhs < font_weight::extra_black);
34 return rhs = static_cast<font_weight>(std::to_underlying(rhs) + 1);
35}
36
37hi_export constexpr font_weight& operator--(font_weight& rhs) noexcept
38{
39 hi_axiom(rhs > font_weight::thin);
40 return rhs = static_cast<font_weight>(std::to_underlying(rhs) - 1);
41}
42
43// clang-format off
44constexpr auto font_weight_metadata = enum_metadata{
45 font_weight::thin, "thin",
46 font_weight::extra_light, "extra-light",
47 font_weight::light, "light",
48 font_weight::regular, "regular",
49 font_weight::medium, "medium",
50 font_weight::semi_bold, "semi-bold",
51 font_weight::bold, "bold",
52 font_weight::extra_bold, "extra-bold",
53 font_weight::black, "black",
54 font_weight::extra_black, "extra-black",
55};
56// clang-format on
57
60hi_export [[nodiscard]] constexpr font_weight font_weight_from_int(numeric_integral auto rhs)
61{
62 if (rhs < 50 or rhs > 1000) {
63 throw parse_error(std::format("Unknown font-weight {}", rhs));
64 }
65 return static_cast<font_weight>(((rhs + 50) / 100) - 1);
66}
67
68hi_export [[nodiscard]] constexpr font_weight font_weight_from_string(std::string_view rhs)
69{
70 try {
71 return font_weight_metadata.at(rhs);
72 } catch (...) {
73 throw parse_error(std::format("Unknown font-weight {}", rhs));
74 }
75}
76
77hi_export [[nodiscard]] constexpr std::string_view to_string_view(font_weight const& x) noexcept
78{
79 return font_weight_metadata[x];
80}
81
82hi_export [[nodiscard]] constexpr std::string to_string(font_weight const& x) noexcept
83{
84 return std::string{to_string_view(x)};
85}
86
87hi_export [[nodiscard]] constexpr char to_char(font_weight const& x) noexcept
88{
89 hilet x_ = static_cast<int>(x);
90 hi_axiom(x_ >= 0 && x_ <= 9);
91 return char_cast<char>('0' + x_);
92}
93
94hi_export [[nodiscard]] constexpr int to_int(font_weight const& x) noexcept
95{
96 hilet x_ = (static_cast<int>(x) + 1) * 100;
97 return (x_ == 1000) ? 950 : x_;
98}
99
100hi_export inline std::ostream& operator<<(std::ostream& lhs, font_weight const& rhs)
101{
102 return lhs << to_string(rhs);
103}
104
105hi_export constexpr bool almost_equal(font_weight const& lhs, font_weight const& rhs) noexcept
106{
107 // Check only if it is bold or not.
108 return (lhs > font_weight::medium) == (rhs > font_weight::medium);
109}
110
116hi_export [[nodiscard]] inline generator<font_weight> alternatives(font_weight start) noexcept
117{
118 co_yield start;
119
120 auto min = start;
121 auto max = start;
122 auto forward = false;
123 while (min > font_weight::thin or max < font_weight::extra_black) {
124 if ((forward and max == font_weight::extra_black) or (not forward and min == font_weight::thin)) {
125 // Change direction to not overflow.
126 forward = not forward;
127 }
128
129 if (forward) {
130 co_yield ++max;
131 } else {
132 co_yield --min;
133 }
134
135 // Zig-zag through each weight.
136 forward = not forward;
137 }
138}
139
140} // namespace hi::inline v1
141
142hi_export template<typename CharT>
143struct std::formatter<hi::font_weight, CharT> : std::formatter<std::string_view, CharT> {
144 auto format(hi::font_weight const& t, auto& fc) const
145 {
146 return std::formatter<std::string_view, CharT>::format(hi::to_string_view(t), fc);
147 }
148};
DOXYGEN BUG.
Definition algorithm.hpp:16
hi_export 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:60
font_weight
Definition font_weight.hpp:18
@ 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
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
T to_string(T... args)