KMGeneralAnnotationViewController.swift 126 KB

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