From a3decd835c3f83cbefc282fb03c7dc32c1e448c7 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 24 Feb 2020 23:31:23 +0100 Subject: [PATCH] Port SVG conversion to Swift --- swift/Sources/QRCodeGenerator/QRCode.swift | 53 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/swift/Sources/QRCodeGenerator/QRCode.swift b/swift/Sources/QRCodeGenerator/QRCode.swift index 7114e97..a6320ce 100644 --- a/swift/Sources/QRCodeGenerator/QRCode.swift +++ b/swift/Sources/QRCodeGenerator/QRCode.swift @@ -29,16 +29,16 @@ struct QRCode { /// The version number of this QR Code, which is between 1 and 40 (inclusive). /// This determines the size of this barcode. - private let version: QRCodeVersion + public let version: QRCodeVersion /// The width and height of this QR Code, measured in modules, between /// 21 and 177 (inclusive). This is equal to version * 4 + 17. - private let size: Int + public let size: Int /// The error correction level used in this QR Code. - private let errorCorrectionLevel: QRCodeECC + public let errorCorrectionLevel: QRCodeECC /// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). /// Even if a QR Code is created with automatic masking requested (mask = None), /// the resulting object still has a mask value between 0 and 7. - private var mask: QRCodeMask + public private(set) var mask: QRCodeMask // Grids of modules/pixels, with dimensions of size*size: @@ -232,4 +232,49 @@ struct QRCode { result.isFunction = [] return result } + + /*---- Public methods ----*/ + + /// Returns the color of the module (pixel) at the given coordinates, + /// which is `false` for white or `true` for black. + /// + /// The top left corner has the coordinates (x=0, y=0). If the given + /// coordinates are out of bounds, then `false` (white) is returned. + public func getModule(_ x: Int, _ y: Int) -> Bool { + 0 <= x && x < size && 0 <= y && y < size && self[x, y] + } + + private subscript(_ x: Int, _ y: Int) -> Bool { + /// Returns the color of the module at the given coordinates, which + /// are assumed to be in bounds. + get { modules[y * size + x] } + /// Sets the color of the module at the given coordintes. + set { modules[y * size + x] = newValue } + } + + /// Returns a string of SVG code for an image depicting + /// this QR Code, with the given number of border modules. + /// + /// The string always uses Unix newlines (\n), regardless of the platform. + public func toSVGString(border: Int) -> String { + assert(border >= 0, "Border must be non-negative") + let dimension = size + (border * 2) + let path = (0.. + + + + + + + """ + return result + } }