Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 */
19class QVariant;
20
21class Q_GUI_EXPORT QFont
22{
24public:
25 enum StyleHint {
26 Helvetica, SansSerif = Helvetica,
27 Times, Serif = Times,
28 Courier, TypeWriter = Courier,
29 OldEnglish, Decorative = OldEnglish,
34 Fantasy
35 };
36 Q_ENUM(StyleHint)
37
39 PreferDefault = 0x0001,
40 PreferBitmap = 0x0002,
41 PreferDevice = 0x0004,
42 PreferOutline = 0x0008,
43 ForceOutline = 0x0010,
44 PreferMatch = 0x0020,
45 PreferQuality = 0x0040,
46 PreferAntialias = 0x0080,
47 NoAntialias = 0x0100,
48 NoSubpixelAntialias = 0x0800,
49 PreferNoShaping = 0x1000,
50 ContextFontMerging = 0x2000,
51 NoFontMerging = 0x8000
52 };
53 Q_ENUM(StyleStrategy)
54
56 PreferDefaultHinting = 0,
57 PreferNoHinting = 1,
58 PreferVerticalHinting = 2,
59 PreferFullHinting = 3
60 };
61 Q_ENUM(HintingPreference)
62
63 enum Weight {
64 Thin = 100,
65 ExtraLight = 200,
66 Light = 300,
67 Normal = 400,
68 Medium = 500,
69 DemiBold = 600,
70 Bold = 700,
71 ExtraBold = 800,
72 Black = 900
73 };
74 Q_ENUM(Weight)
75
76 enum Style {
79 StyleOblique
80 };
81 Q_ENUM(Style)
82
83 enum Stretch {
84 AnyStretch = 0,
85 UltraCondensed = 50,
86 ExtraCondensed = 62,
87 Condensed = 75,
88 SemiCondensed = 87,
89 Unstretched = 100,
90 SemiExpanded = 112,
91 Expanded = 125,
92 ExtraExpanded = 150,
93 UltraExpanded = 200
94 };
95 Q_ENUM(Stretch)
96
104 Q_ENUM(Capitalization)
105
108 AbsoluteSpacing
109 };
110 Q_ENUM(SpacingType)
111
113 NoPropertiesResolved = 0x0000,
114 FamilyResolved = 0x0001,
115 SizeResolved = 0x0002,
116 StyleHintResolved = 0x0004,
117 StyleStrategyResolved = 0x0008,
118 WeightResolved = 0x0010,
119 StyleResolved = 0x0020,
120 UnderlineResolved = 0x0040,
121 OverlineResolved = 0x0080,
122 StrikeOutResolved = 0x0100,
123 FixedPitchResolved = 0x0200,
124 StretchResolved = 0x0400,
125 KerningResolved = 0x0800,
126 CapitalizationResolved = 0x1000,
127 LetterSpacingResolved = 0x2000,
128 WordSpacingResolved = 0x4000,
129 HintingPreferenceResolved = 0x8000,
130 StyleNameResolved = 0x10000,
131 FamiliesResolved = 0x20000,
132 FeaturesResolved = 0x40000,
133 VariableAxesResolved = 0x80000,
134 AllPropertiesResolved = 0xfffff
135 };
136 Q_ENUM(ResolveProperties)
137
138 QFont();
139
140 QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
141 explicit QFont(const QStringList &families, int pointSize = -1, int weight = -1, bool italic = false);
142 QFont(const QFont &font, const QPaintDevice *pd);
143 QFont(const QFont &font);
144 ~QFont();
145
146 void swap(QFont &other) noexcept
147 { d.swap(other.d); std::swap(resolve_mask, other.resolve_mask); }
148
149 QString family() const;
150 void setFamily(const QString &);
151
152 QStringList families() const;
153 void setFamilies(const QStringList &);
154
155 QString styleName() const;
156 void setStyleName(const QString &);
157
158 int pointSize() const;
159 void setPointSize(int);
160 qreal pointSizeF() const;
161 void setPointSizeF(qreal);
162
163 int pixelSize() const;
164 void setPixelSize(int);
165
166 Weight weight() const;
167 void setWeight(Weight weight);
168
169 inline bool bold() const;
170 inline void setBold(bool);
171
172 void setStyle(Style style);
173 Style style() const;
174
175 inline bool italic() const;
176 inline void setItalic(bool b);
177
178 bool underline() const;
179 void setUnderline(bool);
180
181 bool overline() const;
182 void setOverline(bool);
183
184 bool strikeOut() const;
185 void setStrikeOut(bool);
186
187 bool fixedPitch() const;
188 void setFixedPitch(bool);
189
190 bool kerning() const;
191 void setKerning(bool);
192
193 StyleHint styleHint() const;
194 StyleStrategy styleStrategy() const;
195 void setStyleHint(StyleHint, StyleStrategy = PreferDefault);
196 void setStyleStrategy(StyleStrategy s);
197
198 int stretch() const;
199 void setStretch(int);
200
201 qreal letterSpacing() const;
202 SpacingType letterSpacingType() const;
203 void setLetterSpacing(SpacingType type, qreal spacing);
204
205 qreal wordSpacing() const;
206 void setWordSpacing(qreal spacing);
207
208 void setCapitalization(Capitalization);
209 Capitalization capitalization() const;
210
211 void setHintingPreference(HintingPreference hintingPreference);
212 HintingPreference hintingPreference() const;
213
214 struct Tag
215 {
216 constexpr Tag() = default;
217
218 template <size_t N>
219 constexpr Q_IMPLICIT Tag(const char (&str)[N]) noexcept
220 : m_value((quint32(str[0]) << 24) | (quint32(str[1]) << 16)
221 | (quint32(str[2]) << 8) | quint32(str[3]))
222 {
223 static_assert(N == 5, "The tag name must be exactly 4 characters long!");
224 }
225
226 constexpr bool isValid() const noexcept { return m_value != 0; }
227 constexpr quint32 value() const noexcept { return m_value; }
228
230 {
231 const char data[] = {
232 char((m_value & 0xff000000) >> 24),
233 char((m_value & 0x00ff0000) >> 16),
234 char((m_value & 0x0000ff00) >> 8),
235 char((m_value & 0x000000ff)) };
236 return QByteArray(data, sizeof(data));
237 }
238
239 static constexpr std::optional<Tag> fromValue(quint32 value) noexcept
240 {
241 Tag maybeTag;
242 maybeTag.m_value = value;
243 return maybeTag.isValid() ? std::optional<Tag>(maybeTag) : std::nullopt;
244 }
245 Q_GUI_EXPORT static std::optional<Tag> fromString(QAnyStringView view) noexcept;
246
247#ifndef QT_NO_DATASTREAM
248 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, Tag);
249 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, Tag &);
250#endif
251
252#ifndef QT_NO_DEBUG_STREAM
253 friend Q_GUI_EXPORT QDebug operator<<(QDebug debug, Tag tag);
254#endif
255
256 friend constexpr size_t qHash(Tag key, size_t seed = 0) noexcept
257 { return qHash(key.value(), seed); }
258
259 private:
260 friend constexpr bool comparesEqual(const Tag &lhs, const Tag &rhs) noexcept
261 { return lhs.m_value == rhs.m_value; }
262 friend constexpr Qt::strong_ordering compareThreeWay(const Tag &lhs, const Tag &rhs) noexcept
263 { return Qt::compareThreeWay(lhs.m_value, rhs.m_value); }
265
266 quint32 m_value = 0;
267 };
268
269 void setFeature(Tag tag, quint32 value);
270 void unsetFeature(Tag tag);
271 quint32 featureValue(Tag tag) const;
272 bool isFeatureSet(Tag tag) const;
273 QList<Tag> featureTags() const;
274 void clearFeatures();
275
276 void setVariableAxis(Tag tag, float value);
277 void unsetVariableAxis(Tag tag);
278 bool isVariableAxisSet(Tag tag) const;
279 float variableAxisValue(Tag tag) const;
280 void clearVariableAxes();
281 QList<Tag> variableAxisTags() const;
282
283 // dupicated from QFontInfo
284 bool exactMatch() const;
285
286 QFont &operator=(const QFont &);
287 bool operator==(const QFont &) const;
288 bool operator!=(const QFont &) const;
289 bool operator<(const QFont &) const;
290 operator QVariant() const;
291 bool isCopyOf(const QFont &) const;
292 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFont)
293
294 QString key() const;
295
296 QString toString() const;
297 bool fromString(const QString &);
298
299 static QString substitute(const QString &);
300 static QStringList substitutes(const QString &);
301 static QStringList substitutions();
302 static void insertSubstitution(const QString&, const QString &);
303 static void insertSubstitutions(const QString&, const QStringList &);
304 static void removeSubstitutions(const QString &);
305 static void initialize();
306 static void cleanup();
307 static void cacheStatistics();
308
309 QString defaultFamily() const;
310
311 QFont resolve(const QFont &) const;
312 inline uint resolveMask() const { return resolve_mask; }
313 inline void setResolveMask(uint mask) { resolve_mask = mask; }
314
315#if QT_DEPRECATED_SINCE(6, 0)
316 QT_DEPRECATED_VERSION_X_6_0("Use setWeight() instead") void setLegacyWeight(int legacyWeight);
317 QT_DEPRECATED_VERSION_X_6_0("Use weight() instead") int legacyWeight() const;
318#endif
319
320private:
321 explicit QFont(QFontPrivate *);
322
323 void detach();
324
325
326 friend class QFontPrivate;
327 friend class QFontDialogPrivate;
328 friend class QFontMetrics;
329 friend class QFontMetricsF;
330 friend class QFontInfo;
331 friend class QPainter;
332 friend class QPainterPrivate;
333 friend class QApplication;
334 friend class QWidget;
335 friend class QWidgetPrivate;
336 friend class QTextLayout;
337 friend class QTextEngine;
338 friend class QStackTextEngine;
339 friend class QTextLine;
340 friend struct QScriptLine;
341 friend class QOpenGLContext;
342 friend class QWin32PaintEngine;
343 friend class QAlphaPaintEngine;
344 friend class QPainterPath;
345 friend class QTextItemInt;
347 friend class QPainterReplayer;
348 friend class QPaintBufferEngine;
350 friend class QFontEngine;
351
352#ifndef QT_NO_DATASTREAM
353 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QFont &);
354 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QFont &);
355#endif
356
357#ifndef QT_NO_DEBUG_STREAM
358 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QFont &);
359#endif
360
361 QExplicitlySharedDataPointer<QFontPrivate> d;
362 uint resolve_mask;
363};
364
365Q_DECLARE_SHARED(QFont)
366
367Q_GUI_EXPORT size_t qHash(const QFont &font, size_t seed = 0) noexcept;
368
369inline bool QFont::bold() const
370{ return weight() > Medium; }
371
372
373inline void QFont::setBold(bool enable)
374{ setWeight(enable ? Bold : Normal); }
375
376inline bool QFont::italic() const
377{
378 return (style() != StyleNormal);
379}
380
381inline void QFont::setItalic(bool b) {
383}
384
385
386/*****************************************************************************
387 QFont stream functions
388 *****************************************************************************/
389
390#ifndef QT_NO_DATASTREAM
391Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QFont &);
393#endif
394
395#ifndef QT_NO_DEBUG_STREAM
396Q_GUI_EXPORT QDebug operator<<(QDebug, const QFont &);
397#endif
398
400
401#endif // QFONT_H
\inmodule QtCore
The QApplication class manages the GUI application's control flow and main settings.
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\reentrant
Definition qfontinfo.h:16
\reentrant \inmodule QtGui
\reentrant \inmodule QtGui
\reentrant
Definition qfont.h:22
StyleHint
Style hints are used by the \l{QFont}{font matching} algorithm to find an appropriate default family ...
Definition qfont.h:25
@ Helvetica
Definition qfont.h:26
@ AnyStyle
Definition qfont.h:31
@ Monospace
Definition qfont.h:33
@ System
Definition qfont.h:30
@ Courier
Definition qfont.h:28
@ Cursive
Definition qfont.h:32
void setStyle(Style style)
Sets the style of the font to style.
Definition qfont.cpp:1116
bool italic() const
Returns true if the style() of the font is not QFont::StyleNormal.
Definition qfont.h:376
void swap(QFont &other) noexcept
Definition qfont.h:146
Capitalization
Definition qfont.h:97
@ AllLowercase
Definition qfont.h:100
@ AllUppercase
Definition qfont.h:99
@ MixedCase
Definition qfont.h:98
@ SmallCaps
Definition qfont.h:101
SpacingType
Definition qfont.h:106
@ PercentageSpacing
Definition qfont.h:107
ResolveProperties
Definition qfont.h:112
void setBold(bool)
If enable is true sets the font's weight to \l{Weight}{QFont::Bold}; otherwise sets the weight to \l{...
Definition qfont.h:373
void setItalic(bool b)
Sets the style() of the font to QFont::StyleItalic if enable is true; otherwise the style is set to Q...
Definition qfont.h:381
HintingPreference
Definition qfont.h:55
void setResolveMask(uint mask)
Definition qfont.h:313
uint resolveMask() const
Definition qfont.h:312
Stretch
Predefined stretch values that follow the CSS naming convention.
Definition qfont.h:83
Style style() const
Returns the style of the font.
Definition qfont.cpp:1105
StyleStrategy
The style strategy tells the \l{QFont}{font matching} algorithm what type of fonts should be used to ...
Definition qfont.h:38
Weight
Qt uses a weighting scale from 1 to 1000 compatible with OpenType.
Definition qfont.h:63
@ Bold
Definition qfont.h:70
@ Normal
Definition qfont.h:67
void setWeight(Weight weight)
Sets the weight of the font to weight, using the scale defined by \l QFont::Weight enumeration.
Definition qfont.cpp:1205
Style
This enum describes the different styles of glyphs that are used to display text.
Definition qfont.h:76
@ StyleItalic
Definition qfont.h:78
@ StyleNormal
Definition qfont.h:77
\inmodule QtGui
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Internal QTextItem.
\reentrant
Definition qtextlayout.h:70
\reentrant
\inmodule QtCore
Definition qvariant.h:65
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
QString str
[2]
qreal spacing
Combined button and popup list for selecting options.
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
#define Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(...)
#define Q_IMPLICIT
constexpr bool operator!=(const timespec &t1, const timespec &t2)
AudioChannelLayoutTag tag
static bool initialize()
Definition qctf.cpp:94
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QFont &)
Q_GUI_EXPORT size_t qHash(const QFont &font, size_t seed=0) noexcept
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QFont &)
static QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
quint32 Tag
GLboolean GLboolean GLboolean b
GLuint64 key
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint GLuint GLfloat weight
GLenum type
GLboolean enable
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLdouble s
[6]
Definition qopenglext.h:235
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
static const QQmlJSScope * resolve(const QQmlJSScope *current, const QStringList &names)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
@ Medium
#define QT_DEPRECATED_VERSION_X_6_0(text)
#define Q_ENUM(x)
#define Q_GADGET
unsigned int quint32
Definition qtypes.h:50
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QDataStream & operator>>(QDataStream &in, MyClass &myObj)
QSharedPointer< T > other(t)
[5]
widget setStyle(proxy)
QQuickView * view
[0]
char * toString(const MyType &t)
[31]
The QFont::Tag type provides access to advanced font features.
Definition qfont.h:215
QByteArray toString() const
Returns the string representation of this tag as a byte array.
Definition qfont.h:229
constexpr Q_IMPLICIT Tag(const char(&str)[N]) noexcept
Constructs a tag from a string literal, str.
Definition qfont.h:219
static constexpr std::optional< Tag > fromValue(quint32 value) noexcept
Returns a tag constructed from value, or std::nullopt if the tag produced would be invalid.
Definition qfont.h:239
friend constexpr size_t qHash(Tag key, size_t seed=0) noexcept
Returns the hash value for key, using seed to seed the calculation.
Definition qfont.h:256
constexpr Tag()=default
Default constructor, producing an invalid tag.
friend constexpr bool comparesEqual(const Tag &lhs, const Tag &rhs) noexcept
Definition qfont.h:260
constexpr quint32 value() const noexcept
Returns the numerical value of this tag.
Definition qfont.h:227
friend constexpr Qt::strong_ordering compareThreeWay(const Tag &lhs, const Tag &rhs) noexcept
Compare lhs with rhs for equality and ordering.
Definition qfont.h:262
constexpr bool isValid() const noexcept
Returns whether the tag is valid.
Definition qfont.h:226