diff --git a/kcms/fonts/fonts.h b/kcms/fonts/fonts.h --- a/kcms/fonts/fonts.h +++ b/kcms/fonts/fonts.h @@ -160,7 +160,6 @@ private: bool isSaveNeeded() const override; bool isDefaults() const override; - QFont applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags); void setNearestExistingFonts(); FontsSettings *m_settings; diff --git a/kcms/fonts/fonts.cpp b/kcms/fonts/fonts.cpp --- a/kcms/fonts/fonts.cpp +++ b/kcms/fonts/fonts.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include "../krdb/krdb.h" @@ -103,7 +102,27 @@ if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) { result.setPointSizeF(size); } - return result; + + bool sameFont = font.toString() == result.toString(); + // A "Regular" font will have an empty styleName e.g. "DejaVu Sans,12,-1,5,50,0,0,0,0,0" + // so that setBold(true) can work (we deliberately clear the styleName for "Regular"-like + // font styles when saving via KConfig, see: writeEntryGui() from kconfiggroupgui.cpp). + // Here in this method we could end up with a "result" font that's using a Regular style + // and that has the styleName set, so the the original "font" param. and "result" are + // actually the same font + if (!sameFont) { + if (font.weight() == QFont::Normal && font.styleName().isEmpty() + && result.weight() == QFont::Normal + && (result.styleName() == QLatin1String("Regular") + || result.styleName() == QLatin1String("Normal") + || result.styleName() == QLatin1String("Book") + || result.styleName() == QLatin1String("Roman"))) { + result.setStyleName(QString()); + sameFont = font.toString() == result.toString(); + } + } + + return sameFont ? font : result; } /**** FontAASettings ****/ @@ -549,12 +568,35 @@ void KFonts::setNearestExistingFonts() { - m_settings->setFont(nearestExistingFont(m_settings->font())); - m_settings->setFixed(nearestExistingFont(m_settings->fixed())); - m_settings->setSmallestReadableFont(nearestExistingFont(m_settings->smallestReadableFont())); - m_settings->setToolBarFont(nearestExistingFont(m_settings->toolBarFont())); - m_settings->setMenuFont(nearestExistingFont(m_settings->menuFont())); - m_settings->setActiveFont(nearestExistingFont(m_settings->activeFont())); + const QFont font = nearestExistingFont(m_settings->font()); + if (m_settings->font() != font) { + m_settings->setFont(font); + } + + const QFont fixed = nearestExistingFont(m_settings->fixed()); + if (m_settings->fixed() != fixed) { + m_settings->setFixed(fixed); + } + + const QFont smallest = nearestExistingFont(m_settings->smallestReadableFont()); + if (m_settings->smallestReadableFont() != smallest) { + m_settings->setSmallestReadableFont(smallest); + } + + const QFont toolBar = nearestExistingFont(m_settings->toolBarFont()); + if (m_settings->toolBarFont() != toolBar) { + m_settings->setToolBarFont(toolBar); + } + + const QFont menu = nearestExistingFont(m_settings->menuFont()); + if (m_settings->menuFont() != menu) { + m_settings->setMenuFont(menu); + } + + const QFont active = nearestExistingFont(m_settings->activeFont()); + if (m_settings->activeFont() != active) { + m_settings->setActiveFont(active); + } } void KFonts::load() @@ -571,9 +613,6 @@ // otherwise AA settings will be reset in process of loading // previews engine()->addImageProvider("preview", new PreviewImageProvider(m_settings->font())); - - // KCM expect save state to be false at this point (can be true because of setNearestExistingFonts - setNeedsSave(false); } void KFonts::save() @@ -616,52 +655,47 @@ void KFonts::adjustAllFonts() { - QFont font = m_settings->font(); - KFontChooser::FontDiffFlags fontDiffFlags; - int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags); + bool ok = false; + QFont font = QFontDialog::getFont(&ok, m_settings->font(), nullptr, i18n("Select Font")); - if (ret == KDialog::Accepted && fontDiffFlags) { + if (ok) { if (!m_settings->isImmutable("font")) { - m_settings->setFont(applyFontDiff(m_settings->font(), font, fontDiffFlags)); + m_settings->setFont(font); } if (!m_settings->isImmutable("menuFont")) { - m_settings->setMenuFont(applyFontDiff(m_settings->menuFont(), font, fontDiffFlags)); + m_settings->setMenuFont(font); } if (!m_settings->isImmutable("toolBarFont")) { - m_settings->setToolBarFont(applyFontDiff(m_settings->toolBarFont(), font, fontDiffFlags)); + m_settings->setToolBarFont(font); } if (!m_settings->isImmutable("activeFont")) { - m_settings->setActiveFont(applyFontDiff(m_settings->activeFont(), font, fontDiffFlags)); + m_settings->setActiveFont(font); } if (!m_settings->isImmutable("smallestReadableFont")) { - m_settings->setSmallestReadableFont(applyFontDiff(m_settings->smallestReadableFont(), font, fontDiffFlags)); + m_settings->setSmallestReadableFont(font); } - const QFont adjustedFont = applyFontDiff(m_settings->fixed(), font, fontDiffFlags); - if (QFontInfo(adjustedFont).fixedPitch() && !m_settings->isImmutable("fixed")) { - m_settings->setFixed(adjustedFont); - } - } -} - -QFont KFonts::applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags) -{ - QFont font(fnt); - if (fontDiffFlags & KFontChooser::FontDiffSize) { - font.setPointSizeF(newFont.pointSizeF()); - } - if ((fontDiffFlags & KFontChooser::FontDiffFamily)) { - font.setFamily(newFont.family()); - } - if (fontDiffFlags & KFontChooser::FontDiffStyle) { - font.setWeight(newFont.weight()); - font.setStyle(newFont.style()); - font.setUnderline(newFont.underline()); - font.setStyleName(newFont.styleName()); + // Try to find a fixed-width/monspace font from the font family the user selected, e.g.: + // "Noto Sans" -> "Noto Sans Mono", "Ubuntu" -> "Ubuntu Mono" + // we take the first one that comes along, e.g. "Noto Sans Mono" _not_ "Noto Sans Mono Blk" + QFontDatabase fdb; + const QStringList fontFamilies = fdb.families(); + QString mono; + for (const QString &family : fontFamilies) { + // QString::endsWith() is cheaper than contains(), so try it first + if (family.startsWith(font.family()) + && (family.endsWith(QLatin1String("Mono")) + || family.contains(QLatin1String("Mono")))) { + mono = family; + break; + } + } + // fallback if we find nothing + QFont f = mono.isEmpty() ? QFontDatabase::systemFont(QFontDatabase::FixedFont) : QFont(mono); + if (!m_settings->isImmutable("fixed")) { + m_settings->setFixed(f); + } } - - return font; } #include "fonts.moc" -