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 {
25namespace unit {
26
27template<typename T>
28using font_size_variant = std::variant<au::Quantity<PointsPerEm, T>, au::Quantity<PixelsPerEm, T>, au::Quantity<DipsPerEm, T>>;
29
30template<typename T>
31class font_size_quantity : public font_size_variant<T> {
32 using super = font_size_variant<T>;
33
34 using super::super;
35
36 template<typename O>
37 constexpr explicit font_size_quantity(au::Quantity<Dips, O> const& other) noexcept : super(dips_per_em(other.in(dips)))
38 {
39 }
40
41 template<typename O>
42 constexpr explicit font_size_quantity(au::Quantity<Pixels, O> const& other) noexcept : super(pixels_per_em(other.in(pixels)))
43 {
44 }
45
46 template<typename O>
47 constexpr explicit font_size_quantity(au::Quantity<Points, O> const& other) noexcept : super(points_per_em(other.in(points)))
48 {
49 }
50
51 template<typename O>
52 constexpr explicit font_size_quantity(length_quantity<O> const& other) : super(font_size_quantity_conversion(other))
53 {
54 }
55
56 template<typename O>
57 [[nodiscard]] constexpr friend length_quantity<O>
58 operator*(au::Quantity<EmSquares, O> const& lhs, font_size_quantity const& rhs) noexcept
59 {
60 if (auto* dips_ptr = std::get_if<au::Quantity<DipsPerEm, T>>(&rhs)) {
61 return (*dips_ptr * lhs).as(dips);
62
63 } else if (auto* pixel_ptr = std::get_if<au::Quantity<PixelsPerEm, T>>(&rhs)) {
64 return (*pixel_ptr * lhs).as(pixels);
65
66 } else if (auto* points_ptr = std::get_if<au::Quantity<PointsPerEm, T>>(&rhs)) {
67 return (*points_ptr * lhs).as(points);
68
69 } else {
70 hi_no_default();
71 }
72 }
73
74 template<typename O>
75 [[nodiscard]] constexpr friend length_quantity<O>
76 operator*(font_size_quantity const& lhs, au::Quantity<EmSquares, O> const& rhs) noexcept
77 {
78 return rhs * lhs;
79 }
80
81private:
82 template<typename O>
83 [[nodiscard]] constexpr static font_size_quantity font_size_quantity_conversion(length_quantity<O> const& other)
84 {
85 if (auto* dips_ptr = std::get_if<au::Quantity<Dips, O>>(&other)) {
86 return dips_per_em(other.in(dips));
87 } else if (auto* pixel_ptr = std::get_if<au::Quantity<Pixels, O>>(&other)) {
88 return pixels_per_em(other.in(pixels));
89 } else if (auto* points_ptr = std::get_if<au::Quantity<Points, O>>(&other)) {
90 return points_per_em(other.in(points));
91 } else {
92 throw std::bad_variant_access();
93 }
94 }
95};
96
103template<typename FontSizeT, typename ByT>
106{
107 auto const rounded_x_height = round_as(pixels, by * font_size);
108 return rounded_x_height / by;
109}
110
111using font_size_f = font_size_quantity<float>;
112using font_size_s = font_size_quantity<short>;
113
114}} // namespace v1
115}
116
117template<std::integral T>
118struct std::hash<hi::unit::font_size_quantity<T>> {
119 [[nodiscard]] size_t operator()(hi::unit::font_size_quantity<T> const& rhs) const noexcept
120 {
121 auto h = std::hash<size_t>{}(rhs.index());
122 h ^= std::visit(
123 [](auto&& rhs_) {
124 return std::hash<T>{}(rhs_.in(rhs_.unit));
125 },
126 rhs);
127 return h;
128 }
129};
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:24
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
Definition length.hpp:26
Definition au.hh:3518
T operator()(T... args)