KMMeasureSideVC.swift 44 KB

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