summaryrefslogtreecommitdiffstats
path: root/src/svg/qsvghandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/svg/qsvghandler.cpp')
-rw-r--r--src/svg/qsvghandler.cpp244
1 files changed, 152 insertions, 92 deletions
diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp
index 345dcf3..5950fac 100644
--- a/src/svg/qsvghandler.cpp
+++ b/src/svg/qsvghandler.cpp
@@ -908,103 +908,155 @@ static void parsePen(QSvgNode *node,
//qDebug()<<"Node "<<node->type()<<", attrs are "<<value<<width;
- if (!value.isEmpty() || !width.isEmpty() || !linecap.isEmpty() || !linejoin.isEmpty()) {
- if (value != QLatin1String("none")) {
- QSvgStrokeStyle *inherited =
- static_cast<QSvgStrokeStyle*>(node->styleProperty(
- QSvgStyleProperty::STROKE));
- if (!inherited)
- inherited = static_cast<QSvgStrokeStyle*>(node->parent()->styleProperty(
- QSvgStyleProperty::STROKE));
- QPen pen(handler->defaultPen());
- if (inherited)
- pen = inherited->qpen();
+ /* All the below checks needed because g (or any container element) can have only one of these attributes.
+ * If it doesn't has any one of these, then processing below not needed */
+
+ if (!value.isEmpty() || !width.isEmpty() || !dashArray.isEmpty() || !linecap.isEmpty() ||
+ !linejoin.isEmpty() || !miterlimit.isEmpty() || !opacity.isEmpty() || !dashOffset.isEmpty() ) {
+ //if (value != QLatin1String("none")) {
+ /* If stroke = "none" then also, we need to parse below, because elements like 'g' may
+ have other defined stroke attributes, which will be inherited by its children */
+ QSvgStrokeStyle *inherited =
+ static_cast<QSvgStrokeStyle*>(node->parent()->styleProperty(
+ QSvgStyleProperty::STROKE));
+
+ QPen pen(handler->defaultPen());
+ bool stroke = false;
+ if (inherited) {
+ pen = inherited->qpen();
+ stroke = inherited->strokePresent();
+ }
- if (!value.isEmpty()) {
- if (value.startsWith(QLatin1String("url"))) {
- value = value.remove(0, 3);
- QSvgStyleProperty *style = styleFromUrl(node, value);
- if (style) {
- if (style->type() == QSvgStyleProperty::GRADIENT) {
- QBrush b(*((QSvgGradientStyle*)style)->qgradient());
- pen.setBrush(b);
- } else if (style->type() == QSvgStyleProperty::SOLID_COLOR) {
- pen.setColor(
- ((QSvgSolidColorStyle*)style)->qcolor());
- }
- } else {
- qWarning() << "QSvgHandler::parsePen could not resolve property" << idFromUrl(value);
+ // stroke-opacity attribute handling
+ qreal strokeAlpha;
+ if (!opacity.isEmpty() && opacity != QLatin1String("inherit")) {
+ strokeAlpha = qMin(qreal(1.0), qMax(qreal(0.0), toDouble(opacity)));
+ } else {
+ strokeAlpha = pen.color().alphaF();
+ }
+
+ //stroke attribute handling
+ if (!value.isEmpty() && value != QLatin1String("inherit")) {
+ if (value.startsWith(QLatin1String("url"))) {
+ value = value.remove(0, 3);
+ QSvgStyleProperty *style = styleFromUrl(node, value);
+ if (style) {
+ if (style->type() == QSvgStyleProperty::GRADIENT) {
+ QBrush b(*((QSvgGradientStyle*)style)->qgradient());
+ pen.setBrush(b);
+ } else if (style->type() == QSvgStyleProperty::SOLID_COLOR) {
+ pen.setColor(
+ ((QSvgSolidColorStyle*)style)->qcolor());
}
+ pen.setStyle(Qt::SolidLine);
+ stroke = true;
} else {
- QColor color;
- if (constructColor(value, opacity, color, handler))
- pen.setColor(color);
+ qWarning() << "QSvgHandler::parsePen could not resolve property" << idFromUrl(value);
}
- //since we could inherit stroke="none"
- //we need to reset the style of our stroke to something
- pen.setStyle(Qt::SolidLine);
- }
- if (!width.isEmpty()) {
- QSvgHandler::LengthType lt;
- qreal widthF = parseLength(width, lt, handler);
- //### fixme
- if (!widthF) {
- pen.setStyle(Qt::NoPen);
- return;
+ } else if (value == QLatin1String("none")) {
+ QColor color; //### fixme: dafalut value for color.
+ color.setAlphaF(strokeAlpha);
+ pen.setColor(color);
+ stroke = false; // This is required because, parent may have a valid stroke but the child may have stroke = "none"
+ } else {
+ QColor color;
+ if (resolveColor(value, color, handler)) {
+ color.setAlphaF(strokeAlpha);
+ pen.setColor(color);
}
- pen.setWidthF(widthF);
- }
-
- if (!linejoin.isEmpty()) {
- if (linejoin == QLatin1String("miter"))
- pen.setJoinStyle(Qt::SvgMiterJoin);
- else if (linejoin == QLatin1String("round"))
- pen.setJoinStyle(Qt::RoundJoin);
- else if (linejoin == QLatin1String("bevel"))
- pen.setJoinStyle(Qt::BevelJoin);
+ stroke = true;
}
+ } else {
+ QColor color = pen.color();
+ color.setAlphaF(strokeAlpha);
+ pen.setColor(color);
+ }
- if (!linecap.isEmpty()) {
- if (linecap == QLatin1String("butt"))
- pen.setCapStyle(Qt::FlatCap);
- else if (linecap == QLatin1String("round"))
- pen.setCapStyle(Qt::RoundCap);
- else if (linecap == QLatin1String("square"))
- pen.setCapStyle(Qt::SquareCap);
- }
+ //stroke-width handling
+ if (!width.isEmpty() && width != QLatin1String("inherit")) {
+ qreal widthF;
+ QSvgHandler::LengthType lt;
+ widthF = parseLength(width, lt, handler);
+ pen.setWidthF(widthF);
+ } else if (inherited){
+ qreal widthF = inherited->qpen().widthF();
+ pen.setWidthF(widthF);
+ }
- qreal penw = pen.widthF();
- if (!dashArray.isEmpty()) {
- const QChar *s = dashArray.constData();
- QVector<qreal> dashes = parseNumbersList(s);
- qreal *d = dashes.data();
- if(penw != 0)
- for (int i = 0; i < dashes.size(); ++i) {
- *d /= penw;
- ++d;
- }
+ //stroke-linejoin attribute handling
+ if (!linejoin.isEmpty()) {
+ if (linejoin == QLatin1String("miter"))
+ pen.setJoinStyle(Qt::SvgMiterJoin);
+ else if (linejoin == QLatin1String("round"))
+ pen.setJoinStyle(Qt::RoundJoin);
+ else if (linejoin == QLatin1String("bevel"))
+ pen.setJoinStyle(Qt::BevelJoin);
+ }
- // if the dash count is odd the dashes should be duplicated
- if (dashes.size() % 2 != 0)
- dashes << QVector<qreal>(dashes);
+ //stroke-linecap attribute handling
+ if (!linecap.isEmpty()) {
+ if (linecap == QLatin1String("butt"))
+ pen.setCapStyle(Qt::FlatCap);
+ else if (linecap == QLatin1String("round"))
+ pen.setCapStyle(Qt::RoundCap);
+ else if (linecap == QLatin1String("square"))
+ pen.setCapStyle(Qt::SquareCap);
+ }
- pen.setDashPattern(dashes);
- }
- if (!dashOffset.isEmpty()) {
- qreal doffset = toDouble(dashOffset);
- if (penw != 0)
- doffset /= penw;
- pen.setDashOffset(doffset);
+ //strok-dasharray attribute handling
+ qreal penw = pen.widthF();
+ if (!dashArray.isEmpty() && dashArray != QLatin1String("inherit")) {
+ const QChar *s = dashArray.constData();
+ QVector<qreal> dashes = parseNumbersList(s);
+ qreal *d = dashes.data();
+ if (penw != 0)
+ for (int i = 0; i < dashes.size(); ++i) {
+ *d /= penw;
+ ++d;
+ }
+ // if the dash count is odd the dashes should be duplicated
+ if (dashes.size() % 2 != 0)
+ dashes << QVector<qreal>(dashes);
+ pen.setDashPattern(dashes);
+ } else if (inherited) {
+ QVector<qreal> dashes(inherited->qpen().dashPattern());
+ qreal *d = dashes.data();
+ if (!penw)
+ penw = 1.0;
+ qreal inheritpenw = inherited->qpen().widthF();
+ if (!inheritpenw)
+ inheritpenw = 1.0;
+ for ( int i = 0; i < dashes.size(); ++i) {
+ *d *= (inheritpenw/ penw);
+ ++d;
}
- if (!miterlimit.isEmpty())
- pen.setMiterLimit(toDouble(miterlimit));
+ pen.setDashPattern(dashes);
+ }
- node->appendStyleProperty(new QSvgStrokeStyle(pen), myId);
- } else {
- QPen pen(handler->defaultPen());
- pen.setStyle(Qt::NoPen);
- node->appendStyleProperty(new QSvgStrokeStyle(pen), myId);
+
+ //stroke-dashoffset attribute handling
+ if (!dashOffset.isEmpty() && dashOffset != QLatin1String("inherit")) {
+ qreal doffset = toDouble(dashOffset);
+ if (penw != 0)
+ doffset /= penw;
+ pen.setDashOffset(doffset);
+ } else if (inherited) {
+ qreal doffset = pen.dashOffset();
+ if (!penw)
+ penw = 1.0;
+ qreal inheritpenw = inherited->qpen().widthF();
+ if (!inheritpenw)
+ inheritpenw = 1.0;
+ doffset *= (inheritpenw/ penw);
+ pen.setDashOffset(doffset);
}
+
+ if (!miterlimit.isEmpty() && miterlimit != QLatin1String("inherit"))
+ pen.setMiterLimit(toDouble(miterlimit));
+
+ QSvgStrokeStyle *prop = new QSvgStrokeStyle(pen);
+ prop->setStroke(stroke);
+ node->appendStyleProperty(prop, myId);
}
}
@@ -1071,10 +1123,14 @@ static bool parseQFont(const QSvgAttributes &attributes,
font.setFamily(family.trimmed());
}
if (!size.isEmpty()) {
- QSvgHandler::LengthType dummy; // should always be pixel size
- fontSize = parseLength(size, dummy, handler);
- if (fontSize <= 0)
- fontSize = 1;
+ if (size == QLatin1String("inherit")) {
+ //inherited already
+ } else {
+ QSvgHandler::LengthType dummy; // should always be pixel size
+ fontSize = parseLength(size, dummy, handler);
+ if (fontSize <= 0)
+ fontSize = 1;
+ }
font.setPixelSize(qMax(1, int(fontSize)));
}
if (!style.isEmpty()) {
@@ -1154,9 +1210,13 @@ static void parseFont(QSvgNode *node,
font = inherited->qfont();
fontSize = inherited->pointSize();
}
- if (parseQFont(attributes, font, fontSize, handler)) {
+ // group or any container element can have only text-anchor and should
+ // be processed, because its children can inherit from it.
+ // So checking for text-anchor before parseQfont()
+ QString anchor = attributes.value(QLatin1String("text-anchor")).toString();
+ if (parseQFont(attributes, font, fontSize, handler) || (!anchor.isEmpty())) {
QString myId = someId(attributes);
- QString anchor = attributes.value(QLatin1String("text-anchor")).toString();
+ //QString anchor = attributes.value(QLatin1String("text-anchor")).toString();
QSvgTinyDocument *doc = node->document();
QSvgFontStyle *fontStyle = 0;
QString family = (font.family().isEmpty())?myId:font.family();
@@ -1865,7 +1925,7 @@ static bool parseDefaultTextStyle(QSvgNode *node,
if (fontStyle) {
font = fontStyle->qfont();
fontSize = fontStyle->pointSize();
- if (anchor.isEmpty())
+ if (anchor.isEmpty() || anchor == QLatin1String("inherit"))
anchor = fontStyle->textAnchor();
}
@@ -3395,7 +3455,7 @@ void QSvgHandler::init()
m_style = 0;
m_animEnd = 0;
m_defaultCoords = LT_PX;
- m_defaultPen = QPen(Qt::black, 1, Qt::NoPen, Qt::FlatCap, Qt::SvgMiterJoin);
+ m_defaultPen = QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
m_defaultPen.setMiterLimit(4);
parse();
}