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
|
// Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QGuiApplication>
#include <QFile>
#include <QSvgGenerator>
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QPainterPath>
#include <QRect>
#include <QSize>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
const QStringList arguments = app.arguments();
if (arguments.size() < 2) {
qWarning("Missing file name");
return 0;
}
QFile output(arguments[1]);
if (!output.open(QIODevice::WriteOnly))
qFatal("Cannot open output file name");
QSvgGenerator generator(QSvgGenerator::SvgVersion::Svg11);
generator.setOutputDevice(&output);
generator.setSize(QSize(1000, 500));
generator.setViewBox(QRect(0, 0, 1000, 500));
{
QPainter painter(&generator);
QFont f = painter.font();
f.setPointSize(48);
painter.setFont(f);
{
painter.save();
// clipped rectangle
painter.setClipRect(QRect(100, 100, 250, 200));
painter.setBrush(QColorConstants::Blue);
painter.drawEllipse(QRect(0, 100, 400, 200));
{
painter.save();
// transformed element within clip
painter.setBrush(QColorConstants::Green);
painter.translate(300, 150);
painter.rotate(45);
painter.drawEllipse(QPointF(0, 0), 100, 50);
painter.restore();
}
painter.drawText(200, 200, "A very long clipped text");
painter.restore();
}
{
// unclipped
painter.setBrush(QColorConstants::Red);
painter.drawEllipse(0, 0, 200, 150);
}
{
painter.save();
// transformed clip (by transforming the painter before setting the clip);
painter.translate(500, 0);
painter.rotate(45);
painter.setClipRect(QRect(50, 50, 150, 200));
painter.setBrush(QColorConstants::Green);
QPainterPath path;
path.addRect(50, 50, 100, 100);
path.moveTo(0, 0);
path.cubicTo(300, 0, 150, 150, 300, 300);
path.cubicTo(0, 300, 150, 150, 0, 0);
painter.drawPath(path);
painter.setBrush(QColorConstants::Blue);
painter.drawEllipse(QPointF(125, 125), 100, 50);
painter.restore();
}
{
painter.save();
// transformed clip
painter.translate(700, 50);
// clip by path
QPainterPath path;
path.moveTo(0, 0);
path.cubicTo(300, 0, 150, 150, 300, 300);
path.cubicTo(0, 300, 150, 150, 0, 0);
painter.setBrush(QColorConstants::Svg::red);
painter.drawPath(path);
painter.setClipPath(path);
painter.setBrush(QColorConstants::Svg::purple);
painter.drawEllipse(QPointF(150, 50), 150, 50);
// transform and remove clipping
painter.translate(0, 100);
painter.setClipping(false);
painter.setBrush(QColorConstants::Svg::darkblue);
painter.drawEllipse(QPointF(150, 50), 150, 50);
// transform and clip again
painter.translate(0, 100);
painter.setClipping(true);
painter.setBrush(QColorConstants::Svg::green);
painter.drawEllipse(QPointF(150, 50), 150, 50);
painter.restore();
}
}
output.close();
}
|