KMGeneralAnnotationViewController.swift 120 KB

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