Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix visual quality of text in QR image #71

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
qt: Fix visual quality of text in QR image
  • Loading branch information
hebasto committed Sep 7, 2020
commit 6954156b4091bc1e561502f0eef0cece56c76eec
18 changes: 10 additions & 8 deletions src/qt/qrimagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QApplication>
#include <QClipboard>
#include <QDrag>
#include <QFontDatabase>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
Expand Down Expand Up @@ -64,24 +65,25 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
}
QRcode_free(code);

QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 20), QImage::Format_RGB32);
const int qr_image_size = QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 2 * QR_IMAGE_MARGIN);
QImage qrAddrImage(qr_image_size, qr_image_size, QImage::Format_RGB32);
qrAddrImage.fill(0xffffff);
{
QPainter painter(&qrAddrImage);
painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
painter.drawImage(QR_IMAGE_MARGIN, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));

if (!text.isEmpty()) {
QFont font = GUIUtil::fixedPitchFont();
font.setStyleStrategy(QFont::NoAntialias);
QRect paddedRect = qrAddrImage.rect();
paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);

// calculate ideal font size
qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 20, text, font);
QFont font = GUIUtil::fixedPitchFont();
font.setStretch(QFont::SemiCondensed);
font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);
font.setPointSizeF(font_size);

painter.setFont(font);
paddedRect.setHeight(QR_IMAGE_SIZE+12);
painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, text);
painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/qt/qrimagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
static const int MAX_URI_LENGTH = 255;

/* Size of exported QR Code image */
static const int QR_IMAGE_SIZE = 300;
static constexpr int QR_IMAGE_SIZE = 300;
static constexpr int QR_IMAGE_TEXT_MARGIN = 10;
static constexpr int QR_IMAGE_MARGIN = 2 * QR_IMAGE_TEXT_MARGIN;

QT_BEGIN_NAMESPACE
class QMenu;
Expand Down