Browse Source

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.
reviewable/pr4940/r1
Anthony Bilinski 8 years ago
parent
commit
5df63f9c2e
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 30
      src/persistence/smileypack.cpp
  2. 2
      src/widget/emoticonswidget.cpp

30
src/persistence/smileypack.cpp

@ -258,21 +258,25 @@ QString SmileyPack::smileyfied(const QString& msg) @@ -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;
}

2
src/widget/emoticonswidget.cpp

@ -131,7 +131,7 @@ void EmoticonsWidget::onSmileyClicked() @@ -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);
}
}

Loading…
Cancel
Save