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

Replace internalShapes in KoSvgTextShape with a KoShapePainter.

Wolthera van Hövell tot Westerflier2437cdae118d
diff --git a/libs/flake/svg/KoShapePainter.cpp b/libs/flake/svg/KoShapePainter.cpp
index 37ea59f249..27707e9b28 100644
--- a/libs/flake/svg/KoShapePainter.cpp
+++ b/libs/flake/svg/KoShapePainter.cpp
@@ -120,7 +120,6 @@ KoShapePainter::KoShapePainter()
KoShapePainter::~KoShapePainter()
{
- delete d;
}
void KoShapePainter::setShapes(const QList<KoShape*> &shapes)
@@ -205,3 +204,8 @@ QRectF KoShapePainter::contentRect() const
}
return bound;
}
+
+KoShapeManager *KoShapePainter::internalShapeManager() const
+{
+ return d->canvas->shapeManager();
+}
diff --git a/libs/flake/svg/KoShapePainter.h b/libs/flake/svg/KoShapePainter.h
index 37ea8b0a0c..603450fc47 100644
--- a/libs/flake/svg/KoShapePainter.h
+++ b/libs/flake/svg/KoShapePainter.h
@@ -10,11 +10,13 @@
#include <QList>
#include <QRectF>
+#include <QScopedPointer>
#include "kritaflake_export.h"
class KoShape;
class QPainter;
class QImage;
+class KoShapeManager;
/**
* A utility class to paint a subset of shapes onto a QPainter.
@@ -61,9 +63,16 @@ public:
/// Returns the bounding rect of the shapes to paint
QRectF contentRect() const;
+ /**
+ * @brief internalShapeManager
+ * KoShapePainter has an internal shape manager that is used to paint the shapes.
+ * @return the internal shape manager.
+ */
+ KoShapeManager *internalShapeManager() const;
+
private:
class Private;
- Private * const d;
+ QScopedPointer<Private> d;
};
#endif // KOSHAPEPAINTER_H
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index 67c8d6ce5b..1732e4e335 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -163,7 +163,7 @@ void KoSvgTextShape::shapeChanged(ChangeType type, KoShape *shape)
}
KoShape::shapeChanged(type, shape);
- if ((d->internalShapes.contains(shape))
+ if ((d->shapesInside.contains(shape) || d->shapesSubtract.contains(shape) || d->textPaths.contains(shape))
&& (type == PositionChanged
|| type == RotationChanged
|| type == ScaleChanged
@@ -175,8 +175,9 @@ void KoSvgTextShape::shapeChanged(ChangeType type, KoShape *shape)
//TODO: also handle type == Deleted. Need to figure out the order of operations though.
relayout();
}
- if ((!shape || shape == this) && type == ContentChanged) {
+ if ((!shape || shape == this) && (type == ContentChanged || type == ChildChanged)) {
relayout();
+ d->updateInternalShapesList();
}
}
@@ -1015,7 +1016,7 @@ void KoSvgTextShape::mergePropertiesIntoRange(const int startPos,
bool changed = false;
int currentIndex = 0;
KoSvgText::AutoValue inlineSize = d->textData.childBegin()->properties.propertyOrDefault(KoSvgTextProperties::InlineSizeId).value<KoSvgText::AutoValue>();
- bool isWrapping = !d->shapesInside().isEmpty() || !inlineSize.isAuto;
+ bool isWrapping = !d->shapesInside.isEmpty() || !inlineSize.isAuto;
for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {
if (KoSvgTextShape::Private::childCount(siblingCurrent(it)) > 0) {
continue;
@@ -1784,23 +1785,7 @@ bool KoSvgTextShape::fontMatchingDisabled() const
void KoSvgTextShape::paint(QPainter &painter) const
{
painter.save();
- if (!d->internalShapes.isEmpty()) {
- Q_FOREACH (KoShape* shape, d->internalShapes.keys()) {
- if (!shape->isVisible(false)) continue;
- painter.save();
- painter.setTransform(shape->transformation() * painter.transform());
- Q_FOREACH(const KoShape::PaintOrder p, shape->paintOrder()) {
- if (p == KoShape::Fill) {
- shape->paint(painter);
- } else if (p == KoShape::Stroke) {
- shape->paintStroke(painter);
- } else if (p == KoShape::Markers) {
- shape->paintMarkers(painter);
- }
- }
- painter.restore();
- }
- }
+ d->internalShapesPainter->paint(painter);
KoSvgText::TextRendering textRendering = KoSvgText::TextRendering(textProperties().propertyOrDefault(KoSvgTextProperties::TextRenderingId).toInt());
if (textRendering == KoSvgText::RenderingOptimizeSpeed || !painter.testRenderHint(QPainter::Antialiasing)) {
// also apply antialiasing only if antialiasing is active on provided target QPainter
@@ -1833,20 +1818,19 @@ void KoSvgTextShape::paintStroke(QPainter &painter) const
QPainterPath KoSvgTextShape::outline() const {
QPainterPath result;
- if (!d->internalShapes.isEmpty()) {
- //TODO: what about shape subtract?
- Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {
+ if (!d->internalShapes().isEmpty()) {
+ // TODO: Cache the computed wrapping shapes and draw their outlines instead.
+ Q_FOREACH(KoShape *shape, d->internalShapes()) {
result.addPath(shape->transformation().map(shape->outline()));
}
- } else {
- for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {
- result.addPath(it->associatedOutline);
- for (int i = 0; i < it->textDecorations.values().size(); ++i) {
- result.addPath(it->textDecorations.values().at(i));
- }
-
+ }
+ for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {
+ result.addPath(it->associatedOutline);
+ for (int i = 0; i < it->textDecorations.values().size(); ++i) {
+ result.addPath(it->textDecorations.values().at(i));
}
}
+
return result;
}
QRectF KoSvgTextShape::outlineRect() const
@@ -1856,40 +1840,37 @@ QRectF KoSvgTextShape::outlineRect() const
QRectF KoSvgTextShape::boundingRect() const
{
- QRectF result;
- if (!d->internalShapes.isEmpty()) {
- //TODO: what about invisible shapes?
- result = KoShape::boundingRect(d->internalShapes.keys());
- } else {
- QList<KoShapeStrokeModelSP> parentStrokes;
- for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {
- if (it.state() == KisForestDetail::Enter) {
- if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {
- parentStrokes.append(it->properties.property(KoSvgTextProperties::StrokeId).value<KoSvgText::StrokeProperty>().property);
- }
- } else {
- KoShapeStrokeModelSP stroke = parentStrokes.size() > 0? parentStrokes.last(): nullptr;
- QRectF bb = it->associatedOutline.boundingRect();
- QMap<KoSvgText::TextDecoration, QPainterPath> decorations = it->textDecorations;
- for (int i = 0; i < decorations.values().size(); ++i) {
- bb |= decorations.values().at(i).boundingRect();
- }
- if (!bb.isEmpty()) {
- if (stroke) {
- KoInsets insets;
- stroke->strokeInsets(this, insets);
- result |= bb.adjusted(-insets.left, -insets.top, insets.right, insets.bottom);
- } else {
- result |= bb;
- }
- }
- if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {
- // reset stroke to use parent stroke.
- parentStrokes.pop_back();
+ QRectF result = d->internalShapesPainter->contentRect();
+
+ QList<KoShapeStrokeModelSP> parentStrokes;
+ for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {
+ if (it.state() == KisForestDetail::Enter) {
+ if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {
+ parentStrokes.append(it->properties.property(KoSvgTextProperties::StrokeId).value<KoSvgText::StrokeProperty>().property);
+ }
+ } else {
+ KoShapeStrokeModelSP stroke = parentStrokes.size() > 0? parentStrokes.last(): nullptr;
+ QRectF bb = it->associatedOutline.boundingRect();
+ QMap<KoSvgText::TextDecoration, QPainterPath> decorations = it->textDecorations;
+ for (int i = 0; i < decorations.values().size(); ++i) {
+ bb |= decorations.values().at(i).boundingRect();
+ }
+ if (!bb.isEmpty()) {
+ if (stroke) {
+ KoInsets insets;
+ stroke->strokeInsets(this, insets);
+ result |= bb.adjusted(-insets.left, -insets.top, insets.right, insets.bottom);
+ } else {
+ result |= bb;
}
}
+ if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {
+ // reset stroke to use parent stroke.
+ parentStrokes.pop_back();
+ }
}
}
+
return this->absoluteTransformation().mapRect(result);
}
@@ -1908,8 +1889,8 @@ void KoSvgTextShape::setSize(const QSizeF &size)
const qreal scaleX = size.width() / oldSize.width();
const qreal scaleY = size.height() / oldSize.height();
- if (!d->internalShapes.isEmpty()) {
- Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {
+ if (!d->internalShapes().isEmpty()) {
+ Q_FOREACH(KoShape *shape, d->internalShapes()) {
KoFlake::resizeShapeCommon(shape, scaleX, scaleY, shape->boundingRect().topLeft() - oRect.topLeft(), false, false, this->transformation());
}
} else {
@@ -1932,12 +1913,12 @@ void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements)
}
//Debug shape outlines.
- Q_FOREACH (KoShape *shapeInside, d->shapesInside()) {
+ Q_FOREACH (KoShape *shapeInside, d->shapesInside) {
QPainterPath p = shapeInside->outline();
p = shapeInside->transformation().map(p);
painter.strokePath(p, QPen(Qt::green));
}
- Q_FOREACH (KoShape *shapeInside, d->shapesSubtract()) {
+ Q_FOREACH (KoShape *shapeInside, d->shapesSubtract) {
QPainterPath p = shapeInside->outline();
p = shapeInside->transformation().map(p);
painter.strokePath(p, QPen(Qt::red));
@@ -2009,58 +1990,54 @@ KoSvgTextShape::TextType KoSvgTextShape::textType() const
void KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside)
{
- Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {
- if (d->internalShapes.value(shape) == KoSvgTextShape::Private::InternalShapeState::ShapeInside) {
- shape->removeDependee(this);
- d->internalShapes.remove(shape);
- }
+ Q_FOREACH(KoShape *shape, d->shapesInside) {
+ shape->removeDependee(this);
+ delete shape;
}
- Q_FOREACH(KoShape *shape, shapesInside) {
- d->internalShapes.insert(shape, KoSvgTextShape::Private::InternalShapeState::ShapeInside);
+ d->shapesInside = shapesInside;
+ Q_FOREACH(KoShape *shape, d->shapesInside) {
shape->addDependee(this);
}
- shapeChangedPriv(ContentChanged);
+ shapeChangedPriv(ChildChanged);
}
QList<KoShape *> KoSvgTextShape::shapesInside() const
{
- return d->shapesInside();
+ return d->shapesInside;
}
void KoSvgTextShape::setShapesSubtract(QList<KoShape *> shapesSubtract)
{
- Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {
- if (d->internalShapes.value(shape) == KoSvgTextShape::Private::InternalShapeState::ShapeSubtract) {
- shape->removeDependee(this);
- d->internalShapes.remove(shape);
- }
+ Q_FOREACH(KoShape *shape, d->shapesSubtract) {
+ shape->removeDependee(this);
+ delete shape;
}
- Q_FOREACH(KoShape *shape, shapesSubtract) {
- d->internalShapes.insert(shape, KoSvgTextShape::Private::InternalShapeState::ShapeSubtract);
+ d->shapesSubtract = shapesSubtract;
+ Q_FOREACH(KoShape *shape, d->shapesSubtract) {
shape->addDependee(this);
}
- shapeChangedPriv(ContentChanged);
+ shapeChangedPriv(ChildChanged);
}
QList<KoShape *> KoSvgTextShape::shapesSubtract() const
{
- return d->shapesSubtract();
+ return d->shapesSubtract;
}
QMap<QString, QString> KoSvgTextShape::shapeTypeSpecificStyles(SvgSavingContext &context) const
{
QMap<QString, QString> map = this->textProperties().convertParagraphProperties();
- if (!d->shapesInside().isEmpty()) {
+ if (!d->shapesInside.isEmpty()) {
QStringList shapesInsideList;
- Q_FOREACH(KoShape* shape, d->shapesInside()) {
+ Q_FOREACH(KoShape* shape, d->shapesInside) {
QString id = SvgStyleWriter::embedShape(shape, context);
shapesInsideList.append(QString("url(#%1)").arg(id));
}
map.insert("shape-inside", shapesInsideList.join(" "));
}
- if (!d->shapesSubtract().isEmpty()) {
+ if (!d->shapesSubtract.isEmpty()) {
QStringList shapesInsideList;
- Q_FOREACH(KoShape* shape, d->shapesSubtract()) {
+ Q_FOREACH(KoShape* shape, d->shapesSubtract) {
QString id = SvgStyleWriter::embedShape(shape, context);
shapesInsideList.append(QString("url(#%1)").arg(id));
}
diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.h
index 1e1b84ce04..56f72e62fa 100644
--- a/libs/flake/text/KoSvgTextShape_p.h
+++ b/libs/flake/text/KoSvgTextShape_p.h
@@ -12,6 +12,8 @@
#include "KoSvgText.h"
#include "KoSvgTextContentElement.h"
+#include <KoShapePainter.h>
+#include <KoShapeManager.h>
#include "KoCssTextUtils.h"
@@ -438,7 +440,9 @@ public:
// the shape, though it will be reset locally if the
// accessing thread changes
- Private() = default;
+ Private(): internalShapesPainter(new KoShapePainter) {
+
+ }
enum InternalShapeState {
Decorative,
@@ -448,14 +452,14 @@ public:
};
Private(const Private &rhs)
- : textData(rhs.textData) {
+ : internalShapesPainter(new KoShapePainter)
+ , textData(rhs.textData) {
- Q_FOREACH (KoShape *shape, rhs.internalShapes.keys()) {
- KoShape *clonedShape = shape->cloneShape();
- KIS_ASSERT_RECOVER(clonedShape) { continue; }
+ cloneShapes(rhs.shapesInside, shapesInside);
+ cloneShapes(rhs.shapesSubtract, shapesSubtract);
+ cloneShapes(rhs.textPaths, textPaths);
+ updateInternalShapesList();
- internalShapes.insert(clonedShape, rhs.internalShapes.value(shape));
- }
yRes = rhs.yRes;
xRes = rhs.xRes;
result = rhs.result;
@@ -471,36 +475,43 @@ public:
disableFontMatching = rhs.disableFontMatching;
}
+ void cloneShapes(const QList<KoShape*> &sourceShapeList, QList<KoShape*> &destinationShapeList) {
+ Q_FOREACH (KoShape *shape, sourceShapeList) {
+ KoShape *clonedShape = shape->cloneShape();
+ KIS_ASSERT_RECOVER(clonedShape) { continue; }
+ destinationShapeList.append(clonedShape);
+ }
+ }
+
~Private() {
- QList<KoShape*> internalShapeList = internalShapes.keys();
- internalShapes.clear();
- qDeleteAll(internalShapeList);
+ qDeleteAll(shapesInside);
+ shapesInside.clear();
+ qDeleteAll(shapesSubtract);
+ shapesSubtract.clear();
+ qDeleteAll(textPaths);
+ textPaths.clear();
}
int xRes = 72;
int yRes = 72;
- QMap<KoShape*, InternalShapeState> internalShapes;
+ QScopedPointer<KoShapePainter> internalShapesPainter;
- QList<KoShape*> shapesInside() {
- QList<KoShape*> shapes;
- Q_FOREACH(KoShape *shape, internalShapes.keys()) {
- if (internalShapes.value(shape) == InternalShapeState::ShapeInside) {
- shapes.append(shape);
- }
- }
- return shapes;
+ QList<KoShape*> internalShapes() const {
+ return internalShapesPainter->internalShapeManager()->shapes();
}
- QList<KoShape*> shapesSubtract() {
- QList<KoShape*> shapes;
- Q_FOREACH(KoShape *shape, internalShapes.keys()) {
- if (internalShapes.value(shape) == InternalShapeState::ShapeSubtract) {
- shapes.append(shape);
- }
- }
- return shapes;
+
+ void updateInternalShapesList() {
+ QList<KoShape*> total = shapesInside;
+ total.append(shapesSubtract);
+ total.append(textPaths);
+ internalShapesPainter->setShapes(total);
}
+ QList<KoShape*> shapesInside;
+ QList<KoShape*> shapesSubtract;
+ QList<KoShape*> textPaths;
+
KisForest<KoSvgTextContentElement> textData;
bool isLoading = false; ///< Turned on when loading in text data, blocks updates to shape listeners.
diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cpp
index 1de8c65c1e..8e87a036b0 100644
--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp
@@ -241,7 +241,7 @@ void KoSvgTextShape::Private::relayout()
// to consider it anything but required to have both shaping and bidi-reorder break.
QVector<KoSvgText::CharTransformation> resolvedTransforms(text.size());
globalIndex = 0;
- bool wrapped = !(inlineSize.isAuto && this->shapesInside().isEmpty());
+ bool wrapped = !(inlineSize.isAuto && this->shapesInside.isEmpty());
if (!resolvedTransforms.isEmpty()) {
resolvedTransforms[0].xPos = 0;
resolvedTransforms[0].yPos = 0;
@@ -644,8 +644,8 @@ void KoSvgTextShape::Private::relayout()
// Handle linebreaking.
QPointF startPos = resolvedTransforms.value(0).absolutePos() - result.value(0).dominantBaselineOffset;
- if (!this->shapesInside().isEmpty()) {
- QList<QPainterPath> shapes = getShapes(this->shapesInside(), this->shapesSubtract(), rootProperties);
+ if (!this->shapesInside.isEmpty()) {
+ QList<QPainterPath> shapes = getShapes(this->shapesInside, this->shapesSubtract, rootProperties);
this->lineBoxes = flowTextInShapes(rootProperties, logicalToVisual, result, shapes, startPos, resHandler);
} else {
this->lineBoxes = breakLines(rootProperties, logicalToVisual, result, startPos, resHandler);
@@ -655,7 +655,7 @@ void KoSvgTextShape::Private::relayout()
globalIndex = 0;
this->handleLineBoxAlignment(textData.childBegin(), result, this->lineBoxes, globalIndex, isHorizontal, KoSvgTextProperties::defaultProperties());
- if (inlineSize.isAuto && this->shapesInside().isEmpty()) {
+ if (inlineSize.isAuto && this->shapesInside.isEmpty()) {
debugFlake << "Starting with SVG 1.1 specific portion";
debugFlake << "4. Adjust positions: dx, dy";
// 4. Adjust positions: dx, dy