From 315d174320d2d33c00a4dd348d94ba4d0da043c8 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 24 Feb 2020 23:50:19 +0100 Subject: [PATCH] Port .drawFinderPattern and .drawAlignmentPattern to Swift --- swift/Sources/QRCodeGenerator/QRCode.swift | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/swift/Sources/QRCodeGenerator/QRCode.swift b/swift/Sources/QRCodeGenerator/QRCode.swift index 121527b..9221be2 100644 --- a/swift/Sources/QRCodeGenerator/QRCode.swift +++ b/swift/Sources/QRCodeGenerator/QRCode.swift @@ -367,4 +367,36 @@ struct QRCode { setFunctionModule(x: b, y: a, isBlack: bit) } } + + /// Draws a 9*9 finder pattern including the border separator, + /// with the center module at (x, y). Modules can be out of bounds. + private mutating func drawFinderPattern(x: Int, y: Int) { + for dy in -4...4 { + for dx in -4...4 { + let xx: Int = x + dx + let yy: Int = y + dy + if 0 <= xx && xx < size && 0 <= yy && yy < size { + let dist: Int = max(abs(dx), abs(dy)) + setFunctionModule(x: xx, y: yy, isBlack: dist != 2 && dist != 4) + } + } + } + } + + /// Draws a 5*5 alignment pattern, with the center module + /// at (x, y). All modules must be in bounds. + private mutating func drawAlignmentPattern(x: Int, y: Int) { + for dy in -2...2 { + for dx in -2...2 { + setFunctionModule(x: x + dx, y: y + dy, isBlack: max(abs(dx), abs(dy)) != 1) + } + } + } + + /// Sets the color of a module and marks it as a function mdoule. + /// Only used by the constructor. Coordinates must be in bounds. + private mutating func setFunctionModule(x: Int, y: Int, isBlack: Bool) { + self[x, y] = isBlack + isFunction[y * size + x] = true + } }