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++
Allow storing the resolution in style preset to make them pixel-relative.
Wolthera van Hövell tot Westerflier9da967dd36d1
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cppindex 458b96b2e7..f5b67a80ff 100644--- a/libs/flake/resources/KoCssStylePreset.cpp+++ b/libs/flake/resources/KoCssStylePreset.cpp@@ -24,6 +24,7 @@ const QString DESC = "desc";const QString SAMPLE_SVG = "sample_svg";const QString SAMPLE_ALIGN = "sample_align";const QString STYLE_TYPE = "style_type";+const QString STORED_PPI = "stored_ppi";const QString STYLE_TYPE_PARAGRAPH = "paragraph";const QString STYLE_TYPE_CHARACTER = "character";@@ -66,12 +67,23 @@ KoCssStylePreset::~KoCssStylePreset()}-KoSvgTextProperties KoCssStylePreset::properties() const+KoSvgTextProperties KoCssStylePreset::properties(int ppi, bool removeKraProps) const{const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);- qDebug() << "searching properties" << treeIndex;+if (treeIndex.isEmpty()) return KoSvgTextProperties();- return d->shape->propertiesForTreeIndex(treeIndex);+ KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);+ const int storedPPI = storedPPIResolution();+ if (storedPPI > 0 && ppi > 0) {+ const double scale = double(storedPPI)/double(ppi);+ props.scaleAbsoluteValues(scale, scale);+ }+ if (removeKraProps) {+ props.removeProperty(KoSvgTextProperties::KraTextStyleType);+ props.removeProperty(KoSvgTextProperties::KraTextStyleResolution);+ props.removeProperty(KoSvgTextProperties::KraTextVersionId);+ }+ return props;}void KoCssStylePreset::setProperties(const KoSvgTextProperties &properties)@@ -151,6 +163,11 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope}// This one is added after removing, because otherwise, the type is removed...modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleType, type);+ if (storedPPIResolution() > 0) {+ modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleResolution, storedPPIResolution());+ } else {+ modifiedProps.removeProperty(KoSvgTextProperties::KraTextStyleResolution);+ }// 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.@@ -332,6 +349,17 @@ QString KoCssStylePreset::sampleSvg() constreturn m[SAMPLE_SVG].toString();}+int KoCssStylePreset::storedPPIResolution() const+{+ QMap<QString, QVariant> m = metadata();+ return m.value(STORED_PPI, 0).toInt();+}++void KoCssStylePreset::setStoredPPIResolution(const int ppi)+{+ addMetaData(STORED_PPI, ppi);+}+KoResourceSP KoCssStylePreset::clone() const{return KoResourceSP(new KoCssStylePreset(*this));@@ -377,6 +405,9 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP reif (!treeIndex.isEmpty()) {KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);styleType = props.property(KoSvgTextProperties::KraTextStyleType).toString();+ if (props.hasProperty(KoSvgTextProperties::KraTextStyleResolution)) {+ addMetaData(STORED_PPI, props.property(KoSvgTextProperties::KraTextStyleResolution));+ }}addMetaData(STYLE_TYPE, styleType);diff --git a/libs/flake/resources/KoCssStylePreset.h b/libs/flake/resources/KoCssStylePreset.hindex b2571371f3..d46f277b5f 100644--- a/libs/flake/resources/KoCssStylePreset.h+++ b/libs/flake/resources/KoCssStylePreset.h@@ -29,7 +29,7 @@ public:~KoCssStylePreset();/// The actual text properties.- KoSvgTextProperties properties() const;+ KoSvgTextProperties properties(int ppi = 72, bool removeKraProps = false) const;void setProperties(const KoSvgTextProperties &properties);/// The description associated with this style.@@ -52,6 +52,16 @@ public:/// Returns the sample svg metadata. Use updateThumbnail to update it.QString sampleSvg() const;+ /**+ * The resolution that this style is tied to.+ * if this is above 0, then the properties absolute values are scaled by+ * to fit the document resolution.+ * This allows for pixel-relative styles to be created.+ */+ int storedPPIResolution() const;++ void setStoredPPIResolution(const int ppi);+/*** @brief setSampleText* This allows setting the visible sample text. In effect, this reconstructs the internal textdiff --git a/libs/flake/text/KoSvgTextContentElement.cpp b/libs/flake/text/KoSvgTextContentElement.cppindex 9c7cc769a6..2d610d4481 100644--- a/libs/flake/text/KoSvgTextContentElement.cpp+++ b/libs/flake/text/KoSvgTextContentElement.cpp@@ -158,6 +158,7 @@ KoSvgTextProperties adjustPropertiesForFontSizeWorkaround(const KoSvgTextPropert}const QString TEXT_STYLE_TYPE = "krita:style-type";+const QString TEXT_STYLE_RES = "krita:style-resolution";bool KoSvgTextContentElement::loadSvg(const QDomElement &e, SvgLoadingContext &context, bool rootNode){@@ -233,6 +234,13 @@ bool KoSvgTextContentElement::loadSvg(const QDomElement &e, SvgLoadingContext &cif (e.hasAttribute(TEXT_STYLE_TYPE.toLatin1().data())) {properties.setProperty(KoSvgTextProperties::KraTextStyleType, e.attribute(TEXT_STYLE_TYPE.toLatin1().data()));+ if (e.hasAttribute(TEXT_STYLE_RES.toLatin1().data())) {+ QString resolution = e.attribute(TEXT_STYLE_RES.toLatin1().data()).toLower();+ if (resolution.endsWith("dpi")) {+ resolution.chop(3);+ }+ properties.setProperty(KoSvgTextProperties::KraTextStyleResolution, KisDomUtils::toInt(resolution));+ }}return true;@@ -359,6 +367,9 @@ bool KoSvgTextContentElement::saveSvg(SvgSavingContext &context,if (properties.hasProperty(KoSvgTextProperties::KraTextStyleType)) {context.shapeWriter().addAttribute(TEXT_STYLE_TYPE.toLatin1().data(), properties.property(KoSvgTextProperties::KraTextStyleType).toString());+ if (properties.hasProperty(KoSvgTextProperties::KraTextStyleResolution)) {+ context.shapeWriter().addAttribute(TEXT_STYLE_RES.toLatin1().data(), QString::number(properties.property(KoSvgTextProperties::KraTextStyleResolution).toInt())+"dpi");+ }}if (saveText) {diff --git a/libs/flake/text/KoSvgTextProperties.cpp b/libs/flake/text/KoSvgTextProperties.cppindex 1014bb1dca..f4cbc59712 100644--- a/libs/flake/text/KoSvgTextProperties.cpp+++ b/libs/flake/text/KoSvgTextProperties.cpp@@ -203,6 +203,46 @@ void KoSvgTextProperties::setAllButNonInheritableProperties(const KoSvgTextPrope}}+void KoSvgTextProperties::scaleAbsoluteValues(const double scaleInline, const double scaleBlock)+{+ const KoSvgText::CssLengthPercentage::UnitType absoluteUnit = KoSvgText::CssLengthPercentage::Absolute;++ for (auto it = this->m_d->properties.begin(); it != this->m_d->properties.end(); ++it) {+ if (it.value().canConvert<KoSvgText::CssLengthPercentage>()) {+ KoSvgText::CssLengthPercentage length = it.value().value<KoSvgText::CssLengthPercentage>();+ if (length.unit != absoluteUnit) continue;+ if (it.key() == FontSizeId || it.key() == BaselineShiftValueId) {+ length.value *= scaleBlock;+ } else {+ length.value *= scaleInline;+ }+ it.value() = QVariant::fromValue(length);+ } else if (it.value().canConvert<KoSvgText::LineHeightInfo>()) {+ KoSvgText::LineHeightInfo info = it.value().value<KoSvgText::LineHeightInfo>();+ if (info.length.unit != absoluteUnit) continue;+ info.length.value *= scaleBlock;+ it.value() = QVariant::fromValue(info);+ } else if (it.value().canConvert<KoSvgText::TabSizeInfo>()) {+ KoSvgText::TabSizeInfo info = it.value().value<KoSvgText::TabSizeInfo>();+ if (info.length.unit != absoluteUnit) continue;+ info.length.value *= scaleInline;+ it.value() = QVariant::fromValue(info);+ } else if (it.value().canConvert<KoSvgText::TextIndentInfo>()) {+ KoSvgText::TextIndentInfo info = it.value().value<KoSvgText::TextIndentInfo>();+ if (info.length.unit != absoluteUnit) continue;+ info.length.value *= scaleInline;+ it.value() = QVariant::fromValue(info);+ } else if (it.value().canConvert<KoSvgText::AutoValue>()) {+ KoSvgText::AutoValue info = it.value().value<KoSvgText::AutoValue>();+ if (info.isAuto) continue;+ if (it.key() != InlineSizeId) continue;+ info.customValue *= scaleInline;+ it.value() = QVariant::fromValue(info);+ }+ // TODO: Check shape padding and margin when they become editable.+ }+}+KoSvgTextProperties KoSvgTextProperties::ownProperties(const KoSvgTextProperties &parentProperties, bool keepFontSize) const{KoSvgTextProperties result;diff --git a/libs/flake/text/KoSvgTextProperties.h b/libs/flake/text/KoSvgTextProperties.hindex 13b964f367..6a1c4fdccf 100644--- a/libs/flake/text/KoSvgTextProperties.h+++ b/libs/flake/text/KoSvgTextProperties.h@@ -118,6 +118,7 @@ public:KraTextVersionId, ///< Int, used for handling incorrectly saved files.KraTextStyleType, ///< string, used to identify the style preset type (character or paragraph).+ KraTextStyleResolution, ///< Int, used to scale style presets to be pixel-relative.};KoSvgTextProperties();@@ -201,6 +202,18 @@ public:/// Used to merge child properties into parent propertiesvoid setAllButNonInheritableProperties(const KoSvgTextProperties &properties);+ /**+ * @brief scaleAbsoluteValues+ * This scales all absolute values stored in these text properties. Relative+ * values don't need to be scaled. This can be used to scale styles in setSize+ * as well as scale the text properties for Style Presets.+ * @param scaleInline -- affects inline-direction values, like letter-spacing,+ * inline-size, tab-size, text-indent, etc.+ * @param scaleBlock -- affects block-direction values, like font-size,+ * line-height, and baseline-shift.+ */+ void scaleAbsoluteValues(const double scaleInline = 1.0, const double scaleBlock = 1.0);+/*** Return a set of properties that ar **not** inherited from \p* parentProperties. The property is considered "inherited" **iff* it isdiff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp b/plugins/dockers/textproperties/CssStylePresetEditDialog.cppindex a4c17d80c7..6c545e8e5c 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp@@ -92,6 +92,8 @@ void CssStylePresetEditDialog::setCurrentResource(KoCssStylePresetSP resource)m_quickWidget->rootObject()->setProperty("beforeSample", m_currentResource->beforeText());m_quickWidget->rootObject()->setProperty("sampleText", m_currentResource->sampleText());m_quickWidget->rootObject()->setProperty("afterSample", m_currentResource->afterText());+ const int storedPPI = m_currentResource->storedPPIResolution();+ m_quickWidget->rootObject()->setProperty("makePixelRelative", storedPPI > 0);QMetaObject::invokeMethod(m_quickWidget->rootObject(), "setProperties");m_blockUpdates = false;@@ -166,3 +168,23 @@ void CssStylePresetEditDialog::slotUpdateDirty() {}this->button(KoDialog::Ok)->setEnabled(isDirty && !title.isEmpty());}++void CssStylePresetEditDialog::slotUpdateStoreDPI() {+ //Whenever we make it pixel relative, we'll set the resolution to 72 dpi.+ if (m_blockUpdates) return;+ const bool makePixelRelative = m_quickWidget->rootObject()->property("makePixelRelative").toBool();+ const int storedPPI = m_currentResource->storedPPIResolution();+ const int canvasPPI = m_quickWidget->rootObject()->property("canvasDPI").toInt();+ KoSvgTextPropertyData textData = m_model->textData.get();+ if (storedPPI > 0 && !makePixelRelative) {+ const double scale = double(storedPPI)/double(canvasPPI);+ textData.commonProperties.scaleAbsoluteValues(scale, scale);+ m_currentResource->setStoredPPIResolution(0);+ } else if(storedPPI == 0 && makePixelRelative) {+ const double scale = double(canvasPPI)/double(72);+ textData.commonProperties.scaleAbsoluteValues(scale, scale);+ m_currentResource->setStoredPPIResolution(72);+ }+ m_model->textData.set(textData);+ slotUpdateTextProperties();+}diff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.h b/plugins/dockers/textproperties/CssStylePresetEditDialog.hindex 39afbc9e60..31d615ab5b 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.h+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.h@@ -29,6 +29,7 @@ public Q_SLOTS:QColor modalColorDialog(QColor oldColor);QString wwsFontFamilyName(QString familyName);void slotUpdateDirty();+ void slotUpdateStoreDPI();private:QQuickWidget *m_quickWidget {0};diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex fdc56d4ad8..21c51c5a39 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -92,7 +92,6 @@ struct TextPropertiesDock::Private}KoSvgTextPropertiesModel *textModel {new KoSvgTextPropertiesModel()};- KisResourceModel *fontModel{nullptr};KisCanvasResourceProvider *provider{nullptr};TextPropertyConfigModel *textPropertyConfigModel {nullptr};qreal currentDpi{72.0};@@ -266,11 +265,10 @@ void TextPropertiesDock::applyPreset(KoResourceSP resource)KoCssStylePresetSP preset = resource.staticCast<KoCssStylePreset>();if (!preset) return;KoSvgTextPropertyData textData = d->textModel->textData.get();- KoSvgTextProperties properties = preset->properties();+ KoSvgTextProperties properties = preset->properties(d->currentDpi, true);Q_FOREACH(KoSvgTextProperties::PropertyId p, properties.properties()) {textData.commonProperties.setProperty(p, properties.property(p));}- qDebug() << "applied preset" << resource->name();d->textModel->textData.set(textData);}@@ -302,6 +300,9 @@ void TextPropertiesDock::editPreset(KoResourceSP resource)if (dialog->exec() == QDialog::Accepted) {KisResourceUserOperations::updateResourceWithUserInput(this, dialog->currentResource());+ } else {+ KisResourceModel model = KisResourceModel(ResourceType::CssStyles, this);+ model.reloadResource(preset);}}diff --git a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qmlindex 2e741e9075..246a89d6b7 100644--- a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml+++ b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml@@ -18,6 +18,9 @@ Control {property alias presetSampleAlignment: presetSampleLabel.alignment;property string styleType: "character"; // Defaults to character because the first index in the style combobox is character.property double canvasDPI: 72.0;+ property double pixelRelativeDPI: 72.0;+ property alias makePixelRelative: pixelRelativeCheck.checked;+ onMakePixelRelativeChanged: mainWindow.slotUpdateStoreDPI();property alias beforeSample: beforeSampleField.text;property alias sampleText: sampleField.text;@@ -69,19 +72,19 @@ Control {Label {Layout.minimumWidth: implicitWidth;- text: i18nc("label:title", "Title:");+ text: i18nc("@label:textfield", "Title:");Layout.alignment: Qt.AlignRight | Qt.AlignVCenter;horizontalAlignment: Text.AlignRight;}TextField {id: presetTitleField;Layout.fillWidth: true;- placeholderText: i18nc("info:placeholder", "Style Name");+ placeholderText: i18nc("@info:placeholder", "Style Name:");onTextChanged: mainWindow.slotUpdateDirty();}Label {Layout.minimumWidth: implicitWidth;- text: i18nc("label:title", "Description:");+ text: i18nc("@label:textarea", "Description:");Layout.alignment: Qt.AlignRight | Qt.AlignTop;horizontalAlignment: Text.AlignRight;}@@ -94,7 +97,7 @@ Control {id: presetDescriptionArea;wrapMode: TextInput.Wrap;- placeholderText: i18nc("info:placeholder", "Style Description.");+ placeholderText: i18nc("@info:placeholder", "Style Description.");onTextChanged: mainWindow.slotUpdateDirty();}@@ -106,7 +109,7 @@ Control {}Label {Layout.minimumWidth: implicitWidth;- text: i18nc("label:Title", "Style Type:");+ text: i18nc("label:listbox", "Style Type:");Layout.alignment: Qt.AlignRight | Qt.AlignVCenter;horizontalAlignment: Text.AlignRight;}@@ -114,15 +117,28 @@ Control {id: styleTypeCmb;Layout.fillWidth: true;model: [- {text: i18nc("listbox", "Character"), value: TextPropertyConfigModel.Character},- {text: i18nc("listbox", "Paragraph"), value: TextPropertyConfigModel.Paragraph}+ {text: i18nc("@label:inlistbox", "Character"), value: TextPropertyConfigModel.Character},+ {text: i18nc("@label:inlistbox", "Paragraph"), value: TextPropertyConfigModel.Paragraph}];valueRole: "value";textRole: "text";onActivated: styleEdit.styleType = currentValue === TextPropertyConfigModel.Character? "character": "paragraph";}+ Item {+ width: 1;+ height: 1;+ }+ CheckBox {+ id: pixelRelativeCheck;+ text: i18nc("@option:check", "Style is Pixel Relative");+ ToolTip.text: i18nc("info:tooltip", "By default, styles are stored in points, meaning that the sizes in pixels will be different depending on document print resolution. Toggle this to ensure the style will maintain the same pixel size across different documents.");+ ToolTip.visible: hovered;+ ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval;+ hoverEnabled: true;+ }+Label {- text: i18nc("label:Title", "Sample Text:");+ text: i18nc("@label:textfield", "Sample Text:");}RowLayout {Layout.columnSpan: 2;@@ -131,21 +147,21 @@ Control {id: beforeSampleField;Layout.fillWidth: visible? true: false;Layout.preferredWidth: visible? implicitWidth: 0;- placeholderText: i18nc("info:placeholder", "Before...");+ placeholderText: i18nc("@info:placeholder", "Before...");onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();visible: styleType !== "paragraph";}TextField {id: sampleField;Layout.fillWidth: true;- placeholderText: presetTitleField.text.length == 0? i18nc("info:placeholder", "Sample..."): presetTitleField.text;+ placeholderText: presetTitleField.text.length == 0? i18nc("@info:placeholder", "Sample..."): presetTitleField.text;onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();}TextField {id: afterSampleField;Layout.fillWidth: visible? true: false;Layout.preferredWidth: visible? implicitWidth: 0;- placeholderText: i18nc("info:placeholder", "... After");+ placeholderText: i18nc("@info:placeholder", "... After");onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();visible: styleType !== "paragraph";}@@ -154,7 +170,7 @@ Control {TextPropertyBaseList {Layout.fillWidth: true;Layout.fillHeight: true;- canvasDPI: styleEdit.canvasDPI;+ canvasDPI: styleEdit.makePixelRelative? styleEdit.pixelRelativeDPI: styleEdit.canvasDPI;configModel: styleEdit.configModel;id: presetProperties;propertyType: styleType == "character"? TextPropertyConfigModel.Character: TextPropertyConfigModel.Paragraph;diff --git a/plugins/dockers/textproperties/qml/TextProperties.qml b/plugins/dockers/textproperties/qml/TextProperties.qmlindex f46d7043e1..74981a2982 100644--- a/plugins/dockers/textproperties/qml/TextProperties.qml+++ b/plugins/dockers/textproperties/qml/TextProperties.qml@@ -136,7 +136,7 @@ Rectangle {presetView.applyPreset();}- ToolTip.text: presetDelegate.model.name + "\n" + presetDelegate.meta.description;+ ToolTip.text: typeof presetDelegate.meta.description !== 'undefined'? presetDelegate.model.name + "\n" + presetDelegate.meta.description: presetDelegate.model.name;ToolTip.visible: containsMouse;ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval;