| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qlottieimage_p.h" |
| 5 | |
| 6 | #include <QDir> |
| 7 | #include <QFileInfo> |
| 8 | #include <QJsonObject> |
| 9 | |
| 10 | #include <QtLottie/private/qlottieconstants_p.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QLottieImage::QLottieImage(const QLottieImage &other) |
| 15 | : QLottieBase(other) |
| 16 | { |
| 17 | m_url = other.m_url; |
| 18 | m_size = other.m_size; |
| 19 | m_image = other.m_image; |
| 20 | } |
| 21 | |
| 22 | QLottieImage::QLottieImage(const QJsonObject &definition, QLottieBase *parent) |
| 23 | { |
| 24 | setParent(parent); |
| 25 | construct(definition); |
| 26 | } |
| 27 | |
| 28 | QLottieBase *QLottieImage::clone() const |
| 29 | { |
| 30 | return new QLottieImage(*this); |
| 31 | } |
| 32 | |
| 33 | void QLottieImage::construct(const QJsonObject &definition) |
| 34 | { |
| 35 | QLottieBase::parse(definition); |
| 36 | if (m_hidden) |
| 37 | return; |
| 38 | |
| 39 | qCDebug(lcLottieQtLottieParser) << "QLottieImage::construct():"<< m_name; |
| 40 | |
| 41 | QJsonObject asset = definition.value(key: QLatin1String("asset")).toObject(); |
| 42 | QString assetString = asset.value(key: QLatin1String("p")).toString(); |
| 43 | |
| 44 | if (assetString.startsWith(s: QLatin1String("data:image"))) { |
| 45 | QStringList assetsDataStringList = assetString.split(sep: QLatin1String(",")); |
| 46 | if (assetsDataStringList.size() > 1) { |
| 47 | QByteArray assetData = QByteArray::fromBase64(base64: assetsDataStringList[1].toLatin1()); |
| 48 | m_image.loadFromData(data: assetData); |
| 49 | } |
| 50 | if (m_image.isNull()) { |
| 51 | qCWarning(lcLottieQtLottieParser) << "Unable to load embedded image asset" |
| 52 | << asset.value(key: QLatin1String("id")).toString(); |
| 53 | } |
| 54 | } |
| 55 | else { |
| 56 | QFileInfo info(asset.value(key: QLatin1String("fileSource")).toString()); |
| 57 | QString urlPath = info.path() + QLatin1Char('/') |
| 58 | + asset.value(key: QLatin1String("u")).toString() + QLatin1Char('/') + assetString; |
| 59 | m_url = QUrl(urlPath); |
| 60 | m_url.setScheme(QLatin1String("file")); |
| 61 | QString path = m_url.toLocalFile(); |
| 62 | m_image.load(fileName: path); |
| 63 | if (m_image.isNull()) |
| 64 | qCWarning(lcLottieQtLottieParser) << "Unable to load file"<< path; |
| 65 | } |
| 66 | |
| 67 | const qreal width = asset.value(key: QLatin1String("w")).toDouble(); |
| 68 | const qreal height = asset.value(key: QLatin1String("h")).toDouble(); |
| 69 | m_size = QSizeF(width, height); |
| 70 | } |
| 71 | |
| 72 | void QLottieImage::render(QLottieRenderer &renderer) const |
| 73 | { |
| 74 | renderer.render(image: *this); |
| 75 | } |
| 76 | |
| 77 | QT_END_NAMESPACE |
| 78 |
