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