Skip to content

Commit 72b04ce

Browse files
authored
Merge pull request #269 from OpenShot/fix-resvg-render-size
Fix resvg rendering issues for common transitions
2 parents 1b19ae7 + 9806694 commit 72b04ce

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/QtImageReader.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@ void QtImageReader::Open()
7777
// If defined and found in CMake, utilize the libresvg for parsing
7878
// SVG files and rasterizing them to QImages.
7979
// Only use resvg for files ending in '.svg' or '.svgz'
80-
if (path.find(".svg") != std::string::npos ||
81-
path.find(".svgz") != std::string::npos) {
80+
if (path.find(".svg") != std::string::npos || path.find(".svgz") != std::string::npos) {
8281

8382
ResvgRenderer renderer(QString::fromStdString(path));
8483
if (!renderer.isValid()) {
85-
success = false;
84+
// Attempt to open file (old method using Qt5 limited SVG parsing)
85+
success = image->load(QString::fromStdString(path));
86+
if (success) {
87+
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
88+
}
8689
} else {
8790

8891
image = std::shared_ptr<QImage>(new QImage(renderer.defaultSize(), QImage::Format_RGBA8888));
@@ -225,17 +228,25 @@ std::shared_ptr<Frame> QtImageReader::GetFrame(int64_t requested_frame)
225228
// If defined and found in CMake, utilize the libresvg for parsing
226229
// SVG files and rasterizing them to QImages.
227230
// Only use resvg for files ending in '.svg' or '.svgz'
228-
if (path.find(".svg") != std::string::npos ||
229-
path.find(".svgz") != std::string::npos) {
231+
if (path.find(".svg") != std::string::npos || path.find(".svgz") != std::string::npos) {
230232
ResvgRenderer renderer(QString::fromStdString(path));
231233
if (renderer.isValid()) {
234+
// Scale SVG size to keep aspect ratio, and fill the max_size as best as possible
235+
QSize svg_size(renderer.defaultSize().width(), renderer.defaultSize().height());
236+
svg_size.scale(max_width, max_height, Qt::KeepAspectRatio);
232237

233-
cached_image = std::shared_ptr<QImage>(new QImage(QSize(max_width, max_height), QImage::Format_RGBA8888));
238+
// Create empty QImage
239+
cached_image = std::shared_ptr<QImage>(new QImage(QSize(svg_size.width(), svg_size.height()), QImage::Format_RGBA8888));
234240
cached_image->fill(Qt::transparent);
235241

242+
// Render SVG into QImage
236243
QPainter p(cached_image.get());
237244
renderer.render(&p);
238245
p.end();
246+
} else {
247+
// Resize current rasterized SVG (since we failed to parse original SVG file with resvg)
248+
cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
249+
cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
239250
}
240251
} else {
241252
// We need to resize the original image to a smaller image (for performance reasons)

0 commit comments

Comments
 (0)