Commit 0d2e0bf1 authored by Volker Krause's avatar Volker Krause
Browse files

Decouple JS barcode API from generic PDF extractor code

That is about to be removed.
parent 9092eff8
......@@ -58,6 +58,7 @@ set(kitinerary_lib_srcs
knowledgedb/timezonedb.cpp
knowledgedb/trainstationdb.cpp
pdf/pdfbarcodeutil.cpp
pdf/pdfdocument.cpp
pdf/pdfextractoroutputdevice.cpp
pdf/pdfimage.cpp
......
......@@ -6,8 +6,8 @@
#include "barcode.h"
#include "bitarray.h"
#include <generic/genericpdfextractor_p.h>
#include <era/ssbticket.h>
#include <pdf/pdfbarcodeutil_p.h>
#include <KItinerary/BarcodeDecoder>
#include <KItinerary/IataBcbpParser>
......@@ -29,7 +29,7 @@ QString JsApi::Barcode::decodePdf417(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto pdfImg = img.value<PdfImage>();
if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::PDF417)) {
if (!PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::PDF417)) {
return {};
}
return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::PDF417);
......@@ -41,7 +41,7 @@ QString JsApi::Barcode::decodeAztec(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto pdfImg = img.value<PdfImage>();
if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) {
if (!PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) {
return {};
}
return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::Aztec);
......@@ -53,7 +53,7 @@ QVariant JsApi::Barcode::decodeAztecBinary(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto pdfImg = img.value<PdfImage>();
if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) {
if (!PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) {
return {};
}
const auto content = m_decoder->decodeBinary(pdfImg.image(), BarcodeDecoder::Aztec);
......@@ -69,7 +69,7 @@ QString JsApi::Barcode::decodeQR(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto pdfImg = img.value<PdfImage>();
if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::QRCode)) {
if (!PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::QRCode)) {
return {};
}
return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::QRCode);
......@@ -81,7 +81,7 @@ QString JsApi::Barcode::decodeDataMatrix(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto pdfImg = img.value<PdfImage>();
if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::DataMatrix)) {
if (!PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::DataMatrix)) {
return {};
}
return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::DataMatrix);
......
/*
SPDX-FileCopyrightText: 2018-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "pdfbarcodeutil_p.h"
#include "pdfimage.h"
using namespace KItinerary;
enum {
// unit is 1/72 inch, assuming landscape orientation
MinTargetImageHeight = 28,
MinTargetImageWidth = 36,
MaxTargetImageHeight = 252,
MaxTargetImageWidth = 252,
};
bool PdfBarcodeUtil::maybeBarcode(const PdfImage &img, BarcodeDecoder::BarcodeTypes hint)
{
const auto w = img.width();
const auto h = img.height();
if (!BarcodeDecoder::isPlausibleSize(img.sourceWidth(), img.sourceHeight()) || !BarcodeDecoder::isPlausibleAspectRatio(w, h, hint)) {
return false;
}
// image target size checks
if (std::min(w, h) < MinTargetImageHeight || std::max(w, h) < MinTargetImageWidth || h > MaxTargetImageHeight || w > MaxTargetImageWidth) {
return false;
}
return true;
}
/*
SPDX-FileCopyrightText: 2018-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KITINERARY_PDFBARCODEUTIL_H
#define KITINERARY_PDFBARCODEUTIL_H
#include "barcodedecoder.h"
namespace KItinerary {
class PdfImage;
/** Helper functions for detecting barcodes in PDF data. */
namespace PdfBarcodeUtil
{
/** Quick pre-check without image decoding if @p img might be a barcode. */
bool maybeBarcode(const PdfImage &img, BarcodeDecoder::BarcodeTypes hint = BarcodeDecoder::Any);
}
}
#endif // KITINERARY_PDFBARCODEUTIL_H
......@@ -5,6 +5,7 @@
*/
#include "pdfdocumentprocessor.h"
#include "pdf/pdfbarcodeutil_p.h"
#include <KItinerary/BarcodeDecoder>
#include <KItinerary/ExtractorDocumentNodeFactory>
......@@ -23,11 +24,6 @@ Q_DECLARE_METATYPE(KItinerary::Internal::OwnedPtr<KItinerary::PdfDocument>)
enum {
MaxPageCount = 10, // maximum in the current test set is 6
MaxFileSize = 4000000, // maximum in the current test set is 980kB
// unit is 1/72 inch, assuming landscape orientation
MinTargetImageHeight = 28,
MinTargetImageWidth = 36,
MaxTargetImageHeight = 252,
MaxTargetImageWidth = 252,
};
PdfDocumentProcessor::PdfDocumentProcessor() = default;
......@@ -80,23 +76,6 @@ ExtractorDocumentNode PdfDocumentProcessor::createNodeFromContent(const QVariant
return node;
}
static bool maybeBarcode(const PdfImage &img)
{
const auto w = img.width();
const auto h = img.height();
if (!BarcodeDecoder::isPlausibleSize(img.sourceWidth(), img.sourceHeight()) || !BarcodeDecoder::isPlausibleAspectRatio(w, h)) {
return false;
}
// image target size checks
if (std::min(w, h) < MinTargetImageHeight || std::max(w, h) < MinTargetImageWidth || h > MaxTargetImageHeight || w > MaxTargetImageWidth) {
return false;
}
return true;
}
void PdfDocumentProcessor::expandNode(ExtractorDocumentNode &node, const ExtractorEngine *engine) const
{
const auto doc = node.content<PdfDocument*>();
......@@ -112,7 +91,7 @@ void PdfDocumentProcessor::expandNode(ExtractorDocumentNode &node, const Extract
continue;
}
if (!maybeBarcode(img)) {
if (!PdfBarcodeUtil::maybeBarcode(img)) {
continue;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment