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 value[0] = 0;
20 value[1] = 0;
21 value[2] = 0;
22 value[3] = 0;
23 }
24
25 unicode_ranges(char32_t c) noexcept : unicode_ranges() {
26 add(c);
27 }
28
29 unicode_ranges(grapheme g) noexcept : unicode_ranges() {
30 for (ssize_t i = 0; i != std::ssize(g); ++i) {
31 add(g[i]);
32 }
33 }
34
35 operator bool () const noexcept {
36 return (value[0] != 0) || (value[1] != 0) || (value[2] != 0) || (value[3] != 0);
37 }
38
41 void add(char32_t c) noexcept;
42
47 void add(char32_t first, char32_t last) noexcept;
48
51 [[nodiscard]] bool contains(char32_t c) const noexcept;
52
53 [[nodiscard]] bool contains(grapheme g) const noexcept {
54 for (ssize_t i = 0; i != std::ssize(g); ++i) {
55 if (!contains(g[i])) {
56 return false;
57 }
58 }
59 return true;
60 }
61
62 void set_bit(int i) noexcept {
63 tt_axiom(i >= 0 && i < 128);
64 value[i / 32] |= static_cast<uint32_t>(1) << (i % 32);
65 }
66
67 bool get_bit(int i) const noexcept {
68 tt_axiom(i >= 0 && i < 128);
69 return (value[i / 32] & static_cast<uint32_t>(1) << (i % 32)) != 0;
70 }
71
72 int popcount() const noexcept {
73 int r = 0;
74 for (int i = 0; i != 4; ++i) {
75 r += std::popcount(value[i]);
76 }
77 return r;
78 }
79
80
81 unicode_ranges &operator|=(unicode_ranges const &rhs) noexcept {
82 for (int i = 0; i != 4; ++i) {
83 value[i] |= rhs.value[i];
84 }
85 return *this;
86 }
87
88 [[nodiscard]] friend std::string to_string(unicode_ranges const &rhs) noexcept {
89 return fmt::format("{:08x}:{:08x}:{:08x}:{:08x}", rhs.value[3], rhs.value[2], rhs.value[1], rhs.value[0]);
90 }
91
94 [[nodiscard]] friend bool operator>=(unicode_ranges const &lhs, unicode_ranges const &rhs) noexcept {
95 for (int i = 0; i < 4; i++) {
96 if (!((lhs.value[i] & rhs.value[i]) == rhs.value[i])) {
97 return false;
98 }
99 }
100 return true;
101 }
102
103 [[nodiscard]] friend unicode_ranges operator|(unicode_ranges const &lhs, unicode_ranges const &rhs) noexcept {
104 auto r = lhs;
105 r |= rhs;
106 return r;
107 }
108
109 friend std::ostream &operator<<(std::ostream &lhs, unicode_ranges const &rhs) {
110 return lhs << to_string(rhs);
111 }
112};
113
114}
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:94