KMMeasureSideVC.swift 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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: "ImageNameToolbarMeasureLine")
  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: "ImageNameToolbarPolyline")
  227. thicknessViewRefresh()
  228. } else if measureType == .polygon {
  229. previewImageView.image = NSImage(named: "ImageNameToolbarPolygon")
  230. lineColorViewRefresh()
  231. thicknessViewRefresh()
  232. } else if measureType == .rectangle {
  233. previewImageView.image = NSImage(named: "ImageNameToolbarMeasureSquare")
  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. self.typeBoxView.layer?.borderColor = NSColor(hex: "#424345").cgColor
  412. } else {
  413. let lightColor = NSColor.white.cgColor
  414. let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  415. setViewColor(font_PopUpButton, lightColor, borderColor)
  416. setViewColor(font_SizeComboBox, lightColor, borderColor)
  417. setViewColor(font_StylePopUpButton, lightColor, borderColor)
  418. setViewColor(lineWidthComboBox, lightColor, borderColor)
  419. setViewColor(opacityComboBox, lightColor, borderColor)
  420. setViewColor(opacityComboBox, lightColor, borderColor)
  421. self.typeBoxView.layer?.borderColor = NSColor(hex: "#E2E3E6").cgColor
  422. }
  423. }
  424. func setViewColor(_ view: NSView, _ backgroundColor: CGColor, _ borderColor: CGColor) {
  425. view.wantsLayer = true
  426. view.layer?.backgroundColor = backgroundColor
  427. view.layer?.borderColor = borderColor
  428. }
  429. private func updateAnnotation() {
  430. if annotationModel.annotation != nil {
  431. for tAnnotation in annotations {
  432. pdfView?.setNeedsDisplayAnnotationViewFor((tAnnotation as AnyObject).page)
  433. }
  434. }
  435. }
  436. private func startLineStyle() -> [NSImage] {
  437. let size = NSSize(width: 100.0, height: 12.0)
  438. let image1 = NSImage.image(with: size) { rect in
  439. let path = NSBezierPath()
  440. path.move(to: NSPoint(x: 93.0, y: 6.0))
  441. path.line(to: NSPoint(x: 6.0, y: 6.0))
  442. path.lineWidth = 2.0
  443. NSColor.labelColor.setStroke()
  444. path.stroke()
  445. return true
  446. }
  447. let image2 = NSImage.image(with: size) { dstRect in
  448. let path = NSBezierPath()
  449. path.move(to: NSPoint(x: 93.0, y: 6.0))
  450. path.line(to: NSPoint(x: 12.0, y: 6.0))
  451. path.appendRect(NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  452. path.lineWidth = 2.0
  453. NSColor.labelColor.setStroke()
  454. path.stroke()
  455. return true
  456. }
  457. let image3 = NSImage.image(with: size) { dstRect in
  458. let path = NSBezierPath()
  459. path.move(to: NSPoint(x: 93.0, y: 6.0))
  460. path.line(to: NSPoint(x: 12.0, y: 6.0))
  461. path.appendOval(in: NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  462. path.lineWidth = 2.0
  463. NSColor.labelColor.setStroke()
  464. path.stroke()
  465. return true
  466. }
  467. let image4 = NSImage.image(with: size) { dstRect in
  468. let path = NSBezierPath()
  469. path.move(to: NSPoint(x: 93.0, y: 6.0))
  470. path.line(to: NSPoint(x: 14.0, y: 6.0))
  471. path.move(to: NSPoint(x: 14.0, y: 6.0))
  472. path.line(to: NSPoint(x: 10.0, y: 10.0))
  473. path.line(to: NSPoint(x: 6.0, y: 6.0))
  474. path.line(to: NSPoint(x: 10.0, y: 2.0))
  475. path.close()
  476. NSColor.labelColor.setStroke()
  477. path.lineWidth = 2.0
  478. path.stroke()
  479. return true
  480. }
  481. let image5 = NSImage.image(with: size) { dstRect in
  482. let path = NSBezierPath()
  483. path.move(to: NSPoint(x: 93.0, y: 6.0))
  484. path.line(to: NSPoint(x: 6.0, y: 6.0))
  485. path.move(to: NSPoint(x: 15.0, y: 3.0))
  486. path.line(to: NSPoint(x: 6.0, y: 6.0))
  487. path.line(to: NSPoint(x: 15.0, y: 9.0))
  488. path.lineWidth = 2.0
  489. NSColor.labelColor.setStroke()
  490. path.stroke()
  491. return true
  492. }
  493. let image6 = NSImage.image(with: size) { dstRect in
  494. let path = NSBezierPath()
  495. path.move(to: NSPoint(x: 93.0, y: 6.0))
  496. path.line(to: NSPoint(x: 6.0, y: 6.0))
  497. path.move(to: NSPoint(x: 15.0, y: 3.0))
  498. path.line(to: NSPoint(x: 6.0, y: 6.0))
  499. path.line(to: NSPoint(x: 15.0, y: 9.0))
  500. path.close()
  501. path.lineWidth = 2.0
  502. NSColor.labelColor.setStroke()
  503. path.stroke()
  504. return true
  505. }
  506. return [image1, image5, image6, image2, image3, image4]
  507. }
  508. private func endLineStyle() -> [NSImage] {
  509. let size = NSSize(width: 100.0, height: 12.0)
  510. let image1 = NSImage.image(with: size) { rect in
  511. let path = NSBezierPath()
  512. path.move(to: NSPoint(x: 6.0, y: 6.0))
  513. path.line(to: NSPoint(x: 93.0, y: 6.0))
  514. path.lineWidth = 2.0
  515. NSColor.labelColor.setStroke()
  516. path.stroke()
  517. return true
  518. }
  519. let image2 = NSImage.image(with: size) { rect in
  520. let path = NSBezierPath()
  521. path.move(to: NSPoint(x: 6.0, y: 6.0))
  522. path.line(to: NSPoint(x: 87.0, y: 6.0))
  523. path.appendRect(NSRect(x: 87.0, y: 3.0, width: 6.0, height: 6.0))
  524. path.lineWidth = 2.0
  525. NSColor.labelColor.setStroke()
  526. path.stroke()
  527. return true
  528. }
  529. let image3 = NSImage.image(with: size) { rect in
  530. let path = NSBezierPath()
  531. path.move(to: NSPoint(x: 6.0, y: 6.0))
  532. path.line(to: NSPoint(x: 87.0, y: 6.0))
  533. path.appendOval(in: NSRect(x: 87.0, y: 3.0, width: 6.0, height: 6.0))
  534. path.lineWidth = 2.0
  535. NSColor.labelColor.setStroke()
  536. path.stroke()
  537. return true
  538. }
  539. let image4 = NSImage.image(with: size) { rect in
  540. let path = NSBezierPath()
  541. path.move(to: NSPoint(x: 6.0, y: 6.0))
  542. path.line(to: NSPoint(x: 87.0, y: 6.0))
  543. path.move(to: NSPoint(x: 87.0, y: 6.0))
  544. path.line(to: NSPoint(x: 91.0, y: 10.0))
  545. path.line(to: NSPoint(x: 95.0, y: 6.0))
  546. path.line(to: NSPoint(x: 91.0, y: 2.0))
  547. path.close()
  548. NSColor.labelColor.setStroke()
  549. path.lineWidth = 2.0
  550. path.stroke()
  551. return true
  552. }
  553. let image5 = NSImage.image(with: size) { rect in
  554. let path = NSBezierPath()
  555. path.move(to: NSPoint(x: 6.0, y: 6.0))
  556. path.line(to: NSPoint(x: 93.0, y: 6.0))
  557. path.move(to: NSPoint(x: 84.0, y: 3.0))
  558. path.line(to: NSPoint(x: 93.0, y: 6.0))
  559. path.line(to: NSPoint(x: 84.0, y: 9.0))
  560. path.lineWidth = 2.0
  561. NSColor.labelColor.setStroke()
  562. path.stroke()
  563. return true
  564. }
  565. let image6 = NSImage.image(with: size) { rect in
  566. let path = NSBezierPath()
  567. path.move(to: NSPoint(x: 6.0, y: 6.0))
  568. path.line(to: NSPoint(x: 93.0, y: 6.0))
  569. path.move(to: NSPoint(x: 84.0, y: 3.0))
  570. path.line(to: NSPoint(x: 93.0, y: 6.0))
  571. path.line(to: NSPoint(x: 84.0, y: 9.0))
  572. path.close()
  573. path.lineWidth = 2.0
  574. NSColor.labelColor.setStroke()
  575. path.stroke()
  576. return true
  577. }
  578. return [image1, image5, image6, image2, image3, image4]
  579. }
  580. private func isNumericUsingNSRegularExpression(_ input: String) -> Bool {
  581. let pattern = "^[0-9]+$"
  582. do {
  583. let regex = try NSRegularExpression(pattern: pattern)
  584. let matches = regex.matches(in: input, range: NSRange(location: 0, length: input.utf16.count))
  585. return matches.count > 0
  586. } catch {
  587. print("Invalid regex pattern")
  588. return false
  589. }
  590. }
  591. // MARK: Publicly Methods
  592. // MARK: Action
  593. @IBAction func typeButtonAction(_ sender: NSButton) {
  594. }
  595. @IBAction func borderColorPickerViewAction(_ sender: Any) {
  596. var fillColor = NSColor.clear
  597. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  598. if let borderColor = borderColorPickerView.color {
  599. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  600. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  601. fillColor = color
  602. }
  603. annotationModel.setMeasureBorderColor(fillColor)
  604. annotationModel.setMeasureOpacity(opacity)
  605. opacityViewRefresh()
  606. updateAnnotation()
  607. }
  608. @IBAction func fillColorPickerViewAction(_ sender: Any) {
  609. var fillColor = NSColor.clear
  610. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  611. if let borderColor = fillColorPickerView.color {
  612. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  613. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  614. fillColor = color
  615. }
  616. if opacity == 0 {
  617. annotationModel.setMeasureLineColor(nil)
  618. } else {
  619. annotationModel.setMeasureLineColor(fillColor)
  620. }
  621. opacityViewRefresh()
  622. lineColorViewRefresh()
  623. updateAnnotation()
  624. }
  625. @IBAction func fontColorPickerViewAction(_ sender: Any) {
  626. var fillColor = NSColor.clear
  627. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  628. if let borderColor = fontColorPickerView.color {
  629. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  630. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  631. fillColor = color
  632. }
  633. fontColorViewRefresh()
  634. annotationModel.setMeasureFontColor(fillColor)
  635. updateAnnotation()
  636. }
  637. @IBAction func customColorAction(_ sender: NSButton) {
  638. NSColorPanel.shared.setAction(nil)
  639. NSColorPanel.shared.setTarget(nil)
  640. NSColorPanel.shared.setTarget(self)
  641. NSColorPanel.shared.showsAlpha = true
  642. if sender.tag == 0 {
  643. NSColorPanel.shared.setAction(#selector(colorPanelAction1(_:)))
  644. } else if sender.tag == 1 {
  645. NSColorPanel.shared.setAction(#selector(colorPanelAction2(_:)))
  646. } else if sender.tag == 2 {
  647. NSColorPanel.shared.setAction(#selector(colorPanelAction3(_:)))
  648. }
  649. NSColorPanel.shared.orderFront(nil)
  650. }
  651. @IBAction func opacitySliderAction(_ sender: Any) {
  652. var opcity = CGFloat(opacitySlider.floatValue)
  653. if opcity == 0 {
  654. opcity = 0.0001
  655. }
  656. annotationModel.setMeasureOpacity(opcity)
  657. opacityViewRefresh()
  658. updateAnnotation()
  659. }
  660. @IBAction func opacityComboBoxAction(_ sender: Any) {
  661. opacitySlider.floatValue = Float(opacityComboBox.floatValue / 100)
  662. var opcity = CGFloat(opacitySlider.floatValue)
  663. if opcity == 0 {
  664. opcity = 0.0001
  665. }
  666. annotationModel.setMeasureOpacity(opcity)
  667. opacityViewRefresh()
  668. updateAnnotation()
  669. }
  670. @IBAction func thicknessSliderAction(_ sender: Any) {
  671. var thickness = CGFloat(lineWidthSlider.floatValue)
  672. annotationModel.setMeasureLineWidth(thickness)
  673. thicknessViewRefresh()
  674. updateAnnotation()
  675. }
  676. @IBAction func thicknessComboBoxAction(_ sender: Any) {
  677. lineWidthSlider.floatValue = Float(lineWidthComboBox.floatValue)
  678. let thickness = CGFloat(lineWidthSlider.floatValue)
  679. annotationModel.setMeasureLineWidth(thickness)
  680. thicknessViewRefresh()
  681. updateAnnotation()
  682. }
  683. @IBAction func lineStyleButtonAction(_ sender: NSButton) {
  684. if sender.tag == 0 {
  685. annotationModel.setMeasureLineStyle(.solid)
  686. } else {
  687. annotationModel.setMeasureLineStyle(.dashed)
  688. annotation?.setDashPattern(annotationModel.measureDashPattern())
  689. }
  690. lineStyleViewRefresh()
  691. updateAnnotation()
  692. }
  693. @IBAction func startMenuItemAction(_ sender: NSMenuItem) {
  694. let startType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  695. annotationModel.setMeasureStart(startType)
  696. updateAnnotation()
  697. }
  698. @IBAction func endMenuItemAction(_ sender: NSMenuItem) {
  699. let endType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  700. annotationModel.setMeasureEnd(endType)
  701. updateAnnotation()
  702. }
  703. @IBAction func fontButtonAction(_ sender: NSButton) {
  704. let selectItem = font_PopUpButton.selectedItem
  705. guard let selectItem = selectItem else { return }
  706. let resultAtt = NSMutableAttributedString(attributedString: selectItem.attributedTitle!)
  707. let familyString = resultAtt.string
  708. let styleString = font_StylePopUpButton.selectedItem?.title ?? ""
  709. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: familyString, .face: styleString as Any])
  710. let fontSizeString = font_SizeComboBox.stringValue.replacingOccurrences(of: " pt", with: "")
  711. let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString, fontSize: fontSizeString.stringToCGFloat(), fontColor: fontColorPickerView.color ?? .black, fontAlignment: .left, annotationType: annotationModel.annotationType)
  712. let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
  713. fontWindowController.isHidenAlignmentLabel = true
  714. let window = fontWindowController.window
  715. fontWindowController.callback = { [weak self] model in
  716. self?.fontColorPickerView.color = model.fontColor
  717. DispatchQueue.global(qos: .default).async { [self]
  718. let fonts = CPDFFont.familyNames
  719. let menu = NSMenu()
  720. var selectedIndex = 0
  721. for (index, fontName) in fonts.enumerated() {
  722. if let font = NSFont(name: fontName, size: 12.0) {
  723. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  724. let item = NSMenuItem()
  725. item.attributedTitle = attributedString
  726. menu.addItem(item)
  727. if let family = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  728. let style = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  729. if model.fontName == family {
  730. selectedIndex = index
  731. }
  732. }
  733. }
  734. }
  735. DispatchQueue.main.async {
  736. self?.font_PopUpButton.menu = menu
  737. self?.font_PopUpButton.selectItem(at: selectedIndex)
  738. }
  739. }
  740. let selectedStyleIndex = self?.setFontStyle(fontName: model.fontName, currentStyle: model.fontWeight) ?? 0
  741. self?.font_StylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  742. self?.font_SizeComboBox.stringValue = String(format: "%.f pt", model.fontSize)
  743. self?.annotationModel.setMeasureFontName(model.fontName)
  744. self?.annotationModel.setMeasureFontSize(model.fontSize)
  745. self?.annotationModel.setMeasureFontColor(model.fontColor)
  746. if self?.annotationModel.annotations != nil {
  747. self?.updateAnnotation()
  748. }
  749. }
  750. window?.orderFront(sender)
  751. }
  752. @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
  753. guard let selectedItem = font_PopUpButton.selectedItem else { return }
  754. let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
  755. let familyString = resultAtt.string
  756. let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
  757. guard let styleString = font_StylePopUpButton.selectedItem?.title else { return }
  758. font_StylePopUpButton.selectItem(at: Int(selectIndex))
  759. for tAnnotation in annotations {
  760. if tAnnotation is CPDFLineAnnotation {
  761. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  762. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  763. } else if tAnnotation is CPDFPolygonAnnotation {
  764. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  765. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  766. } else if tAnnotation is CPDFPolylineAnnotation {
  767. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  768. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  769. }
  770. }
  771. self.annotationModel.setMeasureFontName(familyString)
  772. if annotations.count > 0 {
  773. pdfView?.setNeedsDisplay(annotation)
  774. }
  775. }
  776. @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
  777. if annotationModel.annotations != nil {
  778. for tAnnotation in annotations {
  779. if tAnnotation is CPDFLineAnnotation {
  780. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  781. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  782. } else if tAnnotation is CPDFPolygonAnnotation {
  783. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  784. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  785. } else if tAnnotation is CPDFPolylineAnnotation {
  786. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  787. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  788. }
  789. }
  790. } else {
  791. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  792. if let data = annotationModel.measureFontName() {
  793. let font = NSFont(name: data, size: annotationModel.measureFontSize()) ?? NSFont.systemFont(ofSize: 16)
  794. let newFont = NSFont(name: font.fontName, size: CGFloat(font_SizeComboBox.floatValue))
  795. if let newFont = newFont {
  796. annotationModel.setMeasureFontName(newFont.fontName)
  797. annotationModel.setMeasureFontSize(newFont.pointSize)
  798. }
  799. }
  800. }
  801. }
  802. updateAnnotation()
  803. }
  804. //字重 NSFontWeight
  805. @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
  806. if let styleString = font_StylePopUpButton.selectedItem?.title {
  807. if annotationModel.annotations != nil {
  808. for tAnnotation in self.annotations {
  809. if tAnnotation is CPDFLineAnnotation {
  810. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  811. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  812. } else if tAnnotation is CPDFPolygonAnnotation {
  813. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  814. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  815. } else if tAnnotation is CPDFPolylineAnnotation {
  816. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  817. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  818. }
  819. }
  820. } else {
  821. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  822. if let data = annotationModel.measureFontName() {
  823. let font = NSFont(name: data, size: annotationModel.measureFontSize()) ?? NSFont.systemFont(ofSize: 16)
  824. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
  825. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  826. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  827. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
  828. annotationModel.setMeasureFontName(newFont.fontName)
  829. annotationModel.setMeasureFontSize(newFont.pointSize)
  830. }
  831. }
  832. }
  833. }
  834. }
  835. }
  836. updateAnnotation()
  837. }
  838. @IBAction func lineDashComboBoxAction(_ sender: NSComboBox) {
  839. let number = NSNumber(value: lineStyle_DashComBox.floatValue)
  840. var lineDashPattern: [Any] = annotationModel.measureDashPattern()
  841. let array: [NSNumber]
  842. if annotationModel.measureDashPattern() is [NSNumber] {
  843. if lineDashPattern.isEmpty {
  844. array = [number]
  845. } else {
  846. lineDashPattern[0] = number
  847. array = lineDashPattern as! [NSNumber]
  848. }
  849. } else {
  850. array = [number]
  851. }
  852. annotationModel.setMeasureDashPattern(array)
  853. updateAnnotation()
  854. }
  855. // MARK: Set & Get
  856. var annotations: [Any] {
  857. set {
  858. _annotations = newValue
  859. self.annotation = (newValue.first as? CPDFAnnotation)
  860. }
  861. get {
  862. return _annotations
  863. }
  864. }
  865. var annotationModel: CPDFAnnotationModel {
  866. get {
  867. return _annotationModel!
  868. }
  869. set {
  870. _annotationModel = newValue
  871. if _annotationModel?.annotations != nil {
  872. if annotationModel.annotations.count > 0 {
  873. annotations = _annotationModel?.annotations as! [CPDFAnnotation]
  874. }
  875. }
  876. }
  877. }
  878. var annotationType: CAnnotationType {
  879. get {
  880. if annotations.count > 0 {
  881. if let ann = annotation?.isKind(of: CPDFLineAnnotation.self), ann {
  882. if (annotation as! CPDFLineAnnotation).isMeasure {
  883. return .line
  884. }
  885. } else if let ann = annotation?.isKind(of: CPDFPolygonAnnotation.self), ann {
  886. if pdfView?.annotationType == .square {
  887. return .square
  888. }
  889. return .polyGon
  890. } else if let ann = annotation?.isKind(of: CPDFPolylineAnnotation.self), ann {
  891. return .polyLine
  892. }
  893. } else {
  894. return annotationModel.annotationType
  895. }
  896. return .unkown
  897. }
  898. }
  899. }