KMSelfSignAnnotationFreeText.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // KMSelfSignAnnotationFreeText.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/12.
  6. //
  7. import Quartz
  8. @objc enum KMSelfSignAnnotationFreeTextSubType: Int {
  9. case none = 0
  10. case freeText
  11. case date
  12. case profile
  13. }
  14. @objcMembers class KMSelfSignAnnotationFreeText: CPDFFreeTextAnnotation {
  15. var subType: KMSelfSignAnnotationFreeTextSubType = .freeText
  16. var date: Date?
  17. var includeTimeDateArray: [Any]?
  18. var excludeTimeDateArray: [Any]?
  19. static func dateFormatterArray() -> [String] {
  20. return ["Default",
  21. "m/d",
  22. "m/d/yy",
  23. "m/d/yyyy",
  24. "mm/dd/yy",
  25. "mm/dd/yyyy",
  26. "d/m/yy",
  27. "d/m/yyyy",
  28. "dd/mm/yy",
  29. "dd/mm/yyyy",
  30. "mm/yy",
  31. "mm/yyyy",
  32. "m.d.yy",
  33. "m.d.yyyy",
  34. "mm.dd.yy",
  35. "mm.dd.yyyy",
  36. "mm.yy",
  37. "mm.yyyy",
  38. "d.m.yy",
  39. "d.m.yyyy",
  40. "dd.mm.yy",
  41. "dd.mm.yyyy",
  42. "yy-mm-dd",
  43. "yyyy-mm-dd"]
  44. }
  45. static func fetchDateString(with index: Int, includeTime: Bool) -> String {
  46. let dateFormatterArray = dateFormatterArray()
  47. let formatter = DateFormatter()
  48. if index < 0 || index >= dateFormatterArray.count {
  49. return ""
  50. }
  51. let dateStyleString = dateFormatterArray[index]
  52. if dateStyleString == "m/d" {
  53. formatter.dateFormat = includeTime ? "M/dd HH:mm:ss" : "M/dd"
  54. } else if dateStyleString == "m/d/yy" {
  55. formatter.dateFormat = includeTime ? "M/d/yy HH:mm:ss" : "M/d/yy"
  56. } else if dateStyleString == "m/d/yyyy" {
  57. formatter.dateFormat = includeTime ? "M/d/yyyy HH:mm:ss" : "M/d/yyyy"
  58. } else if dateStyleString == "mm/dd/yy" {
  59. formatter.dateFormat = includeTime ? "MM/dd/yy HH:mm:ss" : "MM/dd/yy"
  60. } else if dateStyleString == "mm/dd/yyyy" {
  61. formatter.dateFormat = includeTime ? "MM/dd/yyyy HH:mm:ss" : "MM/dd/yyyy"
  62. } else if dateStyleString == "d/m/yy" {
  63. formatter.dateFormat = includeTime ? "d/M/yy HH:mm:ss" : "d/M/yy"
  64. } else if dateStyleString == "d/m/yyyy" {
  65. formatter.dateFormat = includeTime ? "d/M/yyyy HH:mm:ss" : "d/M/yyyy"
  66. } else if dateStyleString == "dd/mm/yy" {
  67. formatter.dateFormat = includeTime ? "dd/MM/yy HH:mm:ss" : "dd/MM/yy"
  68. } else if dateStyleString == "dd/mm/yyyy" {
  69. formatter.dateFormat = includeTime ? "dd/MM/yyyy HH:mm:ss" : "dd/MM/yyyy"
  70. } else if dateStyleString == "mm/yy" {
  71. formatter.dateFormat = includeTime ? "MM/yy HH:mm:ss" : "MM/yy"
  72. } else if dateStyleString == "mm/yyyy" {
  73. formatter.dateFormat = includeTime ? "MM/yyyy HH:mm:ss" : "MM/yyyy"
  74. } else if dateStyleString == "m.d.yy" {
  75. formatter.dateFormat = includeTime ? "M.d.yy HH:mm:ss" : "M.d.yy"
  76. } else if dateStyleString == "m.d.yyyy" {
  77. formatter.dateFormat = includeTime ? "M.d.yyyy HH:mm:ss" : "M.d.yyyy"
  78. } else if dateStyleString == "mm.dd.yy" {
  79. formatter.dateFormat = includeTime ? "MM.dd.yy HH:mm:ss" : "MM.dd.yy"
  80. } else if dateStyleString == "mm.dd.yyyy" {
  81. formatter.dateFormat = includeTime ? "MM.dd.yyyy HH:mm:ss" : "MM.dd.yyyy"
  82. } else if dateStyleString == "mm.yy" {
  83. formatter.dateFormat = includeTime ? "MM.yy HH:mm:ss" : "MM.yy"
  84. } else if dateStyleString == "mm.yyyy" {
  85. formatter.dateFormat = includeTime ? "MM.yyyy HH:mm:ss" : "MM.yyyy"
  86. } else if dateStyleString == "d.m.yy" {
  87. formatter.dateFormat = includeTime ? "d.M.yy HH:mm:ss" : "d.M.yy"
  88. } else if dateStyleString == "d.m.yyyy" {
  89. formatter.dateFormat = includeTime ? "d.M.yyyy HH:mm:ss" : "d.M.yyyy"
  90. } else if dateStyleString == "dd.mm.yy" {
  91. formatter.dateFormat = includeTime ? "dd.MM.yy HH:mm:ss" : "dd.MM.yy"
  92. } else if dateStyleString == "dd.mm.yyyy" {
  93. formatter.dateFormat = includeTime ? "dd.MM.yyyy HH:mm:ss" : "dd.MM.yyyy"
  94. } else if dateStyleString == "yy-mm-dd" {
  95. formatter.dateFormat = includeTime ? "yy-MM-dd HH:mm:ss" : "yy-MM-dd"
  96. } else if dateStyleString == "yyyy-mm-dd" {
  97. formatter.dateFormat = includeTime ? "yyy-MM-dd HH:mm:ss" : "yyy-MM-dd"
  98. } else {
  99. formatter.dateStyle = .medium
  100. formatter.timeStyle = includeTime ? .short : .none
  101. }
  102. return formatter.string(from: Date())
  103. }
  104. override init() {
  105. super.init()
  106. }
  107. var includeTime: Bool = false {
  108. didSet {
  109. if includeTime != oldValue {
  110. contents = KMSelfSignAnnotationFreeText.fetchDateString(with: dateFormatIndex, includeTime: includeTime)
  111. updateBounds()
  112. }
  113. }
  114. }
  115. var dateFormatIndex: Int = 0 {
  116. didSet {
  117. if dateFormatIndex != oldValue {
  118. contents = KMSelfSignAnnotationFreeText.fetchDateString(with: dateFormatIndex, includeTime: includeTime)
  119. updateBounds()
  120. }
  121. }
  122. }
  123. init(document: CPDFDocument, subType: KMSelfSignAnnotationFreeTextSubType, string: String, bounds: NSRect) {
  124. super.init(document: document)
  125. var annotationModel: CPDFAnnotationModel = CPDFAnnotationModel()
  126. if subType == .date {
  127. annotationModel = CPDFAnnotationModel(annotationType: .signDate)
  128. } else if subType == .profile {
  129. annotationModel = CPDFAnnotationModel(annotationType: .signText)
  130. } else {
  131. annotationModel = CPDFAnnotationModel(annotationType: .freeText)
  132. }
  133. let font = NSFont(name: annotationModel.fontName(), size: annotationModel.fontSize())
  134. self.font = font
  135. self.color = annotationModel.color()
  136. self.alignment = annotationModel.alignment()
  137. let border = CPDFBorder(style: annotationModel.style(), lineWidth: annotationModel.lineWidth(), dashPattern: annotationModel.dashPattern())
  138. if annotationModel.fontColor == nil {
  139. // 将字体颜色设置为透明度必须设一个颜色值,然后将透明度改为0
  140. self.fontColor = NSColor.black
  141. self.opacity = 0
  142. } else {
  143. self.fontColor = annotationModel.fontColor()
  144. self.opacity = annotationModel.opacity()
  145. }
  146. if subType == .freeText {
  147. // 根据需要处理 FreeText 特定的初始化逻辑
  148. } else if subType == .date {
  149. let index = annotationModel.dateFormatIndex
  150. let includeTime = annotationModel.includeTime
  151. dateFormatIndex = index()
  152. self.includeTime = includeTime()
  153. contents = KMSelfSignAnnotationFreeText.fetchDateString(with: index(), includeTime: includeTime())
  154. } else {
  155. contents = string.isEmpty ? "" : string
  156. }
  157. self.bounds = bounds
  158. updateBounds()
  159. self.subType = subType
  160. }
  161. public static func fetchAllDateString(includeTime: Bool) -> [String] {
  162. var rtnArray: [String] = []
  163. for i in 0..<KMSelfSignAnnotationFreeText.dateFormatterArray().count {
  164. rtnArray.append(KMSelfSignAnnotationFreeText.fetchDateString(with: i, includeTime: includeTime))
  165. }
  166. return rtnArray
  167. }
  168. func updateBounds() {
  169. var tempBounds = bounds
  170. let userAttributes = [NSAttributedString.Key.font: font]
  171. if let string = contents as? NSString {
  172. let size = string.size(withAttributes: userAttributes)
  173. let originalHeight = tempBounds.size.height
  174. if size.width < 10 {
  175. tempBounds.size.width = 130
  176. tempBounds.size.height = size.height + 5
  177. } else {
  178. tempBounds.size.width = size.width + 5
  179. tempBounds.size.height = size.height + 5
  180. }
  181. let gap = originalHeight - tempBounds.size.height
  182. tempBounds.origin.y += gap
  183. bounds = tempBounds
  184. }
  185. }
  186. }