9#include "private/qsvganimate_p.h"
17QSvgExtraStates::QSvgExtraStates()
21 textAnchor(Qt::AlignLeft),
22 fontWeight(QFont::Normal),
23 fillRule(Qt::WindingFill),
26 imageRendering(QSvgQualityStyle::ImageRenderingAuto)
30QSvgExtraStates::QSvgExtraStates(
const QSvgDocument *doc) : QSvgExtraStates()
36QSvgStyleProperty::~QSvgStyleProperty()
39QSvgQualityStyle::QSvgQualityStyle(
int color)
40 : m_imageRendering(QSvgQualityStyle::ImageRenderingAuto)
41 , m_oldImageRendering(QSvgQualityStyle::ImageRenderingAuto)
42 , m_imageRenderingSet(0)
47QSvgQualityStyle::~QSvgQualityStyle()
50void QSvgQualityStyle::setImageRendering(ImageRendering hint) {
51 m_imageRendering = hint;
52 m_imageRenderingSet = 1;
55void QSvgQualityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
57 m_oldImageRendering = states.imageRendering;
58 if (m_imageRenderingSet) {
59 states.imageRendering = m_imageRendering;
61 if (m_imageRenderingSet) {
63 if (m_imageRendering == ImageRenderingAuto)
67 smooth = (m_imageRendering == ImageRenderingOptimizeQuality);
68 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
72void QSvgQualityStyle::revert(QPainter *p, QSvgExtraStates &states)
74 if (m_imageRenderingSet) {
75 states.imageRendering = m_oldImageRendering;
77 if (m_oldImageRendering == ImageRenderingAuto)
80 smooth = (m_oldImageRendering == ImageRenderingOptimizeQuality);
81 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
85QSvgFillStyle::QSvgFillStyle()
92QSvgFillStyle::~QSvgFillStyle()
95void QSvgFillStyle::setFillRule(Qt::FillRule f)
101void QSvgFillStyle::setFillOpacity(qreal opacity)
103 m_fillOpacitySet = 1;
104 m_fillOpacity = opacity;
107void QSvgFillStyle::setPaintServer(QSvgPaintServerSharedPtr paintServer)
109 m_paintServer = std::move(paintServer);
113void QSvgFillStyle::setBrush(QBrush brush)
115 m_fill = std::move(brush);
116 m_paintServer.reset();
120void QSvgFillStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
122 m_oldFill = p->brush();
123 m_oldFillRule = states.fillRule;
124 m_oldFillOpacity = states.fillOpacity;
127 states.fillRule = m_fillRule;
130 p->setBrush(m_paintServer->brush(p, n, states));
134 if (m_fillOpacitySet)
135 states.fillOpacity = m_fillOpacity;
138void QSvgFillStyle::revert(QPainter *p, QSvgExtraStates &states)
140 if (m_fillOpacitySet)
141 states.fillOpacity = m_oldFillOpacity;
143 p->setBrush(m_oldFill);
145 states.fillRule = m_oldFillRule;
148QSvgViewportFillStyle::QSvgViewportFillStyle(
const QBrush &brush)
149 : m_viewportFill(brush)
153QSvgViewportFillStyle::~QSvgViewportFillStyle()
156void QSvgViewportFillStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
158 m_oldFill = p->brush();
159 p->setBrush(m_viewportFill);
162void QSvgViewportFillStyle::revert(QPainter *p, QSvgExtraStates &)
164 p->setBrush(m_oldFill);
167const int QSvgFontStyle::LIGHTER;
168const int QSvgFontStyle::BOLDER;
170QSvgFontStyle::QSvgFontStyle()
180QSvgFontStyle::~QSvgFontStyle()
183void QSvgFontStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
185 m_oldQFont = p->font();
186 m_oldSvgFont = states.svgFont;
187 m_oldTextAnchor = states.textAnchor;
188 m_oldWeight = states.fontWeight;
191 states.textAnchor = m_textAnchor;
193 QFont font = m_oldQFont;
195 font.setFamilies(m_qfont.families());
196 states.svgFont = node->document()->svgFont(m_qfont.family());
200 font.setPointSizeF(m_qfont.pointSizeF());
203 font.setStyle(m_qfont.style());
206 font.setCapitalization(m_qfont.capitalization());
209 if (m_weight == BOLDER) {
210 states.fontWeight = qMin(states.fontWeight, QFont::Weight::Black - 100) + 100;
211 }
else if (m_weight == LIGHTER) {
212 states.fontWeight = qMax(states.fontWeight, QFont::Weight::Thin + 100) - 100;
214 states.fontWeight = m_weight;
216 font.setWeight(QFont::Weight(qBound(
static_cast<
int>(QFont::Weight::Thin),
218 static_cast<
int>(QFont::Weight::Black))));
224void QSvgFontStyle::revert(QPainter *p, QSvgExtraStates &states)
226 p->setFont(m_oldQFont);
227 states.svgFont = m_oldSvgFont;
228 states.textAnchor = m_oldTextAnchor;
229 states.fontWeight = m_oldWeight;
232QSvgStrokeStyle::QSvgStrokeStyle()
234 , m_oldVectorEffect(0)
236 , m_strokeDashArraySet(0)
237 , m_strokeDashOffsetSet(0)
238 , m_strokeLineCapSet(0)
239 , m_strokeLineJoinSet(0)
240 , m_strokeMiterLimitSet(0)
241 , m_strokeOpacitySet(0)
242 , m_strokeWidthSet(0)
243 , m_vectorEffectSet(0)
247QSvgStrokeStyle::~QSvgStrokeStyle()
250void QSvgStrokeStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
252 m_oldStroke = p->pen();
253 m_oldStrokeOpacity = states.strokeOpacity;
254 m_oldStrokeDashOffset = states.strokeDashOffset;
255 m_oldVectorEffect = states.vectorEffect;
259 qreal oldWidth = pen.widthF();
260 qreal width = m_stroke.widthF();
265 qreal scale = oldWidth / width;
267 if (m_strokeOpacitySet)
268 states.strokeOpacity = m_strokeOpacity;
270 if (m_vectorEffectSet)
271 states.vectorEffect = m_vectorEffect;
275 pen.setBrush(m_paintServer->brush(p, n, states));
277 pen.setBrush(m_stroke.brush());
280 if (m_strokeWidthSet)
281 pen.setWidthF(m_stroke.widthF());
283 bool setDashOffsetNeeded =
false;
285 if (m_strokeDashOffsetSet) {
286 states.strokeDashOffset = m_strokeDashOffset;
287 setDashOffsetNeeded =
true;
290 if (m_strokeDashArraySet) {
291 if (m_stroke.style() == Qt::SolidLine) {
292 pen.setStyle(Qt::SolidLine);
293 }
else if (m_strokeWidthSet || oldWidth == 1) {
295 pen.setDashPattern(m_stroke.dashPattern());
296 setDashOffsetNeeded =
true;
299 QList<qreal> dashes = m_stroke.dashPattern();
300 for (
int i = 0; i < dashes.size(); ++i)
301 dashes[i] /= oldWidth;
302 pen.setDashPattern(dashes);
303 setDashOffsetNeeded =
true;
305 }
else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
307 QList<qreal> dashes = pen.dashPattern();
308 for (
int i = 0; i < dashes.size(); ++i)
310 pen.setDashPattern(dashes);
311 setDashOffsetNeeded =
true;
314 if (m_strokeLineCapSet)
315 pen.setCapStyle(m_stroke.capStyle());
316 if (m_strokeLineJoinSet)
317 pen.setJoinStyle(m_stroke.joinStyle());
318 if (m_strokeMiterLimitSet)
319 pen.setMiterLimit(m_stroke.miterLimit());
324 if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
325 qreal currentWidth = pen.widthF();
326 if (currentWidth == 0)
328 pen.setDashOffset(states.strokeDashOffset / currentWidth);
331 pen.setCosmetic(states.vectorEffect);
336void QSvgStrokeStyle::revert(QPainter *p, QSvgExtraStates &states)
338 p->setPen(m_oldStroke);
339 states.strokeOpacity = m_oldStrokeOpacity;
340 states.strokeDashOffset = m_oldStrokeDashOffset;
341 states.vectorEffect = m_oldVectorEffect;
344void QSvgStrokeStyle::setDashArray(
const QList<qreal> &dashes)
346 if (m_strokeWidthSet) {
347 QList<qreal> d = dashes;
348 qreal w = m_stroke.widthF();
349 if (w != 0 && w != 1) {
350 for (
int i = 0; i < d.size(); ++i)
353 m_stroke.setDashPattern(d);
355 m_stroke.setDashPattern(dashes);
357 m_strokeDashArraySet = 1;
360QSvgTransformStyle::QSvgTransformStyle(
const QTransform &trans)
365QSvgTransformStyle::~QSvgTransformStyle()
368void QSvgTransformStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
370 m_oldWorldTransform.push(p->worldTransform());
371 p->setWorldTransform(m_transform,
true);
374void QSvgTransformStyle::revert(QPainter *p, QSvgExtraStates &)
376 p->setWorldTransform(m_oldWorldTransform.pop(),
false );
379QSvgStyleProperty::Type QSvgQualityStyle::type()
const
384QSvgStyleProperty::Type QSvgFillStyle::type()
const
389QSvgStyleProperty::Type QSvgViewportFillStyle::type()
const
394QSvgStyleProperty::Type QSvgFontStyle::type()
const
399QSvgStyleProperty::Type QSvgStrokeStyle::type()
const
404QSvgStyleProperty::Type QSvgTransformStyle::type()
const
410QSvgCompOpStyle::QSvgCompOpStyle(QPainter::CompositionMode mode)
416QSvgCompOpStyle::~QSvgCompOpStyle()
419void QSvgCompOpStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
421 m_oldMode = p->compositionMode();
422 p->setCompositionMode(m_mode);
425void QSvgCompOpStyle::revert(QPainter *p, QSvgExtraStates &)
427 p->setCompositionMode(m_oldMode);
430QSvgStyleProperty::Type QSvgCompOpStyle::type()
const
435QSvgOffsetStyle::~QSvgOffsetStyle()
438void QSvgOffsetStyle::apply(QPainter *,
const QSvgNode *, QSvgExtraStates &)
442void QSvgOffsetStyle::revert(QPainter *, QSvgExtraStates &)
446QSvgStyleProperty::Type QSvgOffsetStyle::type()
const
451QSvgOpacityStyle::QSvgOpacityStyle(qreal opacity)
452 : m_opacity(opacity), m_oldOpacity(0)
457QSvgOpacityStyle::~QSvgOpacityStyle()
460void QSvgOpacityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
462 m_oldOpacity = p->opacity();
463 p->setOpacity(m_opacity * m_oldOpacity);
466void QSvgOpacityStyle::revert(QPainter *p, QSvgExtraStates &)
468 p->setOpacity(m_oldOpacity);
471QSvgStyleProperty::Type QSvgOpacityStyle::type()
const
476QSvgStaticStyle::QSvgStaticStyle()
478QSvgStaticStyle::~QSvgStaticStyle()
481void QSvgStaticStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
483 for (
auto &prop : m_properties) {
485 prop->apply(p, node, states);
489void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
491 for (
auto &prop : m_properties) {
493 prop->revert(p, states);
499QColor sumValue(
const QColor &c1,
const QColor &c2)
501 QRgb rgb1 = c1.rgba();
502 QRgb rgb2 = c2.rgba();
503 int sumRed = qRed(rgb1) + qRed(rgb2);
504 int sumGreen = qGreen(rgb1) + qGreen(rgb2);
505 int sumBlue = qBlue(rgb1) + qBlue(rgb2);
507 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
508 qBound(0, sumGreen, 255),
509 qBound(0, sumBlue, 255),
512 return QColor(sumRgb);
515qreal sumValue(qreal value1, qreal value2)
517 qreal sumValue = value1 + value2;
518 return qBound(0.0, sumValue, 1.0);
523QSvgAnimatedStyle::QSvgAnimatedStyle()
527QSvgAnimatedStyle::~QSvgAnimatedStyle()
531void QSvgAnimatedStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
533 QSharedPointer<QSvgAbstractAnimator> animator = node->document()->animator();
534 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
536 savePaintingState(p, node, states);
537 if (nodeAnims.isEmpty())
540 QSvgStyleState currentStyle = m_static;
541 for (
const QSvgAbstractAnimation *anim : std::as_const(nodeAnims)) {
542 if (!anim->isActive())
545 fetchStyleState(anim, currentStyle);
548 applyStyle(p, states, currentStyle);
551void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
553 p->setWorldTransform(m_worldTransform);
554 p->setBrush(m_static.fill);
555 p->setPen(m_static.stroke);
556 p->setOpacity(m_static.opacity);
557 states.fillOpacity = m_static.fillOpacity;
558 states.strokeOpacity = m_static.strokeOpacity;
561void QSvgAnimatedStyle::savePaintingState(
const QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
563 m_worldTransform = p->worldTransform();
564 if (
auto prop = node->style().property(QSvgStyleProperty::Transform))
565 m_static.transform = (
static_cast<QSvgTransformStyle*>(prop))->qtransform();
567 if (
auto prop = node->style().property(QSvgStyleProperty::Offset)) {
568 QSvgOffsetStyle *offset =
static_cast<QSvgOffsetStyle*>(prop);
569 m_static.offsetPath = offset->path();
570 m_static.offsetRotateType = offset->rotateType();
571 m_static.offsetRotate = offset->rotateAngle();
574 m_static.fill = p->brush();
575 m_static.stroke = p->pen();
576 m_static.fillOpacity = states.fillOpacity;
577 m_static.strokeOpacity = states.strokeOpacity;
578 m_static.opacity = p->opacity();
579 m_transformToNode = m_static.transform.inverted() * m_worldTransform;
582void QSvgAnimatedStyle::fetchStyleState(
const QSvgAbstractAnimation *animation, QSvgStyleState ¤tStyle)
584 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ?
true :
585 (
static_cast<
const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
587 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
588 for (
const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
590 QBrush brush = currentStyle.fill;
591 QColor brushColor = brush.color();
592 QColor animatedColor = property->interpolatedValue().value<QColor>();
593 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(brushColor, animatedColor);
594 brush.setColor(sumOrReplaceColor);
595 currentStyle.fill = brush;
596 }
else if (property->propertyName() ==
QStringLiteral(
"stroke")) {
597 QPen &pen = currentStyle.stroke;
598 QBrush penBrush = pen.brush();
599 QColor penColor = penBrush.color();
600 QColor animatedColor = property->interpolatedValue().value<QColor>();
601 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(penColor, animatedColor);
602 penBrush.setColor(sumOrReplaceColor);
603 penBrush.setStyle(Qt::SolidPattern);
604 pen.setBrush(penBrush);
605 }
else if (property->propertyName() ==
QStringLiteral(
"transform")) {
606 QTransform animatedTransform = property->interpolatedValue().value<QTransform>();
607 QTransform sumOrReplaceTransform = replace ? animatedTransform : animatedTransform * currentStyle.transform;
608 currentStyle.transform = sumOrReplaceTransform;
609 }
else if (property->propertyName() ==
QStringLiteral(
"fill-opacity")) {
610 qreal animatedFillOpacity = property->interpolatedValue().value<qreal>();
611 qreal sumOrReplaceOpacity = replace ? animatedFillOpacity : sumValue(currentStyle.fillOpacity, animatedFillOpacity);
612 currentStyle.fillOpacity = sumOrReplaceOpacity;
613 }
else if (property->propertyName() ==
QStringLiteral(
"stroke-opacity")) {
614 qreal animatedStrokeOpacity = property->interpolatedValue().value<qreal>();
615 qreal sumOrReplaceOpacity = replace ? animatedStrokeOpacity : sumValue(currentStyle.strokeOpacity, animatedStrokeOpacity);
616 currentStyle.strokeOpacity = sumOrReplaceOpacity;
617 }
else if (property->propertyName() ==
QStringLiteral(
"opacity")) {
618 qreal animatedOpacity = property->interpolatedValue().value<qreal>();
619 qreal sumOrReplaceOpacity = replace ? animatedOpacity : sumValue(currentStyle.opacity, animatedOpacity);
620 currentStyle.opacity = sumOrReplaceOpacity;
621 }
else if (property->propertyName() ==
QStringLiteral(
"offset-distance")) {
622 qreal offsetDistance = property->interpolatedValue().value<qreal>();
623 currentStyle.offsetDistance = offsetDistance;
628void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states,
const QSvgStyleState ¤tStyle)
630 p->setBrush(currentStyle.fill);
631 states.fillOpacity = currentStyle.fillOpacity;
632 p->setPen(currentStyle.stroke);
633 states.strokeOpacity = currentStyle.strokeOpacity;
634 p->setOpacity(currentStyle.opacity);
636 QTransform transform = currentStyle.transform;
638 if (m_static.offsetPath) {
639 qreal offsetDistance = currentStyle.offsetDistance;
640 qreal angle = m_static.offsetPath.value().angleAtPercent(offsetDistance);
641 QPointF position = m_static.offsetPath.value().pointAtPercent(offsetDistance);
642 offset.translate(position.x(), position.y());
644 switch (m_static.offsetRotateType) {
645 case QtSvg::OffsetRotateType::Auto:
646 offset.rotate(-angle);
648 case QtSvg::OffsetRotateType::Angle:
649 offset.rotate(m_static.offsetRotate);
651 case QtSvg::OffsetRotateType::AutoAngle:
652 offset.rotate(m_static.offsetRotate - angle);
654 case QtSvg::OffsetRotateType::Reverse:
655 offset.rotate(180 - angle);
657 case QtSvg::OffsetRotateType::ReverseAngle:
658 offset.rotate(180 + m_static.offsetRotate - angle);
663 QTransform combinedTransform = transform * offset * m_transformToNode;
664 p->setWorldTransform(combinedTransform);
Combined button and popup list for selecting options.
#define QStringLiteral(str)