| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QtTest> |
| 6 | |
| 7 | #include <QDomDocument> |
| 8 | #include <QDomElement> |
| 9 | #include <QDomNode> |
| 10 | |
| 11 | #include <qguiapplication.h> |
| 12 | #include <qdebug.h> |
| 13 | #include <qpainter.h> |
| 14 | #include <qsvggenerator.h> |
| 15 | #include <qsvgrenderer.h> |
| 16 | |
| 17 | class tst_QSvgGenerator : public QObject |
| 18 | { |
| 19 | Q_OBJECT |
| 20 | |
| 21 | public: |
| 22 | tst_QSvgGenerator(); |
| 23 | virtual ~tst_QSvgGenerator(); |
| 24 | |
| 25 | private slots: |
| 26 | void construction(); |
| 27 | void fileName(); |
| 28 | void escapesTitle(); |
| 29 | void escapesDescription(); |
| 30 | void outputDevice(); |
| 31 | void sizeAndViewBox(); |
| 32 | void metric(); |
| 33 | void radialGradient(); |
| 34 | void fileEncoding(); |
| 35 | void fractionalFontSize(); |
| 36 | void titleAndDescription(); |
| 37 | void gradientInterpolation(); |
| 38 | void patternBrush(); |
| 39 | }; |
| 40 | |
| 41 | tst_QSvgGenerator::tst_QSvgGenerator() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | tst_QSvgGenerator::~tst_QSvgGenerator() |
| 46 | { |
| 47 | QFile::remove(QLatin1String("fileName_output.svg")); |
| 48 | QFile::remove(QLatin1String("outputDevice_output.svg")); |
| 49 | QFile::remove(QLatin1String("radial_gradient.svg")); |
| 50 | } |
| 51 | |
| 52 | void tst_QSvgGenerator::construction() |
| 53 | { |
| 54 | QSvgGenerator generator; |
| 55 | QCOMPARE(generator.fileName(), QString()); |
| 56 | QCOMPARE(generator.outputDevice(), (QIODevice *)0); |
| 57 | QCOMPARE(generator.resolution(), 72); |
| 58 | QCOMPARE(generator.size(), QSize()); |
| 59 | } |
| 60 | |
| 61 | static void removeAttribute(const QDomNode &node, const QString &attribute) |
| 62 | { |
| 63 | if (node.isNull()) |
| 64 | return; |
| 65 | |
| 66 | node.toElement().removeAttribute(attribute); |
| 67 | |
| 68 | removeAttribute(node.firstChild(), attribute); |
| 69 | removeAttribute(node.nextSibling(), attribute); |
| 70 | } |
| 71 | |
| 72 | static void compareWithoutFontInfo(const QByteArray &source, const QByteArray &reference) |
| 73 | { |
| 74 | QDomDocument sourceDoc; |
| 75 | sourceDoc.setContent(source); |
| 76 | |
| 77 | QDomDocument referenceDoc; |
| 78 | referenceDoc.setContent(reference); |
| 79 | |
| 80 | const QString fontAttributes[] = { |
| 81 | "font-family", |
| 82 | "font-size", |
| 83 | "font-weight", |
| 84 | "font-style", |
| 85 | }; |
| 86 | |
| 87 | for (const QString &attribute : fontAttributes) { |
| 88 | removeAttribute(sourceDoc, attribute); |
| 89 | removeAttribute(referenceDoc, attribute); |
| 90 | } |
| 91 | |
| 92 | QCOMPARE(sourceDoc.toByteArray(), referenceDoc.toByteArray()); |
| 93 | } |
| 94 | |
| 95 | static void checkFile(const QString &fileName) |
| 96 | { |
| 97 | QVERIFY(QFile::exists(fileName));; |
| 98 | |
| 99 | QFile file(fileName); |
| 100 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 101 | |
| 102 | QFile referenceFile(QFINDTESTDATA("referenceSvgs/" + fileName)); |
| 103 | QVERIFY(referenceFile.open(QIODevice::ReadOnly)); |
| 104 | |
| 105 | compareWithoutFontInfo(file.readAll(), referenceFile.readAll()); |
| 106 | } |
| 107 | |
| 108 | void tst_QSvgGenerator::fileName() |
| 109 | { |
| 110 | QString fileName = "fileName_output.svg"; |
| 111 | QFile::remove(fileName); |
| 112 | |
| 113 | QSvgGenerator generator; |
| 114 | generator.setFileName(fileName); |
| 115 | QCOMPARE(generator.fileName(), fileName); |
| 116 | |
| 117 | QPainter painter(&generator); |
| 118 | painter.fillRect(0, 0, 100, 100, Qt::red); |
| 119 | painter.end(); |
| 120 | |
| 121 | checkFile(fileName); |
| 122 | } |
| 123 | |
| 124 | void tst_QSvgGenerator::escapesTitle() |
| 125 | { |
| 126 | QByteArray byteArray; |
| 127 | QBuffer buffer(&byteArray); |
| 128 | |
| 129 | const QString titleThatNeedsToBeEscaped("<malicious>\"title\" 'oh no'"); |
| 130 | |
| 131 | { |
| 132 | QSvgGenerator generator; |
| 133 | |
| 134 | generator.setOutputDevice(&buffer); |
| 135 | generator.setTitle(titleThatNeedsToBeEscaped); |
| 136 | |
| 137 | QPainter painter(&generator); |
| 138 | painter.end(); |
| 139 | } |
| 140 | |
| 141 | QDomDocument generated; |
| 142 | generated.setContent(byteArray); |
| 143 | |
| 144 | const auto titleElements = generated.documentElement().elementsByTagName("title"); |
| 145 | |
| 146 | QCOMPARE(1, titleElements.size()); |
| 147 | |
| 148 | const auto theOnlyTitleElement = titleElements.at(0); |
| 149 | |
| 150 | QCOMPARE(1, theOnlyTitleElement.childNodes().size()); |
| 151 | QCOMPARE(titleThatNeedsToBeEscaped, theOnlyTitleElement.firstChild().nodeValue()); |
| 152 | } |
| 153 | |
| 154 | void tst_QSvgGenerator::escapesDescription() |
| 155 | { |
| 156 | QByteArray byteArray; |
| 157 | QBuffer buffer(&byteArray); |
| 158 | |
| 159 | const QString descriptionThatNeedsToBeEscaped("<evil>\"description\" 'whoopsie!'"); |
| 160 | |
| 161 | { |
| 162 | QSvgGenerator generator; |
| 163 | |
| 164 | generator.setOutputDevice(&buffer); |
| 165 | generator.setDescription(descriptionThatNeedsToBeEscaped); |
| 166 | |
| 167 | QPainter painter(&generator); |
| 168 | painter.end(); |
| 169 | } |
| 170 | |
| 171 | QDomDocument generated; |
| 172 | generated.setContent(byteArray); |
| 173 | |
| 174 | const auto descriptionElements = generated.documentElement().elementsByTagName("desc"); |
| 175 | |
| 176 | QCOMPARE(1, descriptionElements.size()); |
| 177 | |
| 178 | const auto theOnlyDescriptionElement = descriptionElements.at(0); |
| 179 | |
| 180 | QCOMPARE(1, theOnlyDescriptionElement.childNodes().size()); |
| 181 | QCOMPARE(descriptionThatNeedsToBeEscaped, theOnlyDescriptionElement.firstChild().nodeValue()); |
| 182 | } |
| 183 | |
| 184 | void tst_QSvgGenerator::outputDevice() |
| 185 | { |
| 186 | QString fileName = "outputDevice_output.svg"; |
| 187 | QFile::remove(fileName); |
| 188 | |
| 189 | QFile file(fileName); |
| 190 | |
| 191 | { |
| 192 | // Device is not open |
| 193 | QSvgGenerator generator; |
| 194 | generator.setOutputDevice(&file); |
| 195 | QCOMPARE(generator.outputDevice(), (QIODevice *)&file); |
| 196 | |
| 197 | QPainter painter; |
| 198 | QVERIFY(painter.begin(&generator)); |
| 199 | QCOMPARE(file.openMode(), QIODevice::OpenMode(QIODevice::Text | QIODevice::WriteOnly)); |
| 200 | file.close(); |
| 201 | } |
| 202 | { |
| 203 | // Device is not open, WriteOnly |
| 204 | QVERIFY(file.open(QIODevice::WriteOnly)); |
| 205 | |
| 206 | QSvgGenerator generator; |
| 207 | generator.setOutputDevice(&file); |
| 208 | QCOMPARE(generator.outputDevice(), (QIODevice *)&file); |
| 209 | |
| 210 | QPainter painter; |
| 211 | QVERIFY(painter.begin(&generator)); |
| 212 | QCOMPARE(file.openMode(), QIODevice::OpenMode(QIODevice::WriteOnly)); |
| 213 | file.close(); |
| 214 | } |
| 215 | { |
| 216 | // Device is not open, ReadOnly |
| 217 | QVERIFY(file.open(QIODevice::ReadOnly)); |
| 218 | |
| 219 | QSvgGenerator generator; |
| 220 | generator.setOutputDevice(&file); |
| 221 | QCOMPARE(generator.outputDevice(), (QIODevice *)&file); |
| 222 | |
| 223 | QPainter painter; |
| 224 | QTest::ignoreMessage(QtWarningMsg, "QSvgPaintEngine::begin(), could not write to read-only output device: 'Unknown error'"); |
| 225 | QVERIFY(!painter.begin(&generator)); |
| 226 | QCOMPARE(file.openMode(), QIODevice::OpenMode(QIODevice::ReadOnly)); |
| 227 | file.close(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void tst_QSvgGenerator::sizeAndViewBox() |
| 232 | { |
| 233 | { // Setting neither properties should result in |
| 234 | // none of the attributes written to the SVG |
| 235 | QSvgGenerator generator; |
| 236 | QByteArray byteArray; |
| 237 | QBuffer buffer(&byteArray); |
| 238 | generator.setOutputDevice(&buffer); |
| 239 | QPainter painter(&generator); |
| 240 | painter.end(); |
| 241 | |
| 242 | QVERIFY(!byteArray.contains("<svg width=\"")); |
| 243 | QVERIFY(!byteArray.contains("viewBox=\"")); |
| 244 | } |
| 245 | |
| 246 | { // Setting size only should write size only |
| 247 | QSvgGenerator generator; |
| 248 | QByteArray byteArray; |
| 249 | QBuffer buffer(&byteArray); |
| 250 | generator.setOutputDevice(&buffer); |
| 251 | generator.setResolution(254); |
| 252 | generator.setSize(QSize(100, 100)); |
| 253 | QPainter painter(&generator); |
| 254 | painter.end(); |
| 255 | |
| 256 | QVERIFY(byteArray.contains("<svg width=\"10mm\" height=\"10mm\"")); |
| 257 | QVERIFY(!byteArray.contains("viewBox=\"")); |
| 258 | } |
| 259 | |
| 260 | { // Setting viewBox only should write viewBox only |
| 261 | QSvgGenerator generator; |
| 262 | QByteArray byteArray; |
| 263 | QBuffer buffer(&byteArray); |
| 264 | generator.setOutputDevice(&buffer); |
| 265 | generator.setViewBox(QRectF(20, 20, 50.666, 50.666)); |
| 266 | QPainter painter(&generator); |
| 267 | painter.end(); |
| 268 | |
| 269 | QVERIFY(!byteArray.contains("<svg width=\"")); |
| 270 | QVERIFY(byteArray.contains("<svg viewBox=\"20 20 50.666 50.666\"")); |
| 271 | } |
| 272 | |
| 273 | { // Setting both properties should result in |
| 274 | // both of the attributes written to the SVG |
| 275 | QSvgGenerator generator; |
| 276 | QByteArray byteArray; |
| 277 | QBuffer buffer(&byteArray); |
| 278 | generator.setOutputDevice(&buffer); |
| 279 | generator.setResolution(254); |
| 280 | generator.setSize(QSize(500, 500)); |
| 281 | generator.setViewBox(QRectF(20.666, 20.666, 50, 50)); |
| 282 | QPainter painter(&generator); |
| 283 | painter.end(); |
| 284 | |
| 285 | QVERIFY(byteArray.contains("<svg width=\"50mm\" height=\"50mm\"")); |
| 286 | QVERIFY(byteArray.contains("viewBox=\"20.666 20.666 50 50\"")); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void tst_QSvgGenerator::metric() |
| 291 | { |
| 292 | QSvgGenerator generator; |
| 293 | generator.setSize(QSize(100, 100)); |
| 294 | generator.setResolution(254); // 254 dots per inch == 10 dots per mm |
| 295 | |
| 296 | QCOMPARE(generator.widthMM(), 10); |
| 297 | QCOMPARE(generator.heightMM(), 10); |
| 298 | } |
| 299 | |
| 300 | void tst_QSvgGenerator::radialGradient() |
| 301 | { |
| 302 | QString fileName = "radial_gradient.svg"; |
| 303 | QFile::remove(fileName); |
| 304 | |
| 305 | QSvgGenerator generator; |
| 306 | generator.setSize(QSize(200, 100)); |
| 307 | generator.setFileName(fileName); |
| 308 | QCOMPARE(generator.fileName(), fileName); |
| 309 | |
| 310 | QRadialGradient gradient(QPointF(0.5, 0.5), 0.5, QPointF(0.5, 0.5), 0); |
| 311 | gradient.setInterpolationMode(QGradient::ComponentInterpolation); |
| 312 | gradient.setColorAt(0, Qt::red); |
| 313 | gradient.setColorAt(1, Qt::blue); |
| 314 | gradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
| 315 | |
| 316 | QPainter painter(&generator); |
| 317 | painter.fillRect(0, 0, 100, 100, gradient); |
| 318 | |
| 319 | gradient = QRadialGradient(QPointF(150, 50), 50, QPointF(150, 50), 0); |
| 320 | gradient.setInterpolationMode(QGradient::ComponentInterpolation); |
| 321 | gradient.setColorAt(0, Qt::red); |
| 322 | gradient.setColorAt(1, Qt::blue); |
| 323 | painter.fillRect(100, 0, 100, 100, gradient); |
| 324 | painter.end(); |
| 325 | |
| 326 | checkFile(fileName); |
| 327 | } |
| 328 | |
| 329 | void tst_QSvgGenerator::fileEncoding() |
| 330 | { |
| 331 | QByteArray byteArray; |
| 332 | QBuffer buffer(&byteArray); |
| 333 | |
| 334 | QSvgGenerator generator; |
| 335 | generator.setOutputDevice(&buffer); |
| 336 | |
| 337 | static const QChar unicode[] = { 'f', 'o', 'o', |
| 338 | u'\u00F8', 'b', 'a', 'r'}; |
| 339 | |
| 340 | int size = sizeof(unicode) / sizeof(QChar); |
| 341 | QString unicodeString = QString::fromRawData(unicode, size); |
| 342 | |
| 343 | QPainter painter(&generator); |
| 344 | painter.drawText(100, 100, unicodeString); |
| 345 | painter.end(); |
| 346 | |
| 347 | QVERIFY(byteArray.contains(unicodeString.toUtf8())); |
| 348 | } |
| 349 | |
| 350 | void tst_QSvgGenerator::fractionalFontSize() |
| 351 | { |
| 352 | QByteArray byteArray; |
| 353 | QBuffer buffer(&byteArray); |
| 354 | |
| 355 | QSvgGenerator generator; |
| 356 | generator.setResolution(72); |
| 357 | generator.setOutputDevice(&buffer); |
| 358 | |
| 359 | QPainter painter(&generator); |
| 360 | QFont fractionalFont = painter.font(); |
| 361 | fractionalFont.setPointSizeF(7.5); |
| 362 | painter.setFont(fractionalFont); |
| 363 | painter.drawText(100, 100, "foo"); |
| 364 | painter.end(); |
| 365 | |
| 366 | QVERIFY(byteArray.contains("7.5")); |
| 367 | } |
| 368 | |
| 369 | void tst_QSvgGenerator::titleAndDescription() |
| 370 | { |
| 371 | QByteArray byteArray; |
| 372 | QBuffer buffer(&byteArray); |
| 373 | |
| 374 | QSvgGenerator generator; |
| 375 | generator.setTitle("foo"); |
| 376 | QCOMPARE(generator.title(), QString("foo")); |
| 377 | generator.setDescription("bar"); |
| 378 | QCOMPARE(generator.description(), QString("bar")); |
| 379 | generator.setOutputDevice(&buffer); |
| 380 | |
| 381 | QPainter painter(&generator); |
| 382 | painter.end(); |
| 383 | |
| 384 | QVERIFY(byteArray.contains("<title>foo</title>")); |
| 385 | QVERIFY(byteArray.contains("<desc>bar</desc>")); |
| 386 | } |
| 387 | |
| 388 | static void drawTestGradients(QPainter &painter) |
| 389 | { |
| 390 | int w = painter.device()->width(); |
| 391 | int h = painter.device()->height(); |
| 392 | if (w <= 0 || h <= 0) |
| 393 | h = w = 72; |
| 394 | |
| 395 | QLinearGradient gradient(QPoint(0, 0), QPoint(1, 1)); |
| 396 | gradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
| 397 | gradient.setColorAt(0, QColor(255, 0, 0, 0)); |
| 398 | gradient.setColorAt(1, QColor(0, 0, 255, 255)); |
| 399 | painter.fillRect(QRectF(0, 0, w/2, h/2), gradient); |
| 400 | |
| 401 | gradient.setInterpolationMode(QGradient::ComponentInterpolation); |
| 402 | painter.fillRect(QRectF(0, h/2, w/2, h - h/2), gradient); |
| 403 | |
| 404 | gradient.setInterpolationMode(QGradient::ColorInterpolation); |
| 405 | gradient.setColorAt(0, QColor(255, 0, 0, 123)); |
| 406 | gradient.setColorAt(1, QColor(0, 0, 255, 123)); |
| 407 | painter.fillRect(QRectF(w/2, 0, w - w/2, h/2), gradient); |
| 408 | |
| 409 | gradient.setInterpolationMode(QGradient::ComponentInterpolation); |
| 410 | painter.fillRect(QRectF(w/2, h/2, w - w/2, h - h/2), gradient); |
| 411 | } |
| 412 | |
| 413 | static qreal sqrImageDiff(const QImage &image1, const QImage &image2) |
| 414 | { |
| 415 | if (image1.size() != image2.size()) |
| 416 | return 1e30; |
| 417 | quint64 sum = 0; |
| 418 | for (int y = 0; y < image1.height(); ++y) { |
| 419 | const quint8 *line1 = reinterpret_cast<const quint8 *>(image1.scanLine(y)); |
| 420 | const quint8 *line2 = reinterpret_cast<const quint8 *>(image2.scanLine(y)); |
| 421 | for (int x = 0; x < image1.width() * 4; ++x) |
| 422 | sum += quint64((int(line1[x]) - int(line2[x])) * (int(line1[x]) - int(line2[x]))); |
| 423 | } |
| 424 | return qreal(sum) / qreal(image1.width() * image1.height()); |
| 425 | } |
| 426 | |
| 427 | void tst_QSvgGenerator::gradientInterpolation() |
| 428 | { |
| 429 | QByteArray byteArray; |
| 430 | QPainter painter; |
| 431 | QImage image(576, 576, QImage::Format_ARGB32_Premultiplied); |
| 432 | QImage refImage(576, 576, QImage::Format_ARGB32_Premultiplied); |
| 433 | image.fill(0x80208050); |
| 434 | refImage.fill(0x80208050); |
| 435 | |
| 436 | { |
| 437 | QSvgGenerator generator; |
| 438 | QBuffer buffer(&byteArray); |
| 439 | generator.setOutputDevice(&buffer); |
| 440 | |
| 441 | QVERIFY(painter.begin(&generator)); |
| 442 | drawTestGradients(painter); |
| 443 | painter.end(); |
| 444 | } |
| 445 | |
| 446 | { |
| 447 | QVERIFY(painter.begin(&image)); |
| 448 | QSvgRenderer renderer(byteArray); |
| 449 | renderer.render(&painter, image.rect()); |
| 450 | painter.end(); |
| 451 | } |
| 452 | |
| 453 | { |
| 454 | QVERIFY(painter.begin(&refImage)); |
| 455 | drawTestGradients(painter); |
| 456 | painter.end(); |
| 457 | } |
| 458 | |
| 459 | QVERIFY(sqrImageDiff(image, refImage) < 2); // pixel error < 1.41 (L2-norm) |
| 460 | } |
| 461 | |
| 462 | void tst_QSvgGenerator::patternBrush() |
| 463 | { |
| 464 | { // Pattern brush should create mask and pattern used as fill |
| 465 | QSvgGenerator generator; |
| 466 | QByteArray byteArray; |
| 467 | QBuffer buffer(&byteArray); |
| 468 | generator.setOutputDevice(&buffer); |
| 469 | QPainter painter(&generator); |
| 470 | painter.setBrush(Qt::CrossPattern); |
| 471 | painter.drawRect(0, 0, 100, 100); |
| 472 | painter.end(); |
| 473 | |
| 474 | QVERIFY(byteArray.contains("<mask id=\"patternmask")); |
| 475 | QVERIFY(byteArray.contains("<pattern id=\"fillpattern")); |
| 476 | QVERIFY(byteArray.contains("<g fill=\"url(#fillpattern")); |
| 477 | } |
| 478 | |
| 479 | { // Masks and patterns should be reused, not regenerated |
| 480 | QSvgGenerator generator; |
| 481 | QByteArray byteArray; |
| 482 | QBuffer buffer(&byteArray); |
| 483 | generator.setOutputDevice(&buffer); |
| 484 | QPainter painter(&generator); |
| 485 | painter.setBrush(QBrush(Qt::red, Qt::Dense3Pattern)); |
| 486 | painter.drawRect(0, 0, 100, 100); |
| 487 | painter.drawEllipse(200, 50, 50, 50); |
| 488 | painter.setBrush(QBrush(Qt::green, Qt::Dense3Pattern)); |
| 489 | painter.drawRoundedRect(0, 200, 100, 100, 10, 10); |
| 490 | painter.setBrush(QBrush(Qt::blue, Qt::Dense4Pattern)); |
| 491 | painter.drawRect(200, 200, 100, 100); |
| 492 | painter.setBrush(QBrush(Qt::red, Qt::Dense3Pattern)); |
| 493 | painter.drawRoundedRect(120, 120, 60, 60, 5, 5); |
| 494 | painter.end(); |
| 495 | |
| 496 | QCOMPARE(byteArray.count("<mask id=\"patternmask"), 2); |
| 497 | QCOMPARE(byteArray.count("<pattern id=\"fillpattern"), 3); |
| 498 | QVERIFY(byteArray.count("<g fill=\"url(#fillpattern") >= 4); |
| 499 | } |
| 500 | |
| 501 | } |
| 502 | |
| 503 | QTEST_MAIN(tst_QSvgGenerator) |
| 504 | #include "tst_qsvggenerator.moc" |