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/module.hpp"
9#include <span>
10#include <cstddef>
11
12namespace hi { inline namespace v1 {
13
14[[nodiscard]] inline auto
15otype_hmtx_get(std::span<std::byte const> bytes, hi::glyph_id glyph_id, uint16_t num_horizontal_metrics, float em_scale)
16{
17 struct entry_type {
18 otype_fuword_buf_t advance_width;
19 otype_fword_buf_t left_side_bearing;
20 };
21
22 struct return_type {
23 float advance_width;
24 float left_side_bearing;
25 };
26
27 hi_axiom(num_horizontal_metrics >= 1);
28
29 auto offset = 0_uz;
30 hilet horizontal_metrics = implicit_cast<entry_type>(offset, bytes, num_horizontal_metrics);
31
32 if (*glyph_id < num_horizontal_metrics) {
33 hilet& entry = horizontal_metrics[*glyph_id];
34 return return_type{entry.advance_width * em_scale, entry.left_side_bearing * em_scale};
35 }
36
37 // In mono-type fonts the advance_width is repeating from the last entry and only the left_side_bearing is needed.
38 hilet advance_width = horizontal_metrics.back().advance_width * em_scale;
39
40 // The rest of the bytes in this table form the left_side_bearings.
41 hilet num_left_side_bearing = (bytes.size() - offset) / sizeof(otype_fword_buf_t);
42 hilet left_side_bearings = implicit_cast<otype_fword_buf_t>(offset, bytes, num_left_side_bearing);
43
44 hilet left_side_bearing_index = *glyph_id - num_horizontal_metrics;
45 hilet left_side_bearing = hi_check_at(left_side_bearings, left_side_bearing_index) * em_scale;
46 return return_type{advance_width, left_side_bearing};
47}
48
49}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_check_at(span, index)
Get an element from a span, or throw a parse_error.
Definition assert.hpp:176
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11