mirror-chatterino2/emojis.cpp

124 lines
3.6 KiB
C++
Raw Normal View History

2017-01-18 04:33:30 +01:00
#include "emojis.h"
#include "emotes.h"
2017-01-07 20:43:55 +01:00
#include <QFile>
2017-01-11 18:52:09 +01:00
#include <QStringBuilder>
2017-01-07 20:43:55 +01:00
#include <QTextStream>
2017-01-18 04:33:30 +01:00
QRegularExpression Emojis::findShortCodesRegex(":([-+\\w]+):");
2017-01-07 20:43:55 +01:00
2017-01-18 04:33:30 +01:00
QMap<QString, Emojis::EmojiData> Emojis::shortCodeToEmoji;
QMap<QString, QString> Emojis::emojiToShortCode;
QMap<QChar, QMap<QString, QString>> Emojis::firstEmojiChars;
2017-01-07 20:43:55 +01:00
2017-01-18 04:33:30 +01:00
ConcurrentMap<QString, LazyLoadedImage *> Emojis::imageCache;
2017-01-07 20:43:55 +01:00
2017-01-11 18:52:09 +01:00
QString
Emojis::replaceShortCodes(const QString &text)
2017-01-07 20:43:55 +01:00
{
// TODO: Implement this xD
2017-01-07 20:43:55 +01:00
return text;
}
2017-01-06 23:28:48 +01:00
2017-01-11 18:52:09 +01:00
void
Emojis::parseEmojis(std::vector<std::tuple<LazyLoadedImage *, QString>> &vector,
const QString &text)
2017-01-06 23:28:48 +01:00
{
2017-01-07 20:43:55 +01:00
long lastSlice = 0;
2017-01-11 18:52:09 +01:00
for (auto i = 0; i < text.length() - 1; i++) {
if (!text.at(i).isLowSurrogate()) {
2017-01-18 04:33:30 +01:00
auto iter = firstEmojiChars.find(text.at(i));
2017-01-07 20:43:55 +01:00
2017-01-18 04:33:30 +01:00
if (iter != firstEmojiChars.end()) {
2017-01-11 18:52:09 +01:00
for (auto j = std::min(8, text.length() - i); j > 0; j--) {
2017-01-07 20:43:55 +01:00
QString emojiString = text.mid(i, 2);
auto emojiIter = iter.value().find(emojiString);
2017-01-11 18:52:09 +01:00
if (emojiIter != iter.value().end()) {
QString url = "https://cdnjs.cloudflare.com/ajax/libs/"
"emojione/2.2.6/assets/png/" +
emojiIter.value() + ".png";
2017-01-07 20:43:55 +01:00
2017-01-11 18:52:09 +01:00
if (i - lastSlice != 0) {
vector.push_back(
std::tuple<LazyLoadedImage *, QString>(
NULL, text.mid(lastSlice, i - lastSlice)));
2017-01-07 20:43:55 +01:00
}
2017-01-11 18:52:09 +01:00
vector.push_back(std::tuple<LazyLoadedImage *, QString>(
2017-01-18 04:33:30 +01:00
imageCache.getOrAdd(url,
[&url] {
return new LazyLoadedImage(
url, 0.35);
}),
2017-01-11 18:52:09 +01:00
QString()));
2017-01-07 20:43:55 +01:00
i += j - 1;
2017-01-06 23:28:48 +01:00
2017-01-07 20:43:55 +01:00
lastSlice = i + 1;
2017-01-06 23:28:48 +01:00
2017-01-07 20:43:55 +01:00
break;
}
}
}
}
}
2017-01-11 18:52:09 +01:00
if (lastSlice < text.length()) {
vector.push_back(
std::tuple<LazyLoadedImage *, QString>(NULL, text.mid(lastSlice)));
2017-01-07 20:43:55 +01:00
}
2017-01-06 23:28:48 +01:00
}
2017-01-11 18:52:09 +01:00
void
2017-01-13 18:59:11 +01:00
Emojis::loadEmojis()
2017-01-06 23:28:48 +01:00
{
2017-01-07 20:43:55 +01:00
QFile file(":/emojidata.txt");
file.open(QFile::ReadOnly);
QTextStream in(&file);
uint emotes[4];
2017-01-11 18:52:09 +01:00
while (!in.atEnd()) {
2017-01-07 20:43:55 +01:00
QString line = in.readLine();
2017-01-11 18:52:09 +01:00
if (line.length() < 3 || line.at(0) == '#')
continue;
2017-01-07 20:43:55 +01:00
QStringList a = line.split(' ');
2017-01-11 18:52:09 +01:00
if (a.length() < 2)
continue;
2017-01-07 20:43:55 +01:00
QStringList b = a.at(1).split('-');
2017-01-11 18:52:09 +01:00
if (b.length() < 1)
continue;
2017-01-07 20:43:55 +01:00
int i = 0;
2017-01-11 18:52:09 +01:00
for (const QString &item : b) {
2017-01-07 20:43:55 +01:00
emotes[i++] = QString(item).toUInt(nullptr, 16);
}
2017-01-18 04:33:30 +01:00
shortCodeToEmoji.insert(
2017-01-11 18:52:09 +01:00
a.at(0), Emojis::EmojiData{QString::fromUcs4(emotes, i), a.at(1)});
2017-01-07 20:43:55 +01:00
}
2017-01-18 04:33:30 +01:00
for (auto const &emoji : shortCodeToEmoji.toStdMap()) {
emojiToShortCode.insert(emoji.second.value, emoji.first);
2017-01-07 20:43:55 +01:00
}
2017-01-18 04:33:30 +01:00
for (auto const &emoji : shortCodeToEmoji.toStdMap()) {
auto iter = firstEmojiChars.find(emoji.first.at(0));
2017-01-06 23:28:48 +01:00
2017-01-18 04:33:30 +01:00
if (iter != firstEmojiChars.end()) {
2017-01-07 20:43:55 +01:00
iter.value().insert(emoji.second.value, emoji.second.value);
continue;
}
2017-01-06 23:28:48 +01:00
2017-01-18 04:33:30 +01:00
firstEmojiChars.insert(
2017-01-11 18:52:09 +01:00
emoji.first.at(0),
QMap<QString, QString>{{emoji.second.value, emoji.second.code}});
2017-01-07 20:43:55 +01:00
}
2017-01-06 23:28:48 +01:00
}