summaryrefslogtreecommitdiff
path: root/tests/auto/qsvgdevice/tst_qsvgdevice.cpp
blob: b9364a6de4cd8fe2ec80ee77538ef46f195ff69e (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#if 0
#include <private/qpaintengine_svg_p.h>
#endif

#include <qapplication.h>
#include <qpainter.h>
#include <qbuffer.h>
#include <qimage.h>
#include <qpicture.h>
#include <qdrawutil.h>
#include <qpaintdevice.h>

class tst_QSvgDevice : public QObject
{
    Q_OBJECT
public:
    tst_QSvgDevice();

private slots:
    void play_data();
    void play();
    void boundingRect();

private:
    void playPaint( QPainter *p, const QString &type );
};

tst_QSvgDevice::tst_QSvgDevice()
{
}

void tst_QSvgDevice::play_data()
{
    QTest::addColumn<QString>("tag name");
    // we only use the tag name
    QTest::newRow( "lines" );
    QTest::newRow( "font" );
    QTest::newRow( "polyline" );
    QTest::newRow( "translate" );
    QTest::newRow( "scaleRect" );
    QTest::newRow( "ellipseOdd" );
    QTest::newRow( "ellipseEven" );
    QTest::newRow( "ellipseRandom" );
    QTest::newRow( "scaleText" );
    QTest::newRow( "scaleTextWithFont" );
    QTest::newRow( "scaleTextSaveRestore" );
    QTest::newRow( "scaleLineWithPen" );
    QTest::newRow( "task-17637" );
    QTest::newRow( "dashed-lines" );
    QTest::newRow( "dot-lines" );
    QTest::newRow( "dashed-dot-lines" );
    QTest::newRow( "dashed-dot-dot-lines" );
    QTest::newRow( "scaleDashed-lines" );
    QTest::newRow( "thick-dashed-lines" );
    QTest::newRow( "negative-rect" );
    QTest::newRow( "lightText" );
    QTest::newRow( "boldText" );
    QTest::newRow( "demiBoldText" );
    QTest::newRow( "blackText" );
    QTest::newRow( "task-20239" );
    QTest::newRow( "clipRect" );
    QTest::newRow( "multipleClipRects" );
    QTest::newRow("qsimplerichtext");
}

#if 0

class SVGDummyDevice : public QPaintDevice
{
public:
    SVGDummyDevice()
	: QPaintDevice(QInternal::ExternalDevice), eng(0) { }
    ~SVGDummyDevice() {
	delete eng;
    }
    QPaintEngine *engine() const {
	if (!eng)
	    const_cast<SVGDummyDevice *>(this)->eng = new QSVGPaintEngine;
	return eng;
    }

private:
    QPaintEngine *eng;
};

void tst_QSvgDevice::play()
{
    // current tag name
    QString type = data()->dataTag();

    // reference pixmap
    QPixmap ref( 100, 100 );
    ref.fill( Qt::white );
    QPainter pref( &ref );
    playPaint( &pref, type );
    pref.end();

    // draw the same into the SVG device
    SVGDummyDevice dev;
    QPainter pdev( &dev );
    playPaint( &pdev, type );
    pdev.end();


    //dev.setBoundingRect( QRect( 0, 0, 100, 100 ) );
    //dev.save( type + "-res.svg" ); // ### sets bounding rect to 0 !

    // replay on a result pixmap and compare
    QPixmap res( 100, 100 );
    res.fill( Qt::white );
    QPainter pres( &res );
    static_cast<QSVGPaintEngine *>(dev.engine())->play( &pres );

#if 0
    // for visual inspection
    ref.save( type + "-ref.xpm", "XPM" );
    res.save( type + "-res.xpm", "XPM" );
#endif

    QVERIFY( res.convertToImage() == ref.convertToImage() );
}

// helper function for play()
void tst_QSvgDevice::playPaint( QPainter *p, const QString &type )
{
    if ( type == "lines" ) {
	// line with pen width 0
	p->setPen( QPen( Qt::black, 0, Qt::SolidLine ) );
	p->drawLine( 10, 0, 20, 3 );

	// line with pen width 1
	p->setPen( QPen( Qt::black, 1, Qt::SolidLine ) );
	p->drawLine( 2, 0, 10, 3 );

	// rect without outline (qt-bugs/arc-17/35556)
	p->setPen( Qt::NoPen );
	p->setBrush( Qt::red );
	p->drawRect( 5, 10, 20, 30 );
    } else if ( type == "text" ) {
	QFont f = p->font();
	f.setItalic( TRUE );
	f.setBold( TRUE );
	p->setFont( f );
	p->drawText( 5, 55, "Text" );
    } else if ( type == "polyline" ) {
	// we'll draw 4 triangular polylines. Only two will show up
	// as the QPainter::drawPolyline() doesn't respect the brush
	// just the pen setting
	QPolygon pa( 3 );
	pa.setPoint( 0, 0, 0 );
	pa.setPoint( 1, 10, 0 );
	pa.setPoint( 2, 0, 10 );

	// frame around the following 4 polylines
	p->setBrush( Qt::NoBrush );
	p->setPen( Qt::SolidLine );
	p->drawRect( 46, 3, 19, 60 );

	// polyline with blue brush, no pen
	p->setPen( Qt::NoPen );
	p->setBrush( Qt::blue );
	p->translate( 50, 5 );
	p->drawPolyline( pa );

	// polyline without brush, no pen
	p->setBrush( Qt::NoBrush );
	p->translate( 0, 15 );
	p->drawPolyline( pa );

	// polyline with green brush, solid pen
	p->setBrush( Qt::green );
	p->setPen( Qt::SolidLine );
	p->translate( 0, 15 );
	p->drawPolyline( pa );

	// polyline without brush, solid pen
	p->setBrush( Qt::NoBrush );
	p->setPen( Qt::SolidLine );
	p->translate( 0, 15 );
	p->drawPolyline( pa );
    } else if ( type == "translate" ) {
	p->translate(-10,-10);
	p->save();
	p->setBrush( Qt::blue );
	p->drawRect( 20, 30, 50, 20 );
	p->restore();
	p->setBrush( Qt::green );
	p->drawRect( 70, 50, 10, 10 );
    } else if ( type == "scaleRect" ) {
	p->scale( 1, 2 );
	p->setBrush( Qt::blue );
	p->drawRect( 20, 20, 60, 60 );
    } else if ( type == "ellipseEven" ) {
	p->setBrush( Qt::blue );
	p->drawEllipse( 20, 20, 60, 60 );
    } else if ( type == "ellipseOdd" ) {
	p->setBrush( Qt::blue );
	p->drawEllipse( 20, 20, 59, 59 );
    } else if ( type == "ellipseRandom" ) {
	p->setBrush( Qt::blue );
	p->drawEllipse( 20, 34, 89, 123 );
    } else if ( type == "scaleText" ) {
	p->scale(0.25,0.25);
	p->drawText(200,200,"Hello!");
    } else if ( type == "scaleTextWithFont" ) {
	p->setFont(QFont("Helvetica",12));
	p->scale(0.25,0.25);
	p->drawText(200,200,"Hello!");
#ifdef Q_WS_WIN
    // Only test on Windows for now, visually it looks fine, but
    // it's failing for some reason on Linux
    } else if ( type == "scaleTextSaveRestore" ) {
        p->scale(1,-1);
	p->save();
	p->setFont(QFont("Helvetica",12));
	p->scale(0.5,0.5);
	p->drawText(0,0,"Hello!");
	p->restore();
#endif
    } else if ( type == "scaleLineWithPen" ) {
        p->scale(0.1,0.1);
	p->setPen(QPen(Qt::red,100));
	p->drawLine(3,3,500,500);
    } else if ( type == "task-17637" ) {
	p->translate(0,200);
	p->scale(0.01,-0.01);
	p->setBrush(Qt::blue);
	p->drawEllipse(2000,2000,6000,6000);
	p->setPen(QPen(Qt::red,100));
	p->setFont(QFont("Helvetica",12));
	p->save();
	p->scale(1,-1);
	p->drawText(4000,4000,"Hello!");
	p->restore();
	p->drawLine(3000,3000,5000,5000);
    } else if ( type == "dashed-lines" ) {
	p->setPen(QPen(Qt::red, 1, Qt::DashLine));
	p->drawLine(10,10,50,50);
    } else if ( type == "dot-lines" ) {
	p->setPen(QPen(Qt::red, 1, Qt::DotLine));
	p->drawLine(10,10,50,50);
    } else if ( type == "dashed-dot-lines" ) {
	p->setPen(QPen(Qt::red, 1, Qt::DashDotLine));
	p->drawLine(10,10,50,50);
    } else if ( type == "dashed-dot-dot-lines" ) {
	p->setPen(QPen(Qt::red, 1, Qt::DashDotDotLine));
	p->drawLine(10,10,50,50);
    } else if ( type == "scaleDashed-lines" ) {
	p->scale(0.1,0.1);
	p->setPen(QPen(Qt::red, 10, Qt::DashLine));
	p->drawLine(10,10,500,500);
    } else if ( type == "thick-dashed-lines" ) {
	p->setPen(QPen(Qt::red, 10, Qt::DashLine));
	p->drawLine(10,10,50,50);
    } else if ( type == "negative-rect" ) {
	p->drawRect(70, 0,-30,30);
    } else if ( type == "lightText" ) {
	QFont f("Helvetica",12);
	f.setWeight( QFont::Light );
	p->setFont(f);
	p->drawText(0,0,"Hello!");
    } else if ( type == "boldText" ) {
	QFont f("Helvetica",12);
	f.setWeight( QFont::Bold );
	p->setFont(f);
	p->drawText(0,0,"Hello!");
    } else if ( type == "demiBoldText" ) {
	QFont f("Helvetica",12);
	f.setWeight( QFont::DemiBold );
	p->setFont(f);
	p->drawText(0,0,"Hello!");
    } else if ( type == "blackText" ) {
	QFont f("Helvetica",12);
	f.setWeight( QFont::Black );
	p->setFont(f);
	p->drawText(0,0,"Hello!");
    } else if ( type == "task-20239" ) {
        p->translate(0,200);
	p->scale(0.02,-0.02);
	p->scale(1,-1);
	for(int y = 1000; y <=10000; y += 2000) {
	    QBrush br(Qt::green);
	    QPalette palette(Qt::green,Qt::blue);
	    qDrawShadePanel(p,4000,5000,4000,2000,palette.active(),false,200,&br);
	    qDrawShadeRect(p,2000,2000,4000,2000,palette.active(),true,100,0,&br);
	}
    } else if (type=="clipRect") {
	p->setClipRect(15,25,10,10);
	p->drawEllipse(10,20,50,70);
    } else if (type=="multipleClipRects") {
	p->setClipRect(15,25,10,10);
	p->drawEllipse(10,20,50,70);
	p->setClipRect(100,200,20,20);
	p->drawEllipse(110,220,50,70);
    } else if (type == "qsimplerichtext") {
	p->setPen(QColor(0,0,0));
        p->setBrush(Qt::NoBrush);
        QRect rect1(10, 10, 100, 50);
        QRect rect2(200, 20, 80, 50);
        p->drawRect(10, 10, 100, 50);
        p->drawRect(200, 20, 80, 50);

        QSimpleRichText text1("Text 1", QApplication::font());
        QSimpleRichText text2("Text 2", QApplication::font());

        QColorGroup cg(Qt::black, Qt::red, Qt::gray, Qt::gray, Qt::gray, Qt::black, Qt::blue, Qt::black, Qt::white);
        text1.draw(p, rect1.x(), rect1.y(), rect1, cg);
        text2.draw(p, rect2.x(), rect2.y(), rect2, cg);
    }
}

void tst_QSvgDevice::boundingRect()
{
    // ### the bounding rect is only calculated/parsed
    // ### when replaying. Therefore we go through QPicture.
    QRect r( 10, 20, 30, 40 );
    // create document
    QPicture pic;
    QPainter p( &pic );
    p.drawRect( r );
    p.end();
    pic.save( "tmp.svg", "svg" );
    // load it
    QPicture pic2;
    pic2.load( "tmp.svg", "svg" );
    QCOMPARE( pic2.boundingRect(), r );
}

#else

void tst_QSvgDevice::play()
{
    QSKIP("This test needs some redoing, this is just a temp measure until I work it out");
}

void tst_QSvgDevice::boundingRect()
{
    QSKIP("This test needs some redoing, this is just a temp measure until I work it out");
}

#endif

QTEST_MAIN(tst_QSvgDevice)
#include "tst_qsvgdevice.moc"