KMGeneralAnnotationViewController.swift 123 KB

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