23using namespace Qt::StringLiterals;
25QSvgDocument::QSvgDocument(QtSvg::Options options, QtSvg::AnimatorType type)
26 : QSvgStructureNode(0)
27 , m_widthPercent(
false)
28 , m_heightPercent(
false)
33 m_states.trustedSource = m_options.testFlag(QtSvg::AssumeTrustedSource);
34 bool animationEnabled = !m_options.testFlag(QtSvg::DisableAnimations);
36 case QtSvg::AnimatorType::Automatic:
38 m_animator.reset(
new QSvgAnimator);
40 case QtSvg::AnimatorType::Controlled:
42 m_animator.reset(
new QSvgAnimationController);
46QSvgDocument::~QSvgDocument()
52 QString h = s.readAll();
53 QStringView th = QStringView(h).trimmed();
55 if (th.startsWith(
"<svg"_L1) || th.startsWith(
"<!DOCTYPE svg"_L1))
57 else if (th.startsWith(
"<?xml"_L1) || th.startsWith(
"<!--"_L1))
58 matched = th.contains(
"<!DOCTYPE svg"_L1) || th.contains(
"<svg"_L1);
64# ifdef QT_BUILD_INTERNAL
65Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(QIODevice *device)
67 return qt_inflateSvgzDataFrom(device,
false);
76 if (!device->isOpen())
77 device->open(QIODevice::ReadOnly);
79 Q_ASSERT(device->isOpen() && device->isReadable());
81 static const int CHUNK_SIZE = 4096;
82 int zlibResult = Z_OK;
89 zlibStream.next_in = Z_NULL;
90 zlibStream.avail_in = 0;
91 zlibStream.avail_out = 0;
92 zlibStream.zalloc = Z_NULL;
93 zlibStream.zfree = Z_NULL;
94 zlibStream.opaque = Z_NULL;
97 if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK) {
98 qCWarning(lcSvgHandler,
"Cannot initialize zlib, because: %s",
99 (zlibStream.msg != NULL ? zlibStream.msg :
"Unknown error"));
103 bool stillMoreWorkToDo =
true;
104 while (stillMoreWorkToDo) {
106 if (!zlibStream.avail_in) {
107 source = device->read(CHUNK_SIZE);
109 if (source.isEmpty())
112 zlibStream.avail_in = source.size();
113 zlibStream.next_in =
reinterpret_cast<Bytef*>(source.data());
118 int oldSize = destination.size();
119 if (oldSize > INT_MAX - CHUNK_SIZE) {
120 inflateEnd(&zlibStream);
121 qCWarning(lcSvgHandler,
"Error while inflating gzip file: integer size overflow");
125 destination.resize(oldSize + CHUNK_SIZE);
126 zlibStream.next_out =
reinterpret_cast<Bytef*>(
127 destination.data() + oldSize - zlibStream.avail_out);
128 zlibStream.avail_out += CHUNK_SIZE;
130 zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
131 switch (zlibResult) {
136 inflateEnd(&zlibStream);
137 qCWarning(lcSvgHandler,
"Error while inflating gzip file: %s",
138 (zlibStream.msg != NULL ? zlibStream.msg :
"Unknown error"));
145 }
while (!zlibStream.avail_out);
147 if (doCheckContent) {
149 const qsizetype destinationContents =
std::min(destination.size(),
static_cast<qsizetype>(zlibStream.total_out));
150 Q_ASSERT(destinationContents ==
static_cast<qsizetype>(zlibStream.total_out));
151 if (!hasSvgHeader(QByteArray::fromRawData(destination.constData(), destinationContents))) {
152 inflateEnd(&zlibStream);
153 qCWarning(lcSvgHandler,
"Error while inflating gzip file: SVG format check failed");
156 doCheckContent =
false;
159 if (zlibResult == Z_STREAM_END) {
161 if (!(zlibStream.avail_in && inflateReset(&zlibStream) == Z_OK))
162 stillMoreWorkToDo =
false;
167 destination.chop(zlibStream.avail_out);
169 inflateEnd(&zlibStream);
173static QByteArray qt_inflateSvgzDataFrom(QIODevice *)
179std::unique_ptr<QSvgDocument> QSvgDocument::load(
const QString &fileName, QtSvg::Options options,
180 QtSvg::AnimatorType type)
182 std::unique_ptr<QSvgDocument> doc;
183 QFile file(fileName);
184 if (!file.open(QFile::ReadOnly)) {
185 qCWarning(lcSvgHandler,
"Cannot open file '%s', because: %s",
190 if (fileName.endsWith(QLatin1String(
".svgz"), Qt::CaseInsensitive)
191 || fileName.endsWith(QLatin1String(
".svg.gz"), Qt::CaseInsensitive)) {
192 return load(qt_inflateSvgzDataFrom(&file));
195 QSvgHandler handler(&file, options, type);
197 doc.reset(handler.document());
199 doc->m_animator->setAnimationDuration(handler.animationDuration());
201 qCWarning(lcSvgHandler,
"Cannot read file '%s', because: %s (line %d)",
203 delete handler.document();
208std::unique_ptr<QSvgDocument> QSvgDocument::load(
const QByteArray &contents, QtSvg::Options options,
209 QtSvg::AnimatorType type)
211 std::unique_ptr<QSvgDocument> doc;
214 if (contents.startsWith(
"\x1f\x8b")) {
216 buffer.setData(contents);
217 svg = qt_inflateSvgzDataFrom(&buffer);
226 buffer.open(QIODevice::ReadOnly);
227 QSvgHandler handler(&buffer, options, type);
230 doc.reset(handler.document());
232 doc->m_animator->setAnimationDuration(handler.animationDuration());
234 delete handler.document();
239std::unique_ptr<QSvgDocument> QSvgDocument::load(QXmlStreamReader *contents, QtSvg::Options options,
240 QtSvg::AnimatorType type)
242 QSvgHandler handler(contents, options, type);
244 std::unique_ptr<QSvgDocument> doc;
246 doc.reset(handler.document());
248 doc->m_animator->setAnimationDuration(handler.animationDuration());
250 delete handler.document();
255void QSvgDocument::draw(QPainter *p,
const QRectF &bounds)
257 if (displayMode() == QSvgNode::NoneMode)
263 mapSourceToTarget(p, bounds);
265 applyStyle(p, m_states);
266 for (
const auto &node : renderers()) {
267 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
268 node->draw(p, m_states);
270 revertStyle(p, m_states);
275void QSvgDocument::draw(QPainter *p,
const QString &id,
276 const QRectF &bounds)
278 QSvgNode *node = scopeNode(id);
281 qCDebug(lcSvgHandler,
"Couldn't find node %s. Skipping rendering.",
qPrintable(id));
285 if (node->displayMode() == QSvgNode::NoneMode)
290 const QRectF elementBounds = node->bounds();
292 mapSourceToTarget(p, bounds, elementBounds);
293 QTransform originalTransform = p->worldTransform();
296 QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
297 pen.setMiterLimit(4);
299 p->setBrush(Qt::black);
300 p->setRenderHint(QPainter::Antialiasing);
301 p->setRenderHint(QPainter::SmoothPixmapTransform);
303 QStack<QSvgNode*> parentApplyStack;
304 QSvgNode *parent = node->parent();
306 parentApplyStack.push(parent);
307 parent = parent->parent();
310 for (
int i = parentApplyStack.size() - 1; i >= 0; --i)
311 parentApplyStack[i]->applyStyle(p, m_states);
315 QTransform currentTransform = p->worldTransform();
316 p->setWorldTransform(originalTransform);
318 node->draw(p, m_states);
320 p->setWorldTransform(currentTransform);
322 for (
int i = 0; i < parentApplyStack.size(); ++i)
323 parentApplyStack[i]->revertStyle(p, m_states);
330QSvgNode::Type QSvgDocument::type()
const
335void QSvgDocument::setWidth(
int len,
bool percent)
337 m_size.setWidth(len);
338 m_widthPercent = percent;
341void QSvgDocument::setHeight(
int len,
bool percent)
343 m_size.setHeight(len);
344 m_heightPercent = percent;
347void QSvgDocument::setPreserveAspectRatio(
bool on)
349 m_preserveAspectRatio = on;
352void QSvgDocument::setViewBox(
const QRectF &rect)
355 m_implicitViewBox = rect.isNull();
358QtSvg::Options QSvgDocument::options()
const
363void QSvgDocument::addSvgFont(QSvgFont *font)
365 m_fonts.insert(font->familyName(), font);
368QSvgFont * QSvgDocument::svgFont(
const QString &family)
const
370 return m_fonts[family];
373void QSvgDocument::addNamedNode(
const QString &id, QSvgNode *node)
375 m_namedNodes.insert(id, node);
378QSvgNode *QSvgDocument::namedNode(
const QString &id)
const
380 return m_namedNodes.value(id);
383void QSvgDocument::addPaintServer(QSvgPaintServerSharedPtr paintServer,
const QString &id)
388 if (!m_paintServers.insert({ id, paintServer } ).second)
389 qCWarning(lcSvgHandler) <<
"Duplicate unique style id:" << id;
392QSvgPaintServerSharedPtr QSvgDocument::paintServer(QStringView id)
const
394 auto it = m_paintServers.find(id.toString());
395 return it != m_paintServers.end() ? it->second :
nullptr;
398void QSvgDocument::restartAnimation()
401 m_animator->restartAnimation();
404bool QSvgDocument::animated()
const
409void QSvgDocument::setAnimated(
bool a)
414void QSvgDocument::draw(QPainter *p)
419void QSvgDocument::drawCommand(QPainter *, QSvgExtraStates &)
421 qCDebug(lcSvgHandler) <<
"SVG Tiny does not support nested <svg> elements: ignored.";
427 qreal determinant = transform.determinant();
428 return qIsFinite(determinant);
431void QSvgDocument::mapSourceToTarget(QPainter *p,
const QRectF &targetRect,
const QRectF &sourceRect)
433 QTransform oldTransform = p->worldTransform();
435 QRectF target = targetRect;
436 if (target.isEmpty()) {
437 QPaintDevice *dev = p->device();
438 QRectF deviceRect(0, 0, dev->width(), dev->height());
439 if (deviceRect.isEmpty()) {
440 if (sourceRect.isEmpty())
441 target = QRectF(QPointF(0, 0), size());
443 target = QRectF(QPointF(0, 0), sourceRect.size());
449 QRectF source = sourceRect;
450 if (source.isEmpty())
453 if (source != target && !qFuzzyIsNull(source.width()) && !qFuzzyIsNull(source.height())) {
454 if (m_implicitViewBox || !preserveAspectRatio()) {
456 QTransform transform;
457 transform.scale(target.width() / source.width(),
458 target.height() / source.height());
459 QRectF c2 = transform.mapRect(source);
460 p->translate(target.x() - c2.x(),
461 target.y() - c2.y());
462 p->scale(target.width() / source.width(),
463 target.height() / source.height());
469 QSizeF viewBoxSize = source.size();
470 viewBoxSize.scale(target.width(), target.height(), Qt::KeepAspectRatio);
473 p->translate(target.x() + (target.width() - viewBoxSize.width()) / 2,
474 target.y() + (target.height() - viewBoxSize.height()) / 2);
476 p->scale(viewBoxSize.width() / source.width(),
477 viewBoxSize.height() / source.height());
480 p->translate(-source.x(), -source.y());
484 if (!isValidMatrix(p->worldTransform()))
485 p->setWorldTransform(oldTransform);
488QRectF QSvgDocument::boundsOnElement(
const QString &id)
const
490 const QSvgNode *node = scopeNode(id);
493 return node->bounds();
496bool QSvgDocument::elementExists(
const QString &id)
const
498 QSvgNode *node = scopeNode(id);
503QTransform QSvgDocument::transformForElement(
const QString &id)
const
505 QSvgNode *node = scopeNode(id);
508 qCDebug(lcSvgHandler,
"Couldn't find node %s. Skipping rendering.",
qPrintable(id));
514 node = node->parent();
516 if (node->m_style.transform)
517 t *= node->m_style.transform->qtransform();
518 node = node->parent();
524int QSvgDocument::currentFrame()
const
526 const double runningPercentage = qMin(currentElapsed() /
double(animationDuration()), 1.);
527 const int totalFrames = m_fps * animationDuration() / 1000;
528 return int(runningPercentage * totalFrames);
531void QSvgDocument::setCurrentFrame(
int frame)
536 const int totalFrames = m_fps * animationDuration() / 1000;
537 if (totalFrames == 0)
540 const int timeForFrame = frame * animationDuration() / totalFrames;
541 const int timeToAdd = timeForFrame - currentElapsed();
542 m_animator->setAnimatorTime(timeToAdd);
545void QSvgDocument::setFramesPerSecond(
int num)
550QSharedPointer<QSvgAbstractAnimator> QSvgDocument::animator()
const
555bool QSvgDocument::isLikelySvg(QIODevice *device,
bool *isCompressed)
557 constexpr int bufSize = 4096;
559 char inflateBuf[bufSize];
560 bool useInflateBuf =
false;
561 int readLen = device->peek(buf, bufSize);
564#ifndef QT_NO_COMPRESS
565 if (quint8(buf[0]) == 0x1f && quint8(buf[1]) == 0x8b) {
568 zlibStream.avail_in = readLen;
569 zlibStream.next_out =
reinterpret_cast<Bytef *>(inflateBuf);
570 zlibStream.avail_out = bufSize;
571 zlibStream.next_in =
reinterpret_cast<Bytef *>(buf);
572 zlibStream.zalloc = Z_NULL;
573 zlibStream.zfree = Z_NULL;
574 zlibStream.opaque = Z_NULL;
575 if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK)
577 int zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
578 inflateEnd(&zlibStream);
579 if ((zlibResult != Z_OK && zlibResult != Z_STREAM_END) || zlibStream.total_out < 8)
581 readLen = zlibStream.total_out;
583 *isCompressed =
true;
584 useInflateBuf =
true;
587 return hasSvgHeader(QByteArray::fromRawData(useInflateBuf ? inflateBuf : buf, readLen));
#define qPrintable(string)
static QByteArray qt_inflateSvgzDataFrom(QIODevice *device, bool doCheckContent=true)
static bool isValidMatrix(const QTransform &transform)
static bool hasSvgHeader(const QByteArray &buf)