KGuiAddons

kcountryflagemojiiconengine.cpp
1// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
2// SPDX-FileCopyrightText: 2022-2023 Harald Sitter <sitter@kde.org>
3
4#include "kcountryflagemojiiconengine.h"
5
6#include <QDebug>
7#include <QFont>
8#include <QGuiApplication>
9#include <QPainter>
10#include <QPalette>
11
12using namespace Qt::Literals::StringLiterals;
13
14namespace
15{
16
17Q_GLOBAL_STATIC(QFont, s_globalDefaultFont, "emoji"_L1)
18
19QString makeCountryEmoji(const QString &country)
20{
21 // The way this was set up by unicode is actually pretty smart. Country flags are based on their two character
22 // country codes within a given range of code points. And even better, the offset inside the range is the same
23 // as the offset inside ASCII. Meaning the offset of 'A' from 0 is the same as the offset of πŸ‡¦ in the
24 // flag codepoint range. The way a flag is then denoted is e.g. <SURROGATEPAIR>πŸ‡¦<SURROGATEPAIR>πŸ‡Ή resulting in
25 // the Austrian flag.
26 // https://en.wikipedia.org/wiki/Regional_indicator_symbol
27
28 static constexpr auto surrogatePairCodePoint = 0xD83C; // U+D83C
29 static constexpr auto flagCodePointStart = 0xDDE6; // U+1F1E6 (πŸ‡¦) - NB: we are in UTF-16
30 static constexpr auto offsetCodePointA = 'A'_L1.unicode(); // offset from 0, the flag code points have the same offsets
31 static constexpr auto basePoint = flagCodePointStart - offsetCodePointA;
32
33 QString emoji;
34 emoji.reserve(2 * country.size());
35 for (const auto &c : country) {
36 emoji.append(QChar(surrogatePairCodePoint));
37 emoji.append(QChar(basePoint + c.toUpper().unicode()));
38 }
39
40 return emoji;
41}
42
43QString makeRegionEmoji(const QString &region)
44{
45 // Region flags work much the same as country flags but with a slightly different format in a slightly different
46 // code point region. Specifically they use ISO 3166-2 as input (e.g. GB-SCT for Scotland). It all happens in
47 // the Unicode Block β€œTags” (starting at U+E0000) wherein it functions the same as the country codes do in their
48 // block. The offsets inside the block are the same as the ascii offsets and the emoji is constructed by combining
49 // the off set code points of the incoming region tag. They are prefixed with U+1F3F4 🏴 WAVING BLACK FLAG
50 // and suffixed with U+E007F CANCEL TAG.
51 // https://en.wikipedia.org/wiki/Regional_indicator_symbol
52
53 auto hyphenlessRegion = region;
54 hyphenlessRegion.remove('-'_L1);
55
56 static constexpr auto surrogatePairCodePoint = 0xdb40; // U+DB40
57 static constexpr auto flagCodePointStart = 0xDC41; // U+E0041 (Tag Latin Capital Letter A) - NB: we are in UTF-16
58 static constexpr auto offsetCodePointA = 'A'_L1.unicode(); // offset from 0, the flag code points have the same offsets
59 static constexpr auto basePoint = flagCodePointStart - offsetCodePointA;
60
61 auto emoji = u"🏴"_s;
62 emoji.reserve(emoji.size() + 2 * hyphenlessRegion.size() + 2);
63 for (const auto &c : hyphenlessRegion) {
64 emoji.append(QChar(surrogatePairCodePoint));
65 emoji.append(QChar(basePoint + c.toLower().unicode()));
66 }
67 static const auto cancelTag = QString().append(QChar(surrogatePairCodePoint)).append(QChar(0xDC7F));
68 return emoji.append(cancelTag);
69}
70
71} // namespace
72
73class Q_DECL_HIDDEN KCountryFlagEmojiIconEnginePrivate
74{
75public:
76 explicit KCountryFlagEmojiIconEnginePrivate(const QString &regionOrCountry)
77 : m_country(regionOrCountry)
78 , m_emoji(regionOrCountry.contains("-"_L1) ? makeRegionEmoji(regionOrCountry) : makeCountryEmoji(regionOrCountry))
79 {
80 }
81
82 const QString m_country;
83 const QString m_emoji;
84};
85
87 : d(std::make_unique<KCountryFlagEmojiIconEnginePrivate>(country))
88{
89}
90
91KCountryFlagEmojiIconEngine::~KCountryFlagEmojiIconEngine() = default;
92
93QIconEngine *KCountryFlagEmojiIconEngine::clone() const
94{
95 return new KCountryFlagEmojiIconEngine(d->m_country);
96}
97
98QString KCountryFlagEmojiIconEngine::key() const
99{
100 return u"org.kde.KCountryFlagEmojiIconEngine"_s;
101}
102
103void KCountryFlagEmojiIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
104{
105 // Not supported
106 Q_UNUSED(mode);
107 Q_UNUSED(state);
108
109 QFont font(*s_globalDefaultFont, painter->device());
110 font.setPixelSize(qMax(rect.width(), rect.height()));
111 font.setFixedPitch(true);
112
113 QFontMetricsF metrics(font, painter->device());
114 QRectF tightRect = metrics.tightBoundingRect(d->m_emoji);
115 while (tightRect.width() > rect.width() || tightRect.height() > rect.height()) {
116 const auto widthDelta = std::floor(tightRect.width() - rect.width());
117 const auto heightDelta = std::floor(tightRect.height() - rect.height());
118 auto delta = std::max(std::max(1.0, widthDelta), std::max(1.0, heightDelta));
119 if (delta >= font.pixelSize()) {
120 // when the delta is too large we'll chop the pixel size in half and hope the delta comes within a more reasonable range the next loop run
121 static constexpr auto halfSize = 2;
122 delta = std::floor(font.pixelSize() / halfSize);
123 }
124 font.setPixelSize(std::floor(font.pixelSize() - delta));
125 metrics = QFontMetricsF(font, painter->device());
126 tightRect = metrics.tightBoundingRect(d->m_emoji);
127 }
128
129 const QRectF flagBoundingRect = metrics.boundingRect(rect, Qt::AlignCenter, d->m_emoji);
130
131 painter->setPen(qGuiApp->palette().color(QPalette::WindowText)); // in case we render the letters in absence of a flag
132 // Confusingly the pixelSize for drawing must actually be without DPR but the rect calculation above
133 // seems to be correct even with DPR in the pixelSize.
134 font.setPixelSize(std::floor(font.pixelSize() / painter->device()->devicePixelRatioF()));
135 painter->setFont(font);
136 painter->drawText(flagBoundingRect, d->m_emoji);
137}
138
139QPixmap KCountryFlagEmojiIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
140{
141 return scaledPixmap(size, mode, state, 1.0);
142}
143
144QPixmap KCountryFlagEmojiIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
145{
146 QPixmap pixmap(size);
147 pixmap.setDevicePixelRatio(scale);
148 pixmap.fill(Qt::transparent);
149 {
150 QPainter p(&pixmap);
151 paint(&p, QRect(QPoint(0, 0), size), mode, state);
152 }
153 return pixmap;
154}
155
157{
158 return d->m_emoji.isEmpty();
159}
160
162{
163 QFont swapable(font);
164 s_globalDefaultFont->swap(swapable);
165}
bool isNull() override
Check whether the internal emoji unicode sequence is null This does not necessarily mean that the pix...
static void setGlobalDefaultFont(const QFont &font)
Set the Global Default Font object This is primarily useful for platform themes that wish to force a ...
KCountryFlagEmojiIconEngine(const QString &regionOrCountry)
Construct a new KCountryFlagEmojiIconEngine object Please note that regional flag support can be spot...
KI18NLOCALEDATA_EXPORT KCountry country(const char *ianaId)
qreal devicePixelRatioF() const const
QPaintDevice * device() const const
void drawText(const QPoint &position, const QString &text)
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
void fill(const QColor &color)
void setDevicePixelRatio(qreal scaleFactor)
int height() const const
int width() const const
qreal height() const const
qreal width() const const
QString & append(QChar ch)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
void reserve(qsizetype size)
qsizetype size() const const
const QChar * unicode() const const
AlignCenter
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:59:55 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.