HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_weight.hpp
1// Copyright Take Vos 2021.
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 "../exception.hpp"
8#include <string>
9#include <unordered_map>
10#include <ostream>
11
12namespace tt {
13
14enum class font_weight {
15 Thin,
16 ExtraLight,
17 Light,
18 Regular,
19 Medium,
20 SemiBold,
21 Bold,
22 ExtraBold,
23 Black,
24 ExtraBlack,
25};
26
27inline ttlet font_weight_from_string_table = std::unordered_map<std::string,font_weight>{
28 {"thin", font_weight::Thin},
29 {"hairline", font_weight::Thin},
30 {"ultra-light", font_weight::ExtraLight},
31 {"ultra light", font_weight::ExtraLight},
32 {"extra-light", font_weight::ExtraLight},
33 {"extra light", font_weight::ExtraLight},
34 {"light", font_weight::Light},
35 {"normal", font_weight::Regular},
36 {"regular", font_weight::Regular},
37 {"medium", font_weight::Medium},
38 {"semi-bold", font_weight::SemiBold},
39 {"semi bold", font_weight::SemiBold},
40 {"demi-bold", font_weight::SemiBold},
41 {"demi bold", font_weight::SemiBold},
42 {"bold", font_weight::Bold},
43 {"extra-bold", font_weight::ExtraBold},
44 {"extra bold", font_weight::ExtraBold},
45 {"ultra-bold", font_weight::ExtraBold},
46 {"ultra bold", font_weight::ExtraBold},
47 {"heavy", font_weight::Black},
48 {"black", font_weight::Black},
49 {"extra-black", font_weight::ExtraBlack},
50 {"ultra-black", font_weight::ExtraBlack},
51};
52
55[[nodiscard]] constexpr font_weight font_weight_from_int(int rhs) {
56 if (rhs < 50 || rhs > 1000) {
57 throw parse_error("Unknown font-weight {}", rhs);
58 }
59 return static_cast<font_weight>(((rhs + 50) / 100) - 1);
60}
61
62[[nodiscard]] inline font_weight font_weight_from_string(std::string_view rhs) {
63 ttlet i = font_weight_from_string_table.find(to_lower(rhs));
64 if (i == font_weight_from_string_table.end()) {
65 throw parse_error("Unknown font-weight {}", rhs);
66 }
67 return i->second;
68}
69
70[[nodiscard]] constexpr char const *to_const_string(font_weight const &x) noexcept {
71 switch (x) {
72 case font_weight::Thin: return "Thin";
73 case font_weight::ExtraLight: return "ExtraLight";
74 case font_weight::Light: return "Light";
75 case font_weight::Regular: return "Regular";
76 case font_weight::Medium: return "Medium";
77 case font_weight::SemiBold: return "SemiBold";
78 case font_weight::Bold: return "Bold";
79 case font_weight::ExtraBold: return "ExtraBold";
80 case font_weight::Black: return "Black";
81 case font_weight::ExtraBlack: return "ExtraBlack";
82 default: tt_no_default();
83 }
84}
85
86[[nodiscard]] inline std::string to_string(font_weight const &x) noexcept {
87 return to_const_string(x);
88}
89
90[[nodiscard]] inline char to_char(font_weight const &x) noexcept {
91 ttlet x_ = static_cast<int>(x);
92 tt_axiom(x_ >= 0 && x_ <= 9);
93 return static_cast<char>('0' + x_);
94}
95
96[[nodiscard]] constexpr int to_int(font_weight const &x) noexcept {
97 ttlet x_ = (static_cast<int>(x) + 1) * 100;
98 return (x_ == 1000) ? 950 : x_;
99}
100
101inline std::ostream& operator<<(std::ostream& lhs, font_weight const& rhs) {
102 return lhs << to_string(rhs);
103}
104
105inline bool almost_equal(font_weight const &lhs, font_weight const &rhs) noexcept {
106 // Check only if it is bold or not.
107 return (lhs > font_weight::Medium) == (rhs > font_weight::Medium);
108}
109
110[[nodiscard]] constexpr auto font_weight_alternative_table_generator() noexcept
111{
112 std::array<font_weight,100> r = {font_weight::Regular};
113
114 for (int w = 0; w < 10; ++w) {
115 auto min_w = w;
116 auto max_w = w;
117 auto new_w = w;
118 auto forward = false;
119
120 for (int i = 0; i < 10; ++i) {
121 r[w * 10 + i] = static_cast<font_weight>(new_w);
122
123 // Change direction to not overflow.
124 if ((forward && max_w == 9) || (!forward && min_w == 0)) {
125 forward = !forward;
126 }
127
128 if (forward) {
129 ++max_w;
130 new_w = max_w;
131 } else {
132 --min_w;
133 new_w = min_w;
134 }
135
136 // Change direction to zig-zag.
137 forward = !forward;
138 }
139 }
140 return r;
141}
142
143constexpr auto font_weight_alternative_table = font_weight_alternative_table_generator();
144
145[[nodiscard]] constexpr font_weight font_weight_alterative(font_weight weight, int i) noexcept {
146 tt_axiom(i >= 0 && i < 10);
147 auto w = static_cast<int>(weight);
148 tt_axiom(w >= 0 && w < 10);
149 return font_weight_alternative_table[(w * 10) + i];
150}
151
152}
T forward(T... args)
T to_string(T... args)