// // KMSelfSignAnnotationFreeText.swift // PDF Reader Pro // // Created by lizhe on 2023/10/12. // import Quartz @objc enum KMSelfSignAnnotationFreeTextSubType: Int { case none = 0 case freeText case date case profile } @objcMembers class KMSelfSignAnnotationFreeText: CPDFFreeTextAnnotation { var subType: KMSelfSignAnnotationFreeTextSubType = .freeText var date: Date? var includeTimeDateArray: [Any]? var excludeTimeDateArray: [Any]? static func dateFormatterArray() -> [String] { return ["Default", "m/d", "m/d/yy", "m/d/yyyy", "mm/dd/yy", "mm/dd/yyyy", "d/m/yy", "d/m/yyyy", "dd/mm/yy", "dd/mm/yyyy", "mm/yy", "mm/yyyy", "m.d.yy", "m.d.yyyy", "mm.dd.yy", "mm.dd.yyyy", "mm.yy", "mm.yyyy", "d.m.yy", "d.m.yyyy", "dd.mm.yy", "dd.mm.yyyy", "yy-mm-dd", "yyyy-mm-dd"] } static func fetchDateString(with index: Int, includeTime: Bool) -> String { let dateFormatterArray = dateFormatterArray() let formatter = DateFormatter() if index < 0 || index >= dateFormatterArray.count { return "" } let dateStyleString = dateFormatterArray[index] if dateStyleString == "m/d" { formatter.dateFormat = includeTime ? "M/dd HH:mm:ss" : "M/dd" } else if dateStyleString == "m/d/yy" { formatter.dateFormat = includeTime ? "M/d/yy HH:mm:ss" : "M/d/yy" } else if dateStyleString == "m/d/yyyy" { formatter.dateFormat = includeTime ? "M/d/yyyy HH:mm:ss" : "M/d/yyyy" } else if dateStyleString == "mm/dd/yy" { formatter.dateFormat = includeTime ? "MM/dd/yy HH:mm:ss" : "MM/dd/yy" } else if dateStyleString == "mm/dd/yyyy" { formatter.dateFormat = includeTime ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy" } else if dateStyleString == "d/m/yy" { formatter.dateFormat = includeTime ? "d/M/yy HH:mm:ss" : "d/M/yy" } else if dateStyleString == "d/m/yyyy" { formatter.dateFormat = includeTime ? "d/M/yyyy HH:mm:ss" : "d/M/yyyy" } else if dateStyleString == "dd/mm/yy" { formatter.dateFormat = includeTime ? "dd/MM/yy HH:mm:ss" : "dd/MM/yy" } else if dateStyleString == "dd/mm/yyyy" { formatter.dateFormat = includeTime ? "dd/MM/yyyy HH:mm:ss" : "dd/MM/yyyy" } else if dateStyleString == "mm/yy" { formatter.dateFormat = includeTime ? "MM/yy HH:mm:ss" : "MM/yy" } else if dateStyleString == "mm/yyyy" { formatter.dateFormat = includeTime ? "MM/yyyy HH:mm:ss" : "MM/yyyy" } else if dateStyleString == "m.d.yy" { formatter.dateFormat = includeTime ? "M.d.yy HH:mm:ss" : "M.d.yy" } else if dateStyleString == "m.d.yyyy" { formatter.dateFormat = includeTime ? "M.d.yyyy HH:mm:ss" : "M.d.yyyy" } else if dateStyleString == "mm.dd.yy" { formatter.dateFormat = includeTime ? "MM.dd.yy HH:mm:ss" : "MM.dd.yy" } else if dateStyleString == "mm.dd.yyyy" { formatter.dateFormat = includeTime ? "MM.dd.yyyy HH:mm:ss" : "MM.dd.yyyy" } else if dateStyleString == "mm.yy" { formatter.dateFormat = includeTime ? "MM.yy HH:mm:ss" : "MM.yy" } else if dateStyleString == "mm.yyyy" { formatter.dateFormat = includeTime ? "MM.yyyy HH:mm:ss" : "MM.yyyy" } else if dateStyleString == "d.m.yy" { formatter.dateFormat = includeTime ? "d.M.yy HH:mm:ss" : "d.M.yy" } else if dateStyleString == "d.m.yyyy" { formatter.dateFormat = includeTime ? "d.M.yyyy HH:mm:ss" : "d.M.yyyy" } else if dateStyleString == "dd.mm.yy" { formatter.dateFormat = includeTime ? "dd.MM.yy HH:mm:ss" : "dd.MM.yy" } else if dateStyleString == "dd.mm.yyyy" { formatter.dateFormat = includeTime ? "dd.MM.yyyy HH:mm:ss" : "dd.MM.yyyy" } else if dateStyleString == "yy-mm-dd" { formatter.dateFormat = includeTime ? "yy-MM-dd HH:mm:ss" : "yy-MM-dd" } else if dateStyleString == "yyyy-mm-dd" { formatter.dateFormat = includeTime ? "yyy-MM-dd HH:mm:ss" : "yyy-MM-dd" } else { formatter.dateStyle = .medium formatter.timeStyle = includeTime ? .short : .none } return formatter.string(from: Date()) } override init() { super.init() } var includeTime: Bool = false { didSet { if includeTime != oldValue { contents = KMSelfSignAnnotationFreeText.fetchDateString(with: dateFormatIndex, includeTime: includeTime) updateBounds() } } } var dateFormatIndex: Int = 0 { didSet { if dateFormatIndex != oldValue { contents = KMSelfSignAnnotationFreeText.fetchDateString(with: dateFormatIndex, includeTime: includeTime) updateBounds() } } } init(document: CPDFDocument, subType: KMSelfSignAnnotationFreeTextSubType, string: String, bounds: NSRect) { super.init(document: document) var annotationModel: CPDFAnnotationModel = CPDFAnnotationModel() if subType == .date { annotationModel = CPDFAnnotationModel(annotationType: .signDate) } else if subType == .profile { annotationModel = CPDFAnnotationModel(annotationType: .signText) } else { annotationModel = CPDFAnnotationModel(annotationType: .freeText) } let font = NSFont(name: annotationModel.fontName(), size: annotationModel.fontSize()) self.font = font self.color = annotationModel.color() self.alignment = annotationModel.alignment() let border = CPDFBorder(style: annotationModel.style(), lineWidth: annotationModel.lineWidth(), dashPattern: annotationModel.dashPattern()) if annotationModel.fontColor == nil { // 将字体颜色设置为透明度必须设一个颜色值,然后将透明度改为0 self.fontColor = NSColor.black self.opacity = 0 } else { self.fontColor = annotationModel.fontColor() self.opacity = annotationModel.opacity() } if subType == .freeText { // 根据需要处理 FreeText 特定的初始化逻辑 } else if subType == .date { let index = annotationModel.dateFormatIndex let includeTime = annotationModel.includeTime dateFormatIndex = index() self.includeTime = includeTime() contents = KMSelfSignAnnotationFreeText.fetchDateString(with: index(), includeTime: includeTime()) } else { contents = string.isEmpty ? "" : string } self.bounds = bounds updateBounds() self.subType = subType } public static func fetchAllDateString(includeTime: Bool) -> [String] { var rtnArray: [String] = [] for i in 0..