File indexing completed on 2024-12-22 04:28:17

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "unicodeemoticon.h"
0008 #include "emoticonunicodeutils.h"
0009 
0010 using namespace TextEmoticonsCore;
0011 UnicodeEmoticon::UnicodeEmoticon() = default;
0012 
0013 bool UnicodeEmoticon::isValid() const
0014 {
0015     return !mIdentifier.isEmpty() && !mUnicode.isEmpty();
0016 }
0017 
0018 QString UnicodeEmoticon::identifier() const
0019 {
0020     return mIdentifier;
0021 }
0022 
0023 void UnicodeEmoticon::setIdentifier(const QString &name)
0024 {
0025     mIdentifier = name;
0026 }
0027 
0028 QString UnicodeEmoticon::unicode() const
0029 {
0030     return mUnicode;
0031 }
0032 
0033 QString UnicodeEmoticon::unicodeDisplay() const
0034 {
0035     if (!mUnicode.isEmpty()) {
0036         if (mCachedHtml.isEmpty()) {
0037             mCachedHtml = QStringLiteral("<span style=\"font: x-large %3\" title=\"%2\">%1</span>")
0038                               .arg(mUnicode, mIdentifier, TextEmoticonsCore::EmoticonUnicodeUtils::emojiFontName());
0039         }
0040     }
0041     return mCachedHtml;
0042 }
0043 
0044 // input: codepoints in hex like 1f9d7-1f3fb-2640
0045 // output: QString with 3 ucs4 code points for the above, which is in fact 5 QChars.
0046 QString UnicodeEmoticon::escapeUnicodeEmoji(const QString &pString)
0047 {
0048     QString retString;
0049 
0050     const QList<QStringView> parts = QStringView(pString).split(QLatin1Char('-'));
0051     for (const QStringView &item : parts) {
0052         bool ok;
0053         const int part = item.toInt(&ok, 16);
0054         Q_ASSERT(ok);
0055         if (QChar::requiresSurrogates(part)) {
0056             retString += QChar::highSurrogate(part);
0057             retString += QChar::lowSurrogate(part);
0058         } else {
0059             retString += QChar(part);
0060         }
0061     }
0062 
0063     return retString;
0064 }
0065 
0066 QString UnicodeEmoticon::key() const
0067 {
0068     return mKey;
0069 }
0070 
0071 void UnicodeEmoticon::setKey(const QString &key)
0072 {
0073     mKey = key;
0074 }
0075 
0076 bool UnicodeEmoticon::operator==(const UnicodeEmoticon &other) const
0077 {
0078     return (mAliases == other.aliases()) && (mIdentifier == other.identifier()) && (mUnicode == other.unicode()) && (mCategory == other.category())
0079         && (mKey == other.key()) && (mOrder == other.order());
0080 }
0081 
0082 int UnicodeEmoticon::order() const
0083 {
0084     return mOrder;
0085 }
0086 
0087 void UnicodeEmoticon::setOrder(int order)
0088 {
0089     mOrder = order;
0090 }
0091 
0092 void UnicodeEmoticon::setUnicode(const QString &unicode)
0093 {
0094     mUnicode = escapeUnicodeEmoji(unicode);
0095 }
0096 
0097 QString UnicodeEmoticon::category() const
0098 {
0099     return mCategory;
0100 }
0101 
0102 void UnicodeEmoticon::setCategory(const QString &category)
0103 {
0104     mCategory = category;
0105 }
0106 
0107 QStringList UnicodeEmoticon::aliases() const
0108 {
0109     return mAliases;
0110 }
0111 
0112 void UnicodeEmoticon::setAliases(const QStringList &aliases)
0113 {
0114     mAliases = aliases;
0115 }
0116 
0117 bool UnicodeEmoticon::hasEmoji(const QString &identifier) const
0118 {
0119     return (mIdentifier == identifier) || (mUnicode == identifier) || mAliases.contains(identifier);
0120 }
0121 
0122 QDebug operator<<(QDebug d, const UnicodeEmoticon &t)
0123 {
0124     d << "Identifier : " << t.identifier();
0125     d << "Unicode: " << t.unicode();
0126     d << "Category: " << t.category();
0127     d << "Aliases: " << t.aliases();
0128     d << "Order: " << t.order();
0129     d << "Key:" << t.key();
0130     return d;
0131 }
0132 
0133 #include "moc_unicodeemoticon.cpp"