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.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_H
5#define QFONT_H
6
7#include <QtCore/qcompare.h>
8#include <QtCore/qendian.h>
9#include <QtCore/qshareddata.h>
10#include <QtGui/qtguiglobal.h>
11#include <QtGui/qwindowdefs.h>
12#include <QtCore/qstring.h>
13
14
16
17
18class QFontPrivate; /* don't touch */
20class QVariant;
21
22class Q_GUI_EXPORT QFont
23{
24 Q_GADGET
25public:
26 enum StyleHint {
27 Helvetica, SansSerif = Helvetica,
28 Times, Serif = Times,
29 Courier, TypeWriter = Courier,
30 OldEnglish, Decorative = OldEnglish,
31 System,
32 AnyStyle,
33 Cursive,
34 Monospace,
35 Fantasy
36 };
37 Q_ENUM(StyleHint)
38
39 enum StyleStrategy {
40 PreferDefault = 0x0001,
41 PreferBitmap = 0x0002,
42 PreferDevice = 0x0004,
43 PreferOutline = 0x0008,
44 ForceOutline = 0x0010,
45 PreferMatch = 0x0020,
46 PreferQuality = 0x0040,
47 PreferAntialias = 0x0080,
48 NoAntialias = 0x0100,
49 NoSubpixelAntialias = 0x0800,
50 PreferNoShaping = 0x1000,
51 ContextFontMerging = 0x2000,
52 PreferTypoLineMetrics = 0x4000,
53 NoFontMerging = 0x8000
54 };
55 Q_ENUM(StyleStrategy)
56
57 enum HintingPreference {
58 PreferDefaultHinting = 0,
59 PreferNoHinting = 1,
60 PreferVerticalHinting = 2,
61 PreferFullHinting = 3
62 };
63 Q_ENUM(HintingPreference)
64
65 enum Weight {
66 Thin = 100,
67 ExtraLight = 200,
68 Light = 300,
69 Normal = 400,
70 Medium = 500,
71 DemiBold = 600,
72 Bold = 700,
73 ExtraBold = 800,
74 Black = 900
75 };
76 Q_ENUM(Weight)
77
78 enum Style {
79 StyleNormal,
80 StyleItalic,
81 StyleOblique
82 };
83 Q_ENUM(Style)
84
85 enum Stretch {
86 AnyStretch = 0,
87 UltraCondensed = 50,
88 ExtraCondensed = 62,
89 Condensed = 75,
90 SemiCondensed = 87,
91 Unstretched = 100,
92 SemiExpanded = 112,
93 Expanded = 125,
94 ExtraExpanded = 150,
95 UltraExpanded = 200
96 };
97 Q_ENUM(Stretch)
98
99 enum Capitalization {
100 MixedCase,
101 AllUppercase,
102 AllLowercase,
103 SmallCaps,
104 Capitalize
105 };
106 Q_ENUM(Capitalization)
107
108 enum SpacingType {
109 PercentageSpacing,
110 AbsoluteSpacing
111 };
112 Q_ENUM(SpacingType)
113
114 enum ResolveProperties {
115 NoPropertiesResolved = 0x0000,
116 FamilyResolved = 0x0001,
117 SizeResolved = 0x0002,
118 StyleHintResolved = 0x0004,
119 StyleStrategyResolved = 0x0008,
120 WeightResolved = 0x0010,
121 StyleResolved = 0x0020,
122 UnderlineResolved = 0x0040,
123 OverlineResolved = 0x0080,
124 StrikeOutResolved = 0x0100,
125 FixedPitchResolved = 0x0200,
126 StretchResolved = 0x0400,
127 KerningResolved = 0x0800,
128 CapitalizationResolved = 0x1000,
129 LetterSpacingResolved = 0x2000,
130 WordSpacingResolved = 0x4000,
131 HintingPreferenceResolved = 0x8000,
132 StyleNameResolved = 0x10000,
133 FamiliesResolved = 0x20000,
134 FeaturesResolved = 0x40000,
135 VariableAxesResolved = 0x80000,
136 AllPropertiesResolved = 0xfffff
137 };
138 Q_ENUM(ResolveProperties)
139
140 QFont();
141
142 QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
143 explicit QFont(const QStringList &families, int pointSize = -1, int weight = -1, bool italic = false);
144 QFont(const QFont &font, const QPaintDevice *pd);
145 QFont(const QFont &font);
146 QFont(QFont &&) = default;
147 ~QFont();
148
149 void swap(QFont &other) noexcept
150 { d.swap(other.d); std::swap(resolve_mask, other.resolve_mask); }
151
152 QString family() const;
153 void setFamily(const QString &);
154
155 QStringList families() const;
156 void setFamilies(const QStringList &);
157
158 QString styleName() const;
159 void setStyleName(const QString &);
160
161 int pointSize() const;
162 void setPointSize(int);
163 qreal pointSizeF() const;
164 void setPointSizeF(qreal);
165
166 int pixelSize() const;
167 void setPixelSize(int);
168
169 Weight weight() const;
170 void setWeight(Weight weight);
171
172 inline bool bold() const;
173 inline void setBold(bool);
174
175 void setStyle(Style style);
176 Style style() const;
177
178 inline bool italic() const;
179 inline void setItalic(bool b);
180
181 bool underline() const;
182 void setUnderline(bool);
183
184 bool overline() const;
185 void setOverline(bool);
186
187 bool strikeOut() const;
188 void setStrikeOut(bool);
189
190 bool fixedPitch() const;
191 void setFixedPitch(bool);
192
193 bool kerning() const;
194 void setKerning(bool);
195
196 StyleHint styleHint() const;
197 StyleStrategy styleStrategy() const;
198 void setStyleHint(StyleHint, StyleStrategy = PreferDefault);
199 void setStyleStrategy(StyleStrategy s);
200
201 int stretch() const;
202 void setStretch(int);
203
204 qreal letterSpacing() const;
205 SpacingType letterSpacingType() const;
206 void setLetterSpacing(SpacingType type, qreal spacing);
207
208 qreal wordSpacing() const;
209 void setWordSpacing(qreal spacing);
210
211 void setCapitalization(Capitalization);
212 Capitalization capitalization() const;
213
214 void setHintingPreference(HintingPreference hintingPreference);
215 HintingPreference hintingPreference() const;
216
217 struct Tag
218 {
219 constexpr Tag() = default;
220
221 template <size_t N>
222 constexpr Q_IMPLICIT Tag(const char (&str)[N]) noexcept
223 : m_value((quint32(str[0]) << 24) | (quint32(str[1]) << 16)
224 | (quint32(str[2]) << 8) | quint32(str[3]))
225 {
226 static_assert(N == 5, "The tag name must be exactly 4 characters long!");
227 }
228
229 constexpr bool isValid() const noexcept { return m_value != 0; }
230 constexpr quint32 value() const noexcept { return m_value; }
231
232 QByteArray toString() const
233 {
234 const char data[] = {
235 char((m_value & 0xff000000) >> 24),
236 char((m_value & 0x00ff0000) >> 16),
237 char((m_value & 0x0000ff00) >> 8),
238 char((m_value & 0x000000ff)) };
239 return QByteArray(data, sizeof(data));
240 }
241
242 static constexpr std::optional<Tag> fromValue(quint32 value) noexcept
243 {
244 Tag maybeTag;
245 maybeTag.m_value = value;
246 return maybeTag.isValid() ? std::optional<Tag>(maybeTag) : std::nullopt;
247 }
248 Q_GUI_EXPORT static std::optional<Tag> fromString(QAnyStringView view) noexcept;
249
250#ifndef QT_NO_DATASTREAM
251 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, Tag);
252 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, Tag &);
253#endif
254
255#ifndef QT_NO_DEBUG_STREAM
256 friend Q_GUI_EXPORT QDebug operator<<(QDebug debug, Tag tag);
257#endif
258
259 friend constexpr size_t qHash(Tag key, size_t seed = 0) noexcept
260 { return qHash(key.value(), seed); }
261
262 private:
263 friend constexpr bool comparesEqual(const Tag &lhs, const Tag &rhs) noexcept
264 { return lhs.m_value == rhs.m_value; }
265 friend constexpr Qt::strong_ordering compareThreeWay(const Tag &lhs, const Tag &rhs) noexcept
266 { return Qt::compareThreeWay(lhs.m_value, rhs.m_value); }
267 Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QFont::Tag)
268
269 quint32 m_value = 0;
270 };
271
272 void setFeature(Tag tag, quint32 value);
273 void unsetFeature(Tag tag);
274 quint32 featureValue(Tag tag) const;
275 bool isFeatureSet(Tag tag) const;
276 QList<Tag> featureTags() const;
277 void clearFeatures();
278
279 void setVariableAxis(Tag tag, float value);
280 void unsetVariableAxis(Tag tag);
281 bool isVariableAxisSet(Tag tag) const;
282 float variableAxisValue(Tag tag) const;
283 void clearVariableAxes();
284 QList<Tag> variableAxisTags() const;
285
286 // dupicated from QFontInfo
287 bool exactMatch() const;
288
289 QFont &operator=(const QFont &);
290 bool operator==(const QFont &) const;
291 bool operator!=(const QFont &) const;
292 bool operator<(const QFont &) const;
293 operator QVariant() const;
294 bool isCopyOf(const QFont &) const;
295 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFont)
296
297 QString key() const;
298
299 QString toString() const;
300 bool fromString(const QString &);
301
302 static QString substitute(const QString &);
303 static QStringList substitutes(const QString &);
304 static QStringList substitutions();
305 static void insertSubstitution(const QString&, const QString &);
306 static void insertSubstitutions(const QString&, const QStringList &);
307 static void removeSubstitutions(const QString &);
308 static void initialize();
309 static void cleanup();
310 static void cacheStatistics();
311
312 QString defaultFamily() const;
313
314 QFont resolve(const QFont &) const;
315 inline uint resolveMask() const { return resolve_mask; }
316 inline void setResolveMask(uint mask) { resolve_mask = mask; }
317
318#if QT_DEPRECATED_SINCE(6, 0)
319 QT_DEPRECATED_VERSION_X_6_0("Use setWeight() instead") void setLegacyWeight(int legacyWeight);
320 QT_DEPRECATED_VERSION_X_6_0("Use weight() instead") int legacyWeight() const;
321#endif
322
323private:
324 explicit QFont(QFontPrivate *);
325
326 void detach();
327
328
329 friend class QFontPrivate;
330 friend class QFontDialogPrivate;
331 friend class QFontMetrics;
332 friend class QFontMetricsF;
333 friend class QFontInfo;
334 friend class QPainter;
335 friend class QPainterPrivate;
336 friend class QApplication;
337 friend class QWidget;
338 friend class QWidgetPrivate;
339 friend class QTextLayout;
340 friend class QTextEngine;
341 friend class QStackTextEngine;
342 friend class QTextLine;
343 friend struct QScriptLine;
344 friend class QOpenGLContext;
345 friend class QWin32PaintEngine;
346 friend class QAlphaPaintEngine;
347 friend class QPainterPath;
348 friend class QTextItemInt;
349 friend class QPicturePaintEngine;
350 friend class QPainterReplayer;
351 friend class QPaintBufferEngine;
352 friend class QCommandLinkButtonPrivate;
353 friend class QFontEngine;
354
355#ifndef QT_NO_DATASTREAM
356 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QFont &);
357 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QFont &);
358#endif
359
360#ifndef QT_NO_DEBUG_STREAM
361 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QFont &);
362#endif
363
364 QExplicitlySharedDataPointer<QFontPrivate> d;
365 uint resolve_mask;
366};
367
369
370Q_GUI_EXPORT size_t qHash(const QFont &font, size_t seed = 0) noexcept;
371
372inline bool QFont::bold() const
373{ return weight() > Medium; }
374
375
376inline void QFont::setBold(bool enable)
377{ setWeight(enable ? Bold : Normal); }
378
379inline bool QFont::italic() const
380{
381 return (style() != StyleNormal);
382}
383
384inline void QFont::setItalic(bool b) {
385 setStyle(b ? StyleItalic : StyleNormal);
386}
387
388
389/*****************************************************************************
390 QFont stream functions
391 *****************************************************************************/
392
393#ifndef QT_NO_DATASTREAM
394Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QFont &);
395Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QFont &);
396#endif
397
398#ifndef QT_NO_DEBUG_STREAM
399Q_GUI_EXPORT QDebug operator<<(QDebug, const QFont &);
400#endif
401
402QT_END_NAMESPACE
403
404#endif // QFONT_H
The QClipboard class provides access to the window system clipboard.
Definition qclipboard.h:21
\inmodule QtCore\reentrant
Definition qdatastream.h:50
\reentrant
Definition qfont.h:23
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
QInputDeviceManager acts as a communication hub between QtGui and the input handlers.
The QPalette class contains color groups for each widget state.
Definition qpalette.h:20
The QPlatformIntegration class is the entry for WindowSystem specific functionality.
The QPlatformTheme class allows customizing the UI based on themes.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
The QStyleHints class contains platform specific hints and settings. \inmodule QtGui.
Definition qstylehints.h:18
\inmodule QtGui
Definition qwindow.h:64
Combined button and popup list for selecting options.
#define qApp
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
static bool needsWindowBlockedEvent(const QWindow *w)
Q_CORE_EXPORT void qt_call_post_routines()
static void init_plugins(const QList< QByteArray > &pluginList)
static void initFontUnlocked()
static void clearFontUnlocked()
void qRegisterGuiVariant()
static Q_CONSTINIT unsigned applicationResourceFlags
static Q_CONSTINIT int touchDoubleTapDistance
static QWindowGeometrySpecification windowGeometrySpecification
static bool qt_detectRTLLanguage()
Q_CONSTINIT Q_GUI_EXPORT bool qt_is_tty_app
static Q_CONSTINIT bool force_reverse
static Q_CONSTINIT int mouseDoubleClickDistance
#define Q_WINDOW_GEOMETRY_SPECIFICATION_INITIALIZER
static void init_platform(const QString &pluginNamesWithArguments, const QString &platformPluginPath, const QString &platformThemeName, int &argc, char **argv)
static void initThemeHints()
static int nextGeometryToken(const QByteArray &a, int &pos, char *op)
#define CHECK_QAPP_INSTANCE(...)
ApplicationResourceFlags
@ ApplicationFontExplicitlySet
static void updateBlockedStatusRecursion(QWindow *window, bool shouldBeBlocked)
#define qGuiApp
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)
void applyTo(QWindow *window) const
static QWindowGeometrySpecification fromArgument(const QByteArray &a)