9#include "private/qsvganimate_p.h"
19QSvgExtraStates::QSvgExtraStates()
20 : imageRendering(QSvgQualityStyle::ImageRenderingAuto)
24QSvgExtraStates::QSvgExtraStates(
const QSvgDocument *doc) : QSvgExtraStates()
30QSvgStyleProperty::~QSvgStyleProperty()
33QSvgQualityStyle::QSvgQualityStyle(
int color)
34 : m_imageRendering(QSvgQualityStyle::ImageRenderingAuto)
35 , m_oldImageRendering(QSvgQualityStyle::ImageRenderingAuto)
36 , m_imageRenderingSet(0)
41QSvgQualityStyle::~QSvgQualityStyle()
44void QSvgQualityStyle::setImageRendering(ImageRendering hint) {
45 m_imageRendering = hint;
46 m_imageRenderingSet = 1;
49void QSvgQualityStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &states)
51 m_oldImageRendering = states.imageRendering;
52 if (m_imageRenderingSet) {
53 states.imageRendering = m_imageRendering;
55 if (m_imageRenderingSet) {
57 if (m_imageRendering == ImageRenderingAuto)
61 smooth = (m_imageRendering == ImageRenderingOptimizeQuality);
62 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
66void QSvgQualityStyle::revert(QPainter *p, QSvgExtraStates &states)
68 if (m_imageRenderingSet) {
69 states.imageRendering = m_oldImageRendering;
71 if (m_oldImageRendering == ImageRenderingAuto)
74 smooth = (m_oldImageRendering == ImageRenderingOptimizeQuality);
75 p->setRenderHint(QPainter::SmoothPixmapTransform, smooth);
79QSvgFillStyle::QSvgFillStyle()
86QSvgFillStyle::~QSvgFillStyle()
89void QSvgFillStyle::setFillRule(Qt::FillRule f)
95void QSvgFillStyle::setFillOpacity(qreal opacity)
98 m_fillOpacity = opacity;
101void QSvgFillStyle::setPaintServer(QSvgPaintServerSharedPtr paintServer)
103 m_paintServer = std::move(paintServer);
107void QSvgFillStyle::setBrush(QBrush brush)
109 m_fill = std::move(brush);
110 m_paintServer.reset();
114void QSvgFillStyle::apply(QPainter *p,
const QSvgNode *n, QSvgExtraStates &states)
116 m_oldFill = p->brush();
117 m_oldFillRule = states.fillRule;
118 m_oldFillOpacity = states.fillOpacity;
121 states.fillRule = m_fillRule;
124 p->setBrush(m_paintServer->brush(p, n, states));
128 if (m_fillOpacitySet)
129 states.fillOpacity = m_fillOpacity;
132void QSvgFillStyle::revert(QPainter *p, QSvgExtraStates &states)
134 if (m_fillOpacitySet)
135 states.fillOpacity = m_oldFillOpacity;
137 p->setBrush(m_oldFill);
139 states.fillRule = m_oldFillRule;
142QSvgViewportFillStyle::QSvgViewportFillStyle(
const QBrush &brush)
143 : m_viewportFill(brush)
147QSvgViewportFillStyle::~QSvgViewportFillStyle()
150void QSvgViewportFillStyle::apply(QPainter *p,
const QSvgNode *, QSvgExtraStates &)
152 m_oldFill = p->brush();
153 p->setBrush(m_viewportFill);
156void QSvgViewportFillStyle::revert(QPainter *p, QSvgExtraStates &)
158 p->setBrush(m_oldFill);
161const int QSvgFontStyle::LIGHTER;
162const int QSvgFontStyle::BOLDER;
164QSvgFontStyle::QSvgFontStyle()
174QSvgFontStyle::~QSvgFontStyle()
178
179
180
181
182
183
184
185
186
187
188int QSvgFontStyle::pixelSize(qreal size)
190 return qRound(qBound(qreal(1), size, qreal(std::numeric_limits<
int>::max())));
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 font.setFamilies(m_qfont.families());
206 states.svgFont = states.doc()->svgFont(m_qfont.family());
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, QFont::Weight::Black - 100) + 100;
221 }
else if (m_weight == LIGHTER) {
222 states.fontWeight = qMax(states.fontWeight, QFont::Weight::Thin + 100) - 100;
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
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)
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()
488QSvgStaticStyle::~QSvgStaticStyle()
491void QSvgStaticStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
493 for (
auto &prop : m_properties) {
495 prop->apply(p, node, states);
499void QSvgStaticStyle::revert(QPainter *p, QSvgExtraStates &states)
501 for (
auto &prop : m_properties) {
503 prop->revert(p, states);
509QColor sumValue(
const QColor &c1,
const QColor &c2)
511 QRgb rgb1 = c1.rgba();
512 QRgb rgb2 = c2.rgba();
513 int sumRed = qRed(rgb1) + qRed(rgb2);
514 int sumGreen = qGreen(rgb1) + qGreen(rgb2);
515 int sumBlue = qBlue(rgb1) + qBlue(rgb2);
517 QRgb sumRgb = qRgba(qBound(0, sumRed, 255),
518 qBound(0, sumGreen, 255),
519 qBound(0, sumBlue, 255),
522 return QColor(sumRgb);
525qreal sumValue(qreal value1, qreal value2)
527 qreal sumValue = value1 + value2;
528 return qBound(0.0, sumValue, 1.0);
533QSvgAnimatedStyle::QSvgAnimatedStyle()
537QSvgAnimatedStyle::~QSvgAnimatedStyle()
541void QSvgAnimatedStyle::apply(QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
543 QSharedPointer<QSvgAbstractAnimator> animator = states.doc()->animator();
544 QList<QSvgAbstractAnimation *> nodeAnims = animator->animationsForNode(node);
546 savePaintingState(p, node, states);
547 if (nodeAnims.isEmpty())
550 QSvgStyleState currentStyle = m_static;
551 for (
const QSvgAbstractAnimation *anim : std::as_const(nodeAnims)) {
552 if (!anim->isActive())
555 fetchStyleState(anim, currentStyle);
558 applyStyle(p, states, currentStyle);
561void QSvgAnimatedStyle::revert(QPainter *p, QSvgExtraStates &states)
563 p->setWorldTransform(m_worldTransform);
564 p->setBrush(m_static.fill);
565 p->setPen(m_static.stroke);
566 p->setOpacity(m_static.opacity);
567 states.fillOpacity = m_static.fillOpacity;
568 states.strokeOpacity = m_static.strokeOpacity;
571void QSvgAnimatedStyle::savePaintingState(
const QPainter *p,
const QSvgNode *node, QSvgExtraStates &states)
573 m_worldTransform = p->worldTransform();
574 if (
auto prop = node->style().property(QSvgStyleProperty::Transform))
575 m_static.transform = (
static_cast<QSvgTransformStyle*>(prop))->qtransform();
577 if (
auto prop = node->style().property(QSvgStyleProperty::Offset)) {
578 QSvgOffsetStyle *offset =
static_cast<QSvgOffsetStyle*>(prop);
579 m_static.offsetPath = offset->path();
580 m_static.offsetRotateType = offset->rotateType();
581 m_static.offsetRotate = offset->rotateAngle();
584 m_static.fill = p->brush();
585 m_static.stroke = p->pen();
586 m_static.fillOpacity = states.fillOpacity;
587 m_static.strokeOpacity = states.strokeOpacity;
588 m_static.opacity = p->opacity();
589 m_transformToNode = m_static.transform.inverted() * m_worldTransform;
592void QSvgAnimatedStyle::fetchStyleState(
const QSvgAbstractAnimation *animation, QSvgStyleState ¤tStyle)
594 bool replace = animation->animationType() == QSvgAbstractAnimation::CSS ?
true :
595 (
static_cast<
const QSvgAnimateNode *>(animation))->additiveType() == QSvgAnimateNode::Replace;
597 QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
598 for (
const QSvgAbstractAnimatedProperty *property : std::as_const(properties)) {
600 QBrush brush = currentStyle.fill;
601 QColor brushColor = brush.color();
602 QColor animatedColor = property->interpolatedValue().value<QColor>();
603 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(brushColor, animatedColor);
604 brush.setColor(sumOrReplaceColor);
605 currentStyle.fill = brush;
606 }
else if (property->propertyName() ==
QStringLiteral(
"stroke")) {
607 QPen &pen = currentStyle.stroke;
608 QBrush penBrush = pen.brush();
609 QColor penColor = penBrush.color();
610 QColor animatedColor = property->interpolatedValue().value<QColor>();
611 QColor sumOrReplaceColor = replace ? animatedColor : sumValue(penColor, animatedColor);
612 penBrush.setColor(sumOrReplaceColor);
613 penBrush.setStyle(Qt::SolidPattern);
614 pen.setBrush(penBrush);
615 }
else if (property->propertyName() ==
QStringLiteral(
"transform")) {
616 QTransform animatedTransform = property->interpolatedValue().value<QTransform>();
617 QTransform sumOrReplaceTransform = replace ? animatedTransform : animatedTransform * currentStyle.transform;
618 currentStyle.transform = sumOrReplaceTransform;
619 }
else if (property->propertyName() ==
QStringLiteral(
"fill-opacity")) {
620 qreal animatedFillOpacity = property->interpolatedValue().value<qreal>();
621 qreal sumOrReplaceOpacity = replace ? animatedFillOpacity : sumValue(currentStyle.fillOpacity, animatedFillOpacity);
622 currentStyle.fillOpacity = sumOrReplaceOpacity;
623 }
else if (property->propertyName() ==
QStringLiteral(
"stroke-opacity")) {
624 qreal animatedStrokeOpacity = property->interpolatedValue().value<qreal>();
625 qreal sumOrReplaceOpacity = replace ? animatedStrokeOpacity : sumValue(currentStyle.strokeOpacity, animatedStrokeOpacity);
626 currentStyle.strokeOpacity = sumOrReplaceOpacity;
627 }
else if (property->propertyName() ==
QStringLiteral(
"opacity")) {
628 qreal animatedOpacity = property->interpolatedValue().value<qreal>();
629 qreal sumOrReplaceOpacity = replace ? animatedOpacity : sumValue(currentStyle.opacity, animatedOpacity);
630 currentStyle.opacity = sumOrReplaceOpacity;
631 }
else if (property->propertyName() ==
QStringLiteral(
"offset-distance")) {
632 qreal offsetDistance = property->interpolatedValue().value<qreal>();
633 currentStyle.offsetDistance = offsetDistance;
638void QSvgAnimatedStyle::applyStyle(QPainter *p, QSvgExtraStates &states,
const QSvgStyleState ¤tStyle)
640 p->setBrush(currentStyle.fill);
641 states.fillOpacity = currentStyle.fillOpacity;
642 p->setPen(currentStyle.stroke);
643 states.strokeOpacity = currentStyle.strokeOpacity;
644 p->setOpacity(currentStyle.opacity);
646 QTransform transform = currentStyle.transform;
648 if (m_static.offsetPath) {
649 qreal offsetDistance = currentStyle.offsetDistance;
650 qreal angle = m_static.offsetPath.value().angleAtPercent(offsetDistance);
651 QPointF position = m_static.offsetPath.value().pointAtPercent(offsetDistance);
652 offset.translate(position.x(), position.y());
654 switch (m_static.offsetRotateType) {
655 case QtSvg::OffsetRotateType::Auto:
656 offset.rotate(-angle);
658 case QtSvg::OffsetRotateType::Angle:
659 offset.rotate(m_static.offsetRotate);
661 case QtSvg::OffsetRotateType::AutoAngle:
662 offset.rotate(m_static.offsetRotate - angle);
664 case QtSvg::OffsetRotateType::Reverse:
665 offset.rotate(180 - angle);
667 case QtSvg::OffsetRotateType::ReverseAngle:
668 offset.rotate(180 + m_static.offsetRotate - angle);
673 QTransform combinedTransform = transform * offset * m_transformToNode;
674 p->setWorldTransform(combinedTransform);
Combined button and popup list for selecting options.
#define QStringLiteral(str)