31 theme_mode mode = theme_mode::light;
33 theme() noexcept = default;
36 theme& operator=(
theme const&) noexcept = default;
44 hi_log_info(
"Parsing theme at {}", path.string());
45 auto const data = parse_JSON(path);
48 throw io_error(std::format(
"{}: Could not load theme.\n{}", path.string(), e.
what()));
54 template<
typename T = hi::margins>
55 [[nodiscard]]
constexpr T
margin() const noexcept
57 if constexpr (std::is_same_v<T, hi::margins>) {
59 }
else if constexpr (std::is_same_v<T, float>) {
62 hi_static_not_implemented();
75 template<
typename T = hi::corner_radii>
78 if constexpr (std::is_same_v<T, hi::corner_radii>) {
79 return T{_rounding_radius};
80 }
else if constexpr (std::is_same_v<T, float>) {
81 return _rounding_radius;
83 hi_static_not_implemented();
89 [[nodiscard]]
constexpr float size() const noexcept
96 [[nodiscard]]
constexpr float large_size() const noexcept
103 [[nodiscard]]
constexpr float icon_size() const noexcept
112 return _large_icon_size;
119 return _label_icon_size;
126 return _baseline_adjustment;
143 auto delta_scale = new_pixel_density.ppi / pixel_density.ppi;
144 r.pixel_density = new_pixel_density;
147 r._margin =
std::round(delta_scale * _margin);
148 r._border_width =
std::round(delta_scale * _border_width);
149 r._rounding_radius =
std::round(delta_scale * _rounding_radius);
151 r._large_size =
std::round(delta_scale * _large_size);
152 r._icon_size =
std::round(delta_scale * _icon_size);
153 r._large_icon_size =
std::round(delta_scale * _large_icon_size);
154 r._label_icon_size =
std::round(delta_scale * _label_icon_size);
156 r._baseline_adjustment =
std::round(delta_scale * _baseline_adjustment);
163 auto const& shades = _colors[std::to_underlying(original_color)];
164 hi_assert(not shades.empty());
166 nesting_level =
std::max(ssize_t{0}, nesting_level);
167 return shades[nesting_level % ssize(shades)];
170 [[nodiscard]]
hi::color color(
hi::color original_color, ssize_t nesting_level = 0) const noexcept
172 if (original_color.is_semantic()) {
173 return color(
static_cast<semantic_color>(original_color), nesting_level);
175 return original_color;
179 [[nodiscard]] hi::text_style text_style(semantic_text_style theme_color)
const noexcept
181 return _text_styles[std::to_underlying(theme_color)];
184 [[nodiscard]] hi::text_style text_style(hi::text_style original_style)
const noexcept
186 if (original_style.is_semantic()) {
187 return text_style(
static_cast<semantic_text_style
>(original_style));
189 return original_style;
196 float _margin = 5.0f;
200 float _border_width = 1.0f;
204 float _rounding_radius = 4.0f;
212 float _large_size = 19.0f;
216 float _icon_size = 8.0f;
220 float _large_icon_size = 23.0f;
224 float _label_icon_size = 15.0f;
228 float _baseline_adjustment = 9.0f;
231 std::array<hi::text_style, semantic_text_style_metadata.size()> _text_styles;
233 [[nodiscard]]
float parse_float(datum
const& data,
char const *object_name)
235 if (!data.contains(object_name)) {
236 throw parse_error(std::format(
"Missing '{}'", object_name));
239 auto const object = data[object_name];
240 if (
auto f = get_if<double>(
object)) {
241 return static_cast<float>(*f);
242 }
else if (
auto ll = get_if<long long>(
object)) {
243 return static_cast<float>(*ll);
246 std::format(
"'{}' attribute must be a floating point number, got {}.", object_name,
object.type_name()));
250 [[nodiscard]]
long long parse_long_long(datum
const& data,
char const *object_name)
252 if (!data.contains(object_name)) {
253 throw parse_error(std::format(
"Missing '{}'", object_name));
256 auto const object = data[object_name];
257 if (
auto f = get_if<long long>(
object)) {
258 return static_cast<long long>(*f);
260 throw parse_error(std::format(
"'{}' attribute must be a integer, got {}.", object_name,
object.type_name()));
264 [[nodiscard]]
int parse_int(datum
const& data,
char const *object_name)
266 auto const value = parse_long_long(data, object_name);
268 throw parse_error(std::format(
"'{}' attribute is out of range, got {}.", object_name, value));
270 return narrow_cast<int>(value);
273 [[nodiscard]]
bool parse_bool(datum
const& data,
char const *object_name)
275 if (!data.contains(object_name)) {
276 throw parse_error(std::format(
"Missing '{}'", object_name));
279 auto const object = data[object_name];
280 if (!holds_alternative<bool>(
object)) {
281 throw parse_error(std::format(
"'{}' attribute must be a boolean, got {}.", object_name,
object.type_name()));
284 return to_bool(
object);
287 [[nodiscard]]
std::string parse_string(datum
const& data,
char const *object_name)
290 if (!data.contains(object_name)) {
291 throw parse_error(std::format(
"Missing '{}'", object_name));
293 auto const object = data[object_name];
294 if (!holds_alternative<std::string>(
object)) {
295 throw parse_error(std::format(
"'{}' attribute must be a string, got {}.", object_name,
object.type_name()));
300 [[nodiscard]]
hi::color parse_color_value(datum
const& data)
302 if (holds_alternative<datum::vector_type>(data)) {
303 if (data.size() != 3 && data.size() != 4) {
304 throw parse_error(std::format(
"Expect 3 or 4 values for a color, got {}.", data));
306 auto const r = data[0];
307 auto const g = data[1];
308 auto const b = data[2];
309 auto const a = data.size() == 4 ? data[3] : (holds_alternative<long long>(r) ? datum{255} : datum{1.0});
311 if (holds_alternative<long long>(r) and holds_alternative<long long>(g) and holds_alternative<long long>(b) and
312 holds_alternative<long long>(a)) {
313 auto const r_ = get<long long>(r);
314 auto const g_ = get<long long>(g);
315 auto const b_ = get<long long>(b);
316 auto const a_ = get<long long>(a);
318 hi_check(r_ >= 0 and r_ <= 255,
"integer red-color value not within 0 and 255");
319 hi_check(g_ >= 0 and g_ <= 255,
"integer green-color value not within 0 and 255");
320 hi_check(b_ >= 0 and b_ <= 255,
"integer blue-color value not within 0 and 255");
321 hi_check(a_ >= 0 and a_ <= 255,
"integer alpha-color value not within 0 and 255");
324 static_cast<uint8_t
>(r_),
static_cast<uint8_t
>(g_),
static_cast<uint8_t
>(b_),
static_cast<uint8_t
>(a_));
327 holds_alternative<double>(r) and holds_alternative<double>(g) and holds_alternative<double>(b) and
328 holds_alternative<double>(a)) {
329 auto const r_ =
static_cast<float>(get<double>(r));
330 auto const g_ =
static_cast<float>(get<double>(g));
331 auto const b_ =
static_cast<float>(get<double>(b));
332 auto const a_ =
static_cast<float>(get<double>(a));
337 throw parse_error(std::format(
"Expect all integers or all floating point numbers in a color, got {}.", data));
340 }
else if (
auto const *color_name = get_if<std::string>(data)) {
341 auto const color_name_ = to_lower(*color_name);
342 if (color_name_.starts_with(
"#")) {
346 throw parse_error(std::format(
"Unable to parse color, got {}.", data));
349 throw parse_error(std::format(
"Unable to parse color, got {}.", data));
353 [[nodiscard]]
hi::color parse_color(datum
const& data,
char const *object_name)
355 if (!data.contains(object_name)) {
356 throw parse_error(std::format(
"Missing color '{}'", object_name));
359 auto const color_object = data[object_name];
362 return parse_color_value(color_object);
363 }
catch (parse_error
const&) {
364 if (
auto s = get_if<std::string>(color_object)) {
375 if (!data.contains(object_name)) {
376 throw parse_error(std::format(
"Missing color list '{}'", object_name));
379 auto const color_list_object = data[object_name];
380 if (holds_alternative<datum::vector_type>(color_list_object) and not color_list_object.empty() and
381 holds_alternative<datum::vector_type>(color_list_object[0])) {
384 for (
auto const& color : color_list_object) {
386 r.push_back(parse_color_value(color));
387 }
catch (parse_error
const& e) {
389 std::format(
"Could not parse {}nd entry of color list '{}'\n{}", i + 1, object_name, e.what()));
396 return {parse_color_value(data[object_name])};
397 }
catch (parse_error
const& e) {
398 throw parse_error(std::format(
"Could not parse color '{}'\n{}", object_name, e.what()));
403 [[nodiscard]] hi::text_style parse_text_style_value(datum
const& data)
405 if (not holds_alternative<datum::map_type>(data)) {
406 throw parse_error(std::format(
"Expect a text-style to be an object, got '{}'", data));
410 auto const font_size = points_per_em(gsl::narrow<short>(parse_float(data,
"size")));
412 auto variant = font_variant{};
413 if (data.contains(
"weight")) {
414 variant.set_weight(parse_font_weight(data,
"weight"));
416 variant.set_weight(font_weight::regular);
419 if (data.contains(
"italic")) {
420 variant.set_style(parse_bool(data,
"italic") ? font_style::italic :
font_style::normal);
422 variant.set_style(font_style::normal);
426 auto const color = this->color(parse_color(data,
"color"), 0);
430 phrasing_mask::all, iso_639{}, iso_15924{}, family_id, variant, font_size, color,
text_decoration{});
431 return hi::text_style(sub_styles);
434 [[nodiscard]]
font_weight parse_font_weight(datum
const& data,
char const *object_name)
436 if (!data.contains(object_name)) {
437 throw parse_error(std::format(
"Missing '{}'", object_name));
440 auto const object = data[object_name];
441 if (
auto i = get_if<long long>(
object)) {
443 }
else if (
auto s = get_if<std::string>(
object)) {
444 return font_weight_from_string(*s);
446 throw parse_error(std::format(
"Unable to parse font weight, got {}.",
object.type_name()));
450 [[nodiscard]] hi::text_style parse_text_style(datum
const& data,
char const *object_name)
453 if (!data.contains(object_name)) {
454 throw parse_error(std::format(
"Missing text-style '{}'", object_name));
457 auto const textStyleObject = data[object_name];
459 return parse_text_style_value(textStyleObject);
460 }
catch (parse_error
const& e) {
461 throw parse_error(std::format(
"Could not parse text-style '{}'\n{}", object_name, e.what()));
465 void parse(datum
const& data)
467 hi_assert(holds_alternative<datum::map_type>(data));
469 name = parse_string(data,
"name");
471 auto const mode_name = to_lower(parse_string(data,
"mode"));
472 if (mode_name ==
"light") {
473 mode = theme_mode::light;
474 }
else if (mode_name ==
"dark") {
475 mode = theme_mode::dark;
477 throw parse_error(std::format(
"Attribute 'mode' must be \"light\" or \"dark\", got \"{}\".", mode_name));
480 std::get<std::to_underlying(semantic_color::blue)>(_colors) = parse_color_list(data,
"blue");
481 std::get<std::to_underlying(semantic_color::green)>(_colors) = parse_color_list(data,
"green");
482 std::get<std::to_underlying(semantic_color::indigo)>(_colors) = parse_color_list(data,
"indigo");
483 std::get<std::to_underlying(semantic_color::orange)>(_colors) = parse_color_list(data,
"orange");
484 std::get<std::to_underlying(semantic_color::pink)>(_colors) = parse_color_list(data,
"pink");
485 std::get<std::to_underlying(semantic_color::purple)>(_colors) = parse_color_list(data,
"purple");
486 std::get<std::to_underlying(semantic_color::red)>(_colors) = parse_color_list(data,
"red");
487 std::get<std::to_underlying(semantic_color::teal)>(_colors) = parse_color_list(data,
"teal");
488 std::get<std::to_underlying(semantic_color::yellow)>(_colors) = parse_color_list(data,
"yellow");
490 std::get<std::to_underlying(semantic_color::gray)>(_colors) = parse_color_list(data,
"gray");
491 std::get<std::to_underlying(semantic_color::gray2)>(_colors) = parse_color_list(data,
"gray2");
492 std::get<std::to_underlying(semantic_color::gray3)>(_colors) = parse_color_list(data,
"gray3");
493 std::get<std::to_underlying(semantic_color::gray4)>(_colors) = parse_color_list(data,
"gray4");
494 std::get<std::to_underlying(semantic_color::gray5)>(_colors) = parse_color_list(data,
"gray5");
495 std::get<std::to_underlying(semantic_color::gray6)>(_colors) = parse_color_list(data,
"gray6");
497 std::get<std::to_underlying(semantic_color::foreground)>(_colors) = parse_color_list(data,
"foreground-color");
498 std::get<std::to_underlying(semantic_color::border)>(_colors) = parse_color_list(data,
"border-color");
499 std::get<std::to_underlying(semantic_color::fill)>(_colors) = parse_color_list(data,
"fill-color");
500 std::get<std::to_underlying(semantic_color::accent)>(_colors) = parse_color_list(data,
"accent-color");
501 std::get<std::to_underlying(semantic_color::text_select)>(_colors) = parse_color_list(data,
"text-select-color");
502 std::get<std::to_underlying(semantic_color::primary_cursor)>(_colors) = parse_color_list(data,
"primary-cursor-color");
503 std::get<std::to_underlying(semantic_color::secondary_cursor)>(_colors) =
504 parse_color_list(data,
"secondary-cursor-color");
506 std::get<std::to_underlying(semantic_text_style::label)>(_text_styles) = parse_text_style(data,
"label-style");
507 std::get<std::to_underlying(semantic_text_style::small_label)>(_text_styles) =
508 parse_text_style(data,
"small-label-style");
509 std::get<std::to_underlying(semantic_text_style::warning)>(_text_styles) = parse_text_style(data,
"warning-label-style");
510 std::get<std::to_underlying(semantic_text_style::error)>(_text_styles) = parse_text_style(data,
"error-label-style");
511 std::get<std::to_underlying(semantic_text_style::help)>(_text_styles) = parse_text_style(data,
"help-label-style");
512 std::get<std::to_underlying(semantic_text_style::placeholder)>(_text_styles) =
513 parse_text_style(data,
"placeholder-label-style");
514 std::get<std::to_underlying(semantic_text_style::link)>(_text_styles) = parse_text_style(data,
"link-label-style");
516 _margin = narrow_cast<float>(parse_int(data,
"margin"));
517 _border_width = narrow_cast<float>(parse_int(data,
"border-width"));
518 _rounding_radius = narrow_cast<float>(parse_int(data,
"rounding-radius"));
519 _size = narrow_cast<float>(parse_int(data,
"size"));
520 _large_size = narrow_cast<float>(parse_int(data,
"large-size"));
521 _icon_size = narrow_cast<float>(parse_int(data,
"icon-size"));
522 _large_icon_size = narrow_cast<float>(parse_int(data,
"large-icon-size"));
523 _label_icon_size = narrow_cast<float>(parse_int(data,
"label-icon-size"));
525 _baseline_adjustment = ceil_in(points, std::get<points_f>(std::get<std::to_underlying(semantic_text_style::label)>(_text_styles)->cap_height()));
530 return std::format(
"{}:{}", rhs.name, rhs.mode);