Show only fonts that can render the grin emoji in the emoji font list

Users often are confused what fonts to pick. However this approach is
way too slow. We need to somehow prefilter the list, but for that we
need to know what writing system emojis belong in or some other quick
filter, that we can apply before the QRawFont and possibly the QFont.
This commit is contained in:
Nicolas Werner 2023-02-26 00:29:46 +01:00
parent 9399e68fda
commit aabe61cd5a
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9

View file

@ -8,6 +8,7 @@
#include <QFontDatabase> #include <QFontDatabase>
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QRawFont>
#include <QStandardPaths> #include <QStandardPaths>
#include <QString> #include <QString>
#include <QTextStream> #include <QTextStream>
@ -26,6 +27,29 @@
#include "config/nheko.h" #include "config/nheko.h"
static QStringList
fontFamilies(bool emoji)
{
static QFontDatabase fontDb;
if (!emoji) {
return fontDb.families();
} else {
static QStringList emojiFamilies = [] {
QStringList families;
const auto originalFamilies = fontDb.families();
for (const auto &family : originalFamilies) {
auto rawFont = QRawFont::fromFont(QFont(family));
if (rawFont.supportsCharacter(0x0001F600ull)) {
families.push_back(family);
}
}
return families;
}();
return emojiFamilies;
}
}
QSharedPointer<UserSettings> UserSettings::instance_; QSharedPointer<UserSettings> UserSettings::instance_;
UserSettings::UserSettings() UserSettings::UserSettings()
@ -1526,7 +1550,6 @@ UserSettingsModel::data(const QModelIndex &index, int role) const
l.push_back(QString::fromStdString(d)); l.push_back(QString::fromStdString(d));
return l; return l;
}; };
static QFontDatabase fontDb;
switch (index.row()) { switch (index.row()) {
case Theme: case Theme:
@ -1546,9 +1569,9 @@ UserSettingsModel::data(const QModelIndex &index, int role) const
i->camera().toStdString(), i->cameraResolution().toStdString())); i->camera().toStdString(), i->cameraResolution().toStdString()));
case Font: case Font:
return fontDb.families(); return fontFamilies(false);
case EmojiFont: case EmojiFont:
return fontDb.families(QFontDatabase::WritingSystem::Symbol); return fontFamilies(true);
case Ringtone: { case Ringtone: {
QStringList l{ QStringList l{
QStringLiteral("Mute"), QStringLiteral("Mute"),
@ -1592,8 +1615,6 @@ UserSettingsModel::data(const QModelIndex &index, int role) const
bool bool
UserSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role) UserSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
static QFontDatabase fontDb;
auto i = UserSettings::instance(); auto i = UserSettings::instance();
if (role == Value) { if (role == Value) {
switch (index.row()) { switch (index.row()) {
@ -1836,15 +1857,14 @@ UserSettingsModel::setData(const QModelIndex &index, const QVariant &value, int
} }
case Font: { case Font: {
if (value.userType() == QMetaType::Int) { if (value.userType() == QMetaType::Int) {
i->setFontFamily(fontDb.families().at(value.toInt())); i->setFontFamily(fontFamilies(false).at(value.toInt()));
return true; return true;
} else } else
return false; return false;
} }
case EmojiFont: { case EmojiFont: {
if (value.userType() == QMetaType::Int) { if (value.userType() == QMetaType::Int) {
i->setEmojiFontFamily( i->setEmojiFontFamily(fontFamilies(true).at(value.toInt()));
fontDb.families(QFontDatabase::WritingSystem::Symbol).at(value.toInt()));
return true; return true;
} else } else
return false; return false;