Commit 0325d698 authored by Ahmad Samir's avatar Ahmad Samir

[Fonts KCM] Remove redundant nearestExistingFont()

It seems that nearestExistingFont() gets the same result as
`fc-match font-name`, which is basically the sans serif default font
on the system; this seems redundant as KFontChooser will fallback to
selecting the first font family in the list if the initial font it
was called with doesn't exist _and_ KFontChooser always puts "Sans Serif",
"Serif" and "Monospace" at the top of the list.

Removing nearestExistingFont() solves the issue in bug 420287 as
the font it returns will have the styleName property set, after we
went to so much trouble to clear that property so that setBold() can

CCBUG: 420287

Test Plan:
Before the patch:
- In kdeglobals [General] set:
  fixed=Blah Mono,12,-1,5,50,0,0,0,0,0
  toolBarFont=Bogus Serif,12,-1,5,50,0,0,0,0,0
- Load the fonts KCM, and open the font dialog for Fixed and Toolbar,
  in both cases the default "sans serif" font on the system is selected,
  in my case it's "DejaVu Sans"

Apply the patch and repeat, the "Sans Serif" entry is selected, which is
an alias to "DejaVu Sans" on my system.

Reviewers: #plasma, bport, ngraham

Reviewed By: bport, ngraham

Subscribers: ngraham, plasma-devel

Tags: #plasma

Differential Revision:
parent e064a2cf
......@@ -54,59 +54,6 @@
/**** DLL Interface ****/
K_PLUGIN_FACTORY_WITH_JSON(KFontsFactory, "kcm_fonts.json", registerPlugin<KFonts>();)
//from KFontRequester
// Determine if the font with given properties is available on the system,
// otherwise find and return the best fitting combination.
static QFont nearestExistingFont(const QFont &font)
QFontDatabase dbase;
// Initialize font data according to given font object.
QString family =;
QString style = dbase.styleString(font);
qreal size = font.pointSizeF();
// Check if the family exists.
const QStringList families = dbase.families();
if (!families.contains(family)) {
// Chose another family.
family = QFontInfo(font).family(); // the nearest match
if (!families.contains(family)) {
family = families.count() ? : QStringLiteral("fixed");
// Check if the family has the requested style.
// Easiest by piping it through font selection in the database.
QString retStyle = dbase.styleString(dbase.font(family, style, 10));
style = retStyle;
// Check if the family has the requested size.
// Only for bitmap fonts.
if (!dbase.isSmoothlyScalable(family, style)) {
QList<int> sizes = dbase.smoothSizes(family, style);
if (!sizes.contains(size)) {
// Find nearest available size.
int mindiff = 1000;
int refsize = size;
Q_FOREACH (int lsize, sizes) {
int diff = qAbs(refsize - lsize);
if (mindiff > diff) {
mindiff = diff;
size = lsize;
// Select the font with confirmed properties.
QFont result = dbase.font(family, style, int(size));
if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) {
return result;
/**** KFonts ****/
KFonts::KFonts(QObject *parent, const QVariantList &args)
......@@ -163,24 +110,11 @@ QAbstractItemModel *KFonts::hintingOptionsModel() const
return m_hintingOptionsModel;
void KFonts::setNearestExistingFonts()
void KFonts::load()
// first load all the settings
// Then set the existing fonts based on those settings
// Load preview
// NOTE: This needs to be done AFTER AA settings is loaded
// otherwise AA settings will be reset in process of loading
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment