| 1 | // Copyright (C) 2024 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 | #include "qfontvariableaxis.h" |
| 5 | |
| 6 | #include <QtCore/qdebug.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | class QFontVariableAxisPrivate : public QSharedData |
| 11 | { |
| 12 | public: |
| 13 | QFont::Tag tag; |
| 14 | QString name; |
| 15 | qreal minimumValue = 0.0; |
| 16 | qreal maximumValue = 0.0; |
| 17 | qreal defaultValue = 0.0; |
| 18 | }; |
| 19 | |
| 20 | /*! |
| 21 | \class QFontVariableAxis |
| 22 | \reentrant |
| 23 | \inmodule QtGui |
| 24 | \ingroup shared |
| 25 | \since 6.9 |
| 26 | |
| 27 | \brief The QFontVariableAxis class represents a variable axis in a font. |
| 28 | |
| 29 | Variable fonts provide a way to store multiple variations (with different weights, widths |
| 30 | or styles) in the same font file. The variations are given as floating point values for |
| 31 | a pre-defined set of parameters, called "variable axes". |
| 32 | |
| 33 | Specific parameterizations (sets of values for the axes in a font) can be selected using |
| 34 | the properties in QFont, same as with traditional subfamilies that are defined as stand-alone |
| 35 | font files. But with variable fonts, arbitrary values can be provided for each axis to gain a |
| 36 | fine-grained customization of the font's appearance. |
| 37 | |
| 38 | QFontVariableAxis contains information of one axis. Use \l{QFontInfo::variableAxes()} |
| 39 | to retrieve a list of the variable axes defined for a given font. Specific values can be |
| 40 | provided for an axis by using \l{QFont::setVariableAxis()} and passing in the \l{tag()}. |
| 41 | |
| 42 | \note On Windows, variable axes are not supported if the optional GDI font backend is in use. |
| 43 | */ |
| 44 | QFontVariableAxis::QFontVariableAxis() |
| 45 | : d_ptr(new QFontVariableAxisPrivate) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /*! |
| 50 | Destroys this QFontVariableAxis object. |
| 51 | */ |
| 52 | QFontVariableAxis::~QFontVariableAxis() = default; |
| 53 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QFontVariableAxisPrivate) |
| 54 | |
| 55 | /*! |
| 56 | Creates a QFontVariableAxis object that is a copy of the given \a axis. |
| 57 | |
| 58 | \sa operator=() |
| 59 | */ |
| 60 | QFontVariableAxis::QFontVariableAxis(const QFontVariableAxis &axis) = default; |
| 61 | |
| 62 | /*! |
| 63 | Returns the tag of the axis. This is a four-character sequence which identifies the axis. |
| 64 | Certain tags have standardized meanings, such as "wght" (weight) and "wdth" (width), but any |
| 65 | sequence of four latin-1 characters is a valid tag. By convention, non-standard/custom axes |
| 66 | are denoted by tags in all uppercase. |
| 67 | |
| 68 | \sa QFont::setVariableAxis(), name() |
| 69 | */ |
| 70 | QFont::Tag QFontVariableAxis::tag() const |
| 71 | { |
| 72 | Q_D(const QFontVariableAxis); |
| 73 | return d->tag; |
| 74 | } |
| 75 | |
| 76 | /*! |
| 77 | Sets the tag of QFontVariableAxis to \a tag. |
| 78 | |
| 79 | \note Typically, there will be no need to call this function as it will not affect the font |
| 80 | itself, only this particular representation. |
| 81 | |
| 82 | \sa tag() |
| 83 | */ |
| 84 | void QFontVariableAxis::setTag(QFont::Tag tag) |
| 85 | { |
| 86 | if (d_func()->tag == tag) |
| 87 | return; |
| 88 | detach(); |
| 89 | Q_D(QFontVariableAxis); |
| 90 | d->tag = tag; |
| 91 | } |
| 92 | |
| 93 | /*! |
| 94 | Returns the name of the axis, if provided by the font. |
| 95 | |
| 96 | \sa tag() |
| 97 | */ |
| 98 | QString QFontVariableAxis::name() const |
| 99 | { |
| 100 | Q_D(const QFontVariableAxis); |
| 101 | return d->name; |
| 102 | } |
| 103 | |
| 104 | /*! |
| 105 | Sets the name of this QFontVariableAxis to \a name. |
| 106 | |
| 107 | \note Typically, there will be no need to call this function as it will not affect the font |
| 108 | itself, only this particular representation. |
| 109 | |
| 110 | \sa name() |
| 111 | */ |
| 112 | void QFontVariableAxis::setName(const QString &name) |
| 113 | { |
| 114 | if (d_func()->name == name) |
| 115 | return; |
| 116 | detach(); |
| 117 | Q_D(QFontVariableAxis); |
| 118 | d->name = name; |
| 119 | } |
| 120 | |
| 121 | /*! |
| 122 | Returns the minimum value of the axis. Setting the axis to a value which is lower than this |
| 123 | is not supported. |
| 124 | |
| 125 | \sa maximumValue(), defaultValue() |
| 126 | */ |
| 127 | qreal QFontVariableAxis::minimumValue() const |
| 128 | { |
| 129 | Q_D(const QFontVariableAxis); |
| 130 | return d->minimumValue; |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | Sets the minimum value of this QFontVariableAxis to \a minimumValue. |
| 135 | |
| 136 | \note Typically, there will be no need to call this function as it will not affect the font |
| 137 | itself, only this particular representation. |
| 138 | |
| 139 | \sa minimumValue() |
| 140 | */ |
| 141 | void QFontVariableAxis::setMinimumValue(qreal minimumValue) |
| 142 | { |
| 143 | if (d_func()->minimumValue == minimumValue) |
| 144 | return; |
| 145 | detach(); |
| 146 | Q_D(QFontVariableAxis); |
| 147 | d->minimumValue = minimumValue; |
| 148 | } |
| 149 | |
| 150 | /*! |
| 151 | Returns the maximum value of the axis. Setting the axis to a value which is higher than this |
| 152 | is not supported. |
| 153 | |
| 154 | \sa minimumValue(), defaultValue() |
| 155 | */ |
| 156 | qreal QFontVariableAxis::maximumValue() const |
| 157 | { |
| 158 | Q_D(const QFontVariableAxis); |
| 159 | return d->maximumValue; |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | Sets the maximum value of this QFontVariableAxis to \a maximumValue. |
| 164 | |
| 165 | \note Typically, there will be no need to call this function as it will not affect the font |
| 166 | itself, only this particular representation. |
| 167 | |
| 168 | \sa maximumValue() |
| 169 | */ |
| 170 | void QFontVariableAxis::setMaximumValue(qreal maximumValue) |
| 171 | { |
| 172 | if (d_func()->maximumValue == maximumValue) |
| 173 | return; |
| 174 | detach(); |
| 175 | Q_D(QFontVariableAxis); |
| 176 | d->maximumValue = maximumValue; |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | Returns the default value of the axis. This is the value the axis will have if none has been |
| 181 | provided in the QFont query. |
| 182 | |
| 183 | \sa minimumValue(), maximumValue() |
| 184 | */ |
| 185 | qreal QFontVariableAxis::defaultValue() const |
| 186 | { |
| 187 | Q_D(const QFontVariableAxis); |
| 188 | return d->defaultValue; |
| 189 | } |
| 190 | |
| 191 | /*! |
| 192 | Sets the default value of this QFontVariableAxis to \a defaultValue. |
| 193 | |
| 194 | \note Typically, there will be no need to call this function as it will not affect the font |
| 195 | itself, only this particular representation. |
| 196 | |
| 197 | \sa defaultValue() |
| 198 | */ |
| 199 | void QFontVariableAxis::setDefaultValue(qreal defaultValue) |
| 200 | { |
| 201 | if (d_func()->defaultValue == defaultValue) |
| 202 | return; |
| 203 | detach(); |
| 204 | Q_D(QFontVariableAxis); |
| 205 | d->defaultValue = defaultValue; |
| 206 | } |
| 207 | |
| 208 | /*! |
| 209 | Assigns the given \a axis to this QFontVariableAxis. |
| 210 | |
| 211 | \sa QFontVariableAxis() |
| 212 | */ |
| 213 | QFontVariableAxis &QFontVariableAxis::operator=(const QFontVariableAxis &axis) |
| 214 | { |
| 215 | QFontVariableAxis copy(axis); |
| 216 | swap(other&: copy); |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | /*! |
| 221 | \internal |
| 222 | */ |
| 223 | void QFontVariableAxis::detach() |
| 224 | { |
| 225 | d_ptr.detach(); |
| 226 | } |
| 227 | |
| 228 | #ifndef QT_NO_DEBUG_STREAM |
| 229 | QDebug operator<<(QDebug debug, const QFontVariableAxis &axis) |
| 230 | { |
| 231 | QDebugStateSaver save(debug); |
| 232 | |
| 233 | debug.nospace().noquote(); |
| 234 | const QString name = axis.name(); |
| 235 | if (!name.isEmpty()) |
| 236 | debug << name << '('; |
| 237 | |
| 238 | debug << axis.tag(); |
| 239 | if (!name.isEmpty()) |
| 240 | debug << ')'; |
| 241 | debug << '[' << axis.minimumValue() << "..." << axis.maximumValue() |
| 242 | << "; default=" << axis.defaultValue() << ']'; |
| 243 | |
| 244 | return debug; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | QT_END_NAMESPACE |
| 249 | |