| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsvggenerator.h" |
| 5 | |
| 6 | #ifndef QT_NO_SVGGENERATOR |
| 7 | |
| 8 | #include "qpainterpath.h" |
| 9 | |
| 10 | #include "private/qpaintengine_p.h" |
| 11 | #include "private/qtextengine_p.h" |
| 12 | #include "private/qdrawhelper_p.h" |
| 13 | |
| 14 | #include "qfile.h" |
| 15 | #include "qtextstream.h" |
| 16 | #include "qbuffer.h" |
| 17 | #include "qmath.h" |
| 18 | #include "qbitmap.h" |
| 19 | #include "qtransform.h" |
| 20 | |
| 21 | #include "qdebug.h" |
| 22 | |
| 23 | #include <optional> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | static void translate_color(const QColor &color, QString *color_string, |
| 28 | QString *opacity_string) |
| 29 | { |
| 30 | Q_ASSERT(color_string); |
| 31 | Q_ASSERT(opacity_string); |
| 32 | |
| 33 | *color_string = |
| 34 | QString::fromLatin1(ba: "#%1%2%3" ) |
| 35 | .arg(a: color.red(), fieldWidth: 2, base: 16, fillChar: QLatin1Char('0')) |
| 36 | .arg(a: color.green(), fieldWidth: 2, base: 16, fillChar: QLatin1Char('0')) |
| 37 | .arg(a: color.blue(), fieldWidth: 2, base: 16, fillChar: QLatin1Char('0')); |
| 38 | *opacity_string = QString::number(color.alphaF()); |
| 39 | } |
| 40 | |
| 41 | static void translate_dashPattern(const QList<qreal> &pattern, qreal width, QString *pattern_string) |
| 42 | { |
| 43 | Q_ASSERT(pattern_string); |
| 44 | |
| 45 | // Note that SVG operates in absolute lengths, whereas Qt uses a length/width ratio. |
| 46 | for (qreal entry : pattern) |
| 47 | *pattern_string += QString::fromLatin1(ba: "%1," ).arg(a: entry * width); |
| 48 | |
| 49 | pattern_string->chop(n: 1); |
| 50 | } |
| 51 | |
| 52 | class QSvgPaintEnginePrivate : public QPaintEnginePrivate |
| 53 | { |
| 54 | public: |
| 55 | explicit QSvgPaintEnginePrivate(QSvgGenerator::SvgVersion version) |
| 56 | : svgVersion(version) |
| 57 | { |
| 58 | size = QSize(); |
| 59 | viewBox = QRectF(); |
| 60 | outputDevice = nullptr; |
| 61 | resolution = 72; |
| 62 | |
| 63 | attributes.document_title = QLatin1String("Qt SVG Document" ); |
| 64 | attributes.document_description = QLatin1String("Generated with Qt" ); |
| 65 | attributes.font_family = QLatin1String("serif" ); |
| 66 | attributes.font_size = QLatin1String("10pt" ); |
| 67 | attributes.font_style = QLatin1String("normal" ); |
| 68 | attributes.font_weight = QLatin1String("normal" ); |
| 69 | |
| 70 | afterFirstUpdate = false; |
| 71 | numGradients = 0; |
| 72 | } |
| 73 | |
| 74 | QSvgGenerator::SvgVersion svgVersion; |
| 75 | QSize size; |
| 76 | QRectF viewBox; |
| 77 | QIODevice *outputDevice; |
| 78 | QTextStream *stream; |
| 79 | int resolution; |
| 80 | |
| 81 | QString ; |
| 82 | QString defs; |
| 83 | QString body; |
| 84 | bool afterFirstUpdate; |
| 85 | |
| 86 | QBrush brush; |
| 87 | QPen pen; |
| 88 | QTransform matrix; |
| 89 | QFont font; |
| 90 | |
| 91 | QString generateGradientName() { |
| 92 | ++numGradients; |
| 93 | currentGradientName = QString::fromLatin1(ba: "gradient%1" ).arg(a: numGradients); |
| 94 | return currentGradientName; |
| 95 | } |
| 96 | |
| 97 | QString currentGradientName; |
| 98 | int numGradients; |
| 99 | |
| 100 | QStringList savedPatternBrushes; |
| 101 | QStringList savedPatternMasks; |
| 102 | |
| 103 | struct _attributes { |
| 104 | QString document_title; |
| 105 | QString document_description; |
| 106 | QString font_weight; |
| 107 | QString font_size; |
| 108 | QString font_family; |
| 109 | QString font_style; |
| 110 | QString stroke, strokeOpacity; |
| 111 | QString dashPattern, dashOffset; |
| 112 | QString fill, fillOpacity; |
| 113 | } attributes; |
| 114 | |
| 115 | QString generateClipPathName() { |
| 116 | ++numClipPaths; |
| 117 | currentClipPathName = QStringLiteral("clipPath%1" ).arg(a: numClipPaths); |
| 118 | return currentClipPathName; |
| 119 | } |
| 120 | |
| 121 | std::optional<QPainterPath> clipPath; |
| 122 | bool clipEnabled = false; |
| 123 | bool isClippingEffective() const { |
| 124 | return clipEnabled && clipPath.has_value(); |
| 125 | } |
| 126 | QString currentClipPathName; |
| 127 | int numClipPaths = 0; |
| 128 | bool hasEmittedClipGroup = false; |
| 129 | }; |
| 130 | |
| 131 | static inline QPaintEngine::PaintEngineFeatures svgEngineFeatures() |
| 132 | { |
| 133 | return QPaintEngine::PaintEngineFeatures( |
| 134 | QPaintEngine::AllFeatures |
| 135 | & ~QPaintEngine::PerspectiveTransform |
| 136 | & ~QPaintEngine::ConicalGradientFill |
| 137 | & ~QPaintEngine::PorterDuff); |
| 138 | } |
| 139 | |
| 140 | Q_GUI_EXPORT QImage qt_imageForBrush(int brushStyle, bool invert); |
| 141 | Q_GUI_EXPORT bool qHasPixmapTexture(const QBrush& brush); |
| 142 | |
| 143 | class QSvgPaintEngine : public QPaintEngine |
| 144 | { |
| 145 | Q_DECLARE_PRIVATE(QSvgPaintEngine) |
| 146 | public: |
| 147 | |
| 148 | explicit QSvgPaintEngine(QSvgGenerator::SvgVersion version) |
| 149 | : QPaintEngine(*new QSvgPaintEnginePrivate(version), |
| 150 | svgEngineFeatures()) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | bool begin(QPaintDevice *device) override; |
| 155 | bool end() override; |
| 156 | |
| 157 | void updateState(const QPaintEngineState &state) override; |
| 158 | void updateClipState(const QPaintEngineState &state); |
| 159 | void popGroup(); |
| 160 | |
| 161 | void drawEllipse(const QRectF &r) override; |
| 162 | void drawPath(const QPainterPath &path) override; |
| 163 | void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override; |
| 164 | void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) override; |
| 165 | void drawRects(const QRectF *rects, int rectCount) override; |
| 166 | void drawTextItem(const QPointF &pt, const QTextItem &item) override; |
| 167 | void generateImage(QTextStream &stream, const QRectF &r, const QImage &pm); |
| 168 | void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, |
| 169 | Qt::ImageConversionFlags flags = Qt::AutoColor) override; |
| 170 | |
| 171 | QPaintEngine::Type type() const override { return QPaintEngine::SVG; } |
| 172 | |
| 173 | QSize size() const { return d_func()->size; } |
| 174 | void setSize(const QSize &size) { |
| 175 | Q_ASSERT(!isActive()); |
| 176 | d_func()->size = size; |
| 177 | } |
| 178 | |
| 179 | QRectF viewBox() const { return d_func()->viewBox; } |
| 180 | void setViewBox(const QRectF &viewBox) { |
| 181 | Q_ASSERT(!isActive()); |
| 182 | d_func()->viewBox = viewBox; |
| 183 | } |
| 184 | |
| 185 | QString documentTitle() const { return d_func()->attributes.document_title; } |
| 186 | void setDocumentTitle(const QString &title) { |
| 187 | d_func()->attributes.document_title = title; |
| 188 | } |
| 189 | |
| 190 | QString documentDescription() const { return d_func()->attributes.document_description; } |
| 191 | void setDocumentDescription(const QString &description) { |
| 192 | d_func()->attributes.document_description = description; |
| 193 | } |
| 194 | |
| 195 | QIODevice *outputDevice() const { return d_func()->outputDevice; } |
| 196 | void setOutputDevice(QIODevice *device) { |
| 197 | Q_ASSERT(!isActive()); |
| 198 | d_func()->outputDevice = device; |
| 199 | } |
| 200 | |
| 201 | int resolution() const { return d_func()->resolution; } |
| 202 | void setResolution(int resolution) { |
| 203 | Q_ASSERT(!isActive()); |
| 204 | d_func()->resolution = resolution; |
| 205 | } |
| 206 | |
| 207 | QSvgGenerator::SvgVersion svgVersion() const { return d_func()->svgVersion; } |
| 208 | |
| 209 | QString savePatternMask(Qt::BrushStyle style) |
| 210 | { |
| 211 | QString maskId = QStringLiteral("patternmask%1" ).arg(a: style); |
| 212 | if (!d_func()->savedPatternMasks.contains(str: maskId)) { |
| 213 | QImage img = qt_imageForBrush(brushStyle: style, invert: true); |
| 214 | QRegion reg(QBitmap::fromData(size: img.size(), bits: img.constBits())); |
| 215 | QString rct(QStringLiteral("<rect x=\"%1\" y=\"%2\" width=\"%3\" height=\"%4\" />" )); |
| 216 | QTextStream str(&d_func()->defs, QIODevice::Append); |
| 217 | str << "<mask id=\"" << maskId << "\" x=\"0\" y=\"0\" width=\"8\" height=\"8\" " |
| 218 | << "stroke=\"none\" fill=\"#ffffff\" patternUnits=\"userSpaceOnUse\" >" << Qt::endl; |
| 219 | for (QRect r : reg) |
| 220 | str << rct.arg(a: r.x()).arg(a: r.y()).arg(a: r.width()).arg(a: r.height()) << Qt::endl; |
| 221 | str << QStringLiteral("</mask>" ) << Qt::endl << Qt::endl; |
| 222 | d_func()->savedPatternMasks.append(t: maskId); |
| 223 | } |
| 224 | return maskId; |
| 225 | } |
| 226 | |
| 227 | QString savePatternBrush(const QString &color, const QBrush &brush) |
| 228 | { |
| 229 | QString patternId = QStringLiteral("fillpattern%1_" ).arg(a: brush.style()) + QStringView{color}.mid(pos: 1); |
| 230 | if (!d_func()->savedPatternBrushes.contains(str: patternId)) { |
| 231 | QString maskId = savePatternMask(style: brush.style()); |
| 232 | QString geo(QStringLiteral("x=\"0\" y=\"0\" width=\"8\" height=\"8\"" )); |
| 233 | QTextStream str(&d_func()->defs, QIODevice::Append); |
| 234 | str << QStringLiteral("<pattern id=\"%1\" %2 patternUnits=\"userSpaceOnUse\" >" ).arg(args&: patternId, args&: geo) << Qt::endl; |
| 235 | str << QStringLiteral("<rect %1 stroke=\"none\" fill=\"%2\" mask=\"url(#%3)\" />" ).arg(args&: geo, args: color, args&: maskId) << Qt::endl; |
| 236 | str << QStringLiteral("</pattern>" ) << Qt::endl << Qt::endl; |
| 237 | d_func()->savedPatternBrushes.append(t: patternId); |
| 238 | } |
| 239 | return patternId; |
| 240 | } |
| 241 | |
| 242 | QString saveTextureBrush(const QString &color, const QBrush &brush) |
| 243 | { |
| 244 | QImage tex = brush.textureImage(); |
| 245 | QString patternId = QStringLiteral("texpattern_%1" ).arg(a: QString::number(tex.cacheKey(), base: 16)); |
| 246 | if (qHasPixmapTexture(brush) && brush.texture().isQBitmap()) { |
| 247 | Q_ASSERT(tex.format() == QImage::Format_MonoLSB); |
| 248 | tex.setColorCount(2); |
| 249 | tex.setColor(i: 0, c: qRgba(r: 0, g: 0, b: 0, a: 0)); |
| 250 | tex.setColor(i: 1, c: brush.color().rgba()); |
| 251 | patternId += QLatin1Char('_') + QStringView{color}.mid(pos: 1); |
| 252 | } |
| 253 | if (!d_func()->savedPatternBrushes.contains(str: patternId)) { |
| 254 | QString geo = QStringLiteral("x=\"0\" y=\"0\" width=\"%1\" height=\"%2\"" ).arg(a: tex.width()).arg(a: tex.height()); |
| 255 | QTextStream str(&d_func()->defs, QIODevice::Append); |
| 256 | str << QStringLiteral("<pattern id=\"%1\" %2 patternUnits=\"userSpaceOnUse\" >" ).arg(args&: patternId, args&: geo) << Qt::endl; |
| 257 | generateImage(stream&: str, r: tex.rect(), pm: tex); |
| 258 | str << QStringLiteral("</pattern>" ) << Qt::endl << Qt::endl; |
| 259 | d_func()->savedPatternBrushes.append(t: patternId); |
| 260 | } |
| 261 | return patternId; |
| 262 | } |
| 263 | |
| 264 | void saveLinearGradientBrush(const QGradient *g) |
| 265 | { |
| 266 | QTextStream str(&d_func()->defs, QIODevice::Append); |
| 267 | const QLinearGradient *grad = static_cast<const QLinearGradient*>(g); |
| 268 | str << QLatin1String("<linearGradient " ); |
| 269 | saveGradientUnits(str, gradient: g); |
| 270 | if (grad) { |
| 271 | str << QLatin1String("x1=\"" ) <<grad->start().x()<< QLatin1String("\" " ) |
| 272 | << QLatin1String("y1=\"" ) <<grad->start().y()<< QLatin1String("\" " ) |
| 273 | << QLatin1String("x2=\"" ) <<grad->finalStop().x() << QLatin1String("\" " ) |
| 274 | << QLatin1String("y2=\"" ) <<grad->finalStop().y() << QLatin1String("\" " ); |
| 275 | } |
| 276 | |
| 277 | str << QLatin1String("id=\"" ) << d_func()->generateGradientName() << QLatin1String("\">\n" ); |
| 278 | saveGradientStops(str, g); |
| 279 | str << QLatin1String("</linearGradient>" ) <<Qt::endl; |
| 280 | } |
| 281 | void saveRadialGradientBrush(const QGradient *g) |
| 282 | { |
| 283 | QTextStream str(&d_func()->defs, QIODevice::Append); |
| 284 | const QRadialGradient *grad = static_cast<const QRadialGradient*>(g); |
| 285 | str << QLatin1String("<radialGradient " ); |
| 286 | saveGradientUnits(str, gradient: g); |
| 287 | if (grad) { |
| 288 | str << QLatin1String("cx=\"" ) <<grad->center().x()<< QLatin1String("\" " ) |
| 289 | << QLatin1String("cy=\"" ) <<grad->center().y()<< QLatin1String("\" " ) |
| 290 | << QLatin1String("r=\"" ) <<grad->radius() << QLatin1String("\" " ) |
| 291 | << QLatin1String("fx=\"" ) <<grad->focalPoint().x() << QLatin1String("\" " ) |
| 292 | << QLatin1String("fy=\"" ) <<grad->focalPoint().y() << QLatin1String("\" " ); |
| 293 | } |
| 294 | str << QLatin1String("id=\"" ) <<d_func()->generateGradientName()<< QLatin1String("\">\n" ); |
| 295 | saveGradientStops(str, g); |
| 296 | str << QLatin1String("</radialGradient>" ) << Qt::endl; |
| 297 | } |
| 298 | void saveConicalGradientBrush(const QGradient *) |
| 299 | { |
| 300 | qWarning(msg: "svg's don't support conical gradients!" ); |
| 301 | } |
| 302 | |
| 303 | void saveGradientStops(QTextStream &str, const QGradient *g) { |
| 304 | QGradientStops stops = g->stops(); |
| 305 | |
| 306 | if (g->interpolationMode() == QGradient::ColorInterpolation) { |
| 307 | bool constantAlpha = true; |
| 308 | int alpha = stops.at(i: 0).second.alpha(); |
| 309 | for (int i = 1; i < stops.size(); ++i) |
| 310 | constantAlpha &= (stops.at(i).second.alpha() == alpha); |
| 311 | |
| 312 | if (!constantAlpha) { |
| 313 | const qreal spacing = qreal(0.02); |
| 314 | QGradientStops newStops; |
| 315 | QRgb fromColor = qPremultiply(x: stops.at(i: 0).second.rgba()); |
| 316 | QRgb toColor; |
| 317 | for (int i = 0; i + 1 < stops.size(); ++i) { |
| 318 | int parts = qCeil(v: (stops.at(i: i + 1).first - stops.at(i).first) / spacing); |
| 319 | newStops.append(t: stops.at(i)); |
| 320 | toColor = qPremultiply(x: stops.at(i: i + 1).second.rgba()); |
| 321 | |
| 322 | if (parts > 1) { |
| 323 | qreal step = (stops.at(i: i + 1).first - stops.at(i).first) / parts; |
| 324 | for (int j = 1; j < parts; ++j) { |
| 325 | QRgb color = qUnpremultiply(p: INTERPOLATE_PIXEL_256(x: fromColor, a: 256 - 256 * j / parts, y: toColor, b: 256 * j / parts)); |
| 326 | newStops.append(t: QGradientStop(stops.at(i).first + j * step, QColor::fromRgba(rgba: color))); |
| 327 | } |
| 328 | } |
| 329 | fromColor = toColor; |
| 330 | } |
| 331 | newStops.append(t: stops.back()); |
| 332 | stops = newStops; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | for (const QGradientStop &stop : std::as_const(t&: stops)) { |
| 337 | const QString color = stop.second.name(format: QColor::HexRgb); |
| 338 | str << QLatin1String(" <stop offset=\"" )<< stop.first << QLatin1String("\" " ) |
| 339 | << QLatin1String("stop-color=\"" ) << color << QLatin1String("\" " ) |
| 340 | << QLatin1String("stop-opacity=\"" ) << stop.second.alphaF() <<QLatin1String("\" />\n" ); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void saveGradientUnits(QTextStream &str, const QGradient *gradient) |
| 345 | { |
| 346 | str << QLatin1String("gradientUnits=\"" ); |
| 347 | if (gradient && (gradient->coordinateMode() == QGradient::ObjectBoundingMode || gradient->coordinateMode() == QGradient::ObjectMode)) |
| 348 | str << QLatin1String("objectBoundingBox" ); |
| 349 | else |
| 350 | str << QLatin1String("userSpaceOnUse" ); |
| 351 | str << QLatin1String("\" " ); |
| 352 | } |
| 353 | |
| 354 | void generateQtDefaults() |
| 355 | { |
| 356 | *d_func()->stream << QLatin1String("fill=\"none\" " ); |
| 357 | *d_func()->stream << QLatin1String("stroke=\"black\" " ); |
| 358 | *d_func()->stream << QLatin1String("stroke-width=\"1\" " ); |
| 359 | *d_func()->stream << QLatin1String("fill-rule=\"evenodd\" " ); |
| 360 | *d_func()->stream << QLatin1String("stroke-linecap=\"square\" " ); |
| 361 | *d_func()->stream << QLatin1String("stroke-linejoin=\"bevel\" " ); |
| 362 | *d_func()->stream << QLatin1String(">\n" ); |
| 363 | } |
| 364 | inline QTextStream &stream() |
| 365 | { |
| 366 | return *d_func()->stream; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | void qpenToSvg(const QPen &spen) |
| 371 | { |
| 372 | d_func()->pen = spen; |
| 373 | |
| 374 | switch (spen.style()) { |
| 375 | case Qt::NoPen: |
| 376 | stream() << QLatin1String("stroke=\"none\" " ); |
| 377 | |
| 378 | d_func()->attributes.stroke = QLatin1String("none" ); |
| 379 | d_func()->attributes.strokeOpacity = QString(); |
| 380 | return; |
| 381 | break; |
| 382 | case Qt::SolidLine: { |
| 383 | QString color, colorOpacity; |
| 384 | |
| 385 | translate_color(color: spen.color(), color_string: &color, |
| 386 | opacity_string: &colorOpacity); |
| 387 | d_func()->attributes.stroke = color; |
| 388 | d_func()->attributes.strokeOpacity = colorOpacity; |
| 389 | |
| 390 | stream() << QLatin1String("stroke=\"" )<<color<< QLatin1String("\" " ); |
| 391 | stream() << QLatin1String("stroke-opacity=\"" )<<colorOpacity<< QLatin1String("\" " ); |
| 392 | } |
| 393 | break; |
| 394 | case Qt::DashLine: |
| 395 | case Qt::DotLine: |
| 396 | case Qt::DashDotLine: |
| 397 | case Qt::DashDotDotLine: |
| 398 | case Qt::CustomDashLine: { |
| 399 | QString color, colorOpacity, dashPattern, dashOffset; |
| 400 | |
| 401 | qreal penWidth = spen.width() == 0 ? qreal(1) : spen.widthF(); |
| 402 | |
| 403 | translate_color(color: spen.color(), color_string: &color, opacity_string: &colorOpacity); |
| 404 | translate_dashPattern(pattern: spen.dashPattern(), width: penWidth, pattern_string: &dashPattern); |
| 405 | |
| 406 | // SVG uses absolute offset |
| 407 | dashOffset = QString::number(spen.dashOffset() * penWidth); |
| 408 | |
| 409 | d_func()->attributes.stroke = color; |
| 410 | d_func()->attributes.strokeOpacity = colorOpacity; |
| 411 | d_func()->attributes.dashPattern = dashPattern; |
| 412 | d_func()->attributes.dashOffset = dashOffset; |
| 413 | |
| 414 | stream() << QLatin1String("stroke=\"" )<<color<< QLatin1String("\" " ); |
| 415 | stream() << QLatin1String("stroke-opacity=\"" )<<colorOpacity<< QLatin1String("\" " ); |
| 416 | stream() << QLatin1String("stroke-dasharray=\"" )<<dashPattern<< QLatin1String("\" " ); |
| 417 | stream() << QLatin1String("stroke-dashoffset=\"" )<<dashOffset<< QLatin1String("\" " ); |
| 418 | break; |
| 419 | } |
| 420 | default: |
| 421 | qWarning(msg: "Unsupported pen style" ); |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if (spen.widthF() == 0) |
| 426 | stream() <<"stroke-width=\"1\" " ; |
| 427 | else |
| 428 | stream() <<"stroke-width=\"" << spen.widthF() << "\" " ; |
| 429 | |
| 430 | switch (spen.capStyle()) { |
| 431 | case Qt::FlatCap: |
| 432 | stream() << "stroke-linecap=\"butt\" " ; |
| 433 | break; |
| 434 | case Qt::SquareCap: |
| 435 | stream() << "stroke-linecap=\"square\" " ; |
| 436 | break; |
| 437 | case Qt::RoundCap: |
| 438 | stream() << "stroke-linecap=\"round\" " ; |
| 439 | break; |
| 440 | default: |
| 441 | qWarning(msg: "Unhandled cap style" ); |
| 442 | } |
| 443 | switch (spen.joinStyle()) { |
| 444 | case Qt::SvgMiterJoin: |
| 445 | case Qt::MiterJoin: |
| 446 | stream() << "stroke-linejoin=\"miter\" " |
| 447 | "stroke-miterlimit=\"" <<spen.miterLimit()<<"\" " ; |
| 448 | break; |
| 449 | case Qt::BevelJoin: |
| 450 | stream() << "stroke-linejoin=\"bevel\" " ; |
| 451 | break; |
| 452 | case Qt::RoundJoin: |
| 453 | stream() << "stroke-linejoin=\"round\" " ; |
| 454 | break; |
| 455 | default: |
| 456 | qWarning(msg: "Unhandled join style" ); |
| 457 | } |
| 458 | } |
| 459 | void qbrushToSvg(const QBrush &sbrush) |
| 460 | { |
| 461 | d_func()->brush = sbrush; |
| 462 | switch (sbrush.style()) { |
| 463 | case Qt::SolidPattern: { |
| 464 | QString color, colorOpacity; |
| 465 | translate_color(color: sbrush.color(), color_string: &color, opacity_string: &colorOpacity); |
| 466 | stream() << "fill=\"" << color << "\" " |
| 467 | "fill-opacity=\"" |
| 468 | << colorOpacity << "\" " ; |
| 469 | d_func()->attributes.fill = color; |
| 470 | d_func()->attributes.fillOpacity = colorOpacity; |
| 471 | } |
| 472 | break; |
| 473 | case Qt::Dense1Pattern: |
| 474 | case Qt::Dense2Pattern: |
| 475 | case Qt::Dense3Pattern: |
| 476 | case Qt::Dense4Pattern: |
| 477 | case Qt::Dense5Pattern: |
| 478 | case Qt::Dense6Pattern: |
| 479 | case Qt::Dense7Pattern: |
| 480 | case Qt::HorPattern: |
| 481 | case Qt::VerPattern: |
| 482 | case Qt::CrossPattern: |
| 483 | case Qt::BDiagPattern: |
| 484 | case Qt::FDiagPattern: |
| 485 | case Qt::DiagCrossPattern: |
| 486 | case Qt::TexturePattern: { |
| 487 | QString color, colorOpacity; |
| 488 | translate_color(color: sbrush.color(), color_string: &color, opacity_string: &colorOpacity); |
| 489 | QString patternId = (sbrush.style() == Qt::TexturePattern) ? saveTextureBrush(color, brush: sbrush) |
| 490 | : savePatternBrush(color, brush: sbrush); |
| 491 | QString patternRef = QString(QStringLiteral("url(#%1)" )).arg(a: patternId); |
| 492 | stream() << "fill=\"" << patternRef << "\" fill-opacity=\"" << colorOpacity << "\" " ; |
| 493 | d_func()->attributes.fill = patternRef; |
| 494 | d_func()->attributes.fillOpacity = colorOpacity; |
| 495 | break; |
| 496 | } |
| 497 | case Qt::LinearGradientPattern: |
| 498 | saveLinearGradientBrush(g: sbrush.gradient()); |
| 499 | d_func()->attributes.fill = QString::fromLatin1(ba: "url(#%1)" ).arg(a: d_func()->currentGradientName); |
| 500 | d_func()->attributes.fillOpacity = QString(); |
| 501 | stream() << QLatin1String("fill=\"url(#" ) << d_func()->currentGradientName << QLatin1String(")\" " ); |
| 502 | break; |
| 503 | case Qt::RadialGradientPattern: |
| 504 | saveRadialGradientBrush(g: sbrush.gradient()); |
| 505 | d_func()->attributes.fill = QString::fromLatin1(ba: "url(#%1)" ).arg(a: d_func()->currentGradientName); |
| 506 | d_func()->attributes.fillOpacity = QString(); |
| 507 | stream() << QLatin1String("fill=\"url(#" ) << d_func()->currentGradientName << QLatin1String(")\" " ); |
| 508 | break; |
| 509 | case Qt::ConicalGradientPattern: |
| 510 | saveConicalGradientBrush(sbrush.gradient()); |
| 511 | d_func()->attributes.fill = QString::fromLatin1(ba: "url(#%1)" ).arg(a: d_func()->currentGradientName); |
| 512 | d_func()->attributes.fillOpacity = QString(); |
| 513 | stream() << QLatin1String("fill=\"url(#" ) << d_func()->currentGradientName << QLatin1String(")\" " ); |
| 514 | break; |
| 515 | case Qt::NoBrush: |
| 516 | stream() << QLatin1String("fill=\"none\" " ); |
| 517 | d_func()->attributes.fill = QLatin1String("none" ); |
| 518 | d_func()->attributes.fillOpacity = QString(); |
| 519 | return; |
| 520 | break; |
| 521 | default: |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | void qfontToSvg(const QFont &sfont) |
| 526 | { |
| 527 | Q_D(QSvgPaintEngine); |
| 528 | |
| 529 | d->font = sfont; |
| 530 | |
| 531 | if (d->font.pixelSize() == -1) |
| 532 | d->attributes.font_size = QString::number(d->font.pointSizeF() * d->resolution / 72); |
| 533 | else |
| 534 | d->attributes.font_size = QString::number(d->font.pixelSize()); |
| 535 | |
| 536 | d->attributes.font_weight = QString::number(d->font.weight()); |
| 537 | d->attributes.font_family = d->font.family(); |
| 538 | d->attributes.font_style = d->font.italic() ? QLatin1String("italic" ) : QLatin1String("normal" ); |
| 539 | |
| 540 | *d->stream << "font-family=\"" << d->attributes.font_family << "\" " |
| 541 | "font-size=\"" << d->attributes.font_size << "\" " |
| 542 | "font-weight=\"" << d->attributes.font_weight << "\" " |
| 543 | "font-style=\"" << d->attributes.font_style << "\" " |
| 544 | << Qt::endl; |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | class QSvgGeneratorPrivate |
| 549 | { |
| 550 | public: |
| 551 | QSvgPaintEngine *engine; |
| 552 | |
| 553 | uint owns_iodevice : 1; |
| 554 | QString fileName; |
| 555 | }; |
| 556 | |
| 557 | /*! |
| 558 | \class QSvgGenerator |
| 559 | \ingroup painting |
| 560 | \inmodule QtSvg |
| 561 | \since 4.3 |
| 562 | \brief The QSvgGenerator class provides a paint device that is used to create SVG drawings. |
| 563 | \reentrant |
| 564 | |
| 565 | This paint device represents a Scalable Vector Graphics (SVG) drawing. Like QPrinter, it is |
| 566 | designed as a write-only device that generates output in a specific format. |
| 567 | |
| 568 | To write an SVG file, you first need to configure the output by setting the \l fileName |
| 569 | or \l outputDevice properties. It is usually necessary to specify the size of the drawing |
| 570 | by setting the \l size property, and in some cases where the drawing will be included in |
| 571 | another, the \l viewBox property also needs to be set. |
| 572 | |
| 573 | \snippet svggenerator/window.cpp configure SVG generator |
| 574 | |
| 575 | Other meta-data can be specified by setting the \a title, \a description and \a resolution |
| 576 | properties. |
| 577 | |
| 578 | As with other QPaintDevice subclasses, a QPainter object is used to paint onto an instance |
| 579 | of this class: |
| 580 | |
| 581 | \snippet svggenerator/window.cpp begin painting |
| 582 | \dots |
| 583 | \snippet svggenerator/window.cpp end painting |
| 584 | |
| 585 | Painting is performed in the same way as for any other paint device. However, |
| 586 | it is necessary to use the QPainter::begin() and \l{QPainter::}{end()} to |
| 587 | explicitly begin and end painting on the device. |
| 588 | |
| 589 | \sa QSvgRenderer, QSvgWidget, {Qt SVG C++ Classes} |
| 590 | */ |
| 591 | |
| 592 | /*! |
| 593 | \enum QSvgGenerator::SvgVersion |
| 594 | \since 6.5 |
| 595 | |
| 596 | This enumeration describes the version of the SVG output of the |
| 597 | generator. |
| 598 | |
| 599 | \value SvgTiny12 The generated document follows the SVG Tiny 1.2 specification. |
| 600 | \value Svg11 The generated document follows the SVG 1.1 specification. |
| 601 | */ |
| 602 | |
| 603 | /*! |
| 604 | Constructs a new generator using the SVG Tiny 1.2 profile. |
| 605 | */ |
| 606 | QSvgGenerator::QSvgGenerator() // ### Qt 7: inline |
| 607 | : QSvgGenerator(SvgVersion::SvgTiny12) |
| 608 | { |
| 609 | } |
| 610 | |
| 611 | /*! |
| 612 | \since 6.5 |
| 613 | |
| 614 | Constructs a new generator that uses the SVG version \a version. |
| 615 | */ |
| 616 | QSvgGenerator::QSvgGenerator(SvgVersion version) |
| 617 | : d_ptr(new QSvgGeneratorPrivate) |
| 618 | { |
| 619 | Q_D(QSvgGenerator); |
| 620 | |
| 621 | d->engine = new QSvgPaintEngine(version); |
| 622 | d->owns_iodevice = false; |
| 623 | } |
| 624 | |
| 625 | /*! |
| 626 | Destroys the generator. |
| 627 | */ |
| 628 | QSvgGenerator::~QSvgGenerator() |
| 629 | { |
| 630 | Q_D(QSvgGenerator); |
| 631 | if (d->owns_iodevice) |
| 632 | delete d->engine->outputDevice(); |
| 633 | delete d->engine; |
| 634 | } |
| 635 | |
| 636 | /*! |
| 637 | \property QSvgGenerator::title |
| 638 | \brief the title of the generated SVG drawing |
| 639 | \since 4.5 |
| 640 | \sa description |
| 641 | */ |
| 642 | QString QSvgGenerator::title() const |
| 643 | { |
| 644 | Q_D(const QSvgGenerator); |
| 645 | |
| 646 | return d->engine->documentTitle(); |
| 647 | } |
| 648 | |
| 649 | void QSvgGenerator::setTitle(const QString &title) |
| 650 | { |
| 651 | Q_D(QSvgGenerator); |
| 652 | |
| 653 | d->engine->setDocumentTitle(title); |
| 654 | } |
| 655 | |
| 656 | /*! |
| 657 | \property QSvgGenerator::description |
| 658 | \brief the description of the generated SVG drawing |
| 659 | \since 4.5 |
| 660 | \sa title |
| 661 | */ |
| 662 | QString QSvgGenerator::description() const |
| 663 | { |
| 664 | Q_D(const QSvgGenerator); |
| 665 | |
| 666 | return d->engine->documentDescription(); |
| 667 | } |
| 668 | |
| 669 | void QSvgGenerator::setDescription(const QString &description) |
| 670 | { |
| 671 | Q_D(QSvgGenerator); |
| 672 | |
| 673 | d->engine->setDocumentDescription(description); |
| 674 | } |
| 675 | |
| 676 | /*! |
| 677 | \property QSvgGenerator::size |
| 678 | \brief the size of the generated SVG drawing |
| 679 | \since 4.5 |
| 680 | |
| 681 | By default this property is set to \c{QSize(-1, -1)}, which |
| 682 | indicates that the generator should not output the width and |
| 683 | height attributes of the \c<svg> element. |
| 684 | |
| 685 | \note It is not possible to change this property while a |
| 686 | QPainter is active on the generator. |
| 687 | |
| 688 | \sa viewBox, resolution |
| 689 | */ |
| 690 | QSize QSvgGenerator::size() const |
| 691 | { |
| 692 | Q_D(const QSvgGenerator); |
| 693 | return d->engine->size(); |
| 694 | } |
| 695 | |
| 696 | void QSvgGenerator::setSize(const QSize &size) |
| 697 | { |
| 698 | Q_D(QSvgGenerator); |
| 699 | if (d->engine->isActive()) { |
| 700 | qWarning(msg: "QSvgGenerator::setSize(), cannot set size while SVG is being generated" ); |
| 701 | return; |
| 702 | } |
| 703 | d->engine->setSize(size); |
| 704 | } |
| 705 | |
| 706 | /*! |
| 707 | \property QSvgGenerator::viewBox |
| 708 | \brief the viewBox of the generated SVG drawing |
| 709 | \since 4.5 |
| 710 | |
| 711 | By default this property is set to \c{QRect(0, 0, -1, -1)}, which |
| 712 | indicates that the generator should not output the viewBox attribute |
| 713 | of the \c<svg> element. |
| 714 | |
| 715 | \note It is not possible to change this property while a |
| 716 | QPainter is active on the generator. |
| 717 | |
| 718 | \sa viewBox(), size, resolution |
| 719 | */ |
| 720 | QRectF QSvgGenerator::viewBoxF() const |
| 721 | { |
| 722 | Q_D(const QSvgGenerator); |
| 723 | return d->engine->viewBox(); |
| 724 | } |
| 725 | |
| 726 | /*! |
| 727 | \since 4.5 |
| 728 | |
| 729 | Returns viewBoxF().toRect(). |
| 730 | |
| 731 | \sa viewBoxF() |
| 732 | */ |
| 733 | QRect QSvgGenerator::viewBox() const |
| 734 | { |
| 735 | Q_D(const QSvgGenerator); |
| 736 | return d->engine->viewBox().toRect(); |
| 737 | } |
| 738 | |
| 739 | void QSvgGenerator::setViewBox(const QRectF &viewBox) |
| 740 | { |
| 741 | Q_D(QSvgGenerator); |
| 742 | if (d->engine->isActive()) { |
| 743 | qWarning(msg: "QSvgGenerator::setViewBox(), cannot set viewBox while SVG is being generated" ); |
| 744 | return; |
| 745 | } |
| 746 | d->engine->setViewBox(viewBox); |
| 747 | } |
| 748 | |
| 749 | void QSvgGenerator::setViewBox(const QRect &viewBox) |
| 750 | { |
| 751 | setViewBox(QRectF(viewBox)); |
| 752 | } |
| 753 | |
| 754 | /*! |
| 755 | \property QSvgGenerator::fileName |
| 756 | \brief the target filename for the generated SVG drawing |
| 757 | \since 4.5 |
| 758 | |
| 759 | \sa outputDevice |
| 760 | */ |
| 761 | QString QSvgGenerator::fileName() const |
| 762 | { |
| 763 | Q_D(const QSvgGenerator); |
| 764 | return d->fileName; |
| 765 | } |
| 766 | |
| 767 | void QSvgGenerator::setFileName(const QString &fileName) |
| 768 | { |
| 769 | Q_D(QSvgGenerator); |
| 770 | if (d->engine->isActive()) { |
| 771 | qWarning(msg: "QSvgGenerator::setFileName(), cannot set file name while SVG is being generated" ); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | if (d->owns_iodevice) |
| 776 | delete d->engine->outputDevice(); |
| 777 | |
| 778 | d->owns_iodevice = true; |
| 779 | |
| 780 | d->fileName = fileName; |
| 781 | QFile *file = new QFile(fileName); |
| 782 | d->engine->setOutputDevice(file); |
| 783 | } |
| 784 | |
| 785 | /*! |
| 786 | \property QSvgGenerator::outputDevice |
| 787 | \brief the output device for the generated SVG drawing |
| 788 | \since 4.5 |
| 789 | |
| 790 | If both output device and file name are specified, the output device |
| 791 | will have precedence. |
| 792 | |
| 793 | \sa fileName |
| 794 | */ |
| 795 | QIODevice *QSvgGenerator::outputDevice() const |
| 796 | { |
| 797 | Q_D(const QSvgGenerator); |
| 798 | return d->engine->outputDevice(); |
| 799 | } |
| 800 | |
| 801 | void QSvgGenerator::setOutputDevice(QIODevice *outputDevice) |
| 802 | { |
| 803 | Q_D(QSvgGenerator); |
| 804 | if (d->engine->isActive()) { |
| 805 | qWarning(msg: "QSvgGenerator::setOutputDevice(), cannot set output device while SVG is being generated" ); |
| 806 | return; |
| 807 | } |
| 808 | d->owns_iodevice = false; |
| 809 | d->engine->setOutputDevice(outputDevice); |
| 810 | d->fileName = QString(); |
| 811 | } |
| 812 | |
| 813 | /*! |
| 814 | \property QSvgGenerator::resolution |
| 815 | \brief the resolution of the generated output |
| 816 | \since 4.5 |
| 817 | |
| 818 | The resolution is specified in dots per inch, and is used to |
| 819 | calculate the physical size of an SVG drawing. |
| 820 | |
| 821 | \sa size, viewBox |
| 822 | */ |
| 823 | int QSvgGenerator::resolution() const |
| 824 | { |
| 825 | Q_D(const QSvgGenerator); |
| 826 | return d->engine->resolution(); |
| 827 | } |
| 828 | |
| 829 | void QSvgGenerator::setResolution(int dpi) |
| 830 | { |
| 831 | Q_D(QSvgGenerator); |
| 832 | d->engine->setResolution(dpi); |
| 833 | } |
| 834 | |
| 835 | /*! |
| 836 | \since 6.5 |
| 837 | |
| 838 | Returns the version of the SVG document that this generator is |
| 839 | producing. |
| 840 | */ |
| 841 | QSvgGenerator::SvgVersion QSvgGenerator::svgVersion() const |
| 842 | { |
| 843 | Q_D(const QSvgGenerator); |
| 844 | return d->engine->svgVersion(); |
| 845 | } |
| 846 | |
| 847 | /*! |
| 848 | Returns the paint engine used to render graphics to be converted to SVG |
| 849 | format information. |
| 850 | */ |
| 851 | QPaintEngine *QSvgGenerator::paintEngine() const |
| 852 | { |
| 853 | Q_D(const QSvgGenerator); |
| 854 | return d->engine; |
| 855 | } |
| 856 | |
| 857 | /*! |
| 858 | \reimp |
| 859 | */ |
| 860 | int QSvgGenerator::metric(QPaintDevice::PaintDeviceMetric metric) const |
| 861 | { |
| 862 | Q_D(const QSvgGenerator); |
| 863 | switch (metric) { |
| 864 | case QPaintDevice::PdmDepth: |
| 865 | return 32; |
| 866 | case QPaintDevice::PdmWidth: |
| 867 | return d->engine->size().width(); |
| 868 | case QPaintDevice::PdmHeight: |
| 869 | return d->engine->size().height(); |
| 870 | case QPaintDevice::PdmDpiX: |
| 871 | return d->engine->resolution(); |
| 872 | case QPaintDevice::PdmDpiY: |
| 873 | return d->engine->resolution(); |
| 874 | case QPaintDevice::PdmHeightMM: |
| 875 | return qRound(d: d->engine->size().height() * 25.4 / d->engine->resolution()); |
| 876 | case QPaintDevice::PdmWidthMM: |
| 877 | return qRound(d: d->engine->size().width() * 25.4 / d->engine->resolution()); |
| 878 | case QPaintDevice::PdmNumColors: |
| 879 | return 0xffffffff; |
| 880 | case QPaintDevice::PdmPhysicalDpiX: |
| 881 | return d->engine->resolution(); |
| 882 | case QPaintDevice::PdmPhysicalDpiY: |
| 883 | return d->engine->resolution(); |
| 884 | case QPaintDevice::PdmDevicePixelRatio: |
| 885 | return 1; |
| 886 | case QPaintDevice::PdmDevicePixelRatioScaled: |
| 887 | return 1 * QPaintDevice::devicePixelRatioFScale(); |
| 888 | default: |
| 889 | qWarning(msg: "QSvgGenerator::metric(), unhandled metric %d\n" , metric); |
| 890 | break; |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /***************************************************************************** |
| 896 | * class QSvgPaintEngine |
| 897 | */ |
| 898 | |
| 899 | bool QSvgPaintEngine::begin(QPaintDevice *) |
| 900 | { |
| 901 | Q_D(QSvgPaintEngine); |
| 902 | if (!d->outputDevice) { |
| 903 | qWarning(msg: "QSvgPaintEngine::begin(), no output device" ); |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | if (!d->outputDevice->isOpen()) { |
| 908 | if (!d->outputDevice->open(mode: QIODevice::WriteOnly | QIODevice::Text)) { |
| 909 | qWarning(msg: "QSvgPaintEngine::begin(), could not open output device: '%s'" , |
| 910 | qPrintable(d->outputDevice->errorString())); |
| 911 | return false; |
| 912 | } |
| 913 | } else if (!d->outputDevice->isWritable()) { |
| 914 | qWarning(msg: "QSvgPaintEngine::begin(), could not write to read-only output device: '%s'" , |
| 915 | qPrintable(d->outputDevice->errorString())); |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | d->stream = new QTextStream(&d->header); |
| 920 | |
| 921 | // stream out the header... |
| 922 | *d->stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << Qt::endl << "<svg" ; |
| 923 | |
| 924 | if (d->size.isValid()) { |
| 925 | qreal wmm = d->size.width() * 25.4 / d->resolution; |
| 926 | qreal hmm = d->size.height() * 25.4 / d->resolution; |
| 927 | *d->stream << " width=\"" << wmm << "mm\" height=\"" << hmm << "mm\"" << Qt::endl; |
| 928 | } |
| 929 | |
| 930 | if (d->viewBox.isValid()) { |
| 931 | *d->stream << " viewBox=\"" << d->viewBox.left() << ' ' << d->viewBox.top(); |
| 932 | *d->stream << ' ' << d->viewBox.width() << ' ' << d->viewBox.height() << '\"' << Qt::endl; |
| 933 | } |
| 934 | |
| 935 | *d->stream << " xmlns=\"http://www.w3.org/2000/svg\"" |
| 936 | " xmlns:xlink=\"http://www.w3.org/1999/xlink\"" ; |
| 937 | switch (d->svgVersion) { |
| 938 | case QSvgGenerator::SvgVersion::SvgTiny12: |
| 939 | *d->stream << " version=\"1.2\" baseProfile=\"tiny\">" ; |
| 940 | break; |
| 941 | case QSvgGenerator::SvgVersion::Svg11: |
| 942 | *d->stream << " version=\"1.1\">" ; |
| 943 | break; |
| 944 | } |
| 945 | *d->stream << Qt::endl; |
| 946 | |
| 947 | if (!d->attributes.document_title.isEmpty()) { |
| 948 | *d->stream << "<title>" << d->attributes.document_title.toHtmlEscaped() << "</title>" << Qt::endl; |
| 949 | } |
| 950 | |
| 951 | if (!d->attributes.document_description.isEmpty()) { |
| 952 | *d->stream << "<desc>" << d->attributes.document_description.toHtmlEscaped() << "</desc>" << Qt::endl; |
| 953 | } |
| 954 | |
| 955 | d->stream->setString(string: &d->defs); |
| 956 | *d->stream << "<defs>\n" ; |
| 957 | |
| 958 | d->stream->setString(string: &d->body); |
| 959 | // Start the initial graphics state... |
| 960 | *d->stream << "<g " ; |
| 961 | generateQtDefaults(); |
| 962 | *d->stream << Qt::endl; |
| 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | bool QSvgPaintEngine::end() |
| 968 | { |
| 969 | Q_D(QSvgPaintEngine); |
| 970 | |
| 971 | d->stream->setString(string: &d->defs); |
| 972 | *d->stream << "</defs>\n" ; |
| 973 | |
| 974 | d->stream->setDevice(d->outputDevice); |
| 975 | |
| 976 | *d->stream << d->header; |
| 977 | *d->stream << d->defs; |
| 978 | *d->stream << d->body; |
| 979 | if (d->hasEmittedClipGroup) |
| 980 | *d->stream << "</g>" ; |
| 981 | if (d->afterFirstUpdate) |
| 982 | *d->stream << "</g>" << Qt::endl; // close the updateState |
| 983 | |
| 984 | *d->stream << "</g>" << Qt::endl // close the Qt defaults |
| 985 | << "</svg>" << Qt::endl; |
| 986 | |
| 987 | delete d->stream; |
| 988 | |
| 989 | return true; |
| 990 | } |
| 991 | |
| 992 | void QSvgPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, |
| 993 | const QRectF &sr) |
| 994 | { |
| 995 | drawImage(r, pm: pm.toImage(), sr); |
| 996 | } |
| 997 | |
| 998 | void QSvgPaintEngine::generateImage(QTextStream &stream, const QRectF &r, const QImage &image) |
| 999 | { |
| 1000 | QString quality; |
| 1001 | if (state->renderHints() & QPainter::SmoothPixmapTransform) { |
| 1002 | quality = QLatin1String("optimizeQuality" ); |
| 1003 | } else { |
| 1004 | quality = QLatin1String("optimizeSpeed" ); |
| 1005 | } |
| 1006 | stream << "<image " ; |
| 1007 | stream << "x=\"" <<r.x()<<"\" " |
| 1008 | "y=\"" <<r.y()<<"\" " |
| 1009 | "width=\"" <<r.width()<<"\" " |
| 1010 | "height=\"" <<r.height()<<"\" " |
| 1011 | "preserveAspectRatio=\"none\" " |
| 1012 | "image-rendering=\"" <<quality<<"\" " ; |
| 1013 | |
| 1014 | QByteArray data; |
| 1015 | QBuffer buffer(&data); |
| 1016 | buffer.open(openMode: QBuffer::ReadWrite); |
| 1017 | image.save(device: &buffer, format: "PNG" ); |
| 1018 | buffer.close(); |
| 1019 | stream << "xlink:href=\"data:image/png;base64," |
| 1020 | << data.toBase64() |
| 1021 | <<"\" />\n" ; |
| 1022 | } |
| 1023 | |
| 1024 | void QSvgPaintEngine::drawImage(const QRectF &r, const QImage &image, |
| 1025 | const QRectF &sr, |
| 1026 | Qt::ImageConversionFlags flags) |
| 1027 | { |
| 1028 | Q_UNUSED(sr); |
| 1029 | Q_UNUSED(flags); |
| 1030 | generateImage(stream&: stream(), r, image); |
| 1031 | } |
| 1032 | |
| 1033 | void QSvgPaintEngine::updateState(const QPaintEngineState &state) |
| 1034 | { |
| 1035 | Q_D(QSvgPaintEngine); |
| 1036 | // always stream full gstate, which is not required, but... |
| 1037 | |
| 1038 | // close old state and start a new one... |
| 1039 | if (d->hasEmittedClipGroup) |
| 1040 | *d->stream << "</g>\n" ; |
| 1041 | if (d->afterFirstUpdate) |
| 1042 | *d->stream << "</g>\n\n" ; |
| 1043 | |
| 1044 | updateClipState(state); |
| 1045 | |
| 1046 | if (d->isClippingEffective()) { |
| 1047 | *d->stream << QStringLiteral("<g clip-path=\"url(#%1)\">" ).arg(a: d->currentClipPathName); |
| 1048 | d->hasEmittedClipGroup = true; |
| 1049 | } else { |
| 1050 | d->hasEmittedClipGroup = false; |
| 1051 | } |
| 1052 | |
| 1053 | *d->stream << "<g " ; |
| 1054 | |
| 1055 | qbrushToSvg(sbrush: state.brush()); |
| 1056 | qpenToSvg(spen: state.pen()); |
| 1057 | |
| 1058 | d->matrix = state.transform(); |
| 1059 | *d->stream << "transform=\"matrix(" << d->matrix.m11() << ',' |
| 1060 | << d->matrix.m12() << ',' |
| 1061 | << d->matrix.m21() << ',' << d->matrix.m22() << ',' |
| 1062 | << d->matrix.dx() << ',' << d->matrix.dy() |
| 1063 | << ")\"" |
| 1064 | << Qt::endl; |
| 1065 | |
| 1066 | qfontToSvg(sfont: state.font()); |
| 1067 | |
| 1068 | if (!qFuzzyIsNull(d: state.opacity() - 1)) |
| 1069 | stream() << "opacity=\"" <<state.opacity()<<"\" " ; |
| 1070 | |
| 1071 | *d->stream << '>' << Qt::endl; |
| 1072 | |
| 1073 | d->afterFirstUpdate = true; |
| 1074 | } |
| 1075 | |
| 1076 | void QSvgPaintEngine::updateClipState(const QPaintEngineState &state) |
| 1077 | { |
| 1078 | Q_D(QSvgPaintEngine); |
| 1079 | switch (d->svgVersion) { |
| 1080 | case QSvgGenerator::SvgVersion::SvgTiny12: |
| 1081 | // no clip handling in Tiny 1.2 |
| 1082 | return; |
| 1083 | case QSvgGenerator::SvgVersion::Svg11: |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | const QPaintEngine::DirtyFlags flags = state.state(); |
| 1088 | |
| 1089 | const bool clippingChanged = flags.testAnyFlags(flags: DirtyClipPath | DirtyClipRegion); |
| 1090 | if (clippingChanged) { |
| 1091 | switch (state.clipOperation()) { |
| 1092 | case Qt::NoClip: |
| 1093 | d->clipEnabled = false; |
| 1094 | d->clipPath.reset(); |
| 1095 | break; |
| 1096 | case Qt::ReplaceClip: |
| 1097 | case Qt::IntersectClip: |
| 1098 | d->clipPath = painter()->transform().map(p: painter()->clipPath()); |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (flags & DirtyClipEnabled) |
| 1104 | d->clipEnabled = state.isClipEnabled(); |
| 1105 | |
| 1106 | if (d->isClippingEffective() && clippingChanged) { |
| 1107 | d->stream->setString(string: &d->defs); |
| 1108 | *d->stream << QLatin1String("<clipPath id=\"%1\">\n" ).arg(args: d->generateClipPathName()); |
| 1109 | drawPath(path: *d->clipPath); |
| 1110 | *d->stream << "</clipPath>\n" ; |
| 1111 | d->stream->setString(string: &d->body); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | void QSvgPaintEngine::drawEllipse(const QRectF &r) |
| 1116 | { |
| 1117 | Q_D(QSvgPaintEngine); |
| 1118 | |
| 1119 | const bool isCircle = r.width() == r.height(); |
| 1120 | *d->stream << '<' << (isCircle ? "circle" : "ellipse" ); |
| 1121 | if (state->pen().isCosmetic()) |
| 1122 | *d->stream << " vector-effect=\"non-scaling-stroke\"" ; |
| 1123 | const QPointF c = r.center(); |
| 1124 | *d->stream << " cx=\"" << c.x() << "\" cy=\"" << c.y(); |
| 1125 | if (isCircle) |
| 1126 | *d->stream << "\" r=\"" << r.width() / qreal(2.0); |
| 1127 | else |
| 1128 | *d->stream << "\" rx=\"" << r.width() / qreal(2.0) << "\" ry=\"" << r.height() / qreal(2.0); |
| 1129 | *d->stream << "\"/>" << Qt::endl; |
| 1130 | } |
| 1131 | |
| 1132 | void QSvgPaintEngine::drawPath(const QPainterPath &p) |
| 1133 | { |
| 1134 | Q_D(QSvgPaintEngine); |
| 1135 | |
| 1136 | *d->stream << "<path vector-effect=\"" |
| 1137 | << (state->pen().isCosmetic() ? "non-scaling-stroke" : "none" ) |
| 1138 | << "\" fill-rule=\"" |
| 1139 | << (p.fillRule() == Qt::OddEvenFill ? "evenodd" : "nonzero" ) |
| 1140 | << "\" d=\"" ; |
| 1141 | |
| 1142 | for (int i=0; i<p.elementCount(); ++i) { |
| 1143 | const QPainterPath::Element &e = p.elementAt(i); |
| 1144 | switch (e.type) { |
| 1145 | case QPainterPath::MoveToElement: |
| 1146 | *d->stream << 'M' << e.x << ',' << e.y; |
| 1147 | break; |
| 1148 | case QPainterPath::LineToElement: |
| 1149 | *d->stream << 'L' << e.x << ',' << e.y; |
| 1150 | break; |
| 1151 | case QPainterPath::CurveToElement: |
| 1152 | *d->stream << 'C' << e.x << ',' << e.y; |
| 1153 | ++i; |
| 1154 | while (i < p.elementCount()) { |
| 1155 | const QPainterPath::Element &e = p.elementAt(i); |
| 1156 | if (e.type != QPainterPath::CurveToDataElement) { |
| 1157 | --i; |
| 1158 | break; |
| 1159 | } else |
| 1160 | *d->stream << ' '; |
| 1161 | *d->stream << e.x << ',' << e.y; |
| 1162 | ++i; |
| 1163 | } |
| 1164 | break; |
| 1165 | default: |
| 1166 | break; |
| 1167 | } |
| 1168 | if (i != p.elementCount() - 1) { |
| 1169 | *d->stream << ' '; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | *d->stream << "\"/>" << Qt::endl; |
| 1174 | } |
| 1175 | |
| 1176 | void QSvgPaintEngine::drawPolygon(const QPointF *points, int pointCount, |
| 1177 | PolygonDrawMode mode) |
| 1178 | { |
| 1179 | Q_ASSERT(pointCount >= 2); |
| 1180 | |
| 1181 | //Q_D(QSvgPaintEngine); |
| 1182 | |
| 1183 | QPainterPath path(points[0]); |
| 1184 | for (int i=1; i<pointCount; ++i) |
| 1185 | path.lineTo(p: points[i]); |
| 1186 | |
| 1187 | if (mode == PolylineMode) { |
| 1188 | stream() << "<polyline fill=\"none\" vector-effect=\"" |
| 1189 | << (state->pen().isCosmetic() ? "non-scaling-stroke" : "none" ) |
| 1190 | << "\" points=\"" ; |
| 1191 | for (int i = 0; i < pointCount; ++i) { |
| 1192 | const QPointF &pt = points[i]; |
| 1193 | stream() << pt.x() << ',' << pt.y() << ' '; |
| 1194 | } |
| 1195 | stream() << "\" />" <<Qt::endl; |
| 1196 | } else { |
| 1197 | path.closeSubpath(); |
| 1198 | drawPath(p: path); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void QSvgPaintEngine::drawRects(const QRectF *rects, int rectCount) |
| 1203 | { |
| 1204 | Q_D(QSvgPaintEngine); |
| 1205 | |
| 1206 | for (int i=0; i < rectCount; ++i) { |
| 1207 | const QRectF &rect = rects[i].normalized(); |
| 1208 | *d->stream << "<rect" ; |
| 1209 | if (state->pen().isCosmetic()) |
| 1210 | *d->stream << " vector-effect=\"non-scaling-stroke\"" ; |
| 1211 | *d->stream << " x=\"" << rect.x() << "\" y=\"" << rect.y() |
| 1212 | << "\" width=\"" << rect.width() << "\" height=\"" << rect.height() |
| 1213 | << "\"/>" << Qt::endl; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | void QSvgPaintEngine::drawTextItem(const QPointF &pt, const QTextItem &textItem) |
| 1218 | { |
| 1219 | Q_D(QSvgPaintEngine); |
| 1220 | if (d->pen.style() == Qt::NoPen) |
| 1221 | return; |
| 1222 | |
| 1223 | const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem); |
| 1224 | if (ti.chars == 0) |
| 1225 | QPaintEngine::drawTextItem(p: pt, textItem: ti); // Draw as path |
| 1226 | QString s = QString::fromRawData(ti.chars, size: ti.num_chars); |
| 1227 | |
| 1228 | *d->stream << "<text " |
| 1229 | "fill=\"" << d->attributes.stroke << "\" " |
| 1230 | "fill-opacity=\"" << d->attributes.strokeOpacity << "\" " |
| 1231 | "stroke=\"none\" " |
| 1232 | "xml:space=\"preserve\" " |
| 1233 | "x=\"" << pt.x() << "\" y=\"" << pt.y() << "\" " ; |
| 1234 | qfontToSvg(sfont: textItem.font()); |
| 1235 | *d->stream << " >" |
| 1236 | << s.toHtmlEscaped() |
| 1237 | << "</text>" |
| 1238 | << Qt::endl; |
| 1239 | } |
| 1240 | |
| 1241 | QT_END_NAMESPACE |
| 1242 | |
| 1243 | #endif // QT_NO_SVGGENERATOR |
| 1244 | |