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()
50 if (!m_states.trustedSource)
57 QString h = s.readAll();
58 QStringView th = QStringView(h).trimmed();
60 if (th.startsWith(
"<svg"_L1) || th.startsWith(
"<!DOCTYPE svg"_L1))
62 else if (th.startsWith(
"<?xml"_L1) || th.startsWith(
"<!--"_L1))
63 matched = th.contains(
"<!DOCTYPE svg"_L1) || th.contains(
"<svg"_L1);
69# ifdef QT_BUILD_INTERNAL
70Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(QIODevice *device)
72 return qt_inflateSvgzDataFrom(device,
false);
81 if (!device->isOpen())
82 device->open(QIODevice::ReadOnly);
84 Q_ASSERT(device->isOpen() && device->isReadable());
86 static const int CHUNK_SIZE = 4096;
87 int zlibResult = Z_OK;
94 zlibStream.next_in = Z_NULL;
95 zlibStream.avail_in = 0;
96 zlibStream.avail_out = 0;
97 zlibStream.zalloc = Z_NULL;
98 zlibStream.zfree = Z_NULL;
99 zlibStream.opaque = Z_NULL;
102 if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK) {
103 qCWarning(lcSvgHandler,
"Cannot initialize zlib, because: %s",
104 (zlibStream.msg != NULL ? zlibStream.msg :
"Unknown error"));
108 bool stillMoreWorkToDo =
true;
109 while (stillMoreWorkToDo) {
111 if (!zlibStream.avail_in) {
112 source = device->read(CHUNK_SIZE);
114 if (source.isEmpty())
117 zlibStream.avail_in = source.size();
118 zlibStream.next_in =
reinterpret_cast<Bytef*>(source.data());
123 int oldSize = destination.size();
124 if (oldSize > INT_MAX - CHUNK_SIZE) {
125 inflateEnd(&zlibStream);
126 qCWarning(lcSvgHandler,
"Error while inflating gzip file: integer size overflow");
130 destination.resize(oldSize + CHUNK_SIZE);
131 zlibStream.next_out =
reinterpret_cast<Bytef*>(
132 destination.data() + oldSize - zlibStream.avail_out);
133 zlibStream.avail_out += CHUNK_SIZE;
135 zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
136 switch (zlibResult) {
141 inflateEnd(&zlibStream);
142 qCWarning(lcSvgHandler,
"Error while inflating gzip file: %s",
143 (zlibStream.msg != NULL ? zlibStream.msg :
"Unknown error"));
150 }
while (!zlibStream.avail_out);
152 if (doCheckContent) {
154 const qsizetype destinationContents =
std::min(destination.size(),
static_cast<qsizetype>(zlibStream.total_out));
155 Q_ASSERT(destinationContents ==
static_cast<qsizetype>(zlibStream.total_out));
156 if (!hasSvgHeader(QByteArray::fromRawData(destination.constData(), destinationContents))) {
157 inflateEnd(&zlibStream);
158 qCWarning(lcSvgHandler,
"Error while inflating gzip file: SVG format check failed");
161 doCheckContent =
false;
164 if (zlibResult == Z_STREAM_END) {
166 if (!(zlibStream.avail_in && inflateReset(&zlibStream) == Z_OK))
167 stillMoreWorkToDo =
false;
172 destination.chop(zlibStream.avail_out);
174 inflateEnd(&zlibStream);
178static QByteArray qt_inflateSvgzDataFrom(QIODevice *)
184std::unique_ptr<QSvgDocument> QSvgDocument::load(
const QString &fileName, QtSvg::Options options,
185 QtSvg::AnimatorType type)
187 std::unique_ptr<QSvgDocument> doc;
188 QFile file(fileName);
189 if (!file.open(QFile::ReadOnly)) {
190 qCWarning(lcSvgHandler,
"Cannot open file '%s', because: %s",
195 if (fileName.endsWith(QLatin1String(
".svgz"), Qt::CaseInsensitive)
196 || fileName.endsWith(QLatin1String(
".svg.gz"), Qt::CaseInsensitive)) {
197 return load(qt_inflateSvgzDataFrom(&file));
200 QSvgHandler handler(&file, options, type);
202 doc = handler.takeDocument();
204 doc->m_animator->setAnimationDuration(handler.animationDuration());
206 qCWarning(lcSvgHandler,
"Cannot read file '%s', because: %s (line %d)",
212std::unique_ptr<QSvgDocument> QSvgDocument::load(
const QByteArray &contents, QtSvg::Options options,
213 QtSvg::AnimatorType type)
215 std::unique_ptr<QSvgDocument> doc;
218 if (contents.startsWith(
"\x1f\x8b")) {
220 buffer.setData(contents);
221 svg = qt_inflateSvgzDataFrom(&buffer);
230 buffer.open(QIODevice::ReadOnly);
231 QSvgHandler handler(&buffer, options, type);
234 doc = handler.takeDocument();
236 doc->m_animator->setAnimationDuration(handler.animationDuration());
241std::unique_ptr<QSvgDocument> QSvgDocument::load(QXmlStreamReader *contents, QtSvg::Options options,
242 QtSvg::AnimatorType type)
244 QSvgHandler handler(contents, options, type);
246 std::unique_ptr<QSvgDocument> doc;
248 doc = handler.takeDocument();
250 doc->m_animator->setAnimationDuration(handler.animationDuration());
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 = namedNode(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(
const QString &name, QSvgFontPtr font)
368 if (!m_fonts.insert({ name, std::move(font) }).second)
369 qCWarning(lcSvgHandler) <<
"Font Family already added: " << name;
372QSvgFont *QSvgDocument::svgFont(
const QString &family)
const
374 auto it = m_fonts.find(family);
375 return it != m_fonts.end() ? it->second.get() :
nullptr;
378void QSvgDocument::addNamedNode(
const QString &id, QSvgNode *node)
380 m_namedNodes.insert(id, node);
383QSvgNode *QSvgDocument::namedNode(
const QString &id)
const
385 return m_namedNodes.value(id);
388void QSvgDocument::addPaintServer(QSvgPaintServerSharedPtr paintServer,
const QString &id)
393 if (!m_paintServers.insert({ id, paintServer }).second)
394 qCWarning(lcSvgHandler) <<
"Duplicate unique style id:" << id;
397QSvgPaintServerSharedPtr QSvgDocument::paintServer(QStringView id)
const
399 auto it = m_paintServers.find(id.toString());
400 return it != m_paintServers.end() ? it->second :
nullptr;
403void QSvgDocument::restartAnimation()
406 m_animator->restartAnimation();
409bool QSvgDocument::animated()
const
414void QSvgDocument::setAnimated(
bool a)
419void QSvgDocument::draw(QPainter *p)
424void QSvgDocument::drawCommand(QPainter *, QSvgExtraStates &)
426 qCDebug(lcSvgHandler) <<
"SVG Tiny does not support nested <svg> elements: ignored.";
432 qreal determinant = transform.determinant();
433 return qIsFinite(determinant);
436void QSvgDocument::mapSourceToTarget(QPainter *p,
const QRectF &targetRect,
const QRectF &sourceRect)
438 QTransform oldTransform = p->worldTransform();
440 QRectF target = targetRect;
441 if (target.isEmpty()) {
442 QPaintDevice *dev = p->device();
443 QRectF deviceRect(0, 0, dev->width(), dev->height());
444 if (deviceRect.isEmpty()) {
445 if (sourceRect.isEmpty())
446 target = QRectF(QPointF(0, 0), size());
448 target = QRectF(QPointF(0, 0), sourceRect.size());
454 QRectF source = sourceRect;
455 if (source.isEmpty())
458 if (source != target && !qFuzzyIsNull(source.width()) && !qFuzzyIsNull(source.height())) {
459 if (m_implicitViewBox || !preserveAspectRatio()) {
461 QTransform transform;
462 transform.scale(target.width() / source.width(),
463 target.height() / source.height());
464 QRectF c2 = transform.mapRect(source);
465 p->translate(target.x() - c2.x(),
466 target.y() - c2.y());
467 p->scale(target.width() / source.width(),
468 target.height() / source.height());
474 QSizeF viewBoxSize = source.size();
475 viewBoxSize.scale(target.width(), target.height(), Qt::KeepAspectRatio);
478 p->translate(target.x() + (target.width() - viewBoxSize.width()) / 2,
479 target.y() + (target.height() - viewBoxSize.height()) / 2);
481 p->scale(viewBoxSize.width() / source.width(),
482 viewBoxSize.height() / source.height());
485 p->translate(-source.x(), -source.y());
489 if (!isValidMatrix(p->worldTransform()))
490 p->setWorldTransform(oldTransform);
493QRectF QSvgDocument::boundsOnElement(
const QString &id)
const
495 const QSvgNode *node = namedNode(id);
498 return node->bounds();
501bool QSvgDocument::elementExists(
const QString &id)
const
503 return bool(namedNode(id));
506QTransform QSvgDocument::transformForElement(
const QString &id)
const
508 QSvgNode *node = namedNode(id);
511 qCDebug(lcSvgHandler,
"Couldn't find node %s. Skipping rendering.",
qPrintable(id));
517 node = node->parent();
519 if (
auto prop = node->m_style.property(QSvgStyleProperty::Transform))
520 t *=
static_cast<QSvgTransformStyle*>(prop)->qtransform();
521 node = node->parent();
527int QSvgDocument::currentFrame()
const
529 const double runningPercentage = qMin(currentElapsed() /
double(animationDuration()), 1.);
530 const int totalFrames = m_fps * animationDuration() / 1000;
531 return int(runningPercentage * totalFrames);
534void QSvgDocument::setCurrentFrame(
int frame)
539 const int totalFrames = m_fps * animationDuration() / 1000;
540 if (totalFrames == 0)
543 const int timeForFrame = frame * animationDuration() / totalFrames;
544 const int timeToAdd = timeForFrame - currentElapsed();
545 m_animator->setAnimatorTime(timeToAdd);
548void QSvgDocument::setFramesPerSecond(
int num)
553QSharedPointer<QSvgAbstractAnimator> QSvgDocument::animator()
const
558bool QSvgDocument::isLikelySvg(QIODevice *device,
bool *isCompressed)
560 constexpr int bufSize = 4096;
562 char inflateBuf[bufSize];
563 bool useInflateBuf =
false;
564 int readLen = device->peek(buf, bufSize);
567#ifndef QT_NO_COMPRESS
568 if (quint8(buf[0]) == 0x1f && quint8(buf[1]) == 0x8b) {
571 zlibStream.avail_in = readLen;
572 zlibStream.next_out =
reinterpret_cast<Bytef *>(inflateBuf);
573 zlibStream.avail_out = bufSize;
574 zlibStream.next_in =
reinterpret_cast<Bytef *>(buf);
575 zlibStream.zalloc = Z_NULL;
576 zlibStream.zfree = Z_NULL;
577 zlibStream.opaque = Z_NULL;
578 if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK)
580 int zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
581 inflateEnd(&zlibStream);
582 if ((zlibResult != Z_OK && zlibResult != Z_STREAM_END) || zlibStream.total_out < 8)
584 readLen = zlibStream.total_out;
586 *isCompressed =
true;
587 useInflateBuf =
true;
590 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)