Skip to content

Commit

Permalink
Do tiling and color adjustments after rendering the SVG in device pix…
Browse files Browse the repository at this point in the history
…el resolution.

This also allows to remove the conditional Apple code form Qt4
  • Loading branch information
daschuer committed Sep 21, 2024
1 parent a248708 commit 7747112
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/widget/paintable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,7 @@ Paintable::Paintable(const PixmapSource& source, DrawMode mode, double scaleFact
} else {
return;
}
#ifdef __APPLE__
// Apple does Retina scaling behind the scenes, so we also pass a
// Paintable::FIXED image. On the other targets, it is better to
// cache the pixmap. We do not do this for TILE and color schemas.
// which can result in a correct but possibly blurry picture at a
// Retina display. This can be fixed when switching to QT5
if (mode == TILE || WPixmapStore::willCorrectColors()) {
#else
if (mode == TILE || mode == Paintable::FIXED || WPixmapStore::willCorrectColors()) {
#endif
// The SVG renderer doesn't directly support tiling, so we render
// it to a pixmap which will then get tiled.
QImage copy_buffer(pSvg->defaultSize() * scaleFactor, QImage::Format_ARGB32);
copy_buffer.fill(0x00000000); // Transparent black.
QPainter painter(&copy_buffer);
pSvg->render(&painter);
WPixmapStore::correctImageColors(&copy_buffer);

m_pPixmap.reset(new QPixmap(copy_buffer.size()));
m_pPixmap->convertFromImage(copy_buffer);
} else {
m_pSvg = std::move(pSvg);
}
m_pSvg = std::move(pSvg);
}
}

Expand Down Expand Up @@ -253,7 +231,19 @@ void Paintable::drawInternal(const QRectF& targetRect, QPainter* pPainter,
// << targetRect << sourceRect;
if (m_pSvg) {
if (m_drawMode == TILE) {
qWarning() << "Tiled SVG should have been rendered to pixmap!";
if (!m_pPixmap) {
// qDebug() << "Paintable cache miss";
qreal devicePixelRatio = pPainter->device()->devicePixelRatio();
m_pPixmap = std::make_unique<QPixmap>(m_pSvg->defaultSize());
m_pPixmap->setDevicePixelRatio(devicePixelRatio);
m_pPixmap->fill(Qt::transparent);
auto pixmapPainter = QPainter(m_pPixmap.get());
m_pSvg->render(&pixmapPainter);
mayCorrectColors();
}
// The SVG renderer doesn't directly support tiling, so we render
// it to a pixmap which will then get tiled.
pPainter->drawTiledPixmap(targetRect, *m_pPixmap);
} else {
if (!m_pPixmap ||
m_pPixmap->size() != targetRect.size().toSize() ||
Expand All @@ -266,6 +256,7 @@ void Paintable::drawInternal(const QRectF& targetRect, QPainter* pPainter,
auto pixmapPainter = QPainter(m_pPixmap.get());
m_pSvg->setViewBox(sourceRect);
m_pSvg->render(&pixmapPainter);
mayCorrectColors();
m_lastSourceRect = sourceRect;
}
pPainter->drawPixmap(targetRect.topLeft(), *m_pPixmap);
Expand Down Expand Up @@ -309,3 +300,11 @@ QString Paintable::getAltFileName(const QString& fileName) {
return fileName;
}
}

void Paintable::mayCorrectColors() {
if (WPixmapStore::willCorrectColors()) {
QImage image = m_pPixmap->toImage();
WPixmapStore::correctImageColors(&image);
m_pPixmap->convertFromImage(image);
}
}
1 change: 1 addition & 0 deletions src/widget/paintable.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Paintable {
private:
void drawInternal(const QRectF& targetRect, QPainter* pPainter,
const QRectF& sourceRect);
void mayCorrectColors();

std::unique_ptr<QPixmap> m_pPixmap;
std::unique_ptr<QSvgRenderer> m_pSvg;
Expand Down

0 comments on commit 7747112

Please sign in to comment.