123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // KMSignature.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/10/9.
- //
- import Cocoa
- @objc enum KMPDFSignatureType: Int {
- case ink = 0
- case text
- case image
- }
- @objc enum KMPDFSignature: Int {
- case managerView = 0
- case createView
- }
- @objcMembers class KMSignature: NSObject, NSCoding {
- private let kPathKey = "Paths"
- private let kColorKey = "Color"
- private let kImageKey = "Image"
- private let kTypeKey = "Type"
- var signatureColor: NSColor = .black
- var paths: [NSBezierPath] = []
- var pathsImage: NSImage = NSImage()
-
- var signatureType: KMPDFSignatureType = .text
- var isSelect: Bool = false
-
- override class func className() -> String {
- return "KMSignature"
- }
-
- override class func classForKeyedArchiver() -> AnyClass? {
- return NSClassFromString("KMSignature")
- }
-
- override class func classForCoder() -> AnyClass {
- return NSClassFromString("KMSignature")!
- }
-
- override class func classForArchiver() -> AnyClass? {
- return NSClassFromString("KMSignature")
- }
-
- // override class func classForKeyedUnarchiver() -> AnyClass {
- // if let cls = objc_allocateClassPair(NSObject.self, "KMSignature", 0) {
- // objc_registerClassPair(cls)
- //// let sel = #selector(required init(coder: NSCoder))
- // let sel = NSSelectorFromString("initWithCoder:")
- // let method = class_getInstanceMethod(KMSignature.self, sel)
- // class_addMethod(cls, sel, method_getImplementation(method!), method_getTypeEncoding(method!))
- //
- // let sel2 = NSSelectorFromString("encodeWithCoder:")
- // let method2 = class_getInstanceMethod(KMSignature.self, sel2)
- // class_addMethod(cls, sel2, method_getImplementation(method2!), method_getTypeEncoding(method2!))
- //
- // return cls
- // }
- // return NSClassFromString("KMSignature")!
- //return self.KMSignatureOCClass
- //}
-
- class var KMSignatureOCClass: AnyClass {
- get {
- if let cls = objc_allocateClassPair(NSObject.self, "KMSignature", 0) {
- objc_registerClassPair(cls)
- // let sel = #selector(required init(coder: NSCoder))
- let sel = NSSelectorFromString("initWithCoder:")
- let method = class_getInstanceMethod(KMSignature.self, sel)
- class_addMethod(cls, sel, method_getImplementation(method!), method_getTypeEncoding(method!))
-
- let sel2 = NSSelectorFromString("encodeWithCoder:")
- let method2 = class_getInstanceMethod(KMSignature.self, sel2)
- class_addMethod(cls, sel2, method_getImplementation(method2!), method_getTypeEncoding(method2!))
-
- return cls
- }
- return NSClassFromString("KMSignature")!
- // return KMSignature.self
- }
- }
- override init() {
- super.init()
- }
- func addPath(_ path: NSBezierPath) {
- paths.append(path)
- }
- func removePath(_ path: NSBezierPath) {
- if let index = paths.firstIndex(of: path) {
- paths.remove(at: index)
- }
- }
- func clearPath() {
- paths.removeAll()
- }
- // MARK: - NSCoding
- required init?(coder decoder: NSCoder) {
- if let decodedPaths = decoder.decodeObject(forKey: kPathKey) as? [NSBezierPath],
- let decodedColor = decoder.decodeObject(forKey: kColorKey) as? NSColor,
- let decodedImage = decoder.decodeObject(forKey: kImageKey) as? NSImage,
- let decodedType = decoder.decodeObject(forKey: kTypeKey) as? NSNumber {
- paths = decodedPaths
- signatureColor = decodedColor
- pathsImage = decodedImage
- signatureType = KMPDFSignatureType.init(rawValue: decodedType.intValue) ?? .text
- }
- }
- func encode(with encoder: NSCoder) {
- encoder.encode(paths, forKey: kPathKey)
- encoder.encode(signatureColor, forKey: kColorKey)
- encoder.encode(pathsImage, forKey: kImageKey)
- encoder.encode(NSNumber(integerLiteral: signatureType.rawValue), forKey: kTypeKey)
- }
- }
|