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

QRImageWidget: Sizing and font fixups #506

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 41 additions & 13 deletions src/qt/qrimagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,64 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
return false;
}

QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
qrImage.fill(0xffffff);
QImage qrImage = QImage(code->width, code->width, QImage::Format_RGB32);
unsigned char *p = code->data;
for (int y = 0; y < code->width; ++y) {
for (int x = 0; x < code->width; ++x) {
qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
qrImage.setPixel(x, y, ((*p & 1) ? 0x0 : 0xffffff));
++p;
}
}
QRcode_free(code);

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);
int qr_image_width = QR_IMAGE_SIZE + (2 * QR_IMAGE_MARGIN);
int qr_image_height = qr_image_width;
int qr_image_x_margin = QR_IMAGE_MARGIN;
int text_lines;
QFont font;
if (text.isEmpty()) {
text_lines = 0;
} else {
// Determine font to use
font = GUIUtil::fixedPitchFont();
font.setStretch(QFont::SemiCondensed);
font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
const int max_text_width = qr_image_width - (2 * QR_IMAGE_TEXT_MARGIN);
const qreal font_size = GUIUtil::calculateIdealFontSize(max_text_width, text, font);
font.setPointSizeF(font_size);

// Plan how many lines are needed
QFontMetrics fm(font);
const int text_width = GUIUtil::TextWidth(fm, text);
if (text_width > max_text_width && text_width < max_text_width * 5 / 4) {
// Allow the image to grow up to 25% wider
qr_image_width = text_width + (2 * QR_IMAGE_TEXT_MARGIN);
qr_image_x_margin = (qr_image_width - QR_IMAGE_SIZE) / 2;
text_lines = 1;
} else {
text_lines = (text_width + max_text_width - 1) / max_text_width;
}
qr_image_height += (fm.height() * text_lines) + QR_IMAGE_TEXT_MARGIN;
}
QImage qrAddrImage(qr_image_width, qr_image_height, QImage::Format_RGB32);
qrAddrImage.fill(0xffffff);
{
QPainter painter(&qrAddrImage);
painter.drawImage(QR_IMAGE_MARGIN, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
painter.drawImage(qr_image_x_margin, QR_IMAGE_MARGIN, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));

if (!text.isEmpty()) {
QRect paddedRect = qrAddrImage.rect();
paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);
paddedRect.setHeight(paddedRect.height() - QR_IMAGE_TEXT_MARGIN);

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);
QString text_wrapped = text;
const int char_per_line = (text.size() + text_lines - 1) / text_lines;
for (int line = 1, pos = 0; line < text_lines; ++line) {
pos += char_per_line;
text_wrapped.insert(pos, QChar{'\n'});
}

painter.setFont(font);
painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);
painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text_wrapped);
}
}

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

/* Size of exported QR Code image */
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;
static constexpr int QR_IMAGE_SIZE = 252;
static constexpr int QR_IMAGE_TEXT_MARGIN = 8;
static constexpr int QR_IMAGE_MARGIN = 24;

QT_BEGIN_NAMESPACE
class QMenu;
Expand Down