10#include "private/qsvganimate_p.h"
19QSvgRefCounted::~QSvgRefCounted()
22QSvgExtraStates::QSvgExtraStates()
26 textAnchor(Qt::AlignLeft),
27 fontWeight(QFont::Normal),
28 fillRule(Qt::WindingFill),
31 imageRendering(QSvgQualityStyle::ImageRenderingAuto)
35QSvgStyleProperty::~QSvgStyleProperty()
38QSvgQualityStyle::QSvgQualityStyle(
int color)
39 : m_imageRendering(QSvgQualityStyle::ImageRenderingAuto)
40 , m_oldImageRendering(QSvgQualityStyle::ImageRenderingAuto)
41 , m_imageRenderingSet(0)
46QSvgQualityStyle::~QSvgQualityStyle()
49void QSvgQualityStyle::setImageRendering(ImageRendering hint) {
50 m_imageRendering = hint;
51 m_imageRenderingSet = 1;
54void QSvgQualityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
56 m_oldImageRendering = states.imageRendering;
57 if (m_imageRenderingSet) {
58 states.imageRendering = m_imageRendering;
60 if (m_imageRenderingSet) {
62 if (m_imageRendering == ImageRenderingAuto)
66 smooth = (m_imageRendering == ImageRenderingOptimizeQuality);
67 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
71void QSvgQualityStyle::revert(QPainter *p, QSvgExtraStates &states)
73 if (m_imageRenderingSet) {
74 states.imageRendering = m_oldImageRendering;
76 if (m_oldImageRendering == ImageRenderingAuto)
79 smooth = (m_oldImageRendering == ImageRenderingOptimizeQuality);
80 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
84QSvgFillStyle::QSvgFillStyle()
91QSvgFillStyle::~QSvgFillStyle()
94void QSvgFillStyle::setFillRule(Qt::FillRule f)
100void QSvgFillStyle::setFillOpacity(qreal opacity)
102 m_fillOpacitySet = 1;
103 m_fillOpacity = opacity;
106void QSvgFillStyle::setPaintServer(QSvgPaintServerSharedPtr paintServer)
108 m_paintServer = paintServer;
112void QSvgFillStyle::setBrush(QBrush brush)
114 m_fill = std::move(brush);
115 m_paintServer.reset();
119void QSvgFillStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
121 m_oldFill = p->brush();
122 m_oldFillRule = states.fillRule;
123 m_oldFillOpacity = states.fillOpacity;
126 states.fillRule = m_fillRule;
129 p->setBrush(m_paintServer->brush(p, n, states));
133 if (m_fillOpacitySet)
134 states.fillOpacity = m_fillOpacity;
137void QSvgFillStyle::revert(QPainter *p, QSvgExtraStates &states)
139 if (m_fillOpacitySet)
140 states.fillOpacity = m_oldFillOpacity;
142 p->setBrush(m_oldFill);
144 states.fillRule = m_oldFillRule;
147QSvgViewportFillStyle::QSvgViewportFillStyle(
const QBrush &brush)
148 : m_viewportFill(brush)
152QSvgViewportFillStyle::~QSvgViewportFillStyle()
155void QSvgViewportFillStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
157 m_oldFill = p->brush();
158 p->setBrush(m_viewportFill);
161void QSvgViewportFillStyle::revert(QPainter *p, QSvgExtraStates &)
163 p->setBrush(m_oldFill);
166QSvgFontStyle::QSvgFontStyle(QSvgFont *font, QSvgDocument *doc)
178QSvgFontStyle::QSvgFontStyle()
190QSvgFontStyle::~QSvgFontStyle()
193void QSvgFontStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
195 m_oldQFont = p->font();
196 m_oldSvgFont = states.svgFont;
197 m_oldTextAnchor = states.textAnchor;
198 m_oldWeight = states.fontWeight;
201 states.textAnchor = m_textAnchor;
203 QFont font = m_oldQFont;
205 states.svgFont = m_svgFont;
206 font.setFamilies(m_qfont.families());
210 font.setPointSizeF(m_qfont.pointSizeF());
213 font.setStyle(m_qfont.style());
216 font.setCapitalization(m_qfont.capitalization());
219 if (m_weight == BOLDER) {
220 states.fontWeight = qMin(states.fontWeight + 100,
static_cast<
int>(QFont::Black));
221 }
else if (m_weight == LIGHTER) {
222 states.fontWeight = qMax(states.fontWeight - 100,
static_cast<
int>(QFont::Thin));
224 states.fontWeight = m_weight;
226 font.setWeight(QFont::Weight(qBound(
static_cast<
int>(QFont::Weight::Thin),
228 static_cast<
int>(QFont::Weight::Black))));
234void QSvgFontStyle::revert(QPainter *p, QSvgExtraStates &states)
236 p->setFont(m_oldQFont);
237 states.svgFont = m_oldSvgFont;
238 states.textAnchor = m_oldTextAnchor;
239 states.fontWeight = m_oldWeight;
242QSvgStrokeStyle::QSvgStrokeStyle()
244 , m_oldVectorEffect(0)
246 , m_strokeDashArraySet(0)
247 , m_strokeDashOffsetSet(0)
248 , m_strokeLineCapSet(0)
249 , m_strokeLineJoinSet(0)
250 , m_strokeMiterLimitSet(0)
251 , m_strokeOpacitySet(0)
252 , m_strokeWidthSet(0)
253 , m_vectorEffectSet(0)
257QSvgStrokeStyle::~QSvgStrokeStyle()
260void QSvgStrokeStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
262 m_oldStroke = p->pen();
263 m_oldStrokeOpacity = states.strokeOpacity;
264 m_oldStrokeDashOffset = states.strokeDashOffset;
265 m_oldVectorEffect = states.vectorEffect;
269 qreal oldWidth = pen.widthF();
270 qreal width = m_stroke.widthF();
275 qreal scale = oldWidth / width;
277 if (m_strokeOpacitySet)
278 states.strokeOpacity = m_strokeOpacity;
280 if (m_vectorEffectSet)
281 states.vectorEffect = m_vectorEffect;
285 pen.setBrush(m_paintServer->brush(p, n, states));
287 pen.setBrush(m_stroke.brush());
290 if (m_strokeWidthSet)
291 pen.setWidthF(m_stroke.widthF());
293 bool setDashOffsetNeeded =
false;
295 if (m_strokeDashOffsetSet) {
296 states.strokeDashOffset = m_strokeDashOffset;
297 setDashOffsetNeeded =
true;
300 if (m_strokeDashArraySet) {
301 if (m_stroke.style() == Qt::SolidLine) {
302 pen.setStyle(Qt::SolidLine);
303 }
else if (m_strokeWidthSet || oldWidth == 1) {
305 pen.setDashPattern(m_stroke.dashPattern());
306 setDashOffsetNeeded =
true;
309 QList<qreal> dashes = m_stroke.dashPattern();
310 for (
int i = 0; i < dashes.size(); ++i)
311 dashes[i] /= oldWidth;
312 pen.setDashPattern(dashes);
313 setDashOffsetNeeded =
true;
315 }
else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
317 QList<qreal> dashes = pen.dashPattern();
318 for (
int i = 0; i < dashes.size(); ++i)
320 pen.setDashPattern(dashes);
321 setDashOffsetNeeded =
true;
324 if (m_strokeLineCapSet)
325 pen.setCapStyle(m_stroke.capStyle());
326 if (m_strokeLineJoinSet)
327 pen.setJoinStyle(m_stroke.joinStyle());
328 if (m_strokeMiterLimitSet)
329 pen.setMiterLimit(m_stroke.miterLimit());
334 if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
335 qreal currentWidth = pen.widthF();
336 if (currentWidth == 0)
338 pen.setDashOffset(states.strokeDashOffset / currentWidth);
341 pen.setCosmetic(states.vectorEffect);
346void QSvgStrokeStyle::revert(QPainter *p, QSvgExtraStates &states)
348 p->setPen(m_oldStroke);
349 states.strokeOpacity = m_oldStrokeOpacity;
350 states.strokeDashOffset = m_oldStrokeDashOffset;
351 states.vectorEffect = m_oldVectorEffect;
354void QSvgStrokeStyle::setDashArray(
const QList<qreal> &dashes)
356 if (m_strokeWidthSet) {
357 QList<qreal> d = dashes;
358 qreal w = m_stroke.widthF();
359 if (w != 0 && w != 1) {
360 for (
int i = 0; i < d.size(); ++i)
363 m_stroke.setDashPattern(d);
365 m_stroke.setDashPattern(dashes);
367 m_strokeDashArraySet = 1;
370QSvgTransformStyle::QSvgTransformStyle(
const QTransform &trans)
375QSvgTransformStyle::~QSvgTransformStyle()
378void QSvgTransformStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
380 m_oldWorldTransform.push(p->worldTransform());
381 p->setWorldTransform(m_transform,
true);
384void QSvgTransformStyle::revert(QPainter *p, QSvgExtraStates &)
386 p->setWorldTransform(m_oldWorldTransform.pop(),
false );
389QSvgStyleProperty::Type QSvgQualityStyle::type()
const
394QSvgStyleProperty::Type QSvgFillStyle::type()
const
399QSvgStyleProperty::Type QSvgViewportFillStyle::type()
const
401 return VIEWPORT_FILL;
404QSvgStyleProperty::Type QSvgFontStyle::type()
const
409QSvgStyleProperty::Type QSvgStrokeStyle::type()
const
414QSvgStyleProperty::Type QSvgTransformStyle::type()
const
420QSvgCompOpStyle::QSvgCompOpStyle(QPainter::CompositionMode mode)
426QSvgCompOpStyle::~QSvgCompOpStyle()
429void QSvgCompOpStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
431 m_oldMode = p->compositionMode();
432 p->setCompositionMode(m_mode);
435void QSvgCompOpStyle::revert(QPainter *p, QSvgExtraStates &)
437 p->setCompositionMode(m_oldMode);
440QSvgStyleProperty::Type QSvgCompOpStyle::type()
const
445QSvgOffsetStyle::~QSvgOffsetStyle()
448void QSvgOffsetStyle::apply(QPainter *,
const QSvgNode *, QSvgExtraStates &)
452void QSvgOffsetStyle::revert(QPainter *, QSvgExtraStates &)
456QSvgStyleProperty::Type QSvgOffsetStyle::type()
const
461QSvgOpacityStyle::QSvgOpacityStyle(qreal opacity)
462 : m_opacity(opacity), m_oldOpacity(0)
467QSvgOpacityStyle::~QSvgOpacityStyle()
470void QSvgOpacityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
472 m_oldOpacity = p->opacity();
473 p->setOpacity(m_opacity * m_oldOpacity);
476void QSvgOpacityStyle::revert(QPainter *p, QSvgExtraStates &)
478 p->setOpacity(m_oldOpacity);
481QSvgStyleProperty::Type QSvgOpacityStyle::type()
const
486QSvgStaticStyle::QSvgStaticStyle()
498QSvgStaticStyle::~QSvgStaticStyle()
502void QSvgStaticStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
505 quality->apply(p, node, states);
509 fill->apply(p, node, states);
513 viewportFill->apply(p, node, states);
517 font->apply(p, node, states);
521 stroke->apply(p, node, states);
525 transform->apply(p, node, states);
529 opacity->apply(p, node, states);
533 compop->apply(p, node, states);
537void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
540 quality->revert(p, states);
544 fill->revert(p, states);
548 viewportFill->revert(p, states);
552 font->revert(p, states);
556 stroke->revert(p, states);
560 transform->revert(p, states);
564 opacity->revert(p, states);
568 compop->revert(p, states);
574QColor sumValue(
const QColor &c1,
const QColor &c2)
576 QRgb rgb1 = c1.rgba();
577 QRgb rgb2 = c2.rgba();
578 int sumRed = qRed(rgb1) + qRed(rgb2);
579 int sumGreen = qGreen(rgb1) + qGreen(rgb2);
580 int sumBlue = qBlue(rgb1) + qBlue(rgb2);
582 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
583 qBound(0, sumGreen, 255),
584 qBound(0, sumBlue, 255),
587 return QColor(sumRgb);
590qreal sumValue(qreal value1, qreal value2)
592 qreal sumValue = value1 + value2;
593 return qBound(0.0, sumValue, 1.0);
598QSvgAnimatedStyle::QSvgAnimatedStyle()
602QSvgAnimatedStyle::~QSvgAnimatedStyle()
606void QSvgAnimatedStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
608 QSharedPointer<QSvgAbstractAnimator> animator = node->document()->animator();
609 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
611 savePaintingState(p, node, states);
612 if (nodeAnims.isEmpty())
615 QSvgStyleState currentStyle = m_static;
616 for (
const QSvgAbstractAnimation *anim : std::as_const(nodeAnims)) {
617 if (!anim->isActive())
620 fetchStyleState(anim, currentStyle);
623 applyStyle(p, states, currentStyle);
626void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
628 p->setWorldTransform(m_worldTransform);
629 p->setBrush(m_static.fill);
630 p->setPen(m_static.stroke);
631 p->setOpacity(m_static.opacity);
632 states.fillOpacity = m_static.fillOpacity;
633 states.strokeOpacity = m_static.strokeOpacity;
636void QSvgAnimatedStyle::savePaintingState(
const QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
638 QSvgStaticStyle style = node->style();
639 m_worldTransform = p->worldTransform();
641 m_static.transform = style.transform->qtransform();
644 m_static.offsetPath = style.offset->path();
645 m_static.offsetRotateType = style.offset->rotateType();
646 m_static.offsetRotate = style.offset->rotateAngle();
649 m_static.fill = p->brush();
650 m_static.stroke = p->pen();
651 m_static.fillOpacity = states.fillOpacity;
652 m_static.strokeOpacity = states.strokeOpacity;
653 m_static.opacity = p->opacity();
654 m_transformToNode = m_static.transform.inverted() * m_worldTransform;
657void QSvgAnimatedStyle::fetchStyleState(
const QSvgAbstractAnimation *animation, QSvgStyleState ¤tStyle)
659 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ?
true :
660 (
static_cast<
const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
662 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
663 for (
const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
665 QBrush brush = currentStyle.fill;
666 QColor brushColor = brush.color();
667 QColor animatedColor = property->interpolatedValue().value<QColor>();
668 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(brushColor, animatedColor);
669 brush.setColor(sumOrReplaceColor);
670 currentStyle.fill = brush;
671 }
else if (property->propertyName() ==
QStringLiteral(
"stroke")) {
672 QPen &pen = currentStyle.stroke;
673 QBrush penBrush = pen.brush();
674 QColor penColor = penBrush.color();
675 QColor animatedColor = property->interpolatedValue().value<QColor>();
676 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(penColor, animatedColor);
677 penBrush.setColor(sumOrReplaceColor);
678 penBrush.setStyle(Qt::SolidPattern);
679 pen.setBrush(penBrush);
680 }
else if (property->propertyName() ==
QStringLiteral(
"transform")) {
681 QTransform animatedTransform = property->interpolatedValue().value<QTransform>();
682 QTransform sumOrReplaceTransform = replace ? animatedTransform : animatedTransform * currentStyle.transform;
683 currentStyle.transform = sumOrReplaceTransform;
684 }
else if (property->propertyName() ==
QStringLiteral(
"fill-opacity")) {
685 qreal animatedFillOpacity = property->interpolatedValue().value<qreal>();
686 qreal sumOrReplaceOpacity = replace ? animatedFillOpacity : sumValue(currentStyle.fillOpacity, animatedFillOpacity);
687 currentStyle.fillOpacity = sumOrReplaceOpacity;
688 }
else if (property->propertyName() ==
QStringLiteral(
"stroke-opacity")) {
689 qreal animatedStrokeOpacity = property->interpolatedValue().value<qreal>();
690 qreal sumOrReplaceOpacity = replace ? animatedStrokeOpacity : sumValue(currentStyle.strokeOpacity, animatedStrokeOpacity);
691 currentStyle.strokeOpacity = sumOrReplaceOpacity;
692 }
else if (property->propertyName() ==
QStringLiteral(
"opacity")) {
693 qreal animatedOpacity = property->interpolatedValue().value<qreal>();
694 qreal sumOrReplaceOpacity = replace ? animatedOpacity : sumValue(currentStyle.opacity, animatedOpacity);
695 currentStyle.opacity = sumOrReplaceOpacity;
696 }
else if (property->propertyName() ==
QStringLiteral(
"offset-distance")) {
697 qreal offsetDistance = property->interpolatedValue().value<qreal>();
698 currentStyle.offsetDistance = offsetDistance;
703void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states,
const QSvgStyleState ¤tStyle)
705 p->setBrush(currentStyle.fill);
706 states.fillOpacity = currentStyle.fillOpacity;
707 p->setPen(currentStyle.stroke);
708 states.strokeOpacity = currentStyle.strokeOpacity;
709 p->setOpacity(currentStyle.opacity);
711 QTransform transform = currentStyle.transform;
713 if (m_static.offsetPath) {
714 qreal offsetDistance = currentStyle.offsetDistance;
715 qreal angle = m_static.offsetPath.value().angleAtPercent(offsetDistance);
716 QPointF position = m_static.offsetPath.value().pointAtPercent(offsetDistance);
717 offset.translate(position.x(), position.y());
719 switch (m_static.offsetRotateType) {
720 case QtSvg::OffsetRotateType::Auto:
721 offset.rotate(-angle);
723 case QtSvg::OffsetRotateType::Angle:
724 offset.rotate(m_static.offsetRotate);
726 case QtSvg::OffsetRotateType::AutoAngle:
727 offset.rotate(m_static.offsetRotate - angle);
729 case QtSvg::OffsetRotateType::Reverse:
730 offset.rotate(180 - angle);
732 case QtSvg::OffsetRotateType::ReverseAngle:
733 offset.rotate(180 + m_static.offsetRotate - angle);
738 QTransform combinedTransform = transform * offset * m_transformToNode;
739 p->setWorldTransform(combinedTransform);
Combined button and popup list for selecting options.
#define QStringLiteral(str)