KMGeneralAnnotationViewController.swift 126 KB

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