KMSignature.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // KMSignature.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/9.
  6. //
  7. import Cocoa
  8. @objc enum KMPDFSignatureType: Int {
  9. case ink = 0
  10. case text
  11. case image
  12. }
  13. @objc enum KMPDFSignature: Int {
  14. case managerView = 0
  15. case createView
  16. }
  17. @objcMembers class KMSignature: NSObject, NSCoding {
  18. private let kPathKey = "Paths"
  19. private let kColorKey = "Color"
  20. private let kImageKey = "Image"
  21. private let kTypeKey = "Type"
  22. var signatureColor: NSColor = .black
  23. var paths: [NSBezierPath] = []
  24. var pathsImage: NSImage = NSImage()
  25. var signatureType: KMPDFSignatureType = .text
  26. var isSelect: Bool = false
  27. override class func className() -> String {
  28. return "KMSignature"
  29. }
  30. override class func classForKeyedArchiver() -> AnyClass? {
  31. return NSClassFromString("KMSignature")
  32. }
  33. override class func classForCoder() -> AnyClass {
  34. return NSClassFromString("KMSignature")!
  35. }
  36. override class func classForArchiver() -> AnyClass? {
  37. return NSClassFromString("KMSignature")
  38. }
  39. // override class func classForKeyedUnarchiver() -> AnyClass {
  40. // if let cls = objc_allocateClassPair(NSObject.self, "KMSignature", 0) {
  41. // objc_registerClassPair(cls)
  42. //// let sel = #selector(required init(coder: NSCoder))
  43. // let sel = NSSelectorFromString("initWithCoder:")
  44. // let method = class_getInstanceMethod(KMSignature.self, sel)
  45. // class_addMethod(cls, sel, method_getImplementation(method!), method_getTypeEncoding(method!))
  46. //
  47. // let sel2 = NSSelectorFromString("encodeWithCoder:")
  48. // let method2 = class_getInstanceMethod(KMSignature.self, sel2)
  49. // class_addMethod(cls, sel2, method_getImplementation(method2!), method_getTypeEncoding(method2!))
  50. //
  51. // return cls
  52. // }
  53. // return NSClassFromString("KMSignature")!
  54. //return self.KMSignatureOCClass
  55. //}
  56. class var KMSignatureOCClass: AnyClass {
  57. get {
  58. if let cls = objc_allocateClassPair(NSObject.self, "KMSignature", 0) {
  59. objc_registerClassPair(cls)
  60. // let sel = #selector(required init(coder: NSCoder))
  61. let sel = NSSelectorFromString("initWithCoder:")
  62. let method = class_getInstanceMethod(KMSignature.self, sel)
  63. class_addMethod(cls, sel, method_getImplementation(method!), method_getTypeEncoding(method!))
  64. let sel2 = NSSelectorFromString("encodeWithCoder:")
  65. let method2 = class_getInstanceMethod(KMSignature.self, sel2)
  66. class_addMethod(cls, sel2, method_getImplementation(method2!), method_getTypeEncoding(method2!))
  67. return cls
  68. }
  69. return NSClassFromString("KMSignature")!
  70. // return KMSignature.self
  71. }
  72. }
  73. override init() {
  74. super.init()
  75. }
  76. func addPath(_ path: NSBezierPath) {
  77. paths.append(path)
  78. }
  79. func removePath(_ path: NSBezierPath) {
  80. if let index = paths.firstIndex(of: path) {
  81. paths.remove(at: index)
  82. }
  83. }
  84. func clearPath() {
  85. paths.removeAll()
  86. }
  87. // MARK: - NSCoding
  88. required init?(coder decoder: NSCoder) {
  89. if let decodedPaths = decoder.decodeObject(forKey: kPathKey) as? [NSBezierPath],
  90. let decodedColor = decoder.decodeObject(forKey: kColorKey) as? NSColor,
  91. let decodedImage = decoder.decodeObject(forKey: kImageKey) as? NSImage,
  92. let decodedType = decoder.decodeObject(forKey: kTypeKey) as? NSNumber {
  93. paths = decodedPaths
  94. signatureColor = decodedColor
  95. pathsImage = decodedImage
  96. signatureType = KMPDFSignatureType.init(rawValue: decodedType.intValue) ?? .text
  97. }
  98. }
  99. func encode(with encoder: NSCoder) {
  100. encoder.encode(paths, forKey: kPathKey)
  101. encoder.encode(signatureColor, forKey: kColorKey)
  102. encoder.encode(pathsImage, forKey: kImageKey)
  103. encoder.encode(NSNumber(integerLiteral: signatureType.rawValue), forKey: kTypeKey)
  104. }
  105. }