| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "svgimageprovider.h" |
| 5 | #include <QImage> |
| 6 | #include <QPixmap> |
| 7 | #include <QSvgRenderer> |
| 8 | #include <QPainter> |
| 9 | |
| 10 | SvgImageProvider::SvgImageProvider() : |
| 11 | QQuickImageProvider(QQuickImageProvider::Pixmap) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | SvgImageProvider::~SvgImageProvider() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | QPixmap SvgImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) |
| 20 | { |
| 21 | QSize imageSize(-1, -1); |
| 22 | QUrl request(id); |
| 23 | QString imagePath = QLatin1String(":/") + request.path(); |
| 24 | if (request.hasQuery()) { |
| 25 | const QString query = request.query(); |
| 26 | const QStringList paramList = query.split(sep: QLatin1Char('&'), behavior: Qt::SkipEmptyParts); |
| 27 | QVariantMap params; |
| 28 | for (const QString ¶m : paramList) { |
| 29 | QStringList keyValue = param.split(sep: QLatin1Char('='), behavior: Qt::SkipEmptyParts); |
| 30 | if (keyValue.size() == 2) |
| 31 | params[keyValue[0]] = keyValue[1]; |
| 32 | } |
| 33 | const auto widthIt = params.constFind(key: QLatin1String("width")); |
| 34 | if (widthIt != params.cend()) { |
| 35 | bool ok = false; |
| 36 | int value = widthIt.value().toInt(ok: &ok); |
| 37 | if (ok) |
| 38 | imageSize.setWidth(value); |
| 39 | } |
| 40 | const auto heightIt = params.constFind(key: QLatin1String("height")); |
| 41 | if (heightIt != params.cend()) { |
| 42 | bool ok = false; |
| 43 | int value = heightIt.value().toInt(ok: &ok); |
| 44 | if (ok) |
| 45 | imageSize.setHeight(value); |
| 46 | } |
| 47 | } else { |
| 48 | imageSize = requestedSize; |
| 49 | } |
| 50 | |
| 51 | QPixmap image; |
| 52 | if ((imageSize.width() > 0 || imageSize.height() > 0) && imagePath.endsWith(s: QLatin1String(".svg"))) { |
| 53 | QSvgRenderer renderer(imagePath); |
| 54 | QSize defaultSize(renderer.defaultSize()); |
| 55 | if (defaultSize.isEmpty()) |
| 56 | return image; |
| 57 | if (imageSize.width() <= 0 && imageSize.height() > 0) { |
| 58 | double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height(); |
| 59 | imageSize.setWidth(qRound(d: imageSize.height() * aspectRatio)); |
| 60 | } else if (imageSize.width() > 0 && imageSize.height() <= 0) { |
| 61 | double aspectRatio = (double)defaultSize.width() / (double)defaultSize.height(); |
| 62 | imageSize.setHeight(qRound(d: imageSize.width() / aspectRatio)); |
| 63 | } |
| 64 | image = QPixmap(imageSize); |
| 65 | image.fill(fillColor: Qt::transparent); |
| 66 | QPainter painter(&image); |
| 67 | renderer.render(p: &painter, bounds: image.rect()); |
| 68 | } else { |
| 69 | image = QPixmap(imagePath); |
| 70 | imageSize = image.size(); |
| 71 | } |
| 72 | |
| 73 | QPixmap result; |
| 74 | if (requestedSize.isValid() && requestedSize != imageSize) |
| 75 | result = image.scaled(s: requestedSize, aspectMode: Qt::KeepAspectRatio); |
| 76 | else |
| 77 | result = image; |
| 78 | |
| 79 | *size = result.size(); |
| 80 | |
| 81 | return result; |
| 82 | } |
| 83 |
