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
9hi_export_module(hikogui.font.font_metrics);
10
11namespace hi::inline v1 {
12
19hi_export struct font_metrics {
22 float ascender = 0.0f;
23
28 float descender = 0.0f;
29
34 float line_gap = 0.0f;
35
38 float cap_height = 0.0f;
39
42 float x_height = 0.0f;
43
48 float digit_advance = 0.0f;
49
53 float line_spacing = 1.0f;
54
58 float paragraph_spacing = 1.5f;
59
60 ~font_metrics() = default;
61 constexpr font_metrics() noexcept = default;
62 constexpr font_metrics(font_metrics const&) noexcept = default;
63 constexpr font_metrics(font_metrics&&) noexcept = default;
64 constexpr font_metrics& operator=(font_metrics const&) noexcept = default;
65 constexpr font_metrics& operator=(font_metrics&&) noexcept = default;
66 [[nodiscard]] constexpr friend bool operator==(font_metrics const&, font_metrics const&) noexcept = default;
67
70 [[nodiscard]] constexpr friend font_metrics operator*(float const& lhs, font_metrics const& rhs) noexcept
71 {
73 r.ascender = lhs * rhs.ascender;
74 r.descender = lhs * rhs.descender;
75 r.line_gap = lhs * rhs.line_gap;
76 r.cap_height = lhs * rhs.cap_height;
77 r.x_height = lhs * rhs.x_height;
78 r.digit_advance = lhs * rhs.digit_advance;
79 return r;
80 }
81
82 [[nodiscard]] constexpr friend font_metrics max(font_metrics const& a, font_metrics const& b) noexcept
83 {
85 r.ascender = std::max(a.ascender, b.ascender);
86 r.descender = std::max(a.descender, b.descender);
87 r.line_gap = std::max(a.line_gap, b.line_gap);
88 r.cap_height = std::max(a.cap_height, b.cap_height);
89 r.x_height = std::max(a.x_height, b.x_height);
90 r.digit_advance = std::max(a.digit_advance, b.digit_advance);
91 return r;
92 }
93
96 [[nodiscard]] float round_scale(float size) const noexcept
97 {
98 return std::round(x_height * size) / x_height;
99 }
100};
101
102} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
The metrics of a font.
Definition font_metrics.hpp:19
float descender
Distance from baseline of lowest descender.
Definition font_metrics.hpp:28
float line_gap
Distance between lines.
Definition font_metrics.hpp:34
float ascender
Distance from baseline of highest ascender.
Definition font_metrics.hpp:22
float round_scale(float size) const noexcept
Round a scale so that the scaled x-height is an integral.
Definition font_metrics.hpp:96
float x_height
Height of lower case characters without ascenders or descenders, or the small letter 'x'.
Definition font_metrics.hpp:42
float cap_height
Height of capital letter, or height of the letter 'H'.
Definition font_metrics.hpp:38
float digit_advance
The advance for digits, specifically the digit '8'.
Definition font_metrics.hpp:48
T max(T... args)
T round(T... args)