123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // 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..<KMSelfSignAnnotationFreeText.dateFormatterArray().count {
- rtnArray.append(KMSelfSignAnnotationFreeText.fetchDateString(with: i, includeTime: includeTime))
- }
- return rtnArray
- }
- func updateBounds() {
- var tempBounds = bounds
- let userAttributes = [NSAttributedString.Key.font: font]
- if let string = contents as? NSString {
- let size = string.size(withAttributes: userAttributes)
- let originalHeight = tempBounds.size.height
- if size.width < 10 {
- tempBounds.size.width = 130
- tempBounds.size.height = size.height + 5
- } else {
- tempBounds.size.width = size.width + 5
- tempBounds.size.height = size.height + 5
- }
- let gap = originalHeight - tempBounds.size.height
- tempBounds.origin.y += gap
- bounds = tempBounds
- }
- }
- }
-
-
|