HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
otype_coverage.hpp
1// Copyright Take Vos 2022.
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 "../geometry/geometry.hpp"
8#include "../telemetry/telemetry.hpp"
9#include "../utility/utility.hpp"
10#include "../parser/parser.hpp"
11#include <cstddef>
12#include <span>
13
14hi_export_module(hikogui.font.otype_coverage);
15
16hi_export namespace hi::inline v1 {
17
19 big_int16_buf_t coverage_format;
20 big_int16_buf_t glyph_count;
21};
22
24 big_int16_buf_t coverage_format;
25 big_int16_buf_t range_count;
26};
27
29 big_int16_buf_t start_glyph_id;
30 big_int16_buf_t end_glyph_id;
31 big_int16_buf_t start_coverage_index;
32};
33
34[[nodiscard]] std::ptrdiff_t true_type_font::get_coverage_index(std::span<std::byte const> bytes, hi::glyph_id glyph_id)
35{
36 std::size_t offset = 0;
37
38 hi_assert_or_return(*glyph_id >= 0 && *glyph_id < num_glyphs, -2);
39
40 auto const header1 = make_placement_ptr<coverage_format1>(bytes, offset);
41 if (*header1->coverage_format == 1) {
42 auto const table = make_placement_array<big_uint16_buf_t>(bytes, offset, *header1->glyph_count);
43
44 auto const it = std::lower_bound(table.begin(), table.end(), glyph_id, [](auto const &item, auto const &value) {
45 return *item < *value;
46 });
47
48 if (it != table.end() and **it == *glyph_id) {
49 return std::distance(table.begin(), it);
50 } else {
51 return -1;
52 }
53
54 } else if (*header1->coverage_format == 2) {
55 offset = 0;
56 auto const header2 = make_placement_ptr<coverage_format2>(bytes, offset);
57
58 auto const table = make_placement_array<coverage_format2_range>(bytes, offset, *header2->range_count);
59
60 auto const it = std::lower_bound(table.begin(), table.end(), glyph_id, [](auto const &item, auto const &value) {
61 return *item.end_glyph_id < *value;
62 });
63
64 if (it != table.end() and *it->start_glyph_id <= *glyph_id and *glyph_id <= *it->end_glyph_id) {
65 return *it->start_coverage_index + *glyph_id - *it->start_glyph_id;
66 } else {
67 return -1;
68 }
69
70 } else {
71 return -2;
72 }
73}
74
75} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition otype_coverage.hpp:18
Definition otype_coverage.hpp:23
Definition otype_coverage.hpp:28
Definition tagged_id.hpp:26
T distance(T... args)
T lower_bound(T... args)