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 QtGui module 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 QFONT_P_H
41#define QFONT_P_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 for the convenience
48// of internal files. This header file may change from version to version
49// without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtGui/private/qtguiglobal_p.h>
55#include "QtGui/qfont.h"
56#include "QtCore/qmap.h"
57#include "QtCore/qhash.h"
58#include "QtCore/qobject.h"
59#include "QtCore/qstringlist.h"
60#include <QtGui/qfontdatabase.h>
61#include "private/qfixed_p.h"
62
63QT_BEGIN_NAMESPACE
64
65// forwards
66class QFontCache;
67class QFontEngine;
68
69struct QFontDef
70{
71 inline QFontDef()
72 : pointSize(-1.0), pixelSize(-1),
73 styleStrategy(QFont::PreferDefault), styleHint(QFont::AnyStyle),
74 weight(50), fixedPitch(false), style(QFont::StyleNormal), stretch(QFont::AnyStretch),
75 hintingPreference(QFont::PreferDefaultHinting), ignorePitch(true),
76 fixedPitchComputed(0), reserved(0)
77 {
78 }
79
80 QString family;
81 QStringList families;
82 QString styleName;
83
84 QStringList fallBackFamilies;
85
86 qreal pointSize;
87 qreal pixelSize;
88
89 uint styleStrategy : 16;
90 uint styleHint : 8;
91
92 uint weight : 7; // 0-99
93 uint fixedPitch : 1;
94 uint style : 2;
95 uint stretch : 12; // 0-4000
96
97 uint hintingPreference : 2;
98 uint ignorePitch : 1;
99 uint fixedPitchComputed : 1; // for Mac OS X only
100 int reserved : 14; // for future extensions
101
102 bool exactMatch(const QFontDef &other) const;
103 bool operator==(const QFontDef &other) const
104 {
105 return pixelSize == other.pixelSize
106 && weight == other.weight
107 && style == other.style
108 && stretch == other.stretch
109 && styleHint == other.styleHint
110 && styleStrategy == other.styleStrategy
111 && ignorePitch == other.ignorePitch && fixedPitch == other.fixedPitch
112 && family == other.family
113 && families == other.families
114 && styleName == other.styleName
115 && hintingPreference == other.hintingPreference
116 ;
117 }
118 inline bool operator<(const QFontDef &other) const
119 {
120 if (pixelSize != other.pixelSize) return pixelSize < other.pixelSize;
121 if (weight != other.weight) return weight < other.weight;
122 if (style != other.style) return style < other.style;
123 if (stretch != other.stretch) return stretch < other.stretch;
124 if (styleHint != other.styleHint) return styleHint < other.styleHint;
125 if (styleStrategy != other.styleStrategy) return styleStrategy < other.styleStrategy;
126 if (family != other.family) return family < other.family;
127 if (families != other.families) return families < other.families;
128 if (styleName != other.styleName)
129 return styleName < other.styleName;
130 if (hintingPreference != other.hintingPreference) return hintingPreference < other.hintingPreference;
131
132
133 if (ignorePitch != other.ignorePitch) return ignorePitch < other.ignorePitch;
134 if (fixedPitch != other.fixedPitch) return fixedPitch < other.fixedPitch;
135 return false;
136 }
137};
138
139inline uint qHash(const QFontDef &fd, uint seed = 0) noexcept
140{
141 QtPrivate::QHashCombine hash;
142 seed = hash(seed, qRound64(fd.pixelSize*10000)); // use only 4 fractional digits
143 seed = hash(seed, fd.weight);
144 seed = hash(seed, fd.style);
145 seed = hash(seed, fd.stretch);
146 seed = hash(seed, fd.styleHint);
147 seed = hash(seed, fd.styleStrategy);
148 seed = hash(seed, fd.ignorePitch);
149 seed = hash(seed, fd.fixedPitch);
150 seed = hash(seed, fd.family);
151 seed = hash(seed, fd.families);
152 seed = hash(seed, fd.styleName);
153 seed = hash(seed, fd.hintingPreference);
154 return seed;
155}
156
157class QFontEngineData
158{
159public:
160 QFontEngineData();
161 ~QFontEngineData();
162
163 QAtomicInt ref;
164 const int fontCacheId;
165
166 QFontEngine *engines[QChar::ScriptCount];
167
168private:
169 Q_DISABLE_COPY_MOVE(QFontEngineData)
170};
171
172
173class Q_GUI_EXPORT QFontPrivate
174{
175public:
176
177 QFontPrivate();
178 QFontPrivate(const QFontPrivate &other);
179 ~QFontPrivate();
180
181 QFontEngine *engineForScript(int script) const;
182 void alterCharForCapitalization(QChar &c) const;
183
184 QAtomicInt ref;
185 QFontDef request;
186 mutable QFontEngineData *engineData;
187 int dpi;
188 int screen;
189
190 uint underline : 1;
191 uint overline : 1;
192 uint strikeOut : 1;
193 uint kerning : 1;
194 uint capital : 3;
195 bool letterSpacingIsAbsolute : 1;
196
197 QFixed letterSpacing;
198 QFixed wordSpacing;
199
200 mutable QFontPrivate *scFont;
201 QFont smallCapsFont() const { return QFont(smallCapsFontPrivate()); }
202 QFontPrivate *smallCapsFontPrivate() const;
203
204 static QFontPrivate *get(const QFont &font)
205 {
206 return font.d.data();
207 }
208
209 void resolve(uint mask, const QFontPrivate *other);
210
211 static void detachButKeepEngineData(QFont *font);
212
213private:
214 QFontPrivate &operator=(const QFontPrivate &) { return *this; }
215};
216
217
218class Q_GUI_EXPORT QFontCache : public QObject
219{
220public:
221 // note: these static functions work on a per-thread basis
222 static QFontCache *instance();
223 static void cleanup();
224
225 QFontCache();
226 ~QFontCache();
227
228 int id() const { return m_id; }
229
230 void clear();
231
232 struct Key {
233 Key() : script(0), multi(0), screen(0) { }
234 Key(const QFontDef &d, uchar c, bool m = 0, uchar s = 0)
235 : def(d), script(c), multi(m), screen(s) { }
236
237 QFontDef def;
238 uchar script;
239 uchar multi: 1;
240 uchar screen: 7;
241
242 inline bool operator<(const Key &other) const
243 {
244 if (script != other.script) return script < other.script;
245 if (screen != other.screen) return screen < other.screen;
246 if (multi != other.multi) return multi < other.multi;
247 if (multi && def.fallBackFamilies.size() != other.def.fallBackFamilies.size())
248 return def.fallBackFamilies.size() < other.def.fallBackFamilies.size();
249 return def < other.def;
250 }
251 inline bool operator==(const Key &other) const
252 {
253 return script == other.script
254 && screen == other.screen
255 && multi == other.multi
256 && (!multi || def.fallBackFamilies == other.def.fallBackFamilies)
257 && def == other.def;
258 }
259 };
260
261 // QFontEngineData cache
262 typedef QMap<QFontDef, QFontEngineData*> EngineDataCache;
263 EngineDataCache engineDataCache;
264
265 QFontEngineData *findEngineData(const QFontDef &def) const;
266 void insertEngineData(const QFontDef &def, QFontEngineData *engineData);
267
268 // QFontEngine cache
269 struct Engine {
270 Engine() : data(nullptr), timestamp(0), hits(0) { }
271 Engine(QFontEngine *d) : data(d), timestamp(0), hits(0) { }
272
273 QFontEngine *data;
274 uint timestamp;
275 uint hits;
276 };
277
278 typedef QMultiMap<Key,Engine> EngineCache;
279 EngineCache engineCache;
280 QHash<QFontEngine *, int> engineCacheCount;
281
282 QFontEngine *findEngine(const Key &key);
283
284 void updateHitCountAndTimeStamp(Engine &value);
285 void insertEngine(const Key &key, QFontEngine *engine, bool insertMulti = false);
286
287private:
288 void increaseCost(uint cost);
289 void decreaseCost(uint cost);
290 void timerEvent(QTimerEvent *event) override;
291 void decreaseCache();
292
293 static const uint min_cost;
294 uint total_cost, max_cost;
295 uint current_timestamp;
296 bool fast;
297 int timer_id;
298 const int m_id;
299};
300
301Q_GUI_EXPORT int qt_defaultDpiX();
302Q_GUI_EXPORT int qt_defaultDpiY();
303Q_GUI_EXPORT int qt_defaultDpi();
304
305QT_END_NAMESPACE
306
307#endif // QFONT_P_H
308