From aabe61cd5ae91283054336d260944869778e72a3 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 26 Feb 2023 00:29:46 +0100 Subject: [PATCH] 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. --- src/UserSettingsPage.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 7527c17f..66495ac9 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,29 @@ #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::instance_; UserSettings::UserSettings() @@ -1526,7 +1550,6 @@ UserSettingsModel::data(const QModelIndex &index, int role) const l.push_back(QString::fromStdString(d)); return l; }; - static QFontDatabase fontDb; switch (index.row()) { case Theme: @@ -1546,9 +1569,9 @@ UserSettingsModel::data(const QModelIndex &index, int role) const i->camera().toStdString(), i->cameraResolution().toStdString())); case Font: - return fontDb.families(); + return fontFamilies(false); case EmojiFont: - return fontDb.families(QFontDatabase::WritingSystem::Symbol); + return fontFamilies(true); case Ringtone: { QStringList l{ QStringLiteral("Mute"), @@ -1592,8 +1615,6 @@ UserSettingsModel::data(const QModelIndex &index, int role) const bool UserSettingsModel::setData(const QModelIndex &index, const QVariant &value, int role) { - static QFontDatabase fontDb; - auto i = UserSettings::instance(); if (role == Value) { switch (index.row()) { @@ -1836,15 +1857,14 @@ UserSettingsModel::setData(const QModelIndex &index, const QVariant &value, int } case Font: { if (value.userType() == QMetaType::Int) { - i->setFontFamily(fontDb.families().at(value.toInt())); + i->setFontFamily(fontFamilies(false).at(value.toInt())); return true; } else return false; } case EmojiFont: { if (value.userType() == QMetaType::Int) { - i->setEmojiFontFamily( - fontDb.families(QFontDatabase::WritingSystem::Symbol).at(value.toInt())); + i->setEmojiFontFamily(fontFamilies(true).at(value.toInt())); return true; } else return false;