Skip to content

Commit

Permalink
remove QSvg rendering code duplication by creating a utility routine …
Browse files Browse the repository at this point in the history
…[deploy]
  • Loading branch information
kevinhendricks committed Dec 6, 2023
1 parent c43fee0 commit 5f3206d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pre-2.x.x
- patch Qt6.5.3 to avoid transient child window resize bug on Windows
- fix QuickParser bug when parseing attribute names not properly ignoreing all legal whitespace
- make OPF parsing robust to alternative whitespace usage as well
- fix creation of thumbnails of svg images in SelectFiles and Image Report for macOS


Sigil-2.0.2
Expand Down
15 changes: 1 addition & 14 deletions src/Dialogs/ReportsWidgets/ImageFilesWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QPushButton>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QtSvg/QSvgRenderer>

#include "sigil_exception.h"
#include "BookManipulation/FolderKeeper.h"
Expand Down Expand Up @@ -116,18 +114,7 @@ void ImageFilesWidget::SetupTable(int sort_column, Qt::SortOrder sort_order)
QString path = resource->GetFullPath();
QImage image;
if (resource->Type() == Resource::SVGResourceType) {
QString svgdata = Utility::ReadUnicodeTextFile(path);
// QtSvg has many issues with desc, title, and flowRoot tags
svgdata = Utility::FixupSvgForRendering(svgdata);
QSvgRenderer renderer(this);
renderer.load(svgdata.toUtf8());
QSize sz = renderer.defaultSize();
QImage svgimage(sz, QImage::Format_ARGB32);
// **must** fill it with tranparent pixels BEFORE trying to render anything
svgimage.fill(qRgba(0,0,0,0));
QPainter painter(&svgimage);
renderer.render(&painter);
image = svgimage;
image = Utility::RenderSvgToImage(path);
} else {
image.load(path);
}
Expand Down
17 changes: 2 additions & 15 deletions src/Dialogs/SelectFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
#include <QtCore/QFileInfo>
#include <QEventLoop>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QtSvg/QSvgRenderer>
#include <QtWidgets/QLayout>
#include <QtWebEngineWidgets>
#include <QtWebEngineCore>
Expand Down Expand Up @@ -206,19 +204,8 @@ void SelectFiles::SetImages()
QImage image;
if (type == Resource::ImageResourceType) {
image.load(resource->GetFullPath());
} else { // Svg
QString svgdata = Utility::ReadUnicodeTextFile(resource->GetFullPath());
// QtSvg has many issues with desc, title, and flowRoot tags
svgdata = Utility::FixupSvgForRendering(svgdata);
QSvgRenderer renderer(this);
renderer.load(svgdata.toUtf8());
QSize sz = renderer.defaultSize();
QImage svgimage(sz, QImage::Format_ARGB32);
// **must** fill it with tranparent pixels BEFORE trying to render anything
svgimage.fill(qRgba(0,0,0,0));
QPainter painter(&svgimage);
renderer.render(&painter);
image = svgimage;
} else {
image = Utility::RenderSvgToImage(resource->GetFullPath());
}
QPixmap pixmap = QPixmap::fromImage(image);
if (pixmap.height() > m_ThumbnailSize || pixmap.width() > m_ThumbnailSize) {
Expand Down
20 changes: 20 additions & 0 deletions src/Misc/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
#include <QMenu>
#include <QSet>
#include <QVector>
#include <QImage>
#include <QPainter>
#include <QtSvg/QSvgRenderer>
#include <QDebug>

#include "sigil_constants.h"
Expand Down Expand Up @@ -1528,3 +1531,20 @@ QString Utility::FixupSvgForRendering(const QString& data)
}
return svgdata.join("");
}

QImage Utility::RenderSvgToImage(const QString& filepath)
{
QString svgdata = Utility::ReadUnicodeTextFile(filepath);
// QtSvg has many issues with desc, title, and flowRoot tags
svgdata = Utility::FixupSvgForRendering(svgdata);
QSvgRenderer renderer;
renderer.load(svgdata.toUtf8());
QSize sz = renderer.defaultSize();
QImage svgimage(sz, QImage::Format_ARGB32);
// **must** fill it with tranparent pixels BEFORE trying to render anything
svgimage.fill(qRgba(0,0,0,0));
QPainter painter(&svgimage);
renderer.render(&painter);
return svgimage;

}
3 changes: 3 additions & 0 deletions src/Misc/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
#include <QMessageBox>
#include <QSet>
#include <QStringList>
#include <QImage>

class QStringRef;
class QWidget;
class QMenu;


struct ExceptionBase;

class Utility
Expand Down Expand Up @@ -270,6 +272,7 @@ class Utility
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);

static QString FixupSvgForRendering(const QString& data);
static QImage RenderSvgToImage(const QString& filepath);

};
#endif // UTILITY_H
Expand Down

0 comments on commit 5f3206d

Please sign in to comment.