File indexing completed on 2024-05-19 05:38:06

0001 #pragma once
0002 
0003 /*
0004    SPDX-FileCopyrightText: 2002 Craig Drummond <craig@kde.org>
0005 
0006    SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include <config-workspace.h>
0010 
0011 #ifdef HAVE_FONTCONFIG
0012 
0013 #include <QDateTime>
0014 #include <QDomDocument>
0015 #include <QMetaType>
0016 #include <QStringList>
0017 
0018 class KXftConfig
0019 {
0020 public:
0021     struct Item {
0022         Item(QDomNode &n)
0023             : node(n)
0024             , toBeRemoved(false)
0025         {
0026         }
0027         Item()
0028             : toBeRemoved(false)
0029         {
0030         }
0031         virtual void reset()
0032         {
0033             node.clear();
0034             toBeRemoved = false;
0035         }
0036         bool added()
0037         {
0038             return node.isNull();
0039         }
0040 
0041         QDomNode node;
0042 
0043         virtual ~Item()
0044         {
0045         }
0046         bool toBeRemoved;
0047     };
0048 
0049     struct SubPixel : public Item {
0050         enum Type {
0051             NotSet,
0052             None,
0053             Rgb,
0054             Bgr,
0055             Vrgb,
0056             Vbgr,
0057         };
0058 
0059         SubPixel(Type t, QDomNode &n)
0060             : Item(n)
0061             , type(t)
0062         {
0063         }
0064         SubPixel(Type t = NotSet)
0065             : type(t)
0066         {
0067         }
0068 
0069         void reset() override
0070         {
0071             Item::reset();
0072             type = NotSet;
0073         }
0074 
0075         Type type;
0076     };
0077 
0078     struct Exclude : public Item {
0079         Exclude(double f, double t, QDomNode &n)
0080             : Item(n)
0081             , from(f)
0082             , to(t)
0083         {
0084         }
0085         Exclude(double f = 0, double t = 0)
0086             : from(f)
0087             , to(t)
0088         {
0089         }
0090 
0091         void reset() override
0092         {
0093             Item::reset();
0094             from = to = 0;
0095         }
0096 
0097         double from, to;
0098     };
0099 
0100     struct Hint : public Item {
0101         enum Style {
0102             NotSet,
0103             None,
0104             Slight,
0105             Medium,
0106             Full,
0107         };
0108 
0109         Hint(Style s, QDomNode &n)
0110             : Item(n)
0111             , style(s)
0112         {
0113         }
0114         Hint(Style s = NotSet)
0115             : style(s)
0116         {
0117         }
0118 
0119         void reset() override
0120         {
0121             Item::reset();
0122             style = NotSet;
0123         }
0124 
0125         Style style;
0126     };
0127 
0128     struct Hinting : public Item {
0129         Hinting(bool s, QDomNode &n)
0130             : Item(n)
0131             , set(s)
0132         {
0133         }
0134         Hinting(bool s = true)
0135             : set(s)
0136         {
0137         }
0138 
0139         void reset() override
0140         {
0141             Item::reset();
0142             set = true;
0143         }
0144 
0145         bool set;
0146     };
0147 
0148     struct AntiAliasing : public Item {
0149         enum State {
0150             NotSet,
0151             Enabled,
0152             Disabled,
0153         };
0154 
0155         AntiAliasing(State s, QDomNode &n)
0156             : Item(n)
0157             , state(s)
0158         {
0159         }
0160         AntiAliasing(State s = NotSet)
0161             : state(s)
0162         {
0163         }
0164 
0165         void reset() override
0166         {
0167             Item::reset();
0168             state = NotSet;
0169         }
0170 
0171         enum State state;
0172     };
0173 
0174 public:
0175     explicit KXftConfig(const QString &path = {});
0176 
0177     virtual ~KXftConfig();
0178 
0179     bool reset();
0180     bool apply();
0181     bool getSubPixelType(SubPixel::Type &type);
0182     void setSubPixelType(SubPixel::Type type); // SubPixel::None => turn off sub-pixel rendering
0183     bool getExcludeRange(double &from, double &to);
0184     void setExcludeRange(double from, double to); // from:0, to:0 => turn off exclude range
0185     bool getHintStyle(Hint::Style &style);
0186     void setHintStyle(Hint::Style style);
0187     void setAntiAliasing(AntiAliasing::State state);
0188     AntiAliasing::State getAntiAliasing() const;
0189     bool antiAliasingHasLocalConfig() const;
0190     bool subPixelTypeHasLocalConfig() const;
0191     bool hintStyleHasLocalConfig() const;
0192     bool changed()
0193     {
0194         return m_madeChanges;
0195     }
0196     static QString description(SubPixel::Type t);
0197     static const char *toStr(SubPixel::Type t);
0198     static QString description(Hint::Style s);
0199     static const char *toStr(Hint::Style s);
0200     bool aliasingEnabled();
0201 
0202 private:
0203     bool parseConfigFile(const QString &filename);
0204     void readContents();
0205     void applySubPixelType();
0206     void applyHintStyle();
0207     void applyAntiAliasing();
0208     void setHinting(bool set);
0209     void applyHinting();
0210     void applyExcludeRange(bool pixel);
0211     QString getConfigFile();
0212 
0213 private:
0214     QStringList m_globalFiles;
0215 
0216     SubPixel m_subPixel;
0217     Exclude m_excludeRange, m_excludePixelRange;
0218     Hint m_hint;
0219     Hinting m_hinting;
0220     AntiAliasing m_antiAliasing;
0221     bool m_antiAliasingHasLocalConfig;
0222     bool m_subPixelHasLocalConfig;
0223     bool m_hintHasLocalConfig;
0224     QDomDocument m_doc;
0225     QString m_file;
0226     bool m_madeChanges;
0227     QDateTime m_time;
0228 };
0229 
0230 #endif