8#include "glyph_atlas_info.hpp"
9#include "../graphic_path.hpp"
10#include "../geometry/axis_aligned_rectangle.hpp"
23hi_warning_ignore_msvc(26401);
26hi_warning_ignore_msvc(26409);
28namespace hi::inline
v1 {
49 _num_graphemes = (value >> 4) & 0xf;
50 std::get<0>(_glyphs) =
glyph_id{(value >> 16) & 0xffff};
51 std::get<1>(_glyphs) = new_id;
54 _num_graphemes = (value >> 4) & 0xf;
55 std::get<0>(_glyphs) =
glyph_id{(value >> 16) & 0xffff};
56 std::get<1>(_glyphs) =
glyph_id{(value >> 32) & 0xffff};
57 std::get<2>(_glyphs) =
glyph_id{(value >> 48) & 0xffff};
58 std::get<3>(_glyphs) = new_id;
62 [[nodiscard]]
constexpr std::size_t num_glyphs() const noexcept
67 [[nodiscard]]
constexpr std::size_t num_graphemes() const noexcept
69 return _num_graphemes;
72 constexpr void set_num_graphemes(
std::size_t num_graphemes)
noexcept
75 _num_graphemes = narrow_cast<uint8_t>(num_graphemes);
78 [[nodiscard]]
constexpr std::size_t hash() const noexcept
82 for (
auto i = 0_uz; i != _num_glyphs; ++i) {
84 r ^= std::rotl(r, 16);
90 constexpr glyph_ids_long& operator+=(glyph_id
id)
noexcept
93 if (_num_glyphs < _glyphs.size()) {
94 _glyphs[_num_glyphs++] = id;
99 [[nodiscard]]
constexpr glyph_id
const& operator[](
std::size_t i)
const noexcept
105 template<std::
size_t I>
106 [[nodiscard]]
constexpr friend glyph_id get(glyph_ids_long
const& rhs)
noexcept
109 return std::get<I>(rhs._glyphs);
112 [[nodiscard]]
constexpr friend bool operator==(glyph_ids_long
const&, glyph_ids_long
const&)
noexcept =
default;
116 uint8_t _num_graphemes;
144 constexpr glyph_ids(
glyph_ids const& other) noexcept : _font(other._font), _ptr(other._ptr)
153 hi_return_on_self_assignment(other);
166 constexpr glyph_ids(
glyph_ids&& other) noexcept : _font(other._font), _ptr(std::exchange(other._ptr, make_ptr(1))) {}
180 constexpr glyph_ids() noexcept : _font(
nullptr), _ptr(make_ptr(1)) {}
192 [[nodiscard]]
constexpr font const&
font() const noexcept
219 [[nodiscard]]
constexpr bool empty() const noexcept
221 return _ptr == make_ptr(1);
226 constexpr operator bool() const noexcept
238 template<std::
size_t N>
241 static_assert(N <= num_glyphs_mask);
243 constexpr std::size_t mask = (num_glyphs_mask << num_glyphs_shift) | 1;
244 constexpr std::size_t value = (N << num_glyphs_shift) | 1;
245 return (make_value(_ptr) & mask) == value;
260 return is_long() ? _ptr->num_glyphs() : (make_value(_ptr) >> 1) & num_glyphs_mask;
263 [[nodiscard]]
constexpr std::size_t num_graphemes() const noexcept
265 return is_long() ? _ptr->num_graphemes() : (make_value(_ptr) >> 4) & 0xf;
268 constexpr void set_num_graphemes(
std::size_t num_graphemes)
noexcept
271 _ptr->set_num_graphemes(num_graphemes);
273 hi_axiom(num_graphemes <= num_graphemes_mask);
275 (make_value(_ptr) & ~(num_graphemes_mask << num_graphemes_shift)) | (num_graphemes << num_graphemes_shift));
283 return is_long() ? _ptr->hash() : make_value(_ptr);
295 }
else if (
hilet index = short_num_glyphs(); index < num_glyphs_mask) {
296 increment_num_glyphs();
297 set_glyph(index,
id);
314 return (*_ptr)[index];
316 return get_glyph(index);
320 template<std::
size_t I>
324 return get<I>(*rhs._ptr);
327 return glyph_id{(make_value(rhs._ptr) >> shift) & 0xffff};
331 [[nodiscard]]
constexpr friend bool operator==(glyph_ids
const& lhs, glyph_ids
const& rhs)
noexcept
333 hilet lhs_value = make_value(lhs._ptr);
334 hilet rhs_value = make_value(rhs._ptr);
336 if (lhs._font != rhs._font) {
338 }
else if (lhs_value == rhs_value) {
341 return ((lhs_value | rhs_value) & 1) == 0 and *lhs._ptr == *rhs._ptr;
362 static_assert(sizeof(
std::
size_t) == sizeof(detail::glyph_ids_long *));
364 static constexpr
std::
size_t num_glyphs_shift = 1;
365 static constexpr
std::
size_t num_glyphs_mask = sizeof(
std::
size_t) == 4 ? 1 : 3;
366 static constexpr
std::
size_t num_graphemes_shift = 4;
367 static constexpr
std::
size_t num_graphemes_mask = 15;
382 detail::glyph_ids_long *_ptr;
384 constexpr
void increment_num_glyphs() noexcept
387 hi_axiom(short_num_glyphs() < num_glyphs_mask);
389 _ptr = make_ptr(make_value(_ptr) + (1 << num_glyphs_shift));
400 constexpr void set_glyph(
std::size_t i, glyph_id
id)
noexcept
406 _ptr = make_ptr((make_value(_ptr) & ~mask) | (
static_cast<std::size_t>(
id) << shift));
409 [[nodiscard]]
constexpr std::size_t short_num_glyphs() const noexcept
412 return (make_value(_ptr) >> num_glyphs_shift) & num_glyphs_mask;
415 [[nodiscard]]
constexpr bool is_short() const noexcept
417 return to_bool(make_value(_ptr) & 1);
420 [[nodiscard]]
constexpr bool is_long() const noexcept
422 return not is_short();
425 [[nodiscard]]
static constexpr detail::glyph_ids_long *make_ptr(
std::size_t value)
noexcept
427 return std::bit_cast<detail::glyph_ids_long *>(value);
430 [[nodiscard]]
static constexpr std::size_t make_value(detail::glyph_ids_long *ptr)
noexcept
432 return std::bit_cast<std::size_t>(ptr);
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_assert_not_null(x)
Assert if an expression is not nullptr.
Definition assert.hpp:118
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
@ shift
The shift key is being held.
The HikoGUI namespace.
Definition ascii.hpp:19
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
A path is a vector graphics object.
Definition graphic_path.hpp:29
Definition glyph_atlas_info.hpp:15
Definition glyph_ids.hpp:33
constexpr glyph_ids_long(std::size_t value, glyph_id new_id) noexcept
Construct a list of glyphs starting with a packed set of glyphs.
Definition glyph_ids.hpp:45
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:135
glyph_atlas_info & atlas_info() const noexcept
Get information where the glyph is drawn in the atlas.
constexpr glyph_id get_single() const noexcept
Get the value of the single glyph.
Definition glyph_ids.hpp:250
constexpr std::size_t num_glyphs() const noexcept
Get the number of glyphs.
Definition glyph_ids.hpp:258
constexpr font const & font() const noexcept
Get the font for this glyph_ids object.
Definition glyph_ids.hpp:192
constexpr std::size_t hash() const noexcept
Get the hash value.
Definition glyph_ids.hpp:281
constexpr glyph_ids() noexcept
Create an empty glyph_ids object.
Definition glyph_ids.hpp:180
constexpr glyph_ids & operator+=(glyph_id id) noexcept
Add a glyph to this object.
Definition glyph_ids.hpp:290
constexpr bool has_num_glyphs() const noexcept
Check if this object contains a number of glyphs.
Definition glyph_ids.hpp:239
constexpr bool empty() const noexcept
Check if glyphs are attached.
Definition glyph_ids.hpp:219
constexpr glyph_id operator[](std::size_t index) const noexcept
Get a glyph.
Definition glyph_ids.hpp:309
constexpr void clear() noexcept
Clear the glyphs in this glyph_ids object.
Definition glyph_ids.hpp:209
void set_font(hi::font const &font) noexcept
Set the font for this glyph_ids object.
Definition glyph_ids.hpp:200
constexpr glyph_ids(hi::font const &font) noexcept
Create an empty glyph_ids for a font.
Definition glyph_ids.hpp:188