KMGeneralAnnotationViewController.swift 114 KB

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