KMMeasureSideVC.swift 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. //
  2. // KMMeasureSideVC.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2024/7/15.
  6. //
  7. import Cocoa
  8. private enum KMMeasureType: Int {
  9. case line = 0
  10. case multilines
  11. case polygon
  12. case rectangle
  13. }
  14. @objcMembers
  15. class KMMeasureSideVC: NSViewController {
  16. // Preview
  17. @IBOutlet private weak var previewImageBox: NSBox!
  18. @IBOutlet private weak var previewImageView: NSImageView!
  19. // Type
  20. @IBOutlet private weak var typeView: NSView!
  21. @IBOutlet weak var typeBoxView: NSView!
  22. @IBOutlet private weak var typeSubView1: NSView!
  23. @IBOutlet private weak var typeButton1: NSButton!
  24. @IBOutlet private weak var typeSubView2: NSView!
  25. @IBOutlet private weak var typeButton2: NSButton!
  26. @IBOutlet private weak var typeSubView3: NSView!
  27. @IBOutlet private weak var typeButton3: NSButton!
  28. @IBOutlet private weak var typeSubView4: NSView!
  29. @IBOutlet private weak var typeButton4: NSButton!
  30. @IBOutlet private weak var verticalLine1: NSBox!
  31. @IBOutlet private weak var verticalLine2: NSBox!
  32. @IBOutlet private weak var verticalLine3: NSBox!
  33. @IBOutlet private weak var type_TopConstant: NSLayoutConstraint!
  34. // Border & Fill Color
  35. @IBOutlet private weak var fillColorView: NSView!
  36. @IBOutlet private weak var fillColorLabel: NSTextField!
  37. @IBOutlet private weak var fillColorPickerView: KMColorPickerView!
  38. @IBOutlet private weak var fillColorViewTopConstraint: NSLayoutConstraint!
  39. // Opacity
  40. @IBOutlet private weak var opacityView: NSView! // 透明度
  41. @IBOutlet private weak var opacityLabel: NSTextField!
  42. @IBOutlet private weak var opacitySlider: NSSlider!
  43. @IBOutlet private weak var opacityComboBox: KMComboBox!
  44. // Color
  45. @IBOutlet private weak var colorView: NSView! // 颜色
  46. @IBOutlet private weak var colorTitleLabel: NSTextField!
  47. @IBOutlet private weak var borderColorLabel: NSTextField!
  48. @IBOutlet private weak var borderColorBox: NSBox!
  49. @IBOutlet private weak var borderColorPickerView: KMColorPickerView!
  50. // Thickness
  51. @IBOutlet private weak var borderAndLineView: NSView! // 边框
  52. @IBOutlet private weak var borderLabel: NSTextField!
  53. @IBOutlet private weak var lineWidthSlider: NSSlider!
  54. @IBOutlet private weak var lineWidthComboBox: KMComboBox!
  55. // Line Style
  56. @IBOutlet private weak var lineStyle_View: NSView!
  57. @IBOutlet private weak var lineStyle_Label: NSTextField!
  58. @IBOutlet private weak var lineStyle_SolidButton: NSButton!
  59. @IBOutlet private weak var lineStyle_SolidImageView: NSImageView!
  60. @IBOutlet private weak var lineStyle_DottedButton: NSButton!
  61. @IBOutlet private weak var lineStyle_DottedImageView: NSImageView!
  62. @IBOutlet private weak var lineStyle_DashComBox: KMComboBox!
  63. @IBOutlet private weak var lineStyle_TopConstant: NSLayoutConstraint!
  64. // Start & End
  65. @IBOutlet private weak var startEnd_View: NSView!
  66. @IBOutlet private weak var startEnd_Label: NSTextField!
  67. @IBOutlet private weak var startEnd_StartPopUpButton: KMPopUpButton!
  68. @IBOutlet private weak var startEnd_EndPopUpButton: KMPopUpButton!
  69. @IBOutlet private weak var startEnd_TopConstant: NSLayoutConstraint!
  70. // Font
  71. @IBOutlet private weak var font_View: NSView!
  72. @IBOutlet private weak var font_Label: NSTextField!
  73. @IBOutlet private weak var font_PopUpButton: KMPopUpButton!
  74. @IBOutlet private weak var fontColorPickerView: KMColorPickerView!
  75. @IBOutlet private weak var font_StylePopUpButton: KMPopUpButton!
  76. @IBOutlet private weak var font_SizeComboBox: KMComboBox!
  77. var annotation: CPDFAnnotation?
  78. weak var pdfView: CPDFListView?
  79. var _annotationModel: CPDFAnnotationModel?
  80. var _annotations: [Any] = []
  81. private var measureType: KMMeasureType = .line
  82. deinit {
  83. NotificationCenter.default.removeObserver(self)
  84. borderColorPickerView.target = nil
  85. borderColorPickerView.action = nil
  86. fillColorPickerView.target = nil
  87. fillColorPickerView.action = nil
  88. fontColorPickerView.target = nil
  89. fontColorPickerView.action = nil
  90. NSColorPanel.shared.setTarget(nil)
  91. NSColorPanel.shared.setAction(nil)
  92. }
  93. override func viewDidLoad() {
  94. super.viewDidLoad()
  95. // Do view setup here.
  96. if annotations.count > 0 {
  97. if annotationType == .line {
  98. if pdfView?.toolMode == .measureToolMode {
  99. measureType = .line
  100. }
  101. } else if annotationType == .square {
  102. if pdfView?.toolMode == .measureToolMode {
  103. measureType = .rectangle
  104. }
  105. } else if annotationType == .polyGon {
  106. measureType = .polygon
  107. } else if annotationType == .polyLine {
  108. measureType = .multilines
  109. }
  110. } else {
  111. if annotationModel.annotationType == .line {
  112. measureType = .line
  113. } else if annotationModel.annotationType == .square {
  114. measureType = .rectangle
  115. } else if annotationModel.annotationType == .polyGon {
  116. measureType = .polygon
  117. } else if annotationModel.annotationType == .polyLine {
  118. measureType = .multilines
  119. }
  120. }
  121. languageLocalization()
  122. uiInitialize()
  123. layoutConstraintRefresh()
  124. updateViewColor()
  125. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  126. }
  127. @objc func themeChanged(notification: NSNotification) {
  128. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [self] in
  129. updateViewColor()
  130. }
  131. }
  132. // MARK: Privately Methods
  133. private func languageLocalization() -> Void {
  134. colorTitleLabel.stringValue = NSLocalizedString("Color", comment: "")
  135. borderColorLabel.stringValue = NSLocalizedString("Line Color", comment: "")
  136. fillColorLabel.stringValue = NSLocalizedString("Fill Color", comment: "")
  137. opacityLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  138. borderLabel.stringValue = NSLocalizedString("Line", comment: "")
  139. lineStyle_Label.stringValue = NSLocalizedString("Line Style", comment: "")
  140. startEnd_Label.stringValue = NSLocalizedString("Start & End", comment: "")
  141. font_Label.stringValue = NSLocalizedString("Font", comment: "")
  142. }
  143. private func uiInitialize() -> Void {
  144. borderColorPickerView.annotationType = .lineColors
  145. fillColorPickerView.annotationType = .lineFillColors
  146. borderColorPickerView.isCallColorPanelAction = true
  147. fillColorPickerView.isCallColorPanelAction = true
  148. fontColorPickerView.annotationType = .freeTextColors
  149. fontColorPickerView.isCallColorPanelAction = true
  150. self.typeView.isHidden = true
  151. borderColorLabel.textColor = KMAppearance.Layout.h0Color()
  152. borderColorLabel.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  153. fillColorLabel.textColor = KMAppearance.Layout.h0Color()
  154. fillColorLabel.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  155. opacityLabel.textColor = KMAppearance.Layout.h0Color()
  156. opacityLabel.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  157. borderLabel.textColor = KMAppearance.Layout.h0Color()
  158. borderLabel.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  159. lineStyle_Label.textColor = KMAppearance.Layout.h0Color()
  160. lineStyle_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  161. startEnd_Label.textColor = KMAppearance.Layout.h0Color()
  162. startEnd_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  163. font_Label.textColor = KMAppearance.Layout.h0Color()
  164. font_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  165. opacityComboBox.type = .none
  166. opacityComboBox.comboxRect = opacityComboBox.bounds
  167. lineWidthComboBox.type = .none
  168. lineWidthComboBox.comboxRect = lineWidthComboBox.bounds
  169. lineStyle_DashComBox.type = .none
  170. lineStyle_DashComBox.comboxRect = lineStyle_DashComBox.bounds
  171. font_PopUpButton.type = .arrowDown
  172. font_PopUpButton.removeAllItems()
  173. font_StylePopUpButton.type = .arrowDown
  174. font_StylePopUpButton.removeAllItems()
  175. font_SizeComboBox.type = .none
  176. font_SizeComboBox.comboxRect = font_SizeComboBox.bounds
  177. lineStyle_SolidImageView.image = NSImage(named: "KMImageNameLifeStyle")
  178. lineStyle_DottedImageView.image = NSImage(named: "KMImageNameLifeStyle2")
  179. lineStyle_DashComBox.isEnabled = false
  180. if annotationModel.measureDashPattern().count > 0 {
  181. lineStyle_DashComBox.stringValue = (annotationModel.measureDashPattern().first as! NSNumber).description
  182. } else {
  183. lineStyle_DashComBox.stringValue = "5"
  184. }
  185. annotationFontReload()
  186. if measureType == .line {
  187. previewImageView.image = NSImage(named: "KMImageNamePreviewMeasure")
  188. startEnd_StartPopUpButton.type = .arrowDown
  189. startEnd_StartPopUpButton.removeAllItems()
  190. startEnd_EndPopUpButton.type = .arrowDown
  191. startEnd_EndPopUpButton.removeAllItems()
  192. let startMenu = NSMenu()
  193. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 0)
  194. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 1)
  195. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 2)
  196. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 3)
  197. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 4)
  198. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 5)
  199. startEnd_StartPopUpButton.menu = startMenu
  200. let endMenu = NSMenu()
  201. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 0)
  202. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 1)
  203. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 2)
  204. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 3)
  205. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 4)
  206. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 5)
  207. startEnd_EndPopUpButton.menu = endMenu
  208. let startPopMenu = startEnd_StartPopUpButton.menu
  209. let endPopMenu = startEnd_EndPopUpButton.menu
  210. let startLineStyle = annotationModel.measureStartLineStyle()
  211. let endLineStyle = annotationModel.measureEndLineStyle()
  212. let startArr = self.startLineStyle()
  213. let endArr = self.endLineStyle()
  214. for i in 0..<startArr.count {
  215. let item = startPopMenu!.item(withTag: i)
  216. item?.image = startArr[i]
  217. }
  218. for i in 0..<endArr.count {
  219. let item = endPopMenu!.item(withTag: i)
  220. item?.image = endArr[i]
  221. }
  222. startEnd_StartPopUpButton.selectItem(at: startLineStyle.rawValue)
  223. startEnd_EndPopUpButton.selectItem(at: endLineStyle.rawValue)
  224. thicknessViewRefresh()
  225. } else if measureType == .multilines {
  226. previewImageView.image = NSImage(named: "KMImageNamePreviewPolyline")
  227. thicknessViewRefresh()
  228. } else if measureType == .polygon {
  229. previewImageView.image = NSImage(named: "KMImageNamePreviewPolygon")
  230. lineColorViewRefresh()
  231. thicknessViewRefresh()
  232. } else if measureType == .rectangle {
  233. previewImageView.image = NSImage(named: "KMImageNamePreviewMeasureSquare")
  234. lineColorViewRefresh()
  235. }
  236. fontColorPickerView.color = annotationModel.measureFontColor()
  237. font_SizeComboBox.stringValue = String(format: "%.f pt", annotationModel.measureFontSize())
  238. borderColorViewRefresh()
  239. typeViewRefresh()
  240. opacityViewRefresh()
  241. lineStyleViewRefresh()
  242. }
  243. private func typeViewRefresh() -> Void {
  244. verticalLine1.isHidden = false
  245. verticalLine2.isHidden = false
  246. verticalLine3.isHidden = false
  247. typeSubView1.backgroundColor(.clear)
  248. typeSubView2.backgroundColor(.clear)
  249. typeSubView3.backgroundColor(.clear)
  250. typeSubView4.backgroundColor(.clear)
  251. typeButton1.image = NSImage(named: "ImageNameToolbarMeasureLine")
  252. typeButton2.image = NSImage(named: "ImageNameToolbarPolyline")
  253. typeButton3.image = NSImage(named: "ImageNameToolbarPolygon")
  254. typeButton4.image = NSImage(named: "ImageNameToolbarMeasureSquare")
  255. // typeButton1.isEnabled = false
  256. // typeButton2.isEnabled = false
  257. // typeButton3.isEnabled = false
  258. // typeButton4.isEnabled = false
  259. if measureType == .line {
  260. verticalLine1.isHidden = true
  261. typeSubView1.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  262. typeButton1.isEnabled = true
  263. } else if measureType == .multilines {
  264. verticalLine1.isHidden = true
  265. verticalLine2.isHidden = true
  266. typeSubView2.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  267. typeButton2.isEnabled = true
  268. } else if measureType == .polygon {
  269. verticalLine2.isHidden = true
  270. verticalLine3.isHidden = true
  271. typeSubView3.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  272. typeButton3.isEnabled = false
  273. } else if measureType == .rectangle {
  274. verticalLine3.isHidden = true
  275. typeSubView4.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  276. }
  277. }
  278. private func borderColorViewRefresh() -> Void {
  279. borderColorPickerView.color = annotationModel.measureBorderColor()
  280. }
  281. private func lineColorViewRefresh() -> Void {
  282. fillColorPickerView.color = annotationModel.measureLineColor()
  283. }
  284. private func fontColorViewRefresh() -> Void {
  285. fontColorPickerView.color = annotationModel.measureFontColor()
  286. }
  287. private func opacityViewRefresh() -> Void {
  288. opacitySlider.floatValue = Float(annotationModel.measureOpacity())
  289. opacitySlider.toolTip = "\(Int(Float(annotationModel.measureOpacity()) * 100))%"
  290. opacityComboBox.stringValue = "\(Int(Float(annotationModel.measureOpacity()) * 100))%"
  291. }
  292. private func thicknessViewRefresh() -> Void {
  293. lineWidthSlider.floatValue = Float(annotationModel.measureLineWidth())
  294. lineWidthSlider.toolTip = String(format: "%.1f", Float(annotationModel.measureLineWidth()))
  295. lineWidthComboBox.stringValue = String(format: "%.1f", Float(annotationModel.measureLineWidth()))
  296. }
  297. private func lineStyleViewRefresh() -> Void {
  298. if annotationModel.measureLineStyle() == .solid {
  299. lineStyle_SolidButton.image = NSImage(named: "KMImageNameCheckboxRadiSel")
  300. lineStyle_DottedButton.image = NSImage(named: "KMImageNameCheckboxRadi")
  301. lineStyle_DashComBox.isEnabled = false
  302. } else {
  303. lineStyle_SolidButton.image = NSImage(named: "KMImageNameCheckboxRadi")
  304. lineStyle_DottedButton.image = NSImage(named: "KMImageNameCheckboxRadiSel")
  305. lineStyle_DashComBox.isEnabled = true
  306. }
  307. }
  308. private func layoutConstraintRefresh() -> Void {
  309. colorView.isHidden = false
  310. fillColorView.isHidden = false
  311. opacityView.isHidden = false
  312. borderAndLineView.isHidden = false
  313. lineStyle_View.isHidden = false
  314. startEnd_View.isHidden = false
  315. font_View.isHidden = false
  316. if measureType == .line {
  317. fillColorView.isHidden = true
  318. } else if measureType == .multilines {
  319. fillColorView.isHidden = true
  320. startEnd_View.isHidden = true
  321. } else if measureType == .polygon {
  322. startEnd_View.isHidden = true
  323. } else if measureType == .rectangle {
  324. startEnd_View.isHidden = true
  325. }
  326. fillColorViewTopConstraint.constant = !fillColorView.isHidden ? 92.0 : 10.0
  327. lineStyle_TopConstant.constant = !lineStyle_View.isHidden ? 0.0 : -CGRectGetHeight(lineStyle_View.bounds)
  328. startEnd_TopConstant.constant = !startEnd_View.isHidden ? 0.0 : -CGRectGetHeight(startEnd_View.bounds)
  329. }
  330. private func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
  331. var selectIndex: UInt = 0
  332. let menu = NSMenu()
  333. let fontStyleNames = CPDFFont.fontNames(forFamilyName: fontName)
  334. for i in 0..<fontStyleNames.count {
  335. let styleName = fontStyleNames[i]
  336. if style == styleName {
  337. selectIndex = UInt(i)
  338. }
  339. let item = NSMenuItem()
  340. item.title = styleName
  341. menu.addItem(item)
  342. }
  343. if style == nil {
  344. selectIndex = 0
  345. }
  346. font_StylePopUpButton.menu = menu
  347. return selectIndex
  348. }
  349. private func annotationFontReload() {
  350. DispatchQueue.global(qos: .default).async { [weak self] in
  351. guard let self = self else { return }
  352. let fonts = CPDFFont.familyNames
  353. let menu = NSMenu()
  354. var selectedIndex = 0
  355. for (index, fontName) in fonts.enumerated() {
  356. if let font = NSFont(name: fontName, size: 12.0) {
  357. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  358. let item = NSMenuItem()
  359. item.attributedTitle = attributedString
  360. menu.addItem(item)
  361. if [.line, .polyGon, .polyLine, .square].contains(self.annotationType),
  362. let modelFontName = self.annotationModel.measureFontName(),
  363. modelFontName == fontName {
  364. selectedIndex = index
  365. }
  366. }
  367. }
  368. if [.line, .polyGon, .polyLine, .square].contains(self.annotationType),
  369. let data = self.annotationModel.measureFontName() {
  370. let freetextFont = NSFont(name: data, size: 16) ?? NSFont.systemFont(ofSize: 16)
  371. if let family = freetextFont.fontDescriptor.object(forKey: .family) as? String,
  372. let style = freetextFont.fontDescriptor.object(forKey: .face) as? String {
  373. DispatchQueue.main.async { [weak self] in
  374. guard let self = self else { return }
  375. self.font_PopUpButton.menu = menu
  376. self.font_PopUpButton.selectItem(at: selectedIndex)
  377. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  378. self.font_StylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  379. }
  380. }
  381. }
  382. }
  383. }
  384. @IBAction private func colorPanelAction1(_ sender: NSButton) {
  385. annotationModel.setMeasureBorderColor(NSColorPanel.shared.color)
  386. borderColorViewRefresh()
  387. updateAnnotation()
  388. }
  389. @IBAction private func colorPanelAction2(_ sender: NSButton) {
  390. lineColorViewRefresh()
  391. annotationModel.setMeasureLineColor(NSColorPanel.shared.color)
  392. updateAnnotation()
  393. }
  394. @IBAction private func colorPanelAction3(_ sender: NSButton) {
  395. fontColorViewRefresh()
  396. annotationModel.setMeasureFontColor(NSColorPanel.shared.color)
  397. updateAnnotation()
  398. }
  399. func updateViewColor() {
  400. self.typeBoxView.wantsLayer = true
  401. self.typeBoxView.layer?.borderWidth = 1
  402. if KMAppearance.isDarkMode() {
  403. let darkColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  404. let borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  405. setViewColor(font_PopUpButton, darkColor, borderColor)
  406. setViewColor(font_SizeComboBox, darkColor, borderColor)
  407. setViewColor(font_StylePopUpButton, darkColor, borderColor)
  408. setViewColor(lineWidthComboBox, darkColor, borderColor)
  409. setViewColor(opacityComboBox, darkColor, borderColor)
  410. setViewColor(opacityComboBox, darkColor, borderColor)
  411. setViewColor(lineStyle_DashComBox, darkColor, borderColor)
  412. self.typeBoxView.layer?.borderColor = NSColor.km_init(hex: "#424345").cgColor
  413. } else {
  414. let lightColor = NSColor.white.cgColor
  415. let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  416. setViewColor(font_PopUpButton, lightColor, borderColor)
  417. setViewColor(font_SizeComboBox, lightColor, borderColor)
  418. setViewColor(font_StylePopUpButton, lightColor, borderColor)
  419. setViewColor(lineWidthComboBox, lightColor, borderColor)
  420. setViewColor(opacityComboBox, lightColor, borderColor)
  421. setViewColor(opacityComboBox, lightColor, borderColor)
  422. setViewColor(lineStyle_DashComBox, lightColor, borderColor)
  423. self.typeBoxView.layer?.borderColor = NSColor.km_init(hex: "#E2E3E6").cgColor
  424. }
  425. }
  426. func setViewColor(_ view: NSView, _ backgroundColor: CGColor, _ borderColor: CGColor) {
  427. view.wantsLayer = true
  428. view.layer?.backgroundColor = backgroundColor
  429. view.layer?.borderColor = borderColor
  430. }
  431. private func updateAnnotation() {
  432. if annotationModel.annotation != nil {
  433. for tAnnotation in annotations {
  434. pdfView?.setNeedsDisplayAnnotationViewFor((tAnnotation as AnyObject).page)
  435. }
  436. }
  437. }
  438. private func startLineStyle() -> [NSImage] {
  439. let size = NSSize(width: 100.0, height: 12.0)
  440. let image1 = NSImage.image(with: size) { rect in
  441. let path = NSBezierPath()
  442. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  443. path.line(to: NSPoint(x: 6.0, y: 6.0))
  444. path.lineWidth = 2.0
  445. NSColor.labelColor.setStroke()
  446. path.stroke()
  447. return true
  448. }
  449. let image2 = NSImage.image(with: size) { dstRect in
  450. let path = NSBezierPath()
  451. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  452. path.line(to: NSPoint(x: 12.0, y: 6.0))
  453. path.appendRect(NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  454. path.lineWidth = 2.0
  455. NSColor.labelColor.setStroke()
  456. path.stroke()
  457. return true
  458. }
  459. let image3 = NSImage.image(with: size) { dstRect in
  460. let path = NSBezierPath()
  461. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  462. path.line(to: NSPoint(x: 12.0, y: 6.0))
  463. path.appendOval(in: NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  464. path.lineWidth = 2.0
  465. NSColor.labelColor.setStroke()
  466. path.stroke()
  467. return true
  468. }
  469. let image4 = NSImage.image(with: size) { dstRect in
  470. let path = NSBezierPath()
  471. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  472. path.line(to: NSPoint(x: 14.0, y: 6.0))
  473. path.move(to: NSPoint(x: 14.0, y: 6.0))
  474. path.line(to: NSPoint(x: 10.0, y: 10.0))
  475. path.line(to: NSPoint(x: 6.0, y: 6.0))
  476. path.line(to: NSPoint(x: 10.0, y: 2.0))
  477. path.close()
  478. NSColor.labelColor.setStroke()
  479. path.lineWidth = 2.0
  480. path.stroke()
  481. return true
  482. }
  483. let image5 = NSImage.image(with: size) { dstRect in
  484. let path = NSBezierPath()
  485. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  486. path.line(to: NSPoint(x: 6.0, y: 6.0))
  487. path.move(to: NSPoint(x: 15.0, y: 3.0))
  488. path.line(to: NSPoint(x: 6.0, y: 6.0))
  489. path.line(to: NSPoint(x: 15.0, y: 9.0))
  490. path.lineWidth = 2.0
  491. NSColor.labelColor.setStroke()
  492. path.stroke()
  493. return true
  494. }
  495. let image6 = NSImage.image(with: size) { dstRect in
  496. let path = NSBezierPath()
  497. path.move(to: NSPoint(x: 93.0-4, y: 6.0))
  498. path.line(to: NSPoint(x: 6.0, y: 6.0))
  499. path.move(to: NSPoint(x: 15.0, y: 3.0))
  500. path.line(to: NSPoint(x: 6.0, y: 6.0))
  501. path.line(to: NSPoint(x: 15.0, y: 9.0))
  502. path.close()
  503. path.lineWidth = 2.0
  504. NSColor.labelColor.setStroke()
  505. path.stroke()
  506. return true
  507. }
  508. return [image1, image5, image6, image2, image3, image4]
  509. }
  510. private func endLineStyle() -> [NSImage] {
  511. let size = NSSize(width: 100.0, height: 12.0)
  512. let image1 = NSImage.image(with: size) { rect in
  513. let path = NSBezierPath()
  514. path.move(to: NSPoint(x: 6.0, y: 6.0))
  515. path.line(to: NSPoint(x: 93.0-4, y: 6.0))
  516. path.lineWidth = 2.0
  517. NSColor.labelColor.setStroke()
  518. path.stroke()
  519. return true
  520. }
  521. let image2 = NSImage.image(with: size) { rect in
  522. let path = NSBezierPath()
  523. path.move(to: NSPoint(x: 6.0, y: 6.0))
  524. path.line(to: NSPoint(x: 87.0-4, y: 6.0))
  525. path.appendRect(NSRect(x: 87.0-4, y: 3.0, width: 6.0, height: 6.0))
  526. path.lineWidth = 2.0
  527. NSColor.labelColor.setStroke()
  528. path.stroke()
  529. return true
  530. }
  531. let image3 = NSImage.image(with: size) { rect in
  532. let path = NSBezierPath()
  533. path.move(to: NSPoint(x: 6.0, y: 6.0))
  534. path.line(to: NSPoint(x: 87.0-4, y: 6.0))
  535. path.appendOval(in: NSRect(x: 87.0-4, y: 3.0, width: 6.0, height: 6.0))
  536. path.lineWidth = 2.0
  537. NSColor.labelColor.setStroke()
  538. path.stroke()
  539. return true
  540. }
  541. let image4 = NSImage.image(with: size) { rect in
  542. let path = NSBezierPath()
  543. path.move(to: NSPoint(x: 6.0, y: 6.0))
  544. path.line(to: NSPoint(x: 87.0-4, y: 6.0))
  545. path.move(to: NSPoint(x: 87.0-4, y: 6.0))
  546. path.line(to: NSPoint(x: 91.0-4, y: 10.0))
  547. path.line(to: NSPoint(x: 95.0-4, y: 6.0))
  548. path.line(to: NSPoint(x: 91.0-4, y: 2.0))
  549. path.close()
  550. NSColor.labelColor.setStroke()
  551. path.lineWidth = 2.0
  552. path.stroke()
  553. return true
  554. }
  555. let image5 = NSImage.image(with: size) { rect in
  556. let path = NSBezierPath()
  557. path.move(to: NSPoint(x: 6.0, y: 6.0))
  558. path.line(to: NSPoint(x: 93.0-4, y: 6.0))
  559. path.move(to: NSPoint(x: 84.0-4, y: 3.0))
  560. path.line(to: NSPoint(x: 93.0-4, y: 6.0))
  561. path.line(to: NSPoint(x: 84.0-4, y: 9.0))
  562. path.lineWidth = 2.0
  563. NSColor.labelColor.setStroke()
  564. path.stroke()
  565. return true
  566. }
  567. let image6 = NSImage.image(with: size) { rect in
  568. let path = NSBezierPath()
  569. path.move(to: NSPoint(x: 6.0, y: 6.0))
  570. path.line(to: NSPoint(x: 93.0-4, y: 6.0))
  571. path.move(to: NSPoint(x: 84.0-4, y: 3.0))
  572. path.line(to: NSPoint(x: 93.0-4, y: 6.0))
  573. path.line(to: NSPoint(x: 84.0-4, y: 9.0))
  574. path.close()
  575. path.lineWidth = 2.0
  576. NSColor.labelColor.setStroke()
  577. path.stroke()
  578. return true
  579. }
  580. return [image1, image5, image6, image2, image3, image4]
  581. }
  582. private func isNumericUsingNSRegularExpression(_ input: String) -> Bool {
  583. let pattern = "^[0-9]+$"
  584. do {
  585. let regex = try NSRegularExpression(pattern: pattern)
  586. let matches = regex.matches(in: input, range: NSRange(location: 0, length: input.utf16.count))
  587. return matches.count > 0
  588. } catch {
  589. print("Invalid regex pattern")
  590. return false
  591. }
  592. }
  593. // MARK: Publicly Methods
  594. // MARK: Action
  595. @IBAction func typeButtonAction(_ sender: NSButton) {
  596. }
  597. @IBAction func borderColorPickerViewAction(_ sender: Any) {
  598. var fillColor = NSColor.clear
  599. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  600. if let borderColor = borderColorPickerView.color {
  601. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  602. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  603. fillColor = color
  604. }
  605. annotationModel.setMeasureBorderColor(fillColor)
  606. annotationModel.setMeasureOpacity(opacity)
  607. opacityViewRefresh()
  608. updateAnnotation()
  609. }
  610. @IBAction func fillColorPickerViewAction(_ sender: Any) {
  611. var fillColor = NSColor.clear
  612. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  613. if let borderColor = fillColorPickerView.color {
  614. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  615. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  616. fillColor = color
  617. }
  618. if opacity == 0 {
  619. annotationModel.setMeasureLineColor(nil)
  620. } else {
  621. annotationModel.setMeasureLineColor(fillColor)
  622. }
  623. opacityViewRefresh()
  624. lineColorViewRefresh()
  625. updateAnnotation()
  626. }
  627. @IBAction func fontColorPickerViewAction(_ sender: Any) {
  628. var fillColor = NSColor.clear
  629. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  630. if let borderColor = fontColorPickerView.color {
  631. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  632. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  633. fillColor = color
  634. }
  635. fontColorViewRefresh()
  636. annotationModel.setMeasureFontColor(fillColor)
  637. updateAnnotation()
  638. }
  639. @IBAction func customColorAction(_ sender: NSButton) {
  640. NSColorPanel.shared.setAction(nil)
  641. NSColorPanel.shared.setTarget(nil)
  642. NSColorPanel.shared.setTarget(self)
  643. NSColorPanel.shared.showsAlpha = true
  644. if sender.tag == 0 {
  645. NSColorPanel.shared.setAction(#selector(colorPanelAction1(_:)))
  646. } else if sender.tag == 1 {
  647. NSColorPanel.shared.setAction(#selector(colorPanelAction2(_:)))
  648. } else if sender.tag == 2 {
  649. NSColorPanel.shared.setAction(#selector(colorPanelAction3(_:)))
  650. }
  651. NSColorPanel.shared.orderFront(nil)
  652. }
  653. @IBAction func opacitySliderAction(_ sender: Any) {
  654. var opcity = CGFloat(opacitySlider.floatValue)
  655. if opcity == 0 {
  656. opcity = 0.0001
  657. }
  658. annotationModel.setMeasureOpacity(opcity)
  659. opacityViewRefresh()
  660. updateAnnotation()
  661. }
  662. @IBAction func opacityComboBoxAction(_ sender: Any) {
  663. opacitySlider.floatValue = Float(opacityComboBox.floatValue / 100)
  664. var opcity = CGFloat(opacitySlider.floatValue)
  665. if opcity == 0 {
  666. opcity = 0.0001
  667. }
  668. annotationModel.setMeasureOpacity(opcity)
  669. opacityViewRefresh()
  670. updateAnnotation()
  671. }
  672. @IBAction func thicknessSliderAction(_ sender: Any) {
  673. var thickness = CGFloat(lineWidthSlider.floatValue)
  674. annotationModel.setMeasureLineWidth(thickness)
  675. thicknessViewRefresh()
  676. updateAnnotation()
  677. }
  678. @IBAction func thicknessComboBoxAction(_ sender: Any) {
  679. lineWidthSlider.floatValue = Float(lineWidthComboBox.floatValue)
  680. let thickness = CGFloat(lineWidthSlider.floatValue)
  681. annotationModel.setMeasureLineWidth(thickness)
  682. thicknessViewRefresh()
  683. updateAnnotation()
  684. }
  685. @IBAction func lineStyleButtonAction(_ sender: NSButton) {
  686. if sender.tag == 0 {
  687. annotationModel.setMeasureLineStyle(.solid)
  688. } else {
  689. annotationModel.setMeasureLineStyle(.dashed)
  690. annotation?.setDashPattern(annotationModel.measureDashPattern())
  691. }
  692. lineStyleViewRefresh()
  693. updateAnnotation()
  694. }
  695. @IBAction func startMenuItemAction(_ sender: NSMenuItem) {
  696. let startType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  697. annotationModel.setMeasureStart(startType)
  698. updateAnnotation()
  699. }
  700. @IBAction func endMenuItemAction(_ sender: NSMenuItem) {
  701. let endType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  702. annotationModel.setMeasureEnd(endType)
  703. updateAnnotation()
  704. }
  705. @IBAction func fontButtonAction(_ sender: NSButton) {
  706. let selectItem = font_PopUpButton.selectedItem
  707. guard let selectItem = selectItem else { return }
  708. let resultAtt = NSMutableAttributedString(attributedString: selectItem.attributedTitle!)
  709. let familyString = resultAtt.string
  710. let styleString = font_StylePopUpButton.selectedItem?.title ?? ""
  711. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: familyString, .face: styleString as Any])
  712. let fontSizeString = font_SizeComboBox.stringValue.replacingOccurrences(of: " pt", with: "")
  713. let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString, fontSize: fontSizeString.stringToCGFloat(), fontColor: fontColorPickerView.color ?? .black, fontAlignment: .left, annotationType: annotationModel.annotationType)
  714. let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
  715. fontWindowController.isHidenAlignmentLabel = true
  716. let window = fontWindowController.window
  717. fontWindowController.callback = { [weak self] model in
  718. self?.fontColorPickerView.color = model.fontColor
  719. DispatchQueue.global(qos: .default).async { [self]
  720. let fonts = CPDFFont.familyNames
  721. let menu = NSMenu()
  722. var selectedIndex = 0
  723. for (index, fontName) in fonts.enumerated() {
  724. if let font = NSFont(name: fontName, size: 12.0) {
  725. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  726. let item = NSMenuItem()
  727. item.attributedTitle = attributedString
  728. menu.addItem(item)
  729. if let family = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  730. let style = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  731. if model.fontName == family {
  732. selectedIndex = index
  733. }
  734. }
  735. }
  736. }
  737. DispatchQueue.main.async {
  738. self?.font_PopUpButton.menu = menu
  739. self?.font_PopUpButton.selectItem(at: selectedIndex)
  740. }
  741. }
  742. let selectedStyleIndex = self?.setFontStyle(fontName: model.fontName, currentStyle: model.fontWeight) ?? 0
  743. self?.font_StylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  744. self?.font_SizeComboBox.stringValue = String(format: "%.f pt", model.fontSize)
  745. self?.annotationModel.setMeasureFontName(model.fontName)
  746. self?.annotationModel.setMeasureFontSize(model.fontSize)
  747. self?.annotationModel.setMeasureFontColor(model.fontColor)
  748. if self?.annotationModel.annotations != nil {
  749. self?.updateAnnotation()
  750. }
  751. }
  752. window?.orderFront(sender)
  753. }
  754. @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
  755. guard let selectedItem = font_PopUpButton.selectedItem else { return }
  756. let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
  757. let familyString = resultAtt.string
  758. let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
  759. guard let styleString = font_StylePopUpButton.selectedItem?.title else { return }
  760. font_StylePopUpButton.selectItem(at: Int(selectIndex))
  761. for tAnnotation in annotations {
  762. if tAnnotation is CPDFLineAnnotation {
  763. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  764. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  765. } else if tAnnotation is CPDFPolygonAnnotation {
  766. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  767. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  768. } else if tAnnotation is CPDFPolylineAnnotation {
  769. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  770. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  771. }
  772. }
  773. self.annotationModel.setMeasureFontName(familyString)
  774. if annotations.count > 0 {
  775. pdfView?.setNeedsDisplay(annotation)
  776. }
  777. }
  778. @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
  779. if annotationModel.annotations != nil {
  780. for tAnnotation in annotations {
  781. if tAnnotation is CPDFLineAnnotation {
  782. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  783. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  784. } else if tAnnotation is CPDFPolygonAnnotation {
  785. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  786. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  787. } else if tAnnotation is CPDFPolylineAnnotation {
  788. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  789. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  790. }
  791. }
  792. } else {
  793. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  794. if let data = annotationModel.measureFontName() {
  795. let font = NSFont(name: data, size: annotationModel.measureFontSize()) ?? NSFont.systemFont(ofSize: 16)
  796. let newFont = NSFont(name: font.fontName, size: CGFloat(font_SizeComboBox.floatValue))
  797. if let newFont = newFont {
  798. annotationModel.setMeasureFontName(newFont.fontName)
  799. annotationModel.setMeasureFontSize(newFont.pointSize)
  800. }
  801. }
  802. }
  803. }
  804. updateAnnotation()
  805. }
  806. //字重 NSFontWeight
  807. @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
  808. if let styleString = font_StylePopUpButton.selectedItem?.title {
  809. if annotationModel.annotations != nil {
  810. for tAnnotation in self.annotations {
  811. if tAnnotation is CPDFLineAnnotation {
  812. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  813. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  814. } else if tAnnotation is CPDFPolygonAnnotation {
  815. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  816. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  817. } else if tAnnotation is CPDFPolylineAnnotation {
  818. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  819. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  820. }
  821. }
  822. } else {
  823. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  824. if let data = annotationModel.measureFontName() {
  825. let font = NSFont(name: data, size: annotationModel.measureFontSize()) ?? NSFont.systemFont(ofSize: 16)
  826. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
  827. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  828. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  829. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
  830. annotationModel.setMeasureFontName(newFont.fontName)
  831. annotationModel.setMeasureFontSize(newFont.pointSize)
  832. }
  833. }
  834. }
  835. }
  836. }
  837. }
  838. updateAnnotation()
  839. }
  840. @IBAction func lineDashComboBoxAction(_ sender: NSComboBox) {
  841. let number = NSNumber(value: lineStyle_DashComBox.floatValue)
  842. var lineDashPattern: [Any] = annotationModel.measureDashPattern()
  843. let array: [NSNumber]
  844. if annotationModel.measureDashPattern() is [NSNumber] {
  845. if lineDashPattern.isEmpty {
  846. array = [number]
  847. } else {
  848. lineDashPattern[0] = number
  849. array = lineDashPattern as! [NSNumber]
  850. }
  851. } else {
  852. array = [number]
  853. }
  854. annotationModel.setMeasureDashPattern(array)
  855. updateAnnotation()
  856. }
  857. // MARK: Set & Get
  858. var annotations: [Any] {
  859. set {
  860. _annotations = newValue
  861. self.annotation = (newValue.first as? CPDFAnnotation)
  862. }
  863. get {
  864. return _annotations
  865. }
  866. }
  867. var annotationModel: CPDFAnnotationModel {
  868. get {
  869. return _annotationModel!
  870. }
  871. set {
  872. _annotationModel = newValue
  873. if _annotationModel?.annotations != nil {
  874. if annotationModel.annotations.count > 0 {
  875. annotations = _annotationModel?.annotations as! [CPDFAnnotation]
  876. }
  877. }
  878. }
  879. }
  880. var annotationType: CAnnotationType {
  881. get {
  882. if annotations.count > 0 {
  883. if let ann = annotation?.isKind(of: CPDFLineAnnotation.self), ann {
  884. if (annotation as! CPDFLineAnnotation).isMeasure {
  885. return .line
  886. }
  887. } else if let ann = annotation?.isKind(of: CPDFPolygonAnnotation.self), ann {
  888. if pdfView?.annotationType == .square {
  889. return .square
  890. }
  891. return .polyGon
  892. } else if let ann = annotation?.isKind(of: CPDFPolylineAnnotation.self), ann {
  893. return .polyLine
  894. }
  895. } else {
  896. return annotationModel.annotationType
  897. }
  898. return .unkown
  899. }
  900. }
  901. }