7#include "otype_utilities.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
13hi_export_module(hikogui.font.otype_kern);
15hi_export
namespace hi {
inline namespace v1 {
17[[nodiscard]]
inline std::optional<float>
18otype_kern_sub0_find(
size_t &offset, std::span<std::byte const> bytes, glyph_id first_glyph_id, glyph_id second_glyph_id,
float em_scale)
21 big_uint16_buf_t num_pairs;
22 big_uint16_buf_t search_range;
23 big_uint16_buf_t entry_selector;
24 big_uint16_buf_t range_shift;
28 big_uint16_buf_t
left;
29 big_uint16_buf_t
right;
30 otype_fword_buf_t value;
33 auto const& header = implicit_cast<header_type>(offset, bytes);
34 auto const entries = implicit_cast<entry_type>(offset, bytes, *header.num_pairs);
36 auto const key = (wide_cast<uint32_t>(*first_glyph_id) << 16) | wide_cast<uint32_t>(*second_glyph_id);
37 if (
auto const entry_ptr = fast_binary_search_eq<std::endian::big>(entries, key)) {
38 return entry_ptr->value * em_scale;
49[[nodiscard]]
inline vector2
50otype_kern_v0_find(std::span<std::byte const> bytes, glyph_id first_glyph_id, glyph_id second_glyph_id,
float em_scale)
53 big_uint16_buf_t version;
54 big_uint16_buf_t num_tables;
58 big_uint16_buf_t version;
59 big_uint16_buf_t length;
60 big_uint16_buf_t coverage;
64 auto const& header = implicit_cast<header_type>(offset, bytes);
65 hi_check(*header.version == 0,
"'kern' table expect version to be version 0.");
66 auto const num_tables = *header.num_tables;
70 for (
auto i = 0_uz; i != num_tables; ++i) {
71 auto const& entry = implicit_cast<entry_type>(offset, bytes);
72 hi_check(*entry.version == 0,
"'kern' expect sub-table version to be 0.");
82 auto const entry_coverage = *entry.coverage;
84 auto const cross_stream = to_bool(entry_coverage & 0x0004);
85 hi_check(not cross_stream,
"'kern' this font contains cross-stream kerning which is unsuported.");
87 auto const format = entry_coverage >> 8;
88 hi_check(format == 0,
"'kern' this font contains a unsuported subtable.");
90 auto const kerning = otype_kern_sub0_find(offset, bytes, first_glyph_id, second_glyph_id, em_scale);
92 auto const horizontal = to_bool(entry_coverage & 0x0001);
93 auto const minimum = to_bool(entry_coverage & 0x0002);
94 auto const overwrite = to_bool(entry_coverage & 0x0008);
97 r = horizontal ?
vector2{*kerning, 0.0f} :
vector2{0.0f, *kerning};
107 r += horizontal ?
vector2{*kerning, 0.0f} :
vector2{0.0f, *kerning};
114[[nodiscard]]
inline vector2
115otype_kern_v1_find(std::span<std::byte const> bytes, glyph_id first_glyph_id, glyph_id second_glyph_id,
float em_scale)
118 big_uint32_buf_t version;
119 big_uint32_buf_t num_tables;
123 big_uint32_buf_t length;
124 big_uint16_buf_t coverage;
125 big_uint16_buf_t tuple_index;
129 auto const& header = implicit_cast<header_type>(offset, bytes);
130 hi_check(*header.version == 0x00010000,
"'kern' table expect version to be version 0x00010000.");
131 auto const num_tables = *header.num_tables;
135 for (
auto i = 0_uz; i != num_tables; ++i) {
136 auto sub_table_offset = offset;
137 auto const& entry = implicit_cast<entry_type>(sub_table_offset, bytes);
139 auto const entry_length = *entry.length;
140 hi_check(entry_length >=
sizeof(entry_type),
"'kern' subtable length is invalid.");
144 offset += entry_length;
147 auto const entry_coverage = *entry.coverage;
149 auto const cross_stream = to_bool(entry_coverage & 0x4000);
158 auto const variation = to_bool(entry_coverage & 0x2000);
159 if (variation or *entry.tuple_index != 0) {
164 auto const format = entry_coverage & 0xff;
166 auto const kerning = [&]() -> std::optional<float> {
168 return otype_kern_sub0_find(sub_table_offset, bytes, first_glyph_id, second_glyph_id, em_scale);
175 auto const vertical = to_bool(entry_coverage & 0x8000);
177 auto const kerning_2D = vertical ? vector2{0.0f, *kerning} : vector2{*kerning, 0.0f};
184[[nodiscard]]
inline vector2
185otype_kern_find(std::span<std::byte const> bytes, glyph_id first_glyph_id, glyph_id second_glyph_id,
float em_scale)
192 auto const version = *implicit_cast<big_uint16_buf_t>(bytes);
196 return otype_kern_v1_find(bytes, first_glyph_id, second_glyph_id, em_scale);
The HikoGUI namespace.
Definition array_generic.hpp:20
vector2 otype_kern_v0_find(std::span< std::byte const > bytes, glyph_id first_glyph_id, glyph_id second_glyph_id, float em_scale)
'kern' version 0 find.
Definition otype_kern.hpp:50
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:65