6#include "TTauri/Foundation/exceptions.hpp"
8#include <unordered_map>
13enum class 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},
54[[nodiscard]]
constexpr FontWeight FontWeight_from_int(
int rhs) {
55 if (rhs < 50 || rhs > 1000) {
56 TTAURI_THROW(parse_error(
"Unknown font-weight {}", rhs));
58 return static_cast<FontWeight
>(((rhs + 50) / 100) - 1);
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));
69[[nodiscard]]
constexpr char const *to_const_string(FontWeight
const &x)
noexcept {
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;
86 return to_const_string(x);
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_);
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_;
104inline bool almost_equal(FontWeight
const &lhs, FontWeight
const &rhs)
noexcept {
106 return (lhs > FontWeight::Medium) == (rhs > FontWeight::Medium);
109[[nodiscard]]
constexpr auto FontWeight_alternative_table_generator() noexcept
113 for (
int w = 0; w < 10; ++w) {
119 for (
int i = 0; i < 10; ++i) {
120 r[w * 10 + i] =
static_cast<FontWeight
>(new_w);
123 if ((forward && max_w == 9) || (!
forward && min_w == 0)) {
142constexpr auto FontWeight_alternative_table = FontWeight_alternative_table_generator();
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];