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 = std::move(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);
166const int QSvgFontStyle::LIGHTER;
167const int QSvgFontStyle::BOLDER;
169QSvgFontStyle::QSvgFontStyle(QSvgFont *font)
180QSvgFontStyle::QSvgFontStyle()
191QSvgFontStyle::~QSvgFontStyle()
194void QSvgFontStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
196 m_oldQFont = p->font();
197 m_oldSvgFont = states.svgFont;
198 m_oldTextAnchor = states.textAnchor;
199 m_oldWeight = states.fontWeight;
202 states.textAnchor = m_textAnchor;
204 QFont font = m_oldQFont;
206 states.svgFont = m_svgFont;
207 font.setFamilies(m_qfont.families());
211 font.setPointSizeF(m_qfont.pointSizeF());
214 font.setStyle(m_qfont.style());
217 font.setCapitalization(m_qfont.capitalization());
220 if (m_weight == BOLDER) {
221 states.fontWeight = qMin(states.fontWeight, QFont::Weight::Black - 100) + 100;
222 }
else if (m_weight == LIGHTER) {
223 states.fontWeight = qMax(states.fontWeight, QFont::Weight::Thin + 100) - 100;
225 states.fontWeight = m_weight;
227 font.setWeight(QFont::Weight(qBound(
static_cast<
int>(QFont::Weight::Thin),
229 static_cast<
int>(QFont::Weight::Black))));
235void QSvgFontStyle::revert(QPainter *p, QSvgExtraStates &states)
237 p->setFont(m_oldQFont);
238 states.svgFont = m_oldSvgFont;
239 states.textAnchor = m_oldTextAnchor;
240 states.fontWeight = m_oldWeight;
243QSvgStrokeStyle::QSvgStrokeStyle()
245 , m_oldVectorEffect(0)
247 , m_strokeDashArraySet(0)
248 , m_strokeDashOffsetSet(0)
249 , m_strokeLineCapSet(0)
250 , m_strokeLineJoinSet(0)
251 , m_strokeMiterLimitSet(0)
252 , m_strokeOpacitySet(0)
253 , m_strokeWidthSet(0)
254 , m_vectorEffectSet(0)
258QSvgStrokeStyle::~QSvgStrokeStyle()
261void QSvgStrokeStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
263 m_oldStroke = p->pen();
264 m_oldStrokeOpacity = states.strokeOpacity;
265 m_oldStrokeDashOffset = states.strokeDashOffset;
266 m_oldVectorEffect = states.vectorEffect;
270 qreal oldWidth = pen.widthF();
271 qreal width = m_stroke.widthF();
276 qreal scale = oldWidth / width;
278 if (m_strokeOpacitySet)
279 states.strokeOpacity = m_strokeOpacity;
281 if (m_vectorEffectSet)
282 states.vectorEffect = m_vectorEffect;
286 pen.setBrush(m_paintServer->brush(p, n, states));
288 pen.setBrush(m_stroke.brush());
291 if (m_strokeWidthSet)
292 pen.setWidthF(m_stroke.widthF());
294 bool setDashOffsetNeeded =
false;
296 if (m_strokeDashOffsetSet) {
297 states.strokeDashOffset = m_strokeDashOffset;
298 setDashOffsetNeeded =
true;
301 if (m_strokeDashArraySet) {
302 if (m_stroke.style() == Qt::SolidLine) {
303 pen.setStyle(Qt::SolidLine);
304 }
else if (m_strokeWidthSet || oldWidth == 1) {
306 pen.setDashPattern(m_stroke.dashPattern());
307 setDashOffsetNeeded =
true;
310 QList<qreal> dashes = m_stroke.dashPattern();
311 for (
int i = 0; i < dashes.size(); ++i)
312 dashes[i] /= oldWidth;
313 pen.setDashPattern(dashes);
314 setDashOffsetNeeded =
true;
316 }
else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
318 QList<qreal> dashes = pen.dashPattern();
319 for (
int i = 0; i < dashes.size(); ++i)
321 pen.setDashPattern(dashes);
322 setDashOffsetNeeded =
true;
325 if (m_strokeLineCapSet)
326 pen.setCapStyle(m_stroke.capStyle());
327 if (m_strokeLineJoinSet)
328 pen.setJoinStyle(m_stroke.joinStyle());
329 if (m_strokeMiterLimitSet)
330 pen.setMiterLimit(m_stroke.miterLimit());
335 if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
336 qreal currentWidth = pen.widthF();
337 if (currentWidth == 0)
339 pen.setDashOffset(states.strokeDashOffset / currentWidth);
342 pen.setCosmetic(states.vectorEffect);
347void QSvgStrokeStyle::revert(QPainter *p, QSvgExtraStates &states)
349 p->setPen(m_oldStroke);
350 states.strokeOpacity = m_oldStrokeOpacity;
351 states.strokeDashOffset = m_oldStrokeDashOffset;
352 states.vectorEffect = m_oldVectorEffect;
355void QSvgStrokeStyle::setDashArray(
const QList<qreal> &dashes)
357 if (m_strokeWidthSet) {
358 QList<qreal> d = dashes;
359 qreal w = m_stroke.widthF();
360 if (w != 0 && w != 1) {
361 for (
int i = 0; i < d.size(); ++i)
364 m_stroke.setDashPattern(d);
366 m_stroke.setDashPattern(dashes);
368 m_strokeDashArraySet = 1;
371QSvgTransformStyle::QSvgTransformStyle(
const QTransform &trans)
376QSvgTransformStyle::~QSvgTransformStyle()
379void QSvgTransformStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
381 m_oldWorldTransform.push(p->worldTransform());
382 p->setWorldTransform(m_transform,
true);
385void QSvgTransformStyle::revert(QPainter *p, QSvgExtraStates &)
387 p->setWorldTransform(m_oldWorldTransform.pop(),
false );
390QSvgStyleProperty::Type QSvgQualityStyle::type()
const
395QSvgStyleProperty::Type QSvgFillStyle::type()
const
400QSvgStyleProperty::Type QSvgViewportFillStyle::type()
const
405QSvgStyleProperty::Type QSvgFontStyle::type()
const
410QSvgStyleProperty::Type QSvgStrokeStyle::type()
const
415QSvgStyleProperty::Type QSvgTransformStyle::type()
const
421QSvgCompOpStyle::QSvgCompOpStyle(QPainter::CompositionMode mode)
427QSvgCompOpStyle::~QSvgCompOpStyle()
430void QSvgCompOpStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
432 m_oldMode = p->compositionMode();
433 p->setCompositionMode(m_mode);
436void QSvgCompOpStyle::revert(QPainter *p, QSvgExtraStates &)
438 p->setCompositionMode(m_oldMode);
441QSvgStyleProperty::Type QSvgCompOpStyle::type()
const
446QSvgOffsetStyle::~QSvgOffsetStyle()
449void QSvgOffsetStyle::apply(QPainter *,
const QSvgNode *, QSvgExtraStates &)
453void QSvgOffsetStyle::revert(QPainter *, QSvgExtraStates &)
457QSvgStyleProperty::Type QSvgOffsetStyle::type()
const
462QSvgOpacityStyle::QSvgOpacityStyle(qreal opacity)
463 : m_opacity(opacity), m_oldOpacity(0)
468QSvgOpacityStyle::~QSvgOpacityStyle()
471void QSvgOpacityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
473 m_oldOpacity = p->opacity();
474 p->setOpacity(m_opacity * m_oldOpacity);
477void QSvgOpacityStyle::revert(QPainter *p, QSvgExtraStates &)
479 p->setOpacity(m_oldOpacity);
482QSvgStyleProperty::Type QSvgOpacityStyle::type()
const
487QSvgStaticStyle::QSvgStaticStyle()
489QSvgStaticStyle::~QSvgStaticStyle()
492void QSvgStaticStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
494 for (
auto &prop : m_properties) {
496 prop->apply(p, node, states);
500void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
502 for (
auto &prop : m_properties) {
504 prop->revert(p, states);
510QColor sumValue(
const QColor &c1,
const QColor &c2)
512 QRgb rgb1 = c1.rgba();
513 QRgb rgb2 = c2.rgba();
514 int sumRed = qRed(rgb1) + qRed(rgb2);
515 int sumGreen = qGreen(rgb1) + qGreen(rgb2);
516 int sumBlue = qBlue(rgb1) + qBlue(rgb2);
518 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
519 qBound(0, sumGreen, 255),
520 qBound(0, sumBlue, 255),
523 return QColor(sumRgb);
526qreal sumValue(qreal value1, qreal value2)
528 qreal sumValue = value1 + value2;
529 return qBound(0.0, sumValue, 1.0);
534QSvgAnimatedStyle::QSvgAnimatedStyle()
538QSvgAnimatedStyle::~QSvgAnimatedStyle()
542void QSvgAnimatedStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
544 QSharedPointer<QSvgAbstractAnimator> animator = node->document()->animator();
545 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
547 savePaintingState(p, node, states);
548 if (nodeAnims.isEmpty())
551 QSvgStyleState currentStyle = m_static;
552 for (
const QSvgAbstractAnimation *anim : std::as_const(nodeAnims)) {
553 if (!anim->isActive())
556 fetchStyleState(anim, currentStyle);
559 applyStyle(p, states, currentStyle);
562void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
564 p->setWorldTransform(m_worldTransform);
565 p->setBrush(m_static.fill);
566 p->setPen(m_static.stroke);
567 p->setOpacity(m_static.opacity);
568 states.fillOpacity = m_static.fillOpacity;
569 states.strokeOpacity = m_static.strokeOpacity;
572void QSvgAnimatedStyle::savePaintingState(
const QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
574 m_worldTransform = p->worldTransform();
575 if (
auto prop = node->style().property(QSvgStyleProperty::Transform))
576 m_static.transform = (
static_cast<QSvgTransformStyle*>(prop))->qtransform();
578 if (
auto prop = node->style().property(QSvgStyleProperty::Offset)) {
579 QSvgOffsetStyle *offset =
static_cast<QSvgOffsetStyle*>(prop);
580 m_static.offsetPath = offset->path();
581 m_static.offsetRotateType = offset->rotateType();
582 m_static.offsetRotate = offset->rotateAngle();
585 m_static.fill = p->brush();
586 m_static.stroke = p->pen();
587 m_static.fillOpacity = states.fillOpacity;
588 m_static.strokeOpacity = states.strokeOpacity;
589 m_static.opacity = p->opacity();
590 m_transformToNode = m_static.transform.inverted() * m_worldTransform;
593void QSvgAnimatedStyle::fetchStyleState(
const QSvgAbstractAnimation *animation, QSvgStyleState ¤tStyle)
595 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ?
true :
596 (
static_cast<
const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
598 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
599 for (
const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
601 QBrush brush = currentStyle.fill;
602 QColor brushColor = brush.color();
603 QColor animatedColor = property->interpolatedValue().value<QColor>();
604 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(brushColor, animatedColor);
605 brush.setColor(sumOrReplaceColor);
606 currentStyle.fill = brush;
607 }
else if (property->propertyName() ==
QStringLiteral(
"stroke")) {
608 QPen &pen = currentStyle.stroke;
609 QBrush penBrush = pen.brush();
610 QColor penColor = penBrush.color();
611 QColor animatedColor = property->interpolatedValue().value<QColor>();
612 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(penColor, animatedColor);
613 penBrush.setColor(sumOrReplaceColor);
614 penBrush.setStyle(Qt::SolidPattern);
615 pen.setBrush(penBrush);
616 }
else if (property->propertyName() ==
QStringLiteral(
"transform")) {
617 QTransform animatedTransform = property->interpolatedValue().value<QTransform>();
618 QTransform sumOrReplaceTransform = replace ? animatedTransform : animatedTransform * currentStyle.transform;
619 currentStyle.transform = sumOrReplaceTransform;
620 }
else if (property->propertyName() ==
QStringLiteral(
"fill-opacity")) {
621 qreal animatedFillOpacity = property->interpolatedValue().value<qreal>();
622 qreal sumOrReplaceOpacity = replace ? animatedFillOpacity : sumValue(currentStyle.fillOpacity, animatedFillOpacity);
623 currentStyle.fillOpacity = sumOrReplaceOpacity;
624 }
else if (property->propertyName() ==
QStringLiteral(
"stroke-opacity")) {
625 qreal animatedStrokeOpacity = property->interpolatedValue().value<qreal>();
626 qreal sumOrReplaceOpacity = replace ? animatedStrokeOpacity : sumValue(currentStyle.strokeOpacity, animatedStrokeOpacity);
627 currentStyle.strokeOpacity = sumOrReplaceOpacity;
628 }
else if (property->propertyName() ==
QStringLiteral(
"opacity")) {
629 qreal animatedOpacity = property->interpolatedValue().value<qreal>();
630 qreal sumOrReplaceOpacity = replace ? animatedOpacity : sumValue(currentStyle.opacity, animatedOpacity);
631 currentStyle.opacity = sumOrReplaceOpacity;
632 }
else if (property->propertyName() ==
QStringLiteral(
"offset-distance")) {
633 qreal offsetDistance = property->interpolatedValue().value<qreal>();
634 currentStyle.offsetDistance = offsetDistance;
639void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states,
const QSvgStyleState ¤tStyle)
641 p->setBrush(currentStyle.fill);
642 states.fillOpacity = currentStyle.fillOpacity;
643 p->setPen(currentStyle.stroke);
644 states.strokeOpacity = currentStyle.strokeOpacity;
645 p->setOpacity(currentStyle.opacity);
647 QTransform transform = currentStyle.transform;
649 if (m_static.offsetPath) {
650 qreal offsetDistance = currentStyle.offsetDistance;
651 qreal angle = m_static.offsetPath.value().angleAtPercent(offsetDistance);
652 QPointF position = m_static.offsetPath.value().pointAtPercent(offsetDistance);
653 offset.translate(position.x(), position.y());
655 switch (m_static.offsetRotateType) {
656 case QtSvg::OffsetRotateType::Auto:
657 offset.rotate(-angle);
659 case QtSvg::OffsetRotateType::Angle:
660 offset.rotate(m_static.offsetRotate);
662 case QtSvg::OffsetRotateType::AutoAngle:
663 offset.rotate(m_static.offsetRotate - angle);
665 case QtSvg::OffsetRotateType::Reverse:
666 offset.rotate(180 - angle);
668 case QtSvg::OffsetRotateType::ReverseAngle:
669 offset.rotate(180 + m_static.offsetRotate - angle);
674 QTransform combinedTransform = transform * offset * m_transformToNode;
675 p->setWorldTransform(combinedTransform);
Combined button and popup list for selecting options.
#define QStringLiteral(str)