Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Save and load gradient and color for vectors, fix dashes.

Wolthera van Hövell tot Westerflier6b9e4a6afaa8
diff --git a/libs/psd/psd_additional_layer_info_block.h b/libs/psd/psd_additional_layer_info_block.h
index 5836e94021..2cf5c99522 100644
--- a/libs/psd/psd_additional_layer_info_block.h
+++ b/libs/psd/psd_additional_layer_info_block.h
@@ -207,15 +207,18 @@ struct KRITAPSD_EXPORT psd_layer_solid_color {
QDomDocument getASLXML() {
KisAslXmlWriter w;
w.enterDescriptor("", "", "null");
+ writeASL(w);
+ w.leaveDescriptor();
+
+ return w.document();
+ }
+
+ void writeASL(KisAslXmlWriter &w) {
if (cs) {
w.writeColor("Clr ", fill_color.convertedTo(cs));
} else {
w.writeColor("Clr ", fill_color);
}
-
- w.leaveDescriptor();
-
- return w.document();
}
QBrush getBrush() {
@@ -462,7 +465,14 @@ struct KRITAPSD_EXPORT psd_layer_gradient_fill {
QDomDocument getASLXML() {
KisAslXmlWriter w;
w.enterDescriptor("", "", "null");
+ writeASL(w);
+ w.leaveDescriptor();
+
+ return w.document();
+ }
+
+ void writeASL(KisAslXmlWriter &w) {
if (!gradient.isNull()) {
const QDomElement gradientElement = gradient.firstChildElement();
if (!gradientElement.isNull()) {
@@ -501,16 +511,11 @@ struct KRITAPSD_EXPORT psd_layer_gradient_fill {
w.writeBoolean("Algn", align_with_layer);
w.writeUnitFloat("Scl ", "#Prc", scale);
w.writePoint("Ofst", offset);
-
- w.leaveDescriptor();
-
- return w.document();
}
QBrush getBrush() {
QGradient *grad = getGradient();
if (grad) {
- grad->setCoordinateMode(QGradient::ObjectBoundingMode);
QBrush brush = *grad;
return brush;
}
@@ -535,17 +540,33 @@ struct KRITAPSD_EXPORT psd_layer_gradient_fill {
}
}
}
+ if (pointer) {
+ if (repeat == "alternate") {
+ pointer->setSpread(QGradient::ReflectSpread);
+ }
+ pointer->setCoordinateMode(QGradient::ObjectBoundingMode);
+ QLinearGradient *g = static_cast<QLinearGradient*>(pointer);
+ if (g) {
+ g->setStart(QPointF(0,0.5));
+ g->setFinalStop(QPointF(1.0, 0.5));
+ pointer = g;
+ }
+ }
+
return pointer;
}
QSharedPointer<KoShapeBackground> getBackground() {
QGradient *pointer = getGradient();
- pointer->setCoordinateMode(QGradient::ObjectBoundingMode);
- if (repeat == "alternate") {
- pointer->setSpread(QGradient::RepeatSpread);
- }
QSharedPointer<KoGradientBackground> bg = QSharedPointer<KoGradientBackground>(new KoGradientBackground(pointer));
return bg;
}
+
+ bool svgCompatible() {
+ if (style == "radial" || style == "linear") {
+ return true;
+ }
+ return false;
+ }
};
struct KRITAPSD_EXPORT psd_layer_pattern_fill {
@@ -640,14 +661,23 @@ struct KRITAPSD_EXPORT psd_layer_pattern_fill {
KisAslXmlWriter w;
w.enterDescriptor("", "", "null");
- // pattern ref
- w.enterDescriptor("Ptrn", "", "Ptrn");
-
- w.writeText("Nm ", patternName);
if (patternID.isEmpty()) {
qWarning() << "This pattern cannot be saved: No pattern UUID available.";
return QDomDocument();
}
+
+ writeASL(w);
+
+ w.leaveDescriptor();
+
+ return w.document();
+ }
+
+ void writeASL(KisAslXmlWriter &w) {
+ // pattern ref
+ w.enterDescriptor("Ptrn", "", "Ptrn");
+
+ w.writeText("Nm ", patternName);
w.writeText("Idnt", patternID);
w.leaveDescriptor();
@@ -657,10 +687,6 @@ struct KRITAPSD_EXPORT psd_layer_pattern_fill {
w.writeUnitFloat("Scl ", "#Prc", scale);
w.writeUnitFloat("Angl", "#Ang", angle);
w.writePoint("phase", offset);
-
- w.leaveDescriptor();
-
- return w.document();
}
QSharedPointer<KoShapeBackground> getBackground() {
@@ -673,8 +699,10 @@ struct KRITAPSD_EXPORT psd_layer_pattern_fill {
pattern = res.resource<KoPattern>();
if (pattern) {
bg->setPattern(pattern->pattern());
+ } else {
+ res = KisGlobalResourcesInterface::instance()->source(ResourceType::Patterns).fallbackResource();
+ bg->setPattern(res.resource<KoPattern>()->pattern());
}
- bg->setTileRepeatOffset(offset);
return bg;
}
};
@@ -841,16 +869,28 @@ struct KRITAPSD_EXPORT psd_vector_mask {
};
struct KRITAPSD_EXPORT psd_vector_stroke_data {
+
+ int strokeVersion {2};
+
bool strokeEnabled {false};
bool fillEnabled {true};
QPen pen;
+ bool gradient{false};
+
+ bool scaleLock {false};
+ bool strokeAdjust {false};
QVector<double> dashPattern;
double opacity {1.0};
double resolution {72};
+ bool pixelWidth {false};
+
+ void setVersion(int version) {
+ strokeVersion = version;
+ }
void setStrokeEnabled(bool enabled) {
strokeEnabled = enabled;
@@ -860,6 +900,11 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
}
void setStrokeWidth(double width) {
pen.setWidthF(width);
+ pixelWidth = false;
+ }
+ void setStrokePixel(double width) {
+ pen.setWidthF(width);
+ pixelWidth = true;
}
void setStrokeDashOffset(double dashOffset) {
@@ -887,8 +932,18 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
}
}
+ void setScaleLock(bool enabled) {
+ scaleLock = enabled;
+ }
+
+ void setStrokeAdjust(bool enabled) {
+ strokeAdjust = enabled;
+ }
+
void appendToDashPattern(double val) {
- dashPattern.append(val);
+ if (val > 0) {
+ dashPattern.append(val);
+ }
}
void setOpacityFromPercentage(double o) {
opacity = o * 0.01;
@@ -898,23 +953,30 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
}
void loadFromShapeStroke(KoShapeStrokeSP stroke) {
- pen.setWidthF(stroke->lineWidth());
- pen.setCapStyle(stroke->capStyle());
- pen.setJoinStyle(stroke->joinStyle());
- pen.setDashOffset(stroke->dashOffset());
- pen.setMiterLimit(stroke->miterLimit());
+ pen = stroke->resultLinePen();
+ gradient = stroke->lineBrush().gradient()? true: false;
+ opacity = stroke->color().alphaF();
dashPattern = stroke->lineDashes();
}
static void setupCatcher(const QString path, KisAslCallbackObjectCatcher &catcher, psd_vector_stroke_data *data) {
+ catcher.subscribeInteger(path + "/strokeStyle/strokeStyleVersion", std::bind(&psd_vector_stroke_data::setVersion, data, std::placeholders::_1));
catcher.subscribeBoolean(path + "/strokeStyle/strokeEnabled", std::bind(&psd_vector_stroke_data::setStrokeEnabled, data, std::placeholders::_1));
catcher.subscribeBoolean(path + "/strokeStyle/fillEnabled", std::bind(&psd_vector_stroke_data::setFillEnabled, data, std::placeholders::_1));
+
catcher.subscribeUnitFloat(path + "/strokeStyle/strokeStyleLineWidth", "#Pnt", std::bind(&psd_vector_stroke_data::setStrokeWidth, data, std::placeholders::_1));
+ //catcher.subscribeUnitFloat(path + "/strokeStyle/strokeStyleLineWidth", "#Pxl", std::bind(&psd_vector_stroke_data::setStrokePixel, data, std::placeholders::_1));
catcher.subscribeUnitFloat(path + "/strokeStyle/strokeStyleLineDashOffset", "#Pnt", std::bind(&psd_vector_stroke_data::setStrokeDashOffset, data, std::placeholders::_1));
catcher.subscribeDouble(path + "/strokeStyle/strokeStyleMiterLimit", std::bind(&psd_vector_stroke_data::setStrokeMiterLimit, data, std::placeholders::_1));
+
catcher.subscribeEnum(path + "/strokeStyle/strokeStyleLineCapType", "strokeStyleLineCapType", std::bind(&psd_vector_stroke_data::setLineCapType, data, std::placeholders::_1));
catcher.subscribeEnum(path + "/strokeStyle/strokeStyleLineJoinType", "strokeStyleLineJoinType", std::bind(&psd_vector_stroke_data::setLineJoinType, data, std::placeholders::_1));
+
+ catcher.subscribeBoolean(path + "/strokeStyle/strokeStyleScaleLock", std::bind(&psd_vector_stroke_data::setScaleLock, data, std::placeholders::_1));
+ catcher.subscribeBoolean(path + "/strokeStyle/strokeStyleStrokeAdjust", std::bind(&psd_vector_stroke_data::setStrokeAdjust, data, std::placeholders::_1));
+
catcher.subscribeUnitFloat(path + "/strokeStyle/strokeStyleLineDashSet/", "#Nne", std::bind(&psd_vector_stroke_data::appendToDashPattern, data, std::placeholders::_1));
+
catcher.subscribeUnitFloat(path + "/strokeStyle/strokeStyleOpacity", "#Prc", std::bind(&psd_vector_stroke_data::setOpacityFromPercentage, data, std::placeholders::_1));
catcher.subscribeDouble(path + "/strokeStyle/strokeStyleResolution", std::bind(&psd_vector_stroke_data::setResolution, data, std::placeholders::_1));
}
@@ -953,7 +1015,7 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
w.writeBoolean("strokeStyleStrokeAdjust", false);
w.enterList("strokeStyleLineDashSet");
- Q_FOREACH(qreal val, dashPattern) {
+ Q_FOREACH(double val, dashPattern) {
w.writeUnitFloat("", "#Nne", val);
}
w.leaveList();
@@ -962,12 +1024,23 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
w.writeUnitFloat("strokeStyleOpacity ", "#Prc", opacity * 100.0);
// Color Descriptor
- // Others are "gradientLayer" and "patternLayer"
- w.enterDescriptor("strokeStyleContent", "", "solidColorLayer");
- KoColor c = KoColor();
- c.setOpacity(1.0);
- w.writeColor("Clr ", c);
- w.leaveDescriptor();
+ // Missing is "patternLayer"
+ if (gradient) {
+ w.enterDescriptor("strokeStyleContent", "", "gradientLayer");
+ psd_layer_gradient_fill fill;
+ fill.setGradient(KoStopGradient::fromQGradient(pen.brush().gradient()));
+ fill.writeASL(w);
+ w.leaveDescriptor();
+ } else {
+ w.enterDescriptor("strokeStyleContent", "", "solidColorLayer");
+ KoColor c (KoColorSpaceRegistry::instance()->rgb8());
+ c.fromQColor(pen.color());
+ c.setOpacity(1.0);
+ psd_layer_solid_color fill;
+ fill.fill_color = c;
+ fill.writeASL(w);
+ w.leaveDescriptor();
+ }
w.writeDouble("strokeStyleResolution", resolution);
@@ -975,6 +1048,29 @@ struct KRITAPSD_EXPORT psd_vector_stroke_data {
return w.document();
}
+
+ void setupShapeStroke(KoShapeStrokeSP stroke) {
+ double width = pen.widthF();
+ if (pixelWidth) {
+ width = (width / resolution) * 72.0;
+ }
+ stroke->setLineWidth(width);
+ stroke->setCapStyle(pen.capStyle());
+ stroke->setJoinStyle(pen.joinStyle());
+ stroke->setDashOffset(pen.dashOffset());
+ stroke->setMiterLimit(pen.miterLimit());
+ if (dashPattern.isEmpty()) {
+ stroke->setLineStyle(Qt::SolidLine, QVector<double>());
+ } else {
+ if (dashPattern.size() % 2 > 0) {
+ QVector<double> pattern = dashPattern;
+ pattern.append(dashPattern);
+ stroke->setLineStyle(Qt::CustomDashLine, pattern);
+ } else {
+ stroke->setLineStyle(Qt::CustomDashLine, dashPattern);
+ }
+ }
+ }
};
/**
diff --git a/libs/psd/psd_layer_section.cpp b/libs/psd/psd_layer_section.cpp
index 2111a2379a..dc224fce78 100644
--- a/libs/psd/psd_layer_section.cpp
+++ b/libs/psd/psd_layer_section.cpp
@@ -675,6 +675,8 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_c
if (pathShape){
layerRecord->addPathShapeToPSDPath(vectorMask.path, pathShape, vectorWidth, vectorHeight);
KoColorBackground *b = dynamic_cast<KoColorBackground *>(pathShape->background().data());
+ KoGradientBackground *g = dynamic_cast<KoGradientBackground *>(pathShape->background().data());
+ KoPatternBackground *p = dynamic_cast<KoPatternBackground *>(pathShape->background().data());
if (b) {
psd_layer_solid_color fill;
@@ -686,6 +688,15 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_c
fill.setColor(KoColor(b->color(), fill.cs));
fillConfig = fill.getASLXML();
fillType = psd_fill_solid_color;
+ } else if (g) {
+ psd_layer_gradient_fill fill;
+ fill.setGradient(KoStopGradient::fromQGradient(g->gradient()));
+ fillConfig = fill.getASLXML();
+ fillType = psd_fill_gradient;
+ } else if (p) {
+ psd_layer_pattern_fill fill;
+ fillConfig = fill.getASLXML();
+ fillType = psd_fill_pattern;
} else if (!pathShape->background()) {
psd_layer_solid_color fill;
diff --git a/libs/psdutils/asl/kis_asl_writer.cpp b/libs/psdutils/asl/kis_asl_writer.cpp
index 30141ecec7..f8ce73363d 100644
--- a/libs/psdutils/asl/kis_asl_writer.cpp
+++ b/libs/psdutils/asl/kis_asl_writer.cpp
@@ -81,7 +81,9 @@ void parseElement(const QDomElement &el, QIODevice &device, bool forceTypeInfo =
double v = KisDomUtils::toDouble(el.attribute("value", "0"));
QString unit = el.attribute("unit", "#Pxl");
- writeVarString<byteOrder>(key, device);
+ if (!key.isEmpty()) {
+ writeVarString<byteOrder>(key, device);
+ }
writeFixedString<byteOrder>("UntF", device);
writeFixedString<byteOrder>(unit, device);
SAFE_WRITE_EX(byteOrder, device, v);
diff --git a/plugins/impex/psd/psd_loader.cpp b/plugins/impex/psd/psd_loader.cpp
index 015f477269..783635c0e1 100644
--- a/plugins/impex/psd/psd_loader.cpp
+++ b/plugins/impex/psd/psd_loader.cpp
@@ -389,24 +389,13 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)
QColor c = fill.getBrush().color();
c.setAlphaF(data.opacity);
stroke->setColor(c);
- if (!grad.getBrush().isOpaque()) {
- stroke->setLineBrush(fill.getBrush());
- } else {
+ if (grad.getBrush().gradient()) {
stroke->setLineBrush(grad.getBrush());
}
} else {
stroke->setColor(Qt::transparent);
}
- stroke->setLineWidth(data.pen.widthF());
- stroke->setCapStyle(data.pen.capStyle());
- stroke->setJoinStyle(data.pen.joinStyle());
- stroke->setDashOffset(data.pen.dashOffset());
- stroke->setMiterLimit(data.pen.miterLimit());
- if (data.dashPattern.isEmpty()) {
- stroke->setLineStyle(Qt::SolidLine, QVector<double>());
- } else {
- stroke->setLineStyle(Qt::CustomDashLine, data.dashPattern);
- }
+ data.setupShapeStroke(stroke);
vectorMask->setStroke(stroke);
}