1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(chatlog): parse multi-length emoji properly

Before, when multi-length emoji were next to any other character, they would not be transformed into images. With this change, emoji are replaced with images no matter where they are in the string. Parsing is now done by checking to see if two-character blocks are valid as a single UTF-32 character, or if they are truly two distinct characters. Because we no longer need white space before emoji, I also removed our addition of spaces on either side of an emoji when a user sends them.
This commit is contained in:
Anthony Bilinski 2018-01-31 19:48:25 -08:00
parent 0dea03906e
commit 5df63f9c2e
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
2 changed files with 18 additions and 14 deletions

View File

@ -258,21 +258,25 @@ QString SmileyPack::smileyfied(const QString& msg)
{
QMutexLocker locker(&loadingMutex);
QString result(msg);
QRegularExpression exp("\\S+");
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
int replaceDiff = 0;
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString key = match.captured();
int startPos = match.capturedStart();
int keyLength = key.length();
if (emoticonToPath.find(key) != emoticonToPath.end()) {
QString imgRichText = getAsRichText(key);
result.replace(startPos + replaceDiff, keyLength, imgRichText);
replaceDiff += imgRichText.length() - keyLength;
int replaceDiff = 0; // how much we have increased message size by replacing emoji with image tags
int curCharLength;
QStringRef curChar; // the current UTF-32 which we are examining, which may look like a 2-length QString
for (int strIndex = 0; strIndex < msg.length(); strIndex += curCharLength) {
if ((msg.length() - strIndex >= 2) && ((curChar = msg.midRef(strIndex, 2)).toUcs4().length() == 1)) {
// if converting two characters to UTF-32 gives us a single valid character, this is actually a single
// 4-byte character, with a QString length of 2
curCharLength = 2;
}
else {
curChar = msg.midRef(strIndex, 1);
curCharLength = 1;
}
if (emoticonToPath.find(curChar.toString()) != emoticonToPath.end()) {
QString imgRichText = getAsRichText(curChar.toString());
result.replace(strIndex + replaceDiff, curCharLength, imgRichText);
replaceDiff += imgRichText.length() - curCharLength;
}
}
return result;
}

View File

@ -131,7 +131,7 @@ void EmoticonsWidget::onSmileyClicked()
if (sender) {
QString sequence =
sender->property("sequence").toString().replace("&lt;", "<").replace("&gt;", ">");
emit insertEmoticon(' ' + sequence + ' ');
emit insertEmoticon(sequence);
}
}