KMGeneralAnnotationViewController.swift 115 KB

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