fonts.cpp 27.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
    Copyright 1997 Mark Donohoe
    Copyright 1999 Lars Knoll
    Copyright 2000 Rik Hemsley

    Ported to kcontrol2 by Geert Jansen.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
23
24
25
26
27
28
29

#include <config-workspace.h>

#include "fonts.h"

#include <stdlib.h>

30
#include <QApplication>
31
32
#include <QCheckBox>
#include <QComboBox>
33
34
#include <QDialogButtonBox>
#include <QDoubleSpinBox>
35
#include <QLabel>
36
#include <QProcess>
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <QPushButton>
#include <QSpinBox>
#include <qplatformdefs.h>

//Added by qt3to4:
#include <QPixmap>
#include <QByteArray>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>

#include <KFontDialog>
#include <KAcceleratorManager>
#include <KGlobalSettings>
#include <KMessageBox>
#include <KConfig>
54
#include <KConfigGroup>
55
56
#include <KLocalizedString>
#include <KPluginFactory>
57
#include <KWindowSystem>
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include "../krdb/krdb.h"

#ifdef HAVE_FREETYPE
#include <ft2build.h>
#ifdef FT_LCD_FILTER_H
#include FT_FREETYPE_H
#include FT_LCD_FILTER_H
#endif
#endif

#if HAVE_X11
#include <X11/Xlib.h>
#endif

#if HAVE_X11
// X11 headers
#undef Bool
#undef Unsorted
#undef None
#endif

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
static const char *const aa_rgb_xpm[] = {
    "12 12 3 1",
    "a c #0000ff",
    "# c #00ff00",
    ". c #ff0000",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa"
};
static const char *const aa_bgr_xpm[] = {
    "12 12 3 1",
    ". c #0000ff",
    "# c #00ff00",
    "a c #ff0000",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa",
    "....####aaaa"
};
static const char *const aa_vrgb_xpm[] = {
    "12 12 3 1",
    "a c #0000ff",
    "# c #00ff00",
    ". c #ff0000",
    "............",
    "............",
    "............",
    "............",
    "############",
    "############",
    "############",
    "############",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa"
};
static const char *const aa_vbgr_xpm[] = {
    "12 12 3 1",
    ". c #0000ff",
    "# c #00ff00",
    "a c #ff0000",
    "............",
    "............",
    "............",
    "............",
    "############",
    "############",
    "############",
    "############",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa",
    "aaaaaaaaaaaa"
};

153
static const char *const *const aaPixmaps[] = { 0, 0, aa_rgb_xpm, aa_bgr_xpm, aa_vrgb_xpm, aa_vbgr_xpm };
154
155

/**** DLL Interface ****/
156
K_PLUGIN_FACTORY(FontFactory, registerPlugin<KFonts>();)
157
158
159
160

/**** FontUseItem ****/

FontUseItem::FontUseItem(
161
162
163
164
165
166
167
    QWidget *parent,
    const QString &name,
    const QString &grp,
    const QString &key,
    const QString &rc,
    const QFont &default_fnt,
    bool f
168
)
169
170
171
172
173
    : KFontRequester(parent, f),
      _rcfile(rc),
      _rcgroup(grp),
      _rckey(key),
      _default(default_fnt)
174
{
175
176
177
    KAcceleratorManager::setNoAccel(this);
    setTitle(name);
    readFont();
178
179
180
181
}

void FontUseItem::setDefault()
{
182
    setFont(_default, isFixedOnly());
183
184
185
186
}

void FontUseItem::readFont()
{
187
188
    const KConfig *config = KSharedConfig::openConfig(_rcfile).data();
    const KConfigGroup group(config, _rcgroup);
189
190
    QFont tmpFnt(_default);
    setFont(group.readEntry(_rckey, tmpFnt), isFixedOnly());
191
192
193
194
}

void FontUseItem::writeFont()
{
195
    KConfig *config = KSharedConfig::openConfig(_rcfile).data();
196
197
198
199
200
201
    if (_rcfile.isEmpty()) {
        KConfigGroup(config, _rcgroup).writeEntry(_rckey, font(), KConfig::Normal | KConfig::Global);
    } else {
        KConfigGroup(config, _rcgroup).writeEntry(_rckey, font());
        config->sync();
    }
202
203
}

204
void FontUseItem::applyFontDiff(const QFont &fnt, int fontDiffFlags)
205
{
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    QFont _font(font());

    if (fontDiffFlags & KFontChooser::FontDiffSize) {
        _font.setPointSizeF(fnt.pointSizeF());
    }
    if (fontDiffFlags & KFontChooser::FontDiffFamily) {
        if (!isFixedOnly() || QFontInfo(fnt).fixedPitch()) {
            _font.setFamily(fnt.family());
        }
    }
    if (fontDiffFlags & KFontChooser::FontDiffStyle) {
        _font.setWeight(fnt.weight());
        _font.setStyle(fnt.style());
        _font.setUnderline(fnt.underline());
220
#if QT_VERSION >= 0x040800
221
        _font.setStyleName(fnt.styleName());
222
#endif
223
    }
224

225
    setFont(_font, isFixedOnly());
226
227
228
229
230
}

/**** FontAASettings ****/
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
FontAASettings::FontAASettings(QWidget *parent)
231
    : QDialog(parent),
232
      changesMade(false)
233
{
234
235
    setObjectName("FontAASettings");
    setModal(true);
236
    setWindowTitle(i18n("Configure Anti-Alias Settings"));
237

238
239
240
241
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    QFormLayout *layout = new QFormLayout();
    layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
242

243
    excludeRange = new QCheckBox(i18n("E&xclude range:"), this);
244
    QHBoxLayout *rangeLayout = new QHBoxLayout();
245
246
247
248
249
250
251

    excludeFrom = new QDoubleSpinBox(this);
    excludeFrom->setRange(0.0, 72.0);
    excludeFrom->setValue(8.0);
    excludeFrom->setSingleStep(1.0);
    excludeFrom->setDecimals(1);
    excludeFrom->setSuffix(i18nc("abbreviation for unit of points", " pt"));
252
    rangeLayout->addWidget(excludeFrom);
253
254

    excludeToLabel = new QLabel(i18n(" to "), this);
255
    rangeLayout->addWidget(excludeToLabel);
256
257
258
259
260
261
262

    excludeTo = new QDoubleSpinBox(this);
    excludeTo->setRange(0.0, 72.0);
    excludeTo->setValue(15.0);
    excludeTo->setSingleStep(1.0);
    excludeTo->setDecimals(1);
    excludeTo->setSuffix(i18nc("abbreviation for unit of points", " pt"));
263
    rangeLayout->addWidget(excludeTo);
264

265
266
267
268
269
270
271
272
273
274
275
276
277
    layout->addRow(excludeRange, rangeLayout);

    QString subPixelWhatsThis = i18n("<p>If you have a TFT or LCD screen you"
                                     " can further improve the quality of displayed fonts by selecting"
                                     " this option.<br />Sub-pixel rendering is also known as ClearType(tm).<br />"
                                     " In order for sub-pixel rendering to"
                                     " work correctly you need to know how the sub-pixels of your display"
                                     " are aligned.</p>"
                                     " <p>On TFT or LCD displays a single pixel is actually composed of"
                                     " three sub-pixels, red, green and blue. Most displays"
                                     " have a linear ordering of RGB sub-pixel, some have BGR.<br />"
                                     " This feature does not work with CRT monitors.</p>");

278
    subPixelLabel = new QLabel(i18n("Sub-pixel rendering type:"), this);
279
    subPixelLabel->setWhatsThis(subPixelWhatsThis);
280

281
    subPixelType = new QComboBox(this);
282
    layout->addRow(subPixelLabel, subPixelType);
283
284
285
286

    subPixelType->setEditable(false);
    subPixelType->setWhatsThis(subPixelWhatsThis);

287
288
    for (int t = KXftConfig::SubPixel::NotSet; t <= KXftConfig::SubPixel::Vbgr; ++t) {
        subPixelType->addItem(QPixmap(aaPixmaps[t]), i18n(KXftConfig::description((KXftConfig::SubPixel::Type)t).toUtf8()));
289
290
    }

291
292
    QLabel *hintingLabel = new QLabel(i18n("Hinting style:"), this);
    hintingStyle = new QComboBox(this);
293
294
    hintingStyle->setEditable(false);
    layout->addRow(hintingLabel, hintingStyle);
295
    for (int s = KXftConfig::Hint::NotSet; s <= KXftConfig::Hint::Full; ++s) {
296
297
298
299
300
301
302
303
        hintingStyle->addItem(i18n(KXftConfig::description((KXftConfig::Hint::Style)s).toUtf8()));
    }

    QString hintingText(i18n("Hinting is a process used to enhance the quality of fonts at small sizes."));
    hintingStyle->setWhatsThis(hintingText);
    hintingLabel->setWhatsThis(hintingText);
    load();
    enableWidgets();
304
305
306
307
308
309
310
311
312
313
314
315
316
317

    QHBoxLayout *outerLayout = new QHBoxLayout();
    outerLayout->addLayout(layout);
    outerLayout->addStretch(1);

    mainLayout->addLayout(outerLayout);
    mainLayout->addStretch(1);
    mainLayout->addSpacing(style()->pixelMetric(QStyle::PM_LayoutBottomMargin));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this);
    buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL|Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    mainLayout->addWidget(buttonBox);
318

319
    connect(excludeRange, &QAbstractButton::toggled, this, &FontAASettings::changed);
320
321
322
323
324
325
326
327
    connect(excludeFrom, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &FontAASettings::changed);
    connect(excludeTo, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &FontAASettings::changed);
    connect(subPixelType, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
            this, &FontAASettings::changed);
    connect(hintingStyle, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
            this, &FontAASettings::changed);
328
329
330
331
}

bool FontAASettings::load()
{
332
333
334
335
336
337
338
339
340
341
    double     from, to;
    KXftConfig xft;

    if (xft.getExcludeRange(from, to)) {
        excludeRange->setChecked(true);
    } else {
        excludeRange->setChecked(false);
        from = 8.0;
        to = 15.0;
    }
342

343
344
    excludeFrom->setValue(from);
    excludeTo->setValue(to);
345

346
    KXftConfig::SubPixel::Type spType;
347

348
349
    xft.getSubPixelType(spType);
    int idx = getIndex(spType);
350

351
    subPixelType->setCurrentIndex(idx);
352

353
    KXftConfig::Hint::Style hStyle;
354

355
    if (!xft.getHintStyle(hStyle) || KXftConfig::Hint::NotSet == hStyle) {
356
        hStyle = KXftConfig::Hint::NotSet;
357
358
359
        xft.setHintStyle(hStyle);
        runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);
    }
360

361
    hintingStyle->setCurrentIndex(getIndex(hStyle));
362

363
    enableWidgets();
364

365
    return xft.aliasingEnabled();
366
367
}

368
bool FontAASettings::save(KXftConfig::AntiAliasing::State aaState)
369
{
370
371
372
    KXftConfig   xft;
    KConfig      kglobals("kdeglobals", KConfig::NoGlobals);
    KConfigGroup grp(&kglobals, "General");
373

374
    xft.setAntiAliasing(aaState);
375

376
377
378
379
380
    if (excludeRange->isChecked()) {
        xft.setExcludeRange(excludeFrom->value(), excludeTo->value());
    } else {
        xft.setExcludeRange(0, 0);
    }
381

382
    KXftConfig::SubPixel::Type spType(getSubPixelType());
383

384
    xft.setSubPixelType(spType);
385
386
387
388
389
390
    if (KXftConfig::SubPixel::NotSet == spType) {
        grp.revertToDefault("XftSubPixel");
    } else {
        grp.writeEntry("XftSubPixel", KXftConfig::toStr(spType));
    }

391
392
393
394
395
    if (KXftConfig::AntiAliasing::NotSet == aaState) {
        grp.revertToDefault("XftAntialias");
    } else {
        grp.writeEntry("XftAntialias", aaState == KXftConfig::AntiAliasing::Enabled);
    }
396

397
398
    bool mod = false;
    KXftConfig::Hint::Style hStyle(getHintStyle());
399

400
    xft.setHintStyle(hStyle);
401

402
    QString hs(KXftConfig::toStr(hStyle));
403
404
405
406
407
408
    if (hs != grp.readEntry("XftHintStyle")) {
        if (KXftConfig::Hint::NotSet == hStyle) {
            grp.revertToDefault("XftHintStyle");
        } else {
            grp.writeEntry("XftHintStyle", hs);
        }
409
    }
410
    mod = true;
411
    kglobals.sync();
412

413
414
415
    if (!mod) {
        mod = xft.changed();
    }
416

417
    xft.apply();
418

419
    return mod;
420
421
422
423
}

void FontAASettings::defaults()
{
424
    excludeRange->setChecked(false);
425
426
    excludeFrom->setValue(8.0);
    excludeTo->setValue(15.0);
427
428
    subPixelType->setCurrentIndex(getIndex(KXftConfig::SubPixel::NotSet));
    hintingStyle->setCurrentIndex(getIndex(KXftConfig::Hint::NotSet));
429
    enableWidgets();
430
431
432
433
}

int FontAASettings::getIndex(KXftConfig::SubPixel::Type spType)
{
434
435
436
437
438
439
440
441
    int pos = -1;
    int index;

    for (index = 0; index < subPixelType->count(); ++index)
        if (subPixelType->itemText(index) == i18n(KXftConfig::description(spType).toUtf8())) {
            pos = index;
            break;
        }
442

443
    return pos;
444
445
446
447
}

KXftConfig::SubPixel::Type FontAASettings::getSubPixelType()
{
448
    int t;
449

450
    for (t = KXftConfig::SubPixel::NotSet; t <= KXftConfig::SubPixel::Vbgr; ++t)
451
452
453
        if (subPixelType->currentText() == i18n(KXftConfig::description((KXftConfig::SubPixel::Type)t).toUtf8())) {
            return (KXftConfig::SubPixel::Type)t;
        }
454

455
    return KXftConfig::SubPixel::NotSet;
456
457
458
459
}

int FontAASettings::getIndex(KXftConfig::Hint::Style hStyle)
{
460
461
    int pos = -1;
    int index;
462

463
464
465
466
467
    for (index = 0; index < hintingStyle->count(); ++index)
        if (hintingStyle->itemText(index) == i18n(KXftConfig::description(hStyle).toUtf8())) {
            pos = index;
            break;
        }
468

469
470
    return pos;
}
471
472
473

KXftConfig::Hint::Style FontAASettings::getHintStyle()
{
474
    int s;
475

476
477
478
479
    for (s = KXftConfig::Hint::NotSet; s <= KXftConfig::Hint::Full; ++s)
        if (hintingStyle->currentText() == i18n(KXftConfig::description((KXftConfig::Hint::Style)s).toUtf8())) {
            return (KXftConfig::Hint::Style)s;
        }
480

481
    return KXftConfig::Hint::Medium;
482
483
484
485
}

void FontAASettings::enableWidgets()
{
486
487
488
    excludeFrom->setEnabled(excludeRange->isChecked());
    excludeTo->setEnabled(excludeRange->isChecked());
    excludeToLabel->setEnabled(excludeRange->isChecked());
489
#ifdef FT_LCD_FILTER_H
490
491
492
493
494
495
496
497
    static int ft_has_subpixel = -1;
    if (ft_has_subpixel == -1) {
        FT_Library            ftLibrary;
        if (FT_Init_FreeType(&ftLibrary) == 0) {
            ft_has_subpixel = (FT_Library_SetLcdFilter(ftLibrary, FT_LCD_FILTER_DEFAULT)
                               == FT_Err_Unimplemented_Feature) ? 0 : 1;
            FT_Done_FreeType(ftLibrary);
        }
498
    }
499
    subPixelType->setEnabled(ft_has_subpixel);
500
501
502
503
504
505
506
#endif
}
#endif

void FontAASettings::changed()
{
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
507
    changesMade = true;
508
509
510
511
512
513
514
    enableWidgets();
#endif
}

#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
int FontAASettings::exec()
{
515
    const int i = QDialog::exec();
516

517
518
519
    if (!i) {
        load();    // Reset settings...
    }
520
521
522
523
524
525
526
527
528
529

    return i && changesMade;
}
#endif

/**** KFonts ****/

KFonts::KFonts(QWidget *parent, const QVariantList &args)
    :   KCModule(parent, args)
{
530
    QStringList nameGroupKeyRc;
531

532
533
534
535
536
537
    nameGroupKeyRc
            << i18nc("font usage", "General")       << "General"    << "font"         << ""
            << i18nc("font usage", "Fixed width")   << "General"    << "fixed"        << ""
            << i18nc("font usage", "Small")         << "General"    << "smallestReadableFont" << ""
            << i18nc("font usage", "Toolbar")       << "General"    << "toolBarFont"  << ""
            << i18nc("font usage", "Menu")          << "General"    << "menuFont"     << ""
538
            << i18nc("font usage", "Window title")  << "WM"         << "activeFont"   << "";
539

540
    QList<QFont> defaultFontList;
541

542
    // NOTE: keep in sync with plasma-integration/src/platformtheme/kfontsettingsdata.cpp
543

544
#ifdef Q_OS_MACOS
545
546
547
    QFont f0("Lucida Grande", 13); // general/menu/desktop
    QFont f1("Monaco", 10);
    QFont f2("Lucida Grande", 11); // toolbar
548
#elif defined(Q_WS_MAEMO_5) || defined(MEEGO_EDITION_HARMATTAN)
549
550
    QFont f0("Sans Serif", 16); // general/menu/desktop
    QFont f1("Monospace", 16;
551
    QFont f2("Sans Serif", 16); // toolbar
552
#else
553
    QFont f0("Noto Sans", 10); // general/menu/desktop
554
    QFont f1("Hack", 9); // fixed font
555
    QFont f2("Noto Sans", 10); // toolbar
556
#endif
557
558
559
560
#ifdef Q_OS_MACOS
    QFont f3("Lucida Grande", 14); // window title
    QFont f5("Lucida Grande", 9); // smallestReadableFont
#else
561
562
    QFont f3("Noto Sans", 10); // window title
    QFont f5("Noto Sans", 8); // smallestReadableFont
563
#endif
564

Lukáš Tinkl's avatar
Lukáš Tinkl committed
565
    defaultFontList << f0 << f1 << f5 << f2 << f0 << f3;
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

    QList<bool> fixedList;

    fixedList
            <<  false
            <<  true
            <<  false
            <<  false
            <<  false
            <<  false;

    QStringList quickHelpList;

    quickHelpList
            << i18n("Used for normal text (e.g. button labels, list items).")
            << i18n("A non-proportional font (i.e. typewriter font).")
            << i18n("Smallest font that is still readable well.")
            << i18n("Used to display text beside toolbar icons.")
            << i18n("Used by menu bars and popup menus.")
585
            << i18n("Used by the window titlebar.");
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setMargin(0);

    QGridLayout *fontUseLayout = new QGridLayout();
    layout->addLayout(fontUseLayout);
    fontUseLayout->setColumnStretch(0, 0);
    fontUseLayout->setColumnStretch(1, 1);
    fontUseLayout->setColumnStretch(2, 0);

    QList<QFont>::ConstIterator defaultFontIt(defaultFontList.begin());
    QList<bool>::ConstIterator fixedListIt(fixedList.begin());
    QStringList::ConstIterator quickHelpIt(quickHelpList.begin());
    QStringList::ConstIterator it(nameGroupKeyRc.begin());

    unsigned int count = 0;

    while (it != nameGroupKeyRc.constEnd()) {
        QString name = *it; it++;
        QString group = *it; it++;
        QString key = *it; it++;
        QString file = *it; it++;

        FontUseItem *i =
            new FontUseItem(
            this,
            name,
            group,
            key,
            file,
            *defaultFontIt++,
            *fixedListIt++
        );

        fontUseList.append(i);
621
        connect(i, &KFontRequester::fontSelected, this, &KFonts::fontSelected);
622
623
624
625
626

        QLabel *fontUse = new QLabel(i18nc("Font role", "%1: ", name), this);
        fontUse->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        fontUse->setWhatsThis(*quickHelpIt++);

627
628
        fontUse->setBuddy(i);

629
630
631
632
633
634
635
636
637
638
639
640
        fontUseLayout->addWidget(fontUse, count, 0);
        fontUseLayout->addWidget(i, count, 1);

        ++count;
    }

    QHBoxLayout *hblay = new QHBoxLayout();
    layout->addLayout(hblay);
    hblay->addStretch();
    QPushButton *fontAdjustButton = new QPushButton(i18n("Ad&just All Fonts..."), this);
    fontAdjustButton->setWhatsThis(i18n("Click to change all fonts"));
    hblay->addWidget(fontAdjustButton);
641
    connect(fontAdjustButton, &QAbstractButton::clicked, this, &KFonts::slotApplyFontDiff);
642

643
    layout->addSpacing(style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
644
645
646
647

    QGridLayout *lay = new QGridLayout();
    layout->addLayout(lay);
    lay->setColumnStretch(3, 10);
648
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
649
650
651
652
653
654
655
656
    QLabel *label = 0L;
    label = new QLabel(i18n("Use a&nti-aliasing:"), this);
    label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    lay->addWidget(label, 0, 0);
    cbAA = new QComboBox(this);
    cbAA->insertItem(AAEnabled, i18nc("Use anti-aliasing", "Enabled"));    // change AASetting type if order changes
    cbAA->insertItem(AASystem, i18nc("Use anti-aliasing", "System Settings"));
    cbAA->insertItem(AADisabled, i18nc("Use anti-aliasing", "Disabled"));
657
    cbAA->setWhatsThis(i18n("Select this option to smooth the edges of curves in fonts."));
658
    aaSettingsButton = new QPushButton(i18n("Configure..."), this);
659
    connect(aaSettingsButton, &QAbstractButton::clicked, this, &KFonts::slotCfgAa);
660
661
662
    label->setBuddy(cbAA);
    lay->addWidget(cbAA, 0, 1);
    lay->addWidget(aaSettingsButton, 0, 2);
663
664
665
666
    // Initialize aaSettingsButton state based on the current cbAA->currentIndex value, will be eventually updated at load()
    slotUseAntiAliasing();

    connect(cbAA, SIGNAL(currentIndexChanged(int)), SLOT(slotUseAntiAliasing()));
667
#endif
668
669
670
671
672
673
674
675
676
677
678
679
    checkboxForceDpi = new QCheckBox(i18n("Force fonts DPI:"), this);
    lay->addWidget(checkboxForceDpi, 1, 0);
    spinboxDpi = new QSpinBox(this);
    spinboxDpi->setRange(1, 1000);
    spinboxDpi->setSingleStep(24); // The common DPI values 72, 96 and 120 are multiples of 24
    QString whatsthis = i18n(
                            "<p>This option forces a specific DPI value for fonts. It may be useful"
                            " when the real DPI of the hardware is not detected properly and it"
                            " is also often misused when poor quality fonts are used that do not"
                            " look well with DPI values other than 96 or 120 DPI.</p>"
                            "<p>The use of this option is generally discouraged. For selecting proper DPI"
                            " value a better option is explicitly configuring it for the whole X server if"
680
                            " possible (e.g. DisplaySize in xorg.conf). When fonts do not render"
681
682
683
684
685
686
687
                            " properly with real DPI value better fonts should be used or configuration"
                            " of font hinting should be checked.</p>");
    spinboxDpi->setWhatsThis(whatsthis);
    checkboxForceDpi->setChecked(false);
    spinboxDpi->setEnabled(false);
    connect(spinboxDpi, SIGNAL(valueChanged(int)), SLOT(changed()));
    connect(checkboxForceDpi, SIGNAL(toggled(bool)), SLOT(changed()));
688
    connect(checkboxForceDpi, &QAbstractButton::toggled, spinboxDpi, &QWidget::setEnabled);
689
690
    lay->addWidget(spinboxDpi, 1, 1);
    layout->addStretch(1);
691
692

#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
693
    aaSettings = new FontAASettings(this);
694
695
696
697
698
699
#endif

}

KFonts::~KFonts()
{
700
701
    QList<FontUseItem *>::Iterator it(fontUseList.begin()),
          end(fontUseList.end());
702

703
704
705
706
    for (; it != end; ++it) {
        delete(*it);
    }
    fontUseList.clear();
707
708
709
710
}

void KFonts::fontSelected()
{
711
    emit changed(true);
712
713
714
715
}

void KFonts::defaults()
{
716
717
718
    for (int i = 0; i < (int) fontUseList.count(); i++) {
        fontUseList.at(i)->setDefault();
    }
719
720

#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
721
722
723
    useAA = AASystem;
    cbAA->setCurrentIndex(useAA);
    aaSettings->defaults();
724
#endif
725
726
727
    checkboxForceDpi->setChecked(false);
    spinboxDpi->setValue(96);
    emit changed(true);
728
729
730
731
}

void KFonts::load()
{
732
733
    QList<FontUseItem *>::Iterator it(fontUseList.begin()),
          end(fontUseList.end());
734

735
736
737
    for (; it != end; ++it) {
        (*it)->readFont();
    }
738
739

#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
740
741
    useAA_original = useAA = aaSettings->load() ? AAEnabled : AADisabled;
    cbAA->setCurrentIndex(useAA);
742
743
#endif

744
745
    KConfig _cfgfonts("kcmfonts");
    KConfigGroup cfgfonts(&_cfgfonts, "General");
746
747
748
749
750
751
752
    int dpicfg;
    if (KWindowSystem::isPlatformWayland()) {
        dpicfg = cfgfonts.readEntry("forceFontDPIWayland", 0);
    } else {
        dpicfg = cfgfonts.readEntry("forceFontDPI", 0);
    }

753
754
755
756
757
758
759
760
761
    if (dpicfg <= 0) {
        checkboxForceDpi->setChecked(false);
        spinboxDpi->setValue(96);
        dpi_original = 0;
    } else {
        checkboxForceDpi->setChecked(true);
        spinboxDpi->setValue(dpicfg);
        dpi_original = dpicfg;
    };
762
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
763
764
765
766
    if (cfgfonts.readEntry("dontChangeAASettings", true)) {
        useAA_original = useAA = AASystem;
        cbAA->setCurrentIndex(useAA);
    }
767
768
#endif

769
    emit changed(false);
770
771
772
773
}

void KFonts::save()
{
774
775
    QList<FontUseItem *>::Iterator it(fontUseList.begin()),
          end(fontUseList.end());
776

777
778
779
    for (; it != end; ++it) {
        (*it)->writeFont();
    }
780

781
    KSharedConfig::openConfig()->sync();
782

783
784
785
    KConfig _cfgfonts("kcmfonts");
    KConfigGroup cfgfonts(&_cfgfonts, "General");
    int dpi = (checkboxForceDpi->isChecked() ? spinboxDpi->value() : 0);
786
787
788
789
790
    if (KWindowSystem::isPlatformWayland()) {
        cfgfonts.writeEntry("forceFontDPIWayland", dpi);
    } else {
        cfgfonts.writeEntry("forceFontDPI", dpi);
    }
791
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
792
    cfgfonts.writeEntry("dontChangeAASettings", cbAA->currentIndex() == AASystem);
793
#endif
794
    cfgfonts.sync();
795
#if HAVE_X11
796
797
798
    // if the setting is reset in the module, remove the dpi value,
    // otherwise don't explicitly remove it and leave any possible system-wide value
    if (dpi == 0 && dpi_original != 0) {
799
800
801
        QProcess proc;
        proc.setProcessChannelMode(QProcess::ForwardedChannels);
        proc.start("xrdb", QStringList() << "-quiet" << "-remove" << "-nocpp");
802
803
804
805
806
807
        if (proc.waitForStarted()) {
            proc.write(QByteArray("Xft.dpi\n"));
            proc.closeWriteChannel();
            proc.waitForFinished();
        }
    }
808
809
#endif

810
    KGlobalSettings::self()->emitChange(KGlobalSettings::FontChanged);
811

812
    QApplication::processEvents();			// Process font change ourselves
813

814
815
816
    // Don't overwrite global settings unless explicitly asked for - e.g. the system
    // fontconfig setup may be much more complex than this module can provide.
    // TODO: With AASystem the changes already made by this module should be reverted somehow.
817
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
818
    bool aaSave = false;
819
820
821
822
823
824
825
826
827
    if (cbAA->currentIndex() == AAEnabled ) {
        aaSave = aaSettings->save(KXftConfig::AntiAliasing::Enabled);
    } else if (cbAA->currentIndex() == AADisabled) {
        aaSave = aaSettings->save(KXftConfig::AntiAliasing::Disabled);
    } else {
        // If AASystem is selected, this removes all fontconfig settings made by
        // this module.
        aaSettings->defaults();
        aaSave = aaSettings->save(KXftConfig::AntiAliasing::NotSet);
828
829
830
831
832
833
834
835
836
837
    }

    if (aaSave || (useAA != useAA_original) || dpi != dpi_original) {
        KMessageBox::information(this,
                                 i18n(
                                     "<p>Some changes such as anti-aliasing or DPI will only affect newly started applications.</p>"
                                 ), i18n("Font Settings Changed"), "FontSettingsChanged");
        useAA_original = useAA;
        dpi_original = dpi;
    }
838
839
#else
#if HAVE_X11
840
841
842
843
844
845
846
    if (dpi != dpi_original) {
        KMessageBox::information(this,
                                 i18n(
                                     "<p>Some changes such as DPI will only affect newly started applications.</p>"
                                 ), i18n("Font Settings Changed"), "FontSettingsChanged");
        dpi_original = dpi;
    }
847
848
#endif
#endif
849
    runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);
850

851
    emit changed(false);
852
853
854
855
}

void KFonts::slotApplyFontDiff()
{
856
857
858
859
860
861
862
863
864
865
    QFont font = QFont(fontUseList.first()->font());
    KFontChooser::FontDiffFlags fontDiffFlags = 0;
    int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags, this);

    if (ret == KDialog::Accepted && fontDiffFlags) {
        for (int i = 0; i < (int) fontUseList.count(); i++) {
            fontUseList.at(i)->applyFontDiff(font, fontDiffFlags);
        }
        emit changed(true);
    }
866
867
868
869
870
}

void KFonts::slotUseAntiAliasing()
{
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
871
    useAA = static_cast< AASetting >(cbAA->currentIndex());
872
    aaSettingsButton->setEnabled(useAA == AAEnabled);
873
874
875
876
877
878
879
    emit changed(true);
#endif
}

void KFonts::slotCfgAa()
{
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
880
881
882
    if (aaSettings->exec()) {
        emit changed(true);
    }
883
884
885
886
887
#endif
}

#include "fonts.moc"