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