KMMeasureSideVC.swift 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 private weak var typeSubView1: NSView!
  22. @IBOutlet private weak var typeButton1: NSButton!
  23. @IBOutlet private weak var typeSubView2: NSView!
  24. @IBOutlet private weak var typeButton2: NSButton!
  25. @IBOutlet private weak var typeSubView3: NSView!
  26. @IBOutlet private weak var typeButton3: NSButton!
  27. @IBOutlet private weak var typeSubView4: NSView!
  28. @IBOutlet private weak var typeButton4: NSButton!
  29. @IBOutlet private weak var verticalLine1: NSBox!
  30. @IBOutlet private weak var verticalLine2: NSBox!
  31. @IBOutlet private weak var verticalLine3: NSBox!
  32. @IBOutlet private weak var type_TopConstant: NSLayoutConstraint!
  33. // Border & Fill Color
  34. @IBOutlet private weak var border_FillView: NSView!
  35. @IBOutlet private weak var border_Label: NSTextField!
  36. @IBOutlet private weak var borderColor_View: NSView!
  37. @IBOutlet private weak var borderColor_Button: NSButton!
  38. @IBOutlet private weak var borderColor_TopConstant: NSLayoutConstraint!
  39. // Line Color
  40. @IBOutlet private weak var lineColor_View: NSView!
  41. @IBOutlet private weak var lineColor_Label: NSTextField!
  42. @IBOutlet private weak var lineColor_ColorView: NSView!
  43. @IBOutlet private weak var lineColor_ColorButton: NSButton!
  44. @IBOutlet private weak var lineColor_TopConstant: NSLayoutConstraint!
  45. // Opacity
  46. @IBOutlet private weak var opacity_View: NSView!
  47. @IBOutlet private weak var opacity_Label: NSTextField!
  48. @IBOutlet private weak var opacity_Slider: NSSlider!
  49. @IBOutlet private weak var opacity_ComboBox: KMComboBox!
  50. @IBOutlet private weak var opacity_TopConstant: NSLayoutConstraint!
  51. // Thickness
  52. @IBOutlet private weak var thickness_View: NSView!
  53. @IBOutlet private weak var thickness_Label: NSTextField!
  54. @IBOutlet private weak var thickness_Slider: NSSlider!
  55. @IBOutlet private weak var thickness_ComboBox: KMComboBox!
  56. @IBOutlet private weak var thickness_TopConstant: NSLayoutConstraint!
  57. // Line Style
  58. @IBOutlet private weak var lineStyle_View: NSView!
  59. @IBOutlet private weak var lineStyle_Label: NSTextField!
  60. @IBOutlet private weak var lineStyle_SolidButton: NSButton!
  61. @IBOutlet private weak var lineStyle_SolidImageView: NSImageView!
  62. @IBOutlet private weak var lineStyle_DottedButton: NSButton!
  63. @IBOutlet private weak var lineStyle_DottedImageView: NSImageView!
  64. @IBOutlet private weak var lineStyle_TextField: NSTextField!
  65. @IBOutlet private weak var lineStyle_TopConstant: NSLayoutConstraint!
  66. // Start & End
  67. @IBOutlet private weak var startEnd_View: NSView!
  68. @IBOutlet private weak var startEnd_Label: NSTextField!
  69. @IBOutlet private weak var startEnd_StartPopUpButton: KMPopUpButton!
  70. @IBOutlet private weak var startEnd_EndPopUpButton: KMPopUpButton!
  71. @IBOutlet private weak var startEnd_TopConstant: NSLayoutConstraint!
  72. // Font
  73. @IBOutlet private weak var font_View: NSView!
  74. @IBOutlet private weak var font_Label: NSTextField!
  75. @IBOutlet private weak var font_PopUpButton: KMPopUpButton!
  76. @IBOutlet private weak var font_ColorView: NSView!
  77. @IBOutlet private weak var font_ColorButton: NSButton!
  78. @IBOutlet private weak var font_StylePopUpButton: KMPopUpButton!
  79. @IBOutlet private weak var font_SizeComboBox: KMComboBox!
  80. @IBOutlet private weak var font_TopConstant: NSLayoutConstraint!
  81. var annotation: CPDFAnnotation?
  82. weak var pdfView: CPDFListView?
  83. var _annotationModel: CPDFAnnotationModel?
  84. var _annotations: [Any] = []
  85. private var measureType: KMMeasureType = .line
  86. deinit {
  87. NotificationCenter.default.removeObserver(self)
  88. }
  89. override func viewDidLoad() {
  90. super.viewDidLoad()
  91. // Do view setup here.
  92. if annotationType == .line {
  93. if pdfView?.toolMode == .measureToolMode {
  94. measureType = .line
  95. }
  96. } else if annotationType == .square {
  97. if pdfView?.toolMode == .measureToolMode {
  98. measureType = .rectangle
  99. }
  100. } else if annotationType == .polyGon {
  101. measureType = .polygon
  102. } else if annotationType == .polyLine {
  103. measureType = .multilines
  104. }
  105. languageLocalization()
  106. uiInitialize()
  107. layoutConstraintRefresh()
  108. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: lineStyle_TextField)
  109. }
  110. // MARK: Privately Methods
  111. private func languageLocalization() -> Void {
  112. if measureType == .polygon {
  113. border_Label.stringValue = NSLocalizedString("Fill", comment: "")
  114. } else {
  115. border_Label.stringValue = NSLocalizedString("Border", comment: "")
  116. }
  117. lineColor_Label.stringValue = NSLocalizedString("Line Color", comment: "")
  118. opacity_Label.stringValue = NSLocalizedString("Opacity", comment: "")
  119. thickness_Label.stringValue = NSLocalizedString("Thickness", comment: "")
  120. lineStyle_Label.stringValue = NSLocalizedString("Line Style", comment: "")
  121. startEnd_Label.stringValue = NSLocalizedString("Start & End", comment: "")
  122. font_Label.stringValue = NSLocalizedString("Font", comment: "")
  123. }
  124. private func uiInitialize() -> Void {
  125. border_Label.textColor = KMAppearance.Layout.h0Color()
  126. border_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  127. lineColor_Label.textColor = KMAppearance.Layout.h0Color()
  128. lineColor_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  129. opacity_Label.textColor = KMAppearance.Layout.h0Color()
  130. opacity_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  131. thickness_Label.textColor = KMAppearance.Layout.h0Color()
  132. thickness_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  133. lineStyle_Label.textColor = KMAppearance.Layout.h0Color()
  134. lineStyle_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  135. startEnd_Label.textColor = KMAppearance.Layout.h0Color()
  136. startEnd_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  137. font_Label.textColor = KMAppearance.Layout.h0Color()
  138. font_Label.font = NSFont(descriptor: NSFontDescriptor(name: "Segoe UI", size: 14.0).withSymbolicTraits(.bold), size: 14.0) ?? NSFont(name: "Segoe UI", size: 14.0)
  139. opacity_ComboBox.type = .none
  140. opacity_ComboBox.comboxRect = opacity_ComboBox.bounds
  141. thickness_ComboBox.type = .none
  142. thickness_ComboBox.comboxRect = thickness_ComboBox.bounds
  143. font_PopUpButton.type = .arrowDown
  144. font_PopUpButton.removeAllItems()
  145. font_PopUpButton.type = .arrowDown
  146. font_PopUpButton.removeAllItems()
  147. font_SizeComboBox.type = .none
  148. lineStyle_SolidImageView.image = NSImage(named: "KMImageNameLifeStyle")
  149. lineStyle_DottedImageView.image = NSImage(named: "KMImageNameLifeStyle2")
  150. lineStyle_TextField.isEnabled = false
  151. if annotationModel.dashPattern().count > 0 {
  152. lineStyle_TextField.stringValue = (annotationModel.dashPattern().first as! NSNumber).description
  153. } else {
  154. lineStyle_TextField.stringValue = "5"
  155. }
  156. annotationFontReload()
  157. if measureType == .line {
  158. previewImageView.image = NSImage(named: "ImageNameToolbarMeasureLine")
  159. startEnd_StartPopUpButton.type = .arrowDown
  160. startEnd_StartPopUpButton.removeAllItems()
  161. startEnd_EndPopUpButton.type = .arrowDown
  162. startEnd_EndPopUpButton.removeAllItems()
  163. let startMenu = NSMenu()
  164. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 0)
  165. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 1)
  166. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 2)
  167. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 3)
  168. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 4)
  169. _ = startMenu.addItem(withTitle: "", action: #selector(startMenuItemAction(_:)), target: self, tag: 5)
  170. startEnd_StartPopUpButton.menu = startMenu
  171. let endMenu = NSMenu()
  172. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 0)
  173. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 1)
  174. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 2)
  175. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 3)
  176. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 4)
  177. _ = endMenu.addItem(withTitle: "", action: #selector(endMenuItemAction(_:)), target: self, tag: 5)
  178. startEnd_EndPopUpButton.menu = endMenu
  179. let startPopMenu = startEnd_StartPopUpButton.menu
  180. let endPopMenu = startEnd_EndPopUpButton.menu
  181. let startLineStyle = annotationModel.startLineStyle()
  182. let endLineStyle = annotationModel.endLineStyle()
  183. let startArr = self.startLineStyle()
  184. let endArr = self.endLineStyle()
  185. for i in 0..<startArr.count {
  186. let item = startPopMenu!.item(withTag: i)
  187. item?.image = startArr[i]
  188. }
  189. for i in 0..<endArr.count {
  190. let item = endPopMenu!.item(withTag: i)
  191. item?.image = endArr[i]
  192. }
  193. if let line = annotation?.isKind(of: CPDFLineAnnotation.self), line {
  194. startEnd_StartPopUpButton.selectItem(at: (annotation as! CPDFLineAnnotation).startLineStyle.rawValue)
  195. startEnd_EndPopUpButton.selectItem(at: (annotation as! CPDFLineAnnotation).endLineStyle.rawValue)
  196. } else {
  197. startEnd_StartPopUpButton.selectItem(at: startLineStyle.rawValue)
  198. startEnd_EndPopUpButton.selectItem(at: endLineStyle.rawValue)
  199. }
  200. thicknessViewRefresh()
  201. } else if measureType == .multilines {
  202. previewImageView.image = NSImage(named: "ImageNameToolbarPolyline")
  203. thicknessViewRefresh()
  204. } else if measureType == .polygon {
  205. previewImageView.image = NSImage(named: "ImageNameToolbarPolygon")
  206. lineColorViewRefresh()
  207. thicknessViewRefresh()
  208. } else if measureType == .rectangle {
  209. previewImageView.image = NSImage(named: "ImageNameToolbarMeasureSquare")
  210. lineColorViewRefresh()
  211. }
  212. font_ColorView.backgroundColor(annotationModel.fontColor())
  213. font_SizeComboBox.stringValue = String(format: "%.f pt", annotationModel.fontSize())
  214. borderColorViewRefresh()
  215. typeViewRefresh()
  216. opacityViewRefresh()
  217. lineStyleViewRefresh()
  218. }
  219. private func typeViewRefresh() -> Void {
  220. verticalLine1.isHidden = false
  221. verticalLine2.isHidden = false
  222. verticalLine3.isHidden = false
  223. typeSubView1.backgroundColor(.clear)
  224. typeSubView2.backgroundColor(.clear)
  225. typeSubView3.backgroundColor(.clear)
  226. typeSubView4.backgroundColor(.clear)
  227. typeButton1.image = NSImage(named: "ImageNameToolbarMeasureLine")
  228. typeButton2.image = NSImage(named: "ImageNameToolbarPolyline")
  229. typeButton3.image = NSImage(named: "ImageNameToolbarPolygon")
  230. typeButton4.image = NSImage(named: "ImageNameToolbarMeasureSquare")
  231. typeButton1.isEnabled = false
  232. typeButton2.isEnabled = false
  233. typeButton3.isEnabled = false
  234. typeButton4.isEnabled = false
  235. if measureType == .line {
  236. verticalLine1.isHidden = true
  237. typeSubView1.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  238. typeButton1.isEnabled = true
  239. } else if measureType == .multilines {
  240. verticalLine1.isHidden = true
  241. verticalLine2.isHidden = true
  242. typeSubView2.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  243. typeButton2.isEnabled = true
  244. } else if measureType == .polygon {
  245. verticalLine2.isHidden = true
  246. verticalLine3.isHidden = true
  247. typeSubView3.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  248. typeButton3.isEnabled = false
  249. } else if measureType == .rectangle {
  250. verticalLine3.isHidden = true
  251. typeSubView4.backgroundColor(NSColor.km_init(hex: "#477EDE", alpha: 0.1))
  252. }
  253. }
  254. private func borderColorViewRefresh() -> Void {
  255. if measureType == .polygon || measureType == .rectangle {
  256. if annotation is CPDFPolygonAnnotation {
  257. let newAnnotation = (annotation as! CPDFPolygonAnnotation)
  258. borderColor_View.backgroundColor(newAnnotation.interiorColor)
  259. }
  260. } else {
  261. borderColor_View.backgroundColor(annotationModel.color())
  262. }
  263. }
  264. private func lineColorViewRefresh() -> Void {
  265. lineColor_ColorView.backgroundColor(annotationModel.color())
  266. }
  267. private func fontColorViewRefresh() -> Void {
  268. font_ColorView.backgroundColor(annotationModel.fontColor())
  269. }
  270. private func opacityViewRefresh() -> Void {
  271. opacity_Slider.floatValue = Float(annotationModel.opacity())
  272. opacity_Slider.toolTip = "\(Int(Float(annotationModel.opacity()) * 100))%"
  273. opacity_ComboBox.stringValue = "\(Int(Float(annotationModel.opacity()) * 100))%"
  274. }
  275. private func thicknessViewRefresh() -> Void {
  276. thickness_Slider.floatValue = Float(annotationModel.lineWidth())
  277. thickness_Slider.toolTip = "\(Int(Float(annotationModel.lineWidth())))"
  278. thickness_ComboBox.stringValue = "\(Int(Float(annotationModel.lineWidth())))"
  279. }
  280. private func lineStyleViewRefresh() -> Void {
  281. if annotationModel.style() == .solid {
  282. lineStyle_SolidButton.image = NSImage(named: "KMImageNameCheckboxRadiSel")
  283. lineStyle_DottedButton.image = NSImage(named: "KMImageNameCheckboxRadi")
  284. lineStyle_TextField.isEnabled = false
  285. } else {
  286. lineStyle_SolidButton.image = NSImage(named: "KMImageNameCheckboxRadi")
  287. lineStyle_DottedButton.image = NSImage(named: "KMImageNameCheckboxRadiSel")
  288. lineStyle_TextField.isEnabled = true
  289. }
  290. }
  291. private func layoutConstraintRefresh() -> Void {
  292. border_FillView.isHidden = false
  293. lineColor_View.isHidden = false
  294. opacity_View.isHidden = false
  295. thickness_View.isHidden = false
  296. lineStyle_View.isHidden = false
  297. startEnd_View.isHidden = false
  298. font_View.isHidden = false
  299. if measureType == .line {
  300. lineColor_View.isHidden = true
  301. } else if measureType == .multilines {
  302. lineColor_View.isHidden = true
  303. startEnd_View.isHidden = true
  304. } else if measureType == .polygon {
  305. startEnd_View.isHidden = true
  306. } else if measureType == .rectangle {
  307. thickness_View.isHidden = true
  308. startEnd_View.isHidden = true
  309. }
  310. borderColor_TopConstant.constant = !border_FillView.isHidden ? 10.0 : -CGRectGetHeight(border_FillView.bounds)
  311. lineColor_TopConstant.constant = !lineColor_View.isHidden ? 0.0 : -CGRectGetHeight(lineColor_View.bounds)
  312. opacity_TopConstant.constant = !opacity_View.isHidden ? 0.0 : -CGRectGetHeight(opacity_View.bounds)
  313. thickness_TopConstant.constant = !thickness_View.isHidden ? 0.0 : -CGRectGetHeight(thickness_View.bounds)
  314. lineStyle_TopConstant.constant = !lineStyle_View.isHidden ? 0.0 : -CGRectGetHeight(lineStyle_View.bounds)
  315. startEnd_TopConstant.constant = !startEnd_View.isHidden ? 0.0 : -CGRectGetHeight(startEnd_View.bounds)
  316. font_TopConstant.constant = !font_View.isHidden ? 0.0 : -CGRectGetHeight(font_View.bounds)
  317. }
  318. private func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
  319. var selectIndex: UInt = 0
  320. let menu = NSMenu()
  321. let fontStyleNames = CPDFFont.fontNames(forFamilyName: fontName)
  322. for i in 0..<fontStyleNames.count {
  323. let styleName = fontStyleNames[i]
  324. if style == styleName {
  325. selectIndex = UInt(i)
  326. }
  327. // let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
  328. // let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0)
  329. // let attrited = [NSAttributedString.Key.font: font]
  330. // let string = NSAttributedString(string: styleName, attributes: attrited as [NSAttributedString.Key : Any])
  331. //
  332. // let item = NSMenuItem()
  333. // item.attributedTitle = string
  334. let item = NSMenuItem()
  335. item.title = styleName
  336. menu.addItem(item)
  337. }
  338. if style == nil {
  339. selectIndex = 0
  340. }
  341. font_StylePopUpButton.menu = menu
  342. return selectIndex
  343. }
  344. private func annotationFontReload() {
  345. DispatchQueue.global(qos: .default).async { [weak self] in
  346. guard let self = self else { return }
  347. let fonts = CPDFFont.familyNames
  348. let menu = NSMenu()
  349. var selectedIndex = 0
  350. for (index, fontName) in fonts.enumerated() {
  351. if let font = NSFont(name: fontName, size: 12.0) {
  352. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  353. let item = NSMenuItem()
  354. item.attributedTitle = attributedString
  355. menu.addItem(item)
  356. if [.line, .polyGon, .polyLine, .square].contains(self.annotationType),
  357. let modelFontName = self.annotationModel.fontName(),
  358. modelFontName == font.fontName {
  359. selectedIndex = index
  360. }
  361. }
  362. }
  363. if [.line, .polyGon, .polyLine, .square].contains(self.annotationType),
  364. let data = self.annotationModel.fontName() {
  365. let freetextFont = NSFont(name: data, size: 16) ?? NSFont.systemFont(ofSize: 16)
  366. if let family = freetextFont.fontDescriptor.object(forKey: .family) as? String,
  367. let style = freetextFont.fontDescriptor.object(forKey: .face) as? String {
  368. DispatchQueue.main.async { [weak self] in
  369. guard let self = self else { return }
  370. font_PopUpButton.menu = menu
  371. font_PopUpButton.selectItem(at: selectedIndex)
  372. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  373. font_StylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  374. }
  375. }
  376. }
  377. }
  378. }
  379. @IBAction private func colorPanelAction1(_ sender: NSButton) {
  380. if measureType == .polygon || measureType == .rectangle {
  381. for tAnnotation in annotations {
  382. if tAnnotation is CPDFPolygonAnnotation {
  383. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  384. newAnnotation.interiorColor = NSColorPanel.shared.color
  385. }
  386. }
  387. } else {
  388. annotationModel.setColor(NSColorPanel.shared.color)
  389. }
  390. borderColorViewRefresh()
  391. updateAnnotation()
  392. }
  393. @IBAction private func colorPanelAction2(_ sender: NSButton) {
  394. lineColorViewRefresh()
  395. annotationModel.setInteriorColor(NSColorPanel.shared.color)
  396. updateAnnotation()
  397. }
  398. @IBAction private func colorPanelAction3(_ sender: NSButton) {
  399. fontColorViewRefresh()
  400. annotationModel.setFontColor(NSColorPanel.shared.color)
  401. updateAnnotation()
  402. }
  403. private func updateAnnotation() {
  404. if annotationModel.annotation != nil {
  405. for tAnnotation in annotations {
  406. pdfView?.setNeedsDisplayAnnotationViewFor((tAnnotation as AnyObject).page)
  407. }
  408. }
  409. }
  410. private func startLineStyle() -> [NSImage] {
  411. let size = NSSize(width: 109.0, height: 12.0)
  412. let image1 = NSImage.image(with: size) { rect in
  413. let path = NSBezierPath()
  414. path.move(to: NSPoint(x: 93.0, y: 6.0))
  415. path.line(to: NSPoint(x: 6.0, y: 6.0))
  416. path.lineWidth = 2.0
  417. NSColor.black.setStroke()
  418. path.stroke()
  419. return true
  420. }
  421. let image2 = NSImage.image(with: size) { dstRect in
  422. let path = NSBezierPath()
  423. path.move(to: NSPoint(x: 93.0, y: 6.0))
  424. path.line(to: NSPoint(x: 12.0, y: 6.0))
  425. path.appendRect(NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  426. path.lineWidth = 2.0
  427. NSColor.black.setStroke()
  428. path.stroke()
  429. return true
  430. }
  431. let image3 = NSImage.image(with: size) { dstRect in
  432. let path = NSBezierPath()
  433. path.move(to: NSPoint(x: 93.0, y: 6.0))
  434. path.line(to: NSPoint(x: 12.0, y: 6.0))
  435. path.appendOval(in: NSRect(x: 6.0, y: 3.0, width: 6.0, height: 6.0))
  436. path.lineWidth = 2.0
  437. NSColor.black.setStroke()
  438. path.stroke()
  439. return true
  440. }
  441. let image4 = NSImage.image(with: size) { dstRect in
  442. let path = NSBezierPath()
  443. path.move(to: NSPoint(x: 93.0, y: 6.0))
  444. path.line(to: NSPoint(x: 14.0, y: 6.0))
  445. path.move(to: NSPoint(x: 14.0, y: 6.0))
  446. path.line(to: NSPoint(x: 10.0, y: 10.0))
  447. path.line(to: NSPoint(x: 6.0, y: 6.0))
  448. path.line(to: NSPoint(x: 10.0, y: 2.0))
  449. path.close()
  450. path.lineWidth = 2.0
  451. path.stroke()
  452. return true
  453. }
  454. let image5 = NSImage.image(with: size) { dstRect in
  455. let path = NSBezierPath()
  456. path.move(to: NSPoint(x: 93.0, y: 6.0))
  457. path.line(to: NSPoint(x: 6.0, y: 6.0))
  458. path.move(to: NSPoint(x: 15.0, y: 3.0))
  459. path.line(to: NSPoint(x: 6.0, y: 6.0))
  460. path.line(to: NSPoint(x: 15.0, y: 9.0))
  461. path.lineWidth = 2.0
  462. NSColor.black.setStroke()
  463. path.stroke()
  464. return true
  465. }
  466. let image6 = 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: 6.0, y: 6.0))
  470. path.move(to: NSPoint(x: 15.0, y: 3.0))
  471. path.line(to: NSPoint(x: 6.0, y: 6.0))
  472. path.line(to: NSPoint(x: 15.0, y: 9.0))
  473. path.close()
  474. path.lineWidth = 2.0
  475. NSColor.black.setStroke()
  476. path.stroke()
  477. return true
  478. }
  479. return [image1, image5, image6, image2, image3, image4]
  480. }
  481. private func endLineStyle() -> [NSImage] {
  482. let size = NSSize(width: 109.0, height: 12.0)
  483. let image1 = NSImage.image(with: size) { rect in
  484. let path = NSBezierPath()
  485. path.move(to: NSPoint(x: 6.0, y: 6.0))
  486. path.line(to: NSPoint(x: 93.0, y: 6.0))
  487. path.lineWidth = 2.0
  488. NSColor.black.setStroke()
  489. path.stroke()
  490. return true
  491. }
  492. let image2 = NSImage.image(with: size) { rect in
  493. let path = NSBezierPath()
  494. path.move(to: NSPoint(x: 6.0, y: 6.0))
  495. path.line(to: NSPoint(x: 87.0, y: 6.0))
  496. path.appendRect(NSRect(x: 87.0, y: 3.0, width: 6.0, height: 6.0))
  497. path.lineWidth = 2.0
  498. NSColor.black.setStroke()
  499. path.stroke()
  500. return true
  501. }
  502. let image3 = NSImage.image(with: size) { rect in
  503. let path = NSBezierPath()
  504. path.move(to: NSPoint(x: 6.0, y: 6.0))
  505. path.line(to: NSPoint(x: 87.0, y: 6.0))
  506. path.appendOval(in: NSRect(x: 87.0, y: 3.0, width: 6.0, height: 6.0))
  507. path.lineWidth = 2.0
  508. NSColor.black.setStroke()
  509. path.stroke()
  510. return true
  511. }
  512. let image4 = 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: 87.0, y: 6.0))
  516. path.move(to: NSPoint(x: 87.0, y: 6.0))
  517. path.line(to: NSPoint(x: 91.0, y: 10.0))
  518. path.line(to: NSPoint(x: 95.0, y: 6.0))
  519. path.line(to: NSPoint(x: 91.0, y: 2.0))
  520. path.close()
  521. path.lineWidth = 2.0
  522. path.stroke()
  523. return true
  524. }
  525. let image5 = NSImage.image(with: size) { rect in
  526. let path = NSBezierPath()
  527. path.move(to: NSPoint(x: 6.0, y: 6.0))
  528. path.line(to: NSPoint(x: 93.0, y: 6.0))
  529. path.move(to: NSPoint(x: 84.0, y: 3.0))
  530. path.line(to: NSPoint(x: 93.0, y: 6.0))
  531. path.line(to: NSPoint(x: 84.0, y: 9.0))
  532. path.lineWidth = 2.0
  533. NSColor.black.setStroke()
  534. path.stroke()
  535. return true
  536. }
  537. let image6 = NSImage.image(with: size) { rect in
  538. let path = NSBezierPath()
  539. path.move(to: NSPoint(x: 6.0, y: 6.0))
  540. path.line(to: NSPoint(x: 93.0, y: 6.0))
  541. path.move(to: NSPoint(x: 84.0, y: 3.0))
  542. path.line(to: NSPoint(x: 93.0, y: 6.0))
  543. path.line(to: NSPoint(x: 84.0, y: 9.0))
  544. path.close()
  545. path.lineWidth = 2.0
  546. NSColor.black.setStroke()
  547. path.stroke()
  548. return true
  549. }
  550. return [image1, image5, image6, image2, image3, image4]
  551. }
  552. private func isNumericUsingNSRegularExpression(_ input: String) -> Bool {
  553. let pattern = "^[0-9]+$"
  554. do {
  555. let regex = try NSRegularExpression(pattern: pattern)
  556. let matches = regex.matches(in: input, range: NSRange(location: 0, length: input.utf16.count))
  557. return matches.count > 0
  558. } catch {
  559. print("Invalid regex pattern")
  560. return false
  561. }
  562. }
  563. // MARK: Publicly Methods
  564. // MARK: Action
  565. @IBAction func typeButtonAction(_ sender: NSButton) {
  566. }
  567. @IBAction func customColorAction(_ sender: NSButton) {
  568. NSColorPanel.shared.setAction(nil)
  569. NSColorPanel.shared.setTarget(nil)
  570. NSColorPanel.shared.setTarget(self)
  571. NSColorPanel.shared.showsAlpha = true
  572. if sender.tag == 0 {
  573. NSColorPanel.shared.setAction(#selector(colorPanelAction1(_:)))
  574. } else if sender.tag == 1 {
  575. NSColorPanel.shared.setAction(#selector(colorPanelAction2(_:)))
  576. } else if sender.tag == 2 {
  577. NSColorPanel.shared.setAction(#selector(colorPanelAction3(_:)))
  578. }
  579. NSColorPanel.shared.orderFront(nil)
  580. }
  581. @IBAction func opacitySliderAction(_ sender: Any) {
  582. var opcity = CGFloat(opacity_Slider.floatValue)
  583. if opcity == 0 {
  584. opcity = 0.0001
  585. }
  586. annotationModel.setOpacity(opcity)
  587. opacityViewRefresh()
  588. updateAnnotation()
  589. }
  590. @IBAction func opacityComboBoxAction(_ sender: Any) {
  591. opacity_Slider.floatValue = Float(opacity_ComboBox.floatValue / 100)
  592. var opcity = CGFloat(opacity_Slider.floatValue)
  593. if opcity == 0 {
  594. opcity = 0.0001
  595. }
  596. annotationModel.setOpacity(opcity)
  597. opacityViewRefresh()
  598. updateAnnotation()
  599. }
  600. @IBAction func thicknessSliderAction(_ sender: Any) {
  601. let thickness = CGFloat(thickness_Slider.floatValue)
  602. annotationModel.setLineWidth(thickness)
  603. thicknessViewRefresh()
  604. updateAnnotation()
  605. }
  606. @IBAction func thicknessComboBoxAction(_ sender: Any) {
  607. thickness_Slider.floatValue = Float(thickness_ComboBox.floatValue)
  608. let thickness = CGFloat(thickness_Slider.floatValue)
  609. annotationModel.setLineWidth(thickness)
  610. thicknessViewRefresh()
  611. updateAnnotation()
  612. }
  613. @IBAction func lineStyleButtonAction(_ sender: NSButton) {
  614. if sender.tag == 0 {
  615. annotationModel.setStyle(.solid)
  616. } else {
  617. annotationModel.setStyle(.dashed)
  618. annotation?.setDashPattern(annotationModel.dashPattern())
  619. }
  620. lineStyleViewRefresh()
  621. updateAnnotation()
  622. }
  623. @IBAction func startMenuItemAction(_ sender: NSMenuItem) {
  624. let startType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  625. annotationModel.setStart(startType)
  626. updateAnnotation()
  627. }
  628. @IBAction func endMenuItemAction(_ sender: NSMenuItem) {
  629. let endType: CPDFLineStyle = (CPDFLineStyle(rawValue: sender.tag) ?? CPDFLineStyle(rawValue: 0))!
  630. annotationModel.setEnd(endType)
  631. updateAnnotation()
  632. }
  633. @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
  634. guard let selectedItem = font_PopUpButton.selectedItem else { return }
  635. let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
  636. let familyString = resultAtt.string
  637. let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
  638. guard let styleString = font_StylePopUpButton.selectedItem?.title else { return }
  639. font_StylePopUpButton.selectItem(at: Int(selectIndex))
  640. for tAnnotation in annotations {
  641. if tAnnotation is CPDFLineAnnotation {
  642. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  643. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  644. } else if tAnnotation is CPDFPolygonAnnotation {
  645. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  646. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  647. } else if tAnnotation is CPDFPolylineAnnotation {
  648. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  649. newAnnotation.cFont = CPDFFont(familyName: familyString, fontStyle: styleString)
  650. }
  651. }
  652. pdfView?.setNeedsDisplay(annotation)
  653. }
  654. @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
  655. if annotationModel.annotations != nil {
  656. for tAnnotation in annotations {
  657. if tAnnotation is CPDFLineAnnotation {
  658. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  659. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  660. } else if tAnnotation is CPDFPolygonAnnotation {
  661. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  662. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  663. } else if tAnnotation is CPDFPolylineAnnotation {
  664. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  665. newAnnotation.fontSize = CGFloat(font_SizeComboBox.floatValue)
  666. }
  667. }
  668. } else {
  669. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  670. if let data = annotationModel.fontName() {
  671. let font = NSFont(name: data, size: annotationModel.fontSize()) ?? NSFont.systemFont(ofSize: 16)
  672. let newFont = NSFont(name: font.fontName, size: CGFloat(font_SizeComboBox.floatValue))
  673. if let newFont = newFont {
  674. annotationModel.setFontName(newFont.fontName)
  675. annotationModel.setFontSize(newFont.pointSize)
  676. }
  677. }
  678. }
  679. }
  680. updateAnnotation()
  681. }
  682. //字重 NSFontWeight
  683. @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
  684. if let styleString = font_StylePopUpButton.selectedItem?.title {
  685. if annotationModel.annotations != nil {
  686. for tAnnotation in self.annotations {
  687. if tAnnotation is CPDFLineAnnotation {
  688. let newAnnotation = (tAnnotation as! CPDFLineAnnotation)
  689. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  690. } else if tAnnotation is CPDFPolygonAnnotation {
  691. let newAnnotation = (tAnnotation as! CPDFPolygonAnnotation)
  692. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  693. } else if tAnnotation is CPDFPolylineAnnotation {
  694. let newAnnotation = (tAnnotation as! CPDFPolylineAnnotation)
  695. newAnnotation.cFont = CPDFFont(familyName: newAnnotation.cFont.familyName, fontStyle: styleString)
  696. }
  697. }
  698. } else {
  699. if annotationType == .line || annotationType == .polyGon || annotationType == .polyLine {
  700. if let data = annotationModel.fontName() {
  701. let font = NSFont(name: data, size: annotationModel.fontSize()) ?? NSFont.systemFont(ofSize: 16)
  702. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
  703. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  704. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  705. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
  706. annotationModel.setFontName(newFont.fontName)
  707. annotationModel.setFontSize(newFont.pointSize)
  708. }
  709. }
  710. }
  711. }
  712. }
  713. }
  714. updateAnnotation()
  715. }
  716. // MARK: Set & Get
  717. var annotations: [Any] {
  718. set {
  719. _annotations = newValue
  720. self.annotation = (newValue.first as? CPDFAnnotation)
  721. }
  722. get {
  723. return _annotations
  724. }
  725. }
  726. var annotationModel: CPDFAnnotationModel {
  727. get {
  728. return _annotationModel!
  729. }
  730. set {
  731. _annotationModel = newValue
  732. if _annotationModel?.annotations != nil {
  733. if annotationModel.annotations.count > 0 {
  734. annotations = _annotationModel?.annotations as! [CPDFAnnotation]
  735. }
  736. }
  737. }
  738. }
  739. var annotationType: CAnnotationType {
  740. get {
  741. if let ann = annotation?.isKind(of: CPDFLineAnnotation.self), ann {
  742. if (annotation as! CPDFLineAnnotation).isMeasure {
  743. return .line
  744. }
  745. } else if let ann = annotation?.isKind(of: CPDFPolygonAnnotation.self), ann {
  746. if pdfView?.annotationType == .square {
  747. return .square
  748. }
  749. return .polyGon
  750. } else if let ann = annotation?.isKind(of: CPDFPolylineAnnotation.self), ann {
  751. return .polyLine
  752. }
  753. return .unkown
  754. }
  755. }
  756. // MARK: Notification
  757. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  758. if lineStyle_TextField.stringValue.isEmpty == true {
  759. return
  760. }
  761. if isNumericUsingNSRegularExpression(lineStyle_TextField.stringValue) == false {
  762. lineStyle_TextField.stringValue = "5"
  763. return
  764. }
  765. var dashFloat = Float(lineStyle_TextField.stringValue)
  766. if dashFloat == 0 {
  767. lineStyle_TextField.stringValue = "1"
  768. dashFloat = 1.0
  769. }
  770. let number = NSNumber(value: dashFloat!)
  771. var lineDashPattern: [Any] = annotationModel.dashPattern()
  772. let array: [NSNumber]
  773. if annotationModel.dashPattern() is [NSNumber] {
  774. if lineDashPattern.isEmpty {
  775. array = [number]
  776. } else {
  777. lineDashPattern[0] = number
  778. array = lineDashPattern as! [NSNumber]
  779. }
  780. } else {
  781. array = [number]
  782. }
  783. annotation?.setDashPattern(array)
  784. updateAnnotation()
  785. }
  786. }