|
| 1 | +// |
| 2 | +// Copyright © 2025 Darren Ford. All rights reserved. |
| 3 | +// |
| 4 | +// MIT license |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 7 | +// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 8 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | +// permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 12 | +// portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 15 | +// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 16 | +// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 17 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | +// |
| 19 | + |
| 20 | +import CoreGraphics |
| 21 | +import Foundation |
| 22 | + |
| 23 | +public extension QRCode.PixelInset { |
| 24 | + /// A pixel inset generator that creates a bigger inset the closer to the center of the QR code |
| 25 | + @objc(QRCodePixelInsetPinch) |
| 26 | + class Pinch: NSObject, QRCodePixelInsetGenerator { |
| 27 | + public static var Name: String { "pinch" } |
| 28 | + public static func Create() -> QRCodePixelInsetGenerator { QRCode.PixelInset.Pinch() } |
| 29 | + public func reset() {} |
| 30 | + public func copyInsetGenerator() -> QRCodePixelInsetGenerator { QRCode.PixelInset.Pinch() } |
| 31 | + public func insetValue(for matrix: BoolMatrix, row: Int, column: Int, insetFraction: CGFloat) -> CGFloat { |
| 32 | + assert(insetFraction.in(0 ... 1)) |
| 33 | + let half = Double(matrix.dimension) / 2 |
| 34 | + |
| 35 | + let cc = Double(column) - half |
| 36 | + let cr = Double(row) - half |
| 37 | + |
| 38 | + let len = sqrt(cc*cc + cr*cr) / half |
| 39 | + return len * insetFraction |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments