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:
parent
0dea03906e
commit
5df63f9c2e
|
@ -258,21 +258,25 @@ QString SmileyPack::smileyfied(const QString& msg)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&loadingMutex);
|
QMutexLocker locker(&loadingMutex);
|
||||||
QString result(msg);
|
QString result(msg);
|
||||||
QRegularExpression exp("\\S+");
|
int replaceDiff = 0; // how much we have increased message size by replacing emoji with image tags
|
||||||
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
|
int curCharLength;
|
||||||
int replaceDiff = 0;
|
QStringRef curChar; // the current UTF-32 which we are examining, which may look like a 2-length QString
|
||||||
while (iter.hasNext()) {
|
for (int strIndex = 0; strIndex < msg.length(); strIndex += curCharLength) {
|
||||||
QRegularExpressionMatch match = iter.next();
|
if ((msg.length() - strIndex >= 2) && ((curChar = msg.midRef(strIndex, 2)).toUcs4().length() == 1)) {
|
||||||
QString key = match.captured();
|
// if converting two characters to UTF-32 gives us a single valid character, this is actually a single
|
||||||
int startPos = match.capturedStart();
|
// 4-byte character, with a QString length of 2
|
||||||
int keyLength = key.length();
|
curCharLength = 2;
|
||||||
if (emoticonToPath.find(key) != emoticonToPath.end()) {
|
}
|
||||||
QString imgRichText = getAsRichText(key);
|
else {
|
||||||
result.replace(startPos + replaceDiff, keyLength, imgRichText);
|
curChar = msg.midRef(strIndex, 1);
|
||||||
replaceDiff += imgRichText.length() - keyLength;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -131,7 +131,7 @@ void EmoticonsWidget::onSmileyClicked()
|
||||||
if (sender) {
|
if (sender) {
|
||||||
QString sequence =
|
QString sequence =
|
||||||
sender->property("sequence").toString().replace("<", "<").replace(">", ">");
|
sender->property("sequence").toString().replace("<", "<").replace(">", ">");
|
||||||
emit insertEmoticon(' ' + sequence + ' ');
|
emit insertEmoticon(sequence);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user