HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_metrics.hpp
1// Copyright Take Vos 2021.
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 "../macros.hpp"
8#include "../units/units.hpp"
9#include <algorithm>
10#include <cmath>
11
12hi_export_module(hikogui.font.font_metrics);
13
14hi_export namespace hi::inline v1 {
21template<typename Unit, typename T>
22hi_export struct font_metrics {
26
32
38
42
46
52
56 float line_spacing = 0.0f;
57
61 float paragraph_spacing = 0.0f;
62
63 ~font_metrics() = default;
64 constexpr font_metrics() noexcept = default;
65 constexpr font_metrics(font_metrics const&) noexcept = default;
66 constexpr font_metrics(font_metrics&&) noexcept = default;
67 constexpr font_metrics& operator=(font_metrics const&) noexcept = default;
68 constexpr font_metrics& operator=(font_metrics&&) noexcept = default;
69 [[nodiscard]] constexpr friend bool operator==(font_metrics const&, font_metrics const&) noexcept = default;
70
73 template<typename LhsUnit, typename LhsT>
74 [[nodiscard]] constexpr friend auto operator*(au::Quantity<LhsUnit, LhsT> const& lhs, font_metrics const& rhs) noexcept requires std::same_as<Unit, EmSquares>
75 {
76 // clang-format off
77 using result_unit =
78 std::conditional_t<std::is_same_v<LhsUnit, PixelsPerEm>, Pixels,
79 std::conditional_t<std::is_same_v<LhsUnit, PointsPerEm>, Points,
80 std::conditional_t<std::is_same_v<LhsUnit, DipsPerEm>, Dips,
81 void>>>;
82 // clang-format on
83
85 r.ascender = lhs * rhs.ascender;
86 r.descender = lhs * rhs.descender;
87 r.line_gap = lhs * rhs.line_gap;
88 r.cap_height = lhs * rhs.cap_height;
89 r.x_height = lhs * rhs.x_height;
90 r.digit_advance = lhs * rhs.digit_advance;
91 r.line_spacing = rhs.line_spacing;
92 r.paragraph_spacing = rhs.paragraph_spacing;
93 return r;
94 }
95
96 [[nodiscard]] constexpr friend font_metrics max(font_metrics const& a, font_metrics const& b) noexcept
97 {
98 auto r = font_metrics{};
99 r.ascender = std::max(a.ascender, b.ascender);
100 r.descender = std::max(a.descender, b.descender);
101 r.line_gap = std::max(a.line_gap, b.line_gap);
102 r.cap_height = std::max(a.cap_height, b.cap_height);
103 r.x_height = std::max(a.x_height, b.x_height);
104 r.digit_advance = std::max(a.digit_advance, b.digit_advance);
105 r.line_spacing = std::max(a.line_spacing, b.line_spacing);
106 r.paragraph_spacing = std::max(a.paragraph_spacing, b.paragraph_spacing);
107 return r;
108 }
109};
110
111using font_metrics_em = font_metrics<EmSquares, float>;
112using font_metrics_pt = font_metrics<Points, float>;
113using font_metrics_px = font_metrics<Pixels, float>;
114
115} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The metrics of a font.
Definition font_metrics.hpp:22
au::Quantity< Unit, T > ascender
Distance from baseline of highest ascender.
Definition font_metrics.hpp:25
au::Quantity< Unit, T > line_gap
Distance between lines.
Definition font_metrics.hpp:37
au::Quantity< Unit, T > descender
Distance from baseline of lowest descender.
Definition font_metrics.hpp:31
au::Quantity< Unit, T > cap_height
Height of capital letter, or height of the letter 'H'.
Definition font_metrics.hpp:41
au::Quantity< Unit, T > x_height
Height of lower case characters without ascenders or descenders, or the small letter 'x'.
Definition font_metrics.hpp:45
au::Quantity< Unit, T > digit_advance
The advance for digits, specifically the digit '8'.
Definition font_metrics.hpp:51
Definition au.hh:3518
T max(T... args)