Repository navigation
Expand file tree
/
Copy pathunits.hpp
More file actions
72 lines (58 loc) · 2.5 KB
/
Copy pathunits.hpp
File metadata and controls
72 lines (58 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "mfl/detail/quantity.hpp"
#include <array>
#include <format>
namespace mfl
{
using dots_per_inch = detail::quantity<struct dots_per_inch_tag>;
using inches = detail::quantity<struct inches_tag>;
using pixels = detail::quantity<struct pixels_tag>;
using points = detail::quantity<struct points_tag>;
[[nodiscard]] pixels inches_to_pixels(const inches x, const dots_per_inch dpi);
[[nodiscard]] points pixels_to_points(const pixels x, const dots_per_inch dpi);
[[nodiscard]] pixels points_to_pixels(const points x, const dots_per_inch dpi);
namespace units_literals
{
constexpr inches operator""_in(const long double x) { return inches{static_cast<double>(x)}; }
constexpr inches operator""_in(const unsigned long long x) { return inches{static_cast<double>(x)}; }
constexpr dots_per_inch operator""_dpi(const long double x) { return dots_per_inch{static_cast<double>(x)}; }
constexpr dots_per_inch operator""_dpi(const unsigned long long x)
{
return dots_per_inch{static_cast<double>(x)};
}
constexpr pixels operator""_px(const long double x) { return pixels{static_cast<double>(x)}; }
constexpr pixels operator""_px(const unsigned long long x) { return pixels{static_cast<double>(x)}; }
constexpr points operator""_pt(const long double x) { return points{static_cast<double>(x)}; }
constexpr points operator""_pt(const unsigned long long x) { return points{static_cast<double>(x)}; }
}
template <typename Unit, char c0 = 0, char c1 = 0, char c2 = 0>
struct unit_formatter
{
constexpr auto parse(const std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
constexpr auto format(const Unit x, FormatContext& ctx) const
{
constexpr auto suffix = std::array{c0, c1, c2, '\0'};
return std::format_to(ctx.out(), "{}{}", x.value(), suffix.data());
}
};
}
namespace std
{
template <>
struct [[maybe_unused]] formatter<mfl::inches> : mfl::unit_formatter<mfl::inches, '\"'>
{
};
template <>
struct [[maybe_unused]] formatter<mfl::dots_per_inch> : mfl::unit_formatter<mfl::dots_per_inch, 'd', 'p', 'i'>
{
};
template <>
struct [[maybe_unused]] formatter<mfl::pixels> : mfl::unit_formatter<mfl::pixels, 'p', 'x'>
{
};
template <>
struct [[maybe_unused]] formatter<mfl::points> : mfl::unit_formatter<mfl::points, 'p', 't'>
{
};
}