10#include "private/qsvganimate_p.h"
19QSvgExtraStates::QSvgExtraStates()
23 textAnchor(Qt::AlignLeft),
24 fontWeight(QFont::Normal),
25 fillRule(Qt::WindingFill),
28 imageRendering(QSvgQualityStyle::ImageRenderingAuto)
32QSvgStyleProperty::~QSvgStyleProperty()
36void QSvgPaintStyleProperty::apply(QPainter *,
const QSvgNode *, QSvgExtraStates &)
38 Q_ASSERT(!
"This should not be called!");
41void QSvgPaintStyleProperty::revert(QPainter *, QSvgExtraStates &)
43 Q_ASSERT(!
"This should not be called!");
47QSvgQualityStyle::QSvgQualityStyle(
int color)
48 : m_imageRendering(QSvgQualityStyle::ImageRenderingAuto)
49 , m_oldImageRendering(QSvgQualityStyle::ImageRenderingAuto)
50 , m_imageRenderingSet(0)
55void QSvgQualityStyle::setImageRendering(ImageRendering hint) {
56 m_imageRendering = hint;
57 m_imageRenderingSet = 1;
60void QSvgQualityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
62 m_oldImageRendering = states.imageRendering;
63 if (m_imageRenderingSet) {
64 states.imageRendering = m_imageRendering;
66 if (m_imageRenderingSet) {
68 if (m_imageRendering == ImageRenderingAuto)
72 smooth = (m_imageRendering == ImageRenderingOptimizeQuality);
73 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
77void QSvgQualityStyle::revert(QPainter *p, QSvgExtraStates &states)
79 if (m_imageRenderingSet) {
80 states.imageRendering = m_oldImageRendering;
82 if (m_oldImageRendering == ImageRenderingAuto)
85 smooth = (m_oldImageRendering == ImageRenderingOptimizeQuality);
86 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
90QSvgFillStyle::QSvgFillStyle()
92 , m_fillRule(Qt::WindingFill)
93 , m_oldFillRule(Qt::WindingFill)
96 , m_paintStyleResolved(1)
103void QSvgFillStyle::setFillRule(Qt::FillRule f)
109void QSvgFillStyle::setFillOpacity(qreal opacity)
111 m_fillOpacitySet = 1;
112 m_fillOpacity = opacity;
115void QSvgFillStyle::setFillStyle(QSvgPaintStyleProperty* style)
121void QSvgFillStyle::setBrush(QBrush brush)
123 m_fill = std::move(brush);
128void QSvgFillStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
130 m_oldFill = p->brush();
131 m_oldFillRule = states.fillRule;
132 m_oldFillOpacity = states.fillOpacity;
135 states.fillRule = m_fillRule;
138 p->setBrush(m_style->brush(p, n, states));
142 if (m_fillOpacitySet)
143 states.fillOpacity = m_fillOpacity;
146void QSvgFillStyle::revert(QPainter *p, QSvgExtraStates &states)
148 if (m_fillOpacitySet)
149 states.fillOpacity = m_oldFillOpacity;
151 p->setBrush(m_oldFill);
153 states.fillRule = m_oldFillRule;
156QSvgViewportFillStyle::QSvgViewportFillStyle(
const QBrush &brush)
157 : m_viewportFill(brush)
161void QSvgViewportFillStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
163 m_oldFill = p->brush();
164 p->setBrush(m_viewportFill);
167void QSvgViewportFillStyle::revert(QPainter *p, QSvgExtraStates &)
169 p->setBrush(m_oldFill);
172QSvgFontStyle::QSvgFontStyle(QSvgFont *font, QSvgDocument *doc)
184QSvgFontStyle::QSvgFontStyle()
196void QSvgFontStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
198 m_oldQFont = p->font();
199 m_oldSvgFont = states.svgFont;
200 m_oldTextAnchor = states.textAnchor;
201 m_oldWeight = states.fontWeight;
204 states.textAnchor = m_textAnchor;
206 QFont font = m_oldQFont;
208 states.svgFont = m_svgFont;
209 font.setFamilies(m_qfont.families());
213 font.setPointSizeF(m_qfont.pointSizeF());
216 font.setStyle(m_qfont.style());
219 font.setCapitalization(m_qfont.capitalization());
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));
227 states.fontWeight = m_weight;
229 font.setWeight(QFont::Weight(qBound(
static_cast<
int>(QFont::Weight::Thin),
231 static_cast<
int>(QFont::Weight::Black))));
237void QSvgFontStyle::revert(QPainter *p, QSvgExtraStates &states)
239 p->setFont(m_oldQFont);
240 states.svgFont = m_oldSvgFont;
241 states.textAnchor = m_oldTextAnchor;
242 states.fontWeight = m_oldWeight;
245QSvgStrokeStyle::QSvgStrokeStyle()
246 : m_strokeOpacity(1.0)
247 , m_oldStrokeOpacity(0.0)
248 , m_strokeDashOffset(0)
249 , m_oldStrokeDashOffset(0)
251 , m_paintStyleResolved(1)
253 , m_oldVectorEffect(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)
266void QSvgStrokeStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
268 m_oldStroke = p->pen();
269 m_oldStrokeOpacity = states.strokeOpacity;
270 m_oldStrokeDashOffset = states.strokeDashOffset;
271 m_oldVectorEffect = states.vectorEffect;
275 qreal oldWidth = pen.widthF();
276 qreal width = m_stroke.widthF();
281 qreal scale = oldWidth / width;
283 if (m_strokeOpacitySet)
284 states.strokeOpacity = m_strokeOpacity;
286 if (m_vectorEffectSet)
287 states.vectorEffect = m_vectorEffect;
291 pen.setBrush(m_style->brush(p, n, states));
293 pen.setBrush(m_stroke.brush());
296 if (m_strokeWidthSet)
297 pen.setWidthF(m_stroke.widthF());
299 bool setDashOffsetNeeded =
false;
301 if (m_strokeDashOffsetSet) {
302 states.strokeDashOffset = m_strokeDashOffset;
303 setDashOffsetNeeded =
true;
306 if (m_strokeDashArraySet) {
307 if (m_stroke.style() == Qt::SolidLine) {
308 pen.setStyle(Qt::SolidLine);
309 }
else if (m_strokeWidthSet || oldWidth == 1) {
311 pen.setDashPattern(m_stroke.dashPattern());
312 setDashOffsetNeeded =
true;
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;
321 }
else if (m_strokeWidthSet && pen.style() != Qt::SolidLine && scale != 1) {
323 QList<qreal> dashes = pen.dashPattern();
324 for (
int i = 0; i < dashes.size(); ++i)
326 pen.setDashPattern(dashes);
327 setDashOffsetNeeded =
true;
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());
340 if (setDashOffsetNeeded && pen.style() != Qt::SolidLine) {
341 qreal currentWidth = pen.widthF();
342 if (currentWidth == 0)
344 pen.setDashOffset(states.strokeDashOffset / currentWidth);
347 pen.setCosmetic(states.vectorEffect);
352void QSvgStrokeStyle::revert(QPainter *p, QSvgExtraStates &states)
354 p->setPen(m_oldStroke);
355 states.strokeOpacity = m_oldStrokeOpacity;
356 states.strokeDashOffset = m_oldStrokeDashOffset;
357 states.vectorEffect = m_oldVectorEffect;
360void QSvgStrokeStyle::setDashArray(
const QList<qreal> &dashes)
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)
369 m_stroke.setDashPattern(d);
371 m_stroke.setDashPattern(dashes);
373 m_strokeDashArraySet = 1;
376QSvgSolidColorStyle::QSvgSolidColorStyle(
const QColor &color)
377 : m_solidColor(color)
381QSvgGradientStyle::QSvgGradientStyle(QGradient *grad)
382 : m_gradient(grad), m_gradientStopsSet(
false)
386QBrush QSvgGradientStyle::brush(QPainter *,
const QSvgNode *, QSvgExtraStates &)
388 if (!m_link.isEmpty()) {
393 if (!m_gradientStopsSet) {
394 m_gradient->setStops(QGradientStops() << QGradientStop(0.0, QColor(0, 0, 0, 0)));
395 m_gradientStopsSet =
true;
398 QBrush b(*m_gradient);
400 if (!m_transform.isIdentity())
401 b.setTransform(m_transform);
407void QSvgGradientStyle::setTransform(
const QTransform &transform)
409 m_transform = transform;
412QSvgPatternStyle::QSvgPatternStyle(QSvgPattern *pattern)
418QBrush QSvgPatternStyle::brush(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
420 QBrush b(m_pattern->patternImage(p, states, node));
421 b.setTransform(m_pattern->appliedTransform());
425QSvgTransformStyle::QSvgTransformStyle(
const QTransform &trans)
430void QSvgTransformStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
432 m_oldWorldTransform.push(p->worldTransform());
433 p->setWorldTransform(m_transform,
true);
436void QSvgTransformStyle::revert(QPainter *p, QSvgExtraStates &)
438 p->setWorldTransform(m_oldWorldTransform.pop(),
false );
441QSvgStyleProperty::Type QSvgQualityStyle::type()
const
446QSvgStyleProperty::Type QSvgFillStyle::type()
const
451QSvgStyleProperty::Type QSvgViewportFillStyle::type()
const
453 return VIEWPORT_FILL;
456QSvgStyleProperty::Type QSvgFontStyle::type()
const
461QSvgStyleProperty::Type QSvgStrokeStyle::type()
const
466QSvgStyleProperty::Type QSvgSolidColorStyle::type()
const
471QSvgStyleProperty::Type QSvgGradientStyle::type()
const
476QSvgStyleProperty::Type QSvgPatternStyle::type()
const
481QSvgStyleProperty::Type QSvgTransformStyle::type()
const
487QSvgCompOpStyle::QSvgCompOpStyle(QPainter::CompositionMode mode)
493void QSvgCompOpStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
495 m_oldMode = p->compositionMode();
496 p->setCompositionMode(m_mode);
499void QSvgCompOpStyle::revert(QPainter *p, QSvgExtraStates &)
501 p->setCompositionMode(m_oldMode);
504QSvgStyleProperty::Type QSvgCompOpStyle::type()
const
509void QSvgOffsetStyle::apply(QPainter *,
const QSvgNode *, QSvgExtraStates &)
513void QSvgOffsetStyle::revert(QPainter *, QSvgExtraStates &)
517QSvgStyleProperty::Type QSvgOffsetStyle::type()
const
522QSvgOpacityStyle::QSvgOpacityStyle(qreal opacity)
523 : m_opacity(opacity), m_oldOpacity(0)
528void QSvgOpacityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
530 m_oldOpacity = p->opacity();
531 p->setOpacity(m_opacity * m_oldOpacity);
534void QSvgOpacityStyle::revert(QPainter *p, QSvgExtraStates &)
536 p->setOpacity(m_oldOpacity);
539QSvgStyleProperty::Type QSvgOpacityStyle::type()
const
544void QSvgGradientStyle::setStopLink(
const QString &link, QSvgDocument *doc)
550void QSvgGradientStyle::resolveStops()
553 resolveStops_helper(&visited);
556void QSvgGradientStyle::resolveStops_helper(QStringList *visited)
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();
570 qWarning(
"Could not resolve property : %s",
qPrintable(m_link));
576QSvgStaticStyle::QSvgStaticStyle()
591QSvgStaticStyle::~QSvgStaticStyle()
595void QSvgStaticStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
598 quality->apply(p, node, states);
602 fill->apply(p, node, states);
606 viewportFill->apply(p, node, states);
610 font->apply(p, node, states);
614 stroke->apply(p, node, states);
618 transform->apply(p, node, states);
622 opacity->apply(p, node, states);
626 compop->apply(p, node, states);
630void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
633 quality->revert(p, states);
637 fill->revert(p, states);
641 viewportFill->revert(p, states);
645 font->revert(p, states);
649 stroke->revert(p, states);
653 transform->revert(p, states);
657 opacity->revert(p, states);
661 compop->revert(p, states);
667QColor sumValue(
const QColor &c1,
const QColor &c2)
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);
675 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
676 qBound(0, sumGreen, 255),
677 qBound(0, sumBlue, 255),
680 return QColor(sumRgb);
683qreal sumValue(qreal value1, qreal value2)
685 qreal sumValue = value1 + value2;
686 return qBound(0.0, sumValue, 1.0);
691QSvgAnimatedStyle::QSvgAnimatedStyle()
695QSvgAnimatedStyle::~QSvgAnimatedStyle()
699void QSvgAnimatedStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
701 QSharedPointer<QSvgAbstractAnimator> animator = node->document()->animator();
702 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
704 savePaintingState(p, node, states);
705 if (nodeAnims.isEmpty())
708 QSvgStyleState currentStyle = m_static;
709 for (
auto anim : nodeAnims) {
710 if (!anim->isActive())
713 fetchStyleState(anim, currentStyle);
716 applyStyle(p, states, currentStyle);
719void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
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;
729void QSvgAnimatedStyle::savePaintingState(
const QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
731 QSvgStaticStyle style = node->style();
732 m_worldTransform = p->worldTransform();
734 m_static.transform = style.transform->qtransform();
737 m_static.offsetPath = style.offset->path();
738 m_static.offsetRotateType = style.offset->rotateType();
739 m_static.offsetRotate = style.offset->rotateAngle();
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;
750void QSvgAnimatedStyle::fetchStyleState(
const QSvgAbstractAnimation *animation, QSvgStyleState ¤tStyle)
752 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ?
true :
753 (
static_cast<
const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
755 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
756 for (
const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
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;
796void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states,
const QSvgStyleState ¤tStyle)
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);
804 QTransform transform = currentStyle.transform;
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());
812 switch (m_static.offsetRotateType) {
813 case QtSvg::OffsetRotateType::Auto:
814 offset.rotate(-angle);
816 case QtSvg::OffsetRotateType::Angle:
817 offset.rotate(m_static.offsetRotate);
819 case QtSvg::OffsetRotateType::AutoAngle:
820 offset.rotate(m_static.offsetRotate - angle);
822 case QtSvg::OffsetRotateType::Reverse:
823 offset.rotate(180 - angle);
825 case QtSvg::OffsetRotateType::ReverseAngle:
826 offset.rotate(180 + m_static.offsetRotate - angle);
831 QTransform combinedTransform = transform * offset * m_transformToNode;
832 p->setWorldTransform(combinedTransform);
Combined button and popup list for selecting options.
#define qPrintable(string)
#define QStringLiteral(str)