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 <algorithm>
9#include <cmath>
10
11hi_export_module(hikogui.font.font_metrics);
12
13hi_export namespace hi::inline v1 {
14
21hi_export struct font_metrics {
24 float ascender = 0.0f;
25
30 float descender = 0.0f;
31
36 float line_gap = 0.0f;
37
40 float cap_height = 0.0f;
41
44 float x_height = 0.0f;
45
50 float digit_advance = 0.0f;
51
55 float line_spacing = 1.0f;
56
60 float paragraph_spacing = 1.5f;
61
62 ~font_metrics() = default;
63 constexpr font_metrics() noexcept = default;
64 constexpr font_metrics(font_metrics const&) noexcept = default;
65 constexpr font_metrics(font_metrics&&) noexcept = default;
66 constexpr font_metrics& operator=(font_metrics const&) noexcept = default;
67 constexpr font_metrics& operator=(font_metrics&&) noexcept = default;
68 [[nodiscard]] constexpr friend bool operator==(font_metrics const&, font_metrics const&) noexcept = default;
69
72 [[nodiscard]] constexpr friend font_metrics operator*(float const& lhs, font_metrics const& rhs) noexcept
73 {
75 r.ascender = lhs * rhs.ascender;
76 r.descender = lhs * rhs.descender;
77 r.line_gap = lhs * rhs.line_gap;
78 r.cap_height = lhs * rhs.cap_height;
79 r.x_height = lhs * rhs.x_height;
80 r.digit_advance = lhs * rhs.digit_advance;
81 return r;
82 }
83
84 [[nodiscard]] constexpr friend font_metrics max(font_metrics const& a, font_metrics const& b) noexcept
85 {
87 r.ascender = std::max(a.ascender, b.ascender);
88 r.descender = std::max(a.descender, b.descender);
89 r.line_gap = std::max(a.line_gap, b.line_gap);
90 r.cap_height = std::max(a.cap_height, b.cap_height);
91 r.x_height = std::max(a.x_height, b.x_height);
92 r.digit_advance = std::max(a.digit_advance, b.digit_advance);
93 return r;
94 }
95
98 [[nodiscard]] float round_scale(float size) const noexcept
99 {
100 return std::round(x_height * size) / x_height;
101 }
102};
103
104} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
The metrics of a font.
Definition font_metrics.hpp:21
float descender
Distance from baseline of lowest descender.
Definition font_metrics.hpp:30
float line_gap
Distance between lines.
Definition font_metrics.hpp:36
float ascender
Distance from baseline of highest ascender.
Definition font_metrics.hpp:24
float round_scale(float size) const noexcept
Round a scale so that the scaled x-height is an integral.
Definition font_metrics.hpp:98
float x_height
Height of lower case characters without ascenders or descenders, or the small letter 'x'.
Definition font_metrics.hpp:44
float cap_height
Height of capital letter, or height of the letter 'H'.
Definition font_metrics.hpp:40
float digit_advance
The advance for digits, specifically the digit '8'.
Definition font_metrics.hpp:50
T max(T... args)
T round(T... args)