File indexing completed on 2024-05-12 15:49:08

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "pdf417barcode.h"
0008 #include "zxingutil_p.h"
0009 
0010 #include <ZXing/BitMatrix.h>
0011 #include <ZXing/MultiFormatWriter.h>
0012 
0013 #include <stdexcept>
0014 
0015 using namespace Prison;
0016 
0017 Pdf417Barcode::Pdf417Barcode()
0018     : AbstractBarcode(TwoDimensions)
0019 {
0020 }
0021 
0022 QImage Pdf417Barcode::paintImage(const QSizeF &size)
0023 {
0024     Q_UNUSED(size);
0025 
0026     std::wstring input;
0027     const bool isBinary = data().isEmpty();
0028     if (!isBinary) {
0029         input = data().toStdWString();
0030     } else {
0031         const auto b = byteArrayData();
0032         input.reserve(b.size());
0033         // ensure we explicitly copy unsigned bytes here, ie. each byte ends up in the least significant byte of
0034         // the std::wstring. If we end up converting to a signed value inbetween, values > 127 end up negative and
0035         // will be wrongly represented in the std::wstring
0036         for (uint8_t c : b) {
0037             input.push_back(c);
0038         }
0039     }
0040 
0041     try {
0042         ZXing::MultiFormatWriter writer(ZXing::BarcodeFormat::PDF417);
0043         // ISO/IEC 15438:2006(E) ยง5.8.3 Quiet Zone
0044         writer.setMargin(2);
0045         if (isBinary) {
0046             writer.setEncoding(ZXing::CharacterSet::BINARY);
0047         }
0048         // aspect ratio 4 is hard-coded in ZXing
0049         const auto matrix = writer.encode(input, 4, 1);
0050         return ZXingUtil::toImage(matrix, foregroundColor(), backgroundColor());
0051     } catch (const std::invalid_argument &e) {
0052     }; // input too large
0053     return {};
0054 }