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#include <private/qquickvaluetypes_p.h>
5
6#include <qtquickglobal.h>
7#include <private/qqmlvaluetype_p.h>
8#include <private/qcolorspace_p.h>
9#include <private/qfont_p.h>
10
11QT_BEGIN_NAMESPACE
12
13QQuickColorValueType::QQuickColorValueType(const QString &string)
14 : v(QColor::fromString(name: string))
15{
16}
17
18QVariant QQuickColorValueType::create(const QJSValue &params)
19{
20 return params.isString() ? QColor::fromString(name: params.toString()) : QVariant();
21}
22
23QString QQuickColorValueType::toString() const
24{
25 return v.name(format: v.alpha() != 255 ? QColor::HexArgb : QColor::HexRgb);
26}
27
28QVariant QQuickColorValueType::lighter(qreal factor) const
29{
30 return QQml_colorProvider()->lighter(this->v, factor);
31}
32
33QVariant QQuickColorValueType::darker(qreal factor) const
34{
35 return QQml_colorProvider()->darker(this->v, factor);
36}
37
38QVariant QQuickColorValueType::alpha(qreal value) const
39{
40 return QQml_colorProvider()->alpha(this->v, value);
41}
42
43QVariant QQuickColorValueType::tint(QVariant tintColor) const
44{
45 return QQml_colorProvider()->tint(this->v, tintColor);
46}
47
48qreal QQuickColorValueType::r() const
49{
50 return v.redF();
51}
52
53qreal QQuickColorValueType::g() const
54{
55 return v.greenF();
56}
57
58qreal QQuickColorValueType::b() const
59{
60 return v.blueF();
61}
62
63qreal QQuickColorValueType::a() const
64{
65 return v.alphaF();
66}
67
68qreal QQuickColorValueType::hsvHue() const
69{
70 return v.hsvHueF();
71}
72
73qreal QQuickColorValueType::hsvSaturation() const
74{
75 return v.hsvSaturationF();
76}
77
78qreal QQuickColorValueType::hsvValue() const
79{
80 return v.valueF();
81}
82
83qreal QQuickColorValueType::hslHue() const
84{
85 return v.hslHueF();
86}
87
88qreal QQuickColorValueType::hslSaturation() const
89{
90 return v.hslSaturationF();
91}
92
93qreal QQuickColorValueType::hslLightness() const
94{
95 return v.lightnessF();
96}
97
98bool QQuickColorValueType::isValid() const
99{
100 return v.isValid();
101}
102
103void QQuickColorValueType::setR(qreal r)
104{
105 v.setRedF(r);
106}
107
108void QQuickColorValueType::setG(qreal g)
109{
110 v.setGreenF(g);
111}
112
113void QQuickColorValueType::setB(qreal b)
114{
115 v.setBlueF(b);
116}
117
118void QQuickColorValueType::setA(qreal a)
119{
120 v.setAlphaF(a);
121}
122
123void QQuickColorValueType::setHsvHue(qreal hsvHue)
124{
125 float hue, saturation, value, alpha;
126 v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha);
127 v.setHsvF(h: hsvHue, s: saturation, v: value, a: alpha);
128}
129
130void QQuickColorValueType::setHsvSaturation(qreal hsvSaturation)
131{
132 float hue, saturation, value, alpha;
133 v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha);
134 v.setHsvF(h: hue, s: hsvSaturation, v: value, a: alpha);
135}
136
137void QQuickColorValueType::setHsvValue(qreal hsvValue)
138{
139 float hue, saturation, value, alpha;
140 v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha);
141 v.setHsvF(h: hue, s: saturation, v: hsvValue, a: alpha);
142}
143
144void QQuickColorValueType::setHslHue(qreal hslHue)
145{
146 float hue, saturation, lightness, alpha;
147 v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha);
148 v.setHslF(h: hslHue, s: saturation, l: lightness, a: alpha);
149}
150
151void QQuickColorValueType::setHslSaturation(qreal hslSaturation)
152{
153 float hue, saturation, lightness, alpha;
154 v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha);
155 v.setHslF(h: hue, s: hslSaturation, l: lightness, a: alpha);
156}
157
158void QQuickColorValueType::setHslLightness(qreal hslLightness)
159{
160 float hue, saturation, lightness, alpha;
161 v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha);
162 v.setHslF(h: hue, s: saturation, l: hslLightness, a: alpha);
163}
164
165template<typename T, int NumParams>
166QVariant createValueTypeFromNumberString(const QString &s)
167{
168 Q_STATIC_ASSERT_X(NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16,
169 "Unsupported number of params; add an additional case below if necessary.");
170
171 if (s.count(c: u',') != NumParams - 1)
172 return QVariant();
173
174 QVarLengthArray<float, NumParams> parameters;
175 bool ok = true;
176 for (qsizetype prev = 0, next = s.indexOf(c: u','), length = s.size(); ok && prev < length;) {
177 parameters.append(s.mid(position: prev, n: next - prev).toFloat(ok: &ok));
178 prev = next + 1;
179 next = (parameters.size() == NumParams - 1) ? length : s.indexOf(c: u',', from: prev);
180 }
181
182 if (!ok)
183 return QVariant();
184
185 if constexpr (NumParams == 2) {
186 return T(parameters[0], parameters[1]);
187 } else if constexpr (NumParams == 3) {
188 return T(parameters[0], parameters[1], parameters[2]);
189 } else if constexpr (NumParams == 4) {
190 return T(parameters[0], parameters[1], parameters[2], parameters[3]);
191 } else if constexpr (NumParams == 16) {
192 return T(parameters[0], parameters[1], parameters[2], parameters[3],
193 parameters[4], parameters[5], parameters[6], parameters[7],
194 parameters[8], parameters[9], parameters[10], parameters[11],
195 parameters[12], parameters[13], parameters[14], parameters[15]);
196 } else {
197 Q_UNREACHABLE();
198 }
199
200 return QVariant();
201}
202
203QVariant QQuickVector2DValueType::create(const QJSValue &params)
204{
205 if (params.isString())
206 return createValueTypeFromNumberString<QVector2D, 2>(s: params.toString());
207 if (params.isArray())
208 return QVector2D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber());
209 return QVariant();
210}
211
212QString QQuickVector2DValueType::toString() const
213{
214 return QString(QLatin1String("QVector2D(%1, %2)")).arg(a: v.x()).arg(a: v.y());
215}
216
217qreal QQuickVector2DValueType::x() const
218{
219 return v.x();
220}
221
222qreal QQuickVector2DValueType::y() const
223{
224 return v.y();
225}
226
227void QQuickVector2DValueType::setX(qreal x)
228{
229 v.setX(x);
230}
231
232void QQuickVector2DValueType::setY(qreal y)
233{
234 v.setY(y);
235}
236
237qreal QQuickVector2DValueType::dotProduct(const QVector2D &vec) const
238{
239 return QVector2D::dotProduct(v1: v, v2: vec);
240}
241
242QVector2D QQuickVector2DValueType::times(const QVector2D &vec) const
243{
244 return v * vec;
245}
246
247QVector2D QQuickVector2DValueType::times(qreal scalar) const
248{
249 return v * scalar;
250}
251
252QVector2D QQuickVector2DValueType::plus(const QVector2D &vec) const
253{
254 return v + vec;
255}
256
257QVector2D QQuickVector2DValueType::minus(const QVector2D &vec) const
258{
259 return v - vec;
260}
261
262QVector2D QQuickVector2DValueType::normalized() const
263{
264 return v.normalized();
265}
266
267qreal QQuickVector2DValueType::length() const
268{
269 return v.length();
270}
271
272QVector3D QQuickVector2DValueType::toVector3d() const
273{
274 return v.toVector3D();
275}
276
277QVector4D QQuickVector2DValueType::toVector4d() const
278{
279 return v.toVector4D();
280}
281
282bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec, qreal epsilon) const
283{
284 qreal absEps = qAbs(t: epsilon);
285 if (qAbs(t: v.x() - vec.x()) > absEps)
286 return false;
287 if (qAbs(t: v.y() - vec.y()) > absEps)
288 return false;
289 return true;
290}
291
292bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec) const
293{
294 return qFuzzyCompare(v1: v, v2: vec);
295}
296
297QVariant QQuickVector3DValueType::create(const QJSValue &params)
298{
299 if (params.isString())
300 return createValueTypeFromNumberString<QVector3D, 3>(s: params.toString());
301
302 if (params.isArray()) {
303 return QVector3D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(),
304 params.property(arrayIndex: 2).toNumber());
305 }
306 return QVariant();
307}
308
309QString QQuickVector3DValueType::toString() const
310{
311 return QString(QLatin1String("QVector3D(%1, %2, %3)")).arg(a: v.x()).arg(a: v.y()).arg(a: v.z());
312}
313
314qreal QQuickVector3DValueType::x() const
315{
316 return v.x();
317}
318
319qreal QQuickVector3DValueType::y() const
320{
321 return v.y();
322}
323
324qreal QQuickVector3DValueType::z() const
325{
326 return v.z();
327}
328
329void QQuickVector3DValueType::setX(qreal x)
330{
331 v.setX(x);
332}
333
334void QQuickVector3DValueType::setY(qreal y)
335{
336 v.setY(y);
337}
338
339void QQuickVector3DValueType::setZ(qreal z)
340{
341 v.setZ(z);
342}
343
344QVector3D QQuickVector3DValueType::crossProduct(const QVector3D &vec) const
345{
346 return QVector3D::crossProduct(v1: v, v2: vec);
347}
348
349qreal QQuickVector3DValueType::dotProduct(const QVector3D &vec) const
350{
351 return QVector3D::dotProduct(v1: v, v2: vec);
352}
353
354QVector3D QQuickVector3DValueType::times(const QMatrix4x4 &m) const
355{
356 return (QVector4D(v, 1) * m).toVector3DAffine();
357}
358
359QVector3D QQuickVector3DValueType::times(const QVector3D &vec) const
360{
361 return v * vec;
362}
363
364QVector3D QQuickVector3DValueType::times(qreal scalar) const
365{
366 return v * scalar;
367}
368
369QVector3D QQuickVector3DValueType::plus(const QVector3D &vec) const
370{
371 return v + vec;
372}
373
374QVector3D QQuickVector3DValueType::minus(const QVector3D &vec) const
375{
376 return v - vec;
377}
378
379QVector3D QQuickVector3DValueType::normalized() const
380{
381 return v.normalized();
382}
383
384qreal QQuickVector3DValueType::length() const
385{
386 return v.length();
387}
388
389QVector2D QQuickVector3DValueType::toVector2d() const
390{
391 return v.toVector2D();
392}
393
394QVector4D QQuickVector3DValueType::toVector4d() const
395{
396 return v.toVector4D();
397}
398
399bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec, qreal epsilon) const
400{
401 qreal absEps = qAbs(t: epsilon);
402 if (qAbs(t: v.x() - vec.x()) > absEps)
403 return false;
404 if (qAbs(t: v.y() - vec.y()) > absEps)
405 return false;
406 if (qAbs(t: v.z() - vec.z()) > absEps)
407 return false;
408 return true;
409}
410
411bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec) const
412{
413 return qFuzzyCompare(v1: v, v2: vec);
414}
415
416QVariant QQuickVector4DValueType::create(const QJSValue &params)
417{
418 if (params.isString())
419 return createValueTypeFromNumberString<QVector4D, 4>(s: params.toString());
420
421 if (params.isArray()) {
422 return QVector4D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(),
423 params.property(arrayIndex: 2).toNumber(), params.property(arrayIndex: 3).toNumber());
424 }
425
426 return QVariant();
427}
428
429QString QQuickVector4DValueType::toString() const
430{
431 return QString(QLatin1String("QVector4D(%1, %2, %3, %4)")).arg(a: v.x()).arg(a: v.y()).arg(a: v.z()).arg(a: v.w());
432}
433
434qreal QQuickVector4DValueType::x() const
435{
436 return v.x();
437}
438
439qreal QQuickVector4DValueType::y() const
440{
441 return v.y();
442}
443
444qreal QQuickVector4DValueType::z() const
445{
446 return v.z();
447}
448
449qreal QQuickVector4DValueType::w() const
450{
451 return v.w();
452}
453
454void QQuickVector4DValueType::setX(qreal x)
455{
456 v.setX(x);
457}
458
459void QQuickVector4DValueType::setY(qreal y)
460{
461 v.setY(y);
462}
463
464void QQuickVector4DValueType::setZ(qreal z)
465{
466 v.setZ(z);
467}
468
469void QQuickVector4DValueType::setW(qreal w)
470{
471 v.setW(w);
472}
473
474qreal QQuickVector4DValueType::dotProduct(const QVector4D &vec) const
475{
476 return QVector4D::dotProduct(v1: v, v2: vec);
477}
478
479QVector4D QQuickVector4DValueType::times(const QVector4D &vec) const
480{
481 return v * vec;
482}
483
484QVector4D QQuickVector4DValueType::times(const QMatrix4x4 &m) const
485{
486 return v * m;
487}
488
489QVector4D QQuickVector4DValueType::times(qreal scalar) const
490{
491 return v * scalar;
492}
493
494QVector4D QQuickVector4DValueType::plus(const QVector4D &vec) const
495{
496 return v + vec;
497}
498
499QVector4D QQuickVector4DValueType::minus(const QVector4D &vec) const
500{
501 return v - vec;
502}
503
504QVector4D QQuickVector4DValueType::normalized() const
505{
506 return v.normalized();
507}
508
509qreal QQuickVector4DValueType::length() const
510{
511 return v.length();
512}
513
514QVector2D QQuickVector4DValueType::toVector2d() const
515{
516 return v.toVector2D();
517}
518
519QVector3D QQuickVector4DValueType::toVector3d() const
520{
521 return v.toVector3D();
522}
523
524bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec, qreal epsilon) const
525{
526 qreal absEps = qAbs(t: epsilon);
527 if (qAbs(t: v.x() - vec.x()) > absEps)
528 return false;
529 if (qAbs(t: v.y() - vec.y()) > absEps)
530 return false;
531 if (qAbs(t: v.z() - vec.z()) > absEps)
532 return false;
533 if (qAbs(t: v.w() - vec.w()) > absEps)
534 return false;
535 return true;
536}
537
538bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec) const
539{
540 return qFuzzyCompare(v1: v, v2: vec);
541}
542
543QVariant QQuickQuaternionValueType::create(const QJSValue &params)
544{
545 if (params.isString())
546 return createValueTypeFromNumberString<QQuaternion, 4>(s: params.toString());
547
548 if (params.isArray()) {
549 return QQuaternion(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(),
550 params.property(arrayIndex: 2).toNumber(), params.property(arrayIndex: 3).toNumber());
551 }
552
553 return QVariant();
554}
555
556QString QQuickQuaternionValueType::toString() const
557{
558 return QString(QLatin1String("QQuaternion(%1, %2, %3, %4)")).arg(a: v.scalar()).arg(a: v.x()).arg(a: v.y()).arg(a: v.z());
559}
560
561qreal QQuickQuaternionValueType::scalar() const
562{
563 return v.scalar();
564}
565
566qreal QQuickQuaternionValueType::x() const
567{
568 return v.x();
569}
570
571qreal QQuickQuaternionValueType::y() const
572{
573 return v.y();
574}
575
576qreal QQuickQuaternionValueType::z() const
577{
578 return v.z();
579}
580
581void QQuickQuaternionValueType::setScalar(qreal scalar)
582{
583 v.setScalar(scalar);
584}
585
586void QQuickQuaternionValueType::setX(qreal x)
587{
588 v.setX(x);
589}
590
591void QQuickQuaternionValueType::setY(qreal y)
592{
593 v.setY(y);
594}
595
596void QQuickQuaternionValueType::setZ(qreal z)
597{
598 v.setZ(z);
599}
600
601qreal QQuickQuaternionValueType::dotProduct(const QQuaternion &q) const
602{
603 return QQuaternion::dotProduct(q1: v, q2: q);
604}
605
606QQuaternion QQuickQuaternionValueType::times(const QQuaternion &q) const
607{
608 return v * q;
609}
610
611QVector3D QQuickQuaternionValueType::times(const QVector3D &vec) const
612{
613 return v * vec;
614}
615
616QQuaternion QQuickQuaternionValueType::times(qreal factor) const
617{
618 return v * factor;
619}
620
621QQuaternion QQuickQuaternionValueType::plus(const QQuaternion &q) const
622{
623 return v + q;
624}
625
626QQuaternion QQuickQuaternionValueType::minus(const QQuaternion &q) const
627{
628 return v - q;
629}
630
631QQuaternion QQuickQuaternionValueType::normalized() const
632{
633 return v.normalized();
634}
635
636QQuaternion QQuickQuaternionValueType::inverted() const
637{
638 return v.inverted();
639}
640
641QQuaternion QQuickQuaternionValueType::conjugated() const
642{
643 return v.conjugated();
644}
645
646qreal QQuickQuaternionValueType::length() const
647{
648 return v.length();
649}
650
651QVector3D QQuickQuaternionValueType::toEulerAngles() const
652{
653 return v.toEulerAngles();
654}
655
656QVector4D QQuickQuaternionValueType::toVector4d() const
657{
658 return v.toVector4D();
659}
660
661bool QQuickQuaternionValueType::fuzzyEquals(const QQuaternion &q, qreal epsilon) const
662{
663 qreal absEps = qAbs(t: epsilon);
664 if (qAbs(t: v.scalar() - q.scalar()) > absEps)
665 return false;
666 if (qAbs(t: v.x() - q.x()) > absEps)
667 return false;
668 if (qAbs(t: v.y() - q.y()) > absEps)
669 return false;
670 if (qAbs(t: v.z() - q.z()) > absEps)
671 return false;
672 return true;
673}
674
675bool QQuickQuaternionValueType::fuzzyEquals(const QQuaternion &q) const
676{
677 return qFuzzyCompare(q1: v, q2: q);
678}
679
680QVariant QQuickMatrix4x4ValueType::create(const QJSValue &params)
681{
682 if (params.isNull() || params.isUndefined())
683 return QMatrix4x4();
684
685 if (params.isString())
686 return createValueTypeFromNumberString<QMatrix4x4, 16>(s: params.toString());
687
688 if (params.isArray() && params.property(QStringLiteral("length")).toInt() == 16) {
689 return QMatrix4x4(params.property(arrayIndex: 0).toNumber(),
690 params.property(arrayIndex: 1).toNumber(),
691 params.property(arrayIndex: 2).toNumber(),
692 params.property(arrayIndex: 3).toNumber(),
693 params.property(arrayIndex: 4).toNumber(),
694 params.property(arrayIndex: 5).toNumber(),
695 params.property(arrayIndex: 6).toNumber(),
696 params.property(arrayIndex: 7).toNumber(),
697 params.property(arrayIndex: 8).toNumber(),
698 params.property(arrayIndex: 9).toNumber(),
699 params.property(arrayIndex: 10).toNumber(),
700 params.property(arrayIndex: 11).toNumber(),
701 params.property(arrayIndex: 12).toNumber(),
702 params.property(arrayIndex: 13).toNumber(),
703 params.property(arrayIndex: 14).toNumber(),
704 params.property(arrayIndex: 15).toNumber());
705 }
706
707 return QVariant();
708}
709
710QMatrix4x4 QQuickMatrix4x4ValueType::times(const QMatrix4x4 &m) const
711{
712 return v * m;
713}
714
715QVector4D QQuickMatrix4x4ValueType::times(const QVector4D &vec) const
716{
717 return v * vec;
718}
719
720QVector3D QQuickMatrix4x4ValueType::times(const QVector3D &vec) const
721{
722 return v.map(point: vec);
723}
724
725QMatrix4x4 QQuickMatrix4x4ValueType::times(qreal factor) const
726{
727 return v * factor;
728}
729
730QMatrix4x4 QQuickMatrix4x4ValueType::plus(const QMatrix4x4 &m) const
731{
732 return v + m;
733}
734
735QMatrix4x4 QQuickMatrix4x4ValueType::minus(const QMatrix4x4 &m) const
736{
737 return v - m;
738}
739
740QVector4D QQuickMatrix4x4ValueType::row(int n) const
741{
742 return v.row(index: n);
743}
744
745QVector4D QQuickMatrix4x4ValueType::column(int m) const
746{
747 return v.column(index: m);
748}
749
750qreal QQuickMatrix4x4ValueType::determinant() const
751{
752 return v.determinant();
753}
754
755QMatrix4x4 QQuickMatrix4x4ValueType::inverted() const
756{
757 return v.inverted();
758}
759
760QMatrix4x4 QQuickMatrix4x4ValueType::transposed() const
761{
762 return v.transposed();
763}
764
765QPointF QQuickMatrix4x4ValueType::map(const QPointF p) const
766{
767 return v.map(point: p);
768}
769
770QRectF QQuickMatrix4x4ValueType::mapRect(const QRectF r) const
771{
772 return v.mapRect(rect: r);
773}
774
775bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const
776{
777 qreal absEps = qAbs(t: epsilon);
778 for (int i = 0; i < 4; ++i) {
779 for (int j = 0; j < 4; ++j) {
780 if (qAbs(t: v(i,j) - m(i,j)) > absEps) {
781 return false;
782 }
783 }
784 }
785 return true;
786}
787
788bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m) const
789{
790 return qFuzzyCompare(m1: v, m2: m);
791}
792
793template<typename T>
794void setFontProperty(QFont &font, void (QFont::*setter)(T value), QString name,
795 const QJSValue &params, bool *ok)
796{
797 const QJSValue value = params.property(name);
798
799 if constexpr (std::is_same_v<T, bool>) {
800 if (value.isBool()) {
801 (font.*setter)(value.toBool());
802 *ok = true;
803 }
804 } else if constexpr (std::is_same_v<
805 typename std::remove_cv<typename std::remove_reference<T>::type>::type,
806 QString>) {
807 if (value.isString()) {
808 (font.*setter)(value.toString());
809 *ok = true;
810 }
811 } else if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
812 if (value.isNumber()) {
813 (font.*setter)(T(value.toInt()));
814 *ok = true;
815 }
816 } else if constexpr (std::is_floating_point_v<T>) {
817 if (value.isNumber()) {
818 (font.*setter)(value.toNumber());
819 *ok = true;
820 }
821 }
822}
823
824QVariant QQuickFontValueType::create(const QJSValue &params)
825{
826 if (!params.isObject())
827 return QVariant();
828
829 bool ok = false;
830 QFont ret;
831
832 setFontProperty(font&: ret, setter: &QFont::setBold, QStringLiteral("bold"), params, ok: &ok);
833 setFontProperty(font&: ret, setter: &QFont::setCapitalization, QStringLiteral("capitalization"), params, ok: &ok);
834 setFontProperty(font&: ret, setter: &QFont::setFamily, QStringLiteral("family"), params, ok: &ok);
835 setFontProperty(font&: ret, setter: &QFont::setItalic, QStringLiteral("italic"), params, ok: &ok);
836 setFontProperty(font&: ret, setter: &QFont::setPixelSize, QStringLiteral("pixelSize"), params, ok: &ok);
837 setFontProperty(font&: ret, setter: &QFont::setPointSize, QStringLiteral("pointSize"), params, ok: &ok);
838 setFontProperty(font&: ret, setter: &QFont::setStrikeOut, QStringLiteral("strikeout"), params, ok: &ok);
839 setFontProperty(font&: ret, setter: &QFont::setUnderline, QStringLiteral("underline"), params, ok: &ok);
840 setFontProperty(font&: ret, setter: &QFont::setWeight, QStringLiteral("weight"), params, ok: &ok);
841 setFontProperty(font&: ret, setter: &QFont::setWordSpacing, QStringLiteral("wordSpacing"), params, ok: &ok);
842 setFontProperty(font&: ret, setter: &QFont::setHintingPreference, QStringLiteral("hintingPreference"), params, ok: &ok);
843 setFontProperty(font&: ret, setter: &QFont::setKerning, QStringLiteral("kerning"), params, ok: &ok);
844
845 const QJSValue vlspac = params.property(QStringLiteral("letterSpacing"));
846 if (vlspac.isNumber()) {
847 ret.setLetterSpacing(type: QFont::AbsoluteSpacing, spacing: vlspac.toNumber());
848 ok = true;
849 }
850
851 const QJSValue vshaping = params.property(QStringLiteral("preferShaping"));
852 if (vshaping.isBool()) {
853 const bool enable = vshaping.toBool();
854 const QFont::StyleStrategy strategy = ret.styleStrategy();
855 if (enable)
856 ret.setStyleStrategy(QFont::StyleStrategy(strategy & ~QFont::PreferNoShaping));
857 else
858 ret.setStyleStrategy(QFont::StyleStrategy(strategy | QFont::PreferNoShaping));
859 ok = true;
860 }
861
862 return ok ? ret : QVariant();
863}
864
865QString QQuickFontValueType::toString() const
866{
867 return QString(QLatin1String("QFont(%1)")).arg(a: v.toString());
868}
869
870QString QQuickFontValueType::family() const
871{
872 return v.family();
873}
874
875void QQuickFontValueType::setFamily(const QString &family)
876{
877 v.setFamily(family);
878}
879
880QString QQuickFontValueType::styleName() const
881{
882 return v.styleName();
883}
884
885void QQuickFontValueType::setStyleName(const QString &style)
886{
887 v.setStyleName(style);
888}
889
890bool QQuickFontValueType::bold() const
891{
892 return v.bold();
893}
894
895void QQuickFontValueType::setBold(bool b)
896{
897 v.setBold(b);
898}
899
900int QQuickFontValueType::weight() const
901{
902 return v.weight();
903}
904
905void QQuickFontValueType::setWeight(int w)
906{
907 v.setWeight(QFont::Weight(w));
908}
909
910bool QQuickFontValueType::italic() const
911{
912 return v.italic();
913}
914
915void QQuickFontValueType::setItalic(bool b)
916{
917 v.setItalic(b);
918}
919
920bool QQuickFontValueType::underline() const
921{
922 return v.underline();
923}
924
925void QQuickFontValueType::setUnderline(bool b)
926{
927 v.setUnderline(b);
928}
929
930bool QQuickFontValueType::overline() const
931{
932 return v.overline();
933}
934
935void QQuickFontValueType::setOverline(bool b)
936{
937 v.setOverline(b);
938}
939
940bool QQuickFontValueType::strikeout() const
941{
942 return v.strikeOut();
943}
944
945void QQuickFontValueType::setStrikeout(bool b)
946{
947 v.setStrikeOut(b);
948}
949
950qreal QQuickFontValueType::pointSize() const
951{
952 if (v.pointSizeF() == -1) {
953 return v.pixelSize() * qreal(72.) / qreal(qt_defaultDpi());
954 }
955 return v.pointSizeF();
956}
957
958void QQuickFontValueType::setPointSize(qreal size)
959{
960 if ((v.resolveMask() & QFont::SizeResolved) && v.pixelSize() != -1) {
961 qWarning() << "Both point size and pixel size set. Using pixel size.";
962 return;
963 }
964
965 if (size >= 0.0) {
966 v.setPointSizeF(size);
967 }
968}
969
970int QQuickFontValueType::pixelSize() const
971{
972 if (v.pixelSize() == -1) {
973 return (v.pointSizeF() * qt_defaultDpi()) / qreal(72.);
974 }
975 return v.pixelSize();
976}
977
978void QQuickFontValueType::setPixelSize(int size)
979{
980 if (size >0) {
981 if ((v.resolveMask() & QFont::SizeResolved) && v.pointSizeF() != -1)
982 qWarning() << "Both point size and pixel size set. Using pixel size.";
983 v.setPixelSize(size);
984 }
985}
986
987QQuickFontEnums::Capitalization QQuickFontValueType::capitalization() const
988{
989 return (QQuickFontEnums::Capitalization)v.capitalization();
990}
991
992void QQuickFontValueType::setCapitalization(QQuickFontEnums::Capitalization c)
993{
994 v.setCapitalization((QFont::Capitalization)c);
995}
996
997qreal QQuickFontValueType::letterSpacing() const
998{
999 return v.letterSpacing();
1000}
1001
1002void QQuickFontValueType::setLetterSpacing(qreal size)
1003{
1004 v.setLetterSpacing(type: QFont::AbsoluteSpacing, spacing: size);
1005}
1006
1007qreal QQuickFontValueType::wordSpacing() const
1008{
1009 return v.wordSpacing();
1010}
1011
1012void QQuickFontValueType::setWordSpacing(qreal size)
1013{
1014 v.setWordSpacing(size);
1015}
1016
1017QQuickFontEnums::HintingPreference QQuickFontValueType::hintingPreference() const
1018{
1019 return QQuickFontEnums::HintingPreference(v.hintingPreference());
1020}
1021
1022void QQuickFontValueType::setHintingPreference(QQuickFontEnums::HintingPreference hintingPreference)
1023{
1024 v.setHintingPreference(QFont::HintingPreference(hintingPreference));
1025}
1026
1027bool QQuickFontValueType::kerning() const
1028{
1029 return v.kerning();
1030}
1031
1032void QQuickFontValueType::setKerning(bool b)
1033{
1034 v.setKerning(b);
1035}
1036
1037bool QQuickFontValueType::preferShaping() const
1038{
1039 return (v.styleStrategy() & QFont::PreferNoShaping) == 0;
1040}
1041
1042void QQuickFontValueType::setPreferShaping(bool enable)
1043{
1044 if (enable)
1045 v.setStyleStrategy(static_cast<QFont::StyleStrategy>(v.styleStrategy() & ~QFont::PreferNoShaping));
1046 else
1047 v.setStyleStrategy(static_cast<QFont::StyleStrategy>(v.styleStrategy() | QFont::PreferNoShaping));
1048}
1049
1050void QQuickFontValueType::setFeatures(const QVariantMap &features)
1051{
1052 v.clearFeatures();
1053 for (auto it = features.constBegin(); it != features.constEnd(); ++it) {
1054 QString featureName = it.key();
1055 quint32 tag = QFont::stringToTag(tagString: featureName.toUtf8());
1056 if (tag == 0) {
1057 qWarning() << "Invalid font feature" << featureName << "ignored";
1058 continue;
1059 }
1060
1061 bool ok;
1062 quint32 value = it.value().toUInt(ok: &ok);
1063 if (!ok) {
1064 qWarning() << "Font feature value" << it.value() << "is not an integer.";
1065 continue;
1066 }
1067
1068 v.setFeature(tag, value);
1069 }
1070}
1071
1072QVariantMap QQuickFontValueType::features() const
1073{
1074 QVariantMap ret;
1075 for (quint32 tag : v.featureTags()) {
1076 QString featureName = QString::fromUtf8(ba: QFont::tagToString(tag));
1077
1078 ret.insert(key: featureName, value: v.featureValue(tag));
1079 }
1080
1081 return ret;
1082}
1083
1084QVariant QQuickColorSpaceValueType::create(const QJSValue &params)
1085{
1086 if (!params.isObject())
1087 return QVariant();
1088
1089
1090 const QJSValue vName = params.property(QStringLiteral("namedColorSpace"));
1091 if (vName.isNumber())
1092 return QColorSpace((QColorSpace::NamedColorSpace)vName.toInt());
1093
1094 const QJSValue vPri = params.property(QStringLiteral("primaries"));
1095 const QJSValue vTra = params.property(QStringLiteral("transferFunction"));
1096 if (!vPri.isNumber() || !vTra.isNumber())
1097 return QVariant();
1098
1099 QColorSpace::Primaries pri = static_cast<QColorSpace::Primaries>(vPri.toInt());
1100 QColorSpace::TransferFunction tra = static_cast<QColorSpace::TransferFunction>(vTra.toInt());
1101 float gamma = 0.0f;
1102 if (tra == QColorSpace::TransferFunction::Gamma) {
1103 const QJSValue vGam = params.property(QStringLiteral("gamma"));
1104 if (!vGam.isNumber())
1105 return QVariant();
1106 gamma = vGam.toNumber();
1107 }
1108
1109 return QColorSpace(pri, tra, gamma);
1110}
1111
1112QQuickColorSpaceEnums::NamedColorSpace QQuickColorSpaceValueType::namedColorSpace() const noexcept
1113{
1114 if (const auto *p = QColorSpacePrivate::get(colorSpace: v))
1115 return (QQuickColorSpaceEnums::NamedColorSpace)p->namedColorSpace;
1116 return QQuickColorSpaceEnums::Unknown;
1117}
1118void QQuickColorSpaceValueType::setNamedColorSpace(QQuickColorSpaceEnums::NamedColorSpace namedColorSpace)
1119{
1120 v = { (QColorSpace::NamedColorSpace)namedColorSpace };
1121}
1122
1123QQuickColorSpaceEnums::Primaries QQuickColorSpaceValueType::primaries() const noexcept
1124{
1125 return (QQuickColorSpaceEnums::Primaries)v.primaries();
1126}
1127
1128void QQuickColorSpaceValueType::setPrimaries(QQuickColorSpaceEnums::Primaries primariesId)
1129{
1130 v.setPrimaries((QColorSpace::Primaries)primariesId);
1131}
1132
1133QQuickColorSpaceEnums::TransferFunction QQuickColorSpaceValueType::transferFunction() const noexcept
1134{
1135 return (QQuickColorSpaceEnums::TransferFunction)v.transferFunction();
1136}
1137
1138void QQuickColorSpaceValueType::setTransferFunction(QQuickColorSpaceEnums::TransferFunction transferFunction)
1139{
1140 v.setTransferFunction(transferFunction: (QColorSpace::TransferFunction)transferFunction, gamma: v.gamma());
1141}
1142
1143float QQuickColorSpaceValueType::gamma() const noexcept
1144{
1145 return v.gamma();
1146}
1147
1148void QQuickColorSpaceValueType::setGamma(float gamma)
1149{
1150 v.setTransferFunction(transferFunction: v.transferFunction(), gamma);
1151}
1152
1153QT_END_NAMESPACE
1154
1155#include "moc_qquickvaluetypes_p.cpp"
1156

source code of qtdeclarative/src/quick/util/qquickvaluetypes.cpp