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

Type setting: ensure baseline shift adjustment works properly.

Wolthera van Hövell tot Westerflierf7c0b195d145
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cpp
index 07b71c84c3..2e662b6ce6 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp
+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp
@@ -420,9 +420,63 @@ bool SvgTextCursor::setDominantBaselineFromHandle(const TypeSettingModeHandle ha
}
KoSvgTextProperties props;
props.setProperty(KoSvgTextProperties::DominantBaselineId, QVariant::fromValue(baseline));
+ props.setProperty(KoSvgTextProperties::AlignmentBaselineId, QVariant::fromValue(baseline));
mergePropertiesIntoSelection(props);
return true;
}
+// The baselines that need to get precendence over the others need to go later in the list.
+QMap<SvgTextCursor::TypeSettingModeHandle, int> typeSettingBaselinesFromMetrics(const KoSvgText::FontMetrics metrics, const qreal lineGap, const bool isHorizontal) {
+ return QMap<SvgTextCursor::TypeSettingModeHandle, int> {
+ {SvgTextCursor::Ascender, metrics.ascender},
+ {SvgTextCursor::Descender, metrics.descender},
+ {SvgTextCursor::BaselineAlphabetic, metrics.alphabeticBaseline},
+ {SvgTextCursor::BaselineIdeographic, metrics.ideographicUnderBaseline},
+ {SvgTextCursor::BaselineHanging, metrics.hangingBaseline},
+ {SvgTextCursor::BaselineMathematical, metrics.mathematicalBaseline},
+ {SvgTextCursor::BaselineMiddle, isHorizontal? metrics.xHeight/2: metrics.ideographicCenterBaseline},
+ {SvgTextCursor::BaselineCentral, metrics.ideographicCenterBaseline},
+ {SvgTextCursor::LineHeightTop, metrics.ascender+(lineGap/2)},
+ {SvgTextCursor::LineHeightBottom, metrics.descender-(lineGap/2)},
+ {SvgTextCursor::BaselineShift, 0}
+ };
+}
+
+int SvgTextCursor::posForTypeSettingHandleAndRect(const TypeSettingModeHandle handle, const QRectF regionOfInterest)
+{
+ if (!d->shape) return 0;
+
+ QList<KoSvgTextCharacterInfo> infos =
+ d->shape->getPositionsAndRotationsForRange(d->pos, d->anchor);
+ if (infos.size() < 1) return 0;
+
+ const QRectF roi = d->shape->documentToShape(regionOfInterest);
+
+ for (auto it = infos.begin(); it != infos.end(); it++) {
+ const KoSvgTextProperties props = d->shape->propertiesForPos(d->shape->posForIndex(it->logicalIndex), true);
+ KoSvgText::LineHeightInfo lineHeight = props.propertyOrDefault(KoSvgTextProperties::LineHeightId).value<KoSvgText::LineHeightInfo>();
+
+ KoSvgText::FontMetrics metrics = it->metrics;
+ const bool isHorizontal = d->shape->writingMode() == KoSvgText::HorizontalTB;
+
+ const qreal scaleMetrics = props.fontSize().value/qreal(metrics.fontSize);
+ const int lineGap = lineHeight.isNormal? metrics.lineGap: (lineHeight.length.value/scaleMetrics)-(metrics.ascender-metrics.descender);
+
+ QTransform t = QTransform::fromTranslate(it->finalPos.x(), it->finalPos.y());
+ t.rotate(it->rotateDeg);
+
+ const QMap<SvgTextCursor::TypeSettingModeHandle, int> types
+ = typeSettingBaselinesFromMetrics(metrics, lineGap, isHorizontal);
+
+ const int metric = types.value(handle);
+ QPointF offset = isHorizontal? QPointF(0, -(metric*scaleMetrics)): QPointF(metric*scaleMetrics, 0);
+ QLineF line = t.map(QLineF(offset, offset+it->advance));
+ if (KisAlgebra2D::intersectLineRect(line, roi.toAlignedRect(), false)) return d->shape->posForIndex(it->logicalIndex);
+ }
+
+ return 0;
+}
+
+
void SvgTextCursor::moveCursor(MoveMode mode, bool moveAnchor)
{
@@ -649,9 +703,9 @@ void SvgTextCursor::deselectText()
setPos(d->pos, d->pos);
}
-static QColor bgColorForCaret(QColor c) {
+static QColor bgColorForCaret(QColor c, int opacity = 64) {
- return KisPaintingTweaks::luminosityCoarse(c) > 0.8? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
+ return KisPaintingTweaks::luminosityCoarse(c) > 0.8? QColor(0, 0, 0, opacity) : QColor(255, 255, 255, opacity);
}
void SvgTextCursor::paintDecorations(QPainter &gc, QColor selectionColor, int decorationThickness, qreal handleRadius)
@@ -705,21 +759,12 @@ void SvgTextCursor::paintDecorations(QPainter &gc, QColor selectionColor, int de
helper.setHandleStyle(highlight);
helper.drawPath(p);
- QString name = handleName(handle);
- if (!name.isEmpty()) {
- QPainterPath textP;
- textP.addText(painterTf.map(d->typeSettingDecor.closestBaselinePoint), gc.font(), name);
- gc.save();
- QPen pen(bgColorForCaret(selectionColor));
- pen.setWidth(decorationThickness*2);
- gc.setPen(pen);
- gc.drawPath(textP);
- gc.fillPath(textP, QBrush(selectionColor));
- gc.restore();
- }
+
} else {
gc.save();
- gc.setPen(selectionColor);
+ QPen pen(selectionColor, decorationThickness, handle == BaselineShift? Qt::SolidLine: Qt::DashLine);
+ pen.setCosmetic(true);
+ gc.setPen(pen);
gc.setOpacity(0.5);
gc.drawPath(painterTf.map(p));
gc.restore();
@@ -733,6 +778,19 @@ void SvgTextCursor::paintDecorations(QPainter &gc, QColor selectionColor, int de
helper.setHandleStyle(d->hoveredTypeSettingHandle == StartPos? highlight: regular);
helper.drawHandleRect(d->typeSettingDecor.handles.first);
}
+ QString name = handleName(d->hoveredTypeSettingHandle);
+ if (!name.isEmpty()) {
+ QPainterPath textP;
+ textP.addText(painterTf.map(d->typeSettingDecor.closestBaselinePoint), gc.font(), name);
+ gc.save();
+ QPen pen(bgColorForCaret(selectionColor, 255));
+ pen.setCosmetic(true);
+ pen.setWidth(decorationThickness);
+ gc.setPen(pen);
+ gc.drawPath(textP);
+ gc.fillPath(textP, QBrush(selectionColor));
+ gc.restore();
+ }
}
gc.restore();
@@ -1624,19 +1682,8 @@ void SvgTextCursor::updateTypeSettingDecoration()
QTransform t = QTransform::fromTranslate(it->finalPos.x(), it->finalPos.y());
t.rotate(it->rotateDeg);
- const QMap<SvgTextCursor::TypeSettingModeHandle, int> types {
- {Ascender, metrics.ascender},
- {Descender, metrics.descender},
- {BaselineAlphabetic, metrics.alphabeticBaseline},
- {BaselineIdeographic, metrics.ideographicUnderBaseline},
- {BaselineHanging, metrics.hangingBaseline},
- {BaselineCentral, metrics.ideographicCenterBaseline},
- {BaselineMathematical, metrics.mathematicalBaseline},
- {BaselineMiddle, isHorizontal? metrics.xHeight/2: metrics.ideographicCenterBaseline},
- {LineHeightTop, metrics.ascender+(lineGap/2)},
- {LineHeightBottom, metrics.descender-(lineGap/2)},
- {BaselineShift, 0}
- };
+ const QMap<SvgTextCursor::TypeSettingModeHandle, int> types
+ = typeSettingBaselinesFromMetrics(metrics, lineGap, isHorizontal);
Q_FOREACH(SvgTextCursor::TypeSettingModeHandle handle, types.keys()) {
const int metric = types.value(handle);
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.h
index a0d00da327..98ec0160af 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.h
+++ b/plugins/tools/svgtexttool/SvgTextCursor.h
@@ -137,13 +137,36 @@ public:
/// Get typeSettingMode handle for text;
TypeSettingModeHandle typeSettingHandleAtPos(const QRectF regionOfInterest);
+
+ /// Set a given typesetting handle as hovered, so it will be drawn as such.
void setTypeSettingHandleHovered(TypeSettingModeHandle hovered = TypeSettingModeHandle::NoHandle);
+
+ /// Whether to draw the type setting handles.
+ /// Turned off when the typesetting strategy is active to give artists more control.
void setDrawTypeSettingHandle(bool draw);
+ /**
+ * @brief handleName
+ * @return translated name of a given handle.
+ */
QString handleName(TypeSettingModeHandle handle) const;
+ /**
+ * @brief setDominantBaselineFromHandle
+ * Set the dominant baseline from a given handle.
+ * @return true if dominant baseline was set, false if the handle doesn't
+ * correspond to a baseline.
+ */
bool setDominantBaselineFromHandle(const TypeSettingModeHandle handle);
+ /**
+ * @brief posForHandleAndRect
+ * Returns the closest cursor position for a given region and typesetting handle.
+ * Used by the type setting mode to find the relevant metrics to scale.
+ * @return cursor pos closest.
+ */
+ int posForTypeSettingHandleAndRect(const TypeSettingModeHandle handle, const QRectF regionOfInterest);
+
/// Move the cursor, and, if you don't want a selection, move the anchor.
void moveCursor(MoveMode mode, bool moveAnchor = true);
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 68c39bd557..d2ed0ae181 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -750,7 +750,17 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)
KoSvgTextShape *selectedShape = this->selectedShape();
if (selectedShape) {
- if (m_highlightItem == HighlightItem::MoveBorder) {
+ if (m_highlightItem == HighlightItem::TypeSettingHandle) {
+ SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));
+ if (handle != SvgTextCursor::NoHandle) {
+ if (!m_textCursor.setDominantBaselineFromHandle(handle)) {
+ m_interactionStrategy.reset(new SvgTextTypeSettingStrategy(this, selectedShape, &m_textCursor, handleGrabRect(event->point)));
+ m_textCursor.setDrawTypeSettingHandle(false);
+ }
+ event->accept();
+ return;
+ }
+ } else if (m_highlightItem == HighlightItem::MoveBorder) {
m_interactionStrategy.reset(new SvgMoveTextStrategy(this, selectedShape, event->point));
m_dragging = DragMode::Move;
event->accept();
@@ -784,15 +794,7 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)
canvas()->shapeManager()->selection()->select(hoveredShape);
m_hoveredShapeHighlightRect = QPainterPath();
}
- SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));
- if (handle != SvgTextCursor::NoHandle && hoveredShape == selectedShape) {
- if (!m_textCursor.setDominantBaselineFromHandle(handle)) {
- m_interactionStrategy.reset(new SvgTextTypeSettingStrategy(this, selectedShape, &m_textCursor, handleGrabRect(event->point)));
- m_textCursor.setDrawTypeSettingHandle(false);
- }
- } else {
- m_interactionStrategy.reset(new SvgSelectTextStrategy(this, &m_textCursor, event->point));
- }
+ m_interactionStrategy.reset(new SvgSelectTextStrategy(this, &m_textCursor, event->point));
m_dragging = DragMode::Select;
event->accept();
} else if (crossLayerPossible) {
@@ -872,15 +874,24 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
cursor = m_ibeam_horizontal_done;
const qreal sensitivity = grabSensitivityInPt();
- if (std::optional<InlineSizeInfo> info = InlineSizeInfo::fromShape(selectedShape)) {
- const QPolygonF zone = info->endLineGrabRect(sensitivity);
- const QPolygonF startZone = info->startLineGrabRect(sensitivity);
- if (zone.containsPoint(event->point, Qt::OddEvenFill)) {
- m_highlightItem = HighlightItem::InlineSizeEndHandle;
- cursor = lineToCursor(info->baselineLine(), canvas());
- } else if (startZone.containsPoint(event->point, Qt::OddEvenFill)){
- m_highlightItem = HighlightItem::InlineSizeStartHandle;
- cursor = lineToCursor(info->baselineLine(), canvas());
+ SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));
+ m_textCursor.setTypeSettingHandleHovered(handle);
+ if (handle != SvgTextCursor::NoHandle) {
+ cursor = Qt::ArrowCursor;
+ m_highlightItem = HighlightItem::TypeSettingHandle;
+ }
+
+ if (m_highlightItem == HighlightItem::None) {
+ if (std::optional<InlineSizeInfo> info = InlineSizeInfo::fromShape(selectedShape)) {
+ const QPolygonF zone = info->endLineGrabRect(sensitivity);
+ const QPolygonF startZone = info->startLineGrabRect(sensitivity);
+ if (zone.containsPoint(event->point, Qt::OddEvenFill)) {
+ m_highlightItem = HighlightItem::InlineSizeEndHandle;
+ cursor = lineToCursor(info->baselineLine(), canvas());
+ } else if (startZone.containsPoint(event->point, Qt::OddEvenFill)){
+ m_highlightItem = HighlightItem::InlineSizeStartHandle;
+ cursor = lineToCursor(info->baselineLine(), canvas());
+ }
}
}
@@ -900,13 +911,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
const KoSvgTextShape *hoveredShape = dynamic_cast<KoSvgTextShape *>(canvas()->shapeManager()->shapeAt(event->point));
QPainterPath hoverPath = KisToolUtils::shapeHoverInfoCrossLayer(canvas(), event->point, shapeType, &isHorizontal);
if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {
- SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));
- m_textCursor.setTypeSettingHandleHovered(handle);
- if (handle != SvgTextCursor::NoHandle) {
- cursor = Qt::ArrowCursor;
- } else {
- cursor = selectedShape->writingMode() == KoSvgText::HorizontalTB? m_ibeam_horizontal: m_ibeam_vertical;
- }
+ cursor = selectedShape->writingMode() == KoSvgText::HorizontalTB? m_ibeam_horizontal: m_ibeam_vertical;
} else if (hoveredShape) {
if (!hoveredShape->shapesInside().isEmpty()) {
QPainterPath paths;
diff --git a/plugins/tools/svgtexttool/SvgTextTool.h b/plugins/tools/svgtexttool/SvgTextTool.h
index c6da412b60..8fdbec6471 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.h
+++ b/plugins/tools/svgtexttool/SvgTextTool.h
@@ -195,6 +195,7 @@ private:
InlineSizeStartHandle,
InlineSizeEndHandle,
MoveBorder,
+ TypeSettingHandle,
};
QScopedPointer<SvgTextToolOptionsManager>m_optionManager;
diff --git a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp
index fb4da19a2e..ea391fe9c0 100644
--- a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp
@@ -22,36 +22,13 @@ SvgTextTypeSettingStrategy::SvgTextTypeSettingStrategy(KoToolBase *tool, KoSvgTe
: KoInteractionStrategy(tool)
, m_shape(textShape)
, m_dragStart(regionOfInterest.center())
+ , m_textData(textShape->getMemento())
{
m_cursorPos = textCursor->getPos();
m_cursorAnchor = textCursor->getAnchor();
m_editingType = textCursor->typeSettingHandleAtPos(regionOfInterest);
-}
-
-void SvgTextTypeSettingStrategy::paint(QPainter &painter, const KoViewConverter &converter)
-{
- /*
- if (!m_shape) return;
- const int lineStart = qMax(m_shape->lineStart(m_cursorAnchor), qMin(m_cursorPos, m_cursorAnchor));
- QList<QPair<QPointF, qreal>> firstTf = m_shape->getPositionsAndRotationsForRange(lineStart, qMax(m_cursorPos, m_cursorAnchor), false);
- if (firstTf.size() < 2) return;
- QLineF originalLine(firstTf.first().first, firstTf.last().first);
- QPointF delta = m_dragCurrent - m_dragStart;
- QLineF newLine(originalLine.p1(), originalLine.p2() + delta);
- painter.save();
- painter.setPen(Qt::blue);
- painter.setTransform(converter.documentToView(), true);
- painter.drawPoint(m_dragStart);
- painter.drawPoint(m_dragCurrent);
- painter.setTransform(m_shape->absoluteTransformation(), true);
- painter.setPen(Qt::red);
- painter.setOpacity(0.3);
- painter.drawPoint(originalLine.p1());
- painter.drawLine(originalLine);
- painter.drawLine(newLine);
- painter.restore();
- */
+ m_referenceCursorPos = textCursor->posForTypeSettingHandleAndRect(SvgTextCursor::TypeSettingModeHandle(m_editingType), regionOfInterest);
}
void SvgTextTypeSettingStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
@@ -101,7 +78,7 @@ KUndo2Command *SvgTextTypeSettingStrategy::createCommand()
} else {
const QPointF dragStart = m_shape->documentToShape(m_dragStart);
const QPointF dragCurrent = m_shape->documentToShape(m_dragCurrent);
- const int closestPos = m_shape->posForPointLineSensitive(dragStart);
+ const int closestPos = m_referenceCursorPos;
const QList<KoSvgTextCharacterInfo> infos = m_shape->getPositionsAndRotationsForRange(closestPos, closestPos);
if (infos.empty()) return cmd;
@@ -109,23 +86,23 @@ KUndo2Command *SvgTextTypeSettingStrategy::createCommand()
const KoSvgTextCharacterInfo info = infos.first();
const QTransform tf = QTransform::fromTranslate(info.finalPos.x(), info.finalPos.y()) * QTransform().rotate(info.rotateDeg);
const QLineF line = tf.map(QLineF(QPointF(), info.advance));
- const qreal distOld = kisDistanceToLine(dragStart, line);
const qreal distNew = kisDistanceToLine(dragCurrent, line);
- const qreal scale = distNew/distOld;
KoSvgTextProperties props;
KoSvgTextProperties oldProps = m_shape->propertiesForPos(closestPos, true);
if (m_editingType == int(SvgTextCursor::Ascender) || m_editingType == int(SvgTextCursor::Descender)) {
+ const qreal distOld = kisDistanceToLine(dragStart, line);
+ const qreal scale = distNew/distOld;
KoSvgText::CssLengthPercentage length = oldProps.fontSize();
length.value *= scale;
props.setFontSize(length);
} else if (m_editingType == int(SvgTextCursor::BaselineShift)) {
KoSvgText::CssLengthPercentage length;
- QLineF normal = line.normalVector();
- qreal dot = QVector2D::dotProduct(QVector2D(normal.p2() - normal.p1()), QVector2D(dragCurrent));
- length.value = dot > 0? distNew: -distNew;
+ const QLineF normal = line.normalVector();
+ qreal dot = QVector2D::dotProduct(QVector2D(normal.p2() - normal.p1()), QVector2D(dragCurrent-line.p1()));
+ length.value = dot < 0? -distNew: distNew;
props.setProperty(KoSvgTextProperties::BaselineShiftValueId, QVariant::fromValue(length));
props.setProperty(KoSvgTextProperties::BaselineShiftModeId, QVariant::fromValue(KoSvgText::ShiftLengthPercentage));
} else if (m_editingType == int(SvgTextCursor::LineHeightTop) || m_editingType == int(SvgTextCursor::LineHeightBottom)) {
@@ -153,13 +130,20 @@ void SvgTextTypeSettingStrategy::cancelInteraction()
if (m_previousCmd) {
m_previousCmd->undo();
}
+ QRectF updateRect = m_shape->boundingRect();
+ m_shape->setMemento(m_textData, m_cursorPos, m_cursorAnchor);
+ m_shape->updateAbsolute(updateRect| m_shape->boundingRect());
tool()->repaintDecorations();
}
void SvgTextTypeSettingStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
{
+ Q_UNUSED(modifiers)
if (m_previousCmd) {
m_previousCmd->undo();
}
+ QRectF updateRect = m_shape->boundingRect();
+ m_shape->setMemento(m_textData, m_cursorPos, m_cursorAnchor);
+ m_shape->updateAbsolute(updateRect| m_shape->boundingRect());
tool()->repaintDecorations();
}
diff --git a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.h b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.h
index ef132234d5..02ec57535f 100644
--- a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.h
+++ b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.h
@@ -27,7 +27,7 @@ public:
// KoInteractionStrategy interface
public:
- void paint(QPainter &painter, const KoViewConverter &converter) override;
+ // void paint(QPainter &painter, const KoViewConverter &converter) override;
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override;
KUndo2Command *createCommand() override;
void cancelInteraction() override;
@@ -42,8 +42,10 @@ private:
int m_cursorPos;
int m_cursorAnchor;
int m_editingType;
+ int m_referenceCursorPos;
QScopedPointer<KUndo2Command> m_previousCmd;
+ KoSvgTextShapeMementoSP m_textData;
};
#endif // SVGTEXTTYPESETTINGSTRATEGY_H