KMGeneralAnnotationViewController.swift 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  1. //
  2. // KMGeneralAnnotationViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/12/1.
  6. //
  7. import Cocoa
  8. /**
  9. NSString *SKNFreeTextString = @"FreeText";
  10. NSString *SKNTextString = @"Text";
  11. NSString *SKNNoteString = @"Note";
  12. NSString *SKNCircleString = @"Circle";
  13. NSString *SKNSquareString = @"Square";
  14. NSString *SKNMarkUpString = @"MarkUp";
  15. NSString *SKNHighlightString = @"Highlight";
  16. NSString *SKNUnderlineString = @"Underline";
  17. NSString *SKNStrikeOutString = @"StrikeOut";
  18. NSString *SKNLineString = @"Line";
  19. NSString *SKNInkString = @"Ink";
  20. NSString *SKNPDFAnnotationTypeKey = @"type";
  21. NSString *SKNPDFAnnotationBoundsKey = @"bounds";
  22. NSString *SKNPDFAnnotationPageKey = @"page";
  23. NSString *SKNPDFAnnotationPageIndexKey = @"pageIndex";
  24. NSString *SKNPDFAnnotationContentsKey = @"contents";
  25. NSString *SKNPDFAnnotationStringKey = @"string";
  26. NSString *SKNPDFAnnotationColorKey = @"color";
  27. NSString *SKNPDFAnnotationBorderKey = @"border";
  28. NSString *SKNPDFAnnotationLineWidthKey = @"lineWidth";
  29. NSString *SKNPDFAnnotationBorderStyleKey = @"borderStyle";
  30. NSString *SKNPDFAnnotationDashPatternKey = @"dashPattern";
  31. NSString *SKNPDFAnnotationModificationDateKey = @"modificationDate";
  32. NSString *SKNPDFAnnotationUserNameKey = @"userName";
  33. NSString *SKNPDFAnnotationInteriorColorKey = @"interiorColor";
  34. NSString *SKNPDFAnnotationStartLineStyleKey = @"startLineStyle";
  35. NSString *SKNPDFAnnotationEndLineStyleKey = @"endLineStyle";
  36. NSString *SKNPDFAnnotationStartPointKey = @"startPoint";
  37. NSString *SKNPDFAnnotationEndPointKey = @"endPoint";
  38. NSString *SKNPDFAnnotationFontKey = @"font";
  39. NSString *SKNPDFAnnotationFontColorKey = @"fontColor";
  40. NSString *SKNPDFAnnotationFontNameKey = @"fontName";
  41. NSString *SKNPDFAnnotationFontSizeKey = @"fontSize";
  42. NSString *SKNPDFAnnotationAlignmentKey = @"alignment";
  43. NSString *SKNPDFAnnotationRotationKey = @"rotation";
  44. NSString *SKNPDFAnnotationQuadrilateralPointsKey = @"quadrilateralPoints";
  45. NSString *SKNPDFAnnotationIconTypeKey = @"iconType";
  46. NSString *SKNPDFAnnotationPointListsKey = @"pointLists";
  47. */
  48. enum KMActiveAnnotationType: UInt {
  49. case highlight = 0
  50. case strikeOut
  51. case underline
  52. case inkAndLine
  53. case text
  54. case squareAndCircle
  55. case freeText
  56. case selfSignFreeText
  57. }
  58. enum KMFreeTextAnnotationAlignmentType: UInt {
  59. case left = 0
  60. case center
  61. case right
  62. }
  63. let KMColorPickerViewHeight: CGFloat = 64
  64. @objcMembers class KMGeneralAnnotationViewController: NSViewController, NSTextViewDelegate {
  65. var _annotations: [CPDFAnnotation]?
  66. var subType: KMSelfSignAnnotationFreeTextSubType = .none
  67. var _activeAnnotationType: KMActiveAnnotationType = .highlight
  68. var pdfView: CPDFListView?
  69. var annotationModel: CPDFAnnotationModel?
  70. // var annotationCallback: ((CPDFAnnotation) -> Void)?
  71. @IBOutlet private weak var imageBox: NSBox!
  72. @IBOutlet private weak var imageBoxLayoutConstraint: NSLayoutConstraint!
  73. @IBOutlet private weak var textImageBoxView: NSView!
  74. @IBOutlet private weak var textImageBox: KMBox!
  75. @IBOutlet private weak var textAnnotationImageView: NSImageView!
  76. @IBOutlet private weak var textImageBoxButton: NSButton!
  77. @IBOutlet private weak var textImageUpDateButton: NSButton!
  78. @IBOutlet private weak var generalImageBoxView: NSView!
  79. @IBOutlet private weak var generalImageView: NSImageView!
  80. @IBOutlet private weak var fontView: NSView! // 字体
  81. @IBOutlet private weak var fontLabel: NSTextField!
  82. @IBOutlet private weak var fontButton: NSButton!
  83. @IBOutlet private weak var fontPopUpButton: KMPopUpButton!
  84. @IBOutlet private weak var fontSizeComboBox: KMComboBox!
  85. @IBOutlet private weak var fontStylePopUpButton: KMPopUpButton!
  86. @IBOutlet private weak var leftAlignButton: NSButton!
  87. @IBOutlet private weak var centerAlignButton: NSButton!
  88. @IBOutlet private weak var rightAlignButton: NSButton!
  89. @IBOutlet private weak var fontViewTopConstant: NSLayoutConstraint!
  90. @IBOutlet private weak var fontViewCorloPV: KMColorPickerView!
  91. @IBOutlet private weak var fontViewColorPVConstraint: NSLayoutConstraint!
  92. @IBOutlet private weak var rotateView: NSView! // 旋转
  93. @IBOutlet private weak var rotateLabel: NSTextField!
  94. @IBOutlet private weak var leftRotateBox: KMBox!
  95. @IBOutlet private weak var leftRotateButton: NSButton!
  96. @IBOutlet private weak var rightRotateBox: KMBox!
  97. @IBOutlet private weak var rightRotateButton: NSButton!
  98. @IBOutlet private weak var rotateViewTopConstant: NSLayoutConstraint!
  99. @IBOutlet private weak var typeView: NSView! // 类型
  100. @IBOutlet private weak var typeLabel: NSTextField!
  101. @IBOutlet private weak var tureTypeButton: NSButton!
  102. @IBOutlet private weak var falseTypeButton: NSButton!
  103. @IBOutlet private weak var circletypeButton: NSButton!
  104. @IBOutlet private weak var lineTypeButton: NSButton!
  105. @IBOutlet private weak var dotTypeButton: NSButton!
  106. @IBOutlet private weak var typeViewTopConstant: NSLayoutConstraint!
  107. @IBOutlet private weak var dateView: NSView! // 日期
  108. @IBOutlet private weak var dateLabel: NSTextField!
  109. @IBOutlet private weak var datePopUpButton: KMPopUpButton!
  110. @IBOutlet private weak var dateCheckButton: NSButton!
  111. @IBOutlet private weak var dateViewTopConstant: NSLayoutConstraint!
  112. @IBOutlet private weak var iconView: NSView! // 图标
  113. @IBOutlet private weak var iconLabel: NSTextField!
  114. @IBOutlet private weak var commentBox: NSBox!
  115. @IBOutlet private weak var commentButton: NSButton!
  116. @IBOutlet private weak var noteBox: NSBox!
  117. @IBOutlet private weak var noteButton: NSButton!
  118. @IBOutlet private weak var keyBox: NSBox!
  119. @IBOutlet private weak var keyButton: NSButton!
  120. @IBOutlet private weak var helpBox: NSBox!
  121. @IBOutlet private weak var helpButton: NSButton!
  122. @IBOutlet private weak var paragraphBox: NSBox!
  123. @IBOutlet private weak var paragraphButton: NSButton!
  124. @IBOutlet private weak var insertBox: NSBox!
  125. @IBOutlet private weak var insertButton: NSButton!
  126. @IBOutlet private weak var addParagraphBox: NSBox!
  127. @IBOutlet private weak var addParagraphButton: NSButton!
  128. @IBOutlet private weak var iconViewTopConstant: NSLayoutConstraint!
  129. @IBOutlet private weak var iconButtonHeightConstant: NSLayoutConstraint!
  130. @IBOutlet private weak var borderAndLineView: NSView! // 边框
  131. @IBOutlet private weak var borderLabel: NSTextField!
  132. @IBOutlet private weak var borderButton: NSButton!
  133. @IBOutlet private weak var lineWidthComboBox: KMComboBox!
  134. @IBOutlet private weak var lineWidthSlider: NSSlider!
  135. @IBOutlet private weak var lineTypeLabel: NSTextField!
  136. @IBOutlet private weak var lineStyleButton: KMButton!
  137. @IBOutlet private weak var dottedLineStyleButton: KMButton!
  138. @IBOutlet private weak var rightArrowStyleButton: KMButton!
  139. @IBOutlet private weak var lineStyleBox: NSBox!
  140. @IBOutlet private weak var dottedLineStyleBox: NSBox!
  141. @IBOutlet private weak var rightArrowStyleBox: NSBox!
  142. @IBOutlet private weak var borderViewTopConstant: NSLayoutConstraint!
  143. @IBOutlet private weak var fillColorView: NSView!
  144. @IBOutlet private weak var fillColorLabel: NSTextField!
  145. @IBOutlet private weak var fillColorPickerView: KMColorPickerView!
  146. @IBOutlet private weak var fillColorViewTopConstraint: NSLayoutConstraint!
  147. @IBOutlet private weak var opacityView: NSView! // 透明度
  148. @IBOutlet private weak var opacityLabel: NSTextField!
  149. @IBOutlet private weak var opacitySlider: NSSlider!
  150. @IBOutlet private weak var opacityComboBox: KMComboBox!
  151. @IBOutlet private weak var opacityViewTopConstant: NSLayoutConstraint!
  152. @IBOutlet private weak var noteView: NSView! // 笔记
  153. @IBOutlet private weak var noteViewLabel: NSTextField!
  154. @IBOutlet private var noteTextView: NSTextView!
  155. @IBOutlet private weak var noteViewTopConstant: NSLayoutConstraint!
  156. @IBOutlet private weak var colorView: NSView! // 颜色
  157. @IBOutlet private weak var colorTitleLabel: NSTextField!
  158. @IBOutlet private weak var textColorBox: NSBox!
  159. @IBOutlet private weak var textColorBoxConstraint: NSLayoutConstraint!
  160. @IBOutlet private weak var textColorPickerView: KMColorPickerView!
  161. @IBOutlet private weak var borderColorBox: NSBox!
  162. @IBOutlet private weak var borderColorPickerView: KMColorPickerView!
  163. @IBOutlet private weak var borderColorBoxConstraint: NSLayoutConstraint!
  164. @IBOutlet private weak var lineWidthBox: NSBox!
  165. @IBOutlet private weak var widthLabel: NSTextField!
  166. @IBOutlet private weak var widthComboBox: KMComboBox!
  167. @IBOutlet private weak var widthSlider: NSSlider!
  168. @IBOutlet private weak var ColorViewTopConstant: NSLayoutConstraint!
  169. @IBOutlet private weak var textColorPickerViewConstant: NSLayoutConstraint!
  170. var isannotationMode: Bool = false
  171. var selectedAlignmentButtonLayer: CALayer?
  172. var iconButtonArray: [NSBox] = []
  173. var lineStyleCount: Int = 0
  174. var isTextEdit: Bool = false
  175. var fonts: [[String : [String]]]!
  176. var annotationAlignment: KMFreeTextAnnotationAlignmentType = .left
  177. var noteIcons: [NSImage?] = [nil, nil, nil, nil, nil, nil, nil]
  178. private func makeNoteIcons() {
  179. if noteIcons[0] != nil { return }
  180. let bounds = CGRect(origin: .zero, size: NSMakeSize(16, 16))
  181. let annotation = CPDFTextAnnotation()
  182. annotation.bounds = bounds
  183. annotation.color = NSColor.clear
  184. let page = CPDFPage()
  185. page.setBounds(bounds, for: .mediaBox)
  186. page.addAnnotation(annotation)
  187. for i in 0..<7 {
  188. annotation.setIconType(CPDFTextAnnotationIconType(rawValue: i) ?? .comment)
  189. noteIcons[i] = NSImage.bitmapImage(with: NSMakeSize(16, 16), drawingHandler: { rect in
  190. page.draw(with: .mediaBox, to: (NSGraphicsContext.current?.cgContext))
  191. })
  192. }
  193. }
  194. deinit {
  195. NotificationCenter.default.removeObserver(self)
  196. if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
  197. let keys = ["contents", "markupText"]
  198. for key in keys {
  199. annotationModel?.annotation.removeObserver(self, forKeyPath: key)
  200. }
  201. }
  202. textColorPickerView.target = nil
  203. textColorPickerView.action = nil
  204. borderColorPickerView.target = nil
  205. borderColorPickerView.action = nil
  206. fillColorPickerView.target = nil
  207. fillColorPickerView.action = nil
  208. NSColorPanel.shared.setTarget(nil)
  209. NSColorPanel.shared.setAction(nil)
  210. }
  211. // MARK: View Methods
  212. override func loadView() {
  213. super.loadView()
  214. DispatchQueue.main.async {
  215. if NSColorPanel.shared.isVisible {
  216. NSColorPanel.shared.close()
  217. }
  218. }
  219. isannotationMode = true
  220. fonts = (CPDFAnnotationModel.supportFonts() as! [[String : [String]]])
  221. imageBox.borderColor = KMAppearance.Interactive.s0Color()
  222. imageBox.borderWidth = 1.0
  223. imageBox.fillColor = KMAppearance.Layout.l1Color()
  224. textImageBox.fillColor = KMAppearance.Layout.l1Color()
  225. fontLabel.stringValue = NSLocalizedString("Fonts", comment: "")
  226. fontLabel.textColor = KMAppearance.Layout.h0Color()
  227. colorTitleLabel.stringValue = NSLocalizedString("Color", comment: "")
  228. colorTitleLabel.textColor = KMAppearance.Layout.h0Color()
  229. opacityLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  230. opacityLabel.textColor = KMAppearance.Layout.h1Color()
  231. noteViewLabel.stringValue = NSLocalizedString("Note", comment: "")
  232. noteViewLabel.textColor = KMAppearance.Layout.h0Color()
  233. rotateLabel.stringValue = NSLocalizedString("Rotate", comment: "")
  234. rotateLabel.textColor = KMAppearance.Layout.h0Color()
  235. typeLabel.stringValue = NSLocalizedString("Type", comment: "")
  236. typeLabel.textColor = KMAppearance.Layout.h0Color()
  237. dateLabel.stringValue = NSLocalizedString("Date", comment: "")
  238. dateLabel.textColor = KMAppearance.Layout.h0Color()
  239. iconLabel.stringValue = NSLocalizedString("Icon", comment: "")
  240. iconLabel.textColor = KMAppearance.Layout.h0Color()
  241. borderLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  242. borderLabel.textColor = KMAppearance.Layout.h0Color()
  243. fillColorLabel.stringValue = NSLocalizedString("Fill", comment: "")
  244. fillColorLabel.textColor = KMAppearance.Layout.h0Color()
  245. lineStyleCount = 0
  246. if annotationType == .highlight {
  247. activeAnnotationType = .highlight
  248. } else if annotationType == .strikeOut {
  249. activeAnnotationType = .strikeOut
  250. } else if annotationType == .underline {
  251. activeAnnotationType = .underline
  252. } else if annotationType == .ink || annotationType == .line || annotationType == .arrow {
  253. activeAnnotationType = .inkAndLine
  254. } else if annotationType == .anchored {
  255. activeAnnotationType = .text
  256. } else if annotationType == .circle || annotationType == .square {
  257. borderColorPickerView.isCallColorPanelAction = isannotationMode
  258. textColorPickerView.isCallColorPanelAction = isannotationMode
  259. activeAnnotationType = .squareAndCircle
  260. } else if annotationType == .freeText {
  261. if subType == .date {
  262. colorTitleLabel.stringValue = NSLocalizedString("Fill Color", comment: "")
  263. activeAnnotationType = .selfSignFreeText
  264. } else {
  265. activeAnnotationType = .freeText
  266. }
  267. fontViewCorloPV.isCallColorPanelAction = isannotationMode
  268. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  269. if annotationModel?.alignment() == .left {
  270. alignmentType = .left
  271. } else if annotationModel?.alignment() == .center {
  272. alignmentType = .center
  273. } else if annotationModel?.alignment() == .right {
  274. alignmentType = .right
  275. }
  276. alignmentTypeSelected(alignmentType)
  277. }
  278. textColorPickerView.isCallColorPanelAction = isannotationMode
  279. borderColorPickerView.isCallColorPanelAction = isannotationMode
  280. if annotationType == .freeText {
  281. fontViewCorloPV.isFreeText = true
  282. } else {
  283. fontViewCorloPV.isFreeText = false
  284. }
  285. if annotationType == .ink || annotationType == .line || annotationType == .arrow || annotationType == .square || annotationType == .circle {
  286. let lineType = annotationModel?.style()
  287. if lineType == .dashed {
  288. let lineType = annotationModel?.dashPattern()
  289. // if lineType.count == 0 {
  290. // let number = NSNumber(value: Float(4))
  291. // annotation.setDashPattern([number])
  292. //
  293. // let userDefaults = UserDefaults.standard
  294. // if annotationType == .ink {
  295. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKInkNoteDashPatternKey)
  296. // } else if annotationType == .line {
  297. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKLineNoteDashPatternKey)
  298. // } else if annotationType == .square {
  299. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKSquareNoteDashPatternKey)
  300. // } else if annotationType == .circle {
  301. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKCircleNoteDashPatternKey)
  302. // } else if annotationType == .freeText {
  303. // userDefaults.set(annotationModel?.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
  304. // }
  305. // }
  306. }
  307. }
  308. opacityComboBox.type = .none
  309. opacityComboBox.comboxRect = opacityComboBox.bounds
  310. lineWidthComboBox.type = .none
  311. lineWidthComboBox.comboxRect = lineWidthComboBox.bounds
  312. widthComboBox.type = .none
  313. widthComboBox.comboxRect = widthComboBox.bounds
  314. fontPopUpButton.type = .arrowDown
  315. fontSizeComboBox.type = .none
  316. fontSizeComboBox.comboxRect = fontSizeComboBox.bounds
  317. fontStylePopUpButton.type = .arrowUpDown
  318. datePopUpButton.type = .arrowUpDown
  319. noteTextView.backgroundColor = KMAppearance.Layout.l1Color()
  320. let views: [NSView] = [fontPopUpButton, fontSizeComboBox, fontStylePopUpButton, lineWidthComboBox, opacityComboBox, widthComboBox, datePopUpButton, noteTextView]
  321. for view in views {
  322. view.wantsLayer = true
  323. view.layer?.borderWidth = 1.0
  324. view.layer?.cornerRadius = 1.0
  325. }
  326. refreshLineTypeUI()
  327. rightArrowStyleButton.isHidden = true
  328. rightArrowStyleBox.isHidden = true
  329. annotationFontReload()
  330. reloadData()
  331. updateViewColor()
  332. if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
  333. let annotation = annotationModel?.annotation
  334. let keys = ["contents", "markupText"]
  335. for key in keys {
  336. annotation?.addObserver(self, forKeyPath: key, options: [.new, .old], context: nil)
  337. }
  338. }
  339. // NotificationCenter.default.addObserver(self, selector: #selector(alignmentTypeNotification(_:)), name: "KMAnnotationAlignmentTypeNotification1", object: nil)
  340. // NotificationCenter.default.addObserver(self, selector: #selector(loadingUIAndLocalization(_:)), name: "KMPreferenceDidChangeNotificationName", object: nil)
  341. // NotificationCenter.default.addObserver(self, selector: #selector(annotationChangeNotification(_:)), name: CPDFListViewAnnotationsAttributeHasChangeNotification, object: nil)
  342. }
  343. override func viewDidLoad() {
  344. super.viewDidLoad()
  345. // Do view setup here.
  346. }
  347. override func viewDidAppear() {
  348. super.viewDidAppear()
  349. let showConvertDetails = KMPropertiesViewPopController.showChangeColorDetails()
  350. let fileURL = (self.view.window?.windowController?.document as? NSDocument)?.fileURL
  351. if showConvertDetails && (fileURL != nil) {
  352. KMPropertiesViewPopController.defaultManager.showChangeColorDetailsView(self.textColorPickerView.firstButton)
  353. }
  354. }
  355. func reloadData() {
  356. var annotationColor = annotationModel?.color()
  357. if annotationType == .signFalse ||
  358. annotationType == .signTure ||
  359. annotationType == .signDot ||
  360. annotationType == .signCircle ||
  361. annotationType == .signLine {
  362. annotationColor = annotationModel?.color()
  363. } else if annotationType == .square ||
  364. annotationType == .circle {
  365. annotationColor = annotationModel?.color()
  366. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  367. annotationColor = annotationModel?.interiorColor()
  368. } else {
  369. annotationColor = annotationModel?.color()
  370. }
  371. let color = annotationColor ?? NSColor.black
  372. // guard let color = annotationColor else { return }
  373. var opacity: CGFloat = 1.0
  374. color.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green: nil, blue: nil, alpha: &opacity)
  375. if annotationType == .signFalse ||
  376. annotationType == .signTure ||
  377. annotationType == .signDot ||
  378. annotationType == .signCircle ||
  379. annotationType == .signLine {
  380. opacity = (annotationModel?.opacity())!
  381. } else if annotationType == .square ||
  382. annotationType == .circle {
  383. opacity = (annotationModel?.opacity())!
  384. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  385. opacity = (annotationModel?.interiorOpacity())!
  386. } else {
  387. opacity = (annotationModel?.opacity())!
  388. }
  389. opacitySlider.floatValue = Float(opacity)
  390. opacitySlider.toolTip = "\(Int(opacity * 100))%"
  391. opacityComboBox.stringValue = "\(Int(opacity * 100))%"
  392. if annotationModel?.annotations != nil {
  393. let contextString = annotation.string() ?? ""
  394. noteTextView.string = contextString
  395. noteTextView.textColor = NSColor.labelColor
  396. noteTextView.delegate = self
  397. }
  398. if annotationType == .freeText {
  399. imageBox.contentView = textImageBoxView
  400. textAnnotationImageView.image = annotationModel?.annotationImage
  401. } else if annotationType == .signText || annotationType == .signDate {
  402. imageBox.contentView = generalImageBoxView
  403. generalImageView.image = annotationModel?.annotationImage
  404. } else {
  405. imageBox.contentView = generalImageBoxView
  406. generalImageView.image = annotationModel?.annotationImage
  407. }
  408. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow || annotationType == .ink {
  409. if annotationType == .ink {
  410. textColorPickerView.setColor(color)
  411. } else if annotationType == .square || annotationType == .circle {
  412. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  413. let modelColor = annotationModel?.interiorColor() ?? .black
  414. modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  415. let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity())!)
  416. textColorPickerView.setColor(interiorColor)
  417. textColorPickerView.isFillColor = true
  418. borderColorPickerView.setColor(color)
  419. } else if annotationType == .line || annotationType == .arrow {
  420. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  421. let modelColor = annotationModel?.interiorColor() ?? .black
  422. modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  423. let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity())!)
  424. textColorPickerView.setColor(interiorColor)
  425. textColorPickerView.isFillColor = true
  426. borderColorPickerView.setColor(color)
  427. }
  428. lineWidthSlider.floatValue = Float(annotationModel?.lineWidth() ?? 1.0)
  429. lineWidthSlider.toolTip = "\(annotationModel?.lineWidth() ?? 1.0) pt"
  430. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  431. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  432. if annotationType == .signText || annotationType == .signDate {
  433. if subType == .date {
  434. dateView.isHidden = false
  435. datePopUpButton.removeAllItems()
  436. let freeText = annotation as! KMSelfSignAnnotationFreeText
  437. // self.datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: <#T##Bool#>))
  438. if freeText.dateFormatIndex >= 0, freeText.dateFormatIndex < datePopUpButton.numberOfItems {
  439. datePopUpButton.selectItem(at: freeText.dateFormatIndex)
  440. } else {
  441. datePopUpButton.selectItem(at: 0)
  442. }
  443. dateCheckButton.state = freeText.includeTime ? NSControl.StateValue.on : NSControl.StateValue.off
  444. textColorPickerView.setColor(annotation.color)
  445. }
  446. } else {
  447. lineWidthSlider.minValue = 0.0
  448. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  449. lineWidthSlider.toolTip = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  450. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  451. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  452. if annotationModel?.alignment() == .left {
  453. alignmentType = .left
  454. } else if annotationModel?.alignment() == .center {
  455. alignmentType = .center
  456. } else if annotationModel?.alignment() == .right {
  457. alignmentType = .right
  458. }
  459. alignmentTypeSelected(alignmentType)
  460. annotationFontReload()
  461. fillColorPickerView.color = annotationModel?.color()
  462. }
  463. fontViewCorloPV.color = annotationModel?.fontColor()
  464. fontSizeComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.fontSize())!)
  465. } else if annotationType == .highlight || annotationType == .strikeOut || annotationType == .underline || annotationType == .anchored {
  466. textColorPickerView.setColor(color)
  467. }
  468. noteView.isHidden = isannotationMode
  469. if annotationType == .signText || annotationType == .signDate {
  470. if subType == .date {
  471. noteView.isHidden = true
  472. } else {
  473. noteView.isHidden = false
  474. }
  475. }
  476. if annotationModel?.annotations != nil {
  477. noteView.isHidden = annotationModel?.annotation==nil
  478. } else {
  479. if annotationType == .stamp ||
  480. annotationType == .ink ||
  481. annotationType == .square ||
  482. annotationType == .circle ||
  483. annotationType == .arrow ||
  484. annotationType == .line ||
  485. annotationType == .highlight ||
  486. annotationType == .strikeOut ||
  487. annotationType == .underline ||
  488. annotationType == .freeText {
  489. noteView.isHidden = true
  490. } else {
  491. noteView.isHidden = annotationModel?.annotation==nil
  492. }
  493. }
  494. }
  495. // MARK: Set & Get
  496. var annotations: [CPDFAnnotation] {
  497. get {
  498. return annotationModel?.annotations as! [CPDFAnnotation]
  499. }
  500. }
  501. var annotation: CPDFAnnotation {
  502. get {
  503. return (annotationModel?.annotation)!
  504. }
  505. }
  506. var annotationType: CAnnotationType {
  507. get {
  508. return (annotationModel?.annotationType as? CAnnotationType)!
  509. }
  510. }
  511. func updateannotationMode() {
  512. let defaults = UserDefaults.standard
  513. if annotationType == .freeText {
  514. // if ([annotation isKindOfClass:[SKNPDFAnnotationNote class]]) {
  515. // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
  516. // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
  517. // }
  518. if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  519. defaults.safe_setColor(annotation.color, forKey: CFreeTextNoteColorKey)
  520. defaults.safe_setColor((annotation as! CPDFFreeTextAnnotation).fontColor, forKey: CFreeTextNoteFontColorKey)
  521. defaults.set((annotation as! CPDFFreeTextAnnotation).alignment.rawValue, forKey: CFreeTextNoteAlignmentKey)
  522. defaults.set((annotation as! CPDFFreeTextAnnotation).fontName(), forKey: CFreeTextNoteFontNameKey)
  523. defaults.set((annotation as! CPDFFreeTextAnnotation).fontSize(), forKey: CFreeTextNoteFontSizeKey)
  524. defaults.set(annotation.lineWidth(), forKey: CFreeTextNoteLineWidthKey)
  525. defaults.set(annotation.borderStyle().rawValue, forKey: CFreeTextNoteLineStyleKey)
  526. defaults.set(annotation.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
  527. }
  528. } else if annotationType == .circle {
  529. defaults.safe_setColor(annotation.color, forKey: CCircleNoteColorKey)
  530. defaults.safe_setColor((annotation as! CPDFCircleAnnotation).interiorColor, forKey: CCircleNoteInteriorColorKey)
  531. defaults.set(annotation.lineWidth(), forKey: CCircleNoteLineWidthKey)
  532. defaults.set((annotation as! CPDFCircleAnnotation).borderStyle().rawValue, forKey: CCircleNoteLineStyleKey)
  533. defaults.set((annotation as! CPDFCircleAnnotation).dashPattern(), forKey: CCircleNoteDashPatternKey)
  534. } else if annotationType == .square {
  535. defaults.safe_setColor(annotation.color, forKey: CSquareNoteColorKey)
  536. defaults.safe_setColor((annotation as! CPDFSquareAnnotation).interiorColor, forKey: CSquareNoteInteriorColorKey)
  537. defaults.set(annotation.lineWidth(), forKey: CSquareNoteLineWidthKey)
  538. defaults.set((annotation as! CPDFSquareAnnotation).borderStyle().rawValue, forKey: CSquareNoteLineStyleKey)
  539. defaults.set((annotation as! CPDFSquareAnnotation).dashPattern(), forKey: CSquareNoteDashPatternKey)
  540. } else if annotationType == .highlight {
  541. defaults.safe_setColor(annotation.color, forKey: CHighlightNoteColorKey)
  542. } else if annotationType == .underline {
  543. defaults.safe_setColor(annotation.color, forKey: CUnderlineNoteColorKey)
  544. } else if annotationType == .strikeOut {
  545. defaults.safe_setColor(annotation.color, forKey: CStrikeOutNoteColorKey)
  546. } else if annotationType == .line || annotationType == .arrow {
  547. defaults.safe_setColor(annotation.color, forKey: CLineNoteColorKey)
  548. defaults.set(annotation.lineWidth(), forKey: CLineNoteLineWidthKey)
  549. defaults.set(annotation.borderStyle().rawValue, forKey: CLineNoteLineStyleKey)
  550. defaults.set(annotation.dashPattern(), forKey: CLineNoteDashPatternKey)
  551. defaults.safe_setColor((annotation as! CPDFLineAnnotation).interiorColor, forKey: CLineNoteInteriorColorKey)
  552. defaults.set((annotation as! CPDFLineAnnotation).startLineStyle.rawValue, forKey: CLineNoteStartLineStyleKey)
  553. defaults.set((annotation as! CPDFLineAnnotation).endLineStyle.rawValue, forKey: CLineNoteEndLineStyleKey)
  554. } else if annotationType == .ink {
  555. defaults.safe_setColor(annotation.color, forKey: CInkNoteColorKey)
  556. defaults.set(annotation.lineWidth(), forKey: CInkNoteLineWidthKey)
  557. defaults.set(annotation.borderStyle().rawValue, forKey: CInkNoteLineStyleKey)
  558. defaults.set(annotation.dashPattern(), forKey: CInkNoteDashPatternKey)
  559. } else if annotationType == .anchored {
  560. // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
  561. // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
  562. }
  563. }
  564. var activeAnnotationType: KMActiveAnnotationType {
  565. set {
  566. _activeAnnotationType = newValue
  567. switch self.activeAnnotationType {
  568. case .highlight, .strikeOut, .underline:
  569. self.fontView.isHidden = true
  570. self.borderAndLineView.isHidden = true
  571. self.fillColorView.isHidden = true
  572. self.rotateView.isHidden = true
  573. self.typeView.isHidden = true
  574. self.dateView.isHidden = true
  575. self.iconView.isHidden = true
  576. self.createMarkupProperties()
  577. case .inkAndLine:
  578. self.fontView.isHidden = true
  579. self.fillColorView.isHidden = true
  580. self.rotateView.isHidden = true
  581. self.typeView.isHidden = true
  582. self.dateView.isHidden = true
  583. self.iconView.isHidden = true
  584. self.createInkAndLineProperties()
  585. case .text:
  586. self.fontView.isHidden = true
  587. self.borderAndLineView.isHidden = true
  588. self.fillColorView.isHidden = true
  589. self.rotateView.isHidden = true
  590. self.typeView.isHidden = true
  591. self.dateView.isHidden = true
  592. self.createTextProperties()
  593. case .squareAndCircle:
  594. self.fontView.isHidden = true
  595. self.rotateView.isHidden = true
  596. self.typeView.isHidden = true
  597. self.dateView.isHidden = true
  598. self.iconView.isHidden = true
  599. self.fillColorView.isHidden = true
  600. self.createSquareAndCircleProperties()
  601. case .freeText:
  602. self.colorView.isHidden = true
  603. self.rotateView.isHidden = true
  604. self.typeView.isHidden = true
  605. self.dateView.isHidden = true
  606. self.iconView.isHidden = true
  607. self.createFreeTextProperties()
  608. case .selfSignFreeText:
  609. self.rotateView.isHidden = true
  610. self.borderAndLineView.isHidden = true
  611. self.fillColorView.isHidden = true
  612. self.typeView.isHidden = true
  613. self.iconView.isHidden = true
  614. self.createSelfSignFreeTextProperties()
  615. default:
  616. break
  617. }
  618. }
  619. get {
  620. return _activeAnnotationType
  621. }
  622. }
  623. // MARK: private
  624. func updateViewColor() {
  625. if KMAppearance.isDarkMode() {
  626. let darkColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  627. let borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  628. setViewColor(fontPopUpButton, darkColor, borderColor)
  629. setViewColor(fontSizeComboBox, darkColor, borderColor)
  630. setViewColor(fontStylePopUpButton, darkColor, borderColor)
  631. setViewColor(lineWidthComboBox, darkColor, borderColor)
  632. setViewColor(opacityComboBox, darkColor, borderColor)
  633. setViewColor(datePopUpButton, darkColor, borderColor)
  634. setViewColor(widthComboBox, darkColor, borderColor)
  635. setViewColor(noteTextView, darkColor, borderColor)
  636. } else {
  637. let lightColor = NSColor.white.cgColor
  638. let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  639. setViewColor(fontPopUpButton, lightColor, borderColor)
  640. setViewColor(fontSizeComboBox, lightColor, borderColor)
  641. setViewColor(fontStylePopUpButton, lightColor, borderColor)
  642. setViewColor(lineWidthComboBox, lightColor, borderColor)
  643. setViewColor(opacityComboBox, lightColor, borderColor)
  644. setViewColor(datePopUpButton, lightColor, borderColor)
  645. setViewColor(widthComboBox, lightColor, borderColor)
  646. setViewColor(noteTextView, lightColor, borderColor)
  647. }
  648. }
  649. func setViewColor(_ view: NSView, _ backgroundColor: CGColor, _ borderColor: CGColor) {
  650. view.layer?.backgroundColor = backgroundColor
  651. view.layer?.borderColor = borderColor
  652. }
  653. func refreshLineTypeUI() {
  654. lineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  655. dottedLineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  656. rightArrowStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  657. lineStyleBox.fillColor = KMAppearance.Layout.l1Color()
  658. dottedLineStyleBox.fillColor = KMAppearance.Layout.l1Color()
  659. rightArrowStyleBox.fillColor = KMAppearance.Layout.l1Color()
  660. lineStyleBox.borderWidth = 1
  661. dottedLineStyleBox.borderWidth = 1
  662. rightArrowStyleBox.borderWidth = 1
  663. if lineStyleCount == 0 {
  664. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  665. lineStyleBox.borderWidth = 0.0
  666. } else if lineStyleCount == 1 {
  667. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  668. dottedLineStyleBox.borderWidth = 0
  669. } else if lineStyleCount == 2 {
  670. rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
  671. rightArrowStyleBox.borderWidth = 0
  672. }
  673. }
  674. func hiddenSubviews() {
  675. fontViewTopConstant.constant = fontView.isHidden ? -(fontView.bounds.size.height) - 20 : 10
  676. ColorViewTopConstant.constant = colorView.isHidden ? -(colorView.bounds.size.height) : 10
  677. borderViewTopConstant.constant = borderAndLineView.isHidden ? -(borderAndLineView.bounds.size.height) : 20
  678. opacityViewTopConstant.constant = opacityView.isHidden ? -opacityView.bounds.size.height : 20
  679. rotateViewTopConstant.constant = rotateView.isHidden ? -(rotateView.bounds.size.height) : 20
  680. typeViewTopConstant.constant = typeView.isHidden ? -(typeView.bounds.size.height) : 20
  681. dateViewTopConstant.constant = dateView.isHidden ? -(dateView.bounds.size.height) : 20
  682. iconViewTopConstant.constant = iconView.isHidden ? -(iconView.bounds.size.height) : 20
  683. noteViewTopConstant.constant = noteView.isHidden ? -(noteView.bounds.size.height) : 20
  684. fillColorViewTopConstraint.constant = fillColorView.isHidden ? -fillColorView.bounds.size.height : 20
  685. fontViewColorPVConstraint.constant = fontViewCorloPV.isHidden ? -fontViewCorloPV.bounds.size.height : 10
  686. }
  687. func setActiveAnnotationType(_ activeAnnotationType: KMActiveAnnotationType) {
  688. self.activeAnnotationType = activeAnnotationType
  689. switch activeAnnotationType {
  690. case .highlight, .strikeOut, .underline:
  691. fontView.isHidden = true
  692. borderAndLineView.isHidden = true
  693. fillColorView.isHidden = true
  694. rotateView.isHidden = true
  695. typeView.isHidden = true
  696. dateView.isHidden = true
  697. iconView.isHidden = true
  698. createMarkupProperties()
  699. case .inkAndLine:
  700. fontView.isHidden = true
  701. fillColorView.isHidden = true
  702. rotateView.isHidden = true
  703. typeView.isHidden = true
  704. dateView.isHidden = true
  705. iconView.isHidden = true
  706. createInkAndLineProperties()
  707. case .text:
  708. fontView.isHidden = true
  709. borderAndLineView.isHidden = true
  710. fillColorView.isHidden = true
  711. rotateView.isHidden = true
  712. typeView.isHidden = true
  713. dateView.isHidden = true
  714. createTextProperties()
  715. case .squareAndCircle:
  716. fontView.isHidden = true
  717. rotateView.isHidden = true
  718. typeView.isHidden = true
  719. dateView.isHidden = true
  720. iconView.isHidden = true
  721. fillColorView.isHidden = true
  722. createSquareAndCircleProperties()
  723. case .freeText:
  724. colorView.isHidden = true
  725. rotateView.isHidden = true
  726. typeView.isHidden = true
  727. dateView.isHidden = true
  728. iconView.isHidden = true
  729. createFreeTextProperties()
  730. case .selfSignFreeText:
  731. rotateView.isHidden = true
  732. borderAndLineView.isHidden = true
  733. fillColorView.isHidden = true
  734. typeView.isHidden = true
  735. iconView.isHidden = true
  736. createSelfSignFreeTextProperties()
  737. default:
  738. break
  739. }
  740. }
  741. func createMarkupProperties() {
  742. let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
  743. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  744. textColorBoxConstraint.constant = 10
  745. borderColorBoxConstraint.constant = 0
  746. textColorBox.isHidden = true
  747. lineWidthBox.isHidden = true
  748. imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
  749. hiddenSubviews()
  750. textColorPickerView.noContentString = true
  751. if activeAnnotationType == .highlight {
  752. textColorPickerView.annotationTypeString = NSLocalizedString("Highlight", comment: "Description for export")
  753. } else if activeAnnotationType == .strikeOut {
  754. textColorPickerView.annotationTypeString = NSLocalizedString("Strikethrough", comment: "Description for export")
  755. } else if activeAnnotationType == .underline {
  756. textColorPickerView.annotationTypeString = NSLocalizedString("Underline", comment: "Description for export")
  757. }
  758. textColorPickerView.annotationType = (activeAnnotationType == .highlight) ? .markupHighlightColors : .markupOtherColors
  759. }
  760. func openFreeTextStylesViewController() {
  761. // let vc = KMFreeTextStylesViewController()
  762. // vc.annotations = self.annotations
  763. // vc.view.frame = CGRect(x: 0, y: 0, width: 280, height: CGRectGetHeight(self.view.window?.frame ?? CGRect.zero) - 300)
  764. //
  765. // let createFilePopover = NSPopover()
  766. // createFilePopover.contentViewController = vc
  767. // createFilePopover.animates = true
  768. // createFilePopover.behavior = .transient
  769. // createFilePopover.show(relativeTo: CGRect(x: _textImageBox.bounds.origin.x, y: 10, width: _textImageBox.bounds.size.width, height: _textImageBox.bounds.size.height), of: _textImageBox, preferredEdge: .minY)
  770. //
  771. // // Assuming _textImageBox is an NSView instance
  772. // createFilePopover.show(relativeTo: _textImageBox.bounds, of: _textImageBox, preferredEdge: .minY)
  773. // Release is not needed in Swift due to automatic reference counting (ARC)
  774. }
  775. func changeStoredFontInfo(_ sender: Any) {
  776. // if let senderFont = sender as? NSFont {
  777. // for tAnnotation in self.annotations {
  778. // tAnnotation.removeAllAppearanceStreams()
  779. // let font = senderFont.convert(tAnnotation.font)
  780. // if let validFont = font {
  781. // tAnnotation.font = validFont
  782. // }
  783. // }
  784. // }
  785. }
  786. private func updateAnnotation() {
  787. if annotationModel?.annotation != nil {
  788. if annotation is CPDFFreeTextAnnotation {
  789. let textNote = (annotation as! CPDFFreeTextAnnotation)
  790. if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  791. // pdfView?.commitEditAnnotationFreeText(textNote)
  792. pdfView?.editAnnotationFreeText(textNote)
  793. }
  794. pdfView?.setNeedsDisplay(pdfView?.activeAnnotation)
  795. }
  796. for tAnnotation in annotations {
  797. if tAnnotation is CPDFStampAnnotation {
  798. (tAnnotation as? KMSelfSignAnnotation)?.updateAppearanceStream()
  799. }
  800. if tAnnotation is CPDFMarkupAnnotation {
  801. pdfView?.setNeedsDisplayFor(tAnnotation.page)
  802. } else {
  803. pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
  804. }
  805. }
  806. }
  807. if annotationType == .freeText {
  808. textAnnotationImageView.image = annotationModel?.annotationImage
  809. } else {
  810. generalImageView.image = annotationModel?.annotationImage
  811. }
  812. }
  813. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  814. if self.annotation.isEqual(to: object) == false {
  815. return
  816. }
  817. let newValue = change?[NSKeyValueChangeKey.newKey] as? NSObject
  818. let oldValue = change?[NSKeyValueChangeKey.oldKey]
  819. if let data = newValue?.isEqual(to: oldValue), data {
  820. return
  821. }
  822. if keyPath == "string" {
  823. if newValue == nil || newValue is NSNull {
  824. return
  825. }
  826. //处理编辑时光标后移
  827. if self.isTextEdit == false {
  828. self.noteTextView.string = newValue as? String ?? ""
  829. }
  830. self.isTextEdit = false
  831. } else {
  832. self.reloadData()
  833. if self.isannotationMode {
  834. self.updateannotationMode()
  835. }
  836. }
  837. }
  838. // MARK: Line Note
  839. func createInkAndLineProperties() {
  840. if annotationType == .ink {
  841. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
  842. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  843. textColorBoxConstraint.constant = 10
  844. borderColorBoxConstraint.constant = 0
  845. textColorBox.isHidden = true
  846. lineWidthBox.isHidden = true
  847. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  848. hiddenSubviews()
  849. textColorPickerView.noContentString = true
  850. textColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  851. textColorPickerView.annotationType = .inkColors
  852. } else if annotationType == .line || annotationType == .arrow {
  853. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
  854. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  855. textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
  856. borderColorBoxConstraint.constant = 0
  857. lineWidthBox.isHidden = true
  858. textColorPickerViewConstant.constant = 57
  859. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  860. hiddenSubviews()
  861. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  862. textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
  863. borderColorPickerView.annotationType = .lineColors
  864. textColorPickerView.annotationType = .lineFillColors
  865. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  866. borderColorPickerView.annotationType = .lineColors
  867. }
  868. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  869. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  870. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  871. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  872. lineStyleButton.wantsLayer = true
  873. dottedLineStyleButton.wantsLayer = true
  874. rightArrowStyleButton.wantsLayer = true
  875. lineStyleButton.image = borderStyleSolid(false)
  876. dottedLineStyleButton.image = borderStyleDashed(false)
  877. rightArrowStyleButton.image = lineStyleRightArrow(false)
  878. if annotationModel?.style() == .solid {
  879. lineStyleCount = 0
  880. lineStyleButton.image = borderStyleSolid(true)
  881. lineStyleBox.borderWidth = 0
  882. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  883. } else if annotationModel?.style() == .dashed {
  884. lineStyleCount = 1
  885. dottedLineStyleButton.image = borderStyleDashed(true)
  886. dottedLineStyleBox.borderWidth = 0
  887. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  888. }
  889. if (annotationType == .line || annotationType == .arrow) && annotationModel?.endLineStyle() == .closedArrow {
  890. lineStyleCount = 2
  891. rightArrowStyleButton.image = lineStyleRightArrow(true)
  892. rightArrowStyleBox.borderWidth = 0
  893. rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
  894. }
  895. }
  896. func createTextProperties() {
  897. makeNoteIcons()
  898. let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
  899. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  900. textColorBoxConstraint.constant = 10
  901. borderColorBoxConstraint.constant = 0
  902. textColorBox.isHidden = true
  903. lineWidthBox.isHidden = true
  904. imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
  905. hiddenSubviews()
  906. textColorPickerView.annotationTypeString = NSLocalizedString("Anchored Note", comment: "Description for export")
  907. commentButton.title = NSLocalizedString("Comment", comment: "")
  908. commentButton.toolTip = NSLocalizedString("Comment", comment: "")
  909. commentButton.setTitleColor(KMAppearance.Layout.h0Color())
  910. paragraphButton.title = NSLocalizedString("Paragraph", comment: "")
  911. paragraphButton.toolTip = NSLocalizedString("Paragraph", comment: "")
  912. paragraphButton.setTitleColor(KMAppearance.Layout.h0Color())
  913. insertButton.title = NSLocalizedString("Insert", comment: "")
  914. insertButton.toolTip = NSLocalizedString("Insert", comment: "")
  915. insertButton.setTitleColor(KMAppearance.Layout.h0Color())
  916. addParagraphButton.title = NSLocalizedString("New Paragraph", comment: "")
  917. addParagraphButton.toolTip = NSLocalizedString("New Paragraph", comment: "")
  918. addParagraphButton.setTitleColor(KMAppearance.Layout.h0Color())
  919. keyButton.title = NSLocalizedString("Key", comment: "")
  920. keyButton.toolTip = NSLocalizedString("Key", comment: "")
  921. keyButton.setTitleColor(KMAppearance.Layout.h0Color())
  922. noteButton.title = NSLocalizedString("Note", comment: "")
  923. noteButton.toolTip = NSLocalizedString("Note", comment: "")
  924. noteButton.setTitleColor(KMAppearance.Layout.h0Color())
  925. helpButton.title = NSLocalizedString("Help", comment: "")
  926. helpButton.toolTip = NSLocalizedString("Help", comment: "")
  927. helpButton.setTitleColor(KMAppearance.Layout.h0Color())
  928. let languages = NSLocale.preferredLanguages
  929. var currentLocaleLanguageCode = "en"
  930. if let firstLanguage = languages.first {
  931. currentLocaleLanguageCode = firstLanguage
  932. if currentLocaleLanguageCode.hasPrefix("zh") {
  933. currentLocaleLanguageCode = "zh"
  934. }
  935. }
  936. let buttonArray: [NSButton] = [commentButton, keyButton, noteButton, helpButton, addParagraphButton, paragraphButton, insertButton]
  937. for button in buttonArray {
  938. button.wantsLayer = true
  939. button.layer?.cornerRadius = 6.0
  940. if currentLocaleLanguageCode == "zh" {
  941. iconButtonHeightConstant.constant = 56.0
  942. button.imagePosition = .imageAbove
  943. } else {
  944. iconButtonHeightConstant.constant = 50.0
  945. button.imagePosition = .imageOnly
  946. }
  947. }
  948. iconButtonArray = [commentBox, keyBox, noteBox, helpBox, addParagraphBox, paragraphBox, insertBox]
  949. let iconType = UInt((annotationModel?.anchoredIconType().rawValue)!)
  950. clickAnnotationTextIconButtonAction(iconType: iconType, boxArr: iconButtonArray)
  951. textColorPickerView.annotationType = .anchoredNoteColors
  952. textColorPickerView.isCallColorPanelAction = true
  953. }
  954. func clickAnnotationTextIconButtonAction(iconType: UInt, boxArr buttonArr: [NSBox]) {
  955. for i in 0..<buttonArr.count {
  956. if iconType == 0 {
  957. commentButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  958. commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationSel)
  959. } else {
  960. commentButton.layer?.backgroundColor = NSColor.clear.cgColor
  961. commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationNor)
  962. }
  963. if iconType == 1 {
  964. keyButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  965. keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordSel)
  966. } else {
  967. keyButton.layer?.backgroundColor = NSColor.clear.cgColor
  968. keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordNor)
  969. }
  970. if iconType == 2 {
  971. noteButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  972. noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesSel)
  973. } else {
  974. noteButton.layer?.backgroundColor = NSColor.clear.cgColor
  975. noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesNor)
  976. }
  977. if iconType == 3 {
  978. helpButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  979. helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpSel)
  980. } else {
  981. helpButton.layer?.backgroundColor = NSColor.clear.cgColor
  982. helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpNor)
  983. }
  984. if iconType == 4 {
  985. addParagraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  986. addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphSel)
  987. } else {
  988. addParagraphButton.layer?.backgroundColor = NSColor.clear.cgColor
  989. addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphNor)
  990. }
  991. if iconType == 5 {
  992. paragraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  993. paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphSel)
  994. } else {
  995. paragraphButton.layer?.backgroundColor = NSColor.clear.cgColor
  996. paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphNor)
  997. }
  998. if iconType == 6 {
  999. insertButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  1000. insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertSel)
  1001. } else {
  1002. insertButton.layer?.backgroundColor = NSColor.clear.cgColor
  1003. insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertNor)
  1004. }
  1005. }
  1006. }
  1007. func borderStyleSolid(_ isSelect: Bool) -> NSImage {
  1008. let size = NSSize(width: 49.0, height: 12.0)
  1009. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  1010. let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
  1011. path.lineWidth = 2.0
  1012. isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
  1013. path.stroke()
  1014. return true
  1015. })
  1016. return image
  1017. }
  1018. func borderStyleDashed(_ isSelect: Bool) -> NSImage {
  1019. let size = NSSize(width: 49.0, height: 12.0)
  1020. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  1021. let path = NSBezierPath()
  1022. path.move(to: NSPoint(x: 15.0, y: 5.0))
  1023. path.line(to: NSPoint(x: 19.0, y: 5.0))
  1024. path.move(to: NSPoint(x: 23.0, y: 5.0))
  1025. path.line(to: NSPoint(x: 27.0, y: 5.0))
  1026. path.move(to: NSPoint(x: 31.0, y: 5.0))
  1027. path.line(to: NSPoint(x: 35.0, y: 5.0))
  1028. path.lineWidth = 2.0
  1029. isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
  1030. path.stroke()
  1031. return true
  1032. })
  1033. return image
  1034. }
  1035. func lineStyleRightArrow(_ isSelect: Bool) -> NSImage {
  1036. let size = NSSize(width: 49.0, height: 12.0)
  1037. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  1038. let path = NSBezierPath()
  1039. path.move(to: NSPoint(x: 6.0, y: 6.0))
  1040. path.line(to: NSPoint(x: 43.0, y: 6.0))
  1041. path.move(to: NSPoint(x: 32.0, y: 3.0))
  1042. path.line(to: NSPoint(x: 43.0, y: 6.0))
  1043. path.line(to: NSPoint(x: 32.0, y: 9.0))
  1044. path.close()
  1045. path.lineWidth = 2.0
  1046. if isSelect {
  1047. KMAppearance.Layout.l0Color().setStroke()
  1048. } else {
  1049. KMAppearance.Interactive.s0Color().setStroke()
  1050. }
  1051. path.stroke()
  1052. return true
  1053. })
  1054. return image
  1055. }
  1056. // MARK: SKSquareNote、SKCircleNote、SKArrowNote Properties
  1057. func createSquareAndCircleProperties() {
  1058. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
  1059. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  1060. textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
  1061. borderColorBoxConstraint.constant = 0
  1062. lineWidthBox.isHidden = true
  1063. textColorPickerViewConstant.constant = 57
  1064. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  1065. hiddenSubviews()
  1066. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  1067. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  1068. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1069. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  1070. textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
  1071. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  1072. textColorPickerView.annotationType = .lineFillColors
  1073. borderColorPickerView.annotationType = .lineColors
  1074. lineStyleButton.wantsLayer = true
  1075. dottedLineStyleButton.wantsLayer = true
  1076. rightArrowStyleButton.wantsLayer = true
  1077. lineStyleButton.image = borderStyleSolid(false)
  1078. dottedLineStyleButton.image = borderStyleDashed(false)
  1079. if annotationModel?.annotation != nil {
  1080. if annotationModel?.annotation.borderStyle() == .solid {
  1081. lineStyleCount = 0
  1082. lineStyleButton.image = borderStyleSolid(true)
  1083. lineStyleBox.borderWidth = 0
  1084. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1085. } else if annotationModel?.annotation.borderStyle() == .dashed {
  1086. lineStyleCount = 1
  1087. dottedLineStyleButton.image = borderStyleDashed(true)
  1088. dottedLineStyleBox.borderWidth = 1
  1089. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1090. }
  1091. } else {
  1092. if annotationModel?.style() == .solid {
  1093. lineStyleCount = 0
  1094. lineStyleButton.image = borderStyleSolid(true)
  1095. lineStyleBox.borderWidth = 0
  1096. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1097. } else if annotationModel?.style() == .dashed {
  1098. lineStyleCount = 1
  1099. dottedLineStyleButton.image = borderStyleDashed(true)
  1100. dottedLineStyleBox.borderWidth = 1
  1101. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1102. }
  1103. }
  1104. }
  1105. // MARK: SKFreeTextNote、PDFAnnotationFreeText Properties
  1106. func createFreeTextProperties() {
  1107. imageBox.fillColor = KMAppearance.Layout.w0Color()
  1108. textColorBox.isHidden = true
  1109. lineWidthBox.isHidden = true
  1110. imageBoxLayoutConstraint.constant = CGRectGetHeight(textImageBoxView.frame)
  1111. textImageBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1112. if downEntered {
  1113. self?.openFreeTextStylesViewController()
  1114. }
  1115. }
  1116. textImageUpDateButton.wantsLayer = true
  1117. textImageBoxButton.image = NSImage(named: KMImageNameUXIconBtnTriDownNor)
  1118. textImageBoxButton.isEnabled = true
  1119. textImageUpDateButton.title = NSLocalizedString("Click to refresh", comment: "")
  1120. textImageUpDateButton.setTitleColor(KMAppearance.Layout.w0Color())
  1121. textImageUpDateButton.layer?.cornerRadius = 1.0
  1122. textImageUpDateButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  1123. textImageUpDateButton.isHidden = true
  1124. hiddenSubviews()
  1125. fontViewCorloPV.noContentString = true
  1126. fontViewCorloPV.annotationTypeString = NSLocalizedString("Color", comment: "")
  1127. fillColorPickerView.noContentString = true
  1128. fillColorPickerView.annotationTypeString = NSLocalizedString("Color", comment: "")
  1129. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  1130. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  1131. leftAlignButton.toolTip = NSLocalizedString("Left Alignment", comment: "")
  1132. centerAlignButton.toolTip = NSLocalizedString("Center", comment: "")
  1133. rightAlignButton.toolTip = NSLocalizedString("Right Alignment", comment: "")
  1134. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1135. lineWidthComboBox.stringValue = String(format: "%0.1f pt", Float((annotationModel?.lineWidth())!))
  1136. leftRotateBox.borderColor = KMAppearance.Interactive.s0Color()
  1137. leftRotateBox.borderWidth = 0.5
  1138. leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
  1139. leftRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1140. if downEntered {
  1141. self?.leftRotateBox.fillColor = KMAppearance.Status.selColor()
  1142. self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwisePre)
  1143. } else {
  1144. self?.leftRotateBox.fillColor = KMAppearance.Layout.w0Color()
  1145. self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
  1146. }
  1147. }
  1148. rightRotateBox.borderColor = KMAppearance.Interactive.s0Color()
  1149. rightRotateBox.borderWidth = 0.5
  1150. rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
  1151. rightRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1152. if downEntered {
  1153. self?.rightRotateBox.fillColor = KMAppearance.Status.selColor()
  1154. self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwisePre)
  1155. } else {
  1156. self?.rightRotateBox.fillColor = KMAppearance.Layout.w0Color()
  1157. self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
  1158. }
  1159. }
  1160. lineStyleButton.wantsLayer = true
  1161. dottedLineStyleButton.wantsLayer = true
  1162. rightArrowStyleButton.wantsLayer = true
  1163. lineStyleButton.image = borderStyleSolid(false)
  1164. dottedLineStyleButton.image = borderStyleDashed(false)
  1165. if annotationModel?.style() == .solid {
  1166. lineStyleCount = 0
  1167. lineStyleButton.image = borderStyleSolid(true)
  1168. lineStyleBox.borderWidth = 0
  1169. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1170. lineStyleBox.borderWidth = 0
  1171. } else if annotationModel?.style() == .dashed {
  1172. lineStyleCount = 1
  1173. dottedLineStyleButton.image = borderStyleDashed(true)
  1174. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1175. dottedLineStyleBox.borderWidth = 0
  1176. } else {
  1177. lineStyleCount = 0
  1178. lineStyleButton.image = borderStyleSolid(true)
  1179. lineStyleBox.borderWidth = 0
  1180. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1181. lineStyleBox.borderWidth = 0
  1182. }
  1183. fontViewCorloPV.annotationType = .freeTextColors
  1184. fillColorPickerView.annotationType = .freeTextFillColors
  1185. }
  1186. func alignmentTypeSelected(_ type: KMFreeTextAnnotationAlignmentType) {
  1187. annotationAlignment = type
  1188. selectedAlignmentButtonLayer?.removeFromSuperlayer()
  1189. selectedAlignmentButtonLayer = CALayer()
  1190. selectedAlignmentButtonLayer?.cornerRadius = 6.0
  1191. leftAlignButton.wantsLayer = true
  1192. centerAlignButton.wantsLayer = true
  1193. rightAlignButton.wantsLayer = true
  1194. selectedAlignmentButtonLayer?.bounds = leftAlignButton.layer?.bounds ?? CGRect.zero
  1195. selectedAlignmentButtonLayer?.anchorPoint = CGPoint(x: 0, y: 0)
  1196. var color = NSColor(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 0.16)
  1197. if #available(macOS 10.14, *) {
  1198. let appearanceName = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua])
  1199. if appearanceName == .darkAqua {
  1200. color = NSColor(red: 34/255.0, green: 122/255.0, blue: 255/255.0, alpha: 0.3)
  1201. }
  1202. }
  1203. selectedAlignmentButtonLayer?.backgroundColor = color.cgColor
  1204. leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftNor)
  1205. centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterNor)
  1206. rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightNor)
  1207. if type == .left {
  1208. leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftSel)
  1209. } else if type == .center {
  1210. centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterSel)
  1211. } else if type == .right {
  1212. rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightSel)
  1213. }
  1214. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { [weak self] in
  1215. guard let self = self else { return }
  1216. if type == .left {
  1217. self.leftAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1218. } else if type == .center {
  1219. self.centerAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1220. } else if type == .right {
  1221. self.rightAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1222. }
  1223. }
  1224. }
  1225. // MARK: SKFreeTextNote、KMSelfSignAnnotationFreeText Properties
  1226. func createSelfSignFreeTextProperties() {
  1227. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
  1228. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  1229. textColorBoxConstraint.constant = 10
  1230. borderColorBoxConstraint.constant = 0
  1231. textColorBox.isHidden = true
  1232. lineWidthBox.isHidden = true
  1233. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  1234. hiddenSubviews()
  1235. fontViewTopConstant.constant = colorView.frame.size.height + opacityView.frame.size.height - 10 + 10
  1236. ColorViewTopConstant.constant = -(fontView.frame.size.height + fontViewTopConstant.constant)
  1237. dateViewTopConstant.constant = fontView.frame.size.height + 30
  1238. fontViewCorloPV.noContentString = true
  1239. fontViewCorloPV.annotationTypeString = ""
  1240. textColorPickerView.annotationTypeString = NSLocalizedString("Background", comment: "")
  1241. dateCheckButton.title = NSLocalizedString("Time", comment: "")
  1242. if let attrTitle = dateCheckButton.attributedTitle.mutableCopy() as? NSMutableAttributedString {
  1243. let len = attrTitle.length
  1244. let range = NSRange(location: 0, length: len)
  1245. attrTitle.addAttribute(.foregroundColor, value: KMAppearance.Layout.h1Color(), range: range)
  1246. attrTitle.fixAttributes(in: range)
  1247. dateCheckButton.attributedTitle = attrTitle
  1248. }
  1249. if activeAnnotationType == .selfSignFreeText { // 添加日期 字体默认颜色不要透明色
  1250. textColorPickerView.annotationType = .freeTextFillColors
  1251. fontViewCorloPV.annotationType = .freeTextColors
  1252. } else {
  1253. textColorPickerView.annotationType = .freeTextColors
  1254. fontViewCorloPV.annotationType = .freeTextFillColors
  1255. }
  1256. }
  1257. func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
  1258. var selectIndex: UInt = 0
  1259. let menu = NSMenu()
  1260. if let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName) {
  1261. for i in 0..<fontFamily.count {
  1262. let array = fontFamily[i]
  1263. if let styleName = array[1] as? String {
  1264. if style == styleName {
  1265. selectIndex = UInt(i)
  1266. }
  1267. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
  1268. let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0)
  1269. let attrited = [NSAttributedString.Key.font: font]
  1270. let string = NSAttributedString(string: styleName, attributes: attrited)
  1271. let item = NSMenuItem()
  1272. item.attributedTitle = string
  1273. menu.addItem(item)
  1274. }
  1275. }
  1276. }
  1277. if style == nil {
  1278. selectIndex = 0
  1279. }
  1280. fontStylePopUpButton.menu = menu
  1281. return selectIndex
  1282. }
  1283. // MARK: UI Action
  1284. @IBAction func colorPickerViewAction(_ sender: Any) {
  1285. var fillColor: NSColor = NSColor.clear
  1286. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1287. if let textColor = textColorPickerView.color {
  1288. textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1289. if fabs(opacity - 0) < 0.001 {
  1290. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
  1291. annotationModel?.setInteriorOpacity(0)
  1292. annotationModel?.setInteriorColor(.black)
  1293. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1294. annotationModel?.setOpacity(0)
  1295. annotationModel?.setFontColor(.black)
  1296. } else {
  1297. annotationModel?.setOpacity(0)
  1298. annotationModel?.setFontColor(.black)
  1299. }
  1300. } else {
  1301. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
  1302. annotationModel?.setInteriorOpacity(opacity)
  1303. annotationModel?.setInteriorColor(textColor)
  1304. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1305. annotationModel?.setOpacity(opacity)
  1306. annotationModel?.setFontColor(textColor)
  1307. } else {
  1308. annotationModel?.setOpacity(opacity)
  1309. annotationModel?.setColor(textColor)
  1310. }
  1311. }
  1312. opacitySlider.floatValue = Float(opacity)
  1313. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1314. fillColor = color
  1315. if annotationModel?.annotations != nil {
  1316. if annotationModel?.annotation is CPDFFreeTextAnnotation {
  1317. let textNote = (annotationModel?.annotation as! CPDFFreeTextAnnotation)
  1318. if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  1319. pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
  1320. } else {
  1321. updateAnnotation()
  1322. }
  1323. } else {
  1324. updateAnnotation()
  1325. }
  1326. generalImageView.image = annotationModel?.annotationImage
  1327. } else {
  1328. updateAnnotation()
  1329. }
  1330. }
  1331. }
  1332. @IBAction func fillColorPickerViewAction(_ sender: Any) {
  1333. var fillColor = NSColor.clear
  1334. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1335. if let borderColor = borderColorPickerView.color {
  1336. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1337. opacitySlider.floatValue = Float(opacity)
  1338. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1339. fillColor = color
  1340. }
  1341. if annotationType == .freeText {
  1342. if let fillColorPickerColor = fillColorPickerView.color {
  1343. fillColorPickerColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1344. opacitySlider.floatValue = Float(opacity)
  1345. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1346. annotationModel?.setColor(color)
  1347. annotationModel?.setOpacity(opacity)
  1348. }
  1349. } else {
  1350. annotationModel?.setColor(fillColor)
  1351. annotationModel?.setOpacity(opacity)
  1352. }
  1353. updateAnnotation()
  1354. }
  1355. @IBAction func fontViewCorloPVAction(_ sender: Any) {
  1356. // var fillColor: NSColor = NSColor.clear
  1357. // var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1358. //
  1359. // if let textColor = fontViewCorloPV.color {
  1360. // textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1361. // if fabs(opacity - 0) < 0.001 {
  1362. // if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1363. // annotationModel?.setOpacity(0)
  1364. // annotationModel?.setFontColor(.black)
  1365. // } else {
  1366. // annotationModel?.setOpacity(0)
  1367. // annotationModel?.setFontColor(.black)
  1368. // }
  1369. // } else {
  1370. // if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1371. // annotationModel?.setOpacity(opacity)
  1372. // annotationModel?.setFontColor(textColor)
  1373. // } else {
  1374. // annotationModel?.setOpacity(opacity)
  1375. // annotationModel?.setColor(textColor)
  1376. // }
  1377. // }
  1378. //
  1379. // opacitySlider.floatValue = Float(opacity)
  1380. //
  1381. // if annotationModel?.annotation != nil {
  1382. // if annotationModel?.annotation is CPDFFreeTextAnnotation {
  1383. // let textNote = (annotationModel?.annotation as! CPDFFreeTextAnnotation)
  1384. // if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  1385. // pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
  1386. // } else {
  1387. // updateAnnotation()
  1388. // }
  1389. // } else {
  1390. // updateAnnotation()
  1391. // }
  1392. // generalImageView.image = annotationModel?.annotationImage
  1393. // } else {
  1394. // updateAnnotation()
  1395. // }
  1396. // }
  1397. if annotationType == .freeText {
  1398. annotationModel?.setFontColor(fontViewCorloPV.color)
  1399. } else {
  1400. annotationModel?.setColor(fontViewCorloPV.color)
  1401. }
  1402. updateAnnotation()
  1403. }
  1404. @IBAction func opacitySliderAction(_ sender: Any) {
  1405. var opcity = CGFloat(opacitySlider.floatValue)
  1406. if opcity == 0 {
  1407. opcity = 0.0001
  1408. }
  1409. if annotationType == .signFalse ||
  1410. annotationType == .signTure ||
  1411. annotationType == .signDot ||
  1412. annotationType == .signCircle ||
  1413. annotationType == .signLine {
  1414. annotationModel?.setOpacity(opcity)
  1415. } else if annotationType == .square ||
  1416. annotationType == .circle {
  1417. if opcity != 0 {
  1418. annotationModel?.setOpacity(opcity)
  1419. annotationModel?.setInteriorOpacity(opcity)
  1420. } else {
  1421. annotationModel?.setColor(nil)
  1422. annotationModel?.setInteriorColor(nil)
  1423. }
  1424. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  1425. annotationModel?.setOpacity(opcity)
  1426. var color = annotationModel?.fontColor()
  1427. if annotationModel?.opacity() != 0 {
  1428. var red: CGFloat = 0.0
  1429. var green: CGFloat = 0.0
  1430. var blue: CGFloat = 0.0
  1431. var opacity: CGFloat = 0.0
  1432. color?.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1433. color = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.opacity())!)
  1434. } else {
  1435. color = .clear
  1436. }
  1437. } else {
  1438. annotationModel?.setOpacity(opcity)
  1439. }
  1440. opacityComboBox.stringValue = "\(Int(opcity * 100))%"
  1441. updateAnnotation()
  1442. }
  1443. @IBAction func opacityComboBoxAction(_ sender: Any) {
  1444. opacitySlider.floatValue = Float(opacityComboBox.floatValue / 100)
  1445. var opcity = CGFloat(opacitySlider.floatValue)
  1446. if opcity == 0 {
  1447. opcity = 0.0001
  1448. }
  1449. if annotationType == .signFalse ||
  1450. annotationType == .signTure ||
  1451. annotationType == .signDot ||
  1452. annotationType == .signCircle ||
  1453. annotationType == .signLine {
  1454. annotationModel?.setOpacity(opcity)
  1455. } else if annotationType == .square ||
  1456. annotationType == .circle {
  1457. if opcity != 0 {
  1458. annotationModel?.setOpacity(opcity)
  1459. annotationModel?.setInteriorOpacity(opcity)
  1460. } else {
  1461. annotationModel?.setColor(nil)
  1462. annotationModel?.setInteriorColor(nil)
  1463. }
  1464. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  1465. annotationModel?.setInteriorOpacity(opcity)
  1466. } else {
  1467. annotationModel?.setOpacity(opcity)
  1468. }
  1469. opacitySlider.floatValue = Float(opcity)
  1470. opacitySlider.toolTip = "\(Int(opcity * 100))%"
  1471. updateAnnotation()
  1472. }
  1473. @IBAction func lineWidthSliderAction(_ sender: Any) {
  1474. annotationModel?.setLineWidth(CGFloat(lineWidthSlider.floatValue))
  1475. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  1476. updateAnnotation()
  1477. }
  1478. @IBAction func lineWidthComboBoxAction(_ sender: Any) {
  1479. var lineWidth = lineWidthComboBox.floatValue
  1480. if lineWidth > 18 {
  1481. lineWidth = 18
  1482. } else if lineWidth < 0.5 {
  1483. lineWidth = 0.5
  1484. }
  1485. annotationModel?.setLineWidth(CGFloat(lineWidth))
  1486. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1487. updateAnnotation()
  1488. }
  1489. @IBAction func alignButtonAction(_ sender: NSButton) {
  1490. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  1491. if annotationModel?.annotations != nil {
  1492. for tAnnotation in self.annotations {
  1493. switch sender.tag {
  1494. case 0:
  1495. if tAnnotation is CPDFFreeTextAnnotation {
  1496. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .left
  1497. }
  1498. alignmentType = .left
  1499. case 1:
  1500. if tAnnotation is CPDFFreeTextAnnotation {
  1501. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .center
  1502. }
  1503. alignmentType = .center
  1504. case 2:
  1505. if tAnnotation is CPDFFreeTextAnnotation {
  1506. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .right
  1507. }
  1508. alignmentType = .right
  1509. default:
  1510. break
  1511. }
  1512. }
  1513. } else {
  1514. switch sender.tag {
  1515. case 0:
  1516. annotationModel?.setAlignment(.left)
  1517. alignmentType = .left
  1518. case 1:
  1519. annotationModel?.setAlignment(.center)
  1520. alignmentType = .center
  1521. case 2:
  1522. annotationModel?.setAlignment(.right)
  1523. alignmentType = .right
  1524. default:
  1525. break
  1526. }
  1527. }
  1528. alignmentTypeSelected(alignmentType)
  1529. updateAnnotation()
  1530. NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationAlignmentTypeNotification"),
  1531. object: "\(alignmentType.rawValue)")
  1532. }
  1533. @IBAction func currentFontColorButtonAction(_ sender: NSButton) {
  1534. // if ([_annotation isKindOfClass:[PDFAnnotationFreeText class]]) {
  1535. // KMFontColorViewController *vc = [[[KMFontColorViewController alloc] initWithNibName:@"KMFontColorViewController" bundle:[NSBundle mainBundle]] autorelease];
  1536. // vc.annotationFontColor = _annotation.fontColor;
  1537. // __block typeof(self) blockSelf = self;
  1538. // vc.fontColorChangeCallback = ^(NSColor *fontColor) {
  1539. // for (PDFAnnotation *tAnnotation in blockSelf.annotations) {
  1540. // tAnnotation.fontColor = fontColor;
  1541. // }
  1542. // };
  1543. // NSPopover *popover = [[NSPopover alloc] init];
  1544. // popover.delegate = self;
  1545. // popover.contentViewController = vc;
  1546. // popover.animates = YES;
  1547. // popover.behavior = NSPopoverBehaviorTransient;
  1548. // [popover showRelativeToRect:CGRectMake([sender bounds].origin.x, 0, [sender bounds].size.width, [sender bounds].size.height) ofView:sender preferredEdge:CGRectMaxYEdge];
  1549. // [popover release];
  1550. // }
  1551. }
  1552. @IBAction func borderButtonAction(_ sender: NSButton) {
  1553. var lineWidth: CGFloat = 0.0
  1554. var lineStype: CPDFBorderStyle = .solid
  1555. var startingPoint: CPDFLineStyle = .none
  1556. var endPoint: CPDFLineStyle = .none
  1557. var dashPattern: [NSNumber] = []
  1558. var isLineAnnotation: Bool = false
  1559. if annotationModel?.annotations != nil {
  1560. for tAnnotation in self.annotations {
  1561. lineWidth = tAnnotation.lineWidth()
  1562. lineStype = tAnnotation.borderStyle()
  1563. dashPattern = tAnnotation.dashPattern() as! [NSNumber]
  1564. if tAnnotation is CPDFLineAnnotation {
  1565. isLineAnnotation = true
  1566. startingPoint = (tAnnotation as! CPDFLineAnnotation).startLineStyle
  1567. endPoint = (tAnnotation as! CPDFLineAnnotation).endLineStyle
  1568. } else {
  1569. isLineAnnotation = false
  1570. }
  1571. }
  1572. } else {
  1573. lineWidth = String(format: "%0.1f pt", (annotationModel?.lineWidth())!).stringToCGFloat()
  1574. lineStype = (annotationModel?.style())! as CPDFBorderStyle
  1575. dashPattern = (annotationModel?.dashPattern())! as [NSNumber]
  1576. if annotationModel?.annotationType == .line {
  1577. isLineAnnotation = true
  1578. startingPoint = (annotationModel?.startLineStyle())! as CPDFLineStyle
  1579. endPoint = (annotationModel?.endLineStyle())! as CPDFLineStyle
  1580. } else {
  1581. isLineAnnotation = false
  1582. }
  1583. }
  1584. let lineModel = KMLineModel(lineWidth: lineWidth, lineStype: lineStype, startingPoint: startingPoint, endPoint: endPoint, dashPattern: dashPattern, isLineAnnotation: isLineAnnotation)
  1585. let fontWindowController = KMAnnotationLineWindowController.initWindowController(lineModel)
  1586. guard let window = fontWindowController.window else { return }
  1587. // fontWindowController.annotationLineChangeBlock = { [weak self] lineNote in
  1588. // if lineNote.borderStyle() == .solid {
  1589. // self!.lineTypeButtonAction(self!.lineStyleButton)
  1590. // } else if lineNote.borderStyle() == .dashed {
  1591. // self!.lineTypeButtonAction(self!.dottedLineStyleButton)
  1592. // for tAnnotation in self!.annotations {
  1593. // tAnnotation.setDashPattern(lineNote.dashPattern())
  1594. // }
  1595. // }
  1596. // }
  1597. fontWindowController.callback = { model in
  1598. if self.annotationModel?.annotations != nil {
  1599. } else {
  1600. }
  1601. }
  1602. window.orderFront(sender)
  1603. }
  1604. func lineTypePopAction() {
  1605. annotationModel?.setStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
  1606. updateAnnotation()
  1607. }
  1608. @IBAction func lineTypeButtonAction(_ sender: NSButton) {
  1609. let tag = sender.tag
  1610. switch tag {
  1611. case 0:
  1612. lineStyleCount = 0
  1613. refreshLineTypeUI()
  1614. lineStyleButton.image = borderStyleSolid(true)
  1615. dottedLineStyleButton.image = borderStyleDashed(false)
  1616. rightArrowStyleButton.image = lineStyleRightArrow(false)
  1617. lineTypePopAction()
  1618. case 1:
  1619. lineStyleCount = 1
  1620. refreshLineTypeUI()
  1621. lineStyleButton.image = borderStyleSolid(false)
  1622. dottedLineStyleButton.image = borderStyleDashed(true)
  1623. rightArrowStyleButton.image = lineStyleRightArrow(false)
  1624. lineTypePopAction()
  1625. case 2:
  1626. lineStyleCount = 2
  1627. refreshLineTypeUI()
  1628. lineStyleButton.image = borderStyleSolid(false)
  1629. dottedLineStyleButton.image = borderStyleDashed(false)
  1630. rightArrowStyleButton.image = lineStyleRightArrow(true)
  1631. for tAnnotation in annotations {
  1632. if tAnnotation is CPDFLineAnnotation {
  1633. (tAnnotation as! CPDFLineAnnotation).endLineStyle = .closedArrow
  1634. }
  1635. }
  1636. default:
  1637. break
  1638. }
  1639. }
  1640. @IBAction func iconButtonAction(_ sender: NSButton) {
  1641. var type: CPDFTextAnnotationIconType = .comment
  1642. switch sender.tag {
  1643. case 0:
  1644. type = .comment
  1645. case 1:
  1646. type = .key
  1647. case 2:
  1648. type = .note
  1649. case 3:
  1650. type = .help
  1651. case 4:
  1652. type = .newParagraph
  1653. case 5:
  1654. type = .paragraph
  1655. case 6, _:
  1656. type = .insert
  1657. }
  1658. if annotationModel?.annotations != nil {
  1659. for tAnnotation in self.annotations {
  1660. if tAnnotation is CPDFTextAnnotation {
  1661. (tAnnotation as! CPDFTextAnnotation).setIconType(type)
  1662. }
  1663. }
  1664. self.updateAnnotation()
  1665. } else {
  1666. annotationModel?.setAnchoredIconType(type)
  1667. }
  1668. generalImageView.image = annotationModel?.annotationImage
  1669. updateannotationMode()
  1670. clickAnnotationTextIconButtonAction(iconType: UInt(sender.tag), boxArr: iconButtonArray)
  1671. }
  1672. @IBAction func textImageBoxButtonAction(_ sender: Any) {
  1673. openFreeTextStylesViewController()
  1674. }
  1675. // MARK: Font、FontSize Action
  1676. @IBAction func fontButtonAction(_ sender: NSButton) {
  1677. let selectItem = self.fontPopUpButton.selectedItem
  1678. guard let selectItem = selectItem else { return }
  1679. let resultAtt = NSMutableAttributedString(attributedString: selectItem.attributedTitle!)
  1680. let familyString = resultAtt.string
  1681. let styleString = self.fontStylePopUpButton.selectedItem?.title
  1682. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: familyString, .face: styleString as Any])
  1683. let fontSizeString = fontSizeComboBox.stringValue.replacingOccurrences(of: " pt", with: "")
  1684. let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSizeString.stringToCGFloat())
  1685. let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString!, fontSize: fontSizeString.stringToCGFloat(), fontColor: textColorPickerView.color ?? .black, fontAlignment: annotationAlignment, annotationType: annotationModel!.annotationType)
  1686. let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
  1687. let window = fontWindowController.window
  1688. fontWindowController.callback = { [weak self] model in
  1689. self!.alignmentTypeSelected(model.fontAlignment)
  1690. self!.textColorPickerView.color = model.fontColor
  1691. DispatchQueue.global(qos: .default).async {
  1692. let fonts = NSFontManager.shared.availableFontFamilies
  1693. let menu = NSMenu()
  1694. var selectedIndex = 0
  1695. for (index, fontName) in fonts.enumerated() {
  1696. if let font = NSFont(name: fontName, size: 12.0) {
  1697. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  1698. let item = NSMenuItem()
  1699. item.attributedTitle = attributedString
  1700. menu.addItem(item)
  1701. if model.fontName == font.fontName {
  1702. selectedIndex = index
  1703. }
  1704. }
  1705. }
  1706. DispatchQueue.main.async {
  1707. self!.fontPopUpButton.menu = menu
  1708. self!.fontPopUpButton.selectItem(at: selectedIndex)
  1709. }
  1710. }
  1711. let selectedStyleIndex = self!.setFontStyle(fontName: model.fontName, currentStyle: model.fontWeight)
  1712. self!.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1713. self!.fontSizeComboBox.stringValue = String(format: "%.f pt", model.fontSize)
  1714. if self!.annotationModel?.annotations != nil {
  1715. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: model.fontName, .face: model.fontWeight])
  1716. let annotationFont = NSFont(descriptor: attributeFontDescriptor, size: model.fontSize)
  1717. for tAnnotation in self!.annotations {
  1718. if tAnnotation is CPDFFreeTextAnnotation {
  1719. (tAnnotation as! CPDFFreeTextAnnotation).font = annotationFont
  1720. (tAnnotation as! CPDFFreeTextAnnotation).fontColor = model.fontColor
  1721. if model.fontAlignment == .left {
  1722. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .left
  1723. } else if model.fontAlignment == .center {
  1724. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .center
  1725. } else if model.fontAlignment == .right {
  1726. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .right
  1727. }
  1728. }
  1729. }
  1730. self!.updateAnnotation()
  1731. } else {
  1732. self!.annotationModel?.setFontName(model.fontName)
  1733. self!.annotationModel?.setFontSize(model.fontSize)
  1734. self!.annotationModel?.setFontColor(model.fontColor)
  1735. if model.fontAlignment == .left {
  1736. self!.annotationModel?.setAlignment(.left)
  1737. } else if model.fontAlignment == .center {
  1738. self!.annotationModel?.setAlignment(.center)
  1739. } else if model.fontAlignment == .right {
  1740. self!.annotationModel?.setAlignment(.right)
  1741. }
  1742. }
  1743. }
  1744. window?.orderFront(sender)
  1745. }
  1746. @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
  1747. guard let selectedItem = fontPopUpButton.selectedItem else { return }
  1748. let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
  1749. let familyString = resultAtt.string
  1750. let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
  1751. guard let styleString = fontStylePopUpButton.selectedItem?.title else { return }
  1752. fontStylePopUpButton.selectItem(at: Int(selectIndex))
  1753. if annotationModel?.annotations != nil {
  1754. for tAnnotation in annotations {
  1755. if tAnnotation is CPDFFreeTextAnnotation {
  1756. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1757. if let font = newAnnotation.font {
  1758. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1759. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1760. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1761. newAnnotation.font = newFont
  1762. }
  1763. }
  1764. if let tFont = newAnnotation.font {
  1765. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1766. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1767. DispatchQueue.main.async {
  1768. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1769. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1770. }
  1771. }
  1772. }
  1773. }
  1774. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1775. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1776. if let font = newAnnotation.font {
  1777. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1778. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1779. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1780. newAnnotation.font = newFont
  1781. }
  1782. }
  1783. newAnnotation.updateBounds()
  1784. if let tFont = newAnnotation.font {
  1785. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1786. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1787. DispatchQueue.main.async {
  1788. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1789. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1790. }
  1791. }
  1792. }
  1793. }
  1794. }
  1795. }
  1796. } else {
  1797. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1798. let font = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1799. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1800. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1801. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1802. annotationModel?.setFontName(newFont.fontName)
  1803. annotationModel?.setFontSize(newFont.pointSize)
  1804. }
  1805. }
  1806. let tFont = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1807. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1808. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1809. DispatchQueue.main.async {
  1810. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1811. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1812. }
  1813. }
  1814. }
  1815. }
  1816. updateAnnotation()
  1817. }
  1818. @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
  1819. if annotationModel?.annotations != nil {
  1820. for tAnnotation in annotations {
  1821. if tAnnotation is CPDFFreeTextAnnotation {
  1822. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1823. if let font = newAnnotation.font {
  1824. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1825. if let newFont = newFont {
  1826. newAnnotation.font = newFont
  1827. }
  1828. }
  1829. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1830. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1831. if let font = newAnnotation.font {
  1832. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1833. if let newFont = newFont {
  1834. newAnnotation.font = newFont
  1835. }
  1836. }
  1837. newAnnotation.updateBounds()
  1838. }
  1839. }
  1840. } else {
  1841. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1842. let font = NSFont(name: (annotationModel?.fontName()!)!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1843. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1844. if let newFont = newFont {
  1845. annotationModel?.setFontName(newFont.fontName)
  1846. annotationModel?.setFontSize(newFont.pointSize)
  1847. }
  1848. }
  1849. }
  1850. updateAnnotation()
  1851. NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationFontTypeNotification"),
  1852. object: pdfView)
  1853. adjustFreeText()
  1854. }
  1855. //字重 NSFontWeight
  1856. @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
  1857. if let styleString = self.fontStylePopUpButton.selectedItem?.title {
  1858. if annotationModel?.annotations != nil {
  1859. for tAnnotation in self.annotations {
  1860. if tAnnotation is CPDFFreeTextAnnotation {
  1861. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1862. if let font = newAnnotation.font {
  1863. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1864. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1865. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1866. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1867. newAnnotation.font = newFont
  1868. }
  1869. }
  1870. }
  1871. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1872. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1873. if let font = newAnnotation.font {
  1874. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1875. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1876. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1877. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1878. newAnnotation.font = newFont
  1879. }
  1880. newAnnotation.updateBounds()
  1881. }
  1882. }
  1883. }
  1884. }
  1885. } else {
  1886. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1887. let font = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1888. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1889. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1890. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1891. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1892. annotationModel?.setFontName(newFont.fontName)
  1893. annotationModel?.setFontSize(newFont.pointSize)
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. updateAnnotation()
  1900. }
  1901. @IBAction func buttonClicked_SwitchIncludeTime(_ sender: NSButton) {
  1902. for case let tAnnotation as KMSelfSignAnnotationFreeText in annotations {
  1903. tAnnotation.includeTime = sender.state == .on ? true : false
  1904. let index = datePopUpButton.indexOfSelectedItem
  1905. datePopUpButton.removeAllItems()
  1906. datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: false))
  1907. datePopUpButton.selectItem(at: index)
  1908. if self.subType == .date && isannotationMode {
  1909. UserDefaults.standard.set(sender.state == .on ? true : false, forKey: SKAnnotationSelfSignDateFormatterIncludeTimeKey)
  1910. }
  1911. }
  1912. reloadData()
  1913. }
  1914. @IBAction func dateCheckButtonAction(_ sender: NSPopUpButton) {
  1915. for case let tAnnotation as KMSelfSignAnnotationFreeText in annotations {
  1916. if subType == .date && isannotationMode {
  1917. let fetchArray = KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: false)
  1918. if sender.indexOfSelectedItem >= 0 && sender.indexOfSelectedItem <= fetchArray.count - 1 {
  1919. UserDefaults.standard.set(sender.indexOfSelectedItem, forKey: SKAnnotationSelfSignDateFormatterKey)
  1920. }
  1921. }
  1922. tAnnotation.dateFormatIndex = sender.indexOfSelectedItem
  1923. }
  1924. reloadData()
  1925. }
  1926. func annotationFontReload() {
  1927. DispatchQueue.global(qos: .default).async { [self] in
  1928. let fonts = NSFontManager.shared.availableFontFamilies
  1929. let menu = NSMenu()
  1930. var selectedIndex = 0
  1931. for (index, fontName) in fonts.enumerated() {
  1932. if let font = NSFont(name: fontName, size: 12.0) {
  1933. let attributedString = NSAttributedString(string: fontName, attributes: [NSAttributedString.Key.font: font])
  1934. let item = NSMenuItem()
  1935. item.attributedTitle = attributedString
  1936. menu.addItem(item)
  1937. if annotationType == .freeText {
  1938. if annotationModel?.fontName() == font.fontName {
  1939. selectedIndex = index
  1940. }
  1941. }
  1942. }
  1943. }
  1944. if annotationType == .freeText {
  1945. let freetextFont = NSFont(name: (annotationModel?.fontName() as? String)!, size: 16) ?? NSFont.systemFont(ofSize: 16)
  1946. if let family = freetextFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1947. let style = freetextFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1948. DispatchQueue.main.async { [self] in
  1949. fontPopUpButton.menu = menu
  1950. fontPopUpButton.selectItem(at: selectedIndex)
  1951. let selectedStyleIndex = setFontStyle(fontName: family, currentStyle: style)
  1952. fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1953. }
  1954. }
  1955. }
  1956. }
  1957. }
  1958. func adjustFreeText() {
  1959. if annotationModel?.annotations != nil {
  1960. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1961. for an in (annotationModel?.annotations)! {
  1962. if let freeTextAn = an as? CPDFFreeTextAnnotation {
  1963. let font = freeTextAn.font
  1964. var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font as Any]
  1965. let style = NSMutableParagraphStyle()
  1966. style.alignment = freeTextAn.alignment
  1967. attributes[NSAttributedString.Key.paragraphStyle] = style
  1968. let contents = freeTextAn.contents ?? ""
  1969. let textViewSize = contents.boundingRect(with: CGSize(width: freeTextAn.bounds.size.width, height: CGFloat(MAXFLOAT)),
  1970. options: .usesLineFragmentOrigin,
  1971. attributes: attributes).size
  1972. var rect = freeTextAn.bounds
  1973. if textViewSize.height != freeTextAn.bounds.size.height {
  1974. rect.origin.y -= (textViewSize.height - rect.size.height)
  1975. rect.size.height = textViewSize.height
  1976. }
  1977. if textViewSize.width < freeTextAn.bounds.size.width {
  1978. rect.size.width = textViewSize.width
  1979. }
  1980. freeTextAn.bounds = rect
  1981. }
  1982. }
  1983. }
  1984. }
  1985. }
  1986. // MARK: NSNotification
  1987. @objc func textDidChange(_ notification: Notification) {
  1988. // self.isTextEdit = true
  1989. //
  1990. // if self.subType == KMSelfSignAnnotationFreeTextSubType.freeText && isannotationMode {
  1991. // if let obj = notification.object as? NSObject {
  1992. // for tAnnotation in annotations {
  1993. // tAnnotation.setString(noteTextView.string)
  1994. // }
  1995. //
  1996. // if obj.isEqual(to: noteTextView) {
  1997. // UserDefaults.standard.set(noteTextView.string, forKey: SKAnnotationSelfSignPlaceHolderStringKey)
  1998. // }
  1999. //
  2000. // if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  2001. // textAnnotationImageView.image = annotationModel?.annotationImage
  2002. // } else {
  2003. // generalImageView.image = annotationModel?.annotationImage
  2004. // }
  2005. // }
  2006. // } else {
  2007. // if let obj = notification.object as? NSObject, obj.isEqual(to: noteTextView) {
  2008. // for tAnnotation in self.annotations {
  2009. // tAnnotation.setString(noteTextView.string)
  2010. // }
  2011. // }
  2012. // }
  2013. }
  2014. @objc func textDidEndEditing(_ notification: Notification) {
  2015. self.isTextEdit = true
  2016. if self.subType == KMSelfSignAnnotationFreeTextSubType.freeText && isannotationMode {
  2017. if let obj = notification.object as? NSObject {
  2018. for tAnnotation in annotations {
  2019. tAnnotation.setString(noteTextView.string)
  2020. }
  2021. if obj.isEqual(to: noteTextView) {
  2022. UserDefaults.standard.set(noteTextView.string, forKey: SKAnnotationSelfSignPlaceHolderStringKey)
  2023. }
  2024. if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  2025. textAnnotationImageView.image = annotationModel?.annotationImage
  2026. } else {
  2027. generalImageView.image = annotationModel?.annotationImage
  2028. }
  2029. }
  2030. } else {
  2031. if let obj = notification.object as? NSObject, obj.isEqual(to: noteTextView) {
  2032. for tAnnotation in self.annotations {
  2033. tAnnotation.setString(noteTextView.string)
  2034. }
  2035. }
  2036. }
  2037. }
  2038. @objc func themeChanged(_ notification: Notification) {
  2039. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [self] in
  2040. updateViewColor()
  2041. }
  2042. }
  2043. }
  2044. class KMGeneralAnnotationColorButton: NSButton {
  2045. var _circleColor: NSColor = .clear
  2046. var _buttonBorderColor: NSColor = .clear
  2047. // MARK: Get & Set
  2048. var circleColor: NSColor {
  2049. set {
  2050. _circleColor = newValue
  2051. needsDisplay = true
  2052. }
  2053. get {
  2054. return _circleColor
  2055. }
  2056. }
  2057. var buttonBorderColor: NSColor {
  2058. set {
  2059. _buttonBorderColor = newValue
  2060. needsDisplay = true
  2061. }
  2062. get {
  2063. return _buttonBorderColor
  2064. }
  2065. }
  2066. // MARK: Init
  2067. required init?(coder: NSCoder) {
  2068. super.init(coder: coder)
  2069. wantsLayer = true
  2070. layer?.cornerRadius = 12.0
  2071. layer?.masksToBounds = true
  2072. layer?.borderColor = NSColor.clear.cgColor
  2073. }
  2074. override func draw(_ dirtyRect: NSRect) {
  2075. super.draw(dirtyRect)
  2076. let path3 = NSBezierPath(ovalIn: NSMakeRect(3, 3, dirtyRect.size.width - 6, dirtyRect.size.height - 6))
  2077. }
  2078. }
  2079. class KMNoteIconButtonCell: NSButtonCell {
  2080. override func drawImage(_ image: NSImage, withFrame frame: NSRect, in controlView: NSView) {
  2081. let titleSize = self.attributedTitle.size()
  2082. let imageSize = self.image!.size
  2083. var rect = NSZeroRect
  2084. let leftGap = (controlView.bounds.size.width - image.size.width) / 2.0
  2085. rect.origin.x = leftGap
  2086. if self.imagePosition == .imageAbove {
  2087. rect.origin.y = 6
  2088. } else {
  2089. rect.origin.y = (controlView.frame.size.height - image.size.height) / 2.0
  2090. }
  2091. rect.size = imageSize
  2092. super.drawImage(self.image!, withFrame: rect, in: controlView)
  2093. }
  2094. override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
  2095. let titleSize = self.attributedTitle.size()
  2096. let imageSize = self.image!.size
  2097. let totalHeight = titleSize.height + imageSize.height + 1
  2098. var rect = NSZeroRect
  2099. rect.origin.x = (controlView.bounds.size.width - titleSize.width) / 2.0
  2100. rect.origin.y = (controlView.bounds.size.height - totalHeight) / 2.0 + imageSize.height + 1
  2101. rect.size = titleSize
  2102. return super.drawTitle(title, withFrame: rect, in: controlView)
  2103. }
  2104. }