Format code & update example

This commit is contained in:
Alex Spataru
2025-04-16 18:25:57 -05:00
parent 40c941bf1b
commit 05ef848a2a
12 changed files with 835 additions and 652 deletions
+37 -122
View File
@@ -1,123 +1,38 @@
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BasedOnStyle: LLVM
Standard: Cpp11
CommentPragmas: "^!|^:"
PointerBindsToType: false
SpaceAfterTemplateKeyword: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
ConstructorInitializerIndentWidth: 2
NamespaceIndentation: None
AlignAfterOpenBracket: true
AlwaysBreakTemplateDeclarations: true
AllowShortFunctionsOnASingleLine: Inline
SortIncludes: false
IndentCaseLabels: true
IndentPPDirectives: AfterHash
AccessModifierOffset: -2
IndentWidth: 2
ColumnLimit: 80
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<config.h>$'
Priority: -1
- Regex: '^<.*\.h>$'
Priority: 1
- Regex: '^<.*>'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '(Test)?$'
IndentCaseBlocks: true
IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: AfterHash
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2000
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
TabWidth: 2
UseTab: Never
...
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ]
#StatementMacros ['Q_OBJECT', 'Q_UNUSED']
+18
View File
@@ -3,6 +3,19 @@
.DS_Store
.AppleDouble
.LSOverride
.cache
# Binary builds
udp_function_generator
udp_function_generator.exe
# Build & IDEs
*.kdev*
build/*
example/build/*
*.vscode
*.layout
*.qmlls.ini
# Icon must end with two \r
Icon
@@ -28,3 +41,8 @@ Temporary Items
# Qt project file
*.pro.user
*.user
# Clion
.idea/*
cmake-build-*
+1 -1
View File
@@ -2,7 +2,7 @@
Qt QR Code Generator is a simple C++ class that uses the [qrcodegen](https://github.com/nayuki/QR-Code-generator) library to generate QR codes from QStrings in Qt applications.
[![Screenshot](example/screenshot.png)](example/screenshot.png)
[![Demo](example/demo.gif)](example/demo.gif)
## Usage
+86 -57
View File
@@ -22,75 +22,104 @@
#include "MainWindow.h"
MainWindow::MainWindow() {
// Initialize layout-related widgets
m_centralWidget = new QWidget(this);
m_header = new QWidget(m_centralWidget);
#include <QFile>
#include <QFileDialog>
#include <QStandardPaths>
// Initialize horizontal layout widgets
m_label = new QLabel(tr("Message") + ": ", m_header);
m_pushButton = new QPushButton(tr("Generate QR Code"), m_header);
m_lineEdit = new QLineEdit(m_header);
MainWindow::MainWindow()
{
// Initialize layout-related widgets
m_centralWidget = new QWidget(this);
m_header = new QWidget(m_centralWidget);
// Initialize QR code image label
m_qrCodeDisplay = new QLabel(m_centralWidget);
m_qrCodeDisplay->setMinimumSize(QSize(500, 500));
m_qrCodeDisplay->setMaximumSize(QSize(500, 500));
// Initialize horizontal layout widgets
m_label = new QLabel(tr("Message") + ": ", m_header);
m_saveButton = new QPushButton(tr("Export QR Code"), m_header);
m_lineEdit = new QLineEdit(m_header);
// Setup horizontal layout for line edit & push button
m_verticalLayout = new QVBoxLayout(m_centralWidget);
m_headerLayout = new QHBoxLayout(m_header);
m_headerLayout->addWidget(m_label);
m_headerLayout->addWidget(m_lineEdit);
m_headerLayout->addWidget(m_pushButton);
m_headerLayout->setContentsMargins(0, 0, 0, 0);
m_headerLayout->setStretch(1, 1);
// Initialize QR code image label
m_qrCodeDisplay = new QLabel(m_centralWidget);
m_qrCodeDisplay->setMinimumSize(QSize(500, 500));
m_qrCodeDisplay->setMaximumSize(QSize(500, 500));
// Setup vertical layout with QR code label & QR code generation controls
m_verticalLayout->addWidget(m_header);
m_verticalLayout->addWidget(m_qrCodeDisplay);
m_verticalLayout->setStretch(0, 1);
// Setup horizontal layout for line edit & push button
m_verticalLayout = new QVBoxLayout(m_centralWidget);
m_headerLayout = new QHBoxLayout(m_header);
m_headerLayout->addWidget(m_label);
m_headerLayout->addWidget(m_lineEdit);
m_headerLayout->addWidget(m_saveButton);
m_headerLayout->setContentsMargins(0, 0, 0, 0);
m_headerLayout->setStretch(1, 1);
// Set central widget & resize window to minimum size possible
setCentralWidget(m_centralWidget);
setFixedSize(minimumSize());
// Setup vertical layout with QR code label & QR code generation controls
m_verticalLayout->addWidget(m_header);
m_verticalLayout->addWidget(m_qrCodeDisplay);
m_verticalLayout->setStretch(0, 1);
// Set window title
setWindowTitle(tr("QR Code Generator"));
// Set central widget & resize window to minimum size possible
setCentralWidget(m_centralWidget);
setFixedSize(minimumSize());
// Disable "Generate QR code" button when line edit contains empty text
connect(m_lineEdit, &QLineEdit::textChanged, this, [this](const QString& text) {
m_pushButton->setEnabled(!text.isEmpty());
});
// Set window title
setWindowTitle(tr("QR Code Generator"));
// Update QR code when user clicks on the generate QR code button
connect(m_pushButton, &QPushButton::clicked, this, &MainWindow::onGenerateButtonClicked);
// Disable "Generate QR code" button when line edit contains empty text
connect(m_lineEdit, &QLineEdit::textChanged, this,
[this](const QString &text) {
m_saveButton->setEnabled(!text.isEmpty());
if (!text.isEmpty())
generateQrCode();
});
// Set "Hello World" text & generate QR code
m_lineEdit->setText(tr("Hello World!"));
onGenerateButtonClicked();
// Update QR code when user clicks on the generate QR code button
connect(m_saveButton, &QPushButton::clicked, this, &MainWindow::saveQrCode);
// Set "Hello World" text & generate QR code
m_lineEdit->setText(tr("Hello World!"));
generateQrCode();
}
MainWindow::~MainWindow() {
delete m_label;
delete m_lineEdit;
delete m_pushButton;
delete m_qrCodeDisplay;
delete m_headerLayout;
delete m_verticalLayout;
delete m_header;
delete m_centralWidget;
MainWindow::~MainWindow()
{
delete m_label;
delete m_lineEdit;
delete m_saveButton;
delete m_qrCodeDisplay;
delete m_headerLayout;
delete m_verticalLayout;
delete m_header;
delete m_centralWidget;
}
void MainWindow::onGenerateButtonClicked() {
// Get text from line edit
auto text = m_lineEdit->text();
void MainWindow::saveQrCode()
{
auto svg = m_generator.generateSvgQr(m_lineEdit->text());
if (svg.isEmpty())
return;
// Generate QR code from text
QImage unscaledImage = m_generator.generateQr(text);
QImage image = unscaledImage.scaled(500, 500);
// Display generated image
m_qrCodeDisplay->setPixmap(QPixmap::fromImage(image));
m_qrCodeDisplay->setToolTip(text);
auto path = QFileDialog::getSaveFileName(this, tr("Save QR Code"),
QDir::homePath(), tr("*.svg"));
if (!path.isEmpty())
{
QFile file(path);
if (file.open(QFile::WriteOnly))
{
file.write(svg.toUtf8());
file.close();
}
}
}
void MainWindow::generateQrCode()
{
// Get text from line edit
auto text = m_lineEdit->text();
// Generate QR code from text
QImage unscaledImage = m_generator.generateQr(text);
QImage image = unscaledImage.scaled(500, 500);
// Display generated image
m_qrCodeDisplay->setPixmap(QPixmap::fromImage(image));
m_qrCodeDisplay->setToolTip(text);
}
+16 -14
View File
@@ -30,27 +30,29 @@
#include <QrCodeGenerator.h>
class MainWindow : public QMainWindow {
Q_OBJECT
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow();
~MainWindow();
explicit MainWindow();
~MainWindow();
private Q_SLOTS:
void onGenerateButtonClicked();
void saveQrCode();
void generateQrCode();
private:
QrCodeGenerator m_generator;
QrCodeGenerator m_generator;
QWidget *m_header;
QWidget *m_centralWidget;
QWidget *m_header;
QWidget *m_centralWidget;
QLabel *m_label;
QLineEdit *m_lineEdit;
QLabel *m_qrCodeDisplay;
QPushButton *m_pushButton;
QLabel *m_label;
QLineEdit *m_lineEdit;
QLabel *m_qrCodeDisplay;
QPushButton *m_saveButton;
QHBoxLayout *m_headerLayout;
QVBoxLayout *m_verticalLayout;
QHBoxLayout *m_headerLayout;
QVBoxLayout *m_verticalLayout;
};
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

+6 -5
View File
@@ -22,9 +22,10 @@
#include "MainWindow.h"
int main(int argc, char** argv) {
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 364 KiB

+42 -38
View File
@@ -33,9 +33,9 @@
* @param parent QObject parent
*/
QrCodeGenerator::QrCodeGenerator(QObject *parent)
: QObject(parent)
: QObject(parent)
{
// No implementation needed for default constructor.
// No implementation needed for default constructor.
}
/**
@@ -46,12 +46,14 @@ QrCodeGenerator::QrCodeGenerator(QObject *parent)
* @param errorCorrection The level of error correction to apply.
* @return QImage representing the generated QR code.
*/
QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, quint16 borderSize,
QImage QrCodeGenerator::generateQr(const QString &data, quint16 size,
quint16 borderSize,
qrcodegen::QrCode::Ecc errorCorrection)
{
auto b = data.toUtf8();
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return qrCodeToImage(qrCode, borderSize, size);
auto b = data.toUtf8();
const auto qrCode
= qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return qrCodeToImage(qrCode, borderSize, size);
}
/**
@@ -64,9 +66,10 @@ QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, quint16 bo
QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
qrcodegen::QrCode::Ecc errorCorrection)
{
auto b = data.toUtf8();
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return toSvgString(qrCode, borderSize);
auto b = data.toUtf8();
const auto qrCode
= qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return toSvgString(qrCode, borderSize);
}
/**
@@ -75,31 +78,32 @@ QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
* @param border The border size to use.
* @return QString containing the SVG representation of the QR code.
*/
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr,
quint16 border) const
{
QString str;
QTextStream sb(&str);
QString str;
QTextStream sb(&str);
sb << R"(<?xml version="1.0" encoding="UTF-8"?>)"
<< R"(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)"
<< R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 )"
<< (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2)
<< R"(" stroke="none"><rect width="100%" height="100%" fill="#FFFFFF"/><path d=")";
sb << R"(<?xml version="1.0" encoding="UTF-8"?>)"
<< R"(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)"
<< R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 )"
<< (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2)
<< R"(" stroke="none"><rect width="100%" height="100%" fill="#FFFFFF"/><path d=")";
for (int y = 0; y < qr.getSize(); y++)
for (int y = 0; y < qr.getSize(); y++)
{
for (int x = 0; x < qr.getSize(); x++)
{
for (int x = 0; x < qr.getSize(); x++)
{
if (qr.getModule(x, y))
{
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << "," << (y + border)
<< "h1v1h-1z";
}
}
if (qr.getModule(x, y))
{
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << ","
<< (y + border) << "h1v1h-1z";
}
}
}
sb << R"(" fill="#000000"/></svg>)";
return str;
sb << R"(" fill="#000000"/></svg>)";
return str;
}
/**
@@ -109,15 +113,15 @@ QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border
* @param size The image size to generate.
* @return QImage representing the QR code.
*/
QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
quint16 size) const
QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode,
quint16 border, quint16 size) const
{
QString svg = toSvgString(qrCode, border);
QSvgRenderer render(svg.toUtf8());
QImage image(size, size, QImage::Format_Mono);
image.fill(Qt::white);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
render.render(&painter);
return image;
QString svg = toSvgString(qrCode, border);
QSvgRenderer render(svg.toUtf8());
QImage image(size, size, QImage::Format_Mono);
image.fill(Qt::white);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
render.render(&painter);
return image;
}
+53 -47
View File
@@ -30,60 +30,66 @@
/**
* @class QrCodeGenerator
* @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library
* to generate QR codes from QStrings in Qt applications.
* @brief The QrCodeGenerator class is a simple C++ class that uses the
* qrcodegen library to generate QR codes from QStrings in Qt applications.
*/
class QrCodeGenerator : public QObject
{
public:
/**
* @brief Constructs a QrCodeGenerator object.
* @param parent The parent QObject.
*/
explicit QrCodeGenerator(QObject *parent = nullptr);
/**
* @brief Constructs a QrCodeGenerator object.
* @param parent The parent QObject.
*/
explicit QrCodeGenerator(QObject *parent = nullptr);
/**
* @brief Generates a QR code from the given data and error correction level.
* @param data The QString containing the data to encode in the QR code.
* @param size The desired width/height of the generated image (default: 500).
* @param borderSize The desired border width of the generated image (default: 1).
* @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM).
*
* @return QImage containing the generated QR code.
*/
QImage generateQr(const QString &data, const quint16 size = 1000, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
/**
* @brief Generates a QR code from the given data and error correction level.
* @param data The QString containing the data to encode in the QR code.
* @param size The desired width/height of the generated image (default: 500).
* @param borderSize The desired border width of the generated image (default:
* 1).
* @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM).
*
* @return QImage containing the generated QR code.
*/
QImage generateQr(const QString &data, const quint16 size = 1000,
const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection
= qrcodegen::QrCode::Ecc::MEDIUM);
/**
* @brief Generates a QR code from the given data and error correction level.
* @param data The QString containing the data to encode in the QR code.
* @param borderSize The desired border width of the generated image (default: 1).
* @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM).
*
* @return QString string containing the generated QR code in SVG format.
*/
QString generateSvgQr(const QString &data, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
/**
* @brief Generates a QR code from the given data and error correction level.
* @param data The QString containing the data to encode in the QR code.
* @param borderSize The desired border width of the generated image (default:
* 1).
* @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM).
*
* @return QString string containing the generated QR code in SVG format.
*/
QString generateSvgQr(const QString &data, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection
= qrcodegen::QrCode::Ecc::MEDIUM);
private:
/**
* @brief Converts a qrcodegen::QrCode object to a SVG image.
* @param qrCode The qrcodegen::QrCode object to convert.
* @param border The desired border width of the generated image (default: 1).
*
* @return SVG containing the QR code.
*/
QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const;
/**
* @brief Converts a qrcodegen::QrCode object to a SVG image.
* @param qrCode The qrcodegen::QrCode object to convert.
* @param border The desired border width of the generated image (default: 1).
*
* @return SVG containing the QR code.
*/
QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const;
/**
* @brief Converts a qrcodegen::QrCode object to a QImage.
* @param qrCode The qrcodegen::QrCode object to convert.
* @param size The desired width/height of the generated image.
* @param borderSize The desired border width of the generated image.
*
* @return QImage containing the QR code.
*/
QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const;
/**
* @brief Converts a qrcodegen::QrCode object to a QImage.
* @param qrCode The qrcodegen::QrCode object to convert.
* @param size The desired width/height of the generated image.
* @param borderSize The desired border width of the generated image.
*
* @return QImage containing the QR code.
*/
QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
const quint16 size) const;
};
File diff suppressed because it is too large Load Diff
+157 -118
View File
@@ -4,21 +4,21 @@
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
* liability, whether in an action of contract, tort or otherwise, arising
* from, out of or in connection with the Software or the use or other dealings
* in the Software.
*/
#pragma once
@@ -29,7 +29,8 @@
#include <string>
#include <vector>
namespace qrcodegen {
namespace qrcodegen
{
/*
* A segment of character/binary/control data in a QR Code symbol.
@@ -38,11 +39,13 @@ namespace qrcodegen {
* and call a static factory function such as QrSegment::makeNumeric().
* The low-level way to create a segment is to custom-make the bit buffer
* and call the QrSegment() constructor with appropriate values.
* This segment class imposes no length restrictions, but QR Codes have restrictions.
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
* This segment class imposes no length restrictions, but QR Codes have
* restrictions. Even in the most favorable conditions, a QR Code can only hold
* 7089 characters of data. Any segment longer than this is meaningless for the
* purpose of generating QR Codes.
*/
class QrSegment final {
class QrSegment final
{
/*---- Public helper enumeration ----*/
@@ -50,7 +53,8 @@ class QrSegment final {
* Describes how a segment's data bits are interpreted. Immutable.
*/
public:
class Mode final {
class Mode final
{
/*-- Constants --*/
@@ -87,15 +91,16 @@ public:
/*-- Methods --*/
/*
* (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0
* to 15).
* (Package-private) Returns the mode indicator bits, which is an unsigned
* 4-bit value (range 0 to 15).
*/
public:
int getModeBits() const;
/*
* (Package-private) Returns the bit width of the character count field for a segment in
* this mode in a QR Code at the given version number. The result is in the range [0, 16].
* (Package-private) Returns the bit width of the character count field for
* a segment in this mode in a QR Code at the given version number. The
* result is in the range [0, 16].
*/
public:
int numCharCountBits(int ver) const;
@@ -112,22 +117,25 @@ public:
static QrSegment makeBytes(const std::vector<std::uint8_t> &data);
/*
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
* Returns a segment representing the given string of decimal digits encoded
* in numeric mode.
*/
public:
static QrSegment makeNumeric(const char *digits);
/*
* Returns a segment representing the given text string encoded in alphanumeric mode.
* The characters allowed are: 0 to 9, A to Z (uppercase only), space,
* dollar, percent, asterisk, plus, hyphen, period, slash, colon.
* Returns a segment representing the given text string encoded in
* alphanumeric mode. The characters allowed are: 0 to 9, A to Z (uppercase
* only), space, dollar, percent, asterisk, plus, hyphen, period, slash,
* colon.
*/
public:
static QrSegment makeAlphanumeric(const char *text);
/*
* Returns a list of zero or more segments to represent the given text string. The result
* may use various segment modes and switch modes to optimize the length of the bit stream.
* Returns a list of zero or more segments to represent the given text string.
* The result may use various segment modes and switch modes to optimize the
* length of the bit stream.
*/
public:
static std::vector<QrSegment> makeSegments(const char *text);
@@ -149,9 +157,10 @@ public:
static bool isNumeric(const char *text);
/*
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
* A string is encodable iff each character is in the following set: 0 to 9, A to Z
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
* Tests whether the given string can be encoded as a segment in alphanumeric
* mode. A string is encodable iff each character is in the following set: 0
* to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus,
* hyphen, period, slash, colon.
*/
public:
static bool isAlphanumeric(const char *text);
@@ -177,16 +186,18 @@ private:
/*
* Creates a new QR Code segment with the given attributes and data.
* The character count (numCh) must agree with the mode and the bit buffer length,
* but the constraint isn't checked. The given bit buffer is copied and stored.
* The character count (numCh) must agree with the mode and the bit buffer
* length, but the constraint isn't checked. The given bit buffer is copied
* and stored.
*/
public:
QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);
/*
* Creates a new QR Code segment with the given parameters and data.
* The character count (numCh) must agree with the mode and the bit buffer length,
* but the constraint isn't checked. The given bit buffer is moved and stored.
* The character count (numCh) must agree with the mode and the bit buffer
* length, but the constraint isn't checked. The given bit buffer is moved and
* stored.
*/
public:
QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);
@@ -211,9 +222,10 @@ public:
public:
const std::vector<bool> &getData() const;
// (Package-private) Calculates the number of bits needed to encode the given segments at
// the given version. Returns a non-negative number if successful. Otherwise returns -1 if a
// segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.
// (Package-private) Calculates the number of bits needed to encode the given
// segments at the given version. Returns a non-negative number if successful.
// Otherwise returns -1 if a segment has too many characters to fit its length
// field, or the total bits exceeds INT_MAX.
public:
static int getTotalBits(const std::vector<QrSegment> &segs, int version);
@@ -228,20 +240,24 @@ private:
/*
* A QR Code symbol, which is a type of two-dimension barcode.
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
* Instances of this class represent an immutable square grid of dark and light cells.
* The class provides static factory functions to create a QR Code from text or binary data.
* The class covers the QR Code Model 2 specification, supporting all versions (sizes)
* from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
* Instances of this class represent an immutable square grid of dark and light
* cells. The class provides static factory functions to create a QR Code from
* text or binary data. The class covers the QR Code Model 2 specification,
* supporting all versions (sizes) from 1 to 40, all 4 error correction levels,
* and 4 character encoding modes.
*
* Ways to create a QR Code object:
* - High level: Take the payload data and call QrCode::encodeText() or QrCode::encodeBinary().
* - Mid level: Custom-make the list of segments and call QrCode::encodeSegments().
* - High level: Take the payload data and call QrCode::encodeText() or
* QrCode::encodeBinary().
* - Mid level: Custom-make the list of segments and call
* QrCode::encodeSegments().
* - Low level: Custom-make the array of data codeword bytes (including
* segment headers and final padding, excluding error correction codewords),
* supply the appropriate version number, and call the QrCode() constructor.
* (Note that all ways require supplying the desired error correction level.)
*/
class QrCode final {
class QrCode final
{
/*---- Public helper enumeration ----*/
@@ -249,11 +265,12 @@ class QrCode final {
* The error correction level in a QR Code symbol.
*/
public:
enum class Ecc {
LOW = 0, // The QR Code can tolerate about 7% erroneous codewords
MEDIUM, // The QR Code can tolerate about 15% erroneous codewords
QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
HIGH, // The QR Code can tolerate about 30% erroneous codewords
enum class Ecc
{
LOW = 0, // The QR Code can tolerate about 7% erroneous codewords
MEDIUM, // The QR Code can tolerate about 15% erroneous codewords
QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
HIGH, // The QR Code can tolerate about 30% erroneous codewords
};
// Returns a value in the range 0 to 3 (unsigned 2-bit integer).
@@ -263,22 +280,24 @@ private:
/*---- Static factory functions (high level) ----*/
/*
* Returns a QR Code representing the given Unicode text string at the given error correction
* level. As a conservative upper bound, this function is guaranteed to succeed for strings that
* have 2953 or fewer UTF-8 code units (not Unicode code points) if the low error correction level
* is used. The smallest possible QR Code version is automatically chosen for the output. The ECC
* level of the result may be higher than the ecl argument if it can be done without increasing
* the version.
* Returns a QR Code representing the given Unicode text string at the given
* error correction level. As a conservative upper bound, this function is
* guaranteed to succeed for strings that have 2953 or fewer UTF-8 code units
* (not Unicode code points) if the low error correction level is used. The
* smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can
* be done without increasing the version.
*/
public:
static QrCode encodeText(const char *text, Ecc ecl);
/*
* Returns a QR Code representing the given binary data at the given error correction level.
* This function always encodes using the binary segment mode, not any text mode. The maximum
* number of bytes allowed is 2953. The smallest possible QR Code version is automatically chosen
* for the output. The ECC level of the result may be higher than the ecl argument if it can be
* done without increasing the version.
* Returns a QR Code representing the given binary data at the given error
* correction level. This function always encodes using the binary segment
* mode, not any text mode. The maximum number of bytes allowed is 2953. The
* smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can
* be done without increasing the version.
*/
public:
static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);
@@ -286,20 +305,22 @@ public:
/*---- Static factory functions (mid level) ----*/
/*
* Returns a QR Code representing the given segments with the given encoding parameters.
* The smallest possible QR Code version within the given range is automatically
* chosen for the output. Iff boostEcl is true, then the ECC level of the result
* may be higher than the ecl argument if it can be done without increasing the
* version. The mask number is either between 0 to 7 (inclusive) to force that
* mask, or -1 to automatically choose an appropriate mask (which may be slow).
* This function allows the user to create a custom sequence of segments that switches
* between modes (such as alphanumeric and byte) to encode text in less space.
* This is a mid-level API; the high-level API is encodeText() and encodeBinary().
* Returns a QR Code representing the given segments with the given encoding
* parameters. The smallest possible QR Code version within the given range is
* automatically chosen for the output. Iff boostEcl is true, then the ECC
* level of the result may be higher than the ecl argument if it can be done
* without increasing the version. The mask number is either between 0 to 7
* (inclusive) to force that mask, or -1 to automatically choose an
* appropriate mask (which may be slow). This function allows the user to
* create a custom sequence of segments that switches between modes (such as
* alphanumeric and byte) to encode text in less space. This is a mid-level
* API; the high-level API is encodeText() and encodeBinary().
*/
public:
static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl, int minVersion = 1,
int maxVersion = 40, int mask = -1,
bool boostEcl = true); // All optional parameters
static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,
int minVersion = 1, int maxVersion = 40,
int mask = -1,
bool boostEcl = true); // All optional parameters
/*---- Instance fields ----*/
@@ -319,9 +340,10 @@ private:
private:
Ecc errorCorrectionLevel;
/* The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
* Even if a QR Code is created with automatic masking requested (mask = -1),
* the resulting object still has a mask value between 0 and 7. */
/* The index of the mask pattern used in this QR Code, which is between 0 and
* 7 (inclusive). Even if a QR Code is created with automatic masking
* requested (mask = -1), the resulting object still has a mask value between
* 0 and 7. */
private:
int mask;
@@ -332,8 +354,8 @@ private:
private:
std::vector<std::vector<bool>> modules;
// Indicates function modules that are not subjected to masking. Discarded when constructor
// finishes.
// Indicates function modules that are not subjected to masking. Discarded
// when constructor finishes.
private:
std::vector<std::vector<bool>> isFunction;
@@ -346,7 +368,8 @@ private:
* A mid-level API is the encodeSegments() function.
*/
public:
QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int msk);
QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords,
int msk);
/*---- Public instance methods ----*/
@@ -375,16 +398,18 @@ public:
int getMask() const;
/*
* Returns the color of the module (pixel) at the given coordinates, which is false
* for light or true for dark. The top left corner has the coordinates (x=0, y=0).
* If the given coordinates are out of bounds, then false (light) is returned.
* Returns the color of the module (pixel) at the given coordinates, which is
* false for light or true for dark. The top left corner has the coordinates
* (x=0, y=0). If the given coordinates are out of bounds, then false (light)
* is returned.
*/
public:
bool getModule(int x, int y) const;
/*---- Private helper methods for constructor: Drawing function modules ----*/
// Reads this object's version field, and draws and marks all function modules.
// Reads this object's version field, and draws and marks all function
// modules.
private:
void drawFunctionPatterns();
@@ -413,19 +438,23 @@ private:
private:
void setFunctionModule(int x, int y, bool isDark);
// Returns the color of the module at the given coordinates, which must be in range.
// Returns the color of the module at the given coordinates, which must be in
// range.
private:
bool module(int x, int y) const;
/*---- Private helper methods for constructor: Codewords and masking ----*/
// Returns a new byte string representing the given data with the appropriate error correction
// codewords appended to it, based on this object's version and error correction level.
// Returns a new byte string representing the given data with the appropriate
// error correction codewords appended to it, based on this object's version
// and error correction level.
private:
std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t> &data) const;
std::vector<std::uint8_t>
addEccAndInterleave(const std::vector<std::uint8_t> &data) const;
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
// data area of this QR Code. Function modules need to be marked off before this is called.
// Draws the given sequence of 8-bit codewords (data and error correction)
// onto the entire data area of this QR Code. Function modules need to be
// marked off before this is called.
private:
void drawCodewords(const std::vector<std::uint8_t> &data);
@@ -437,39 +466,44 @@ private:
private:
void applyMask(int msk);
// Calculates and returns the penalty score based on state of this QR Code's current modules.
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the
// lowest score.
// Calculates and returns the penalty score based on state of this QR Code's
// current modules. This is used by the automatic mask choice algorithm to
// find the mask pattern that yields the lowest score.
private:
long getPenaltyScore() const;
/*---- Private helper functions ----*/
// Returns an ascending list of positions of alignment patterns for this version number.
// Each position is in the range [0,177), and are used on both the x and y axes.
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
// Returns an ascending list of positions of alignment patterns for this
// version number. Each position is in the range [0,177), and are used on both
// the x and y axes. This could be implemented as lookup table of 40
// variable-length lists of unsigned bytes.
private:
std::vector<int> getAlignmentPatternPositions() const;
// Returns the number of data bits that can be stored in a QR Code of the given version number,
// after all function modules are excluded. This includes remainder bits, so it might not be a
// multiple of 8. The result is in the range [208, 29648]. This could be implemented as a 40-entry
// lookup table.
// Returns the number of data bits that can be stored in a QR Code of the
// given version number, after all function modules are excluded. This
// includes remainder bits, so it might not be a multiple of 8. The result is
// in the range [208, 29648]. This could be implemented as a 40-entry lookup
// table.
private:
static int getNumRawDataModules(int ver);
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
// Returns the number of 8-bit data (i.e. not error correction) codewords
// contained in any QR Code of the given version number and error correction
// level, with remainder bits discarded. This stateless pure function could be
// implemented as a (40*4)-cell lookup table.
private:
static int getNumDataCodewords(int ver, Ecc ecl);
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This
// could be implemented as a lookup table over all possible parameter values,
// instead of as an algorithm.
private:
static std::vector<std::uint8_t> reedSolomonComputeDivisor(int degree);
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
// Returns the Reed-Solomon error correction codeword for the given data and
// divisor polynomials.
private:
static std::vector<std::uint8_t>
reedSolomonComputeRemainder(const std::vector<std::uint8_t> &data,
@@ -485,16 +519,17 @@ private:
private:
int finderPenaltyCountPatterns(const std::array<int, 7> &runHistory) const;
// Must be called at the end of a line (row or column) of modules. A helper function for
// getPenaltyScore().
// Must be called at the end of a line (row or column) of modules. A helper
// function for getPenaltyScore().
private:
int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength,
std::array<int, 7> &runHistory) const;
// Pushes the given value to the front and drops the last value. A helper function for
// getPenaltyScore().
// Pushes the given value to the front and drops the last value. A helper
// function for getPenaltyScore().
private:
void finderPenaltyAddHistory(int currentRunLength, std::array<int, 7> &runHistory) const;
void finderPenaltyAddHistory(int currentRunLength,
std::array<int, 7> &runHistory) const;
// Returns true iff the i'th bit of x is set to 1.
private:
@@ -533,19 +568,22 @@ private:
/*---- Public exception class ----*/
/*
* Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception
* include:
* Thrown when the supplied data does not fit any QR Code version. Ways to
* handle this exception include:
* - Decrease the error correction level if it was greater than Ecc::LOW.
* - If the encodeSegments() function was called with a maxVersion argument, then increase
* it if it was less than QrCode::MAX_VERSION. (This advice does not apply to the other
* factory functions because they search all versions up to QrCode::MAX_VERSION.)
* - Split the text data into better or optimal segments in order to reduce the number of bits
* required.
* - If the encodeSegments() function was called with a maxVersion argument,
* then increase it if it was less than QrCode::MAX_VERSION. (This advice does
* not apply to the other factory functions because they search all versions up
* to QrCode::MAX_VERSION.)
* - Split the text data into better or optimal segments in order to reduce the
* number of bits required.
* - Change the text or binary data to be shorter.
* - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
* - Change the text to fit the character set of a particular segment mode (e.g.
* alphanumeric).
* - Propagate the error upward to the caller/user.
*/
class data_too_long : public std::length_error {
class data_too_long : public std::length_error
{
public:
explicit data_too_long(const std::string &msg);
@@ -554,7 +592,8 @@ public:
/*
* An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
*/
class BitBuffer final : public std::vector<bool> {
class BitBuffer final : public std::vector<bool>
{
/*---- Constructor ----*/
@@ -570,4 +609,4 @@ public:
void appendBits(std::uint32_t val, int len);
};
} // namespace qrcodegen
} // namespace qrcodegen