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