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

source code of qtbase/src/gui/text/qfont_p.h