mirror of https://github.com/procxx/kepka.git
improved emoji text replaces
This commit is contained in:
parent
47f673aa69
commit
d92356ce28
|
@ -84,7 +84,21 @@ void EmojiBox::fillBlocks() {
|
||||||
BlockRow currentRow;
|
BlockRow currentRow;
|
||||||
currentRow.reserve(replacesInRow);
|
currentRow.reserve(replacesInRow);
|
||||||
for (uint32 i = 0; i < replacesCount; ++i) {
|
for (uint32 i = 0; i < replacesCount; ++i) {
|
||||||
Block block(emojiGet(replaces[i].code), QString::fromUtf8(replaces[i].replace));
|
EmojiPtr emoji = emojiGet(replaces[i].code);
|
||||||
|
if (!emoji || emoji == TwoSymbolEmoji) continue;
|
||||||
|
if (emoji->color) {
|
||||||
|
EmojiColorVariants::const_iterator it = cEmojiVariants().constFind(emoji->code);
|
||||||
|
if (it != cEmojiVariants().cend()) {
|
||||||
|
EmojiPtr replace = emojiFromKey(it.value());
|
||||||
|
if (replace) {
|
||||||
|
if (replace != TwoSymbolEmoji && replace->code == emoji->code && replace->code2 == emoji->code2) {
|
||||||
|
emoji = replace;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Block block(emoji, QString::fromUtf8(replaces[i].replace));
|
||||||
currentRow.push_back(block);
|
currentRow.push_back(block);
|
||||||
if (uint32(currentRow.size()) == replacesInRow) {
|
if (uint32(currentRow.size()) == replacesInRow) {
|
||||||
_blocks.push_back(currentRow);
|
_blocks.push_back(currentRow);
|
||||||
|
|
|
@ -26,6 +26,31 @@ EmojiPtr emojiGet(EmojiPtr emoji, uint32 color);
|
||||||
EmojiPtr emojiGet(const QChar *from, const QChar *end);
|
EmojiPtr emojiGet(const QChar *from, const QChar *end);
|
||||||
QString emojiGetSequence(int index);
|
QString emojiGetSequence(int index);
|
||||||
|
|
||||||
|
inline QString emojiString(EmojiPtr emoji) {
|
||||||
|
if ((emoji->code & 0xFFFF0000U) == 0xFFFF0000U) { // sequence
|
||||||
|
return emojiGetSequence(emoji->code & 0xFFFFU);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString result;
|
||||||
|
result.reserve(emoji->len + (emoji->postfix ? 1 : 0));
|
||||||
|
if (!(emoji->code >> 16)) {
|
||||||
|
result.append(QChar(emoji->code & 0xFFFF));
|
||||||
|
} else {
|
||||||
|
result.append(QChar((emoji->code >> 16) & 0xFFFF));
|
||||||
|
result.append(QChar(emoji->code & 0xFFFF));
|
||||||
|
if (emoji->code2) {
|
||||||
|
result.append(QChar((emoji->code2 >> 16) & 0xFFFF));
|
||||||
|
result.append(QChar(emoji->code2 & 0xFFFF));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (emoji->color && ((emoji->color & 0xFFFF0000U) != 0xFFFF0000U)) {
|
||||||
|
result.append(QChar((emoji->color >> 16) & 0xFFFF));
|
||||||
|
result.append(QChar(emoji->color & 0xFFFF));
|
||||||
|
}
|
||||||
|
if (emoji->postfix) result.append(QChar(emoji->postfix));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
inline uint64 emojiKey(EmojiPtr emoji) {
|
inline uint64 emojiKey(EmojiPtr emoji) {
|
||||||
uint64 key = emoji->code;
|
uint64 key = emoji->code;
|
||||||
if (emoji->code2) {
|
if (emoji->code2) {
|
||||||
|
@ -138,7 +163,8 @@ inline QString replaceEmojis(const QString &text) {
|
||||||
while (currentLink < lnkCount && ch >= lnkRanges[currentLink].from + lnkRanges[currentLink].len) {
|
while (currentLink < lnkCount && ch >= lnkRanges[currentLink].from + lnkRanges[currentLink].len) {
|
||||||
++currentLink;
|
++currentLink;
|
||||||
}
|
}
|
||||||
if (emojiCode &&
|
EmojiPtr emoji = emojiCode ? emojiGet(emojiCode) : 0;
|
||||||
|
if (emoji && emoji != TwoSymbolEmoji &&
|
||||||
(ch == emojiStart || !ch->isLetterOrNumber() || !(ch - 1)->isLetterOrNumber()) &&
|
(ch == emojiStart || !ch->isLetterOrNumber() || !(ch - 1)->isLetterOrNumber()) &&
|
||||||
(newEmojiEnd == e || !newEmojiEnd->isLetterOrNumber() || newEmojiEnd == emojiStart || !(newEmojiEnd - 1)->isLetterOrNumber()) &&
|
(newEmojiEnd == e || !newEmojiEnd->isLetterOrNumber() || newEmojiEnd == emojiStart || !(newEmojiEnd - 1)->isLetterOrNumber()) &&
|
||||||
(currentLink >= lnkCount || (ch < lnkRanges[currentLink].from && newEmojiEnd <= lnkRanges[currentLink].from) || (ch >= lnkRanges[currentLink].from + lnkRanges[currentLink].len && newEmojiEnd > lnkRanges[currentLink].from + lnkRanges[currentLink].len))
|
(currentLink >= lnkCount || (ch < lnkRanges[currentLink].from && newEmojiEnd <= lnkRanges[currentLink].from) || (ch >= lnkRanges[currentLink].from + lnkRanges[currentLink].len && newEmojiEnd > lnkRanges[currentLink].from + lnkRanges[currentLink].len))
|
||||||
|
@ -148,10 +174,18 @@ inline QString replaceEmojis(const QString &text) {
|
||||||
if (ch > emojiEnd + (consumePrevious ? 1 : 0)) {
|
if (ch > emojiEnd + (consumePrevious ? 1 : 0)) {
|
||||||
result.append(emojiEnd, ch - emojiEnd - (consumePrevious ? 1 : 0));
|
result.append(emojiEnd, ch - emojiEnd - (consumePrevious ? 1 : 0));
|
||||||
}
|
}
|
||||||
if (emojiCode > 65535) {
|
if (emoji->color) {
|
||||||
result.append(QChar((emojiCode >> 16) & 0xFFFF));
|
EmojiColorVariants::const_iterator it = cEmojiVariants().constFind(emoji->code);
|
||||||
|
if (it != cEmojiVariants().cend()) {
|
||||||
|
EmojiPtr replace = emojiFromKey(it.value());
|
||||||
|
if (replace) {
|
||||||
|
if (replace != TwoSymbolEmoji && replace->code == emoji->code && replace->code2 == emoji->code2) {
|
||||||
|
emoji = replace;
|
||||||
}
|
}
|
||||||
result.append(QChar(emojiCode & 0xFFFF));
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.append(emojiString(emoji));
|
||||||
|
|
||||||
ch = emojiEnd = newEmojiEnd;
|
ch = emojiEnd = newEmojiEnd;
|
||||||
canFindEmoji = true;
|
canFindEmoji = true;
|
||||||
|
|
|
@ -373,7 +373,7 @@ QString FlatTextarea::getText(int32 start, int32 end) const {
|
||||||
QString imageName = static_cast<QTextImageFormat*>(&f)->name();
|
QString imageName = static_cast<QTextImageFormat*>(&f)->name();
|
||||||
if (imageName.startsWith(QLatin1String("emoji://e."))) {
|
if (imageName.startsWith(QLatin1String("emoji://e."))) {
|
||||||
if (EmojiPtr emoji = emojiFromUrl(imageName)) {
|
if (EmojiPtr emoji = emojiFromUrl(imageName)) {
|
||||||
emojiText = textEmojiString(emoji);
|
emojiText = emojiString(emoji);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,31 +239,6 @@ const QChar *textSkipCommand(const QChar *from, const QChar *end, bool canLink)
|
||||||
return (result < end && *result == TextCommand) ? (result + 1) : from;
|
return (result < end && *result == TextCommand) ? (result + 1) : from;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString textEmojiString(EmojiPtr emoji) {
|
|
||||||
if ((emoji->code & 0xFFFF0000U) == 0xFFFF0000U) { // sequence
|
|
||||||
return emojiGetSequence(emoji->code & 0xFFFFU);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString result;
|
|
||||||
result.reserve(emoji->len + (emoji->postfix ? 1 : 0));
|
|
||||||
if (!(emoji->code >> 16)) {
|
|
||||||
result.append(QChar(emoji->code & 0xFFFF));
|
|
||||||
} else {
|
|
||||||
result.append(QChar((emoji->code >> 16) & 0xFFFF));
|
|
||||||
result.append(QChar(emoji->code & 0xFFFF));
|
|
||||||
if (emoji->code2) {
|
|
||||||
result.append(QChar((emoji->code2 >> 16) & 0xFFFF));
|
|
||||||
result.append(QChar(emoji->code2 & 0xFFFF));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (emoji->color && ((emoji->color & 0xFFFF0000U) != 0xFFFF0000U)) {
|
|
||||||
result.append(QChar((emoji->color >> 16) & 0xFFFF));
|
|
||||||
result.append(QChar(emoji->color & 0xFFFF));
|
|
||||||
}
|
|
||||||
if (emoji->postfix) result.append(QChar(emoji->postfix));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
class TextParser {
|
class TextParser {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -527,6 +502,10 @@ public:
|
||||||
for (int l = len - skipped - 1; l > 0; --l) {
|
for (int l = len - skipped - 1; l > 0; --l) {
|
||||||
_t->_text.push_back(*++ptr);
|
_t->_text.push_back(*++ptr);
|
||||||
}
|
}
|
||||||
|
if (e->postfix && _t->_text.at(_t->_text.size() - 1).unicode() != e->postfix) {
|
||||||
|
_t->_text.push_back(e->postfix);
|
||||||
|
++len;
|
||||||
|
}
|
||||||
|
|
||||||
createBlock(-len);
|
createBlock(-len);
|
||||||
emoji = e;
|
emoji = e;
|
||||||
|
|
|
@ -534,8 +534,6 @@ QString textcmdStartColor(const style::color &color);
|
||||||
QString textcmdStopColor();
|
QString textcmdStopColor();
|
||||||
const QChar *textSkipCommand(const QChar *from, const QChar *end, bool canLink = true);
|
const QChar *textSkipCommand(const QChar *from, const QChar *end, bool canLink = true);
|
||||||
|
|
||||||
QString textEmojiString(EmojiPtr emoji);
|
|
||||||
|
|
||||||
inline bool chIsSpace(QChar ch, bool rich = false) {
|
inline bool chIsSpace(QChar ch, bool rich = false) {
|
||||||
return ch.isSpace() || (ch < 32 && !(rich && ch == TextCommand)) || (ch == QChar::ParagraphSeparator) || (ch == QChar::LineSeparator) || (ch == QChar::ObjectReplacementCharacter) || (ch == QChar::SoftHyphen) || (ch == QChar::CarriageReturn) || (ch == QChar::Tabulation);
|
return ch.isSpace() || (ch < 32 && !(rich && ch == TextCommand)) || (ch == QChar::ParagraphSeparator) || (ch == QChar::LineSeparator) || (ch == QChar::ObjectReplacementCharacter) || (ch == QChar::SoftHyphen) || (ch == QChar::CarriageReturn) || (ch == QChar::Tabulation);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2900,7 +2900,7 @@ bool HistorySticker::updateStickerEmoji() {
|
||||||
}
|
}
|
||||||
const EmojiStickersMap &stickers(cEmojiStickers());
|
const EmojiStickersMap &stickers(cEmojiStickers());
|
||||||
EmojiStickersMap::const_iterator i = stickers.constFind(data);
|
EmojiStickersMap::const_iterator i = stickers.constFind(data);
|
||||||
QString emoji = (i == stickers.cend()) ? QString() : textEmojiString(i.value());
|
QString emoji = (i == stickers.cend()) ? QString() : emojiString(i.value());
|
||||||
if (emoji != _emoji) {
|
if (emoji != _emoji) {
|
||||||
_emoji = emoji;
|
_emoji = emoji;
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue