HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
otype_hmtx.hpp
1// Copyright Take Vos 2023.
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 "otype_utilities.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <span>
11#include <cstddef>
12
13hi_export_module(hikogui.font.otype_htmx);
14
15hi_export namespace hi { inline namespace v1 {
16
17[[nodiscard]] inline auto
18otype_hmtx_get(std::span<std::byte const> bytes, hi::glyph_id glyph_id, uint16_t num_horizontal_metrics, float em_scale)
19{
20 struct entry_type {
21 otype_fuword_buf_t advance_width;
22 otype_fword_buf_t left_side_bearing;
23 };
24
25 struct return_type {
26 float advance_width;
27 float left_side_bearing;
28 };
29
30 hi_axiom(num_horizontal_metrics >= 1);
31
32 auto offset = 0_uz;
33 auto const horizontal_metrics = implicit_cast<entry_type>(offset, bytes, num_horizontal_metrics);
34
35 if (*glyph_id < num_horizontal_metrics) {
36 auto const& entry = horizontal_metrics[*glyph_id];
37 return return_type{entry.advance_width * em_scale, entry.left_side_bearing * em_scale};
38 }
39
40 // In mono-type fonts the advance_width is repeating from the last entry and only the left_side_bearing is needed.
41 auto const advance_width = horizontal_metrics.back().advance_width * em_scale;
42
43 // The rest of the bytes in this table form the left_side_bearings.
44 auto const num_left_side_bearing = (bytes.size() - offset) / sizeof(otype_fword_buf_t);
45 auto const left_side_bearings = implicit_cast<otype_fword_buf_t>(offset, bytes, num_left_side_bearing);
46
47 auto const left_side_bearing_index = *glyph_id - num_horizontal_metrics;
48 auto const left_side_bearing = hi_check_at(left_side_bearings, left_side_bearing_index) * em_scale;
49 return return_type{advance_width, left_side_bearing};
50}
51
52}} // namespace hi::v1
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20