Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Make it so that the Style Preset sample svg can have inherited color.
Wolthera van Hövell tot Westerflier7dde6ec27933
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cppindex 8f347c9f83..8223d4fd0a 100644--- a/libs/flake/resources/KoCssStylePreset.cpp+++ b/libs/flake/resources/KoCssStylePreset.cpp@@ -144,13 +144,15 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPropemodifiedProps.removeProperty(p);}}- // Always remove inline size, it is shape-specific.- if (p == KoSvgTextProperties::InlineSizeId) {- modifiedProps.removeProperty(p);- }+}// This one is added after removing, because otherwise, the type is removed...modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleType, type);+ // Always remove inline size, it is shape-specific.+ modifiedProps.removeProperty(KoSvgTextProperties::InlineSizeId);+ // Remove fill and stroke for now as we have no widgets for them.+ modifiedProps.removeProperty(KoSvgTextProperties::FillId);+ modifiedProps.removeProperty(KoSvgTextProperties::StrokeId);sampleText->setPropertiesAtPos(-1, modifiedProps);@@ -185,14 +187,16 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope}newShape->insertRichText(newShape->posForIndex(before.size()), sampleText);+d->shape.reset(newShape);}}- d->shape->debugParsing();// Don't d->shape->cleanup() here, as it *can* lead to the sample getting cleaned up.+ d->shape->cleanUp();updateThumbnail();setValid(true);+ setDirty(true);}Qt::Alignment KoCssStylePreset::alignSample() constdiff --git a/libs/flake/svg/SvgGraphicContext.h b/libs/flake/svg/SvgGraphicContext.hindex b273d95d97..36d781f95b 100644--- a/libs/flake/svg/SvgGraphicContext.h+++ b/libs/flake/svg/SvgGraphicContext.h@@ -20,7 +20,8 @@ public:enum StyleType {None, ///< no styleSolid, ///< solid style- Complex ///< gradient or pattern style+ Complex, ///< gradient or pattern style+ Inherit};SvgGraphicsContext();diff --git a/libs/flake/svg/SvgParser.cpp b/libs/flake/svg/SvgParser.cppindex e856e61284..a97f63f6de 100644--- a/libs/flake/svg/SvgParser.cpp+++ b/libs/flake/svg/SvgParser.cpp@@ -203,6 +203,11 @@ void SvgParser::setDefaultKraTextVersion(int version)m_context.currentGC()->textProperties.setProperty(KoSvgTextProperties::KraTextVersionId, version);}+void SvgParser::setInheritByDefault(const bool enable)+{+ m_inheritByDefault = enable;+}+QList<KoShape*> SvgParser::shapes() const{return m_shapes;@@ -940,7 +945,7 @@ void SvgParser::uploadStyleToContext(const QDomElement &e){SvgStyles styles = m_context.styleParser().collectStyles(e);m_context.styleParser().parseFont(styles);- m_context.styleParser().parseStyle(styles);+ m_context.styleParser().parseStyle(styles, m_inheritByDefault);}void SvgParser::applyCurrentStyle(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates)@@ -1001,7 +1006,7 @@ void SvgParser::applyStyle(KoShape *obj, const SvgStyles &styles, const QPointFif (!gc)return;- m_context.styleParser().parseStyle(styles);+ m_context.styleParser().parseStyle(styles, m_inheritByDefault);if (!obj)return;@@ -1200,6 +1205,8 @@ void SvgParser::applyFillStyle(KoShape *shape)shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(gc->fillColor)));}}+ } else if (gc->fillType == SvgGraphicsContext::Inherit) {+ shape->setInheritBackground(true);}KoPathShape *path = dynamic_cast<KoPathShape*>(shape);@@ -1266,6 +1273,8 @@ void SvgParser::applyStrokeStyle(KoShape *shape)applyDashes(gc->stroke, stroke);shape->setStroke(stroke);}+ } else if (gc->strokeType == SvgGraphicsContext::Inherit) {+ shape->setInheritStroke(true);}}diff --git a/libs/flake/svg/SvgParser.h b/libs/flake/svg/SvgParser.hindex b4ffcef7cd..dfa420a736 100644--- a/libs/flake/svg/SvgParser.h+++ b/libs/flake/svg/SvgParser.h@@ -58,6 +58,10 @@ public:void setResolution(const QRectF boundsInPixels, qreal pixelsPerInch);void setDefaultKraTextVersion(int version);+ // Set whether to always consider shapes without fill or stroke explicitely set as inherited.+ // By default, this is off, and the parser retrieves the fill from the graphicsContext.+ void setInheritByDefault(const bool enable);+/// Returns the list of all shapes of the svg documentQList<KoShape*> shapes() const;@@ -231,6 +235,7 @@ private:QString m_documentDescription;QVector<KoID> m_warnings;QMap<KoShape *, QTransform> m_shapeParentTransform;+ bool m_inheritByDefault = false;};#endifdiff --git a/libs/flake/svg/SvgStyleParser.cpp b/libs/flake/svg/SvgStyleParser.cppindex d99af96905..fa2f3b5a37 100644--- a/libs/flake/svg/SvgStyleParser.cpp+++ b/libs/flake/svg/SvgStyleParser.cpp@@ -112,11 +112,16 @@ SvgStyleParser::~SvgStyleParser()delete d;}-void SvgStyleParser::parseStyle(const SvgStyles &styles)+void SvgStyleParser::parseStyle(const SvgStyles &styles, const bool inheritByDefault){SvgGraphicsContext *gc = d->context.currentGC();if (!gc) return;+ if (inheritByDefault) {+ gc->fillType = SvgGraphicsContext::Inherit;+ gc->strokeType = SvgGraphicsContext::Inherit;+ }+// make sure we parse the style attributes in the right orderQ_FOREACH (const QString & command, d->styleAttributes) {const QString ¶ms = styles.value(command);diff --git a/libs/flake/svg/SvgStyleParser.h b/libs/flake/svg/SvgStyleParser.hindex d3450e57c5..671809ef6a 100644--- a/libs/flake/svg/SvgStyleParser.h+++ b/libs/flake/svg/SvgStyleParser.h@@ -30,7 +30,7 @@ public:~SvgStyleParser();/// Parses specified style attributes- void parseStyle(const SvgStyles &styles);+ void parseStyle(const SvgStyles &styles, const bool inheritByDefault = false);/// Parses font attributesvoid parseFont(const SvgStyles &styles);diff --git a/libs/flake/svg/SvgStyleWriter.cpp b/libs/flake/svg/SvgStyleWriter.cppindex 5fe673e956..dde733c5a9 100644--- a/libs/flake/svg/SvgStyleWriter.cpp+++ b/libs/flake/svg/SvgStyleWriter.cpp@@ -57,8 +57,12 @@ void SvgStyleWriter::saveSvgStyle(KoShape *shape, SvgSavingContext &context)KoPathShape *pathShape = dynamic_cast<KoPathShape*>(shape);bool fillRule = pathShape && pathShape->background() && pathShape->fillRule() == Qt::OddEvenFill? true: false;- saveSvgFill(shape->background(), fillRule, shape->outlineRect(), shape->size(), shape->absoluteTransformation(), context);- saveSvgStroke(shape->stroke(), context);+ if (!shape->inheritBackground()) {+ saveSvgFill(shape->background(), fillRule, shape->outlineRect(), shape->size(), shape->absoluteTransformation(), context);+ }+ if (!shape->inheritStroke()) {+ saveSvgStroke(shape->stroke(), context);+ }saveSvgEffects(shape, context);saveSvgClipping(shape, context);diff --git a/libs/flake/text/KoSvgTextLoader.cpp b/libs/flake/text/KoSvgTextLoader.cppindex 9f0e002dc1..17047b7a4c 100644--- a/libs/flake/text/KoSvgTextLoader.cpp+++ b/libs/flake/text/KoSvgTextLoader.cpp@@ -88,15 +88,19 @@ void KoSvgTextLoader::setStyleInfo(KoShape *s)}}- if ((parentBg && !parentBg->compareTo(s->background().data()))- || (!parentBg && s->background())) {- d->currentNode->properties.setProperty(KoSvgTextProperties::FillId,- QVariant::fromValue(KoSvgText::BackgroundProperty(s->background())));+ if (!s->inheritBackground()) {+ if ((parentBg && !parentBg->compareTo(s->background().data()))+ || (!parentBg && s->background())) {+ d->currentNode->properties.setProperty(KoSvgTextProperties::FillId,+ QVariant::fromValue(KoSvgText::BackgroundProperty(s->background())));+ }}- if ((parentStroke && (!parentStroke->compareFillTo(s->stroke().data()) || !parentStroke->compareStyleTo(s->stroke().data())))- || (!parentStroke && s->stroke())) {- d->currentNode->properties.setProperty(KoSvgTextProperties::StrokeId,- QVariant::fromValue(KoSvgText::StrokeProperty(s->stroke())));+ if (!s->inheritStroke()) {+ if ((parentStroke && (!parentStroke->compareFillTo(s->stroke().data()) || !parentStroke->compareStyleTo(s->stroke().data())))+ || (!parentStroke && s->stroke())) {+ d->currentNode->properties.setProperty(KoSvgTextProperties::StrokeId,+ QVariant::fromValue(KoSvgText::StrokeProperty(s->stroke())));+ }}d->currentNode->properties.setProperty(KoSvgTextProperties::Opacity,s->transparency());diff --git a/libs/flake/text/KoSvgTextShape_p_output.cpp b/libs/flake/text/KoSvgTextShape_p_output.cppindex c6369be412..56b1787e41 100644--- a/libs/flake/text/KoSvgTextShape_p_output.cpp+++ b/libs/flake/text/KoSvgTextShape_p_output.cpp@@ -372,11 +372,13 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteconst QString imageViewTransformProp = "viewboxTransform";KoShapeFactoryBase *rectangleFactory = KoShapeRegistry::instance()->value("RectangleShape");-KoShapeStrokeModelSP stroke = rootShape->stroke();QSharedPointer<KoShapeBackground> background = rootShape->background();QVector<KoShape::PaintOrder> paintOrder = rootShape->paintOrder();+ bool currentNodeInheritsBg = false;+ bool currentNodeInheritsStroke = false;+for (auto it = compositionBegin(textData); it != compositionEnd(textData); it++) {bool hasPaintOrder = it->properties.hasProperty(KoSvgTextProperties::PaintOrder);inheritPaintProperties(it, stroke, background, paintOrder);@@ -394,6 +396,11 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<CharacteKoShape::PaintOrder first = paintOrder.at(0);KoShape::PaintOrder second = paintOrder.at(1);+ if (it != compositionBegin(textData)) {+ currentNodeInheritsBg = currentNodeInheritsBg? !it->properties.hasProperty(KoSvgTextProperties::FillId): false;+ currentNodeInheritsStroke = currentNodeInheritsStroke? !it->properties.hasProperty(KoSvgTextProperties::StrokeId): false;+ }+if (it.state() == KisForestDetail::Enter) {@@ -408,6 +415,10 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteif (hasPaintOrder)shape->setPaintOrder(first, second);shapes.append(shape);+ if (currentNodeInheritsBg && !textDecorationColor.isValid()) {+ shape->setInheritBackground(true);+ }+ shape->setInheritStroke(currentNodeInheritsStroke);}if (textDecorations.contains(KoSvgText::DecorationOverline)) {KoPathShape *shape = KoPathShape::createShapeFromPainterPath(textDecorations.value(KoSvgText::DecorationOverline));@@ -422,6 +433,10 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteif (hasPaintOrder)shape->setPaintOrder(first, second);shapes.append(shape);+ if (currentNodeInheritsBg && !textDecorationColor.isValid()) {+ shape->setInheritBackground(true);+ }+ shape->setInheritStroke(currentNodeInheritsStroke);}if (childCount(siblingCurrent(it)) == 0) {@@ -448,6 +463,10 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteshape->setFillRule(Qt::WindingFill);shape->setPaintOrder(first, second);shapes.append(shape);+ if (replace && currentNodeInheritsBg) {+ shape->setInheritBackground(true);+ }+ shape->setInheritStroke(currentNodeInheritsStroke);}} else if (const auto *outlineGlyph = std::get_if<Glyph::Outline>(&result.at(i).glyph)) {chunk.addPath(tf.map(outlineGlyph->path));@@ -478,6 +497,8 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characterect->setTransformation(imageTf*tf);shapes.append(rect);+ rect->setInheritBackground(currentNodeInheritsBg);+ rect->setInheritStroke(currentNodeInheritsStroke);} else {shape->setSize(drawRect.size());shape->setTransformation(imageTf*tf);@@ -501,9 +522,18 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteif (hasPaintOrder)shape->setPaintOrder(first, second);shapes.append(shape);+ shape->setInheritBackground(currentNodeInheritsBg);+ shape->setInheritStroke(currentNodeInheritsStroke);currentIndex = j;}+ if (it == compositionBegin(textData)) {+ // We don't want the root to inherit stroke or fill, but after+ // that it should, so we turn both to inherit at the end of the+ // 'enter' code block for the root.+ currentNodeInheritsBg = true;+ currentNodeInheritsStroke = true;+ }}if (it.state() ==KisForestDetail::Leave) {inheritPaintProperties(it, stroke, background, paintOrder);@@ -520,6 +550,10 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Characteif (hasPaintOrder)shape->setPaintOrder(first, second);shapes.append(shape);+ if (currentNodeInheritsBg && !textDecorationColor.isValid()) {+ shape->setInheritBackground(true);+ }+ shape->setInheritStroke(currentNodeInheritsStroke);}}}@@ -536,14 +570,6 @@ KoSvgTextShape::Private::collectPaths(const KoShape *rootShape, QVector<Charactegroup->setStroke(rootShape->stroke());group->setPaintOrder(rootShape->paintOrder().first(), rootShape->paintOrder().at(1));group->setInheritPaintOrder(rootShape->inheritPaintOrder());- for(int i = 0; i < group->shapes().size(); i++) {- if (group->shapes().at(i)->background() == group->background()) {- group->shapes().at(i)->setInheritBackground(true);- }- if (group->shapes().at(i)->stroke() == group->stroke()) {- group->shapes().at(i)->setInheritStroke(true);- }- }parentShape = group;}diff --git a/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cppindex 7772b007cc..3921694d8f 100644--- a/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp+++ b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp@@ -29,7 +29,7 @@ struct KoShapeQtQuickLabel::Private {QScopedPointer<KoShapePainter> shapePainter;QString svgData;- QColor fgColor;+ QColor fgColor = Qt::black;bool fullColor = false;KoShapeQtQuickLabel::ScalingType scalingType = KoShapeQtQuickLabel::Fit;Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignVCenter;@@ -129,6 +129,7 @@ void KoShapeQtQuickLabel::setSvgData(const QString &newSvgData)QRectF bb = QRectF( 0, 0, 200, 200);QSizeF sz = bb.size();p.setResolution(bb, 72);+ p.setInheritByDefault(true);QList<KoShape*> shapes = p.parseSvg(doc.documentElement(), &sz);