11#include "glyph_id.hpp"
12#include "../algorithm.hpp"
13#include "../utility/module.hpp"
20namespace hi {
inline namespace v1 {
38 [[nodiscard]]
constexpr bool empty()
const noexcept
53 constexpr size_t count() const noexcept
66 for (
hilet &entry: _map) {
68 for (
auto cp = entry.start_code_point(); cp <= entry.end_code_point; ++cp) {
69 if (not mask.test(cp)) {
84 [[nodiscard]]
constexpr void add(
char32_t start_code_point,
char32_t end_code_point, uint16_t start_glyph)
noexcept
89 hi_axiom(start_code_point <= end_code_point);
90 auto todo = wide_cast<size_t>(end_code_point - start_code_point + 1);
92 hi_axiom(start_glyph + todo < 0xffff,
"Only glyph_ids 0 through 0xfffe are valid");
98 _map.
emplace_back(start_code_point, char_cast<char32_t>(start_code_point + doing - 1), start_glyph);
101 start_code_point += narrow_cast<char32_t>(doing);
102 start_glyph += narrow_cast<uint16_t>(doing);
119 return a.end_code_point < b.end_code_point;
122 auto it = _map.
begin();
124 while (it != _map.
end()) {
125 hi_axiom(prev_it->end_code_point < it->start_code_point());
127 if (mergable(*prev_it, *it)) {
128 hilet merged_count =
std::min(prev_it->count() + it->count(), entry_type::max_count);
129 hilet move_count = merged_count - prev_it->count();
130 hi_axiom(move_count <= entry_type::max_count);
132 prev_it->end_code_point += narrow_cast<char32_t>(move_count);
133 prev_it->set_count(prev_it->count() + move_count);
135 if (move_count == it->count()) {
142 it->start_glyph += narrow_cast<uint16_t>(move_count);
143 it->set_count(it->count() - move_count);
162 [[nodiscard]]
inline glyph_id
find(
char32_t code_point)
const noexcept
168 if (
hilet item_ptr = fast_lower_bound(std::span{_map}, char_cast<uint32_t>(code_point))) {
169 return item_ptr->get(code_point);
176 constexpr static size_t max_count = 0x1'0000;
178 char32_t end_code_point;
179 uint16_t start_glyph;
182 constexpr entry_type(
char32_t start_code_point,
char32_t end_code_point, uint16_t start_glyph) noexcept :
183 end_code_point(end_code_point),
184 start_glyph(start_glyph),
185 _count(narrow_cast<uint16_t>(end_code_point - start_code_point))
187 hi_axiom(start_code_point <= end_code_point);
190 [[nodiscard]]
constexpr size_t count() const noexcept
192 return wide_cast<size_t>(_count) + 1;
195 [[nodiscard]]
constexpr void set_count(
size_t new_count)
noexcept
199 _count = narrow_cast<uint16_t>(new_count - 1);
202 [[nodiscard]]
constexpr char32_t start_code_point() const noexcept
204 return end_code_point - _count;
207 [[nodiscard]]
constexpr uint16_t end_glyph() const noexcept
209 return narrow_cast<uint16_t>(start_glyph + _count);
212 [[nodiscard]]
constexpr friend bool mergable(entry_type
const& lhs, entry_type
const& rhs)
noexcept
214 return lhs.end_code_point + 1 == rhs.start_code_point() and lhs.end_glyph() + 1 == rhs.start_glyph;
217 [[nodiscard]]
constexpr glyph_id get(
char32_t code_point)
const noexcept
219 auto diff = wide_cast<ptrdiff_t>(code_point) - wide_cast<ptrdiff_t>(end_code_point);
228 return glyph_id{truncate<uint16_t>(diff)};
239 bool _prepared =
false;
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#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
Character map of a font.
Definition font_char_map.hpp:30
constexpr void reserve(size_t n)
Reserve space for a set of ranges to be added.
Definition font_char_map.hpp:46
glyph_id find(char32_t code_point) const noexcept
Find a glyph for a code_point.
Definition font_char_map.hpp:162
constexpr size_t count() const noexcept
Get the number of code-points supported by the char-map.
Definition font_char_map.hpp:53
constexpr void add(char32_t start_code_point, char32_t end_code_point, uint16_t start_glyph) noexcept
Add a range of code points.
Definition font_char_map.hpp:84
constexpr size_t update_mask(std::bitset< 0x11 '0000 > &mask) const noexcept
Update a code-point mask.
Definition font_char_map.hpp:63
void prepare() noexcept
Prepare the map for searching.
Definition font_char_map.hpp:111
T emplace_back(T... args)
T shrink_to_fit(T... args)