Merge branch 'qt_qrcode_sizefixes' into qt_fontsel_qrcodes-27+knots

This commit is contained in:
Luke Dashjr 2024-05-16 00:30:43 +00:00
commit 1e265fb9ed
2 changed files with 44 additions and 16 deletions

View File

@ -50,36 +50,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);
}
}

View File

@ -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;