HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
unicode_ranges.hpp
1// Copyright Take Vos 2020-2021.
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 "grapheme.hpp"
8#include <cstdint>
9#include <bit>
10
11namespace tt {
12
16 uint32_t value[4];
17
18 unicode_ranges() noexcept
19 {
20 value[0] = 0;
21 value[1] = 0;
22 value[2] = 0;
23 value[3] = 0;
24 }
25
26 unicode_ranges(char32_t c) noexcept : unicode_ranges()
27 {
28 add(c);
29 }
30
32 {
33 for (ssize_t i = 0; i != std::ssize(g); ++i) {
34 add(g[i]);
35 }
36 }
37
38 operator bool() const noexcept
39 {
40 return (value[0] != 0) || (value[1] != 0) || (value[2] != 0) || (value[3] != 0);
41 }
42
45 void add(char32_t c) noexcept;
46
51 void add(char32_t first, char32_t last) noexcept;
52
55 [[nodiscard]] bool contains(char32_t c) const noexcept;
56
57 [[nodiscard]] bool contains(grapheme g) const noexcept
58 {
59 for (ssize_t i = 0; i != std::ssize(g); ++i) {
60 if (!contains(g[i])) {
61 return false;
62 }
63 }
64 return true;
65 }
66
67 void set_bit(int i) noexcept
68 {
69 tt_axiom(i >= 0 && i < 128);
70 value[i / 32] |= static_cast<uint32_t>(1) << (i % 32);
71 }
72
73 bool get_bit(int i) const noexcept
74 {
75 tt_axiom(i >= 0 && i < 128);
76 return (value[i / 32] & static_cast<uint32_t>(1) << (i % 32)) != 0;
77 }
78
79 int popcount() const noexcept
80 {
81 int r = 0;
82 for (int i = 0; i != 4; ++i) {
83 r += std::popcount(value[i]);
84 }
85 return r;
86 }
87
88 unicode_ranges &operator|=(unicode_ranges const &rhs) noexcept
89 {
90 for (int i = 0; i != 4; ++i) {
91 value[i] |= rhs.value[i];
92 }
93 return *this;
94 }
95
96 [[nodiscard]] friend std::string to_string(unicode_ranges const &rhs) noexcept
97 {
98 return std::format("{:08x}:{:08x}:{:08x}:{:08x}", rhs.value[3], rhs.value[2], rhs.value[1], rhs.value[0]);
99 }
100
103 [[nodiscard]] friend bool operator>=(unicode_ranges const &lhs, unicode_ranges const &rhs) noexcept
104 {
105 for (int i = 0; i < 4; i++) {
106 if (!((lhs.value[i] & rhs.value[i]) == rhs.value[i])) {
107 return false;
108 }
109 }
110 return true;
111 }
112
113 [[nodiscard]] friend unicode_ranges operator|(unicode_ranges const &lhs, unicode_ranges const &rhs) noexcept
114 {
115 auto r = lhs;
116 r |= rhs;
117 return r;
118 }
119
120 friend std::ostream &operator<<(std::ostream &lhs, unicode_ranges const &rhs)
121 {
122 return lhs << to_string(rhs);
123 }
124};
125
126} // namespace tt
127
128namespace std {
129
130template<typename CharT>
131struct std::formatter<tt::unicode_ranges, CharT> : std::formatter<std::string_view, CharT> {
132 auto format(tt::unicode_ranges const &t, auto &fc)
133 {
134 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
135 }
136};
137
138} // namespace std
STL namespace.
Definition grapheme.hpp:21
Unicode Ranges based on the OS/2 table in TrueType fonts.
Definition unicode_ranges.hpp:15
bool contains(char32_t c) const noexcept
Check if the code point is present in the unicode-ranges.
void add(char32_t c) noexcept
Add code point to unicode-ranges.
void add(char32_t first, char32_t last) noexcept
Add code points to unicode-ranges.
friend bool operator>=(unicode_ranges const &lhs, unicode_ranges const &rhs) noexcept
The lhs has at least all bits on the rhs set.
Definition unicode_ranges.hpp:103
T to_string(T... args)