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
qsvgstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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// Qt-Security score:significant reason:default
4
5
6#include "qsvgstyle_p.h"
7
8#include "qsvgfont_p.h"
9#include "qsvgnode_p.h"
10#include "private/qsvganimate_p.h"
11#include "qsvgdocument_p.h"
12
13#include "qpainter.h"
14#include "qcolor.h"
15#include "qdebug.h"
16
18
19QSvgExtraStates::QSvgExtraStates()
20 : fillOpacity(1.0),
21 strokeOpacity(1.0),
22 svgFont(0),
23 textAnchor(Qt::AlignLeft),
24 fontWeight(QFont::Normal),
25 fillRule(Qt::WindingFill),
26 strokeDashOffset(0),
27 vectorEffect(false),
28 imageRendering(QSvgQualityStyle::ImageRenderingAuto)
29{
30}
31
32QSvgStyleProperty::~QSvgStyleProperty()
33{
34}
35
36void QSvgPaintStyleProperty::apply(QPainter *, const QSvgNode *, QSvgExtraStates &)
37{
38 Q_ASSERT(!"This should not be called!");
39}
40
41void QSvgPaintStyleProperty::revert(QPainter *, QSvgExtraStates &)
42{
43 Q_ASSERT(!"This should not be called!");
44}
45
46
47QSvgQualityStyle::QSvgQualityStyle(int color)
48 : m_imageRendering(QSvgQualityStyle::ImageRenderingAuto)
49 , m_oldImageRendering(QSvgQualityStyle::ImageRenderingAuto)
50 , m_imageRenderingSet(0)
51{
52 Q_UNUSED(color);
53}
54
55void QSvgQualityStyle::setImageRendering(ImageRendering hint) {
56 m_imageRendering = hint;
57 m_imageRenderingSet = 1;
58}
59
60void QSvgQualityStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &states)
61{
62 m_oldImageRendering = states.imageRendering;
63 if (m_imageRenderingSet) {
64 states.imageRendering = m_imageRendering;
65 }
66 if (m_imageRenderingSet) {
67 bool smooth = false;
68 if (m_imageRendering == ImageRenderingAuto)
69 // auto (the spec says to prefer quality)
70 smooth = true;
71 else
72 smooth = (m_imageRendering == ImageRenderingOptimizeQuality);
73 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
74 }
75}
76
77void QSvgQualityStyle::revert(QPainter *p, QSvgExtraStates &states)
78{
79 if (m_imageRenderingSet) {
80 states.imageRendering = m_oldImageRendering;
81 bool smooth = false;
82 if (m_oldImageRendering == ImageRenderingAuto)
83 smooth = true;
84 else
85 smooth = (m_oldImageRendering == ImageRenderingOptimizeQuality);
86 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
87 }
88}
89
90QSvgFillStyle::QSvgFillStyle()
91 : m_style(0)
92 , m_fillRule(Qt::WindingFill)
93 , m_oldFillRule(Qt::WindingFill)
94 , m_fillOpacity(1.0)
95 , m_oldFillOpacity(0)
96 , m_paintStyleResolved(1)
97 , m_fillRuleSet(0)
98 , m_fillOpacitySet(0)
99 , m_fillSet(0)
100{
101}
102
103void QSvgFillStyle::setFillRule(Qt::FillRule f)
104{
105 m_fillRuleSet = 1;
106 m_fillRule = f;
107}
108
109void QSvgFillStyle::setFillOpacity(qreal opacity)
110{
111 m_fillOpacitySet = 1;
112 m_fillOpacity = opacity;
113}
114
115void QSvgFillStyle::setFillStyle(QSvgPaintStyleProperty* style)
116{
117 m_style = style;
118 m_fillSet = 1;
119}
120
121void QSvgFillStyle::setBrush(QBrush brush)
122{
123 m_fill = std::move(brush);
124 m_style = nullptr;
125 m_fillSet = 1;
126}
127
128void QSvgFillStyle::apply(QPainter *p, const QSvgNode *n, QSvgExtraStates &states)
129{
130 m_oldFill = p->brush();
131 m_oldFillRule = states.fillRule;
132 m_oldFillOpacity = states.fillOpacity;
133
134 if (m_fillRuleSet)
135 states.fillRule = m_fillRule;
136 if (m_fillSet) {
137 if (m_style)
138 p->setBrush(m_style->brush(p, n, states));
139 else
140 p->setBrush(m_fill);
141 }
142 if (m_fillOpacitySet)
143 states.fillOpacity = m_fillOpacity;
144}
145
146void QSvgFillStyle::revert(QPainter *p, QSvgExtraStates &states)
147{
148 if (m_fillOpacitySet)
149 states.fillOpacity = m_oldFillOpacity;
150 if (m_fillSet)
151 p->setBrush(m_oldFill);
152 if (m_fillRuleSet)
153 states.fillRule = m_oldFillRule;
154}
155
156QSvgViewportFillStyle::QSvgViewportFillStyle(const QBrush &brush)
157 : m_viewportFill(brush)
158{
159}
160
161void QSvgViewportFillStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &)
162{
163 m_oldFill = p->brush();
164 p->setBrush(m_viewportFill);
165}
166
167void QSvgViewportFillStyle::revert(QPainter *p, QSvgExtraStates &)
168{
169 p->setBrush(m_oldFill);
170}
171
172QSvgFontStyle::QSvgFontStyle(QSvgFont *font, QSvgDocument *doc)
173 : m_svgFont(font)
174 , m_doc(doc)
175 , m_familySet(0)
176 , m_sizeSet(0)
177 , m_styleSet(0)
178 , m_variantSet(0)
179 , m_weightSet(0)
180 , m_textAnchorSet(0)
181{
182}
183
184QSvgFontStyle::QSvgFontStyle()
185 : m_svgFont(0)
186 , m_doc(0)
187 , m_familySet(0)
188 , m_sizeSet(0)
189 , m_styleSet(0)
190 , m_variantSet(0)
191 , m_weightSet(0)
192 , m_textAnchorSet(0)
193{
194}
195
196void QSvgFontStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &states)
197{
198 m_oldQFont = p->font();
199 m_oldSvgFont = states.svgFont;
200 m_oldTextAnchor = states.textAnchor;
201 m_oldWeight = states.fontWeight;
202
203 if (m_textAnchorSet)
204 states.textAnchor = m_textAnchor;
205
206 QFont font = m_oldQFont;
207 if (m_familySet) {
208 states.svgFont = m_svgFont;
209 font.setFamilies(m_qfont.families());
210 }
211
212 if (m_sizeSet)
213 font.setPointSizeF(m_qfont.pointSizeF());
214
215 if (m_styleSet)
216 font.setStyle(m_qfont.style());
217
218 if (m_variantSet)
219 font.setCapitalization(m_qfont.capitalization());
220
221 if (m_weightSet) {
222 if (m_weight == BOLDER) {
223 states.fontWeight = qMin(states.fontWeight + 100, static_cast<int>(QFont::Black));
224 } else if (m_weight == LIGHTER) {
225 states.fontWeight = qMax(states.fontWeight - 100, static_cast<int>(QFont::Thin));
226 } else {
227 states.fontWeight = m_weight;
228 }
229 font.setWeight(QFont::Weight(qBound(static_cast<int>(QFont::Weight::Thin),
230 states.fontWeight,
231 static_cast<int>(QFont::Weight::Black))));
232 }
233
234 p->setFont(font);
235}
236
237void QSvgFontStyle::revert(QPainter *p, QSvgExtraStates &states)
238{
239 p->setFont(m_oldQFont);
240 states.svgFont = m_oldSvgFont;
241 states.textAnchor = m_oldTextAnchor;
242 states.fontWeight = m_oldWeight;
243}
244
245QSvgStrokeStyle::QSvgStrokeStyle()
246 : m_strokeOpacity(1.0)
247 , m_oldStrokeOpacity(0.0)
248 , m_strokeDashOffset(0)
249 , m_oldStrokeDashOffset(0)
250 , m_style(0)
251 , m_paintStyleResolved(1)
252 , m_vectorEffect(0)
253 , m_oldVectorEffect(0)
254 , m_strokeSet(0)
255 , m_strokeDashArraySet(0)
256 , m_strokeDashOffsetSet(0)
257 , m_strokeLineCapSet(0)
258 , m_strokeLineJoinSet(0)
259 , m_strokeMiterLimitSet(0)
260 , m_strokeOpacitySet(0)
261 , m_strokeWidthSet(0)
262 , m_vectorEffectSet(0)
263{
264}
265
266void QSvgStrokeStyle::apply(QPainter *p, const QSvgNode *n, QSvgExtraStates &states)
267{
268 m_oldStroke = p->pen();
269 m_oldStrokeOpacity = states.strokeOpacity;
270 m_oldStrokeDashOffset = states.strokeDashOffset;
271 m_oldVectorEffect = states.vectorEffect;
272
273 QPen pen = p->pen();
274
275 qreal oldWidth = pen.widthF();
276 qreal width = m_stroke.widthF();
277 if (oldWidth == 0)
278 oldWidth = 1;
279 if (width == 0)
280 width = 1;
281 qreal scale = oldWidth / width;
282
283 if (m_strokeOpacitySet)
284 states.strokeOpacity = m_strokeOpacity;
285
286 if (m_vectorEffectSet)
287 states.vectorEffect = m_vectorEffect;
288
289 if (m_strokeSet) {
290 if (m_style)
291 pen.setBrush(m_style->brush(p, n, states));
292 else
293 pen.setBrush(m_stroke.brush());
294 }
295
296 if (m_strokeWidthSet)
297 pen.setWidthF(m_stroke.widthF());
298
299 bool setDashOffsetNeeded = false;
300
301 if (m_strokeDashOffsetSet) {
302 states.strokeDashOffset = m_strokeDashOffset;
303 setDashOffsetNeeded = true;
304 }
305
306 if (m_strokeDashArraySet) {
307 if (m_stroke.style() == Qt::SolidLine) {
308 pen.setStyle(Qt::SolidLine);
309 } else if (m_strokeWidthSet || oldWidth == 1) {
310 // If both width and dash array was set, the dash array is already scaled correctly.
311 pen.setDashPattern(m_stroke.dashPattern());
312 setDashOffsetNeeded = true;
313 } else {
314 // If dash array was set, but not the width, the dash array has to be scaled with respect to the old width.
315 QList<qreal> dashes = m_stroke.dashPattern();
316 for (int i = 0; i < dashes.size(); ++i)
317 dashes[i] /= oldWidth;
318 pen.setDashPattern(dashes);
319 setDashOffsetNeeded = true;
320 }
321 } else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
322 // If the width was set, but not the dash array, the old dash array must be scaled with respect to the new width.
323 QList<qreal> dashes = pen.dashPattern();
324 for (int i = 0; i < dashes.size(); ++i)
325 dashes[i] *= scale;
326 pen.setDashPattern(dashes);
327 setDashOffsetNeeded = true;
328 }
329
330 if (m_strokeLineCapSet)
331 pen.setCapStyle(m_stroke.capStyle());
332 if (m_strokeLineJoinSet)
333 pen.setJoinStyle(m_stroke.joinStyle());
334 if (m_strokeMiterLimitSet)
335 pen.setMiterLimit(m_stroke.miterLimit());
336
337 // You can have dash offset on solid strokes in SVG files, but not in Qt.
338 // QPen::setDashOffset() will set the pen style to Qt::CustomDashLine,
339 // so don't call the method if the pen is solid.
340 if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
341 qreal currentWidth = pen.widthF();
342 if (currentWidth == 0)
343 currentWidth = 1;
344 pen.setDashOffset(states.strokeDashOffset / currentWidth);
345 }
346
347 pen.setCosmetic(states.vectorEffect);
348
349 p->setPen(pen);
350}
351
352void QSvgStrokeStyle::revert(QPainter *p, QSvgExtraStates &states)
353{
354 p->setPen(m_oldStroke);
355 states.strokeOpacity = m_oldStrokeOpacity;
356 states.strokeDashOffset = m_oldStrokeDashOffset;
357 states.vectorEffect = m_oldVectorEffect;
358}
359
360void QSvgStrokeStyle::setDashArray(const QList<qreal> &dashes)
361{
362 if (m_strokeWidthSet) {
363 QList<qreal> d = dashes;
364 qreal w = m_stroke.widthF();
365 if (w != 0 && w != 1) {
366 for (int i = 0; i < d.size(); ++i)
367 d[i] /= w;
368 }
369 m_stroke.setDashPattern(d);
370 } else {
371 m_stroke.setDashPattern(dashes);
372 }
373 m_strokeDashArraySet = 1;
374}
375
376QSvgSolidColorStyle::QSvgSolidColorStyle(const QColor &color)
377 : m_solidColor(color)
378{
379}
380
381QSvgGradientStyle::QSvgGradientStyle(QGradient *grad)
382 : m_gradient(grad), m_gradientStopsSet(false)
383{
384}
385
386QBrush QSvgGradientStyle::brush(QPainter *, const QSvgNode *, QSvgExtraStates &)
387{
388 if (!m_link.isEmpty()) {
389 resolveStops();
390 }
391
392 // If the gradient is marked as empty, insert transparent black
393 if (!m_gradientStopsSet) {
394 m_gradient->setStops(QGradientStops() << QGradientStop(0.0, QColor(0, 0, 0, 0)));
395 m_gradientStopsSet = true;
396 }
397
398 QBrush b(*m_gradient);
399
400 if (!m_transform.isIdentity())
401 b.setTransform(m_transform);
402
403 return b;
404}
405
406
407void QSvgGradientStyle::setTransform(const QTransform &transform)
408{
409 m_transform = transform;
410}
411
412QSvgPatternStyle::QSvgPatternStyle(QSvgPattern *pattern)
413 : m_pattern(pattern)
414{
415
416}
417
418QBrush QSvgPatternStyle::brush(QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
419{
420 QBrush b(m_pattern->patternImage(p, states, node));
421 b.setTransform(m_pattern->appliedTransform());
422 return b;
423}
424
425QSvgTransformStyle::QSvgTransformStyle(const QTransform &trans)
426 : m_transform(trans)
427{
428}
429
430void QSvgTransformStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &)
431{
432 m_oldWorldTransform.push(p->worldTransform());
433 p->setWorldTransform(m_transform, true);
434}
435
436void QSvgTransformStyle::revert(QPainter *p, QSvgExtraStates &)
437{
438 p->setWorldTransform(m_oldWorldTransform.pop(), false /* don't combine */);
439}
440
441QSvgStyleProperty::Type QSvgQualityStyle::type() const
442{
443 return QUALITY;
444}
445
446QSvgStyleProperty::Type QSvgFillStyle::type() const
447{
448 return FILL;
449}
450
451QSvgStyleProperty::Type QSvgViewportFillStyle::type() const
452{
453 return VIEWPORT_FILL;
454}
455
456QSvgStyleProperty::Type QSvgFontStyle::type() const
457{
458 return FONT;
459}
460
461QSvgStyleProperty::Type QSvgStrokeStyle::type() const
462{
463 return STROKE;
464}
465
466QSvgStyleProperty::Type QSvgSolidColorStyle::type() const
467{
468 return SOLID_COLOR;
469}
470
471QSvgStyleProperty::Type QSvgGradientStyle::type() const
472{
473 return GRADIENT;
474}
475
476QSvgStyleProperty::Type QSvgPatternStyle::type() const
477{
478 return PATTERN;
479}
480
481QSvgStyleProperty::Type QSvgTransformStyle::type() const
482{
483 return TRANSFORM;
484}
485
486
487QSvgCompOpStyle::QSvgCompOpStyle(QPainter::CompositionMode mode)
488 : m_mode(mode)
489{
490
491}
492
493void QSvgCompOpStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &)
494{
495 m_oldMode = p->compositionMode();
496 p->setCompositionMode(m_mode);
497}
498
499void QSvgCompOpStyle::revert(QPainter *p, QSvgExtraStates &)
500{
501 p->setCompositionMode(m_oldMode);
502}
503
504QSvgStyleProperty::Type QSvgCompOpStyle::type() const
505{
506 return COMP_OP;
507}
508
509void QSvgOffsetStyle::apply(QPainter *, const QSvgNode *, QSvgExtraStates &)
510{
511}
512
513void QSvgOffsetStyle::revert(QPainter *, QSvgExtraStates &)
514{
515}
516
517QSvgStyleProperty::Type QSvgOffsetStyle::type() const
518{
519 return OFFSET;
520}
521
522QSvgOpacityStyle::QSvgOpacityStyle(qreal opacity)
523 : m_opacity(opacity), m_oldOpacity(0)
524{
525
526}
527
528void QSvgOpacityStyle::apply(QPainter *p, const QSvgNode *, QSvgExtraStates &)
529{
530 m_oldOpacity = p->opacity();
531 p->setOpacity(m_opacity * m_oldOpacity);
532}
533
534void QSvgOpacityStyle::revert(QPainter *p, QSvgExtraStates &)
535{
536 p->setOpacity(m_oldOpacity);
537}
538
539QSvgStyleProperty::Type QSvgOpacityStyle::type() const
540{
541 return OPACITY;
542}
543
544void QSvgGradientStyle::setStopLink(const QString &link, QSvgDocument *doc)
545{
546 m_link = link;
547 m_doc = doc;
548}
549
550void QSvgGradientStyle::resolveStops()
551{
552 QStringList visited;
553 resolveStops_helper(&visited);
554}
555
556void QSvgGradientStyle::resolveStops_helper(QStringList *visited)
557{
558 if (!m_link.isEmpty() && m_doc) {
559 QSvgStyleProperty *prop = m_doc->styleProperty(m_link);
560 if (prop && !visited->contains(m_link)) {
561 visited->append(m_link);
562 if (prop->type() == QSvgStyleProperty::GRADIENT) {
563 QSvgGradientStyle *st =
564 static_cast<QSvgGradientStyle*>(prop);
565 st->resolveStops_helper(visited);
566 m_gradient->setStops(st->qgradient()->stops());
567 m_gradientStopsSet = st->gradientStopsSet();
568 }
569 } else {
570 qWarning("Could not resolve property : %s", qPrintable(m_link));
571 }
572 m_link = QString();
573 }
574}
575
576QSvgStaticStyle::QSvgStaticStyle()
577 : quality(0)
578 , fill(0)
579 , viewportFill(0)
580 , font(0)
581 , stroke(0)
582 , solidColor(0)
583 , gradient(0)
584 , pattern(0)
585 , transform(0)
586 , opacity(0)
587 , compop(0)
588{
589}
590
591QSvgStaticStyle::~QSvgStaticStyle()
592{
593}
594
595void QSvgStaticStyle::apply(QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
596{
597 if (quality) {
598 quality->apply(p, node, states);
599 }
600
601 if (fill) {
602 fill->apply(p, node, states);
603 }
604
605 if (viewportFill) {
606 viewportFill->apply(p, node, states);
607 }
608
609 if (font) {
610 font->apply(p, node, states);
611 }
612
613 if (stroke) {
614 stroke->apply(p, node, states);
615 }
616
617 if (transform) {
618 transform->apply(p, node, states);
619 }
620
621 if (opacity) {
622 opacity->apply(p, node, states);
623 }
624
625 if (compop) {
626 compop->apply(p, node, states);
627 }
628}
629
630void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
631{
632 if (quality) {
633 quality->revert(p, states);
634 }
635
636 if (fill) {
637 fill->revert(p, states);
638 }
639
640 if (viewportFill) {
641 viewportFill->revert(p, states);
642 }
643
644 if (font) {
645 font->revert(p, states);
646 }
647
648 if (stroke) {
649 stroke->revert(p, states);
650 }
651
652 if (transform) {
653 transform->revert(p, states);
654 }
655
656 if (opacity) {
657 opacity->revert(p, states);
658 }
659
660 if (compop) {
661 compop->revert(p, states);
662 }
663}
664
665namespace {
666
667QColor sumValue(const QColor &c1, const QColor &c2)
668{
669 QRgb rgb1 = c1.rgba();
670 QRgb rgb2 = c2.rgba();
671 int sumRed = qRed(rgb1) + qRed(rgb2);
672 int sumGreen = qGreen(rgb1) + qGreen(rgb2);
673 int sumBlue = qBlue(rgb1) + qBlue(rgb2);
674
675 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
676 qBound(0, sumGreen, 255),
677 qBound(0, sumBlue, 255),
678 255);
679
680 return QColor(sumRgb);
681}
682
683qreal sumValue(qreal value1, qreal value2)
684{
685 qreal sumValue = value1 + value2;
686 return qBound(0.0, sumValue, 1.0);
687}
688
689}
690
691QSvgAnimatedStyle::QSvgAnimatedStyle()
692{
693}
694
695QSvgAnimatedStyle::~QSvgAnimatedStyle()
696{
697}
698
699void QSvgAnimatedStyle::apply(QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
700{
701 QSharedPointer<QSvgAbstractAnimator> animator = node->document()->animator();
702 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
703
704 savePaintingState(p, node, states);
705 if (nodeAnims.isEmpty())
706 return;
707
708 QSvgStyleState currentStyle = m_static;
709 for (auto anim : nodeAnims) {
710 if (!anim->isActive())
711 continue;
712
713 fetchStyleState(anim, currentStyle);
714 }
715
716 applyStyle(p, states, currentStyle);
717}
718
719void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
720{
721 p->setWorldTransform(m_worldTransform);
722 p->setBrush(m_static.fill);
723 p->setPen(m_static.stroke);
724 p->setOpacity(m_static.opacity);
725 states.fillOpacity = m_static.fillOpacity;
726 states.strokeOpacity = m_static.strokeOpacity;
727}
728
729void QSvgAnimatedStyle::savePaintingState(const QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
730{
731 QSvgStaticStyle style = node->style();
732 m_worldTransform = p->worldTransform();
733 if (style.transform)
734 m_static.transform = style.transform->qtransform();
735
736 if (style.offset) {
737 m_static.offsetPath = style.offset->path();
738 m_static.offsetRotateType = style.offset->rotateType();
739 m_static.offsetRotate = style.offset->rotateAngle();
740 }
741
742 m_static.fill = p->brush();
743 m_static.stroke = p->pen();
744 m_static.fillOpacity = states.fillOpacity;
745 m_static.strokeOpacity = states.strokeOpacity;
746 m_static.opacity = p->opacity();
747 m_transformToNode = m_static.transform.inverted() * m_worldTransform;
748}
749
750void QSvgAnimatedStyle::fetchStyleState(const QSvgAbstractAnimation *animation, QSvgStyleState &currentStyle)
751{
752 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ? true :
753 (static_cast<const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
754
755 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
756 for (const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
757 if (property->propertyName() == QStringLiteral("fill")) {
758 QBrush brush = currentStyle.fill;
759 QColor brushColor = brush.color();
760 QColor animatedColor = property->interpolatedValue().value<QColor>();
761 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(brushColor, animatedColor);
762 brush.setColor(sumOrReplaceColor);
763 currentStyle.fill = brush;
764 } else if (property->propertyName() == QStringLiteral("stroke")) {
765 QPen &pen = currentStyle.stroke;
766 QBrush penBrush = pen.brush();
767 QColor penColor = penBrush.color();
768 QColor animatedColor = property->interpolatedValue().value<QColor>();
769 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(penColor, animatedColor);
770 penBrush.setColor(sumOrReplaceColor);
771 penBrush.setStyle(Qt::SolidPattern);
772 pen.setBrush(penBrush);
773 } else if (property->propertyName() == QStringLiteral("transform")) {
774 QTransform animatedTransform = property->interpolatedValue().value<QTransform>();
775 QTransform sumOrReplaceTransform = replace ? animatedTransform : animatedTransform * currentStyle.transform;
776 currentStyle.transform = sumOrReplaceTransform;
777 } else if (property->propertyName() == QStringLiteral("fill-opacity")) {
778 qreal animatedFillOpacity = property->interpolatedValue().value<qreal>();
779 qreal sumOrReplaceOpacity = replace ? animatedFillOpacity : sumValue(currentStyle.fillOpacity, animatedFillOpacity);
780 currentStyle.fillOpacity = sumOrReplaceOpacity;
781 } else if (property->propertyName() == QStringLiteral("stroke-opacity")) {
782 qreal animatedStrokeOpacity = property->interpolatedValue().value<qreal>();
783 qreal sumOrReplaceOpacity = replace ? animatedStrokeOpacity : sumValue(currentStyle.strokeOpacity, animatedStrokeOpacity);
784 currentStyle.strokeOpacity = sumOrReplaceOpacity;
785 } else if (property->propertyName() == QStringLiteral("opacity")) {
786 qreal animatedOpacity = property->interpolatedValue().value<qreal>();
787 qreal sumOrReplaceOpacity = replace ? animatedOpacity : sumValue(currentStyle.opacity, animatedOpacity);
788 currentStyle.opacity = sumOrReplaceOpacity;
789 } else if (property->propertyName() == QStringLiteral("offset-distance")) {
790 qreal offsetDistance = property->interpolatedValue().value<qreal>();
791 currentStyle.offsetDistance = offsetDistance;
792 }
793 }
794}
795
796void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states, const QSvgStyleState &currentStyle)
797{
798 p->setBrush(currentStyle.fill);
799 states.fillOpacity = currentStyle.fillOpacity;
800 p->setPen(currentStyle.stroke);
801 states.strokeOpacity = currentStyle.strokeOpacity;
802 p->setOpacity(currentStyle.opacity);
803
804 QTransform transform = currentStyle.transform;
805 QTransform offset;
806 if (m_static.offsetPath) {
807 qreal offsetDistance = currentStyle.offsetDistance;
808 qreal angle = m_static.offsetPath.value().angleAtPercent(offsetDistance);
809 QPointF position = m_static.offsetPath.value().pointAtPercent(offsetDistance);
810 offset.translate(position.x(), position.y());
811
812 switch (m_static.offsetRotateType) {
813 case QtSvg::OffsetRotateType::Auto:
814 offset.rotate(-angle);
815 break;
816 case QtSvg::OffsetRotateType::Angle:
817 offset.rotate(m_static.offsetRotate);
818 break;
819 case QtSvg::OffsetRotateType::AutoAngle:
820 offset.rotate(m_static.offsetRotate - angle);
821 break;
822 case QtSvg::OffsetRotateType::Reverse:
823 offset.rotate(180 - angle);
824 break;
825 case QtSvg::OffsetRotateType::ReverseAngle:
826 offset.rotate(180 + m_static.offsetRotate - angle);
827 break;
828 }
829 }
830
831 QTransform combinedTransform = transform * offset * m_transformToNode;
832 p->setWorldTransform(combinedTransform);
833}
834
835QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define qPrintable(string)
Definition qstring.h:1683
#define QStringLiteral(str)
Definition qstring.h:1825