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: Allow editing paragraph properties, fine tune cursors a little.
Wolthera van Hövell tot Westerflier7939e8e80c50
diff --git a/libs/flake/text/KoSvgTextProperties.cpp b/libs/flake/text/KoSvgTextProperties.cppindex f4cbc59712..3e7b15a023 100644--- a/libs/flake/text/KoSvgTextProperties.cpp+++ b/libs/flake/text/KoSvgTextProperties.cpp@@ -1088,7 +1088,7 @@ qreal KoSvgTextProperties::xHeight() constreturn fontSizeVal * 0.5;}-KoSvgText::FontMetrics KoSvgTextProperties::metrics(const bool withResolvedLineHeight) const+KoSvgText::FontMetrics KoSvgTextProperties::metrics(const bool withResolvedLineHeight, const bool offsetByBaseline) const{const KoCSSFontInfo info = cssFontInfo();@@ -1096,6 +1096,14 @@ KoSvgText::FontMetrics KoSvgTextProperties::metrics(const bool withResolvedLineHconst KoSvgText::TextRendering textRendering = KoSvgText::TextRendering(propertyOrDefault(KoSvgTextProperties::TextRenderingId).toInt());KoSvgText::FontMetrics metrics = KoFontRegistry::instance()->fontMetricsForCSSValues(info, isHorizontal, textRendering);+ if (offsetByBaseline) {+ KoSvgText::Baseline baseline = KoSvgText::Baseline(propertyOrDefault(DominantBaselineId).toInt());+ if (baseline == KoSvgText::BaselineAuto || baseline == KoSvgText::BaselineNoChange || baseline == KoSvgText::BaselineResetSize || baseline == KoSvgText::BaselineUseScript) {+ baseline = isHorizontal? KoSvgText::BaselineAlphabetic: KoSvgText::BaselineCentral;+ }+ metrics.offsetMetricsToNewOrigin(baseline);+ }+return withResolvedLineHeight? applyLineHeight(metrics): metrics;}diff --git a/libs/flake/text/KoSvgTextProperties.h b/libs/flake/text/KoSvgTextProperties.hindex a7bca46bcf..6bca73bbbc 100644--- a/libs/flake/text/KoSvgTextProperties.h+++ b/libs/flake/text/KoSvgTextProperties.h@@ -256,9 +256,10 @@ public:* @brief metrics* Return the metrics of the first available font.* @param withResolvedLineHeight -- apply the lineheight into the linegap property.+ * @param offsetByBaseline -- ensure the metrics are offset by the dominant baseline.* @return metrics for the current font.*/- KoSvgText::FontMetrics metrics(const bool withResolvedLineHeight = true) const;+ KoSvgText::FontMetrics metrics(const bool withResolvedLineHeight = true, const bool offsetByBaseline = false) const;/*** @brief applyLineHeightdiff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cppindex 2e662b6ce6..00306875b6 100644--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp@@ -354,6 +354,18 @@ void SvgTextCursor::setDrawTypeSettingHandle(bool draw)d->drawTypeSettingHandle = draw;}+QCursor SvgTextCursor::cursorTypeForTypeSetting() const+{+ if (d->hoveredTypeSettingHandle == StartPos ||+ d->hoveredTypeSettingHandle == StartPos ||+ d->typeSettingDecor.testBaselines(d->lastKnownModifiers)) {+ return Qt::ArrowCursor;+ } else if (d->shape) {+ return (d->shape->writingMode() == KoSvgText::HorizontalTB)? Qt::SizeVerCursor: Qt::SizeHorCursor;+ }+ return Qt::ArrowCursor;+}+QString SvgTextCursor::handleName(TypeSettingModeHandle handle) const{bool baseline = d->typeSettingDecor.testBaselines(d->lastKnownModifiers);@@ -432,9 +444,9 @@ QMap<SvgTextCursor::TypeSettingModeHandle, int> typeSettingBaselinesFromMetrics({SvgTextCursor::BaselineAlphabetic, metrics.alphabeticBaseline},{SvgTextCursor::BaselineIdeographic, metrics.ideographicUnderBaseline},{SvgTextCursor::BaselineHanging, metrics.hangingBaseline},+ {SvgTextCursor::BaselineCentral, metrics.ideographicCenterBaseline},{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}@@ -452,10 +464,11 @@ int SvgTextCursor::posForTypeSettingHandleAndRect(const TypeSettingModeHandle haconst 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);+ const int currentPos = (d->pos == d->anchor)? -1 :d->shape->posForIndex(it->logicalIndex);+ const KoSvgTextProperties props = d->shape->propertiesForPos(currentPos, true);KoSvgText::LineHeightInfo lineHeight = props.propertyOrDefault(KoSvgTextProperties::LineHeightId).value<KoSvgText::LineHeightInfo>();- KoSvgText::FontMetrics metrics = it->metrics;+ KoSvgText::FontMetrics metrics = (d->pos == d->anchor)? props.metrics(true, true): it->metrics;const bool isHorizontal = d->shape->writingMode() == KoSvgText::HorizontalTB;const qreal scaleMetrics = props.fontSize().value/qreal(metrics.fontSize);@@ -1255,6 +1268,7 @@ void SvgTextCursor::notifyShapeChanged(KoShape::ChangeType type, KoShape *shape)updateCursor(true);updateSelection();updateInputMethodItemTransform();+ updateTypeSettingDecoration();}void SvgTextCursor::notifyCursorPosChanged(int pos, int anchor)@@ -1263,6 +1277,7 @@ void SvgTextCursor::notifyCursorPosChanged(int pos, int anchor)d->anchor = anchor;updateCursor();updateSelection();+ updateTypeSettingDecoration();}void SvgTextCursor::notifyMarkupChanged()@@ -1549,6 +1564,9 @@ bool SvgTextCursor::registerPropertyAction(QAction *action, const QString &name)d->actions.append(action);connect(action, SIGNAL(triggered(bool)), this, SLOT(pastePlainText()));return true;+ } else if (action) {+ d->actions.append(action);+ return true;}return false;}@@ -1665,15 +1683,20 @@ void SvgTextCursor::updateTypeSettingDecoration()d->typeSettingDecor.handles.first = rtl? last.finalPos: first.finalPos;d->typeSettingDecor.handles.second = rtl? first.finalPos: last.finalPos;- // Slightly cursed...+ // This could be better, it's quite clunky right now.// It'd be better if we could get KoSvgTextNodeIndex for a pos, but that one is only implemented in shape branch...d->typeSettingDecor.paths = QMap<SvgTextCursor::TypeSettingModeHandle, QPainterPath>();d->typeSettingDecor.baselines = QMap<SvgTextCursor::TypeSettingModeHandle, QPainterPath>();- for (auto it = infos.begin(); it != infos.end(); it++) {- const KoSvgTextProperties props = d->shape->propertiesForPos(d->shape->posForIndex(it->logicalIndex), true);+ QList<KoSvgTextCharacterInfo> metricInfos = infos;+ if (d->pos == d->anchor) {+ metricInfos = d->shape->getPositionsAndRotationsForRange(0, d->shape->posForIndex(d->shape->plainText().size()));+ }+ for (auto it = metricInfos.begin(); it != metricInfos.end(); it++) {+ const int currentPos = (d->pos == d->anchor)? -1 :d->shape->posForIndex(it->logicalIndex);+ const KoSvgTextProperties props = d->shape->propertiesForPos(currentPos, true);KoSvgText::LineHeightInfo lineHeight = props.propertyOrDefault(KoSvgTextProperties::LineHeightId).value<KoSvgText::LineHeightInfo>();- KoSvgText::FontMetrics metrics = it->metrics;+ KoSvgText::FontMetrics metrics = (d->pos == d->anchor)? props.metrics(true, true) :it->metrics;const bool isHorizontal = d->shape->writingMode() == KoSvgText::HorizontalTB;const qreal scaleMetrics = props.fontSize().value/qreal(metrics.fontSize);@@ -1703,6 +1726,7 @@ void SvgTextCursor::updateTypeSettingDecoration()d->typeSettingDecor.baselines.remove(LineHeightTop);d->typeSettingDecor.baselines.remove(LineHeightBottom);+updateRect = d->shape->shapeToDocument(d->typeSettingDecor.boundingRect(d->handleRadius));}Q_EMIT updateCursorDecoration(updateRect | d->oldTypeSettingRect);diff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.hindex 98ec0160af..32ed2e456e 100644--- a/plugins/tools/svgtexttool/SvgTextCursor.h+++ b/plugins/tools/svgtexttool/SvgTextCursor.h@@ -145,6 +145,9 @@ public:/// Turned off when the typesetting strategy is active to give artists more control.void setDrawTypeSettingHandle(bool draw);+ /// Return appropriate typeSetting cursor;+ QCursor cursorTypeForTypeSetting() const;+/*** @brief handleName* @return translated name of a given handle.diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex d2ed0ae181..899f27a004 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -128,29 +128,16 @@ SvgTextTool::SvgTextTool(KoCanvasBase *canvas)}m_textTypeSignalsMapper.reset(new KisSignalMapper(this));- const QMap<QString, int> convertActions {- {"text_type_preformatted", KoSvgTextShape::PreformattedText},- {"text_type_inline_wrap", KoSvgTextShape::InlineWrap},- {"text_type_pre_positioned", KoSvgTextShape::PrePositionedText}- };- Q_FOREACH(const QString name, convertActions.keys()) {- QAction *a = action(name);- connect(a, SIGNAL(triggered()), m_textTypeSignalsMapper.data(), SLOT(map()));- m_textTypeSignalsMapper->setMapping(a, convertActions.value(name));- }+ addMappedAction(m_textTypeSignalsMapper.data(), "text_type_preformatted", KoSvgTextShape::PreformattedText);+ addMappedAction(m_textTypeSignalsMapper.data(), "text_type_inline_wrap", KoSvgTextShape::InlineWrap);+ addMappedAction(m_textTypeSignalsMapper.data(), "text_type_pre_positioned", KoSvgTextShape::PrePositionedText);m_typeSettingMovementMapper.reset(new KisSignalMapper(this));- const QMap<QString, int> typeSetActions {- {"svg_type_setting_move_selection_start_down_1_px", Qt::Key_Down},- {"svg_type_setting_move_selection_start_up_1_px", Qt::Key_Up},- {"svg_type_setting_move_selection_start_left_1_px", Qt::Key_Left},- {"svg_type_setting_move_selection_start_right_1_px", Qt::Key_Right}- };- Q_FOREACH(const QString name, typeSetActions.keys()) {- QAction *a = action(name);- connect(a, SIGNAL(triggered()), m_typeSettingMovementMapper.data(), SLOT(map()));- m_typeSettingMovementMapper->setMapping(a, typeSetActions.value(name));- }+ addMappedAction(m_typeSettingMovementMapper.data(), "svg_type_setting_move_selection_start_down_1_px", Qt::Key_Down);+ addMappedAction(m_typeSettingMovementMapper.data(), "svg_type_setting_move_selection_start_up_1_px", Qt::Key_Up);+ addMappedAction(m_typeSettingMovementMapper.data(), "svg_type_setting_move_selection_start_left_1_px", Qt::Key_Left);+ addMappedAction(m_typeSettingMovementMapper.data(), "svg_type_setting_move_selection_start_right_1_px", Qt::Key_Right);+m_base_cursor = QCursor(QPixmap(":/tool_text_basic.xpm"), 7, 7);m_text_inline_horizontal = QCursor(QPixmap(":/tool_text_inline_horizontal.xpm"), 7, 7);@@ -877,7 +864,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));m_textCursor.setTypeSettingHandleHovered(handle);if (handle != SvgTextCursor::NoHandle) {- cursor = Qt::ArrowCursor;+ cursor = m_textCursor.cursorTypeForTypeSetting();m_highlightItem = HighlightItem::TypeSettingHandle;}@@ -912,7 +899,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)QPainterPath hoverPath = KisToolUtils::shapeHoverInfoCrossLayer(canvas(), event->point, shapeType, &isHorizontal);if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {cursor = selectedShape->writingMode() == KoSvgText::HorizontalTB? m_ibeam_horizontal: m_ibeam_vertical;- } else if (hoveredShape) {+ } else if (hoveredShape && selectedShape != hoveredShape) {if (!hoveredShape->shapesInside().isEmpty()) {QPainterPath paths;Q_FOREACH(KoShape *s, hoveredShape->shapesInside()) {@@ -1070,3 +1057,13 @@ KoSvgText::WritingMode SvgTextTool::writingMode() constreturn KoSvgText::WritingMode(props.propertyOrDefault(KoSvgTextProperties::WritingModeId).toInt());}+void SvgTextTool::addMappedAction(KisSignalMapper *mapper, const QString &actionName, const int value)+{+ QAction *a = action(actionName);+ if (a) {+ connect(a, SIGNAL(triggered()), mapper, SLOT(map()));+ mapper->setMapping(a, value);+ m_textCursor.registerPropertyAction(a, actionName);+ }+}+diff --git a/plugins/tools/svgtexttool/SvgTextTool.h b/plugins/tools/svgtexttool/SvgTextTool.hindex 8fdbec6471..02fdea4ed1 100644--- a/plugins/tools/svgtexttool/SvgTextTool.h+++ b/plugins/tools/svgtexttool/SvgTextTool.h@@ -99,6 +99,8 @@ private:KoSvgText::WritingMode writingMode() const;+ void addMappedAction(KisSignalMapper* mapper, const QString &actionName, const int value);+private Q_SLOTS:void showEditor();@@ -180,6 +182,10 @@ private Q_SLOTS:*/void slotMoveTextSelection(int index);+ /**+ * @brief slotUpdateTypeSettingMode+ * Enable typesetting mode from the tool options.+ */void slotUpdateTypeSettingMode();private:diff --git a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cppindex ea391fe9c0..9caee75c83 100644--- a/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp+++ b/plugins/tools/svgtexttool/SvgTextTypeSettingStrategy.cpp@@ -24,7 +24,6 @@ SvgTextTypeSettingStrategy::SvgTextTypeSettingStrategy(KoToolBase *tool, KoSvgTe, m_dragStart(regionOfInterest.center()), m_textData(textShape->getMemento()){-m_cursorPos = textCursor->getPos();m_cursorAnchor = textCursor->getAnchor();m_editingType = textCursor->typeSettingHandleAtPos(regionOfInterest);@@ -119,7 +118,9 @@ KUndo2Command *SvgTextTypeSettingStrategy::createCommand()props.setProperty(KoSvgTextProperties::LineHeightId, QVariant::fromValue(lineHeight));}if (!props.isEmpty()) {- cmd = new SvgTextMergePropertiesRangeCommand(m_shape, props, m_cursorPos, m_cursorAnchor);+ int pos = m_cursorPos == m_cursorAnchor? -1: m_cursorPos;+ int anchor = m_cursorPos == m_cursorAnchor? -1: m_cursorAnchor;+ cmd = new SvgTextMergePropertiesRangeCommand(m_shape, props, pos, anchor);}}return cmd;@@ -127,10 +128,12 @@ KUndo2Command *SvgTextTypeSettingStrategy::createCommand()void SvgTextTypeSettingStrategy::cancelInteraction(){+ tool()->canvas()->snapGuide()->reset();+ QRectF updateRect = m_shape->boundingRect();if (m_previousCmd) {m_previousCmd->undo();}- QRectF updateRect = m_shape->boundingRect();+ updateRect |= m_shape->boundingRect();m_shape->setMemento(m_textData, m_cursorPos, m_cursorAnchor);m_shape->updateAbsolute(updateRect| m_shape->boundingRect());tool()->repaintDecorations();@@ -139,11 +142,5 @@ void SvgTextTypeSettingStrategy::cancelInteraction()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();+ cancelInteraction();}