KMGeneralAnnotationViewController.swift 118 KB

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