KMGeneralAnnotationViewController.swift 121 KB

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