|
| 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.EyeShape { |
| 24 | + @objc(QRCodeEyeShapeHolePunch) class HolePunch: NSObject, QRCodeEyeShapeGenerator { |
| 25 | + @objc public static let Name = "holePunch" |
| 26 | + @objc public static var Title: String { "Hole Punch" } |
| 27 | + @objc public static func Create(_ settings: [String: Any]?) -> any QRCodeEyeShapeGenerator { HolePunch() } |
| 28 | + |
| 29 | + @objc public func settings() -> [String: Any] { return [:] } |
| 30 | + @objc public func supportsSettingValue(forKey key: String) -> Bool { false } |
| 31 | + @objc public func setSettingValue(_ value: Any?, forKey key: String) -> Bool { false } |
| 32 | + |
| 33 | + /// Make a copy of this shape generator |
| 34 | + @objc public func copyShape() -> any QRCodeEyeShapeGenerator { HolePunch() } |
| 35 | + |
| 36 | + /// Reset the eye shape generator back to defaults |
| 37 | + @objc public func reset() { } |
| 38 | + |
| 39 | + /// Returns the path representing the eye-outer shape |
| 40 | + public func eyePath() -> CGPath { eyeFrameOuter__ } |
| 41 | + /// Returns the path representing the background shape for the entire eye |
| 42 | + public func eyeBackgroundPath() -> CGPath { eyeFrameBackground__ } |
| 43 | + |
| 44 | + private static let _defaultPupil = QRCode.PupilShape.Circle() |
| 45 | + /// Returns the generator for the default pixel shape |
| 46 | + public func defaultPupil() -> any QRCodePupilShapeGenerator { Self._defaultPupil } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +public extension QRCodeEyeShapeGenerator where Self == QRCode.EyeShape.HolePunch { |
| 51 | + /// Create a dot drag eye shape generator |
| 52 | + /// - Returns: An eye shape generator |
| 53 | + @inlinable static func holePunch() -> QRCodeEyeShapeGenerator { QRCode.EyeShape.HolePunch() } |
| 54 | +} |
| 55 | + |
| 56 | +// MARK: - Path generation |
| 57 | + |
| 58 | +private let eyeFrameBackground__ = CGPath.make { eyebackgroundPath in |
| 59 | + eyebackgroundPath.move(to: CGPoint(x: 0, y: 0)) |
| 60 | + eyebackgroundPath.line(to: CGPoint(x: 90, y: 0)) |
| 61 | + eyebackgroundPath.line(to: CGPoint(x: 90, y: 90)) |
| 62 | + eyebackgroundPath.line(to: CGPoint(x: 0, y: 90)) |
| 63 | + eyebackgroundPath.line(to: CGPoint(x: 0, y: 0)) |
| 64 | + eyebackgroundPath.close() |
| 65 | +} |
| 66 | + |
| 67 | +private let eyeFrameOuter__ = CGPath.make { eyeouterPath in |
| 68 | + eyeouterPath.move(to: CGPoint(x: 45, y: 70)) |
| 69 | + eyeouterPath.curve(to: CGPoint(x: 20, y: 45), controlPoint1: CGPoint(x: 31.19, y: 70), controlPoint2: CGPoint(x: 20, y: 58.81)) |
| 70 | + eyeouterPath.curve(to: CGPoint(x: 34.91, y: 22.12), controlPoint1: CGPoint(x: 20, y: 34.78), controlPoint2: CGPoint(x: 26.13, y: 26)) |
| 71 | + eyeouterPath.curve(to: CGPoint(x: 45, y: 20), controlPoint1: CGPoint(x: 37.99, y: 20.76), controlPoint2: CGPoint(x: 41.41, y: 20)) |
| 72 | + eyeouterPath.curve(to: CGPoint(x: 70, y: 45), controlPoint1: CGPoint(x: 58.81, y: 20), controlPoint2: CGPoint(x: 70, y: 31.19)) |
| 73 | + eyeouterPath.curve(to: CGPoint(x: 45, y: 70), controlPoint1: CGPoint(x: 70, y: 58.81), controlPoint2: CGPoint(x: 58.81, y: 70)) |
| 74 | + eyeouterPath.close() |
| 75 | + eyeouterPath.move(to: CGPoint(x: 80, y: 80)) |
| 76 | + eyeouterPath.curve(to: CGPoint(x: 80, y: 10), controlPoint1: CGPoint(x: 80, y: 80), controlPoint2: CGPoint(x: 80, y: 10)) |
| 77 | + eyeouterPath.line(to: CGPoint(x: 10, y: 10)) |
| 78 | + eyeouterPath.line(to: CGPoint(x: 10, y: 80)) |
| 79 | + eyeouterPath.line(to: CGPoint(x: 80, y: 80)) |
| 80 | + eyeouterPath.line(to: CGPoint(x: 80, y: 80)) |
| 81 | + eyeouterPath.close() |
| 82 | +} |
0 commit comments