KMGeneralAnnotationViewController.swift 121 KB

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