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