KMGeneralAnnotationViewController.swift 123 KB

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