HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_size.hpp
1// Copyright Take Vos 2024.
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 "dips_per_em.hpp"
8#include "dips.hpp"
9#include "em_squares.hpp"
10#include "length.hpp"
11#include "pixels_per_em.hpp"
12#include "pixels.hpp"
13#include "points_per_em.hpp"
14#include "points.hpp"
15#include "../utility/utility.hpp"
16#include "../macros.hpp"
17#include <hikothird/au.hh>
18#include <concepts>
19#include <variant>
20
21hi_export_module(hikogui.unit : pixels_per_inch);
22
23hi_export namespace hi {
24inline namespace v1 {
25
26template<typename T>
27using font_size_variant = std::variant<au::Quantity<PointsPerEm, T>, au::Quantity<PixelsPerEm, T>, au::Quantity<DipsPerEm, T>>;
28
29template<typename T>
30class font_size_quantity : public font_size_variant<T> {
31 using super = font_size_variant<T>;
32
33 using super::super;
34
35 template<typename O>
36 constexpr explicit font_size_quantity(au::Quantity<Dips, O> const& other) noexcept : super(dips_per_em(other.in(dips)))
37 {
38 }
39
40 template<typename O>
41 constexpr explicit font_size_quantity(au::Quantity<Pixels, O> const& other) noexcept : super(pixels_per_em(other.in(pixels)))
42 {
43 }
44
45 template<typename O>
46 constexpr explicit font_size_quantity(au::Quantity<Points, O> const& other) noexcept : super(points_per_em(other.in(points)))
47 {
48 }
49
50 template<typename O>
51 constexpr explicit font_size_quantity(length_quantity<O> const& other) : super(font_size_quantity_conversion(other))
52 {
53 }
54
55 template<typename O>
56 [[nodiscard]] constexpr friend length_quantity<O>
57 operator*(au::Quantity<EmSquares, O> const& lhs, font_size_quantity const& rhs) noexcept
58 {
59 if (auto* dips_ptr = std::get_if<au::Quantity<DipsPerEm, T>>(&rhs)) {
60 return (*dips_ptr * lhs).as(dips);
61
62 } else if (auto* pixel_ptr = std::get_if<au::Quantity<PixelsPerEm, T>>(&rhs)) {
63 return (*pixel_ptr * lhs).as(pixels);
64
65 } else if (auto* points_ptr = std::get_if<au::Quantity<PointsPerEm, T>>(&rhs)) {
66 return (*points_ptr * lhs).as(points);
67
68 } else {
69 hi_no_default();
70 }
71 }
72
73 template<typename O>
74 [[nodiscard]] constexpr friend length_quantity<O>
75 operator*(font_size_quantity const& lhs, au::Quantity<EmSquares, O> const& rhs) noexcept
76 {
77 return rhs * lhs;
78 }
79
80private:
81 template<typename O>
82 [[nodiscard]] constexpr static font_size_quantity font_size_quantity_conversion(length_quantity<O> const& other)
83 {
84 if (auto* dips_ptr = std::get_if<au::Quantity<Dips, O>>(&other)) {
85 return dips_per_em(other.in(dips));
86 } else if (auto* pixel_ptr = std::get_if<au::Quantity<Pixels, O>>(&other)) {
87 return pixels_per_em(other.in(pixels));
88 } else if (auto* points_ptr = std::get_if<au::Quantity<Points, O>>(&other)) {
89 return points_per_em(other.in(points));
90 } else {
91 throw std::bad_variant_access();
92 }
93 }
94};
95
102template<typename FontSizeT, typename ByT>
105{
106 auto const rounded_x_height = round_as(pixels, by * font_size);
107 return rounded_x_height / by;
108}
109
110using font_size_f = font_size_quantity<float>;
111using font_size_s = font_size_quantity<short>;
112
113} // namespace v1
114}
115
116template<std::integral T>
117struct std::hash<hi::font_size_quantity<T>> {
118 [[nodiscard]] size_t operator()(hi::font_size_quantity<T> const& rhs) const noexcept
119 {
120 auto h = std::hash<size_t>{}(rhs.index());
121 h ^= std::visit(
122 [](auto&& rhs_) {
123 return std::hash<T>{}(rhs_.in(rhs_.unit));
124 },
125 rhs);
126 return h;
127 }
128};
@ round
The end cap of the line is round.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition font_size.hpp:30
Definition length.hpp:26
Definition au.hh:3518
T operator()(T... args)