2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-14 11:00:23 +00:00
|
|
|
#include "ui/text/text.h"
|
2014-10-07 17:57:57 +00:00
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
void emojiInit();
|
|
|
|
EmojiPtr emojiGet(uint32 code);
|
|
|
|
EmojiPtr emojiGet(uint32 code, uint32 code2);
|
|
|
|
EmojiPtr emojiGet(EmojiPtr emoji, uint32 color);
|
|
|
|
EmojiPtr emojiGet(const QChar *from, const QChar *end);
|
|
|
|
QString emojiGetSequence(int index);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-05-12 15:01:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
inline uint64 emojiKey(EmojiPtr emoji) {
|
|
|
|
uint64 key = emoji->code;
|
|
|
|
if (emoji->code2) {
|
|
|
|
key = (key << 32) | uint64(emoji->code2);
|
|
|
|
} else if (emoji->color && ((emoji->color & 0xFFFF0000U) != 0xFFFF0000U)) {
|
|
|
|
key = (key << 32) | uint64(emoji->color);
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline EmojiPtr emojiFromKey(uint64 key) {
|
|
|
|
uint32 code = uint32(key >> 32), code2 = uint32(key & 0xFFFFFFFFLLU);
|
|
|
|
if (!code && code2) {
|
|
|
|
code = code2;
|
|
|
|
code2 = 0;
|
|
|
|
}
|
|
|
|
EmojiPtr emoji = emojiGet(code);
|
|
|
|
if (emoji == TwoSymbolEmoji) {
|
|
|
|
return emojiGet(code, code2);
|
|
|
|
} else if (emoji && emoji->color && code2) {
|
|
|
|
return emojiGet(emoji, code2);
|
|
|
|
}
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline EmojiPtr emojiFromUrl(const QString &url) {
|
|
|
|
return emojiFromKey(url.midRef(10).toULongLong(0, 16)); // skip emoji://e.
|
|
|
|
}
|
|
|
|
|
2016-05-04 16:46:24 +00:00
|
|
|
inline EmojiPtr emojiFromText(const QChar *ch, const QChar *end, int *outLength = nullptr) {
|
|
|
|
EmojiPtr emoji = nullptr;
|
|
|
|
if (ch + 1 < end && ((ch->isHighSurrogate() && (ch + 1)->isLowSurrogate()) || (((ch->unicode() >= 0x30 && ch->unicode() < 0x3A) || ch->unicode() == 0x23 || ch->unicode() == 0x2A) && (ch + 1)->unicode() == 0x20E3))) {
|
2015-05-08 12:45:14 +00:00
|
|
|
uint32 code = (ch->unicode() << 16) | (ch + 1)->unicode();
|
|
|
|
emoji = emojiGet(code);
|
|
|
|
if (emoji) {
|
|
|
|
if (emoji == TwoSymbolEmoji) { // check two symbol
|
2016-05-04 16:46:24 +00:00
|
|
|
if (ch + 3 >= end) {
|
2015-05-08 12:45:14 +00:00
|
|
|
emoji = 0;
|
|
|
|
} else {
|
|
|
|
uint32 code2 = ((uint32((ch + 2)->unicode()) << 16) | uint32((ch + 3)->unicode()));
|
|
|
|
emoji = emojiGet(code, code2);
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-04 16:46:24 +00:00
|
|
|
if (ch + 2 < end && (ch + 2)->unicode() == 0x200D) { // check sequence
|
|
|
|
EmojiPtr seq = emojiGet(ch, end);
|
2015-05-08 12:45:14 +00:00
|
|
|
if (seq) {
|
|
|
|
emoji = seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
} else if (ch + 2 < end && ((ch->unicode() >= 0x30 && ch->unicode() < 0x3A) || ch->unicode() == 0x23 || ch->unicode() == 0x2A) && (ch + 1)->unicode() == 0xFE0F && (ch + 2)->unicode() == 0x20E3) {
|
2015-11-27 17:10:16 +00:00
|
|
|
uint32 code = (ch->unicode() << 16) | (ch + 2)->unicode();
|
|
|
|
emoji = emojiGet(code);
|
2016-05-04 16:46:24 +00:00
|
|
|
if (outLength) *outLength = emoji->len + 1;
|
2015-11-27 17:10:16 +00:00
|
|
|
return emoji;
|
2016-05-04 16:46:24 +00:00
|
|
|
} else if (ch < end) {
|
2015-05-08 12:45:14 +00:00
|
|
|
emoji = emojiGet(ch->unicode());
|
2016-01-09 11:24:16 +00:00
|
|
|
t_assert(emoji != TwoSymbolEmoji);
|
2015-05-08 12:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (emoji) {
|
2016-05-04 16:46:24 +00:00
|
|
|
int32 len = emoji->len + ((ch + emoji->len < end && (ch + emoji->len)->unicode() == 0xFE0F) ? 1 : 0);
|
|
|
|
if (emoji->color && (ch + len + 1 < end && (ch + len)->isHighSurrogate() && (ch + len + 1)->isLowSurrogate())) { // color
|
2015-05-08 12:45:14 +00:00
|
|
|
uint32 color = ((uint32((ch + len)->unicode()) << 16) | uint32((ch + len + 1)->unicode()));
|
|
|
|
EmojiPtr col = emojiGet(emoji, color);
|
|
|
|
if (col && col != emoji) {
|
|
|
|
len += col->len - emoji->len;
|
|
|
|
emoji = col;
|
2016-05-04 16:46:24 +00:00
|
|
|
if (ch + len < end && (ch + len)->unicode() == 0xFE0F) {
|
2015-05-08 12:45:14 +00:00
|
|
|
++len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 16:46:24 +00:00
|
|
|
if (outLength) *outLength = len;
|
2016-01-09 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline EmojiPtr emojiFromText(const QString &text, int32 *plen = 0) {
|
|
|
|
return text.isEmpty() ? EmojiPtr(0) : emojiFromText(text.constBegin(), text.constEnd(), plen);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline EmojiPtr emojiGetNoColor(EmojiPtr emoji) {
|
|
|
|
if (emoji && emoji->color && (emoji->color & 0xFFFF0000U) != 0xFFFF0000U) {
|
|
|
|
EmojiPtr result = emojiGet(emoji->code);
|
|
|
|
return (result == TwoSymbolEmoji) ? emojiGet(emoji->code, emoji->code2) : result;
|
2015-05-08 12:45:14 +00:00
|
|
|
}
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern int EmojiSizes[5], EIndex, ESize;
|
|
|
|
extern const char *EmojiNames[5], *EName;
|
2015-04-30 13:56:33 +00:00
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
void emojiFind(const QChar *ch, const QChar *e, const QChar *&newEmojiEnd, uint32 &emojiCode);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
inline bool emojiEdge(const QChar *ch) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (ch->unicode()) {
|
|
|
|
case '.': case ',': case ':': case ';': case '!': case '?': case '#': case '@':
|
|
|
|
case '(': case ')': case '[': case ']': case '{': case '}': case '<': case '>':
|
|
|
|
case '+': case '=': case '-': case '_': case '*': case '/': case '\\': case '^': case '$':
|
|
|
|
case '"': case '\'':
|
|
|
|
case 8212: case 171: case 187: // --, <<, >>
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
inline void appendPartToResult(QString &result, const QChar *start, const QChar *from, const QChar *to, EntitiesInText *inOutEntities) {
|
2015-10-23 16:06:56 +00:00
|
|
|
if (to > from) {
|
2016-04-30 17:04:14 +00:00
|
|
|
for (auto &entity : *inOutEntities) {
|
2016-04-29 12:00:48 +00:00
|
|
|
if (entity.offset() >= to - start) break;
|
|
|
|
if (entity.offset() + entity.length() < from - start) continue;
|
|
|
|
if (entity.offset() >= from - start) {
|
|
|
|
entity.extendToLeft(from - start - result.size());
|
2015-10-23 16:06:56 +00:00
|
|
|
}
|
2016-09-20 14:02:50 +00:00
|
|
|
if (entity.offset() + entity.length() <= to - start) {
|
2016-04-29 12:00:48 +00:00
|
|
|
entity.shrinkFromRight(from - start - result.size());
|
2015-10-23 16:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.append(from, to - from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
inline QString replaceEmojis(const QString &text, EntitiesInText *inOutEntities) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QString result;
|
2016-04-30 17:04:14 +00:00
|
|
|
auto currentEntity = inOutEntities->begin(), entitiesEnd = inOutEntities->end();
|
2015-10-23 16:06:56 +00:00
|
|
|
const QChar *emojiStart = text.constData(), *emojiEnd = emojiStart, *e = text.constData() + text.size();
|
|
|
|
bool canFindEmoji = true;
|
2014-05-30 08:53:19 +00:00
|
|
|
for (const QChar *ch = emojiEnd; ch != e;) {
|
|
|
|
uint32 emojiCode = 0;
|
|
|
|
const QChar *newEmojiEnd = 0;
|
|
|
|
if (canFindEmoji) {
|
2015-05-08 12:45:14 +00:00
|
|
|
emojiFind(ch, e, newEmojiEnd, emojiCode);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-09 11:24:16 +00:00
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
while (currentEntity != entitiesEnd && ch >= emojiStart + currentEntity->offset() + currentEntity->length()) {
|
2015-10-23 16:06:56 +00:00
|
|
|
++currentEntity;
|
2014-10-07 17:57:57 +00:00
|
|
|
}
|
2015-05-12 15:01:49 +00:00
|
|
|
EmojiPtr emoji = emojiCode ? emojiGet(emojiCode) : 0;
|
|
|
|
if (emoji && emoji != TwoSymbolEmoji &&
|
2014-10-07 17:57:57 +00:00
|
|
|
(ch == emojiStart || !ch->isLetterOrNumber() || !(ch - 1)->isLetterOrNumber()) &&
|
|
|
|
(newEmojiEnd == e || !newEmojiEnd->isLetterOrNumber() || newEmojiEnd == emojiStart || !(newEmojiEnd - 1)->isLetterOrNumber()) &&
|
2016-04-29 12:00:48 +00:00
|
|
|
(currentEntity == entitiesEnd || (ch < emojiStart + currentEntity->offset() && newEmojiEnd <= emojiStart + currentEntity->offset()) || (ch >= emojiStart + currentEntity->offset() + currentEntity->length() && newEmojiEnd > emojiStart + currentEntity->offset() + currentEntity->length()))
|
2014-10-07 17:57:57 +00:00
|
|
|
) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (result.isEmpty()) result.reserve(text.size());
|
2015-10-23 16:06:56 +00:00
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
appendPartToResult(result, emojiStart, emojiEnd, ch, inOutEntities);
|
2015-10-23 16:06:56 +00:00
|
|
|
|
2015-05-12 15:01:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-05-12 15:01:49 +00:00
|
|
|
result.append(emojiString(emoji));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
ch = emojiEnd = newEmojiEnd;
|
|
|
|
canFindEmoji = true;
|
|
|
|
} else {
|
2015-10-23 16:06:56 +00:00
|
|
|
if (emojiEdge(ch)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
canFindEmoji = true;
|
|
|
|
} else {
|
|
|
|
canFindEmoji = false;
|
|
|
|
}
|
|
|
|
++ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.isEmpty()) return text;
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
appendPartToResult(result, emojiStart, emojiEnd, e, inOutEntities);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-23 16:06:56 +00:00
|
|
|
return result;
|
2015-10-14 11:51:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
int emojiPackCount(DBIEmojiTab tab);
|
2014-05-30 08:53:19 +00:00
|
|
|
EmojiPack emojiPack(DBIEmojiTab tab);
|