KMGeneralAnnotationViewController.swift 123 KB

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