diff options
Diffstat (limited to 'tests/auto/qsvgrenderer')
-rw-r--r-- | tests/auto/qsvgrenderer/qsvgrenderer.pro | 2 | ||||
-rw-r--r-- | tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp | 651 |
2 files changed, 643 insertions, 10 deletions
diff --git a/tests/auto/qsvgrenderer/qsvgrenderer.pro b/tests/auto/qsvgrenderer/qsvgrenderer.pro index 120d9f0..8cfbcce 100644 --- a/tests/auto/qsvgrenderer/qsvgrenderer.pro +++ b/tests/auto/qsvgrenderer/qsvgrenderer.pro @@ -8,7 +8,7 @@ QT += svg SOURCES += tst_qsvgrenderer.cpp RESOURCES += resources.qrc -wince*: { +wince*|symbian { addFiles.sources = *.svg *.svgz addFiles.path = . diff --git a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp index 6fe6229..9700085 100644 --- a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp +++ b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp @@ -75,7 +75,15 @@ private slots: void stylePropagation() const; void matrixForElement() const; void gradientStops() const; + void gradientRefs(); void fillRule(); + void opacity(); + void paths(); + void displayMode(); + void strokeInherit(); + void testFillInheritance(); + void testStopOffsetOpacity(); + void testUseElement(); #ifndef QT_NO_COMPRESS void testGzLoading(); @@ -331,7 +339,7 @@ void tst_QSvgRenderer::nestedQXmlStreamReader() const QCOMPARE(reader.readNext(), QXmlStreamReader::StartDocument); QCOMPARE(reader.readNext(), QXmlStreamReader::StartElement); QCOMPARE(reader.name().toString(), QLatin1String("bar")); - + QPicture picture; QPainter painter(&picture); QSvgRenderer renderer(&reader); @@ -533,7 +541,10 @@ void tst_QSvgRenderer::gradientStops() const QCOMPARE(image, refImage); } +} +void tst_QSvgRenderer::gradientRefs() +{ const char *svgs[] = { "<svg>" "<defs>" @@ -565,6 +576,17 @@ void tst_QSvgRenderer::gradientStops() const "</linearGradient>" "</defs>" "<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" + "</svg>", + "<svg>" + "<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" + "<defs>" + "<linearGradient id=\"gradient0\">" + "<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" + "<stop offset=\"1\" stop-color=\"blue\"/>" + "</linearGradient>" + "<linearGradient id=\"gradient\" xlink:href=\"#gradient0\">" + "</linearGradient>" + "</defs>" "</svg>" }; for (int i = 0 ; i < sizeof(svgs) / sizeof(svgs[0]) ; ++i) @@ -589,6 +611,7 @@ void tst_QSvgRenderer::gradientStops() const } } + #ifndef QT_NO_COMPRESS void tst_QSvgRenderer::testGzLoading() { @@ -605,9 +628,11 @@ void tst_QSvgRenderer::testGzLoading() QVERIFY(autoDetectGzData.isValid()); } +#ifdef QT_BUILD_INTERNAL QT_BEGIN_NAMESPACE QByteArray qt_inflateGZipDataFrom(QIODevice *device); QT_END_NAMESPACE +#endif void tst_QSvgRenderer::testGzHelper_data() { @@ -640,6 +665,7 @@ void tst_QSvgRenderer::testGzHelper_data() void tst_QSvgRenderer::testGzHelper() { +#ifdef QT_BUILD_INTERNAL QFETCH(QByteArray, in); QFETCH(QByteArray, out); @@ -648,27 +674,634 @@ void tst_QSvgRenderer::testGzHelper() QVERIFY(buffer.isReadable()); QByteArray result = qt_inflateGZipDataFrom(&buffer); QCOMPARE(result, out); +#endif } #endif void tst_QSvgRenderer::fillRule() { - QByteArray data( + static const char *svgs[] = { + // Paths + // Default fill-rule (nonzero) + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" />" + "</svg>", + // nonzero + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"nonzero\" />" + "</svg>", + // evenodd "<svg>" - "<rect x=\"0\" y=\"0\" height=\"300\" width=\"400\" fill=\"blue\" />" - "<path d=\"M100 200 L300 200 L300 100 L100 100 M100 200 L300 200 L300 100 L100 100 Z\" fill=\"red\" stroke=\"black\" />" - "</svg>"); + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", - QSvgRenderer renderer(data); + // Polygons + // Default fill-rule (nonzero) + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" />" + "</svg>", + // nonzero + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"nonzero\" />" + "</svg>", + // evenodd + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>" + }; - QImage image(128, 128, QImage::Format_ARGB32_Premultiplied); - image.fill(0); + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage refImageNonZero(30, 30, QImage::Format_ARGB32_Premultiplied); + QImage refImageEvenOdd(30, 30, QImage::Format_ARGB32_Premultiplied); + refImageNonZero.fill(0xffff0000); + refImageEvenOdd.fill(0xffff0000); + QPainter p; + p.begin(&refImageNonZero); + p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); + p.end(); + p.begin(&refImageEvenOdd); + p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); + p.fillRect(QRectF(10, 10, 10, 10), Qt::blue); + p.end(); + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QImage image(30, 30, QImage::Format_ARGB32_Premultiplied); + image.fill(0); + p.begin(&image); + renderer.render(&p); + p.end(); + QCOMPARE(image, i % 3 == 2 ? refImageEvenOdd : refImageNonZero); + } +} +static void opacity_drawSvgAndVerify(const QByteArray &data) +{ + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); + image.fill(0xffff00ff); QPainter painter(&image); renderer.render(&painter); painter.end(); + QCOMPARE(image.pixel(5, 5), 0xff7f7f7f); +} + +void tst_QSvgRenderer::opacity() +{ + static const char *opacities[] = {"-1.4641", "0", "0.5", "1", "1.337"}; + static const char *firstColors[] = {"#7f7f7f", "#7f7f7f", "#402051", "blue", "#123456"}; + static const char *secondColors[] = {"red", "#bad", "#bedead", "#7f7f7f", "#7f7f7f"}; + + // Fill-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(firstColors[i]); + data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(secondColors[i]); + data.append("\" fill-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // Stroke-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg viewBox=\"0 0 10 10\"><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); + data.append(firstColors[i]); + data.append("\" stroke-width=\"10\"/><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\""); + data.append(secondColors[i]); + data.append("\" stroke-width=\"10\" stroke-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // As gradients: + // Fill-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg><defs><linearGradient id=\"gradient\"><stop offset=\"0\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/><stop offset=\"1\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/></linearGradient></defs><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(firstColors[i]); + data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"url(#gradient)\" fill-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // Stroke-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg viewBox=\"0 0 10 10\"><defs><linearGradient id=\"grad\"><stop offset=\"0\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/><stop offset=\"1\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/></linearGradient></defs><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); + data.append(firstColors[i]); + data.append("\" stroke-width=\"10\" /><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\"url(#grad)\" stroke-width=\"10\" stroke-opacity=\""); + data.append(opacities[i]); + data.append("\" /></svg>"); + opacity_drawSvgAndVerify(data); + } +} + +void tst_QSvgRenderer::paths() +{ + static const char *svgs[] = { + // Absolute coordinates, explicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"M50 0 V50 H0 Q0 25 25 25 T50 0 C25 0 50 50 25 50 S25 0 0 0 Z\" fill=\"red\" fill-rule=\"evenodd\"/>" + "</svg>", + // Absolute coordinates, implicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"M50 0 50 50 0 50 Q0 25 25 25 Q50 25 50 0 C25 0 50 50 25 50 C0 50 25 0 0 0 Z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Relative coordinates, explicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0 v50 h-50 q0 -25 25 -25 t25 -25 c-25 0 0 50 -25 50 s0 -50 -25 -50 z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Relative coordinates, implicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0 0 50 -50 0 q0 -25 25 -25 25 0 25 -25 c-25 0 0 50 -25 50 -25 0 0 -50 -25 -50 z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Absolute coordinates, explicit commands, minimal whitespace. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0v50h-50q0-25 25-25t25-25c-25 0 0 50-25 50s0-50-25-50z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Absolute coordinates, explicit commands, extra whitespace. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\" M 50 0 V 50 H 0 Q 0 25 25 25 T 50 0 C 25 0 50 50 25 50 S 25 0 0 0 Z \" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>" + }; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage images[COUNT]; + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + images[i] = QImage(50, 50, QImage::Format_ARGB32_Premultiplied); + images[i].fill(0); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + if (i != 0) { + QCOMPARE(images[i], images[0]); + } + } +} + +void tst_QSvgRenderer::displayMode() +{ + static const char *svgs[] = { + // All visible. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display svg element. + "<svg display=\"none\">" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display g element. + "<svg>" + " <g display=\"none\">" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display first rect element. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"none\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display second rect element. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"none\" />" + " </g>" + "</svg>", + // Don't display svg element, but set display mode to "inline" for other elements. + "<svg display=\"none\">" + " <g display=\"inline\">" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"inline\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"inline\" />" + " </g>" + "</svg>" + }; + + QRgb expectedColors[] = {0xff0000ff, 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00}; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); + image.fill(0xff00ff00); + p.begin(&image); + renderer.render(&p); + p.end(); + QCOMPARE(image.pixel(5, 5), expectedColors[i]); + } +} + +void tst_QSvgRenderer::strokeInherit() +{ + static const char *svgs[] = { + // Reference. + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\"/>" + " </g>" + "</svg>", + // stroke + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"none\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke=\"blue\"/>" + " </g>" + " <g stroke=\"yellow\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\" stroke=\"green\"/>" + " </g>" + "</svg>", + // stroke-width + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"0\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-width=\"20\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"10\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\" stroke-width=\"0\"/>" + " </g>" + "</svg>", + // stroke-linecap + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"round\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-linecap=\"butt\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\"/>" + " </g>" + "</svg>", + // stroke-linejoin + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"round\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-linejoin=\"miter\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\"/>" + " </g>" + "</svg>", + // stroke-miterlimit + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"2\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-miterlimit=\"1\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\"/>" + " </g>" + "</svg>", + // stroke-dasharray + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"1,1,1,1,1,1,3,1,3,1,3,1,1,1,1,1,1,3\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-dasharray=\"20,10\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"none\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\" stroke-dasharray=\"3,3,1\"/>" + " </g>" + "</svg>", + // stroke-dashoffset + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"0\" stroke-opacity=\"0.5\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-dashoffset=\"10\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"0\">" + " <polyline fill=\"none\" points=\"10 25 80 25\" stroke-dashoffset=\"4.5\"/>" + " </g>" + "</svg>", + // stroke-opacity + "<svg viewBox=\"0 0 200 30\">" + " <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" + " stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" + " stroke-dashoffset=\"10\" stroke-opacity=\"0\">" + " <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-opacity=\"0.5\"/>" + " </g>" + " <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" + " <polyline fill=\"none\" points=\"10 25 80 25\"/>" + " </g>" + "</svg>" + }; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage images[COUNT]; + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + images[i] = QImage(200, 30, QImage::Format_ARGB32_Premultiplied); + images[i].fill(-1); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + if (i != 0) { + QCOMPARE(images[0], images[i]); + } + } +} - QCOMPARE(image.pixel(64, 64), QColor(Qt::red).rgba()); +void tst_QSvgRenderer::testFillInheritance() +{ + static const char *svgs[] = { + //reference + "<svg viewBox = \"0 0 200 200\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" + " <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\"/>" + " </g>" + " <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"green\" fill-rule = \"nonzero\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\" fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" + " </g>" + " <g fill-opacity = \"0.8\" fill = \"red\">" + " <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" + " </g>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"red\" >" + " <g fill-opacity = \"0.5\">" + " <g fill-rule = \"evenodd\">" + " <g>" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\"/>" + " </g>" + " </g>" + " </g>" + " </g>" + " <g fill-opacity = \"0.8\" >" + " <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"none\"/>" + " </g>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"none\" fill-opacity = \"0\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\" fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" + " </g>" + " <g fill-opacity = \"0\" >" + " <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\"/>" + " </g>" + "</svg>" + }; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage images[COUNT]; + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + images[i] = QImage(200, 200, QImage::Format_ARGB32_Premultiplied); + images[i].fill(-1); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + if (i != 0) { + QCOMPARE(images[0], images[i]); + } + } +} +void tst_QSvgRenderer::testStopOffsetOpacity() +{ + static const char *svgs[] = { + //reference + "<svg viewBox=\"0 0 64 64\">" + "<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" + "<stop offset=\"0.0\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"1\"/>" + "<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"1\"/>" + "</radialGradient>" + "<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" + "<stop offset=\"0.0\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"1\"/>" + "<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"1\"/>" + "</radialGradient>" + "<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" + "<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" + "</svg>", + //Stop Offset + "<svg viewBox=\"0 0 64 64\">" + "<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" + "<stop offset=\"abc\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"1\"/>" + "<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"1\"/>" + "</radialGradient>" + "<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" + "<stop offset=\"-3.bc\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"1\"/>" + "<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"1\"/>" + "</radialGradient>" + "<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" + "<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" + "</svg>", + //Stop Opacity + "<svg viewBox=\"0 0 64 64\">" + "<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" + "<stop offset=\"0.0\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"x.45\"/>" + "<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"-3.abc\"/>" + "</radialGradient>" + "<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" + "<stop offset=\"0.0\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"-0.xy\"/>" + "<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"z.5\"/>" + "</radialGradient>" + "<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" + "<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" + "</svg>", + //Stop offset and Stop opacity + "<svg viewBox=\"0 0 64 64\">" + "<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" + "<stop offset=\"abc\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"x.45\"/>" + "<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"-3.abc\"/>" + "</radialGradient>" + "<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" + "<stop offset=\"-3.bc\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" + "<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"-0.xy\"/>" + "<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"z.5\"/>" + "</radialGradient>" + "<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" + "<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" + "</svg>" + }; + + QImage images[4]; + QPainter p; + + for (int i = 0; i < 4; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + images[i] = QImage(64, 64, QImage::Format_ARGB32_Premultiplied); + images[i].fill(-1); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + } + QCOMPARE(images[0], images[1]); + QCOMPARE(images[0], images[2]); + QCOMPARE(images[0], images[3]); +} + +void tst_QSvgRenderer::testUseElement() +{ + static const char *svgs[] = { + //Use refering to non group node (1) + "<svg viewBox = \"0 0 200 200\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\"/>" + " <polygon points=\"20,80 50,180 100,70 40,140 50,140\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" + " <use y = \"60\" xlink:href = \"#usedPolyline\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" + " <g fill = \" red\" fill-opacity =\"0.2\">" + "<use y = \"60\" xlink:href = \"#usedPolyline\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\"/>" + "</g>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" + " <g stroke-width = \"3\" stroke = \"yellow\">" + " <use y = \"60\" xlink:href = \"#usedPolyline\" fill = \" red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\"/>" + " </g>" + "</svg>", + //Use refering to non group node (2) + "<svg viewBox = \"0 0 200 200\">" + " <polygon points=\"20,20 50,120 100,10 40,80 50,80\" fill = \"green\" fill-rule = \"nonzero\" stroke = \"purple\" stroke-width = \"4\" stroke-dasharray = \"1,1,3,1\" stroke-offset = \"3\" stroke-miterlimit = \"6\" stroke-linecap = \"butt\" stroke-linejoin = \"round\"/>" + " <polygon points=\"20,80 50,180 100,70 40,140 50,140\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\" stroke-dasharray = \"1,1,1,1\" stroke-offset = \"5\" stroke-miterlimit = \"3\" stroke-linecap = \"butt\" stroke-linejoin = \"square\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"green\" fill-rule = \"nonzero\" stroke = \"purple\" stroke-width = \"4\" stroke-dasharray = \"1,1,3,1\" stroke-offset = \"3\" stroke-miterlimit = \"6\" stroke-linecap = \"butt\" stroke-linejoin = \"round\">" + " <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\" />" + " </g>" + " <g stroke = \"blue\" stroke-width = \"3\" stroke-dasharray = \"1,1,1,1\" stroke-offset = \"5\" stroke-miterlimit = \"3\" stroke-linecap = \"butt\" stroke-linejoin = \"square\">" + " <use y = \"60\" xlink:href = \"#usedPolyline\" fill-opacity = \"0.7\" fill= \"red\" stroke = \"blue\" fill-rule = \"evenodd\"/>" + " </g>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <g fill = \"green\" fill-rule = \"nonzero\" stroke = \"purple\" stroke-width = \"4\" stroke-dasharray = \"1,1,3,1\" stroke-offset = \"3\" stroke-miterlimit = \"6\" stroke-linecap = \"butt\" stroke-linejoin = \"round\">" + " <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\" />" + " </g>" + " <g stroke-width = \"3\" stroke-dasharray = \"1,1,1,1\" stroke-offset = \"5\" stroke-miterlimit = \"3\" stroke-linecap = \"butt\" stroke-linejoin = \"square\" >" + " <use y = \"60\" xlink:href = \"#usedPolyline\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" />" + " </g>" + "</svg>", + //Use refering to group node + "<svg viewBox = \"0 0 200 200\">" + " <g>" + " <circle cx=\"0\" cy=\"0\" r=\"100\" fill = \"red\" fill-opacity = \"0.6\"/>" + " <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\" fill = \"red\" fill-opacity = \"0.5\"/>" + " <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" fill-opacity = \"0.5\"/>" + " </g>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <defs>" + " <g id=\"usedG\">" + " <circle cx=\"0\" cy=\"0\" r=\"100\" fill-opacity = \"0.6\"/>" + " <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\"/>" + " <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" />" + " </g>" + " </defs>" + " <use xlink:href =\"#usedG\" fill = \"red\" fill-opacity =\"0.5\"/>" + "</svg>", + "<svg viewBox = \"0 0 200 200\">" + " <defs>" + " <g fill = \"blue\" fill-opacity = \"0.3\">" + " <g id=\"usedG\">" + " <circle cx=\"0\" cy=\"0\" r=\"100\" fill-opacity = \"0.6\"/>" + " <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\"/>" + " <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" />" + " </g>" + " </g>" + " </defs>" + " <g fill = \"red\" fill-opacity =\"0.5\">" + " <use xlink:href =\"#usedG\" />" + " </g>" + "</svg>" + }; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage images[COUNT]; + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + images[i] = QImage(200, 200, QImage::Format_ARGB32_Premultiplied); + images[i].fill(-1); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + if (i < 4 && i != 0) { + QCOMPARE(images[0], images[i]); + } else if (i > 4 && i < 7) { + QCOMPARE(images[4], images[i]); + } else if (i > 7) { + QCOMPARE(images[8], images[i]); + } + } } QTEST_MAIN(tst_QSvgRenderer) |