From f314d018ea18459fbb25a9522fbdc9d4f87e02ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Fri, 5 Jan 2024 13:24:35 +0100 Subject: [PATCH 1/2] Codabar: Fixed Codabar generator implementation - Renamed class to RSCodaBarGenerator - Ensured compatibilty with Apple Vision framework payloads (Vision Codabar payload includes the initiator and terminator of Codabar barcodes as the specification does not require them) - Fixed RSUnifiedCodeValidator.isValid support of Codabar barcodes --- ...nerator.swift => RSCodabarGenerator.swift} | 66 +++---------------- Source/RSCodeDataMatrixGenerator.swift | 14 ---- Source/RSDataMatrixGenerator.swift | 17 +++++ Source/RSUnifiedCodeGenerator.swift | 2 +- Source/RSUnifiedCodeValidator.swift | 13 ++-- 5 files changed, 35 insertions(+), 77 deletions(-) rename Source/{RSCodaBarGenerator.swift => RSCodabarGenerator.swift} (52%) delete mode 100644 Source/RSCodeDataMatrixGenerator.swift create mode 100644 Source/RSDataMatrixGenerator.swift diff --git a/Source/RSCodaBarGenerator.swift b/Source/RSCodabarGenerator.swift similarity index 52% rename from Source/RSCodaBarGenerator.swift rename to Source/RSCodabarGenerator.swift index aca164a..faf064e 100644 --- a/Source/RSCodaBarGenerator.swift +++ b/Source/RSCodabarGenerator.swift @@ -1,5 +1,5 @@ // -// RSCodaBarGenerator.swift +// RSCodeCodabarGenerator.swift // RSBarcodes // // Created by 山崎謙登 on 2022/11/14. @@ -10,26 +10,6 @@ import Foundation private let CODABAR_ALPHABET_STRING = "0123456789-$:/.+ABCD" -/// Special characters that can be the initiator or the terminator of CodaBar -public enum RSCodaBarSpecialCharacterType { - case auto - case a, b, c, d - - /// Index of each character - var index: Int { - switch self { - case .auto, .a: - return 16 - case .b: - return 17 - case .c: - return 19 - case .d: - return 20 - } - } -} - /// https://www.keyence.com/ss/products/auto_id/codereader/basic/code39.jsp /// https://en.wikipedia.org/wiki/Codabar @available(macCatalyst 14.0, *) @@ -65,49 +45,21 @@ open class RSCodaBarGenerator: RSAbstractCodeGenerator { let location = CODABAR_ALPHABET_STRING.location(characterString) return CODABAR_CHARACTER_ENCODINGS[location] } - - private let initialCharactorType: RSCodaBarSpecialCharacterType - private let terminatorCharactorType: RSCodaBarSpecialCharacterType - - init( - initialCharactorType: RSCodaBarSpecialCharacterType = .auto, - terminatorCharactorType: RSCodaBarSpecialCharacterType = .auto - ) { - self.initialCharactorType = initialCharactorType - self.terminatorCharactorType = terminatorCharactorType - } - + // MARK: RSAbstractCodeGenerator override open func isValid(_ contents: String) -> Bool { // Contents length is greater than 0 and // Contents do not include start and stop characters - if contents.length() > 0 - && contents.range(of: "A") == nil - && contents.range(of: "B") == nil - && contents.range(of: "C") == nil - && contents.range(of: "D") == nil { - - for character in contents { - let location = CODABAR_ALPHABET_STRING.location(String(character)) - if location == NSNotFound { - // Contents include any character that is not allowed as CODABAR - return false - } + guard contents.length() > 0 else { return false } + for character in contents { + let location = CODABAR_ALPHABET_STRING.location(String(character)) + if location == NSNotFound { + // Contents include any character that is not allowed as CODABAR + return false } - - return true - } else { - return false } - } - - override open func initiator() -> String { - return CODABAR_CHARACTER_ENCODINGS[initialCharactorType.index] - } - - override open func terminator() -> String { - return CODABAR_CHARACTER_ENCODINGS[terminatorCharactorType.index] + return true } override open func barcode(_ contents: String) -> String { diff --git a/Source/RSCodeDataMatrixGenerator.swift b/Source/RSCodeDataMatrixGenerator.swift deleted file mode 100644 index 98aafa4..0000000 --- a/Source/RSCodeDataMatrixGenerator.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// RSCodeDataMatrixGenerator.swift -// RSBarcodes -// -// Created by R0CKSTAR on 15/10/26. -// Copyright (c) 2015 P.D.Q. All rights reserved. -// - -import UIKit - -@available(macCatalyst 14.0, *) -class RSCodeDataMatrixGenerator: RSAbstractCodeGenerator { - -} diff --git a/Source/RSDataMatrixGenerator.swift b/Source/RSDataMatrixGenerator.swift new file mode 100644 index 0000000..4d568b3 --- /dev/null +++ b/Source/RSDataMatrixGenerator.swift @@ -0,0 +1,17 @@ +// +// RSCodeDataMatrixGenerator.swift +// RSBarcodes +// +// Created by R0CKSTAR on 15/10/26. +// Copyright (c) 2015 P.D.Q. All rights reserved. +// + +import UIKit + +@available(macCatalyst 14.0, *) +class RSDataMatrixGenerator: RSAbstractCodeGenerator { + override func isValid(_ contents: String) -> Bool { + // TODO: Implement this once DataMatrix generator is implemented. + return false + } +} diff --git a/Source/RSUnifiedCodeGenerator.swift b/Source/RSUnifiedCodeGenerator.swift index 6dcfc9f..52a6193 100644 --- a/Source/RSUnifiedCodeGenerator.swift +++ b/Source/RSUnifiedCodeGenerator.swift @@ -57,7 +57,7 @@ open class RSUnifiedCodeGenerator: RSCodeGenerator { codeGenerator = RSCode128Generator() } case AVMetadataObject.ObjectType.dataMatrix.rawValue: - codeGenerator = RSCodeDataMatrixGenerator() + codeGenerator = RSDataMatrixGenerator() case RSBarcodesTypeISBN13Code: codeGenerator = RSISBN13Generator() case RSBarcodesTypeISSN13Code: diff --git a/Source/RSUnifiedCodeValidator.swift b/Source/RSUnifiedCodeValidator.swift index 32f35b6..ba652f3 100644 --- a/Source/RSUnifiedCodeValidator.swift +++ b/Source/RSUnifiedCodeValidator.swift @@ -38,10 +38,8 @@ open class RSUnifiedCodeValidator { codeGenerator = RSCode93Generator() case AVMetadataObject.ObjectType.code128.rawValue: codeGenerator = RSCode128Generator() - /** TODO: Uncomment this once DataMatrix generator is implemented. case AVMetadataObject.ObjectType.dataMatrix.rawValue: - codeGenerator = RSCodeDataMatrixGenerator() - */ + codeGenerator = RSDataMatrixGenerator() case RSBarcodesTypeISBN13Code: codeGenerator = RSISBN13Generator() case RSBarcodesTypeISSN13Code: @@ -49,8 +47,13 @@ open class RSUnifiedCodeValidator { case RSBarcodesTypeExtendedCode39Code: codeGenerator = RSExtendedCode39Generator() default: - print("No code generator selected.") - return false + if #available(iOS 15.4, *), machineReadableCodeObjectType == AVMetadataObject.ObjectType.codabar.rawValue { + codeGenerator = RSCodaBarGenerator() + } + else { + print("No code generator selected.") + return false + } } return codeGenerator!.isValid(contents) } From ede410f6e493d9b81ec1540e45a9fb0ba80e62a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Fri, 5 Jan 2024 13:25:56 +0100 Subject: [PATCH 2/2] Codabar: Fixed Codabar generator implementation - Renamed class to RSCodaBarGenerator - Ensured compatibilty with Apple Vision framework payloads (Vision Codabar payload includes the initiator and terminator of Codabar barcodes as the specification does not require them) - Fixed RSUnifiedCodeValidator.isValid support of Codabar barcodes --- Source/RSCodabarGenerator.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/RSCodabarGenerator.swift b/Source/RSCodabarGenerator.swift index faf064e..b3e53c1 100644 --- a/Source/RSCodabarGenerator.swift +++ b/Source/RSCodabarGenerator.swift @@ -1,5 +1,5 @@ // -// RSCodeCodabarGenerator.swift +// RSCodabarGenerator.swift // RSBarcodes // // Created by 山崎謙登 on 2022/11/14.