Skip to content

Commit

Permalink
Add typeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
EyreFree committed Jul 10, 2023
1 parent f92822e commit aecbafe
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 10 deletions.
14 changes: 9 additions & 5 deletions Sources/QRCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class QRCode {
public let correctLevel: QRErrorCorrectLevel
/// If the image codes has a border around its content.
public let hasBorder: Bool
private let model: QRCodeModel
public let model: QRCodeModel

/// Construct a QRCode instance.
/// - Parameters:
Expand All @@ -42,13 +42,15 @@ open class QRCode {
_ text: String,
encoding: String.Encoding = .utf8,
errorCorrectLevel: QRErrorCorrectLevel = .H,
withBorder hasBorder: Bool = true
withBorder hasBorder: Bool = true,
needTypeTable: Bool = false
) throws {
guard let data = text.data(using: encoding) else {
throw QRCodeError.text(text, incompatibleWithEncoding: encoding)
}
try self.init(data, errorCorrectLevel: errorCorrectLevel,
withBorder: hasBorder)
withBorder: hasBorder,
needTypeTable: needTypeTable)
}

/// Construct a QRCode instance.
Expand All @@ -60,10 +62,12 @@ open class QRCode {
public init(
_ data: Data,
errorCorrectLevel: QRErrorCorrectLevel = .H,
withBorder hasBorder: Bool = true
withBorder hasBorder: Bool = true,
needTypeTable: Bool = false
) throws {
self.model = try QRCodeModel(data: data,
errorCorrectLevel: errorCorrectLevel)
errorCorrectLevel: errorCorrectLevel,
needTypeTable: needTypeTable)
self.correctLevel = errorCorrectLevel
self.hasBorder = hasBorder
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/QRCodeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
SOFTWARE.
*/

import Foundation

/// All possible errors that could occur when constructing `QRCode`.
public enum QRCodeError: Error {
/// The thing you want to save is too large for `QRCode`.
Expand Down
90 changes: 85 additions & 5 deletions Sources/QRCodeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@

import Foundation

struct QRCodeModel {
let typeNumber: Int
let errorCorrectLevel: QRErrorCorrectLevel
public struct QRCodeModel {
public let typeNumber: Int
public let errorCorrectLevel: QRErrorCorrectLevel
private var modules: [[Bool?]]! = nil
private(set) var moduleCount = 0
public private(set) var moduleCount = 0
private let encodedText: QR8bitByte
private var dataCache: [Int]
public let needTypeTable: Bool
private var position: [[Int]] = []

init(data: Data,
errorCorrectLevel: QRErrorCorrectLevel) throws {
errorCorrectLevel: QRErrorCorrectLevel,
needTypeTable: Bool = false) throws {
self.needTypeTable = needTypeTable

self.encodedText = QR8bitByte(parsedData: data)

self.typeNumber = try QRCodeType
Expand Down Expand Up @@ -109,13 +114,23 @@ struct QRCodeModel {
#else
let pos = QRPatternLocator[typeNumber]
#endif

if needTypeTable {
self.position = []
}

for i in pos.indices {
for j in pos.indices {
let row = pos[i]
let col = pos[j]
if modules[row][col] != nil {
continue
}

if needTypeTable {
self.position.append([row, col])
}

for r in -2...2 {
for c in -2...2 {
if r == -2 || r == 2 || c == -2 || c == 2 || r == 0 && c == 0 {
Expand Down Expand Up @@ -400,3 +415,68 @@ extension QRCodeModel {
return lostPoint
}
}

extension QRCodeModel {

public func getTypeTable() -> [[QRPointType]] {
if !needTypeTable {
return []
}

let nCount: Int = self.moduleCount
let position: [[Int]] = self.position
let PD: [[Int]] = [[3, 3], [3, nCount - 4], [nCount - 4, 3]]

var typeTable: [[QRPointType]] = Array(repeating: Array(repeating: QRPointType.data, count: nCount), count: nCount)

for i in 8..<nCount - 7 {
typeTable[i][6] = QRPointType.timing
typeTable[6][i] = QRPointType.timing
}

for i in 0..<position.count {
typeTable[position[i][0]][position[i][1]] = QRPointType.alignCenter
for r in -2...2 {
for c in -2...2 {
if !(r == 0 && c == 0) {
typeTable[position[i][0] + r][position[i][1] + c] = QRPointType.alignOther
}
}
}
}

for i in 0..<PD.count {
typeTable[PD[i][0]][PD[i][1]] = QRPointType.posCenter
for r in -4...4 {
for c in -4...4 {
if PD[i][0] + r >= 0 && PD[i][0] + r < nCount && PD[i][1] + c >= 0 && PD[i][1] + c < nCount {
if !(r == 0 && c == 0) {
typeTable[PD[i][0] + r][PD[i][1] + c] = QRPointType.posOther
}
}
}
}
}

for i in 0...8 {
if i != 6 {
typeTable[i][8] = QRPointType.format
typeTable[8][i] = QRPointType.format
}
if i < 7 {
typeTable[nCount - i - 1][8] = QRPointType.format
}
if i < 8 {
typeTable[8][nCount - i - 1] = QRPointType.format
}
}

for i in (nCount - 11)...(nCount - 9) {
for j in 0...5 {
typeTable[i][j] = QRPointType.version
typeTable[j][i] = QRPointType.version
}
}
return typeTable
}
}
34 changes: 34 additions & 0 deletions Sources/QRPointType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright (c) 2017-2020 ApolloZhu <[email protected]>

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.
*/

import Foundation

public enum QRPointType: Int {
case data = 0
case posCenter = 1
case posOther = 2
case alignCenter = 3
case alignOther = 4
case timing = 5
case format = 6
case version = 7
}
4 changes: 4 additions & 0 deletions swift_qrcodejs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
122A6CC22A5C2BA600EE18B1 /* QRPointType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 122A6CC12A5C2BA600EE18B1 /* QRPointType.swift */; };
OBJ_33 /* BCH.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_8 /* BCH.swift */; };
OBJ_34 /* QR8bitByte.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* QR8bitByte.swift */; };
OBJ_35 /* QRBitBuffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* QRBitBuffer.swift */; };
Expand Down Expand Up @@ -58,6 +59,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
122A6CC12A5C2BA600EE18B1 /* QRPointType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRPointType.swift; sourceTree = "<group>"; };
OBJ_10 /* QRBitBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRBitBuffer.swift; sourceTree = "<group>"; };
OBJ_11 /* QRCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCode.swift; sourceTree = "<group>"; };
OBJ_12 /* QRCodeError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -136,6 +138,7 @@
OBJ_7 /* Sources */ = {
isa = PBXGroup;
children = (
122A6CC12A5C2BA600EE18B1 /* QRPointType.swift */,
OBJ_8 /* BCH.swift */,
OBJ_9 /* QR8bitByte.swift */,
OBJ_10 /* QRBitBuffer.swift */,
Expand Down Expand Up @@ -238,6 +241,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 0;
files = (
122A6CC22A5C2BA600EE18B1 /* QRPointType.swift in Sources */,
OBJ_33 /* BCH.swift in Sources */,
OBJ_34 /* QR8bitByte.swift in Sources */,
OBJ_35 /* QRBitBuffer.swift in Sources */,
Expand Down

0 comments on commit aecbafe

Please sign in to comment.