1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QWINDOWSFONTDATABASE_H
41 #define QWINDOWSFONTDATABASE_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API. It exists purely as an
48 // implementation detail. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <qpa/qplatformfontdatabase.h>
55 #include <QtCore/QSharedPointer>
56 #include <QtCore/QLoggingCategory>
57 #include <QtCore/qt_windows.h>
58 
59 #if !defined(QT_NO_DIRECTWRITE)
60     struct IDWriteFactory;
61     struct IDWriteGdiInterop;
62 #endif
63 
64 QT_BEGIN_NAMESPACE
65 
Q_DECLARE_LOGGING_CATEGORY(lcQpaFonts)66 Q_DECLARE_LOGGING_CATEGORY(lcQpaFonts)
67 
68 class QWindowsFontEngineData
69 {
70     Q_DISABLE_COPY_MOVE(QWindowsFontEngineData)
71 public:
72     QWindowsFontEngineData();
73     ~QWindowsFontEngineData();
74 
75     uint pow_gamma[256];
76 
77     bool clearTypeEnabled = false;
78     qreal fontSmoothingGamma;
79     HDC hdc = 0;
80 #if !defined(QT_NO_DIRECTWRITE)
81     IDWriteFactory *directWriteFactory = nullptr;
82     IDWriteGdiInterop *directWriteGdiInterop = nullptr;
83 #endif
84 };
85 
86 class QWindowsFontDatabase : public QPlatformFontDatabase
87 {
88     Q_DISABLE_COPY_MOVE(QWindowsFontDatabase)
89 public:
90     enum FontOptions {
91         // Relevant bits from QWindowsIntegration::Options
92         DontUseDirectWriteFonts = 0x40,
93         DontUseColorFonts = 0x80
94     };
95 
96     QWindowsFontDatabase();
97     ~QWindowsFontDatabase() override;
98 
99     void ensureFamilyPopulated(const QString &familyName);
100 
101     void populateFontDatabase() override;
102     bool populateFamilyAliases(const QString &missingFamily) override;
103     void populateFamily(const QString &familyName) override;
104     QFontEngine *fontEngine(const QFontDef &fontDef, void *handle) override;
105     QFontEngine *fontEngine(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference) override;
106     QStringList fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const override;
107     QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName) override;
108     void releaseHandle(void *handle) override;
109     QString fontDir() const override;
110 
defaultFont()111     QFont defaultFont() const  override { return systemDefaultFont(); }
112     bool fontsAlwaysScalable() const override;
113     void derefUniqueFont(const QString &uniqueFont);
114     void refUniqueFont(const QString &uniqueFont);
115     bool isPrivateFontFamily(const QString &family) const override;
116 
117     static QFont systemDefaultFont();
118 
119     static QFontEngine *createEngine(const QFontDef &request, const QString &faceName,
120                                      int dpi,
121                                      const QSharedPointer<QWindowsFontEngineData> &data);
122 
123     static HFONT systemFont();
124     static QFont LOGFONT_to_QFont(const LOGFONT& lf, int verticalDPI = 0);
125 
126     static qreal fontSmoothingGamma();
127     static LOGFONT fontDefToLOGFONT(const QFontDef &fontDef, const QString &faceName);
128 
129     static QStringList extraTryFontsForFamily(const QString &family);
130     static QString familyForStyleHint(QFont::StyleHint styleHint);
131 
132     static int defaultVerticalDPI();
133     static void setDefaultVerticalDPI(int d);
134 
135     static void setFontOptions(unsigned options);
136     static unsigned fontOptions();
137 
138 private:
139     void removeApplicationFonts();
140     void addDefaultEUDCFont();
141 
142     struct WinApplicationFont {
143         HANDLE handle;
144         QString fileName;
145     };
146 
147     QList<WinApplicationFont> m_applicationFonts;
148 
149     struct UniqueFontData {
150         HANDLE handle;
151         QAtomicInt refCount;
152     };
153 
154     QMap<QString, UniqueFontData> m_uniqueFontData;
155 
156     static unsigned m_fontOptions;
157     QStringList m_eudcFonts;
158     bool m_hasPopulatedAliases = false;
159 };
160 
161 #ifndef QT_NO_DEBUG_STREAM
162 QDebug operator<<(QDebug, const QFontDef &def);
163 #endif
164 
qt_getUShort(const unsigned char * p)165 inline quint16 qt_getUShort(const unsigned char *p)
166 {
167     quint16 val;
168     val = *p++ << 8;
169     val |= *p;
170 
171     return val;
172 }
173 
174 struct QFontNames
175 {
176     QString name;   // e.g. "DejaVu Sans Condensed"
177     QString style;  // e.g. "Italic"
178     QString preferredName;  // e.g. "DejaVu Sans"
179     QString preferredStyle; // e.g. "Condensed Italic"
180 };
181 
182 struct QFontValues
183 {
184     quint16 weight = 0;
185     bool isItalic = false;
186     bool isOverstruck = false;
187     bool isUnderlined = false;
188 };
189 
190 bool qt_localizedName(const QString &name);
191 QString qt_getEnglishName(const QString &familyName, bool includeStyle = false);
192 QFontNames qt_getCanonicalFontNames(const LOGFONT &lf);
193 
194 QT_END_NAMESPACE
195 
196 #endif // QWINDOWSFONTDATABASE_H
197