diff --git a/autotests/aztecbarcodetest.cpp b/autotests/aztecbarcodetest.cpp index 09cc742..ca4fb19 100644 --- a/autotests/aztecbarcodetest.cpp +++ b/autotests/aztecbarcodetest.cpp @@ -1,401 +1,404 @@ /* SPDX-FileCopyrightText: 2017 Volker Krause SPDX-License-Identifier: MIT */ #include "../src/lib/aztecbarcode.h" #include "../src/lib/bitvector_p.h" #include #include #include #include #include Q_DECLARE_METATYPE(Prison::BitVector) using namespace Prison; class AztecBarcodeTest : public QObject { Q_OBJECT private Q_SLOTS: void testAztecEncode_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("empty") << QByteArray() << BitVector(); BitVector v; v.appendMSB(12, 5); v.appendMSB(5, 5); v.appendMSB(6, 5); QTest::newRow("all uppper") << QByteArray("KDE") << v; v.clear(); v.appendMSB(28, 5); v.appendMSB(12, 5); v.appendMSB(5, 5); v.appendMSB(6, 5); QTest::newRow("all lower") << QByteArray("kde") << v; v.clear(); v.appendMSB(12, 5); v.appendMSB(28, 5); v.appendMSB(5, 5); v.appendMSB(6, 5); QTest::newRow("upper -> lower latch") << QByteArray("Kde") << v; v.clear(); v.appendMSB(28, 5); v.appendMSB(2, 5); v.appendMSB(29, 5); v.appendMSB(30, 5); v.appendMSB(16, 5); v.appendMSB(16, 5); QTest::newRow("lower -> punct latch") << QByteArray("a++") << v; v.clear(); v.appendMSB(30, 5); v.appendMSB(6, 4); v.appendMSB(4, 4); QTest::newRow("digit") << QByteArray("42") << v; v.clear(); v.appendMSB(29, 5); v.appendMSB(30, 5); v.appendMSB(25, 5); v.appendMSB(24, 5); v.appendMSB(31, 5); v.appendMSB(30, 5); v.appendMSB(11, 4); v.appendMSB(2, 4); QTest::newRow("punct -> digit latch") << QByteArray(">=90") << v; v.clear(); v.appendMSB(30, 5); v.appendMSB(10, 4); v.appendMSB(3, 4); v.appendMSB(14, 4); v.appendMSB(29, 5); v.appendMSB(30, 5); v.appendMSB(13, 5); v.appendMSB(14, 5); QTest::newRow("digit -> punct latch") << QByteArray("81()") << v; v.clear(); v.appendMSB(29, 5); v.appendMSB(11, 5); v.appendMSB(8, 5); QTest::newRow("mixed") << QByteArray("\n\a") << v; v.clear(); v.appendMSB(29, 5); v.appendMSB(30, 5); v.appendMSB(2, 5); v.appendMSB(2, 5); QTest::newRow("CR LF") << QByteArray("\r\n\r\n") << v; v.clear(); v.appendMSB(31, 5); v.appendMSB(2, 5); v.appendMSB(128, 8); v.appendMSB(129, 8); QTest::newRow("binary") << QByteArray("\x80\x81") << v; v.clear(); v.appendMSB(31, 5); v.appendMSB(2, 5); v.appendMSB(255, 8); v.appendMSB(254, 8); v.appendMSB(28, 5); v.appendMSB(3, 5); QTest::newRow("binary/lower") << QByteArray("\xff\xfe" "b") << v; v.clear(); v.appendMSB(12, 5); v.appendMSB(0, 5); v.appendMSB(6, 5); QTest::newRow("upper -> punct shift") << QByteArray("K!") << v; v.clear(); v.appendMSB(30, 5); v.appendMSB(9, 4); v.appendMSB(4, 4); v.appendMSB(15, 4); v.appendMSB(6, 5); v.appendMSB(5, 4); QTest::newRow("digit -> upper shift") << QByteArray("72E3") << v; v.clear(); v.appendMSB(25, 5); v.appendMSB(1, 5); v.appendMSB(26, 5); QTest::newRow("upper space") << QByteArray("X Y") << v; v.clear(); v.appendMSB(20, 5); v.appendMSB(0, 5); v.appendMSB(4, 5); v.appendMSB(23, 5); QTest::newRow("upper punct double char shift") << QByteArray("S, V") << v; v.clear(); v.appendMSB(17, 5); v.appendMSB(0, 5); v.appendMSB(17, 5); v.appendMSB(18, 5); QTest::newRow("upper ambigious punct shift") << QByteArray("P,Q") << v; v.clear(); v.appendMSB(30, 5); v.appendMSB(13, 4); v.appendMSB(7, 4); QTest::newRow("digit ambigious punct latch") << QByteArray(".5") << v; v.clear(); v.appendMSB(29, 5); v.appendMSB(30, 5); v.appendMSB(25, 5); v.appendMSB(26,5); v.appendMSB(31, 5); v.appendMSB(29, 5); v.appendMSB(20, 5); v.appendMSB(29, 5); v.appendMSB(2, 5); QTest::newRow("punct/mixed/upper sequence") << QByteArray(">?@A") << v; } void testAztecEncode() { QFETCH(QByteArray, input); QFETCH(BitVector, output); AztecBarcode code; const auto v = code.aztecEncode(input); if (v != output) { qDebug() << "Actual :" << v; qDebug() << "Expected:" << output; } QCOMPARE(v, output); } void testStuffAndPad_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::addColumn("codeWordSize"); BitVector in, out; QTest::newRow("emtpy") << in << out << 4; in.appendMSB(0x2, 2); out.appendMSB(0xB, 4); QTest::newRow("pad only") << in << out << 4; in.clear(); out.clear(); in.appendMSB(0x3, 2); out.appendMSB(0xE, 4); QTest::newRow("pad only inverted") << in << out << 4; in.clear(); out.clear(); in.appendMSB(0xe0, 8); out.appendMSB(0xe13, 12); QTest::newRow("stuff and pad") << in << out << 4; in.clear(); out.clear(); in.appendMSB(0, 6); out.appendMSB(0x11, 8); QTest::newRow("stuff only") << in << out << 4; } void testStuffAndPad() { QFETCH(BitVector, input); QFETCH(BitVector, output); QFETCH(int, codeWordSize); AztecBarcode code; const auto res= code.bitStuffAndPad(input, codeWordSize); QCOMPARE(res.size(), output.size()); if (res != output) { qDebug() << "Actual :" << res; qDebug() << "Expected:" << output; } QCOMPARE(res, output); } void testFullGrid() { AztecBarcode code; QImage img(151, 151, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintFullGrid(&img); QImage ref(QStringLiteral(":/rendering/aztec-full-grid.png")); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testCompactGrid() { AztecBarcode code; QImage img(27, 27, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintCompactGrid(&img); img.save(QStringLiteral("aztec-compact-grid.png")); QImage ref(QStringLiteral(":/rendering/aztec-compact-grid.png")); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testFullData_data() { QTest::addColumn("data"); QTest::addColumn("refName"); QTest::addColumn("layer"); BitVector v; for (int i = 0; i < 1248; ++i) v.appendLSB(0x9249, 16); QTest::newRow("1001-31") << v << QStringLiteral("aztec-full-data-1001.png") << 32; v.clear(); for (int i = 0; i < 1248 * 8; ++i) v.appendLSB(0x2, 2); QTest::newRow("0101-31") << v << QStringLiteral("aztec-full-data-0101.png") << 32; v.clear(); for (int i = 0; i < 1248; ++i) v.appendLSB(0xffff, 16); QTest::newRow("1111-31") << v << QStringLiteral("aztec-full-data-1111.png") << 32; v.clear(); for (int i = 0; i < 704 * 4; ++i) v.appendLSB(0x1, 2); QTest::newRow("1010-15") << v << QStringLiteral("aztec-full-data-1010.png") << 16; v.clear(); for (int i = 0; i < 16; ++i) v.appendLSB(0xCC, 8); QTest::newRow("0011-0") << v << QStringLiteral("aztec-full-data-0011.png") << 1; } void testFullData() { QFETCH(BitVector, data); QFETCH(QString, refName); QFETCH(int, layer); AztecBarcode code; QImage img(151, 151, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintFullData(&img, data, layer); img.save(refName); QImage ref(QStringLiteral(":/rendering/") + refName); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testFullModeMessage_data() { QTest::addColumn("data"); QTest::addColumn("refName"); BitVector v; for (int i = 0; i < 8; ++i) v.appendMSB(i + 1, 5); QTest::newRow("1234") << v << QStringLiteral("aztec-full-mode-1234.png"); v.clear(); for (int i = 0; i < 8; ++i) v.appendLSB(i + 1, 5); QTest::newRow("1234-rev") << v << QStringLiteral("aztec-full-mode-1234-rev.png"); v.clear(); for (int i = 0; i < 4; ++i) v.appendMSB(0xffff, 10); QTest::newRow("1111") << v << QStringLiteral("aztec-full-mode-1111.png"); } void testFullModeMessage() { QFETCH(BitVector, data); QFETCH(QString, refName); AztecBarcode code; QImage img(151, 151, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintFullModeMessage(&img, data); img.save(refName); QImage ref(QStringLiteral(":/rendering/") + refName); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testCompactData_data() { QTest::addColumn("data"); QTest::addColumn("refName"); QTest::addColumn("layer"); BitVector v; for (int i = 0; i < 304; ++i) v.appendLSB(0x9249, 16); QTest::newRow("1001-3") << v << QStringLiteral("aztec-compact-data-1001.png") << 4; v.clear(); for (int i = 0; i < 608 * 4; ++i) v.appendLSB(0x2, 2); QTest::newRow("0101-3") << v << QStringLiteral("aztec-compact-data-0101.png") << 4; v.clear(); for (int i = 0; i < 304; ++i) v.appendLSB(0xffff, 16); QTest::newRow("1111-3") << v << QStringLiteral("aztec-compact-data-1111.png") << 4; v.clear(); for (int i = 0; i < 102 * 4; ++i) v.appendLSB(0x1, 2); QTest::newRow("1010-2") << v << QStringLiteral("aztec-compact-data-1010.png") << 3; v.clear(); for (int i = 0; i < 13; ++i) v.appendLSB(0xCC, 8); QTest::newRow("0011-0") << v << QStringLiteral("aztec-compact-data-0011.png") << 1; } void testCompactData() { QFETCH(BitVector, data); QFETCH(QString, refName); QFETCH(int, layer); AztecBarcode code; QImage img(27, 27, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintCompactData(&img, data, layer); img.save(refName); QImage ref(QStringLiteral(":/rendering/") + refName); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testCompactModeMessage_data() { QTest::addColumn("data"); QTest::addColumn("refName"); BitVector v; for (int i = 0; i < 4; ++i) v.appendMSB(i + 1, 7); QTest::newRow("1234") << v << QStringLiteral("aztec-compact-mode-1234.png"); v.clear(); for (int i = 0; i < 4; ++i) v.appendLSB(i + 1, 7); QTest::newRow("1234-rev") << v << QStringLiteral("aztec-compact-mode-1234-rev.png"); v.clear(); for (int i = 0; i < 4; ++i) v.appendMSB(0xffff, 7); QTest::newRow("1111") << v << QStringLiteral("aztec-compact-mode-1111.png"); } void testCompactModeMessage() { QFETCH(BitVector, data); QFETCH(QString, refName); AztecBarcode code; QImage img(151, 151, QImage::Format_ARGB32_Premultiplied); img.fill(code.backgroundColor()); code.paintCompactModeMessage(&img, data); img.save(refName); QImage ref(QStringLiteral(":/rendering/") + refName); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testCodeGen_data() { QTest::addColumn("input"); QTest::addColumn("refName"); QTest::newRow("short compact") << QStringLiteral("KF5::Prison") << "aztec-complete-compact1.png"; QTest::newRow("compact 3 layer") << QStringLiteral("M1KRAUSE/VOLKER ABCDEFG TXLRIXBT 0212 309Y014E0063 100") << "aztec-complete-compact3.png"; QTest::newRow("long compact") << QStringLiteral("KF5::Prison - the barcode generation library of KDE Frameworks 5!") << "aztec-complete-compact4.png"; QTest::newRow("short full") << QStringLiteral("KF5::Prison - the MIT licensed free software barcode generation library of KDE Frameworks 5!") << "aztec-complete-full5.png"; QTest::newRow("long full") << QString::fromLatin1( "Permission is hereby granted, free of charge, to any person\n" "obtaining a copy of this software and associated documentation\n" "files (the \"Software\"), to deal in the Software without\n" "restriction, including without limitation the rights to use,\n" "copy, modify, merge, publish, distribute, sublicense, and/or sell\n" "copies of the Software, and to permit persons to whom the\n" "Software is furnished to do so, subject to the following\n" "conditions:\n\n" "The above copyright notice and this permission notice shall be\n" "included in all copies or substantial portions of the Software.\n\n" "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\n" "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n" "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n" "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n" "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n" "OTHER DEALINGS IN THE SOFTWARE.") << "aztec-complete-big.png"; } void testCodeGen() { QFETCH(QString, input); QFETCH(QString, refName); AztecBarcode code; code.setData(input); const auto img = code.paintImage({}); img.save(refName); QImage ref(QStringLiteral(":/encoding/") + refName); ref = ref.convertToFormat(img.format()); QCOMPARE(img, ref); } void testDimension() { std::unique_ptr barcode(Prison::createBarcode(Prison::Aztec)); QVERIFY(barcode); QCOMPARE(barcode->dimensions(), Prison::AbstractBarcode::TwoDimensions); } void testSize() { std::unique_ptr barcode(Prison::createBarcode(Prison::Aztec)); QVERIFY(barcode); QVERIFY(!barcode->minimumSize().isValid()); barcode->setData(QStringLiteral("UNIT TEST")); QCOMPARE(barcode->minimumSize(), QSize(60, 60)); + QCOMPARE(barcode->trueMinimumSize(), QSize(15, 15)); + QCOMPARE(barcode->preferredSize(1), QSize(60, 60)); + QCOMPARE(barcode->preferredSize(2), QSize(30, 30)); QCOMPARE(barcode->toImage(barcode->minimumSize()).size(), QSize(60, 60)); QCOMPARE(barcode->toImage({1, 1}).isNull(), true); } }; QTEST_APPLESS_MAIN(AztecBarcodeTest) #include "aztecbarcodetest.moc" diff --git a/autotests/code128barcodetest.cpp b/autotests/code128barcodetest.cpp index 8965bca..8f3b9f8 100644 --- a/autotests/code128barcodetest.cpp +++ b/autotests/code128barcodetest.cpp @@ -1,118 +1,121 @@ /* SPDX-FileCopyrightText: 2018 Volker Krause SPDX-License-Identifier: MIT */ #include "../src/lib/code128barcode.h" #include "../src/lib/bitvector_p.h" #include #include #include #include Q_DECLARE_METATYPE(Prison::BitVector) using namespace Prison; class Code128BarcodeTest : public QObject { Q_OBJECT private Q_SLOTS: void testEncode_data() { QTest::addColumn("input"); QTest::addColumn("output"); BitVector v; QTest::newRow("empty") << QByteArray() << v; v.appendMSB(1680, 11); v.appendMSB(1554, 11); v.appendMSB(1062, 11); v.appendMSB(1424, 11); v.appendMSB(1220, 11); v.appendMSB(6379, 13); QTest::newRow("all lower") << QByteArray("kde") << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1422, 11); v.appendMSB(1416, 11); v.appendMSB(1128, 11); v.appendMSB(1764, 11); v.appendMSB(6379, 13); QTest::newRow("all uppper") << QByteArray("KDE") << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1614, 11); v.appendMSB(1764, 11); v.appendMSB(6379, 13); QTest::newRow("1 digit") << QByteArray("4") << v; v.clear(); v.appendMSB(1692, 11); v.appendMSB(1464, 11); v.appendMSB(1134, 11); v.appendMSB(6379, 13); QTest::newRow("2 digits") << QByteArray("42") << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1614, 11); v.appendMSB(1650, 11); v.appendMSB(1650, 11); v.appendMSB(1124, 11); v.appendMSB(6379, 13); QTest::newRow("3 digits") << QByteArray("422") << v; v.clear(); v.appendMSB(1692, 11); v.appendMSB(1464, 11); v.appendMSB(1902, 11); v.appendMSB(1782, 11); v.appendMSB(6379, 13); QTest::newRow("4 digits") << QByteArray("4223") << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1814, 11); v.appendMSB(1260, 11); v.appendMSB(1260, 11); v.appendMSB(1896, 11); v.appendMSB(1814, 11); v.appendMSB(6379, 13); QTest::newRow("mixed") << QByteArray("X00Y") << v; v.clear(); v.appendMSB(1668, 11); v.appendMSB(1292, 11); v.appendMSB(1292, 11); v.appendMSB(6379, 13); QTest::newRow("null") << QByteArray("\0", 1) << v; v.clear(); v.appendMSB(1668, 11); v.appendMSB(1422, 11); v.appendMSB(1416, 11); v.appendMSB(1128, 11); v.appendMSB(1292, 11); v.appendMSB(1412, 11); v.appendMSB(6379, 13); QTest::newRow("Code A only") << QByteArray("KDE\0", 4) << v; v.clear(); v.appendMSB(1668, 11); v.appendMSB(1292, 11); v.appendMSB(1518, 11); v.appendMSB(1554, 11); v.appendMSB(1062, 11); v.appendMSB(1424, 11); v.appendMSB(1616, 11); v.appendMSB(6379, 13); QTest::newRow("Start A -> Latch B") << QByteArray("\0kde", 4) << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1554, 11); v.appendMSB(1062, 11); v.appendMSB(1886, 11); v.appendMSB(1292, 11); v.appendMSB(1292, 11); v.appendMSB(1602, 11); v.appendMSB(6379, 13); QTest::newRow("Start B -> Latch A") << QByteArray("kd\0\0", 4) << v; v.clear(); v.appendMSB(1668, 11); v.appendMSB(1292, 11); v.appendMSB(1954, 11); v.appendMSB(1118, 11); v.appendMSB(1590, 11); v.appendMSB(1292, 11); v.appendMSB(1328, 11); v.appendMSB(6379, 13); QTest::newRow("Start A -> Shift B") << QByteArray("\0~@\0", 4) << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1974, 11); v.appendMSB(1954, 11); v.appendMSB(1292, 11); v.appendMSB(1310, 11); v.appendMSB(1844, 11); v.appendMSB(6379, 13); QTest::newRow("Start B -> Shift A") << QByteArray("{\0}", 3) << v; v.clear(); v.appendMSB(1692, 11); v.appendMSB(1436, 11); v.appendMSB(1112, 11); v.appendMSB(1518, 11); v.appendMSB(1304, 11); v.appendMSB(1112, 11); v.appendMSB(1158, 11); v.appendMSB(6379, 13); QTest::newRow("Start C -> Latch B") << QByteArray("1234AB") << v; v.clear(); v.appendMSB(1680, 11); v.appendMSB(1304, 11); v.appendMSB(1112, 11); v.appendMSB(1502, 11); v.appendMSB(1436, 11); v.appendMSB(1112, 11); v.appendMSB(1966, 11); v.appendMSB(6379, 13); QTest::newRow("Start B -> Latch C") << QByteArray("AB1234") << v; } void testEncode() { QFETCH(QByteArray, input); QFETCH(BitVector, output); Code128Barcode code; const auto v = code.encode(input); if (v != output) { qDebug() << "Actual :" << v; qDebug() << "Expected:" << output; } QCOMPARE(v, output); } void testDimension() { std::unique_ptr barcode(Prison::createBarcode(Prison::Code128)); QVERIFY(barcode); QCOMPARE(barcode->dimensions(), Prison::AbstractBarcode::OneDimension); } void testSize() { std::unique_ptr barcode(Prison::createBarcode(Prison::Code128)); QVERIFY(barcode); QVERIFY(!barcode->minimumSize().isValid()); barcode->setData(QStringLiteral("UNIT TEST")); QCOMPARE(barcode->minimumSize(), QSize(154, 10)); + QCOMPARE(barcode->trueMinimumSize(), QSize(154, 1)); + QCOMPARE(barcode->preferredSize(1), QSize(308, 50)); + QCOMPARE(barcode->preferredSize(2), QSize(154, 50)); QCOMPARE(barcode->toImage(barcode->minimumSize()).size(), QSize(154, 10)); QCOMPARE(barcode->toImage({1, 1}).isNull(), true); } }; QTEST_APPLESS_MAIN(Code128BarcodeTest) #include "code128barcodetest.moc" diff --git a/src/lib/abstractbarcode.cpp b/src/lib/abstractbarcode.cpp index 0170448..bbd931b 100644 --- a/src/lib/abstractbarcode.cpp +++ b/src/lib/abstractbarcode.cpp @@ -1,143 +1,163 @@ /* SPDX-FileCopyrightText: 2010-2016 Sune Vuorela SPDX-License-Identifier: MIT */ #include "abstractbarcode.h" #include #include #include #include using namespace Prison; /** * @cond private */ class AbstractBarcode::Private { public: QString m_data; QImage m_cache; QColor m_foreground = Qt::black; QColor m_background = Qt::white; AbstractBarcode::Dimensions m_dimension = AbstractBarcode::NoDimensions; AbstractBarcode* q; bool sizeTooSmall(const QSizeF& size) const { return m_cache.width() > size.width() || m_cache.height() > size.height(); } void recompute() { if (m_cache.isNull() && !m_data.isEmpty()) { m_cache = q->paintImage({}); } } Private(AbstractBarcode* barcode) : q(barcode) { } }; /** * @endcond */ #if PRISON_BUILD_DEPRECATED_SINCE(5, 69) AbstractBarcode::AbstractBarcode() : d(new AbstractBarcode::Private(this)) { } #endif AbstractBarcode::AbstractBarcode(AbstractBarcode::Dimensions dim) : d(new AbstractBarcode::Private(this)) { d->m_dimension = dim; } QString AbstractBarcode::data() const { return d->m_data; } QImage AbstractBarcode::toImage(const QSizeF& size) { d->recompute(); if (d->m_cache.isNull() || d->sizeTooSmall(size)) { return QImage(); } // scale to the requested size, using only full integer factors to keep the code readable int scaleX = std::max(1, size.width() / d->m_cache.width()); int scaleY = std::max(1, size.height() / d->m_cache.height()); if (dimensions() == TwoDimensions) { scaleX = scaleY = std::min(scaleX, scaleY); } QImage out(d->m_cache.width() * scaleX, d->m_cache.height() * scaleY, d->m_cache.format()); QPainter p(&out); p.setRenderHint(QPainter::SmoothPixmapTransform, false); p.drawImage(out.rect(), d->m_cache, d->m_cache.rect()); return out; } void AbstractBarcode::setData(const QString& data) { d->m_data=data; d->m_cache=QImage(); } QSizeF AbstractBarcode::minimumSize() const { d->recompute(); // ### backward compatibility: this is applying minimum size behavior that the specific // implementations were doing prior to 5.69. This is eventually to be dropped. if (d->m_cache.isNull()) { return {}; } switch (d->m_dimension) { case NoDimensions: return {}; case OneDimension: return QSizeF(d->m_cache.width(), std::max(d->m_cache.height(), 10)); case TwoDimensions: return d->m_cache.size() * 4; } return d->m_cache.size(); } +QSizeF AbstractBarcode::trueMinimumSize() const +{ + d->recompute(); + return d->m_cache.size(); +} + +QSizeF AbstractBarcode::preferredSize(qreal devicePixelRatio) const +{ + d->recompute(); + switch (d->m_dimension) { + case NoDimensions: + return {}; + case OneDimension: + return QSizeF(d->m_cache.width() * (devicePixelRatio < 2 ? 2 : 1), std::max(d->m_cache.height(), 50)); + case TwoDimensions: + return d->m_cache.size() * (devicePixelRatio < 2 ? 4 : 2); + } + return {}; +} + void AbstractBarcode::setMinimumSize(const QSizeF& minimumSize) { Q_UNUSED(minimumSize); } const QColor& AbstractBarcode::backgroundColor() const { return d->m_background; } const QColor& AbstractBarcode::foregroundColor() const { return d->m_foreground; } void AbstractBarcode::setBackgroundColor(const QColor& backgroundcolor) { if(backgroundcolor!=backgroundColor()) { d->m_background=backgroundcolor; d->m_cache=QImage(); } } void AbstractBarcode::setForegroundColor(const QColor& foregroundcolor) { if(foregroundcolor!=foregroundColor()) { d->m_foreground=foregroundcolor; d->m_cache=QImage(); } } AbstractBarcode::Dimensions AbstractBarcode::dimensions() const { return d->m_dimension; } AbstractBarcode::~AbstractBarcode() { delete d; } diff --git a/src/lib/abstractbarcode.h b/src/lib/abstractbarcode.h index adab168..17af326 100644 --- a/src/lib/abstractbarcode.h +++ b/src/lib/abstractbarcode.h @@ -1,133 +1,158 @@ /* SPDX-FileCopyrightText: 2010-2016 Sune Vuorela SPDX-License-Identifier: MIT */ #ifndef PRISON_ABSTRACTBARCODE_H #define PRISON_ABSTRACTBARCODE_H #include #include #include #include "prison_export.h" class QColor; class QPainter; namespace Prison { /** * base class for barcode generators * To add your own barcode generator, subclass this class * and reimplement toImage(const QSizeF&) to do the actual * work of generating the barcode. * * The barcode is cached in AbstractBarcode when painting and * the size and the data doesn't change. Using the same AbstractBarcode * to paint on several surfaces, if they aren't of the exact same size * will break the caching */ class PRISON_EXPORT AbstractBarcode { public: #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69) /** * creates a barcode generator without any data * @deprecated since 5.69 Use Prison::createBarcode instead. */ PRISON_DEPRECATED_VERSION(5, 69, "Use Prison::createBarcode()") AbstractBarcode(); #endif virtual ~AbstractBarcode(); /** * @return the QString encoded in this barcode. */ QString data() const; /** * sets the data to be drawn by this function * calling this function does not do any repaints of anything, they are * your own responsibility. If you are using the barcodes thru BarcodeWidget or BarcodeItem, you * should rather use their setData function, as they handle the relevant updates. * @param data QString containing the data */ void setData(const QString& data); /** * Creates a image with a barcode on * @return QImage with a barcode on, trying to match the requested \param size * * If one of the dimensions of @param size is smaller than the matching dimension in \ref minimumSize, * a null QImage will be returned */ QImage toImage(const QSizeF& size) ; /** * The minimal size of this barcode. + * @note This isn't the absolute minimum, but closer to the result of preferredSize(1). * @return the minimal size for this barcode. + * @deprecated since 5.69 Prefer preferredSize() or trueMinimumSize(). */ QSizeF minimumSize() const; + + /** + * The minimal amount of pixels needed to represent this barcode without loss of information. + * That is, the size of the barcode image if each line or dot is just one pixel wide. + * On normal screens that is not enough for barcode scanners to reliably detect the barcode + * though. + * @see preferredSize + * @since 5.69 + */ + QSizeF trueMinimumSize() const; // TODO KF6: rename to minimumSize + + /** + * The recommended size for this barcode when shown on a screen. + * This is typically significantly larger than trueMinimumSize() so that + * barcode scanners tend to reliably detect the code. As this depends + * on the physical resolution of the output, you have to pass the device + * pixel ration of the output screen here. + * @param devicePixelRatio The device pixel ratio of the screen this is shown on. + * @see trueMinimumSize + * @since 5.69 + */ + QSizeF preferredSize(qreal devicePixelRatio) const; + /** * @return the foreground color (by default black) to be used for the barcode. */ const QColor& foregroundColor() const; /** * @return the background color (by default white) to be used for the barcode. */ const QColor& backgroundColor() const; /** * sets the foreground color * @param foregroundcolor - the new foreground color */ void setForegroundColor(const QColor& foregroundcolor); /** * sets the background color * @param backgroundcolor - the new background color */ void setBackgroundColor(const QColor& backgroundcolor); /** Dimensions of the barcode. * @since 5.69 */ enum Dimensions : uint8_t { NoDimensions, ///< Null barcode. OneDimension, ///< One-dimensional barcode. TwoDimensions ///< 2D matrix code. }; /** Returns the amount of dimensions of the barcode. * @since 5.69 */ Dimensions dimensions() const; protected: ///@cond internal explicit AbstractBarcode(Dimensions dim); ///@endcond #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69) /** * Sets the minimum size for this barcode. * Some barcodes have minimum sizes for when they are readable and such * @param minimumSize QSizeF holding the minimum size for this barcode * @deprecated since 5.69, function is a no-op, no need to call this anymore. */ PRISON_DEPRECATED_VERSION(5, 69, "no need to call this anymore") void setMinimumSize(const QSizeF& minimumSize); #endif /** * Doing the actual painting of the image * @param size unused - will be removed in KF6 * @return image with barcode, or null image */ // TODO KF6: remove the size argument virtual QImage paintImage(const QSizeF& size) = 0; private: class Private; /** * d-pointer */ Private* d; }; } //namespace #endif // PRISON_ABSTRACTBARCODE_H diff --git a/src/quick/barcodequickitem.cpp b/src/quick/barcodequickitem.cpp index 20bd669..1f60bd1 100644 --- a/src/quick/barcodequickitem.cpp +++ b/src/quick/barcodequickitem.cpp @@ -1,130 +1,131 @@ /* SPDX-FileCopyrightText: 2018 Volker Krause SPDX-License-Identifier: MIT */ #include "barcodequickitem.h" #include #include +#include #include +#include using namespace Prison; BarcodeQuickItem::BarcodeQuickItem(QQuickItem *parent) : QQuickPaintedItem(parent) { } BarcodeQuickItem::~BarcodeQuickItem() = default; QString BarcodeQuickItem::content() const { return m_content; } void BarcodeQuickItem::setContent(const QString& content) { if (m_content == content) return; m_content = content; emit contentChanged(); updateBarcode(); } BarcodeQuickItem::BarcodeType BarcodeQuickItem::barcodeType() const { return static_cast(m_type); } void BarcodeQuickItem::setBarcodeType(BarcodeQuickItem::BarcodeType type) { if (m_type == static_cast(type)) return; m_type = static_cast(type); emit barcodeTypeChanged(); m_barcode.reset(); updateBarcode(); } QColor BarcodeQuickItem::foregroundColor() const { return m_fgColor; } void BarcodeQuickItem::setForegroundColor(const QColor &color) { if (m_fgColor == color) return; m_fgColor = color; emit foregroundColorChanged(); updateBarcode(); } QColor BarcodeQuickItem::backgroundColor() const { return m_bgColor; } void BarcodeQuickItem::setBackgroundColor(const QColor &color) { if (m_bgColor == color) return; m_bgColor = color; emit backgroundColorChanged(); updateBarcode(); } BarcodeQuickItem::Dimensions Prison::BarcodeQuickItem::dimensions() const { return m_barcode ? static_cast(m_barcode->dimensions()) : NoDimensions; } void BarcodeQuickItem::paint(QPainter* painter) { if (!m_barcode) return; const auto w_max = std::max(implicitWidth(), width()); const auto h_max = std::max(implicitHeight(), height()); const auto img = m_barcode->toImage(QSizeF(w_max, h_max)); const auto x = (w_max - img.width()) / 2; const auto y = (h_max - img.height()) / 2; painter->setRenderHint(QPainter::SmoothPixmapTransform, false); painter->drawImage(QRectF(x, y, img.width(), img.height()), img, img.rect()); } void BarcodeQuickItem::componentComplete() { QQuickPaintedItem::componentComplete(); updateBarcode(); } void BarcodeQuickItem::updateBarcode() { if (!isComponentComplete()) return; if (m_type == Prison::Null || m_content.isEmpty()) { m_barcode.reset(); update(); emit dimensionsChanged(); return; } if (!m_barcode) m_barcode.reset(Prison::createBarcode(m_type)); if (m_barcode) { m_barcode->setData(m_content); m_barcode->setForegroundColor(m_fgColor); m_barcode->setBackgroundColor(m_bgColor); - // minimumSize() is only providing valid results after the first call to toImage()... - m_barcode->toImage(m_barcode->minimumSize()); - setImplicitSize(m_barcode->minimumSize().width(), m_barcode->minimumSize().height()); + const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio()); + setImplicitSize(size.width(), size.height()); } update(); emit dimensionsChanged(); }