Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfont_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QFONT_P_H
5#define QFONT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of internal files. This header file may change from version to version
13// without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19#include "QtGui/qfont.h"
20#include "QtCore/qbasictimer.h"
21#include "QtCore/qmap.h"
22#include "QtCore/qhash.h"
23#include "QtCore/qobject.h"
24#include "QtCore/qstringlist.h"
25#include <QtGui/qfontdatabase.h>
26#include "private/qfixed_p.h"
27#include "private/qfontdatabase_p.h"
28
30
31// forwards
32class QFontCache;
33class QFontEngine;
34
35#define QFONT_WEIGHT_MIN 1
36#define QFONT_WEIGHT_MAX 1000
37
39{
55
56 // Convenience that returns the first family in the list or an empty string if the list
57 // is empty.
58 inline QString family() const
59 {
60 return families.value(0);
61 }
62
64 QString styleName;
65
68
71
72 // Note: Variable ordering matters to make sure no variable overlaps two 32-bit registers.
73 uint styleStrategy : 16;
74 uint stretch : 12; // 0-4000
75 uint style : 2;
77
78 uint styleHint : 8;
79 uint weight : 10; // 1-1000
80 uint fixedPitch : 1;
81 uint ignorePitch : 1;
82 uint fixedPitchComputed : 1; // for Mac OS X only
83 uint reserved : 11; // for future extensions
84
85 bool exactMatch(const QFontDef &other) const;
86 bool operator==(const QFontDef &other) const
87 {
88 return pixelSize == other.pixelSize
89 && weight == other.weight
90 && style == other.style
91 && stretch == other.stretch
92 && styleHint == other.styleHint
93 && styleStrategy == other.styleStrategy
94 && ignorePitch == other.ignorePitch && fixedPitch == other.fixedPitch
95 && families == other.families
96 && styleName == other.styleName
97 && hintingPreference == other.hintingPreference
98 && variableAxisValues == other.variableAxisValues
99 ;
100 }
101 inline bool operator<(const QFontDef &other) const
102 {
103 if (pixelSize != other.pixelSize) return pixelSize < other.pixelSize;
104 if (weight != other.weight) return weight < other.weight;
105 if (style != other.style) return style < other.style;
106 if (stretch != other.stretch) return stretch < other.stretch;
107 if (styleHint != other.styleHint) return styleHint < other.styleHint;
108 if (styleStrategy != other.styleStrategy) return styleStrategy < other.styleStrategy;
109 if (families != other.families) return families < other.families;
110 if (styleName != other.styleName)
111 return styleName < other.styleName;
113
114
115 if (ignorePitch != other.ignorePitch) return ignorePitch < other.ignorePitch;
116 if (fixedPitch != other.fixedPitch) return fixedPitch < other.fixedPitch;
117 if (variableAxisValues != other.variableAxisValues) {
118 if (variableAxisValues.size() != other.variableAxisValues.size())
119 return variableAxisValues.size() < other.variableAxisValues.size();
120
121 {
122 auto it = variableAxisValues.constBegin();
123 auto jt = other.variableAxisValues.constBegin();
124 for (; it != variableAxisValues.constEnd(); ++it, ++jt) {
125 if (it.key() != jt.key())
126 return jt.key() < it.key();
127 if (it.value() != jt.value())
128 return jt.value() < it.value();
129 }
130 }
131 }
132
133 return false;
134 }
135};
136
137inline size_t qHash(const QFontDef &fd, size_t seed = 0) noexcept
138{
139 return qHashMulti(seed,
140 qRound64(fd.pixelSize*10000), // use only 4 fractional digits
141 fd.weight,
142 fd.style,
143 fd.stretch,
144 fd.styleHint,
146 fd.ignorePitch,
147 fd.fixedPitch,
148 fd.families,
149 fd.styleName,
151 fd.variableAxisValues.keys(),
152 fd.variableAxisValues.values());
153}
154
168
169
170class Q_GUI_EXPORT QFontPrivate
171{
172public:
173 enum class EngineQueryOption {
174 Default = 0,
175 IgnoreSmallCapsEngine = 0x1,
176 };
177 Q_DECLARE_FLAGS(EngineQueryOptions, EngineQueryOption)
178
179 QFontPrivate();
180 QFontPrivate(const QFontPrivate &other);
181 QFontPrivate &operator=(const QFontPrivate &) = delete;
182 ~QFontPrivate();
183
184 QFontEngine *engineForScript(int script) const;
185 QFontEngine *engineForCharacter(char32_t c, EngineQueryOptions opt = {}) const;
186 void alterCharForCapitalization(QChar &c) const;
187
188 QAtomicInt ref;
189 QFontDef request;
190 mutable QFontEngineData *engineData;
191 int dpi;
192
193 uint underline : 1;
194 uint overline : 1;
195 uint strikeOut : 1;
196 uint kerning : 1;
197 uint capital : 3;
198 bool letterSpacingIsAbsolute : 1;
199
200 QFixed letterSpacing;
201 QFixed wordSpacing;
202 QMap<QFont::Tag, quint32> features;
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
217 void setFeature(QFont::Tag tag, quint32 value);
218 void unsetFeature(QFont::Tag tag);
219
220 void setVariableAxis(QFont::Tag tag, float value);
221 void unsetVariableAxis(QFont::Tag tag);
222 bool hasVariableAxis(QFont::Tag tag, float value) const;
223};
224Q_DECLARE_OPERATORS_FOR_FLAGS(QFontPrivate::EngineQueryOptions)
225
226
227class Q_GUI_EXPORT QFontCache : public QObject
228{
229public:
230 // note: these static functions work on a per-thread basis
231 static QFontCache *instance();
232 static void cleanup();
233
234 QFontCache();
235 ~QFontCache();
236
237 int id() const { return m_id; }
238
239 void clear();
240
241 struct Key {
242 Key() : script(0), multi(0) { }
243 Key(const QFontDef &d, uchar c, bool m = 0)
244 : def(d), script(c), multi(m) { }
245
246 QFontDef def;
247 uchar script;
248 uchar multi: 1;
249
250 inline bool operator<(const Key &other) const
251 {
252 if (script != other.script) return script < other.script;
253 if (multi != other.multi) return multi < other.multi;
254 if (multi && def.fallBackFamilies.size() != other.def.fallBackFamilies.size())
255 return def.fallBackFamilies.size() < other.def.fallBackFamilies.size();
256 return def < other.def;
257 }
258 inline bool operator==(const Key &other) const
259 {
260 return script == other.script
261 && multi == other.multi
262 && (!multi || def.fallBackFamilies == other.def.fallBackFamilies)
263 && def == other.def;
264 }
265 };
266
267 // QFontEngineData cache
268 typedef QMap<QFontDef, QFontEngineData*> EngineDataCache;
269 EngineDataCache engineDataCache;
270
271 QFontEngineData *findEngineData(const QFontDef &def) const;
272 void insertEngineData(const QFontDef &def, QFontEngineData *engineData);
273
274 // QFontEngine cache
275 struct Engine {
276 Engine() : data(nullptr), timestamp(0), hits(0) { }
277 Engine(QFontEngine *d) : data(d), timestamp(0), hits(0) { }
278
279 QFontEngine *data;
280 uint timestamp;
281 uint hits;
282 };
283
284 typedef QMultiMap<Key,Engine> EngineCache;
285 EngineCache engineCache;
286 QHash<QFontEngine *, int> engineCacheCount;
287
288 QFontEngine *findEngine(const Key &key);
289
290 void updateHitCountAndTimeStamp(Engine &value);
291 void insertEngine(const Key &key, QFontEngine *engine, bool insertMulti = false);
292
293private:
294 void increaseCost(uint cost);
295 void decreaseCost(uint cost);
296 void timerEvent(QTimerEvent *event) override;
297 void decreaseCache();
298
299 static const uint min_cost;
300 uint total_cost, max_cost;
301 uint current_timestamp;
302 bool fast;
303 const bool autoClean;
304 QBasicTimer timer;
305 const int m_id;
306};
307
308Q_GUI_EXPORT QPoint qt_defaultDpis();
309Q_GUI_EXPORT int qt_defaultDpiX();
310Q_GUI_EXPORT int qt_defaultDpiY();
311Q_GUI_EXPORT int qt_defaultDpi();
312
313Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight);
314Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight);
315
316QT_END_NAMESPACE
317
318#endif // QFONT_P_H
\threadsafe \inmodule QtGui
QAtomicInt ref
Definition qfont_p.h:161
QFontEngine * engines[QFontDatabasePrivate::ScriptCount]
Definition qfont_p.h:164
const int fontCacheId
Definition qfont_p.h:162
friend class QFontEngine
Definition qpainter.h:433
\inmodule QtCore
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight)
Definition qfont.cpp:205
static void set_font_bits(int version, quint8 bits, QFontPrivate *f)
Definition qfont.cpp:2099
QRecursiveMutex * qt_fontdatabase_mutex()
static int convertWeights(int weight, bool inverted)
Definition qfont.cpp:150
static QStringList splitIntoFamilies(const QString &family)
Definition qfont.cpp:179
#define FC_DEBUG
Definition qfont.cpp:41
Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight)
Definition qfont.cpp:199
static constexpr auto fast_timeout
Definition qfont.cpp:3418
static std::optional< std::pair< QFont::Tag, quint32 > > fontFeatureFromString(QStringView view)
Definition qfont.cpp:2203
QHash< QString, QStringList > QFontSubst
Definition qfont.cpp:1935
#define QFONT_DEBUG_SKIP_DEFAULT(prop)
#define QFONTCACHE_MIN_COST
Definition qfont.cpp:3423
#define QT_FONT_ENGINE_FROM_DATA(data, script)
Definition qfont.cpp:243
Q_GUI_EXPORT int qt_defaultDpiY()
Definition qfont.cpp:139
static constexpr auto slow_timeout
Definition qfont.cpp:3419
Q_GUI_EXPORT int qt_defaultDpiX()
Definition qfont.cpp:134
static quint8 get_extended_font_bits(const QFontPrivate *f)
Definition qfont.cpp:2084
Q_GUI_EXPORT int qt_defaultDpi()
Definition qfont.cpp:144
#define QFONTCACHE_DECREASE_TRIGGER_LIMIT
Definition qfont.cpp:47
QStringList qt_fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QFontDatabasePrivate::ExtendedScript script)
static std::optional< std::pair< QFont::Tag, float > > variableAxisFromString(QStringView view)
Definition qfont.cpp:2221
static quint8 get_font_bits(int version, const QFontPrivate *f)
Definition qfont.cpp:2059
static void set_extended_font_bits(quint8 bits, QFontPrivate *f)
Definition qfont.cpp:2114
#define QFONT_WEIGHT_MIN
Definition qfont_p.h:35
size_t qHash(const QFontDef &fd, size_t seed=0) noexcept
Definition qfont_p.h:137
#define QFONT_WEIGHT_MAX
Definition qfont_p.h:36
Q_CONSTINIT Q_GUI_EXPORT bool qt_is_tty_app
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192
uint hintingPreference
Definition qfont_p.h:76
uint reserved
Definition qfont_p.h:83
QFontDef()
Definition qfont_p.h:40
uint stretch
Definition qfont_p.h:74
uint style
Definition qfont_p.h:75
uint styleStrategy
Definition qfont_p.h:73
QStringList fallBackFamilies
Definition qfont_p.h:66
uint fixedPitch
Definition qfont_p.h:80
bool operator<(const QFontDef &other) const
Definition qfont_p.h:101
qreal pixelSize
Definition qfont_p.h:70
QMap< QFont::Tag, float > variableAxisValues
Definition qfont_p.h:67
uint ignorePitch
Definition qfont_p.h:81
bool operator==(const QFontDef &other) const
Definition qfont_p.h:86
uint weight
Definition qfont_p.h:79
QStringList families
Definition qfont_p.h:63
QString family() const
Definition qfont_p.h:58
QString styleName
Definition qfont_p.h:64
uint fixedPitchComputed
Definition qfont_p.h:82
bool exactMatch(const QFontDef &other) const
Definition qfont.cpp:52
uint styleHint
Definition qfont_p.h:78
qreal pointSize
Definition qfont_p.h:69