Skip to content

Commit

Permalink
fix: bounding boxes of exported svg pattern pieces are now correct
Browse files Browse the repository at this point in the history
  • Loading branch information
Onetchou authored and DSCaskey committed Nov 24, 2024
1 parent 041ef3b commit ec1a058
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/libs/vformat/svg_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ 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") {
qDebug() << "Error : the first SVG does not contain a <svg> tag.";
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 <g> 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") {
Expand All @@ -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);
}

//---------------------------------------------------------------------------------------------------------------------
/**
Expand All @@ -101,8 +163,6 @@ QDomDocument SvgGenerator::mergeSvgDoms()
*/
void SvgGenerator::addSvgFromScene(QGraphicsScene *scene)
{


QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
Expand Down
4 changes: 4 additions & 0 deletions src/libs/vformat/svg_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ec1a058

Please sign in to comment.