-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathemojiregistry.cpp
153 lines (142 loc) · 5.14 KB
/
emojiregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* emojiregistry.cpp - A registry of all standard Emoji
* Copyright (C) 2020 Sergey Ilinykh
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <cstdint>
#include "emojiregistry.h"
#include "emojidb.cpp"
const EmojiRegistry &EmojiRegistry::instance()
{
static EmojiRegistry i;
return i;
}
bool EmojiRegistry::isEmoji(const QString &code) const
{
auto cat = startCategory(&code);
return cat == Category::Emoji || cat == Category::SkinTone;
// TODO check the whole code is emoji. not just start
}
EmojiRegistry::Category EmojiRegistry::startCategory(QStringView in) const
{
if (in.isEmpty())
return Category::None;
std::uint32_t ucs;
if (in[0].isHighSurrogate()) {
if (in.length() == 1)
return Category::None;
ucs = QChar::surrogateToUcs4(in[0], in[1]);
} else {
ucs = in[0].unicode();
if (ucs < 256 && (in.length() == 1 || in[1].unicode() != 0xfe0f)) {
return Category::None; // allow only full-qualified emojis from low range
}
}
if (ucs == 0x200d)
return Category::ZWJ;
if (ucs == 0xfe0f)
return Category::FullQualify;
if (ucs <= 0x39 && in.length() > 2 && (ucs >= 0x30 || ucs == 0x2a || ucs == 0x23) && in[1].unicode() == 0xfe0f
&& in[2].unicode() == 0x20e3)
// number, * or #. excludes 10 keycap
return Category::SimpleKeycap;
bool found = false;
auto lb = ranges_.lower_bound(ucs);
if (lb == ranges_.end() || lb->first != ucs) {
--lb;
found = (ucs > lb->first && ucs <= lb->second); // if this is a final range
} else
found = lb->first == ucs;
if (found) {
if (ucs >= 0x1f3fb && ucs <= 0x1f3ff)
return Category::SkinTone;
return Category::Emoji; // there more cases to review. like emoji tags/flags etc
}
return Category::None;
}
int EmojiRegistry::count() const
{
int count = 0;
for (auto const &g : groups) {
for (auto const &s : g.subGroups) {
count += int(s.emojis.size());
}
}
return count;
}
QStringView EmojiRegistry::findEmoji(const QString &in, int idx) const
{
int emojiStart = -1;
bool gotEmoji = false;
bool gotSkin = false;
bool gotFQ = false;
for (; idx < in.size(); idx++) {
auto category = startCategory(QStringView{in}.mid(idx, in.size() - idx));
if (gotEmoji && category != Category::None) {
if (category == Category::ZWJ) { // zero-width joiner
gotEmoji = false;
gotSkin = false;
gotFQ = false;
} else if (category == Category::FullQualify) {
if (gotFQ)
break; // double qualification is an error
gotSkin = true; // we can't get skin false after fill qualification
gotFQ = true;
} else if (category == Category::SkinTone) {
if (gotSkin)
break; // can't have 2 skin tones in the same time
gotSkin = true;
} else
break; // TODO review other categories when implemented
} else if (!gotEmoji
&& (category == Category::Emoji || category == Category::SkinTone
|| category == Category::SimpleKeycap)) {
if (emojiStart == -1)
emojiStart = idx;
if (category == Category::SimpleKeycap) // keycap is ascii in a box. 3 symbols
idx += 2;
else {
gotEmoji = true;
if (category == Category::SkinTone) { // if we start from skin modifier then just draw colored rect
idx++;
break;
}
}
} else if (emojiStart != -1) { // seems got end of emoji sequence
break;
}
if (in[idx].isHighSurrogate())
idx++;
}
return emojiStart == -1 ? QStringView() : QStringView{in}.mid(emojiStart, idx - emojiStart);
}
EmojiRegistry::EmojiRegistry() : groups(std::move(db)), ranges_(std::move(ranges)) { }
EmojiRegistry::iterator &EmojiRegistry::iterator::operator++()
{
auto const &inst = EmojiRegistry::instance();
if (std::size_t(emoji_idx) < inst.groups[group_idx].subGroups[subgroup_idx].emojis.size() - 1) {
emoji_idx++;
return *this;
}
emoji_idx = 0;
if (std::size_t(subgroup_idx) < inst.groups[group_idx].subGroups.size() - 1) {
subgroup_idx++;
return *this;
}
subgroup_idx = 0;
group_idx++;
return *this;
}