File indexing completed on 2024-12-22 05:15:55

0001 #pragma once
0002 
0003 /*
0004  * SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include <QColor>
0009 #include <QFont>
0010 #include <QList>
0011 #include <QRect>
0012 #include <fontconfig/fontconfig.h>
0013 
0014 #include "kfontinst_export.h"
0015 
0016 // Enable the following to use locale aware family name - if font supports this.
0017 // #define KFI_USE_TRANSLATED_FAMILY_NAME
0018 
0019 class KConfig;
0020 
0021 typedef struct _XftFont XftFont;
0022 typedef struct _XftDraw XftDraw;
0023 typedef struct _XftColor XftColor;
0024 
0025 namespace KFI
0026 {
0027 class KFONTINST_EXPORT CFcEngine
0028 {
0029 public:
0030     class Xft;
0031 
0032     struct TRange {
0033         TRange(quint32 f = 0, quint32 t = 0)
0034             : from(f)
0035             , to(t)
0036         {
0037         }
0038         bool null() const
0039         {
0040             return 0 == from && 0 == to;
0041         }
0042 
0043         quint32 from, to;
0044     };
0045 
0046     struct TChar : public QRect {
0047         TChar(const QRect &r = QRect(), quint32 u = 0)
0048             : QRect(r)
0049             , ucs4(u)
0050         {
0051         }
0052 
0053         quint32 ucs4;
0054     };
0055 
0056     static CFcEngine *instance();
0057 
0058     CFcEngine(bool init = true);
0059     virtual ~CFcEngine();
0060 
0061     void readConfig(KConfig &cfg);
0062     void writeConfig(KConfig &cfg);
0063     static void setDirty()
0064     {
0065         theirFcDirty = true;
0066     }
0067     QImage drawPreview(const QString &name, quint32 style, int faceNo, const QColor &txt, const QColor &bgnd, int h);
0068     QImage draw(const QString &name, quint32 style, int faceNo, const QColor &txt, const QColor &bgnd, int fSize, const QString &text);
0069     QImage draw(const QString &name,
0070                 quint32 style,
0071                 int faceNo,
0072                 const QColor &txt,
0073                 const QColor &bgnd,
0074                 int w,
0075                 int h,
0076                 bool thumb,
0077                 const QList<TRange> &range = QList<TRange>(),
0078                 QList<TChar> *chars = nullptr);
0079     int getNumIndexes()
0080     {
0081         return m_indexCount;
0082     } // Only valid after draw has been called!
0083     static QFont getQFont(const QString &family, quint32 style, int size);
0084     const QList<int> &sizes() const
0085     {
0086         return m_sizes;
0087     }
0088     bool atMin() const
0089     {
0090         return 0 == m_sizes.size() || 0 == m_alphaSizeIndex;
0091     }
0092     bool atMax() const
0093     {
0094         return 0 == m_sizes.size() || m_sizes.size() - 1 == m_alphaSizeIndex;
0095     }
0096     void zoomIn()
0097     {
0098         if (!atMax())
0099             m_alphaSizeIndex++;
0100     }
0101     void zoomOut()
0102     {
0103         if (!atMin())
0104             m_alphaSizeIndex--;
0105     }
0106     int alphaSize() const
0107     {
0108         return m_sizes[m_alphaSizeIndex];
0109     }
0110     quint32 styleVal()
0111     {
0112         return m_style;
0113     }
0114     const QString &descriptiveName() const
0115     {
0116         return m_descriptiveName;
0117     }
0118 
0119     const QString &getPreviewString()
0120     {
0121         return m_previewString;
0122     }
0123     static QString getDefaultPreviewString();
0124     void setPreviewString(const QString &str)
0125     {
0126         m_previewString = str.isEmpty() ? getDefaultPreviewString() : str;
0127     }
0128     static QString getUppercaseLetters();
0129     static QString getLowercaseLetters();
0130     static QString getPunctuation();
0131 
0132     static const int constScalableSizes[];
0133     static const int constDefaultAlphaSize;
0134 
0135 private:
0136     bool parse(const QString &name, quint32 style, int faceNo);
0137     XftFont *queryFont();
0138     XftFont *getFont(int size);
0139     bool isCorrect(XftFont *f, bool checkFamily);
0140     void getSizes();
0141     void drawName(int x, int &y, int h);
0142     void addFontFile(const QString &file);
0143     void reinit();
0144     Xft *xft();
0145 
0146 private:
0147     bool m_installed;
0148     QString m_name, m_descriptiveName;
0149     quint32 m_style;
0150     int m_index, m_indexCount, m_alphaSizeIndex;
0151     QList<int> m_sizes;
0152     FcBool m_scalable;
0153     QStringList m_addedFiles;
0154     QString m_previewString;
0155     static bool theirFcDirty;
0156     Xft *m_xft;
0157 };
0158 
0159 }