author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qapplication.h> |
|
46 |
#include <qdebug.h> |
|
47 |
#include <qsvgrenderer.h> |
|
48 |
#include <qsvggenerator.h> |
|
49 |
#include <QPainter> |
|
50 |
#include <QPen> |
|
51 |
#include <QPicture> |
|
52 |
#include <QXmlStreamReader> |
|
53 |
||
54 |
//TESTED_CLASS= |
|
55 |
//TESTED_FILES= |
|
56 |
||
57 |
class tst_QSvgRenderer : public QObject |
|
58 |
{ |
|
59 |
Q_OBJECT |
|
60 |
||
61 |
public: |
|
62 |
tst_QSvgRenderer(); |
|
63 |
virtual ~tst_QSvgRenderer(); |
|
64 |
||
65 |
private slots: |
|
66 |
void getSetCheck(); |
|
67 |
void inexistentUrl(); |
|
68 |
void emptyUrl(); |
|
69 |
void testStrokeWidth(); |
|
70 |
void testMapViewBoxToTarget(); |
|
71 |
void testRenderElement(); |
|
72 |
void constructorQXmlStreamReader() const; |
|
73 |
void loadQXmlStreamReader() const; |
|
74 |
void nestedQXmlStreamReader() const; |
|
75 |
void stylePropagation() const; |
|
76 |
void matrixForElement() const; |
|
77 |
void gradientStops() const; |
|
78 |
void gradientRefs(); |
|
79 |
void fillRule(); |
|
80 |
void opacity(); |
|
81 |
void paths(); |
|
82 |
void displayMode(); |
|
83 |
void strokeInherit(); |
|
84 |
void testFillInheritance(); |
|
85 |
void testStopOffsetOpacity(); |
|
86 |
void testUseElement(); |
|
87 |
||
88 |
#ifndef QT_NO_COMPRESS |
|
89 |
void testGzLoading(); |
|
90 |
void testGzHelper_data(); |
|
91 |
void testGzHelper(); |
|
92 |
#endif |
|
93 |
||
94 |
private: |
|
95 |
static const char *const src; |
|
96 |
}; |
|
97 |
||
98 |
const char *const tst_QSvgRenderer::src = "<svg><g><rect x='250' y='250' width='500' height='500'/>" |
|
99 |
"<rect id='foo' x='400' y='400' width='100' height='100'/></g></svg>"; |
|
100 |
||
101 |
tst_QSvgRenderer::tst_QSvgRenderer() |
|
102 |
{ |
|
103 |
} |
|
104 |
||
105 |
tst_QSvgRenderer::~tst_QSvgRenderer() |
|
106 |
{ |
|
107 |
} |
|
108 |
||
109 |
// Testing get/set functions |
|
110 |
void tst_QSvgRenderer::getSetCheck() |
|
111 |
{ |
|
112 |
QSvgRenderer obj1; |
|
113 |
// int QSvgRenderer::framesPerSecond() |
|
114 |
// void QSvgRenderer::setFramesPerSecond(int) |
|
115 |
obj1.setFramesPerSecond(20); |
|
116 |
QCOMPARE(20, obj1.framesPerSecond()); |
|
117 |
obj1.setFramesPerSecond(0); |
|
118 |
QCOMPARE(0, obj1.framesPerSecond()); |
|
119 |
obj1.setFramesPerSecond(INT_MIN); |
|
120 |
QCOMPARE(0, obj1.framesPerSecond()); // Can't have a negative framerate |
|
121 |
obj1.setFramesPerSecond(INT_MAX); |
|
122 |
QCOMPARE(INT_MAX, obj1.framesPerSecond()); |
|
123 |
} |
|
124 |
||
125 |
void tst_QSvgRenderer::inexistentUrl() |
|
126 |
{ |
|
127 |
const char *src = "<svg><g><path d=\"\" style=\"stroke:url(#inexistent)\"/></g></svg>"; |
|
128 |
||
129 |
QByteArray data(src); |
|
130 |
QSvgRenderer renderer(data); |
|
131 |
||
132 |
QVERIFY(renderer.isValid()); |
|
133 |
} |
|
134 |
||
135 |
void tst_QSvgRenderer::emptyUrl() |
|
136 |
{ |
|
137 |
const char *src = "<svg><text fill=\"url()\" /></svg>"; |
|
138 |
||
139 |
QByteArray data(src); |
|
140 |
QSvgRenderer renderer(data); |
|
141 |
||
142 |
QVERIFY(renderer.isValid()); |
|
143 |
} |
|
144 |
||
145 |
void tst_QSvgRenderer::testStrokeWidth() |
|
146 |
{ |
|
147 |
qreal squareSize = 30.0; |
|
148 |
qreal strokeWidth = 1.0; |
|
149 |
qreal topLeft = 100.0; |
|
150 |
||
151 |
QSvgGenerator generator; |
|
152 |
||
153 |
QBuffer buffer; |
|
154 |
QByteArray byteArray; |
|
155 |
buffer.setBuffer(&byteArray); |
|
156 |
generator.setOutputDevice(&buffer); |
|
157 |
||
158 |
QPainter painter(&generator); |
|
159 |
painter.setBrush(Qt::blue); |
|
160 |
||
161 |
// Draw a rect with stroke |
|
162 |
painter.setPen(QPen(Qt::black, strokeWidth)); |
|
163 |
painter.drawRect(topLeft, topLeft, squareSize, squareSize); |
|
164 |
||
165 |
// Draw a rect without stroke |
|
166 |
painter.setPen(Qt::NoPen); |
|
167 |
painter.drawRect(topLeft, topLeft, squareSize, squareSize); |
|
168 |
painter.end(); |
|
169 |
||
170 |
// Insert ID tags into the document |
|
171 |
byteArray.insert(byteArray.indexOf("stroke=\"#000000\""), "id=\"SquareStroke\" "); |
|
172 |
byteArray.insert(byteArray.indexOf("stroke=\"none\""), "id=\"SquareNoStroke\" "); |
|
173 |
||
174 |
QSvgRenderer renderer(byteArray); |
|
175 |
||
176 |
QRectF noStrokeRect = renderer.boundsOnElement("SquareNoStroke"); |
|
177 |
QCOMPARE(noStrokeRect.width(), squareSize); |
|
178 |
QCOMPARE(noStrokeRect.height(), squareSize); |
|
179 |
QCOMPARE(noStrokeRect.x(), topLeft); |
|
180 |
QCOMPARE(noStrokeRect.y(), topLeft); |
|
181 |
||
182 |
QRectF strokeRect = renderer.boundsOnElement("SquareStroke"); |
|
183 |
QCOMPARE(strokeRect.width(), squareSize + strokeWidth); |
|
184 |
QCOMPARE(strokeRect.height(), squareSize + strokeWidth); |
|
185 |
QCOMPARE(strokeRect.x(), topLeft - (strokeWidth / 2)); |
|
186 |
QCOMPARE(strokeRect.y(), topLeft - (strokeWidth / 2)); |
|
187 |
} |
|
188 |
||
189 |
void tst_QSvgRenderer::testMapViewBoxToTarget() |
|
190 |
{ |
|
191 |
const char *src = "<svg><g><rect x=\"250\" y=\"250\" width=\"500\" height=\"500\" /></g></svg>"; |
|
192 |
QByteArray data(src); |
|
193 |
||
194 |
{ // No viewport, viewBox, targetRect, or deviceRect -> boundingRect |
|
195 |
QPicture picture; |
|
196 |
QPainter painter(&picture); |
|
197 |
QSvgRenderer rend(data); |
|
198 |
rend.render(&painter); |
|
199 |
painter.end(); |
|
200 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 500, 500)); |
|
201 |
} |
|
202 |
||
203 |
{ // No viewport, viewBox, targetRect -> deviceRect |
|
204 |
QPicture picture; |
|
205 |
picture.setBoundingRect(QRect(100, 100, 200, 200)); |
|
206 |
QPainter painter(&picture); |
|
207 |
QSvgRenderer rend(data); |
|
208 |
rend.render(&painter); |
|
209 |
painter.end(); |
|
210 |
QCOMPARE(picture.boundingRect(), QRect(100, 100, 200, 200)); |
|
211 |
} |
|
212 |
||
213 |
{ // No viewport, viewBox -> targetRect |
|
214 |
QPicture picture; |
|
215 |
QPainter painter(&picture); |
|
216 |
QSvgRenderer rend(data); |
|
217 |
rend.render(&painter, QRectF(50, 50, 250, 250)); |
|
218 |
painter.end(); |
|
219 |
QCOMPARE(picture.boundingRect(), QRect(50, 50, 250, 250)); |
|
220 |
||
221 |
} |
|
222 |
||
223 |
data.replace("<svg>", "<svg viewBox=\"0 0 1000 1000\">"); |
|
224 |
||
225 |
{ // No viewport, no targetRect -> viewBox |
|
226 |
QPicture picture; |
|
227 |
QPainter painter(&picture); |
|
228 |
QSvgRenderer rend(data); |
|
229 |
rend.render(&painter); |
|
230 |
painter.end(); |
|
231 |
QCOMPARE(picture.boundingRect(), QRect(250, 250, 500, 500)); |
|
232 |
} |
|
233 |
||
234 |
data.replace("<svg", "<svg width=\"500\" height=\"500\""); |
|
235 |
||
236 |
{ // Viewport |
|
237 |
QPicture picture; |
|
238 |
QPainter painter(&picture); |
|
239 |
QSvgRenderer rend(data); |
|
240 |
rend.render(&painter); |
|
241 |
painter.end(); |
|
242 |
QCOMPARE(picture.boundingRect(), QRect(125, 125, 250, 250)); |
|
243 |
} |
|
244 |
||
245 |
} |
|
246 |
||
247 |
void tst_QSvgRenderer::testRenderElement() |
|
248 |
{ |
|
249 |
QByteArray data(src); |
|
250 |
||
251 |
{ // No viewport, viewBox, targetRect, or deviceRect -> boundingRect |
|
252 |
QPicture picture; |
|
253 |
QPainter painter(&picture); |
|
254 |
QSvgRenderer rend(data); |
|
255 |
rend.render(&painter, QLatin1String("foo")); |
|
256 |
painter.end(); |
|
257 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
258 |
} |
|
259 |
||
260 |
{ // No viewport, viewBox, targetRect -> deviceRect |
|
261 |
QPicture picture; |
|
262 |
picture.setBoundingRect(QRect(100, 100, 200, 200)); |
|
263 |
QPainter painter(&picture); |
|
264 |
QSvgRenderer rend(data); |
|
265 |
rend.render(&painter, QLatin1String("foo")); |
|
266 |
painter.end(); |
|
267 |
QCOMPARE(picture.boundingRect(), QRect(100, 100, 200, 200)); |
|
268 |
} |
|
269 |
||
270 |
{ // No viewport, viewBox -> targetRect |
|
271 |
QPicture picture; |
|
272 |
QPainter painter(&picture); |
|
273 |
QSvgRenderer rend(data); |
|
274 |
rend.render(&painter, QLatin1String("foo"), QRectF(50, 50, 250, 250)); |
|
275 |
painter.end(); |
|
276 |
QCOMPARE(picture.boundingRect(), QRect(50, 50, 250, 250)); |
|
277 |
||
278 |
} |
|
279 |
||
280 |
data.replace("<svg>", "<svg viewBox=\"0 0 1000 1000\">"); |
|
281 |
||
282 |
{ // No viewport, no targetRect -> view box size |
|
283 |
QPicture picture; |
|
284 |
QPainter painter(&picture); |
|
285 |
QSvgRenderer rend(data); |
|
286 |
rend.render(&painter, QLatin1String("foo")); |
|
287 |
painter.end(); |
|
288 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
289 |
} |
|
290 |
||
291 |
data.replace("<svg", "<svg width=\"500\" height=\"500\""); |
|
292 |
||
293 |
{ // Viewport |
|
294 |
QPicture picture; |
|
295 |
QPainter painter(&picture); |
|
296 |
QSvgRenderer rend(data); |
|
297 |
rend.render(&painter, QLatin1String("foo")); |
|
298 |
painter.end(); |
|
299 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
300 |
} |
|
301 |
||
302 |
} |
|
303 |
||
304 |
void tst_QSvgRenderer::constructorQXmlStreamReader() const |
|
305 |
{ |
|
306 |
const QByteArray data(src); |
|
307 |
||
308 |
QXmlStreamReader reader(data); |
|
309 |
||
310 |
QPicture picture; |
|
311 |
QPainter painter(&picture); |
|
312 |
QSvgRenderer rend(&reader); |
|
313 |
rend.render(&painter, QLatin1String("foo")); |
|
314 |
painter.end(); |
|
315 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
316 |
} |
|
317 |
||
318 |
void tst_QSvgRenderer::loadQXmlStreamReader() const |
|
319 |
{ |
|
320 |
const QByteArray data(src); |
|
321 |
||
322 |
QXmlStreamReader reader(data); |
|
323 |
QPicture picture; |
|
324 |
QPainter painter(&picture); |
|
325 |
QSvgRenderer rend; |
|
326 |
rend.load(&reader); |
|
327 |
rend.render(&painter, QLatin1String("foo")); |
|
328 |
painter.end(); |
|
329 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
330 |
} |
|
331 |
||
332 |
||
333 |
void tst_QSvgRenderer::nestedQXmlStreamReader() const |
|
334 |
{ |
|
335 |
const QByteArray data(QByteArray("<bar>") + QByteArray(src) + QByteArray("</bar>")); |
|
336 |
||
337 |
QXmlStreamReader reader(data); |
|
338 |
||
339 |
QCOMPARE(reader.readNext(), QXmlStreamReader::StartDocument); |
|
340 |
QCOMPARE(reader.readNext(), QXmlStreamReader::StartElement); |
|
341 |
QCOMPARE(reader.name().toString(), QLatin1String("bar")); |
|
342 |
||
343 |
QPicture picture; |
|
344 |
QPainter painter(&picture); |
|
345 |
QSvgRenderer renderer(&reader); |
|
346 |
renderer.render(&painter, QLatin1String("foo")); |
|
347 |
painter.end(); |
|
348 |
QCOMPARE(picture.boundingRect(), QRect(0, 0, 100, 100)); |
|
349 |
||
350 |
QCOMPARE(reader.readNext(), QXmlStreamReader::EndElement); |
|
351 |
QCOMPARE(reader.name().toString(), QLatin1String("bar")); |
|
352 |
QCOMPARE(reader.readNext(), QXmlStreamReader::EndDocument); |
|
353 |
||
354 |
QVERIFY(reader.atEnd()); |
|
355 |
QVERIFY(!reader.hasError()); |
|
356 |
} |
|
357 |
||
358 |
void tst_QSvgRenderer::stylePropagation() const |
|
359 |
{ |
|
360 |
QByteArray data("<svg>" |
|
361 |
"<g id='foo' style='fill:#ffff00;'>" |
|
362 |
"<g id='bar' style='fill:#ff00ff;'>" |
|
363 |
"<g id='baz' style='fill:#00ffff;'>" |
|
364 |
"<rect id='alpha' x='0' y='0' width='100' height='100'/>" |
|
365 |
"</g>" |
|
366 |
"<rect id='beta' x='100' y='0' width='100' height='100'/>" |
|
367 |
"</g>" |
|
368 |
"<rect id='gamma' x='0' y='100' width='100' height='100'/>" |
|
369 |
"</g>" |
|
370 |
"<rect id='delta' x='100' y='100' width='100' height='100'/>" |
|
371 |
"</svg>"); // alpha=cyan, beta=magenta, gamma=yellow, delta=black |
|
372 |
||
373 |
QImage image1(200, 200, QImage::Format_RGB32); |
|
374 |
QImage image2(200, 200, QImage::Format_RGB32); |
|
375 |
QImage image3(200, 200, QImage::Format_RGB32); |
|
376 |
QPainter painter; |
|
377 |
QSvgRenderer renderer(data); |
|
378 |
QLatin1String parts[4] = {QLatin1String("alpha"), QLatin1String("beta"), QLatin1String("gamma"), QLatin1String("delta")}; |
|
379 |
||
380 |
QVERIFY(painter.begin(&image1)); |
|
381 |
for (int i = 0; i < 4; ++i) |
|
382 |
renderer.render(&painter, parts[i], QRectF(renderer.boundsOnElement(parts[i]))); |
|
383 |
painter.end(); |
|
384 |
||
385 |
QVERIFY(painter.begin(&image2)); |
|
386 |
renderer.render(&painter, renderer.viewBoxF()); |
|
387 |
painter.end(); |
|
388 |
||
389 |
QVERIFY(painter.begin(&image3)); |
|
390 |
painter.setPen(Qt::NoPen); |
|
391 |
painter.setBrush(QBrush(Qt::cyan)); |
|
392 |
painter.drawRect(0, 0, 100, 100); |
|
393 |
painter.setBrush(QBrush(Qt::magenta)); |
|
394 |
painter.drawRect(100, 0, 100, 100); |
|
395 |
painter.setBrush(QBrush(Qt::yellow)); |
|
396 |
painter.drawRect(0, 100, 100, 100); |
|
397 |
painter.setBrush(QBrush(Qt::black)); |
|
398 |
painter.drawRect(100, 100, 100, 100); |
|
399 |
painter.end(); |
|
400 |
||
401 |
QCOMPARE(image1, image2); |
|
402 |
QCOMPARE(image1, image3); |
|
403 |
} |
|
404 |
||
405 |
static qreal transformNorm(const QTransform &m) |
|
406 |
{ |
|
407 |
return qSqrt(m.m11() * m.m11() |
|
408 |
+ m.m12() * m.m12() |
|
409 |
+ m.m13() * m.m13() |
|
410 |
+ m.m21() * m.m21() |
|
411 |
+ m.m22() * m.m22() |
|
412 |
+ m.m23() * m.m23() |
|
413 |
+ m.m31() * m.m31() |
|
414 |
+ m.m32() * m.m32() |
|
415 |
+ m.m33() * m.m33()); |
|
416 |
} |
|
417 |
||
418 |
static bool diffIsSmallEnough(double diff, double norm) |
|
419 |
{ |
|
420 |
return diff <= 1e-12 * norm; |
|
421 |
} |
|
422 |
||
423 |
static bool diffIsSmallEnough(float diff, float norm) |
|
424 |
{ |
|
425 |
return diff <= 1e-5 * norm; |
|
426 |
} |
|
427 |
||
428 |
static void compareTransforms(const QTransform &m1, const QTransform &m2) |
|
429 |
{ |
|
430 |
qreal norm1 = transformNorm(m1); |
|
431 |
qreal norm2 = transformNorm(m2); |
|
432 |
qreal diffNorm = transformNorm(QTransform(m1.m11() - m2.m11(), |
|
433 |
m1.m12() - m2.m12(), |
|
434 |
m1.m13() - m2.m13(), |
|
435 |
m1.m21() - m2.m21(), |
|
436 |
m1.m22() - m2.m22(), |
|
437 |
m1.m23() - m2.m23(), |
|
438 |
m1.m31() - m2.m31(), |
|
439 |
m1.m32() - m2.m32(), |
|
440 |
m1.m33() - m2.m33())); |
|
441 |
QVERIFY(diffIsSmallEnough(diffNorm, qMin(norm1, norm2))); |
|
442 |
} |
|
443 |
||
444 |
void tst_QSvgRenderer::matrixForElement() const |
|
445 |
{ |
|
446 |
QByteArray data("<svg>" |
|
447 |
"<g id='ichi' transform='translate(-3,1)'>" |
|
448 |
"<g id='ni' transform='rotate(45)'>" |
|
449 |
"<g id='san' transform='scale(4,2)'>" |
|
450 |
"<g id='yon' transform='matrix(1,2,3,4,5,6)'>" |
|
451 |
"<rect id='firkant' x='-1' y='-1' width='2' height='2'/>" |
|
452 |
"</g>" |
|
453 |
"</g>" |
|
454 |
"</g>" |
|
455 |
"</g>" |
|
456 |
"</svg>"); |
|
457 |
||
458 |
QImage image(13, 37, QImage::Format_RGB32); |
|
459 |
QPainter painter(&image); |
|
460 |
QSvgRenderer renderer(data); |
|
461 |
||
462 |
compareTransforms(QTransform(painter.worldMatrix()), QTransform(renderer.matrixForElement(QLatin1String("ichi")))); |
|
463 |
painter.translate(-3, 1); |
|
464 |
compareTransforms(QTransform(painter.worldMatrix()), QTransform(renderer.matrixForElement(QLatin1String("ni")))); |
|
465 |
painter.rotate(45); |
|
466 |
compareTransforms(QTransform(painter.worldMatrix()), QTransform(renderer.matrixForElement(QLatin1String("san")))); |
|
467 |
painter.scale(4, 2); |
|
468 |
compareTransforms(QTransform(painter.worldMatrix()), QTransform(renderer.matrixForElement(QLatin1String("yon")))); |
|
469 |
painter.setWorldMatrix(QMatrix(1, 2, 3, 4, 5, 6), true); |
|
470 |
compareTransforms(QTransform(painter.worldMatrix()), QTransform(renderer.matrixForElement(QLatin1String("firkant")))); |
|
471 |
} |
|
472 |
||
473 |
void tst_QSvgRenderer::gradientStops() const |
|
474 |
{ |
|
475 |
{ |
|
476 |
QByteArray data("<svg>" |
|
477 |
"<defs>" |
|
478 |
"<linearGradient id=\"gradient\">" |
|
479 |
"</linearGradient>" |
|
480 |
"</defs>" |
|
481 |
"<rect fill=\"url(#gradient)\" height=\"64\" width=\"64\" x=\"0\" y=\"0\"/>" |
|
482 |
"</svg>"); |
|
483 |
QSvgRenderer renderer(data); |
|
484 |
||
485 |
QImage image(64, 64, QImage::Format_ARGB32_Premultiplied), refImage(64, 64, QImage::Format_ARGB32_Premultiplied); |
|
486 |
image.fill(0x87654321); |
|
487 |
refImage.fill(0x87654321); |
|
488 |
||
489 |
QPainter painter(&image); |
|
490 |
renderer.render(&painter); |
|
491 |
QCOMPARE(image, refImage); |
|
492 |
} |
|
493 |
||
494 |
{ |
|
495 |
QByteArray data("<svg>" |
|
496 |
"<defs>" |
|
497 |
"<linearGradient id=\"gradient\">" |
|
498 |
"<stop offset=\"1\" stop-color=\"cyan\"/>" |
|
499 |
"</linearGradient>" |
|
500 |
"</defs>" |
|
501 |
"<rect fill=\"url(#gradient)\" height=\"64\" width=\"64\" x=\"0\" y=\"0\"/>" |
|
502 |
"</svg>"); |
|
503 |
QSvgRenderer renderer(data); |
|
504 |
||
505 |
QImage image(64, 64, QImage::Format_ARGB32_Premultiplied), refImage(64, 64, QImage::Format_ARGB32_Premultiplied); |
|
506 |
refImage.fill(0xff00ffff); |
|
507 |
||
508 |
QPainter painter(&image); |
|
509 |
renderer.render(&painter); |
|
510 |
QCOMPARE(image, refImage); |
|
511 |
} |
|
512 |
||
513 |
{ |
|
514 |
QByteArray data("<svg>" |
|
515 |
"<defs>" |
|
516 |
"<linearGradient id=\"gradient\">" |
|
517 |
"<stop offset=\"0\" stop-color=\"red\"/>" |
|
518 |
"<stop offset=\"0\" stop-color=\"cyan\"/>" |
|
519 |
"<stop offset=\"0.5\" stop-color=\"cyan\"/>" |
|
520 |
"<stop offset=\"0.5\" stop-color=\"magenta\"/>" |
|
521 |
"<stop offset=\"0.5\" stop-color=\"yellow\"/>" |
|
522 |
"<stop offset=\"1\" stop-color=\"yellow\"/>" |
|
523 |
"<stop offset=\"1\" stop-color=\"blue\"/>" |
|
524 |
"</linearGradient>" |
|
525 |
"</defs>" |
|
526 |
"<rect fill=\"url(#gradient)\" height=\"64\" width=\"64\" x=\"0\" y=\"0\"/>" |
|
527 |
"</svg>"); |
|
528 |
QSvgRenderer renderer(data); |
|
529 |
||
530 |
QImage image(64, 64, QImage::Format_ARGB32_Premultiplied), refImage(64, 64, QImage::Format_ARGB32_Premultiplied); |
|
531 |
||
532 |
QPainter painter; |
|
533 |
painter.begin(&refImage); |
|
534 |
painter.fillRect(QRectF(0, 0, 32, 64), Qt::cyan); |
|
535 |
painter.fillRect(QRectF(32, 0, 32, 64), Qt::yellow); |
|
536 |
painter.end(); |
|
537 |
||
538 |
painter.begin(&image); |
|
539 |
renderer.render(&painter); |
|
540 |
painter.end(); |
|
541 |
||
542 |
QCOMPARE(image, refImage); |
|
543 |
} |
|
544 |
} |
|
545 |
||
546 |
void tst_QSvgRenderer::gradientRefs() |
|
547 |
{ |
|
548 |
const char *svgs[] = { |
|
549 |
"<svg>" |
|
550 |
"<defs>" |
|
551 |
"<linearGradient id=\"gradient\">" |
|
552 |
"<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" |
|
553 |
"<stop offset=\"1\" stop-color=\"blue\"/>" |
|
554 |
"</linearGradient>" |
|
555 |
"</defs>" |
|
556 |
"<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" |
|
557 |
"</svg>", |
|
558 |
"<svg>" |
|
559 |
"<defs>" |
|
560 |
"<linearGradient id=\"gradient\" xlink:href=\"#gradient0\">" |
|
561 |
"</linearGradient>" |
|
562 |
"<linearGradient id=\"gradient0\">" |
|
563 |
"<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" |
|
564 |
"<stop offset=\"1\" stop-color=\"blue\"/>" |
|
565 |
"</linearGradient>" |
|
566 |
"</defs>" |
|
567 |
"<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" |
|
568 |
"</svg>", |
|
569 |
"<svg>" |
|
570 |
"<defs>" |
|
571 |
"<linearGradient id=\"gradient0\">" |
|
572 |
"<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" |
|
573 |
"<stop offset=\"1\" stop-color=\"blue\"/>" |
|
574 |
"</linearGradient>" |
|
575 |
"<linearGradient id=\"gradient\" xlink:href=\"#gradient0\">" |
|
576 |
"</linearGradient>" |
|
577 |
"</defs>" |
|
578 |
"<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" |
|
579 |
"</svg>", |
|
580 |
"<svg>" |
|
581 |
"<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" |
|
582 |
"<defs>" |
|
583 |
"<linearGradient id=\"gradient0\">" |
|
584 |
"<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" |
|
585 |
"<stop offset=\"1\" stop-color=\"blue\"/>" |
|
586 |
"</linearGradient>" |
|
587 |
"<linearGradient id=\"gradient\" xlink:href=\"#gradient0\">" |
|
588 |
"</linearGradient>" |
|
589 |
"</defs>" |
|
590 |
"</svg>" |
|
591 |
}; |
|
592 |
for (int i = 0 ; i < sizeof(svgs) / sizeof(svgs[0]) ; ++i) |
|
593 |
{ |
|
594 |
QByteArray data = svgs[i]; |
|
595 |
QSvgRenderer renderer(data); |
|
596 |
||
597 |
QImage image(256, 8, QImage::Format_ARGB32_Premultiplied); |
|
598 |
image.fill(0); |
|
599 |
||
600 |
QPainter painter(&image); |
|
601 |
renderer.render(&painter); |
|
602 |
||
603 |
const QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(3)); |
|
604 |
QRgb left = line[0]; // transparent black |
|
605 |
QRgb mid = line[127]; // semi transparent magenta |
|
606 |
QRgb right = line[255]; // opaque blue |
|
607 |
||
608 |
QVERIFY((qAlpha(left) < 3) && (qRed(left) < 3) && (qGreen(left) == 0) && (qBlue(left) < 3)); |
|
609 |
QVERIFY((qAbs(qAlpha(mid) - 127) < 3) && (qAbs(qRed(mid) - 63) < 4) && (qGreen(mid) == 0) && (qAbs(qBlue(mid) - 63) < 4)); |
|
610 |
QVERIFY((qAlpha(right) > 253) && (qRed(right) < 3) && (qGreen(right) == 0) && (qBlue(right) > 251)); |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
||
615 |
#ifndef QT_NO_COMPRESS |
|
616 |
void tst_QSvgRenderer::testGzLoading() |
|
617 |
{ |
|
618 |
QSvgRenderer renderer(QLatin1String("heart.svgz")); |
|
619 |
QVERIFY(renderer.isValid()); |
|
620 |
||
621 |
QSvgRenderer resourceRenderer(QLatin1String(":/heart.svgz")); |
|
622 |
QVERIFY(resourceRenderer.isValid()); |
|
623 |
||
624 |
QFile largeFileGz("large.svgz"); |
|
625 |
largeFileGz.open(QIODevice::ReadOnly); |
|
626 |
QByteArray data = largeFileGz.readAll(); |
|
627 |
QSvgRenderer autoDetectGzData(data); |
|
628 |
QVERIFY(autoDetectGzData.isValid()); |
|
629 |
} |
|
630 |
||
631 |
#ifdef QT_BUILD_INTERNAL |
|
632 |
QT_BEGIN_NAMESPACE |
|
633 |
QByteArray qt_inflateGZipDataFrom(QIODevice *device); |
|
634 |
QT_END_NAMESPACE |
|
635 |
#endif |
|
636 |
||
637 |
void tst_QSvgRenderer::testGzHelper_data() |
|
638 |
{ |
|
639 |
QTest::addColumn<QByteArray>("in"); |
|
640 |
QTest::addColumn<QByteArray>("out"); |
|
641 |
||
642 |
QTest::newRow("empty") << QByteArray() << QByteArray(); |
|
643 |
QTest::newRow("small") << QByteArray::fromHex(QByteArray("1f8b08005819934800034b" |
|
644 |
"cbcfe70200a865327e04000000")) << QByteArray("foo\n"); |
|
645 |
||
646 |
QFile largeFileGz("large.svgz"); |
|
647 |
largeFileGz.open(QIODevice::ReadOnly); |
|
648 |
QFile largeFile("large.svg"); |
|
649 |
largeFile.open(QIODevice::ReadOnly); |
|
650 |
QTest::newRow("large") << largeFileGz.readAll() << largeFile.readAll(); |
|
651 |
||
652 |
QTest::newRow("zeroes") << QByteArray::fromHex(QByteArray("1f8b0800131f9348000333" |
|
653 |
"301805a360148c54c00500d266708601040000")) << QByteArray(1024, '0').append('\n'); |
|
654 |
||
655 |
QTest::newRow("twoMembers") << QByteArray::fromHex(QByteArray("1f8b08001c2a934800034b" |
|
656 |
"cbcfe70200a865327e040000001f8b08001c2a934800034b4a2ce20200e9b3a20404000000")) |
|
657 |
<< QByteArray("foo\nbar\n"); |
|
658 |
||
659 |
// We should still get data of the first member if subsequent members are corrupt |
|
660 |
QTest::newRow("corruptedSecondMember") << QByteArray::fromHex(QByteArray("1f8b08001c2a934800034b" |
|
661 |
"cbcfe70200a865327e040000001f8c08001c2a934800034b4a2ce20200e9b3a20404000000")) |
|
662 |
<< QByteArray("foo\n"); |
|
663 |
||
664 |
} |
|
665 |
||
666 |
void tst_QSvgRenderer::testGzHelper() |
|
667 |
{ |
|
668 |
#ifdef QT_BUILD_INTERNAL |
|
669 |
QFETCH(QByteArray, in); |
|
670 |
QFETCH(QByteArray, out); |
|
671 |
||
672 |
QBuffer buffer(&in); |
|
673 |
buffer.open(QIODevice::ReadOnly); |
|
674 |
QVERIFY(buffer.isReadable()); |
|
675 |
QByteArray result = qt_inflateGZipDataFrom(&buffer); |
|
676 |
QCOMPARE(result, out); |
|
677 |
#endif |
|
678 |
} |
|
679 |
#endif |
|
680 |
||
681 |
void tst_QSvgRenderer::fillRule() |
|
682 |
{ |
|
683 |
static const char *svgs[] = { |
|
684 |
// Paths |
|
685 |
// Default fill-rule (nonzero) |
|
686 |
"<svg>" |
|
687 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
688 |
" <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" />" |
|
689 |
"</svg>", |
|
690 |
// nonzero |
|
691 |
"<svg>" |
|
692 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
693 |
" <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"nonzero\" />" |
|
694 |
"</svg>", |
|
695 |
// evenodd |
|
696 |
"<svg>" |
|
697 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
698 |
" <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"evenodd\" />" |
|
699 |
"</svg>", |
|
700 |
||
701 |
// Polygons |
|
702 |
// Default fill-rule (nonzero) |
|
703 |
"<svg>" |
|
704 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
705 |
" <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" />" |
|
706 |
"</svg>", |
|
707 |
// nonzero |
|
708 |
"<svg>" |
|
709 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
710 |
" <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"nonzero\" />" |
|
711 |
"</svg>", |
|
712 |
// evenodd |
|
713 |
"<svg>" |
|
714 |
" <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" |
|
715 |
" <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"evenodd\" />" |
|
716 |
"</svg>" |
|
717 |
}; |
|
718 |
||
719 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
720 |
QImage refImageNonZero(30, 30, QImage::Format_ARGB32_Premultiplied); |
|
721 |
QImage refImageEvenOdd(30, 30, QImage::Format_ARGB32_Premultiplied); |
|
722 |
refImageNonZero.fill(0xffff0000); |
|
723 |
refImageEvenOdd.fill(0xffff0000); |
|
724 |
QPainter p; |
|
725 |
p.begin(&refImageNonZero); |
|
726 |
p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); |
|
727 |
p.end(); |
|
728 |
p.begin(&refImageEvenOdd); |
|
729 |
p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); |
|
730 |
p.fillRect(QRectF(10, 10, 10, 10), Qt::blue); |
|
731 |
p.end(); |
|
732 |
||
733 |
for (int i = 0; i < COUNT; ++i) { |
|
734 |
QByteArray data(svgs[i]); |
|
735 |
QSvgRenderer renderer(data); |
|
736 |
QImage image(30, 30, QImage::Format_ARGB32_Premultiplied); |
|
737 |
image.fill(0); |
|
738 |
p.begin(&image); |
|
739 |
renderer.render(&p); |
|
740 |
p.end(); |
|
741 |
QCOMPARE(image, i % 3 == 2 ? refImageEvenOdd : refImageNonZero); |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
static void opacity_drawSvgAndVerify(const QByteArray &data) |
|
746 |
{ |
|
747 |
QSvgRenderer renderer(data); |
|
748 |
QVERIFY(renderer.isValid()); |
|
749 |
QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); |
|
750 |
image.fill(0xffff00ff); |
|
751 |
QPainter painter(&image); |
|
752 |
renderer.render(&painter); |
|
753 |
painter.end(); |
|
754 |
QCOMPARE(image.pixel(5, 5), 0xff7f7f7f); |
|
755 |
} |
|
756 |
||
757 |
void tst_QSvgRenderer::opacity() |
|
758 |
{ |
|
759 |
static const char *opacities[] = {"-1.4641", "0", "0.5", "1", "1.337"}; |
|
760 |
static const char *firstColors[] = {"#7f7f7f", "#7f7f7f", "#402051", "blue", "#123456"}; |
|
761 |
static const char *secondColors[] = {"red", "#bad", "#bedead", "#7f7f7f", "#7f7f7f"}; |
|
762 |
||
763 |
// Fill-opacity |
|
764 |
for (int i = 0; i < 5; ++i) { |
|
765 |
QByteArray data("<svg><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); |
|
766 |
data.append(firstColors[i]); |
|
767 |
data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); |
|
768 |
data.append(secondColors[i]); |
|
769 |
data.append("\" fill-opacity=\""); |
|
770 |
data.append(opacities[i]); |
|
771 |
data.append("\"/></svg>"); |
|
772 |
opacity_drawSvgAndVerify(data); |
|
773 |
} |
|
774 |
// Stroke-opacity |
|
775 |
for (int i = 0; i < 5; ++i) { |
|
776 |
QByteArray data("<svg viewBox=\"0 0 10 10\"><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); |
|
777 |
data.append(firstColors[i]); |
|
778 |
data.append("\" stroke-width=\"10\"/><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\""); |
|
779 |
data.append(secondColors[i]); |
|
780 |
data.append("\" stroke-width=\"10\" stroke-opacity=\""); |
|
781 |
data.append(opacities[i]); |
|
782 |
data.append("\"/></svg>"); |
|
783 |
opacity_drawSvgAndVerify(data); |
|
784 |
} |
|
785 |
// As gradients: |
|
786 |
// Fill-opacity |
|
787 |
for (int i = 0; i < 5; ++i) { |
|
788 |
QByteArray data("<svg><defs><linearGradient id=\"gradient\"><stop offset=\"0\" stop-color=\""); |
|
789 |
data.append(secondColors[i]); |
|
790 |
data.append("\"/><stop offset=\"1\" stop-color=\""); |
|
791 |
data.append(secondColors[i]); |
|
792 |
data.append("\"/></linearGradient></defs><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); |
|
793 |
data.append(firstColors[i]); |
|
794 |
data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"url(#gradient)\" fill-opacity=\""); |
|
795 |
data.append(opacities[i]); |
|
796 |
data.append("\"/></svg>"); |
|
797 |
opacity_drawSvgAndVerify(data); |
|
798 |
} |
|
799 |
// Stroke-opacity |
|
800 |
for (int i = 0; i < 5; ++i) { |
|
801 |
QByteArray data("<svg viewBox=\"0 0 10 10\"><defs><linearGradient id=\"grad\"><stop offset=\"0\" stop-color=\""); |
|
802 |
data.append(secondColors[i]); |
|
803 |
data.append("\"/><stop offset=\"1\" stop-color=\""); |
|
804 |
data.append(secondColors[i]); |
|
805 |
data.append("\"/></linearGradient></defs><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); |
|
806 |
data.append(firstColors[i]); |
|
807 |
data.append("\" stroke-width=\"10\" /><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\"url(#grad)\" stroke-width=\"10\" stroke-opacity=\""); |
|
808 |
data.append(opacities[i]); |
|
809 |
data.append("\" /></svg>"); |
|
810 |
opacity_drawSvgAndVerify(data); |
|
811 |
} |
|
812 |
} |
|
813 |
||
814 |
void tst_QSvgRenderer::paths() |
|
815 |
{ |
|
816 |
static const char *svgs[] = { |
|
817 |
// Absolute coordinates, explicit commands. |
|
818 |
"<svg>" |
|
819 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
820 |
" <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\"/>" |
|
821 |
"</svg>", |
|
822 |
// Absolute coordinates, implicit commands. |
|
823 |
"<svg>" |
|
824 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
825 |
" <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\" />" |
|
826 |
"</svg>", |
|
827 |
// Relative coordinates, explicit commands. |
|
828 |
"<svg>" |
|
829 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
830 |
" <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\" />" |
|
831 |
"</svg>", |
|
832 |
// Relative coordinates, implicit commands. |
|
833 |
"<svg>" |
|
834 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
835 |
" <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\" />" |
|
836 |
"</svg>", |
|
837 |
// Absolute coordinates, explicit commands, minimal whitespace. |
|
838 |
"<svg>" |
|
839 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
840 |
" <path d=\"m50 0v50h-50q0-25 25-25t25-25c-25 0 0 50-25 50s0-50-25-50z\" fill=\"red\" fill-rule=\"evenodd\" />" |
|
841 |
"</svg>", |
|
842 |
// Absolute coordinates, explicit commands, extra whitespace. |
|
843 |
"<svg>" |
|
844 |
" <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" |
|
845 |
" <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\" />" |
|
846 |
"</svg>" |
|
847 |
}; |
|
848 |
||
849 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
850 |
QImage images[COUNT]; |
|
851 |
QPainter p; |
|
852 |
||
853 |
for (int i = 0; i < COUNT; ++i) { |
|
854 |
QByteArray data(svgs[i]); |
|
855 |
QSvgRenderer renderer(data); |
|
856 |
QVERIFY(renderer.isValid()); |
|
857 |
images[i] = QImage(50, 50, QImage::Format_ARGB32_Premultiplied); |
|
858 |
images[i].fill(0); |
|
859 |
p.begin(&images[i]); |
|
860 |
renderer.render(&p); |
|
861 |
p.end(); |
|
862 |
if (i != 0) { |
|
863 |
QCOMPARE(images[i], images[0]); |
|
864 |
} |
|
865 |
} |
|
866 |
} |
|
867 |
||
868 |
void tst_QSvgRenderer::displayMode() |
|
869 |
{ |
|
870 |
static const char *svgs[] = { |
|
871 |
// All visible. |
|
872 |
"<svg>" |
|
873 |
" <g>" |
|
874 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" |
|
875 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" |
|
876 |
" </g>" |
|
877 |
"</svg>", |
|
878 |
// Don't display svg element. |
|
879 |
"<svg display=\"none\">" |
|
880 |
" <g>" |
|
881 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" |
|
882 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" |
|
883 |
" </g>" |
|
884 |
"</svg>", |
|
885 |
// Don't display g element. |
|
886 |
"<svg>" |
|
887 |
" <g display=\"none\">" |
|
888 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" |
|
889 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" |
|
890 |
" </g>" |
|
891 |
"</svg>", |
|
892 |
// Don't display first rect element. |
|
893 |
"<svg>" |
|
894 |
" <g>" |
|
895 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"none\" />" |
|
896 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" |
|
897 |
" </g>" |
|
898 |
"</svg>", |
|
899 |
// Don't display second rect element. |
|
900 |
"<svg>" |
|
901 |
" <g>" |
|
902 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" |
|
903 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"none\" />" |
|
904 |
" </g>" |
|
905 |
"</svg>", |
|
906 |
// Don't display svg element, but set display mode to "inline" for other elements. |
|
907 |
"<svg display=\"none\">" |
|
908 |
" <g display=\"inline\">" |
|
909 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"inline\" />" |
|
910 |
" <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"inline\" />" |
|
911 |
" </g>" |
|
912 |
"</svg>" |
|
913 |
}; |
|
914 |
||
915 |
QRgb expectedColors[] = {0xff0000ff, 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00}; |
|
916 |
||
917 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
918 |
QPainter p; |
|
919 |
||
920 |
for (int i = 0; i < COUNT; ++i) { |
|
921 |
QByteArray data(svgs[i]); |
|
922 |
QSvgRenderer renderer(data); |
|
923 |
QVERIFY(renderer.isValid()); |
|
924 |
QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); |
|
925 |
image.fill(0xff00ff00); |
|
926 |
p.begin(&image); |
|
927 |
renderer.render(&p); |
|
928 |
p.end(); |
|
929 |
QCOMPARE(image.pixel(5, 5), expectedColors[i]); |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
void tst_QSvgRenderer::strokeInherit() |
|
934 |
{ |
|
935 |
static const char *svgs[] = { |
|
936 |
// Reference. |
|
937 |
"<svg viewBox=\"0 0 200 30\">" |
|
938 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
939 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
940 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
941 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\"/>" |
|
942 |
" </g>" |
|
943 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
944 |
" <polyline fill=\"none\" points=\"10 25 80 25\"/>" |
|
945 |
" </g>" |
|
946 |
"</svg>", |
|
947 |
// stroke |
|
948 |
"<svg viewBox=\"0 0 200 30\">" |
|
949 |
" <g stroke=\"none\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
950 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
951 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
952 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke=\"blue\"/>" |
|
953 |
" </g>" |
|
954 |
" <g stroke=\"yellow\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
955 |
" <polyline fill=\"none\" points=\"10 25 80 25\" stroke=\"green\"/>" |
|
956 |
" </g>" |
|
957 |
"</svg>", |
|
958 |
// stroke-width |
|
959 |
"<svg viewBox=\"0 0 200 30\">" |
|
960 |
" <g stroke=\"blue\" stroke-width=\"0\" stroke-linecap=\"butt\"" |
|
961 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
962 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
963 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-width=\"20\"/>" |
|
964 |
" </g>" |
|
965 |
" <g stroke=\"green\" stroke-width=\"10\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
966 |
" <polyline fill=\"none\" points=\"10 25 80 25\" stroke-width=\"0\"/>" |
|
967 |
" </g>" |
|
968 |
"</svg>", |
|
969 |
// stroke-linecap |
|
970 |
"<svg viewBox=\"0 0 200 30\">" |
|
971 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"round\"" |
|
972 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
973 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
974 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-linecap=\"butt\"/>" |
|
975 |
" </g>" |
|
976 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
977 |
" <polyline fill=\"none\" points=\"10 25 80 25\"/>" |
|
978 |
" </g>" |
|
979 |
"</svg>", |
|
980 |
// stroke-linejoin |
|
981 |
"<svg viewBox=\"0 0 200 30\">" |
|
982 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
983 |
" stroke-linejoin=\"round\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
984 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
985 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-linejoin=\"miter\"/>" |
|
986 |
" </g>" |
|
987 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
988 |
" <polyline fill=\"none\" points=\"10 25 80 25\"/>" |
|
989 |
" </g>" |
|
990 |
"</svg>", |
|
991 |
// stroke-miterlimit |
|
992 |
"<svg viewBox=\"0 0 200 30\">" |
|
993 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
994 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"2\" stroke-dasharray=\"20,10\"" |
|
995 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
996 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-miterlimit=\"1\"/>" |
|
997 |
" </g>" |
|
998 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
999 |
" <polyline fill=\"none\" points=\"10 25 80 25\"/>" |
|
1000 |
" </g>" |
|
1001 |
"</svg>", |
|
1002 |
// stroke-dasharray |
|
1003 |
"<svg viewBox=\"0 0 200 30\">" |
|
1004 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
1005 |
" 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\"" |
|
1006 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0.5\">" |
|
1007 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-dasharray=\"20,10\"/>" |
|
1008 |
" </g>" |
|
1009 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"none\" stroke-dashoffset=\"4.5\">" |
|
1010 |
" <polyline fill=\"none\" points=\"10 25 80 25\" stroke-dasharray=\"3,3,1\"/>" |
|
1011 |
" </g>" |
|
1012 |
"</svg>", |
|
1013 |
// stroke-dashoffset |
|
1014 |
"<svg viewBox=\"0 0 200 30\">" |
|
1015 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
1016 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
1017 |
" stroke-dashoffset=\"0\" stroke-opacity=\"0.5\">" |
|
1018 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-dashoffset=\"10\"/>" |
|
1019 |
" </g>" |
|
1020 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"0\">" |
|
1021 |
" <polyline fill=\"none\" points=\"10 25 80 25\" stroke-dashoffset=\"4.5\"/>" |
|
1022 |
" </g>" |
|
1023 |
"</svg>", |
|
1024 |
// stroke-opacity |
|
1025 |
"<svg viewBox=\"0 0 200 30\">" |
|
1026 |
" <g stroke=\"blue\" stroke-width=\"20\" stroke-linecap=\"butt\"" |
|
1027 |
" stroke-linejoin=\"miter\" stroke-miterlimit=\"1\" stroke-dasharray=\"20,10\"" |
|
1028 |
" stroke-dashoffset=\"10\" stroke-opacity=\"0\">" |
|
1029 |
" <polyline fill=\"none\" points=\"10 10 100 10 100 20 190 20\" stroke-opacity=\"0.5\"/>" |
|
1030 |
" </g>" |
|
1031 |
" <g stroke=\"green\" stroke-width=\"0\" stroke-dasharray=\"3,3,1\" stroke-dashoffset=\"4.5\">" |
|
1032 |
" <polyline fill=\"none\" points=\"10 25 80 25\"/>" |
|
1033 |
" </g>" |
|
1034 |
"</svg>" |
|
1035 |
}; |
|
1036 |
||
1037 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
1038 |
QImage images[COUNT]; |
|
1039 |
QPainter p; |
|
1040 |
||
1041 |
for (int i = 0; i < COUNT; ++i) { |
|
1042 |
QByteArray data(svgs[i]); |
|
1043 |
QSvgRenderer renderer(data); |
|
1044 |
QVERIFY(renderer.isValid()); |
|
1045 |
images[i] = QImage(200, 30, QImage::Format_ARGB32_Premultiplied); |
|
1046 |
images[i].fill(-1); |
|
1047 |
p.begin(&images[i]); |
|
1048 |
renderer.render(&p); |
|
1049 |
p.end(); |
|
1050 |
if (i != 0) { |
|
1051 |
QCOMPARE(images[0], images[i]); |
|
1052 |
} |
|
1053 |
} |
|
1054 |
} |
|
1055 |
||
1056 |
void tst_QSvgRenderer::testFillInheritance() |
|
1057 |
{ |
|
1058 |
static const char *svgs[] = { |
|
1059 |
//reference |
|
1060 |
"<svg viewBox = \"0 0 200 200\">" |
|
1061 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" |
|
1062 |
"</svg>", |
|
1063 |
"<svg viewBox = \"0 0 200 200\">" |
|
1064 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" |
|
1065 |
" <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" |
|
1066 |
"</svg>", |
|
1067 |
"<svg viewBox = \"0 0 200 200\">" |
|
1068 |
" <g fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\">" |
|
1069 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\"/>" |
|
1070 |
" </g>" |
|
1071 |
" <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" |
|
1072 |
"</svg>", |
|
1073 |
"<svg viewBox = \"0 0 200 200\">" |
|
1074 |
" <g fill = \"green\" fill-rule = \"nonzero\">" |
|
1075 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\" fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" |
|
1076 |
" </g>" |
|
1077 |
" <g fill-opacity = \"0.8\" fill = \"red\">" |
|
1078 |
" <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\" fill-opacity = \"0\"/>" |
|
1079 |
" </g>" |
|
1080 |
"</svg>", |
|
1081 |
"<svg viewBox = \"0 0 200 200\">" |
|
1082 |
" <g fill = \"red\" >" |
|
1083 |
" <g fill-opacity = \"0.5\">" |
|
1084 |
" <g fill-rule = \"evenodd\">" |
|
1085 |
" <g>" |
|
1086 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\"/>" |
|
1087 |
" </g>" |
|
1088 |
" </g>" |
|
1089 |
" </g>" |
|
1090 |
" </g>" |
|
1091 |
" <g fill-opacity = \"0.8\" >" |
|
1092 |
" <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"none\"/>" |
|
1093 |
" </g>" |
|
1094 |
"</svg>", |
|
1095 |
"<svg viewBox = \"0 0 200 200\">" |
|
1096 |
" <g fill = \"none\" fill-opacity = \"0\">" |
|
1097 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\" stroke = \"blue\" fill = \"red\" fill-opacity = \"0.5\" fill-rule = \"evenodd\"/>" |
|
1098 |
" </g>" |
|
1099 |
" <g fill-opacity = \"0\" >" |
|
1100 |
" <rect x = \"40\" y = \"40\" width = \"70\" height =\"20\" fill = \"green\"/>" |
|
1101 |
" </g>" |
|
1102 |
"</svg>" |
|
1103 |
}; |
|
1104 |
||
1105 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
1106 |
QImage images[COUNT]; |
|
1107 |
QPainter p; |
|
1108 |
||
1109 |
for (int i = 0; i < COUNT; ++i) { |
|
1110 |
QByteArray data(svgs[i]); |
|
1111 |
QSvgRenderer renderer(data); |
|
1112 |
QVERIFY(renderer.isValid()); |
|
1113 |
images[i] = QImage(200, 200, QImage::Format_ARGB32_Premultiplied); |
|
1114 |
images[i].fill(-1); |
|
1115 |
p.begin(&images[i]); |
|
1116 |
renderer.render(&p); |
|
1117 |
p.end(); |
|
1118 |
if (i != 0) { |
|
1119 |
QCOMPARE(images[0], images[i]); |
|
1120 |
} |
|
1121 |
} |
|
1122 |
} |
|
1123 |
void tst_QSvgRenderer::testStopOffsetOpacity() |
|
1124 |
{ |
|
1125 |
static const char *svgs[] = { |
|
1126 |
//reference |
|
1127 |
"<svg viewBox=\"0 0 64 64\">" |
|
1128 |
"<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" |
|
1129 |
"<stop offset=\"0.0\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" |
|
1130 |
"<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"1\"/>" |
|
1131 |
"<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"1\"/>" |
|
1132 |
"</radialGradient>" |
|
1133 |
"<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" |
|
1134 |
"<stop offset=\"0.0\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" |
|
1135 |
"<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"1\"/>" |
|
1136 |
"<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"1\"/>" |
|
1137 |
"</radialGradient>" |
|
1138 |
"<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" |
|
1139 |
"<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" |
|
1140 |
"</svg>", |
|
1141 |
//Stop Offset |
|
1142 |
"<svg viewBox=\"0 0 64 64\">" |
|
1143 |
"<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" |
|
1144 |
"<stop offset=\"abc\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" |
|
1145 |
"<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"1\"/>" |
|
1146 |
"<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"1\"/>" |
|
1147 |
"</radialGradient>" |
|
1148 |
"<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" |
|
1149 |
"<stop offset=\"-3.bc\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" |
|
1150 |
"<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"1\"/>" |
|
1151 |
"<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"1\"/>" |
|
1152 |
"</radialGradient>" |
|
1153 |
"<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" |
|
1154 |
"<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" |
|
1155 |
"</svg>", |
|
1156 |
//Stop Opacity |
|
1157 |
"<svg viewBox=\"0 0 64 64\">" |
|
1158 |
"<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" |
|
1159 |
"<stop offset=\"0.0\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" |
|
1160 |
"<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"x.45\"/>" |
|
1161 |
"<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"-3.abc\"/>" |
|
1162 |
"</radialGradient>" |
|
1163 |
"<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" |
|
1164 |
"<stop offset=\"0.0\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" |
|
1165 |
"<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"-0.xy\"/>" |
|
1166 |
"<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"z.5\"/>" |
|
1167 |
"</radialGradient>" |
|
1168 |
"<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" |
|
1169 |
"<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" |
|
1170 |
"</svg>", |
|
1171 |
//Stop offset and Stop opacity |
|
1172 |
"<svg viewBox=\"0 0 64 64\">" |
|
1173 |
"<radialGradient id=\"MyGradient1\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"50\" r=\"30\" fx=\"20\" fy=\"20\">" |
|
1174 |
"<stop offset=\"abc\" style=\"stop-color:red\" stop-opacity=\"0.3\"/>" |
|
1175 |
"<stop offset=\"0.5\" style=\"stop-color:green\" stop-opacity=\"x.45\"/>" |
|
1176 |
"<stop offset=\"1\" style=\"stop-color:yellow\" stop-opacity=\"-3.abc\"/>" |
|
1177 |
"</radialGradient>" |
|
1178 |
"<radialGradient id=\"MyGradient2\" gradientUnits=\"userSpaceOnUse\" cx=\"50\" cy=\"70\" r=\"70\" fx=\"20\" fy=\"20\">" |
|
1179 |
"<stop offset=\"-3.bc\" style=\"stop-color:blue\" stop-opacity=\"0.3\"/>" |
|
1180 |
"<stop offset=\"0.5\" style=\"stop-color:violet\" stop-opacity=\"-0.xy\"/>" |
|
1181 |
"<stop offset=\"1\" style=\"stop-color:orange\" stop-opacity=\"z.5\"/>" |
|
1182 |
"</radialGradient>" |
|
1183 |
"<rect x=\"5\" y=\"5\" width=\"55\" height=\"55\" fill=\"url(#MyGradient1)\" stroke=\"black\" />" |
|
1184 |
"<rect x=\"20\" y=\"20\" width=\"35\" height=\"35\" fill=\"url(#MyGradient2)\"/>" |
|
1185 |
"</svg>" |
|
1186 |
}; |
|
1187 |
||
1188 |
QImage images[4]; |
|
1189 |
QPainter p; |
|
1190 |
||
1191 |
for (int i = 0; i < 4; ++i) { |
|
1192 |
QByteArray data(svgs[i]); |
|
1193 |
QSvgRenderer renderer(data); |
|
1194 |
QVERIFY(renderer.isValid()); |
|
1195 |
images[i] = QImage(64, 64, QImage::Format_ARGB32_Premultiplied); |
|
1196 |
images[i].fill(-1); |
|
1197 |
p.begin(&images[i]); |
|
1198 |
renderer.render(&p); |
|
1199 |
p.end(); |
|
1200 |
} |
|
1201 |
QCOMPARE(images[0], images[1]); |
|
1202 |
QCOMPARE(images[0], images[2]); |
|
1203 |
QCOMPARE(images[0], images[3]); |
|
1204 |
} |
|
1205 |
||
1206 |
void tst_QSvgRenderer::testUseElement() |
|
1207 |
{ |
|
1208 |
static const char *svgs[] = { |
|
1209 |
//Use refering to non group node (1) |
|
1210 |
"<svg viewBox = \"0 0 200 200\">" |
|
1211 |
" <polygon points=\"20,20 50,120 100,10 40,80 50,80\"/>" |
|
1212 |
" <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\"/>" |
|
1213 |
"</svg>", |
|
1214 |
"<svg viewBox = \"0 0 200 200\">" |
|
1215 |
" <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" |
|
1216 |
" <use y = \"60\" xlink:href = \"#usedPolyline\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\"/>" |
|
1217 |
"</svg>", |
|
1218 |
"<svg viewBox = \"0 0 200 200\">" |
|
1219 |
" <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" |
|
1220 |
" <g fill = \" red\" fill-opacity =\"0.2\">" |
|
1221 |
"<use y = \"60\" xlink:href = \"#usedPolyline\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" stroke-width = \"3\"/>" |
|
1222 |
"</g>" |
|
1223 |
"</svg>", |
|
1224 |
"<svg viewBox = \"0 0 200 200\">" |
|
1225 |
" <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\"/>" |
|
1226 |
" <g stroke-width = \"3\" stroke = \"yellow\">" |
|
1227 |
" <use y = \"60\" xlink:href = \"#usedPolyline\" fill = \" red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\"/>" |
|
1228 |
" </g>" |
|
1229 |
"</svg>", |
|
1230 |
//Use refering to non group node (2) |
|
1231 |
"<svg viewBox = \"0 0 200 200\">" |
|
1232 |
" <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\"/>" |
|
1233 |
" <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\"/>" |
|
1234 |
"</svg>", |
|
1235 |
"<svg viewBox = \"0 0 200 200\">" |
|
1236 |
" <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\">" |
|
1237 |
" <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\" />" |
|
1238 |
" </g>" |
|
1239 |
" <g stroke = \"blue\" stroke-width = \"3\" stroke-dasharray = \"1,1,1,1\" stroke-offset = \"5\" stroke-miterlimit = \"3\" stroke-linecap = \"butt\" stroke-linejoin = \"square\">" |
|
1240 |
" <use y = \"60\" xlink:href = \"#usedPolyline\" fill-opacity = \"0.7\" fill= \"red\" stroke = \"blue\" fill-rule = \"evenodd\"/>" |
|
1241 |
" </g>" |
|
1242 |
"</svg>", |
|
1243 |
"<svg viewBox = \"0 0 200 200\">" |
|
1244 |
" <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\">" |
|
1245 |
" <polygon id = \"usedPolyline\" points=\"20,20 50,120 100,10 40,80 50,80\" />" |
|
1246 |
" </g>" |
|
1247 |
" <g stroke-width = \"3\" stroke-dasharray = \"1,1,1,1\" stroke-offset = \"5\" stroke-miterlimit = \"3\" stroke-linecap = \"butt\" stroke-linejoin = \"square\" >" |
|
1248 |
" <use y = \"60\" xlink:href = \"#usedPolyline\" fill= \"red\" stroke = \"blue\" fill-opacity = \"0.7\" fill-rule = \"evenodd\" />" |
|
1249 |
" </g>" |
|
1250 |
"</svg>", |
|
1251 |
//Use refering to group node |
|
1252 |
"<svg viewBox = \"0 0 200 200\">" |
|
1253 |
" <g>" |
|
1254 |
" <circle cx=\"0\" cy=\"0\" r=\"100\" fill = \"red\" fill-opacity = \"0.6\"/>" |
|
1255 |
" <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\" fill = \"red\" fill-opacity = \"0.5\"/>" |
|
1256 |
" <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" fill-opacity = \"0.5\"/>" |
|
1257 |
" </g>" |
|
1258 |
"</svg>", |
|
1259 |
"<svg viewBox = \"0 0 200 200\">" |
|
1260 |
" <defs>" |
|
1261 |
" <g id=\"usedG\">" |
|
1262 |
" <circle cx=\"0\" cy=\"0\" r=\"100\" fill-opacity = \"0.6\"/>" |
|
1263 |
" <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\"/>" |
|
1264 |
" <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" />" |
|
1265 |
" </g>" |
|
1266 |
" </defs>" |
|
1267 |
" <use xlink:href =\"#usedG\" fill = \"red\" fill-opacity =\"0.5\"/>" |
|
1268 |
"</svg>", |
|
1269 |
"<svg viewBox = \"0 0 200 200\">" |
|
1270 |
" <defs>" |
|
1271 |
" <g fill = \"blue\" fill-opacity = \"0.3\">" |
|
1272 |
" <g id=\"usedG\">" |
|
1273 |
" <circle cx=\"0\" cy=\"0\" r=\"100\" fill-opacity = \"0.6\"/>" |
|
1274 |
" <rect x = \"10\" y = \"10\" width = \"30\" height = \"30\"/>" |
|
1275 |
" <circle fill=\"#a6ce39\" cx=\"0\" cy=\"0\" r=\"33\" />" |
|
1276 |
" </g>" |
|
1277 |
" </g>" |
|
1278 |
" </defs>" |
|
1279 |
" <g fill = \"red\" fill-opacity =\"0.5\">" |
|
1280 |
" <use xlink:href =\"#usedG\" />" |
|
1281 |
" </g>" |
|
1282 |
"</svg>" |
|
1283 |
}; |
|
1284 |
||
1285 |
const int COUNT = sizeof(svgs) / sizeof(svgs[0]); |
|
1286 |
QImage images[COUNT]; |
|
1287 |
QPainter p; |
|
1288 |
||
1289 |
for (int i = 0; i < COUNT; ++i) { |
|
1290 |
QByteArray data(svgs[i]); |
|
1291 |
QSvgRenderer renderer(data); |
|
1292 |
images[i] = QImage(200, 200, QImage::Format_ARGB32_Premultiplied); |
|
1293 |
images[i].fill(-1); |
|
1294 |
p.begin(&images[i]); |
|
1295 |
renderer.render(&p); |
|
1296 |
p.end(); |
|
1297 |
||
1298 |
if (i < 4 && i != 0) { |
|
1299 |
QCOMPARE(images[0], images[i]); |
|
1300 |
} else if (i > 4 && i < 7) { |
|
1301 |
if (sizeof(qreal) != sizeof(float)) |
|
1302 |
{ |
|
1303 |
// These images use blending functions which due to numerical |
|
1304 |
// issues on Windows CE and likes differ in very few pixels. |
|
1305 |
// For this reason an exact comparison will fail. |
|
1306 |
QCOMPARE(images[4], images[i]); |
|
1307 |
} |
|
1308 |
} else if (i > 7) { |
|
1309 |
QCOMPARE(images[8], images[i]); |
|
1310 |
} |
|
1311 |
} |
|
1312 |
} |
|
1313 |
||
1314 |
QTEST_MAIN(tst_QSvgRenderer) |
|
1315 |
#include "tst_qsvgrenderer.moc" |