Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG Import Changes/Fixes #256

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions src/core/svgimporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "svgimporter.h"

#include <QtXml/QDomDocument>
#include <QRegularExpression>

#include "Boxes/containerbox.h"
#include "colorhelpers.h"
Expand Down Expand Up @@ -220,33 +221,42 @@ void extractSvgAttributes(const QString &string,
}
}

bool isColorRGB(const QString &colorStr)
{
static QRegularExpression rx(RGXS "rgb\\(.*\\)" RGXS,
QRegularExpression::CaseInsensitiveOption);
return rx.match(colorStr).hasMatch();
}

bool toColor(const QString &colorStr, QColor &color) {
QRegExp rx = QRegExp(RGXS "rgb\\(.*\\)" RGXS, Qt::CaseInsensitive);
if(rx.exactMatch(colorStr)) {
rx = QRegExp(RGXS "rgb\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)" RGXS, Qt::CaseInsensitive);
if(rx.exactMatch(colorStr)) {
rx.indexIn(colorStr);
QStringList intRGB = rx.capturedTexts();
color.setRgb(intRGB.at(1).toInt(),
intRGB.at(2).toInt(),
intRGB.at(3).toInt());
} else {
rx = QRegExp(RGXS "rgb\\(\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*\\)" RGXS, Qt::CaseInsensitive);
rx.indexIn(colorStr);
QStringList intRGB = rx.capturedTexts();
color.setRgbF(intRGB.at(1).toInt()/100.,
intRGB.at(2).toInt()/100.,
intRGB.at(3).toInt()/100.);
bool isColorRGBA(const QString &colorStr)
{
static QRegularExpression rx(RGXS "rgba\\(.*\\)" RGXS,
QRegularExpression::CaseInsensitiveOption);
return rx.match(colorStr).hasMatch();
}

}
bool toColor(const QString &colorStr, QColor &color)
{
const bool isRGB = isColorRGB(colorStr);
const bool isRGBA = isColorRGBA(colorStr);
if (isRGB || isRGBA) {
QString str = colorStr.toLower().simplified();
str.remove(isRGB ? "rgb" : "rgba").remove(";").remove("(").remove(")");
QStringList components = str.split(",", Qt::SkipEmptyParts);
if (components.size() >= 3) {
qreal r = components[0].contains("%") ? components[0].remove("%").simplified().toFloat() / 100. :
components[0].simplified().toFloat();
qreal g = components[1].contains("%") ? components[1].remove("%").simplified().toFloat() / 100. :
components[1].simplified().toFloat();
qreal b = components[2].contains("%") ? components[2].remove("%").simplified().toFloat() / 100. :
components[2].simplified().toFloat();
qreal a = components.size() == 4 ? components[3].contains("%") ? components[3].remove("%").simplified().toFloat() / 100. :
components[3].simplified().toFloat() : 1.0;
color.setRgbF(r, g, b, a);
} else { return false; }
} else {
rx = QRegExp(RGXS "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" RGXS, Qt::CaseInsensitive);
if(rx.exactMatch(colorStr)) {
color = QColor(colorStr);
} else {
return false;
}
if (QColor::isValidColor(colorStr.simplified())) { color = QColor(colorStr.simplified()); }
else { return false; }
}
return true;
}
Expand Down Expand Up @@ -1146,6 +1156,18 @@ void BoxSvgAttributes::loadBoundingBoxAttributes(const QDomElement &element) {
const QString strokeOp = element.attribute("stroke-opacity");
if(!strokeOp.isEmpty()) mFillAttributes.setColorOpacity(toDouble(strokeOp));

const QString strokeWidth = element.attribute("stroke-width").simplified();
if (mStrokeAttributes.getPaintType() != NOPAINT) {
if (strokeWidth.isEmpty() || strokeWidth.contains("%")) {
if (mStrokeAttributes.getLineWidth() < 1.0) {
mStrokeAttributes.setLineWidth(1.0); // spec says 1 as default
}
// TODO: A percentage value is always computed as a percentage of the normalized viewBox diagonal length.
} else {
mStrokeAttributes.setLineWidth(stripPx(strokeWidth).simplified().toDouble());
}
}

const QString matrixStr = element.attribute("transform");
// const QString transCenterX = element.attribute("inkscape:transform-center-x");
// const QString transCenterY = element.attribute("inkscape:transform-center-y");
Expand Down
Loading