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