KMGeneralAnnotationViewController.swift 116 KB

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