44 #ifndef QT_NO_SVGGENERATOR 48 #include "private/qpaintengine_p.h" 49 #include "private/qtextengine_p.h" 50 #include "private/qdrawhelper_p.h" 81 foreach (
qreal entry, pattern)
84 pattern_string->
chop(1);
148 return QPaintEngine::PaintEngineFeatures(
185 d_func()->size =
size;
196 d_func()->attributes.document_title = title;
201 d_func()->attributes.document_description = description;
207 d_func()->outputDevice = device;
220 saveGradientUnits(str, g);
229 saveGradientStops(str, g);
237 saveGradientUnits(str, g);
246 saveGradientStops(str, g);
251 qWarning(
"svg's don't support conical gradients!");
258 bool constantAlpha =
true;
259 int alpha = stops.
at(0).
second.alpha();
260 for (
int i = 1; i < stops.
size(); ++i)
261 constantAlpha &= (stops.
at(i).
second.alpha() == alpha);
263 if (!constantAlpha) {
268 for (
int i = 0; i + 1 < stops.
size(); ++i) {
275 for (
int j = 1; j < parts; ++j) {
314 *d_func()->stream <<
QLatin1String(
"fill-rule=\"evenodd\" ");
315 *d_func()->stream <<
QLatin1String(
"stroke-linecap=\"square\" ");
316 *d_func()->stream <<
QLatin1String(
"stroke-linejoin=\"bevel\" ");
321 return *d_func()->stream;
329 d_func()->pen = spen;
331 switch (spen.
style()) {
336 d_func()->attributes.strokeOpacity =
QString();
344 d_func()->attributes.stroke = color;
345 d_func()->attributes.strokeOpacity = colorOpacity;
366 d_func()->attributes.stroke = color;
367 d_func()->attributes.strokeOpacity = colorOpacity;
383 stream() <<
"stroke-width=\"1\" ";
389 stream() <<
"stroke-linecap=\"butt\" ";
392 stream() <<
"stroke-linecap=\"square\" ";
395 stream() <<
"stroke-linecap=\"round\" ";
402 stream() <<
"stroke-linejoin=\"miter\" " 403 "stroke-miterlimit=\""<<spen.
miterLimit()<<
"\" ";
406 stream() <<
"stroke-linejoin=\"bevel\" ";
409 stream() <<
"stroke-linejoin=\"round\" ";
412 stream() <<
"stroke-linejoin=\"miter\" " 413 "stroke-miterlimit=\""<<spen.
miterLimit()<<
"\" ";
421 d_func()->brush = sbrush;
422 switch (sbrush.
style()) {
426 stream() <<
"fill=\"" << color <<
"\" " 428 << colorOpacity <<
"\" ";
429 d_func()->attributes.
fill = color;
430 d_func()->attributes.fillOpacity = colorOpacity;
434 saveLinearGradientBrush(sbrush.
gradient());
436 d_func()->attributes.fillOpacity =
QString();
440 saveRadialGradientBrush(sbrush.
gradient());
442 d_func()->attributes.fillOpacity =
QString();
446 saveConicalGradientBrush(sbrush.
gradient());
448 d_func()->attributes.fillOpacity =
QString();
454 d_func()->attributes.fillOpacity =
QString();
467 if (
d->font.pixelSize() == -1)
472 int svgWeight =
d->font.weight();
488 d->attributes.font_family =
d->font.family();
491 *
d->stream <<
"font-family=\"" <<
d->attributes.font_family <<
"\" " 492 "font-size=\"" <<
d->attributes.font_size <<
"\" " 493 "font-weight=\"" <<
d->attributes.font_weight <<
"\" " 494 "font-style=\"" <<
d->attributes.font_style <<
"\" " 557 d->owns_iodevice =
false;
566 if (
d->owns_iodevice)
567 delete d->engine->outputDevice();
584 return d->engine->documentTitle();
591 d->engine->setDocumentTitle(title);
607 return d->engine->documentDescription();
614 d->engine->setDocumentDescription(description);
637 return d->engine->size();
643 if (
d->engine->isActive()) {
644 qWarning(
"QSvgGenerator::setSize(), cannot set size while SVG is being generated");
647 d->engine->setSize(size);
670 return d->engine->viewBox();
686 return d->engine->viewBox().toRect();
692 if (
d->engine->isActive()) {
693 qWarning(
"QSvgGenerator::setViewBox(), cannot set viewBox while SVG is being generated");
696 d->engine->setViewBox(viewBox);
723 if (
d->engine->isActive()) {
724 qWarning(
"QSvgGenerator::setFileName(), cannot set file name while SVG is being generated");
728 if (
d->owns_iodevice)
729 delete d->engine->outputDevice();
731 d->owns_iodevice =
true;
735 d->engine->setOutputDevice(file);
754 return d->engine->outputDevice();
760 if (
d->engine->isActive()) {
761 qWarning(
"QSvgGenerator::setOutputDevice(), cannot set output device while SVG is being generated");
764 d->owns_iodevice =
false;
765 d->engine->setOutputDevice(outputDevice);
785 return d->engine->resolution();
791 d->engine->setResolution(dpi);
814 return d->engine->size().width();
816 return d->engine->size().height();
818 return d->engine->resolution();
820 return d->engine->resolution();
822 return qRound(
d->engine->size().height() * 25.4 /
d->engine->resolution());
824 return qRound(
d->engine->size().width() * 25.4 /
d->engine->resolution());
828 return d->engine->resolution();
830 return d->engine->resolution();
832 qWarning(
"QSvgGenerator::metric(), unhandled metric %d\n", metric);
845 if (!
d->outputDevice) {
846 qWarning(
"QSvgPaintEngine::begin(), no output device");
850 if (!
d->outputDevice->isOpen()) {
852 qWarning(
"QSvgPaintEngine::begin(), could not open output device: '%s'",
856 }
else if (!
d->outputDevice->isWritable()) {
857 qWarning(
"QSvgPaintEngine::begin(), could not write to read-only output device: '%s'",
865 *
d->stream <<
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" <<
endl <<
"<svg";
867 if (
d->size.isValid()) {
868 qreal wmm =
d->size.width() * 25.4 /
d->resolution;
869 qreal hmm =
d->size.height() * 25.4 /
d->resolution;
870 *
d->stream <<
" width=\"" << wmm <<
"mm\" height=\"" << hmm <<
"mm\"" <<
endl;
873 if (
d->viewBox.isValid()) {
874 *
d->stream <<
" viewBox=\"" <<
d->viewBox.left() <<
' ' <<
d->viewBox.top();
875 *
d->stream <<
' ' <<
d->viewBox.width() <<
' ' <<
d->viewBox.height() <<
'\"' <<
endl;
878 *
d->stream <<
" xmlns=\"http://www.w3.org/2000/svg\"" 879 " xmlns:xlink=\"http://www.w3.org/1999/xlink\" " 880 " version=\"1.2\" baseProfile=\"tiny\">" <<
endl;
882 if (!
d->attributes.document_title.isEmpty()) {
883 *
d->stream <<
"<title>" <<
d->attributes.document_title <<
"</title>" <<
endl;
886 if (!
d->attributes.document_description.isEmpty()) {
887 *
d->stream <<
"<desc>" <<
d->attributes.document_description <<
"</desc>" <<
endl;
890 d->stream->setString(&
d->defs);
891 *
d->stream <<
"<defs>\n";
893 d->stream->setString(&
d->body);
896 generateQtDefaults();
906 d->stream->setString(&
d->defs);
907 *
d->stream <<
"</defs>\n";
909 d->stream->setDevice(
d->outputDevice);
910 #ifndef QT_NO_TEXTCODEC 914 *
d->stream <<
d->header;
915 *
d->stream <<
d->defs;
916 *
d->stream <<
d->body;
917 if (
d->afterFirstUpdate)
918 *
d->stream <<
"</g>" <<
endl;
920 *
d->stream <<
"</g>" <<
endl 931 drawImage(r, pm.
toImage(), sr);
943 stream() <<
"x=\""<<r.
x()<<
"\" " 945 "width=\""<<r.
width()<<
"\" " 946 "height=\""<<r.
height()<<
"\" " 947 "preserveAspectRatio=\"none\" ";
952 image.
save(&buffer,
"PNG");
954 stream() <<
"xlink:href=\"data:image/png;base64," 962 QPaintEngine::DirtyFlags flags = state.
state();
968 if (
d->afterFirstUpdate)
969 *
d->stream <<
"</g>\n\n";
974 qbrushToSvg(state.
brush());
978 qpenToSvg(state.
pen());
983 *
d->stream <<
"transform=\"matrix(" <<
d->matrix.m11() <<
',' 984 <<
d->matrix.m12() <<
',' 985 <<
d->matrix.m21() <<
',' <<
d->matrix.m22() <<
',' 986 <<
d->matrix.dx() <<
',' <<
d->matrix.dy()
992 qfontToSvg(state.
font());
1000 *
d->stream <<
'>' <<
endl;
1002 d->afterFirstUpdate =
true;
1009 *
d->stream <<
"<path vector-effect=\"" 1010 << (state->pen().isCosmetic() ?
"non-scaling-stroke" :
"none")
1011 <<
"\" fill-rule=\"" 1019 *
d->stream <<
'M' << e.
x <<
',' << e.
y;
1022 *
d->stream <<
'L' << e.
x <<
',' << e.
y;
1025 *
d->stream <<
'C' << e.
x <<
',' << e.
y;
1034 *
d->stream << e.
x <<
',' << e.
y;
1046 *
d->stream <<
"\"/>" <<
endl;
1057 for (
int i=1; i<pointCount; ++i)
1060 if (mode == PolylineMode) {
1061 stream() <<
"<polyline fill=\"none\" vector-effect=\"" 1062 << (state->pen().isCosmetic() ?
"non-scaling-stroke" :
"none")
1064 for (
int i = 0; i < pointCount; ++i) {
1065 const QPointF &pt = points[i];
1066 stream() << pt.
x() <<
',' << pt.
y() <<
' ';
1084 *
d->stream <<
"<text " 1085 "fill=\"" <<
d->attributes.stroke <<
"\" " 1086 "fill-opacity=\"" <<
d->attributes.strokeOpacity <<
"\" " 1088 "xml:space=\"preserve\" " 1089 "x=\"" << pt.
x() <<
"\" y=\"" << pt.
y() <<
"\" ";
1090 qfontToSvg(textItem.
font());
1099 #endif // QT_NO_SVGGENERATOR ElementType type
the type of element
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setResolution(int resolution)
The QColor class provides colors based on RGB, HSV or CMYK values.
QPointF focalPoint() const
Returns the focal point of this radial gradient in logical coordinates.
The QPainterPath::Element class specifies the position and type of a subpath.
QImage toImage() const
Converts the pixmap to a QImage.
void setDocumentDescription(const QString &description)
QPaintEngine::DirtyFlags state() const
Returns a combination of flags identifying the set of properties that need to be updated when updatin...
qreal y() const
Returns the y-coordinate of the rectangle's top edge.
qreal alphaF() const
Returns the alpha color component of this color.
static void translate_dashPattern(QVector< qreal > pattern, const qreal &width, QString *pattern_string)
qreal opacity() const
Returns the opacity in the current paint engine state.
#define QT_END_NAMESPACE
This macro expands to.
const QColor & color() const
Returns the brush color.
const QGradient * gradient() const
Returns the gradient describing this brush.
The QMatrix class specifies 2D transformations of a coordinate system.
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
struct QSvgPaintEnginePrivate::_attributes attributes
Qt::PenStyle style() const
Returns the pen style.
QString generateGradientName()
CoordinateMode coordinateMode() const
Returns the coordinate mode of this gradient.
bool open(OpenMode openMode)
Reimplemented Function
QString currentGradientName
void drawPath(const QPainterPath &path)
The default implementation ignores the path and does nothing.
The QByteArray class provides an array of bytes.
void chop(int n)
Removes n characters from the end of the string.
void close()
Reimplemented Function
The QPointF class defines a point in the plane using floating point precision.
void closeSubpath()
Closes the current subpath by drawing a line to the beginning of the subpath, automatically starting ...
void setOutputDevice(QIODevice *device)
bool save(const QString &fileName, const char *format=0, int quality=-1) const
Saves the image to the file with the given fileName, using the given image file format and quality fa...
QColor color() const
Returns the color of this pen's brush.
void qbrushToSvg(const QBrush &sbrush)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void generateQtDefaults()
static void translate_color(const QColor &color, QString *color_string, QString *opacity_string)
~QSvgGenerator()
Destroys the generator.
ushort red
Returns the red color component of this color.
The QBuffer class provides a QIODevice interface for a QByteArray.
The QRadialGradient class is used in combination with QBrush to specify a radial gradient brush...
qreal y
the y coordinate of the element's position.
void qpenToSvg(const QPen &spen)
The QString class provides a Unicode character string.
QMatrix matrix() const
Returns the matrix in the current paint engine state.
void setFileName(const QString &fileName)
The QPen class defines how a QPainter should draw lines and outlines of shapes.
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
void drawTextItem(const QPointF &pt, const QTextItem &item)
This function draws the text item textItem at position p.
QVector< qreal > dashPattern() const
Returns the dash pattern of this pen.
const QPainterPath::Element & elementAt(int i) const
Returns the element at the given index in the painter path.
InterpolationMode interpolationMode() const
Returns the interpolation mode of this gradient.
QIODevice * outputDevice() const
void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
Reimplement this virtual function to draw the polygon defined by the pointCount first points in point...
qreal x() const
Returns the x-coordinate of this point.
QPaintEngine * paintEngine() const
Returns the paint engine used to render graphics to be converted to SVG format information.
void lineTo(const QPointF &p)
Adds a straight line from the current position to the given endPoint.
QGradientStops stops() const
Returns the stop points for this gradient.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QBrush brush() const
Returns the brush in the current paint engine state.
The QRectF class defines a rectangle in the plane using floating point precision. ...
static QColor fromRgba(QRgb rgba)
Static convenience function that returns a QColor constructed from the given QRgb value rgba...
void saveConicalGradientBrush(const QGradient *)
Q_STATIC_INLINE_FUNCTION uint PREMUL(uint x)
Qt::PenJoinStyle joinStyle() const
Returns the pen's join style.
Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b)
QFont font() const
Returns the font in the current paint engine state.
bool end()
Reimplement this function to finish painting on the current paint device.
qreal height() const
Returns the height of the rectangle.
void saveGradientUnits(QTextStream &str, const QGradient *gradient)
QString documentDescription() const
void append(const T &t)
Inserts value at the end of the vector.
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
static const char * data(const QByteArray &arr)
void saveGradientStops(QTextStream &str, const QGradient *g)
qreal width() const
Returns the width of the rectangle.
QString documentTitle() const
void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlag=Qt::AutoColor)
Qt::FillRule fillRule() const
Returns the painter path's currently set fill rule.
Qt::BrushStyle style() const
Returns the brush style.
void setDescription(const QString &description)
void saveLinearGradientBrush(const QGradient *g)
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
QPointF start() const
Returns the start point of this linear gradient in logical coordinates.
void setResolution(int dpi)
const T & at(int i) const
Returns the item at index position i in the vector.
Q_GUI_EXPORT QString escape(const QString &plain)
Converts the plain text string plain to a HTML string with HTML metacharacters <, >...
void setSize(const QSize &size)
void setViewBox(const QRect &viewBox)
int width() const
Returns the pen width with integer precision.
The QBrush class defines the fill pattern of shapes drawn by QPainter.
QSvgGenerator()
Constructs a new generator.
static QPaintEngine::PaintEngineFeatures svgEngineFeatures()
void setDocumentTitle(const QString &title)
QPaintEngine::Type type() const
Reimplement this function to return the paint engine Type.
QString description() const
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
void setOutputDevice(QIODevice *outputDevice)
ushort blue
Returns the blue color component of this color.
The QFile class provides an interface for reading from and writing to files.
void setViewBox(const QRectF &viewBox)
The QTextStream class provides a convenient interface for reading and writing text.
The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush...
The QFont class specifies a font used for drawing text.
The QGradient class is used in combination with QBrush to specify gradient fills. ...
qreal x() const
Returns the x-coordinate of the rectangle's left edge.
int metric(QPaintDevice::PaintDeviceMetric metric) const
Reimplemented Function
QPair< qreal, QColor > QGradientStop
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
qreal miterLimit() const
Returns the miter limit of the pen.
bool begin(QPaintDevice *device)
Reimplement this function to initialise your paint engine when painting is to start on the paint devi...
reference back()
This function is provided for STL compatibility.
void setSize(const QSize &size)
QString & fill(QChar c, int size=-1)
Sets every character in the string to character ch.
qreal widthF() const
Returns the pen width with floating point precision.
The QRect class defines a rectangle in the plane using integer precision.
qreal radius() const
Returns the radius of this radial gradient in logical coordinates.
QByteArray toBase64() const
Returns a copy of the byte array, encoded as Base64.
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
QIODevice * outputDevice() const
qreal x
the x coordinate of the element's position.
qreal y() const
Returns the y-coordinate of this point.
QPointF center() const
Returns the center of this radial gradient in logical coordinates.
The QPixmap class is an off-screen image representation that can be used as a paint device...
qreal dashOffset() const
Returns the dash offset for the pen.
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
The QPaintEngineState class provides information about the active paint engine's current state...
The QSize class defines the size of a two-dimensional object using integer point precision.
#define Q_DECLARE_PRIVATE(Class)
int elementCount() const
Returns the number of path elements in the painter path.
QPen pen() const
Returns the pen in the current paint engine state.
The QTextItem class provides all the information required to draw text in a custom paint engine...
QString document_description
Qt::PenCapStyle capStyle() const
Returns the pen's cap style.
static const KeyPair *const end
The QIODevice class is the base interface class of all I/O devices in Qt.
#define qPrintable(string)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
ushort green
Returns the green color component of this color.
int size() const
Returns the number of items in the vector.
QFont font() const
Returns the font that should be used to draw the text.
void saveRadialGradientBrush(const QGradient *g)
The QSvgGenerator class provides a paint device that is used to create SVG drawings.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
void setTitle(const QString &title)
Q_DECL_CONSTEXPR int qRound(qreal d)
void qfontToSvg(const QFont &sfont)
void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
Reimplement this function to draw the part of the pm specified by the sr rectangle in the given r...
QPointF finalStop() const
Returns the final stop point of this linear gradient in logical coordinates.
Q_CORE_EXPORT QTextStream & endl(QTextStream &s)
void updateState(const QPaintEngineState &state)
Reimplement this function to update the state of a paint engine.