KTextAddons

unicodeemoticon.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "unicodeemoticon.h"
8#include "emoticonunicodeutils.h"
9
10using namespace TextEmoticonsCore;
11UnicodeEmoticon::UnicodeEmoticon() = default;
12
13bool UnicodeEmoticon::isValid() const
14{
15 return !mIdentifier.isEmpty() && !mUnicode.isEmpty();
16}
17
18QString UnicodeEmoticon::identifier() const
19{
20 return mIdentifier;
21}
22
23void UnicodeEmoticon::setIdentifier(const QString &name)
24{
25 mIdentifier = name;
26}
27
28QString UnicodeEmoticon::unicode() const
29{
30 return mUnicode;
31}
32
33QString UnicodeEmoticon::unicodeDisplay() const
34{
35 if (!mUnicode.isEmpty()) {
36 if (mCachedHtml.isEmpty()) {
37 mCachedHtml = QStringLiteral("<span style=\"font: x-large %3\" title=\"%2\">%1</span>")
38 .arg(mUnicode, mIdentifier, TextEmoticonsCore::EmoticonUnicodeUtils::emojiFontName());
39 }
40 }
41 return mCachedHtml;
42}
43
44// input: codepoints in hex like 1f9d7-1f3fb-2640
45// output: QString with 3 ucs4 code points for the above, which is in fact 5 QChars.
46QString UnicodeEmoticon::escapeUnicodeEmoji(const QString &pString)
47{
48 QString retString;
49
50 const QList<QStringView> parts = QStringView(pString).split(QLatin1Char('-'));
51 for (const QStringView &item : parts) {
52 bool ok;
53 const int part = item.toInt(&ok, 16);
54 Q_ASSERT(ok);
55 if (QChar::requiresSurrogates(part)) {
56 retString += QChar::highSurrogate(part);
57 retString += QChar::lowSurrogate(part);
58 } else {
59 retString += QChar(part);
60 }
61 }
62
63 return retString;
64}
65
66QString UnicodeEmoticon::key() const
67{
68 return mKey;
69}
70
71void UnicodeEmoticon::setKey(const QString &key)
72{
73 mKey = key;
74}
75
76bool UnicodeEmoticon::operator==(const UnicodeEmoticon &other) const
77{
78 return (mAliases == other.aliases()) && (mIdentifier == other.identifier()) && (mUnicode == other.unicode()) && (mCategory == other.category())
79 && (mKey == other.key()) && (mOrder == other.order());
80}
81
82int UnicodeEmoticon::order() const
83{
84 return mOrder;
85}
86
87void UnicodeEmoticon::setOrder(int order)
88{
89 mOrder = order;
90}
91
92void UnicodeEmoticon::setUnicode(const QString &unicode)
93{
94 mUnicode = escapeUnicodeEmoji(unicode);
95}
96
97QString UnicodeEmoticon::category() const
98{
99 return mCategory;
100}
101
102void UnicodeEmoticon::setCategory(const QString &category)
103{
104 mCategory = category;
105}
106
107QStringList UnicodeEmoticon::aliases() const
108{
109 return mAliases;
110}
111
112void UnicodeEmoticon::setAliases(const QStringList &aliases)
113{
114 mAliases = aliases;
115}
116
117bool UnicodeEmoticon::hasEmoji(const QString &identifier) const
118{
119 return (mIdentifier == identifier) || (mUnicode == identifier) || mAliases.contains(identifier);
120}
121
123{
124 d << "Identifier : " << t.identifier();
125 d << "Unicode: " << t.unicode();
126 d << "Category: " << t.category();
127 d << "Aliases: " << t.aliases();
128 d << "Order: " << t.order();
129 d << "Key:" << t.key();
130 return d;
131}
132
133#include "moc_unicodeemoticon.cpp"
The UnicodeEmoticon class.
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
QString name(StandardAction id)
Category category(StandardShortcut id)
char16_t highSurrogate(char32_t ucs4)
char16_t lowSurrogate(char32_t ucs4)
bool requiresSurrogates(char32_t ucs4)
QString arg(Args &&... args) const const
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.