From ec1a05800e9793d6efed8dca972e8943ff7a131a Mon Sep 17 00:00:00 2001 From: Onetchou <83351185+Onetchou@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:25:43 +0100 Subject: [PATCH] fix: bounding boxes of exported svg pattern pieces are now correct --- src/libs/vformat/svg_generator.cpp | 68 ++++++++++++++++++++++++++++-- src/libs/vformat/svg_generator.h | 4 ++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/libs/vformat/svg_generator.cpp b/src/libs/vformat/svg_generator.cpp index cd5efe4cf4f9..35da62719277 100644 --- a/src/libs/vformat/svg_generator.cpp +++ b/src/libs/vformat/svg_generator.cpp @@ -63,7 +63,7 @@ QDomDocument SvgGenerator::mergeSvgDoms() return QDomDocument(); } - QDomDocument mergedSvg = m_domList.at(0); + QDomDocument mergedSvg = m_domList.at(0).cloneNode().toDocument(); QDomElement mergedSvgRoot = mergedSvg.documentElement(); if (mergedSvgRoot.tagName() != "svg") { @@ -71,7 +71,14 @@ QDomDocument SvgGenerator::mergeSvgDoms() return QDomDocument(); } - for (int i = 1; i < m_domList.size(); ++i) { + QDomNodeList mergedSvgGroups = mergedSvgRoot.elementsByTagName("g"); + if (mergedSvgGroups.isEmpty()) { + qDebug() << "Error : the SVG does not contain a tag."; + return QDomDocument(); + } + mergedSvgRoot.removeChild(mergedSvgGroups.at(0)); + + for (int i = 0; i < m_domList.size(); ++i) { QDomDocument domSvg = m_domList.at(i); QDomElement svgRoot = domSvg.documentElement(); if (svgRoot.tagName() != "svg") { @@ -84,12 +91,67 @@ QDomDocument SvgGenerator::mergeSvgDoms() return QDomDocument(); } QDomElement mainGroup = svgGroups.at(0).toElement(); + cleanSvg(mainGroup); mergedSvgRoot.appendChild(mainGroup); } return mergedSvg; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief Remove empty groups from the SVG + * @return void + * @details This function removes empty unuseful groups from the exported SVG. + * Those empty groups were generated by Qt svg generator. + */ +void SvgGenerator::removeEmptyGroups(QDomElement &mainGroup) +{ + QDomNodeList groups = mainGroup.elementsByTagName("g"); + for (int i = 0; i < groups.size(); ++i) { + QDomElement group = groups.at(i).toElement(); + if (group.childNodes().isEmpty()) { + if (mainGroup.removeChild(group).isNull()) { + qDebug() << "Error : could not remove empty group"; + } + } + } +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief Remove the M0,0 origin path from the SVG + * @return void + * @details This function removes tht M0,0 origin path from the SVG. + * If this path is not removed, the bounding box of the exported pattern piece + * when opened in a svg editing software can be wrong. + */ +void SvgGenerator::removeEmptyOriginPath(QDomElement &mainGroup) +{ + QDomNodeList paths = mainGroup.elementsByTagName("path"); + for (int i = 0; i < paths.size(); ++i) { + QDomElement path = paths.at(i).toElement(); + if (path.attribute("d") == "M0,0") { + QDomElement parentGroup = path.parentNode().toElement(); + parentGroup.removeChild(path); + mainGroup.removeChild(parentGroup); + break; + } + } +} + + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief Clean the SVG + * @return void + * @details This function cleans the SVG by removing empty groups and the origin M0,0 path + */ +void SvgGenerator::cleanSvg(QDomElement &mainGroup) +{ + removeEmptyGroups(mainGroup); + removeEmptyOriginPath(mainGroup); +} //--------------------------------------------------------------------------------------------------------------------- /** @@ -101,8 +163,6 @@ QDomDocument SvgGenerator::mergeSvgDoms() */ void SvgGenerator::addSvgFromScene(QGraphicsScene *scene) { - - QByteArray byteArray; QBuffer buffer(&byteArray); buffer.open(QIODevice::WriteOnly); diff --git a/src/libs/vformat/svg_generator.h b/src/libs/vformat/svg_generator.h index dffb446bb86b..32ed05e8a8ac 100644 --- a/src/libs/vformat/svg_generator.h +++ b/src/libs/vformat/svg_generator.h @@ -45,6 +45,10 @@ class SvgGenerator private: QDomDocument mergeSvgDoms(); + void removeEmptyGroups(QDomElement &mainGroup); + void removeEmptyOriginPath(QDomElement &mainGroup); + void cleanSvg(QDomElement &mainGroup); + QGraphicsRectItem *m_paper; QString m_filepath; QString m_description;