HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
UnicodeRanges.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Text/Grapheme.hpp"
7#include <cstdint>
8
9namespace tt {
10
14 uint32_t value[4];
15
16 UnicodeRanges() noexcept {
17 value[0] = 0;
18 value[1] = 0;
19 value[2] = 0;
20 value[3] = 0;
21 }
22
23 UnicodeRanges(char32_t c) noexcept : UnicodeRanges() {
24 add(c);
25 }
26
27 UnicodeRanges(Grapheme g) noexcept : UnicodeRanges() {
28 for (ssize_t i = 0; i != ssize(g); ++i) {
29 add(g[i]);
30 }
31 }
32
33 operator bool () const noexcept {
34 return (value[0] != 0) || (value[1] != 0) || (value[2] != 0) || (value[3] != 0);
35 }
36
39 void add(char32_t c) noexcept;
40
45 void add(char32_t first, char32_t last) noexcept;
46
49 [[nodiscard]] bool contains(char32_t c) const noexcept;
50
51 [[nodiscard]] bool contains(Grapheme g) const noexcept {
52 for (ssize_t i = 0; i != ssize(g); ++i) {
53 if (!contains(g[i])) {
54 return false;
55 }
56 }
57 return true;
58 }
59
60 void set_bit(int i) noexcept {
61 tt_assume(i >= 0 && i < 128);
62 value[i / 32] |= static_cast<uint32_t>(1) << (i % 32);
63 }
64
65 bool get_bit(int i) const noexcept {
66 tt_assume(i >= 0 && i < 128);
67 return (value[i / 32] & static_cast<uint32_t>(1) << (i % 32)) != 0;
68 }
69
70 int popcount() const noexcept {
71 int r = 0;
72 for (int i = 0; i != 4; ++i) {
73 r += ::tt::popcount(value[i]);
74 }
75 return r;
76 }
77
78
79 UnicodeRanges &operator|=(UnicodeRanges const &rhs) noexcept {
80 for (int i = 0; i != 4; ++i) {
81 value[i] |= rhs.value[i];
82 }
83 return *this;
84 }
85
86 [[nodiscard]] friend std::string to_string(UnicodeRanges const &rhs) noexcept {
87 return fmt::format("{:08x}:{:08x}:{:08x}:{:08x}", rhs.value[3], rhs.value[2], rhs.value[1], rhs.value[0]);
88 }
89
92 [[nodiscard]] friend bool operator>=(UnicodeRanges const &lhs, UnicodeRanges const &rhs) noexcept {
93 for (int i = 0; i < 4; i++) {
94 if (!((lhs.value[i] & rhs.value[i]) == rhs.value[i])) {
95 return false;
96 }
97 }
98 return true;
99 }
100
101 [[nodiscard]] friend UnicodeRanges operator|(UnicodeRanges const &lhs, UnicodeRanges const &rhs) noexcept {
102 auto r = lhs;
103 r |= rhs;
104 return r;
105 }
106
107 friend std::ostream &operator<<(std::ostream &lhs, UnicodeRanges const &rhs) {
108 return lhs << to_string(rhs);
109 }
110};
111
112}
Definition Grapheme.hpp:20
Unicode Ranges based on the OS/2 table in TrueType fonts.
Definition UnicodeRanges.hpp:13
void add(char32_t first, char32_t last) noexcept
Add code points to unicode-ranges.
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.
friend bool operator>=(UnicodeRanges const &lhs, UnicodeRanges const &rhs) noexcept
The lhs has at least all bits on the rhs set.
Definition UnicodeRanges.hpp:92