1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545 |
- //
- // KMGeneralAnnotationViewController.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/12/1.
- //
- import Cocoa
- /**
- NSString *SKNFreeTextString = @"FreeText";
- NSString *SKNTextString = @"Text";
- NSString *SKNNoteString = @"Note";
- NSString *SKNCircleString = @"Circle";
- NSString *SKNSquareString = @"Square";
- NSString *SKNMarkUpString = @"MarkUp";
- NSString *SKNHighlightString = @"Highlight";
- NSString *SKNUnderlineString = @"Underline";
- NSString *SKNStrikeOutString = @"StrikeOut";
- NSString *SKNLineString = @"Line";
- NSString *SKNInkString = @"Ink";
- NSString *SKNPDFAnnotationTypeKey = @"type";
- NSString *SKNPDFAnnotationBoundsKey = @"bounds";
- NSString *SKNPDFAnnotationPageKey = @"page";
- NSString *SKNPDFAnnotationPageIndexKey = @"pageIndex";
- NSString *SKNPDFAnnotationContentsKey = @"contents";
- NSString *SKNPDFAnnotationStringKey = @"string";
- NSString *SKNPDFAnnotationColorKey = @"color";
- NSString *SKNPDFAnnotationBorderKey = @"border";
- NSString *SKNPDFAnnotationLineWidthKey = @"lineWidth";
- NSString *SKNPDFAnnotationBorderStyleKey = @"borderStyle";
- NSString *SKNPDFAnnotationDashPatternKey = @"dashPattern";
- NSString *SKNPDFAnnotationModificationDateKey = @"modificationDate";
- NSString *SKNPDFAnnotationUserNameKey = @"userName";
- NSString *SKNPDFAnnotationInteriorColorKey = @"interiorColor";
- NSString *SKNPDFAnnotationStartLineStyleKey = @"startLineStyle";
- NSString *SKNPDFAnnotationEndLineStyleKey = @"endLineStyle";
- NSString *SKNPDFAnnotationStartPointKey = @"startPoint";
- NSString *SKNPDFAnnotationEndPointKey = @"endPoint";
- NSString *SKNPDFAnnotationFontKey = @"font";
- NSString *SKNPDFAnnotationFontColorKey = @"fontColor";
- NSString *SKNPDFAnnotationFontNameKey = @"fontName";
- NSString *SKNPDFAnnotationFontSizeKey = @"fontSize";
- NSString *SKNPDFAnnotationAlignmentKey = @"alignment";
- NSString *SKNPDFAnnotationRotationKey = @"rotation";
- NSString *SKNPDFAnnotationQuadrilateralPointsKey = @"quadrilateralPoints";
- NSString *SKNPDFAnnotationIconTypeKey = @"iconType";
- NSString *SKNPDFAnnotationPointListsKey = @"pointLists";
- */
- enum KMActiveAnnotationType: UInt {
- case highlight = 0
- case strikeOut
- case underline
- case inkAndLine
- case text
- case squareAndCircle
- case freeText
- case selfSignFreeText
- }
- enum KMFreeTextAnnotationAlignmentType: UInt {
- case left = 0
- case center
- case right
- }
- enum KMRefreshType: UInt {
- case none = 0
- case color
- case opacity
- }
- let KMColorPickerViewHeight: CGFloat = 64
- @objcMembers class KMGeneralAnnotationViewController: NSViewController, NSTextViewDelegate {
-
- var _annotations: [CPDFAnnotation]?
- var subType: KMSelfSignAnnotationFreeTextSubType = .none
- var _activeAnnotationType: KMActiveAnnotationType = .highlight
- var pdfView: CPDFListView?
- var annotationModel: CPDFAnnotationModel? {
- didSet {
- self.initAnnotationMode()
- }
- }
- // var annotationCallback: ((CPDFAnnotation) -> Void)?
-
- @IBOutlet private weak var imageBox: NSBox!
- @IBOutlet private weak var imageBoxLayoutConstraint: NSLayoutConstraint!
- @IBOutlet private weak var textImageBoxView: NSView!
- @IBOutlet private weak var textImageBox: KMBox!
- @IBOutlet private weak var textAnnotationImageView: NSImageView!
- @IBOutlet private weak var textImageBoxButton: NSButton!
- @IBOutlet private weak var textImageUpDateButton: NSButton!
- @IBOutlet private weak var generalImageBoxView: NSView!
- @IBOutlet private weak var generalImageView: NSImageView!
- @IBOutlet private weak var fontView: NSView! // 字体
- @IBOutlet private weak var fontLabel: NSTextField!
- @IBOutlet private weak var fontButton: NSButton!
- @IBOutlet private weak var fontPopUpButton: KMPopUpButton!
- @IBOutlet private weak var fontSizeComboBox: KMComboBox!
- @IBOutlet private weak var fontStylePopUpButton: KMPopUpButton!
- @IBOutlet private weak var leftAlignButton: NSButton!
- @IBOutlet private weak var centerAlignButton: NSButton!
- @IBOutlet private weak var rightAlignButton: NSButton!
- @IBOutlet private weak var fontViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var fontViewCorloPV: KMColorPickerView!
- @IBOutlet private weak var fontViewColorPVConstraint: NSLayoutConstraint!
- @IBOutlet private weak var rotateView: NSView! // 旋转
- @IBOutlet private weak var rotateLabel: NSTextField!
- @IBOutlet private weak var leftRotateBox: KMBox!
- @IBOutlet private weak var leftRotateButton: NSButton!
- @IBOutlet private weak var rightRotateBox: KMBox!
- @IBOutlet private weak var rightRotateButton: NSButton!
- @IBOutlet private weak var rotateViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var typeView: NSView! // 类型
- @IBOutlet private weak var typeLabel: NSTextField!
- @IBOutlet private weak var tureTypeButton: NSButton!
- @IBOutlet private weak var falseTypeButton: NSButton!
- @IBOutlet private weak var circletypeButton: NSButton!
- @IBOutlet private weak var lineTypeButton: NSButton!
- @IBOutlet private weak var dotTypeButton: NSButton!
- @IBOutlet private weak var typeViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var dateView: NSView! // 日期
- @IBOutlet private weak var dateLabel: NSTextField!
- @IBOutlet private weak var datePopUpButton: KMPopUpButton!
- @IBOutlet private weak var dateCheckButton: NSButton!
- @IBOutlet private weak var dateViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var iconView: NSView! // 图标
- @IBOutlet private weak var iconLabel: NSTextField!
- @IBOutlet private weak var commentBox: NSBox!
- @IBOutlet private weak var commentButton: NSButton!
- @IBOutlet private weak var noteBox: NSBox!
- @IBOutlet private weak var noteButton: NSButton!
- @IBOutlet private weak var keyBox: NSBox!
- @IBOutlet private weak var keyButton: NSButton!
- @IBOutlet private weak var helpBox: NSBox!
- @IBOutlet private weak var helpButton: NSButton!
- @IBOutlet private weak var paragraphBox: NSBox!
- @IBOutlet private weak var paragraphButton: NSButton!
- @IBOutlet private weak var insertBox: NSBox!
- @IBOutlet private weak var insertButton: NSButton!
- @IBOutlet private weak var addParagraphBox: NSBox!
- @IBOutlet private weak var addParagraphButton: NSButton!
- @IBOutlet private weak var iconViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var iconButtonHeightConstant: NSLayoutConstraint!
- @IBOutlet private weak var borderAndLineView: NSView! // 边框
- @IBOutlet private weak var borderLabel: NSTextField!
- @IBOutlet private weak var borderButton: NSButton!
- @IBOutlet private weak var lineWidthComboBox: KMComboBox!
- @IBOutlet private weak var lineWidthSlider: NSSlider!
- @IBOutlet private weak var lineTypeLabel: NSTextField!
- @IBOutlet private weak var lineStyleButton: KMButton!
- @IBOutlet private weak var dottedLineStyleButton: KMButton!
- @IBOutlet private weak var rightArrowStyleButton: KMButton!
- @IBOutlet private weak var lineStyleBox: NSBox!
- @IBOutlet private weak var dottedLineStyleBox: NSBox!
- @IBOutlet private weak var rightArrowStyleBox: NSBox!
- @IBOutlet private weak var borderViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var fillColorView: NSView!
- @IBOutlet private weak var fillColorLabel: NSTextField!
- @IBOutlet private weak var fillColorPickerView: KMColorPickerView!
- @IBOutlet private weak var fillColorViewTopConstraint: NSLayoutConstraint!
- @IBOutlet private weak var opacityView: NSView! // 透明度
- @IBOutlet private weak var opacityLabel: NSTextField!
- @IBOutlet private weak var opacitySlider: NSSlider!
- @IBOutlet private weak var opacityComboBox: KMComboBox!
- @IBOutlet private weak var opacityViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var noteView: NSView! // 笔记
- @IBOutlet private weak var noteViewLabel: NSTextField!
- @IBOutlet private var noteTextView: NSTextView!
- @IBOutlet private weak var noteViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var colorView: NSView! // 颜色
- @IBOutlet private weak var colorTitleLabel: NSTextField!
- @IBOutlet private weak var textColorBox: NSBox!
- @IBOutlet private weak var textColorBoxConstraint: NSLayoutConstraint!
- @IBOutlet private weak var textColorPickerView: KMColorPickerView!
- @IBOutlet private weak var borderColorBox: NSBox!
- @IBOutlet private weak var borderColorPickerView: KMColorPickerView!
- @IBOutlet private weak var borderColorBoxConstraint: NSLayoutConstraint!
- @IBOutlet private weak var lineWidthBox: NSBox!
- @IBOutlet private weak var widthLabel: NSTextField!
- @IBOutlet private weak var widthComboBox: KMComboBox!
- @IBOutlet private weak var widthSlider: NSSlider!
- @IBOutlet private weak var ColorViewTopConstant: NSLayoutConstraint!
- @IBOutlet private weak var textColorPickerViewConstant: NSLayoutConstraint!
- var isannotationMode: Bool = false
- var selectedAlignmentButtonLayer: CALayer?
- var iconButtonArray: [NSBox] = []
- var lineStyleCount: Int = 0
- var isTextEdit: Bool = false
- var fonts: [[String : [String]]]?
- var annotationAlignment: KMFreeTextAnnotationAlignmentType = .left
-
- var noteIcons: [NSImage?] = [nil, nil, nil, nil, nil, nil, nil]
-
- private let kDefaultOpacity_: CGFloat = 1.0
- private let kDefaultLineWidth_: CGFloat = 2.0
- private let kDefaultFontSize_: CGFloat = 11.0
-
- private func makeNoteIcons() {
- if noteIcons[0] != nil { return }
- let bounds = CGRect(origin: .zero, size: NSMakeSize(16, 16))
- let annotation = CPDFTextAnnotation()
- annotation.bounds = bounds
- annotation.color = NSColor.clear
- let page = CPDFPage()
- page.setBounds(bounds, for: .mediaBox)
- page.addAnnotation(annotation)
- for i in 0..<7 {
- annotation.setIconType(CPDFTextAnnotationIconType(rawValue: i) ?? .comment)
- noteIcons[i] = NSImage.bitmapImage(with: NSMakeSize(16, 16), drawingHandler: { rect in
- page.draw(with: .mediaBox, to: (NSGraphicsContext.current?.cgContext))
- })
- }
- }
- deinit {
- NotificationCenter.default.removeObserver(self)
- DistributedNotificationCenter.default().removeObserver(self)
- if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
- let keys = ["contents", "markupText"]
- for key in keys {
- annotationModel?.annotation.removeObserver(self, forKeyPath: key)
- }
- }
- textColorPickerView.target = nil
- textColorPickerView.action = nil
- borderColorPickerView.target = nil
- borderColorPickerView.action = nil
- fillColorPickerView.target = nil
- fillColorPickerView.action = nil
-
- NSColorPanel.shared.setTarget(nil)
- NSColorPanel.shared.setAction(nil)
- }
-
- // MARK: View Methods
-
- override func loadView() {
- super.loadView()
-
- DispatchQueue.main.async {
- if NSColorPanel.shared.isVisible {
- NSColorPanel.shared.close()
- }
- }
-
- isannotationMode = true
-
- fonts = (CPDFAnnotationModel.supportFonts() as? [[String : [String]]]) ?? []
-
- imageBox.borderColor = KMAppearance.Interactive.s0Color()
- imageBox.borderWidth = 1.0
- imageBox.fillColor = KMAppearance.Layout.l1Color()
- textImageBox.fillColor = KMAppearance.Layout.l1Color()
- fontLabel.stringValue = NSLocalizedString("Fonts", comment: "")
- fontLabel.textColor = KMAppearance.Layout.h0Color()
- colorTitleLabel.stringValue = NSLocalizedString("Color", comment: "")
- colorTitleLabel.textColor = KMAppearance.Layout.h0Color()
- opacityLabel.stringValue = NSLocalizedString("Opacity", comment: "")
- opacityLabel.textColor = KMAppearance.Layout.h1Color()
- noteViewLabel.stringValue = NSLocalizedString("Note", comment: "")
- noteViewLabel.textColor = KMAppearance.Layout.h0Color()
- rotateLabel.stringValue = NSLocalizedString("Rotate", comment: "")
- rotateLabel.textColor = KMAppearance.Layout.h0Color()
- typeLabel.stringValue = NSLocalizedString("Type", comment: "")
- typeLabel.textColor = KMAppearance.Layout.h0Color()
- dateLabel.stringValue = NSLocalizedString("Date", comment: "")
- dateLabel.textColor = KMAppearance.Layout.h0Color()
- iconLabel.stringValue = NSLocalizedString("Icon", comment: "")
- iconLabel.textColor = KMAppearance.Layout.h0Color()
- borderLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
- borderLabel.textColor = KMAppearance.Layout.h0Color()
- fillColorLabel.stringValue = NSLocalizedString("Fill", comment: "")
- fillColorLabel.textColor = KMAppearance.Layout.h0Color()
-
- lineStyleCount = 0
-
- if annotationType == .highlight {
- activeAnnotationType = .highlight
- } else if annotationType == .strikeOut {
- activeAnnotationType = .strikeOut
- } else if annotationType == .underline {
- activeAnnotationType = .underline
- } else if annotationType == .ink || annotationType == .line || annotationType == .arrow {
- activeAnnotationType = .inkAndLine
- } else if annotationType == .anchored {
- activeAnnotationType = .text
- } else if annotationType == .circle || annotationType == .square {
- borderColorPickerView.isCallColorPanelAction = isannotationMode
- textColorPickerView.isCallColorPanelAction = isannotationMode
- activeAnnotationType = .squareAndCircle
- } else if annotationType == .freeText {
- if subType == .date {
- colorTitleLabel.stringValue = NSLocalizedString("Fill Color", comment: "")
- activeAnnotationType = .selfSignFreeText
- } else {
- activeAnnotationType = .freeText
- }
-
- fontViewCorloPV.isCallColorPanelAction = isannotationMode
-
- var alignmentType: KMFreeTextAnnotationAlignmentType = .left
- if annotationModel?.alignment() == .left {
- alignmentType = .left
- } else if annotationModel?.alignment() == .center {
- alignmentType = .center
- } else if annotationModel?.alignment() == .right {
- alignmentType = .right
- }
- alignmentTypeSelected(alignmentType)
- } else if annotationType == .signDate {
- colorTitleLabel.stringValue = NSLocalizedString("Fill Color", comment: "")
- activeAnnotationType = .selfSignFreeText
- fontViewCorloPV.isCallColorPanelAction = isannotationMode
-
- var alignmentType: KMFreeTextAnnotationAlignmentType = .left
- if annotationModel?.alignment() == .left {
- alignmentType = .left
- } else if annotationModel?.alignment() == .center {
- alignmentType = .center
- } else if annotationModel?.alignment() == .right {
- alignmentType = .right
- }
- alignmentTypeSelected(alignmentType)
- }
-
- fillColorPickerView.isCallColorPanelAction = isannotationMode
- textColorPickerView.isCallColorPanelAction = isannotationMode
- borderColorPickerView.isCallColorPanelAction = isannotationMode
-
- if annotationType == .freeText {
- fontViewCorloPV.isFreeText = true
- } else {
- fontViewCorloPV.isFreeText = false
- }
-
- if annotationType == .ink || annotationType == .line || annotationType == .arrow || annotationType == .square || annotationType == .circle {
- let lineType = annotationModel?.style()
- if lineType == .dashed {
- let lineType = annotationModel?.dashPattern()
- // if lineType.count == 0 {
- // let number = NSNumber(value: Float(4))
- // annotation.setDashPattern([number])
- //
- // let userDefaults = UserDefaults.standard
- // if annotationType == .ink {
- // userDefaults.set(annotationModel?.dashPattern(), forKey: SKInkNoteDashPatternKey)
- // } else if annotationType == .line {
- // userDefaults.set(annotationModel?.dashPattern(), forKey: SKLineNoteDashPatternKey)
- // } else if annotationType == .square {
- // userDefaults.set(annotationModel?.dashPattern(), forKey: SKSquareNoteDashPatternKey)
- // } else if annotationType == .circle {
- // userDefaults.set(annotationModel?.dashPattern(), forKey: SKCircleNoteDashPatternKey)
- // } else if annotationType == .freeText {
- // userDefaults.set(annotationModel?.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
- // }
- // }
- }
- }
-
- opacityComboBox.type = .none
- opacityComboBox.comboxRect = opacityComboBox.bounds
-
- lineWidthComboBox.type = .none
- lineWidthComboBox.comboxRect = lineWidthComboBox.bounds
-
- widthComboBox.type = .none
- widthComboBox.comboxRect = widthComboBox.bounds
-
- fontPopUpButton.type = .arrowDown
- fontSizeComboBox.type = .none
- fontSizeComboBox.comboxRect = fontSizeComboBox.bounds
-
- fontStylePopUpButton.type = .arrowUpDown
- datePopUpButton.type = .arrowUpDown
-
- noteTextView.backgroundColor = KMAppearance.Layout.l1Color()
-
- let views: [NSView] = [fontPopUpButton, fontSizeComboBox, fontStylePopUpButton, lineWidthComboBox, opacityComboBox, widthComboBox, datePopUpButton, noteTextView]
- for view in views {
- view.wantsLayer = true
- view.layer?.borderWidth = 1.0
- view.layer?.cornerRadius = 1.0
- }
-
- refreshLineTypeUI()
- rightArrowStyleButton.isHidden = true
- rightArrowStyleBox.isHidden = true
-
- annotationFontReload()
- reloadData()
- updateViewColor()
-
- if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
- let annotation = annotationModel?.annotation
- let keys = ["contents", "markupText"]
- for key in keys {
- annotation?.addObserver(self, forKeyPath: key, options: [.new, .old], context: nil)
- }
- }
- // NotificationCenter.default.addObserver(self, selector: #selector(alignmentTypeNotification(_:)), name: "KMAnnotationAlignmentTypeNotification1", object: nil)
- // NotificationCenter.default.addObserver(self, selector: #selector(loadingUIAndLocalization(_:)), name: "KMPreferenceDidChangeNotificationName", object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(annotationChangeNotification(_:)), name: NSNotification.Name(rawValue: "CPDFListViewAnnotationsAttributeHasChangeNotification"), object: nil)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
- }
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- let showConvertDetails = KMPropertiesViewPopController.showChangeColorDetails()
- let fileURL = (self.view.window?.windowController?.document as? NSDocument)?.fileURL
- if showConvertDetails && (fileURL != nil) {
- KMPropertiesViewPopController.defaultManager.showChangeColorDetailsView(self.textColorPickerView.firstButton)
- }
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
- }
-
- func reloadData() {
- var annotationColor = annotationModel?.color()
- if annotationType == .signFalse ||
- annotationType == .signTure ||
- annotationType == .signDot ||
- annotationType == .signCircle ||
- annotationType == .signLine {
- annotationColor = annotationModel?.color()
- } else if annotationType == .square ||
- annotationType == .circle {
- annotationColor = annotationModel?.color()
- } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
- annotationColor = annotationModel?.interiorColor()
- } else {
- annotationColor = annotationModel?.color()
- }
-
- let color = annotationColor ?? NSColor.black
- // guard let color = annotationColor else { return }
- var opacity: CGFloat = 1.0
- color.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green: nil, blue: nil, alpha: &opacity)
- if annotationType == .signFalse ||
- annotationType == .signTure ||
- annotationType == .signDot ||
- annotationType == .signCircle ||
- annotationType == .signLine {
- opacity = (annotationModel?.opacity()) ?? self.kDefaultOpacity_
- } else if annotationType == .square ||
- annotationType == .circle {
- opacity = (annotationModel?.opacity()) ?? self.kDefaultOpacity_
- } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
- opacity = (annotationModel?.interiorOpacity()) ?? self.kDefaultOpacity_
- } else {
- opacity = (annotationModel?.opacity()) ?? self.kDefaultOpacity_
- }
-
- opacitySlider.floatValue = Float(opacity)
- opacitySlider.toolTip = "\(Int(opacity * 100))%"
- opacityComboBox.stringValue = "\(Int(opacity * 100))%"
-
- if annotationModel?.annotations != nil {
- let contextString = annotation.string() ?? ""
- noteTextView.string = contextString
- noteTextView.textColor = NSColor.labelColor
- noteTextView.delegate = self
- }
-
- if annotationType == .freeText {
- imageBox.contentView = textImageBoxView
- textAnnotationImageView.image = annotationModel?.annotationImage
- } else if annotationType == .signText || annotationType == .signDate {
- imageBox.contentView = generalImageBoxView
- generalImageView.image = annotationModel?.annotationImage
- } else {
- imageBox.contentView = generalImageBoxView
- generalImageView.image = annotationModel?.annotationImage
- }
-
- if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow || annotationType == .ink {
- if annotationType == .ink {
- textColorPickerView.setColor(color)
- } else if annotationType == .square || annotationType == .circle {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- let modelColor = annotationModel?.interiorColor() ?? .black
- modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity()) ?? self.kDefaultOpacity_)
-
- textColorPickerView.setColor(interiorColor)
- textColorPickerView.isFillColor = true
- borderColorPickerView.setColor(color)
- } else if annotationType == .line || annotationType == .arrow {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- let modelColor = annotationModel?.interiorColor() ?? .black
- modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity()) ?? self.kDefaultOpacity_)
- textColorPickerView.setColor(interiorColor)
- textColorPickerView.isFillColor = true
- borderColorPickerView.setColor(color)
- }
- lineWidthSlider.floatValue = Float(annotationModel?.lineWidth() ?? 1.0)
- lineWidthSlider.toolTip = "\(annotationModel?.lineWidth() ?? 1.0) pt"
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- if annotationType == .signText || annotationType == .signDate {
- if subType == .date {
- dateView.isHidden = false
- datePopUpButton.removeAllItems()
- datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: false))
- if let data = annotationModel?.dateFormatIndex(), data >= 0 && data < datePopUpButton.numberOfItems {
- datePopUpButton.selectItem(at: (annotationModel?.dateFormatIndex())!)
- } else {
- datePopUpButton.selectItem(at: 0)
- }
- dateCheckButton.state = (annotationModel?.includeTime() ?? true) ? NSControl.StateValue.on : NSControl.StateValue.off
- textColorPickerView.setColor((annotationModel?.color() ?? .black))
- }
- } else {
- lineWidthSlider.minValue = 0.0
- lineWidthSlider.floatValue = Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- lineWidthSlider.toolTip = String(format: "%0.1f pt", (annotationModel?.lineWidth() ?? self.kDefaultLineWidth_))
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth() ?? self.kDefaultLineWidth_))
- var alignmentType: KMFreeTextAnnotationAlignmentType = .left
- if annotationModel?.alignment() == .left {
- alignmentType = .left
- } else if annotationModel?.alignment() == .center {
- alignmentType = .center
- } else if annotationModel?.alignment() == .right {
- alignmentType = .right
- }
- alignmentTypeSelected(alignmentType)
- annotationFontReload()
- fillColorPickerView.color = annotationModel?.color()
- }
- fontViewCorloPV.color = annotationModel?.fontColor()
- fontSizeComboBox.stringValue = String(format: "%.f pt", (annotationModel?.fontSize() ?? self.kDefaultFontSize_))
- } else if annotationType == .highlight || annotationType == .strikeOut || annotationType == .underline || annotationType == .anchored {
- textColorPickerView.setColor(color)
- }
-
- // noteView.isHidden = isannotationMode
-
- if annotationType == .signText || annotationType == .signDate {
- if subType == .date {
- // noteView.isHidden = true
- isannotationMode = true
- } else {
- // noteView.isHidden = false
- isannotationMode = false
- }
- } else {
- if annotationModel?.annotations != nil {
- // noteView.isHidden = annotationModel?.annotation==nil
- if (annotationModel?.annotations.count)! > 1 {
- isannotationMode = true
- } else {
- isannotationMode = annotationModel?.annotation==nil
- }
- } else {
- if annotationType == .stamp ||
- annotationType == .ink ||
- annotationType == .square ||
- annotationType == .circle ||
- annotationType == .arrow ||
- annotationType == .line ||
- annotationType == .highlight ||
- annotationType == .strikeOut ||
- annotationType == .underline ||
- annotationType == .freeText {
- // noteView.isHidden = true
- isannotationMode = true
- } else {
- // noteView.isHidden = annotationModel?.annotation==nil
- isannotationMode = annotationModel?.annotation==nil
- }
- }
- }
-
- if isannotationMode {
- if noteView.isHidden == false {
- noteView.isHidden = isannotationMode
- }
- } else {
- if noteView.isHidden {
- noteView.isHidden = isannotationMode
- }
- }
- }
-
- // MARK: Set & Get
-
- var annotations: [CPDFAnnotation] {
- set {
- print(annotations)
- }
- get {
- return annotationModel?.annotations as? [CPDFAnnotation] ?? []
- }
- }
-
- var annotation: CPDFAnnotation {
- get {
- return annotationModel?.annotation ?? CPDFAnnotation()
- }
- }
-
- var annotationType: CAnnotationType {
- get {
- return (annotationModel?.annotationType as? CAnnotationType) ?? .unkown
- }
- }
-
- func initAnnotationMode() {
- if annotationModel?.annotationType == .freeText {
- let sud = UserDefaults.standard
- let bounds = NSMakeRect(0, 0, 50, 50)
- let annotation = CPDFFreeTextAnnotation()
- // defaults.safe_setColor(annotation.color, forKey: CFreeTextNoteColorKey)
- // defaults.safe_setColor((annotation as! CPDFFreeTextAnnotation).fontColor, forKey: CFreeTextNoteFontColorKey)
- // defaults.set((annotation as! CPDFFreeTextAnnotation).alignment.rawValue, forKey: CFreeTextNoteAlignmentKey)
- // defaults.set((annotation as! CPDFFreeTextAnnotation).fontName(), forKey: CFreeTextNoteFontNameKey)
- // defaults.set((annotation as! CPDFFreeTextAnnotation).fontSize(), forKey: CFreeTextNoteFontSizeKey)
- // defaults.set(annotation.lineWidth(), forKey: CFreeTextNoteLineWidthKey)
- // defaults.set(annotation.borderStyle().rawValue, forKey: CFreeTextNoteLineStyleKey)
- // defaults.set(annotation.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
- annotation.bounds = bounds;
- annotation.color = sud.color(forKey: CFreeTextNoteColorKey)
- annotation.fontColor = sud.color(forKey: CFreeTextNoteFontColorKey)
- annotation.alignment = NSTextAlignment(rawValue: sud.integer(forKey: CFreeTextNoteAlignmentKey)) ?? .left
- let font = sud.font(forNameKey: CFreeTextNoteFontNameKey, sizeKey: CFreeTextNoteFontSizeKey)
- if (font != nil) {
- annotation.font = font
- }
- annotation.setLineWidth(CGFloat(sud.float(forKey: CFreeTextNoteLineWidthKey)))
- annotation.setDashPattern(sud.array(forKey: CFreeTextNoteDashPatternKey) ?? [])
- annotation.setBorderStyle(CPDFBorderStyle(rawValue: sud.integer(forKey: CFreeTextNoteLineStyleKey)) ?? .solid)
- // annotationModel?.annotations = [annotation]
- }
- }
-
- func updateannotationMode() {
- let defaults = UserDefaults.standard
- if annotationType == .freeText {
- // if ([annotation isKindOfClass:[SKNPDFAnnotationNote class]]) {
- // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
- // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
- // }
- if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
- defaults.safe_setColor(annotation.color, forKey: CFreeTextNoteColorKey)
- defaults.safe_setColor((annotation as! CPDFFreeTextAnnotation).fontColor, forKey: CFreeTextNoteFontColorKey)
- defaults.set((annotation as! CPDFFreeTextAnnotation).alignment.rawValue, forKey: CFreeTextNoteAlignmentKey)
- defaults.set((annotation as! CPDFFreeTextAnnotation).fontName(), forKey: CFreeTextNoteFontNameKey)
- defaults.set((annotation as! CPDFFreeTextAnnotation).fontSize(), forKey: CFreeTextNoteFontSizeKey)
- defaults.set(annotation.lineWidth(), forKey: CFreeTextNoteLineWidthKey)
- defaults.set(annotation.borderStyle().rawValue, forKey: CFreeTextNoteLineStyleKey)
- defaults.set(annotation.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
- }
- } else if annotationType == .circle {
- defaults.safe_setColor(annotation.color, forKey: CCircleNoteColorKey)
- defaults.safe_setColor((annotation as? CPDFCircleAnnotation)?.interiorColor ?? .black, forKey: CCircleNoteInteriorColorKey)
- defaults.set(annotation.lineWidth(), forKey: CCircleNoteLineWidthKey)
- if let data = annotation as? CPDFCircleAnnotation {
- defaults.set(data.borderStyle().rawValue, forKey: CCircleNoteLineStyleKey)
- defaults.set(data.dashPattern(), forKey: CCircleNoteDashPatternKey)
- }
- } else if annotationType == .square {
- defaults.safe_setColor(annotation.color, forKey: CSquareNoteColorKey)
- defaults.safe_setColor((annotation as? CPDFSquareAnnotation)?.interiorColor ?? .black, forKey: CSquareNoteInteriorColorKey)
- defaults.set(annotation.lineWidth(), forKey: CSquareNoteLineWidthKey)
- if let data = annotation as? CPDFSquareAnnotation {
- defaults.set(data.borderStyle().rawValue, forKey: CSquareNoteLineStyleKey)
- defaults.set(data.dashPattern(), forKey: CSquareNoteDashPatternKey)
- }
- } else if annotationType == .highlight {
- defaults.safe_setColor(annotation.color, forKey: CHighlightNoteColorKey)
- } else if annotationType == .underline {
- defaults.safe_setColor(annotation.color, forKey: CUnderlineNoteColorKey)
- } else if annotationType == .strikeOut {
- defaults.safe_setColor(annotation.color, forKey: CStrikeOutNoteColorKey)
- } else if annotationType == .line || annotationType == .arrow {
- defaults.safe_setColor(annotation.color, forKey: CLineNoteColorKey)
- defaults.set(annotation.lineWidth(), forKey: CLineNoteLineWidthKey)
- defaults.set(annotation.borderStyle().rawValue, forKey: CLineNoteLineStyleKey)
- defaults.set(annotation.dashPattern(), forKey: CLineNoteDashPatternKey)
- if let data = annotation as? CPDFLineAnnotation {
- defaults.safe_setColor(data.interiorColor, forKey: CLineNoteInteriorColorKey)
- defaults.set(data.startLineStyle.rawValue, forKey: CLineNoteStartLineStyleKey)
- defaults.set(data.endLineStyle.rawValue, forKey: CLineNoteEndLineStyleKey)
- }
- } else if annotationType == .ink {
- defaults.safe_setColor(annotation.color, forKey: CInkNoteColorKey)
- defaults.set(annotation.lineWidth(), forKey: CInkNoteLineWidthKey)
- defaults.set(annotation.borderStyle().rawValue, forKey: CInkNoteLineStyleKey)
- defaults.set(annotation.dashPattern(), forKey: CInkNoteDashPatternKey)
- } else if annotationType == .anchored {
- // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
- // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
- }
- }
-
- var activeAnnotationType: KMActiveAnnotationType {
- set {
- _activeAnnotationType = newValue
- switch self.activeAnnotationType {
- case .highlight, .strikeOut, .underline:
- self.fontView.isHidden = true
- self.borderAndLineView.isHidden = true
- self.fillColorView.isHidden = true
- self.rotateView.isHidden = true
- self.typeView.isHidden = true
- self.dateView.isHidden = true
- self.iconView.isHidden = true
- self.createMarkupProperties()
- case .inkAndLine:
- self.fontView.isHidden = true
- self.fillColorView.isHidden = true
- self.rotateView.isHidden = true
- self.typeView.isHidden = true
- self.dateView.isHidden = true
- self.iconView.isHidden = true
- self.createInkAndLineProperties()
- case .text:
- self.fontView.isHidden = true
- self.borderAndLineView.isHidden = true
- self.fillColorView.isHidden = true
- self.rotateView.isHidden = true
- self.typeView.isHidden = true
- self.dateView.isHidden = true
- self.createTextProperties()
- case .squareAndCircle:
- self.fontView.isHidden = true
- self.rotateView.isHidden = true
- self.typeView.isHidden = true
- self.dateView.isHidden = true
- self.iconView.isHidden = true
- self.fillColorView.isHidden = true
- self.createSquareAndCircleProperties()
- case .freeText:
- self.colorView.isHidden = true
- self.rotateView.isHidden = true
- self.typeView.isHidden = true
- self.dateView.isHidden = true
- self.iconView.isHidden = true
- self.createFreeTextProperties()
- case .selfSignFreeText:
- self.rotateView.isHidden = true
- self.borderAndLineView.isHidden = true
- self.fillColorView.isHidden = true
- self.typeView.isHidden = true
- self.iconView.isHidden = true
- self.createSelfSignFreeTextProperties()
- default:
- break
- }
- }
- get {
- return _activeAnnotationType
- }
- }
- // MARK: private
-
- func updateViewColor() {
- if KMAppearance.isDarkMode() {
- let darkColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
- let borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
-
- setViewColor(fontPopUpButton, darkColor, borderColor)
- setViewColor(fontSizeComboBox, darkColor, borderColor)
- setViewColor(fontStylePopUpButton, darkColor, borderColor)
- setViewColor(lineWidthComboBox, darkColor, borderColor)
- setViewColor(opacityComboBox, darkColor, borderColor)
- setViewColor(datePopUpButton, darkColor, borderColor)
- setViewColor(widthComboBox, darkColor, borderColor)
- setViewColor(noteTextView, darkColor, borderColor)
- } else {
- let lightColor = NSColor.white.cgColor
- let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
-
- setViewColor(fontPopUpButton, lightColor, borderColor)
- setViewColor(fontSizeComboBox, lightColor, borderColor)
- setViewColor(fontStylePopUpButton, lightColor, borderColor)
- setViewColor(lineWidthComboBox, lightColor, borderColor)
- setViewColor(opacityComboBox, lightColor, borderColor)
- setViewColor(datePopUpButton, lightColor, borderColor)
- setViewColor(widthComboBox, lightColor, borderColor)
- setViewColor(noteTextView, lightColor, borderColor)
- }
- }
- func setViewColor(_ view: NSView, _ backgroundColor: CGColor, _ borderColor: CGColor) {
- view.layer?.backgroundColor = backgroundColor
- view.layer?.borderColor = borderColor
- }
-
- func refreshLineTypeUI() {
- lineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
- dottedLineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
- rightArrowStyleBox.borderColor = KMAppearance.Interactive.s0Color()
- lineStyleBox.fillColor = KMAppearance.Layout.l1Color()
- dottedLineStyleBox.fillColor = KMAppearance.Layout.l1Color()
- rightArrowStyleBox.fillColor = KMAppearance.Layout.l1Color()
- lineStyleBox.borderWidth = 1
- dottedLineStyleBox.borderWidth = 1
- rightArrowStyleBox.borderWidth = 1
-
- if lineStyleCount == 0 {
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- lineStyleBox.borderWidth = 0.0
- } else if lineStyleCount == 1 {
- dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
- dottedLineStyleBox.borderWidth = 0
- } else if lineStyleCount == 2 {
- rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
- rightArrowStyleBox.borderWidth = 0
- }
- }
- func hiddenSubviews() {
- fontViewTopConstant.constant = fontView.isHidden ? -(fontView.bounds.size.height) - 20 : 10
- ColorViewTopConstant.constant = colorView.isHidden ? -(colorView.bounds.size.height) : 10
- borderViewTopConstant.constant = borderAndLineView.isHidden ? -(borderAndLineView.bounds.size.height) : 20
- opacityViewTopConstant.constant = opacityView.isHidden ? -opacityView.bounds.size.height : 20
- rotateViewTopConstant.constant = rotateView.isHidden ? -(rotateView.bounds.size.height) : 20
- typeViewTopConstant.constant = typeView.isHidden ? -(typeView.bounds.size.height) : 20
- dateViewTopConstant.constant = dateView.isHidden ? -(dateView.bounds.size.height) : 20
- iconViewTopConstant.constant = iconView.isHidden ? -(iconView.bounds.size.height) : 20
- noteViewTopConstant.constant = noteView.isHidden ? -(noteView.bounds.size.height) : 20
- fillColorViewTopConstraint.constant = fillColorView.isHidden ? -fillColorView.bounds.size.height : 20
- fontViewColorPVConstraint.constant = fontViewCorloPV.isHidden ? -fontViewCorloPV.bounds.size.height : 10
- }
-
- func setActiveAnnotationType(_ activeAnnotationType: KMActiveAnnotationType) {
- self.activeAnnotationType = activeAnnotationType
-
- switch activeAnnotationType {
- case .highlight, .strikeOut, .underline:
- fontView.isHidden = true
- borderAndLineView.isHidden = true
- fillColorView.isHidden = true
- rotateView.isHidden = true
- typeView.isHidden = true
- dateView.isHidden = true
- iconView.isHidden = true
- createMarkupProperties()
- case .inkAndLine:
- fontView.isHidden = true
- fillColorView.isHidden = true
- rotateView.isHidden = true
- typeView.isHidden = true
- dateView.isHidden = true
- iconView.isHidden = true
- createInkAndLineProperties()
- case .text:
- fontView.isHidden = true
- borderAndLineView.isHidden = true
- fillColorView.isHidden = true
- rotateView.isHidden = true
- typeView.isHidden = true
- dateView.isHidden = true
- createTextProperties()
- case .squareAndCircle:
- fontView.isHidden = true
- rotateView.isHidden = true
- typeView.isHidden = true
- dateView.isHidden = true
- iconView.isHidden = true
- fillColorView.isHidden = true
- createSquareAndCircleProperties()
- case .freeText:
- colorView.isHidden = true
- rotateView.isHidden = true
- typeView.isHidden = true
- dateView.isHidden = true
- iconView.isHidden = true
- createFreeTextProperties()
- case .selfSignFreeText:
- rotateView.isHidden = true
- borderAndLineView.isHidden = true
- fillColorView.isHidden = true
- typeView.isHidden = true
- iconView.isHidden = true
- createSelfSignFreeTextProperties()
- default:
- break
- }
- }
-
- func createMarkupProperties() {
- let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
- colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = 10
- borderColorBoxConstraint.constant = 0
- textColorBox.isHidden = true
- lineWidthBox.isHidden = true
- imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
-
- hiddenSubviews()
- textColorPickerView.noContentString = true
-
- if activeAnnotationType == .highlight {
- textColorPickerView.annotationTypeString = NSLocalizedString("Highlight", comment: "Description for export")
- } else if activeAnnotationType == .strikeOut {
- textColorPickerView.annotationTypeString = NSLocalizedString("Strikethrough", comment: "Description for export")
- } else if activeAnnotationType == .underline {
- textColorPickerView.annotationTypeString = NSLocalizedString("Underline", comment: "Description for export")
- }
-
- textColorPickerView.annotationType = (activeAnnotationType == .highlight) ? .markupHighlightColors : .markupOtherColors
- }
-
- func openFreeTextStylesViewController() {
- let vc = KMFreeTextStylesViewController()
- vc.annotations = annotations
- vc.annotationModel = self.annotationModel
- vc.view.frame = CGRect(x: 0, y: 0, width: 280, height: CGRectGetHeight(self.view.window?.frame ?? CGRect.zero) - 300)
- vc.didSelect = { [unowned self] data in
- self.updateAnnotation()
- }
-
- let createFilePopover = NSPopover()
- createFilePopover.contentViewController = vc
- createFilePopover.animates = true
- createFilePopover.behavior = .transient
- 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)
-
- // Assuming _textImageBox is an NSView instance
- createFilePopover.show(relativeTo: textImageBox.bounds, of: textImageBox, preferredEdge: .minY)
-
- // Release is not needed in Swift due to automatic reference counting (ARC)
- }
- func changeStoredFontInfo(_ sender: Any) {
- // if let senderFont = sender as? NSFont {
- // for tAnnotation in self.annotations {
- // tAnnotation.removeAllAppearanceStreams()
- // let font = senderFont.convert(tAnnotation.font)
- // if let validFont = font {
- // tAnnotation.font = validFont
- // }
- // }
- // }
- }
-
- private func updateAnnotation(_ type: KMRefreshType = .none) {
- if annotationModel?.annotation != nil {
- if annotation is CPDFFreeTextAnnotation {
- let textNote = (annotation as! CPDFFreeTextAnnotation)
- if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
- pdfView?.commitEditAnnotationFreeText(textNote)
- // pdfView?.editAnnotationFreeText(textNote)
- }
-
- pdfView?.setNeedsDisplay(pdfView?.activeAnnotation)
- } else if annotationModel?.annotation is CSelfSignAnnotationFreeText {
- let textNote = (annotation as! CSelfSignAnnotationFreeText)
- if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
- pdfView?.editAnnotationFreeText(textNote)
- }
- pdfView?.setNeedsDisplay(pdfView?.activeAnnotation)
- }
- for tAnnotation in annotations {
- if tAnnotation is CPDFStampAnnotation {
- (tAnnotation as? KMSelfSignAnnotation)?.updateAppearanceStream()
- }
-
- if tAnnotation is CPDFMarkupAnnotation {
- pdfView?.setNeedsDisplayFor(tAnnotation.page)
- } else {
- pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
- }
- }
- }
- refreshColorPickerView(type)
- if annotationType == .freeText {
- textAnnotationImageView.image = annotationModel?.annotationImage
- } else {
- generalImageView.image = annotationModel?.annotationImage
- }
- }
-
- private func refreshColorPickerView(_ type: KMRefreshType) {
- if type == .color {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- if annotationType == .highlight || annotationType == .underline || annotationType == .strikeOut ||
- annotationType == .ink ||
- annotationType == .freeText ||
- annotationType == .anchored ||
- annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
- let modelOpcity = annotationModel?.opacity() ?? 1.0
-
- opacitySlider.floatValue = Float(modelOpcity)
- opacitySlider.toolTip = "\(Int(modelOpcity * 100))%"
- opacityComboBox.stringValue = "\(Int(modelOpcity * 100))%"
- }
- } else if type == .opacity {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- if annotationType == .highlight ||
- annotationType == .underline ||
- annotationType == .strikeOut ||
- annotationType == .ink ||
- annotationType == .anchored {
- let modelColor = annotationModel?.color() ?? .red
- modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- let modelOpcity = annotationModel?.opacity() ?? 1.0
-
- textColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- opacitySlider.floatValue = Float(modelOpcity)
- opacitySlider.toolTip = "\(Int(modelOpcity * 100))%"
- opacityComboBox.stringValue = "\(Int(modelOpcity * 100))%"
- } else if annotationType == .freeText {
- let modelOpcity = annotationModel?.opacity() ?? 1.0
- let fontColor = annotationModel?.fontColor() ?? .black
- fontColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- fontViewCorloPV.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
-
- let fillColor = annotationModel?.color() ?? .red
- fillColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- fillColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- } else if annotationType == .square || annotationType == .circle {
- let modelOpcity = annotationModel?.opacity() ?? 1.0
- let lineColor = annotationModel?.interiorColor() ?? .red
- lineColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- textColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- let fillColor = annotationModel?.color() ?? .clear
- fillColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- borderColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- } else if annotationType == .line || annotationType == .arrow {
- let modelOpcity = annotationModel?.opacity() ?? 1.0
- let lineColor = annotationModel?.color() ?? .red
- lineColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- borderColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- let fillColor = annotationModel?.interiorColor() ?? .clear
- fillColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- textColorPickerView.setColor(NSColor(red: red, green: green, blue: blue, alpha: modelOpcity))
- }
- }
- }
-
- override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
- if self.annotation.isEqual(to: object) == false {
- return
- }
- let newValue = change?[NSKeyValueChangeKey.newKey] as? NSObject
- let oldValue = change?[NSKeyValueChangeKey.oldKey]
- if let data = newValue?.isEqual(to: oldValue), data {
- return
- }
- if keyPath == "string" || keyPath == "contents"{
- if newValue == nil || newValue is NSNull {
- return
- }
- //处理编辑时光标后移
- if self.isTextEdit == false {
- self.noteTextView.string = newValue as? String ?? ""
- }
- self.isTextEdit = false
- } else {
- self.reloadData()
- if self.isannotationMode {
- self.updateannotationMode()
- }
- }
- }
-
- func updateDateView() -> Void {
- datePopUpButton.removeAllItems()
- let includeTime = annotationModel?.includeTime() ?? false
- datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: includeTime))
- let dateFormatIndex = annotationModel?.dateFormatIndex() ?? 0
- if dateFormatIndex >= 0 && dateFormatIndex < datePopUpButton.numberOfItems {
- datePopUpButton.selectItem(at: dateFormatIndex)
- } else {
- datePopUpButton.selectItem(at: 0)
- }
- dateCheckButton.state = includeTime ? .on : .off
- }
-
- // MARK: Line Note
-
- func createInkAndLineProperties() {
- if annotationType == .ink {
- let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
- colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = 10
- borderColorBoxConstraint.constant = 0
- textColorBox.isHidden = true
- lineWidthBox.isHidden = true
- imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
- hiddenSubviews()
- textColorPickerView.noContentString = true
- textColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
- textColorPickerView.annotationType = .inkColors
- } else if annotationType == .line || annotationType == .arrow {
- let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
- colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
- borderColorBoxConstraint.constant = 0
- lineWidthBox.isHidden = true
- textColorPickerViewConstant.constant = 57
- imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
- hiddenSubviews()
- borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
- textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
- borderColorPickerView.annotationType = .lineColors
- textColorPickerView.annotationType = .lineFillColors
- }
- lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
- lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
- lineWidthSlider.floatValue = Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- lineStyleButton.wantsLayer = true
- dottedLineStyleButton.wantsLayer = true
- rightArrowStyleButton.wantsLayer = true
- lineStyleButton.image = borderStyleSolid(false)
- dottedLineStyleButton.image = borderStyleDashed(false)
- rightArrowStyleButton.image = lineStyleRightArrow(false)
- if annotationModel?.style() == .solid {
- lineStyleCount = 0
- lineStyleButton.image = borderStyleSolid(true)
- lineStyleBox.borderWidth = 0
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- } else if annotationModel?.style() == .dashed {
- lineStyleCount = 1
- dottedLineStyleButton.image = borderStyleDashed(true)
- dottedLineStyleBox.borderWidth = 0
- dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
- }
-
- if (annotationType == .line || annotationType == .arrow) && annotationModel?.endLineStyle() == .closedArrow {
- lineStyleCount = 2
- rightArrowStyleButton.image = lineStyleRightArrow(true)
- rightArrowStyleBox.borderWidth = 0
- rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
- }
- }
-
- func createTextProperties() {
- makeNoteIcons()
- let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
- colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = 10
- borderColorBoxConstraint.constant = 0
- textColorBox.isHidden = true
- lineWidthBox.isHidden = true
- imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
- hiddenSubviews()
- textColorPickerView.annotationTypeString = NSLocalizedString("Anchored Note", comment: "Description for export")
- commentButton.title = NSLocalizedString("Comment", comment: "")
- commentButton.toolTip = NSLocalizedString("Comment", comment: "")
- commentButton.setTitleColor(KMAppearance.Layout.h0Color())
- paragraphButton.title = NSLocalizedString("Paragraph", comment: "")
- paragraphButton.toolTip = NSLocalizedString("Paragraph", comment: "")
- paragraphButton.setTitleColor(KMAppearance.Layout.h0Color())
- insertButton.title = NSLocalizedString("Insert", comment: "")
- insertButton.toolTip = NSLocalizedString("Insert", comment: "")
- insertButton.setTitleColor(KMAppearance.Layout.h0Color())
- addParagraphButton.title = NSLocalizedString("New Paragraph", comment: "")
- addParagraphButton.toolTip = NSLocalizedString("New Paragraph", comment: "")
- addParagraphButton.setTitleColor(KMAppearance.Layout.h0Color())
- keyButton.title = NSLocalizedString("Key", comment: "")
- keyButton.toolTip = NSLocalizedString("Key", comment: "")
- keyButton.setTitleColor(KMAppearance.Layout.h0Color())
- noteButton.title = NSLocalizedString("Note", comment: "")
- noteButton.toolTip = NSLocalizedString("Note", comment: "")
- noteButton.setTitleColor(KMAppearance.Layout.h0Color())
- helpButton.title = NSLocalizedString("Help", comment: "")
- helpButton.toolTip = NSLocalizedString("Help", comment: "")
- helpButton.setTitleColor(KMAppearance.Layout.h0Color())
- // let languages = NSLocale.preferredLanguages
- var currentLocaleLanguageCode = NSLocalizedString("en-new", comment: "")
- // if let firstLanguage = languages.first {
- // currentLocaleLanguageCode = firstLanguage
- // if currentLocaleLanguageCode.hasPrefix("zh") {
- // currentLocaleLanguageCode = "zh"
- // }
- // }
- let buttonArray: [NSButton] = [commentButton, keyButton, noteButton, helpButton, addParagraphButton, paragraphButton, insertButton]
- for button in buttonArray {
- button.wantsLayer = true
- button.layer?.cornerRadius = 6.0
- if currentLocaleLanguageCode == "zh" {
- iconButtonHeightConstant.constant = 56.0
- button.imagePosition = .imageAbove
- } else {
- iconButtonHeightConstant.constant = 50.0
- button.imagePosition = .imageOnly
- }
- }
- iconButtonArray = [commentBox, keyBox, noteBox, helpBox, addParagraphBox, paragraphBox, insertBox]
- if let data = annotationModel?.anchoredIconType().rawValue {
- let iconType = UInt(data)
- clickAnnotationTextIconButtonAction(iconType: iconType, boxArr: iconButtonArray)
- } else {
- clickAnnotationTextIconButtonAction(iconType: 0, boxArr: iconButtonArray)
- }
- textColorPickerView.annotationType = .anchoredNoteColors
- textColorPickerView.isCallColorPanelAction = true
- }
-
- func clickAnnotationTextIconButtonAction(iconType: UInt, boxArr buttonArr: [NSBox]) {
- for i in 0..<buttonArr.count {
- if iconType == 0 {
- commentButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationSel)
- } else {
- commentButton.layer?.backgroundColor = NSColor.clear.cgColor
- commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationNor)
- }
-
- if iconType == 1 {
- keyButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordSel)
- } else {
- keyButton.layer?.backgroundColor = NSColor.clear.cgColor
- keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordNor)
- }
-
- if iconType == 2 {
- noteButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesSel)
- } else {
- noteButton.layer?.backgroundColor = NSColor.clear.cgColor
- noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesNor)
- }
-
- if iconType == 3 {
- helpButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpSel)
- } else {
- helpButton.layer?.backgroundColor = NSColor.clear.cgColor
- helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpNor)
- }
-
- if iconType == 4 {
- addParagraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphSel)
- } else {
- addParagraphButton.layer?.backgroundColor = NSColor.clear.cgColor
- addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphNor)
- }
-
- if iconType == 5 {
- paragraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphSel)
- } else {
- paragraphButton.layer?.backgroundColor = NSColor.clear.cgColor
- paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphNor)
- }
-
- if iconType == 6 {
- insertButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertSel)
- } else {
- insertButton.layer?.backgroundColor = NSColor.clear.cgColor
- insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertNor)
- }
- }
- }
-
- func borderStyleSolid(_ isSelect: Bool) -> NSImage {
- let size = NSSize(width: 49.0, height: 12.0)
- let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
- let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
- path.lineWidth = 2.0
- isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
- path.stroke()
- return true
- })
- return image
- }
- func borderStyleDashed(_ isSelect: Bool) -> NSImage {
- let size = NSSize(width: 49.0, height: 12.0)
- let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
- let path = NSBezierPath()
- path.move(to: NSPoint(x: 15.0, y: 5.0))
- path.line(to: NSPoint(x: 19.0, y: 5.0))
- path.move(to: NSPoint(x: 23.0, y: 5.0))
- path.line(to: NSPoint(x: 27.0, y: 5.0))
- path.move(to: NSPoint(x: 31.0, y: 5.0))
- path.line(to: NSPoint(x: 35.0, y: 5.0))
- path.lineWidth = 2.0
- isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
- path.stroke()
- return true
- })
- return image
- }
-
- func lineStyleRightArrow(_ isSelect: Bool) -> NSImage {
- let size = NSSize(width: 49.0, height: 12.0)
-
- let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
- let path = NSBezierPath()
- path.move(to: NSPoint(x: 6.0, y: 6.0))
- path.line(to: NSPoint(x: 43.0, y: 6.0))
- path.move(to: NSPoint(x: 32.0, y: 3.0))
- path.line(to: NSPoint(x: 43.0, y: 6.0))
- path.line(to: NSPoint(x: 32.0, y: 9.0))
- path.close()
- path.lineWidth = 2.0
-
- if isSelect {
- KMAppearance.Layout.l0Color().setStroke()
- } else {
- KMAppearance.Interactive.s0Color().setStroke()
- }
- path.stroke()
- return true
- })
- return image
- }
-
- // MARK: SKSquareNote、SKCircleNote、SKArrowNote Properties
-
- func createSquareAndCircleProperties() {
- let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
- colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
- borderColorBoxConstraint.constant = 0
- lineWidthBox.isHidden = true
- textColorPickerViewConstant.constant = 57
-
- imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
-
- hiddenSubviews()
-
- lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
- lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
- lineWidthSlider.floatValue = Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
-
- textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
- borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
- textColorPickerView.annotationType = .lineFillColors
- borderColorPickerView.annotationType = .lineColors
-
- lineStyleButton.wantsLayer = true
- dottedLineStyleButton.wantsLayer = true
- rightArrowStyleButton.wantsLayer = true
- lineStyleButton.image = borderStyleSolid(false)
- dottedLineStyleButton.image = borderStyleDashed(false)
- if annotationModel?.annotation != nil {
- if annotationModel?.annotation.borderStyle() == .solid {
- lineStyleCount = 0
- lineStyleButton.image = borderStyleSolid(true)
-
- lineStyleBox.borderWidth = 0
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- } else if annotationModel?.annotation.borderStyle() == .dashed {
- lineStyleCount = 1
- dottedLineStyleButton.image = borderStyleDashed(true)
-
- dottedLineStyleBox.borderWidth = 1
- dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
- }
- } else {
- if annotationModel?.style() == .solid {
- lineStyleCount = 0
- lineStyleButton.image = borderStyleSolid(true)
-
- lineStyleBox.borderWidth = 0
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- } else if annotationModel?.style() == .dashed {
- lineStyleCount = 1
- dottedLineStyleButton.image = borderStyleDashed(true)
-
- dottedLineStyleBox.borderWidth = 1
- dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
- }
- }
- }
-
- // MARK: SKFreeTextNote、PDFAnnotationFreeText Properties
-
- func createFreeTextProperties() {
- imageBox.fillColor = KMAppearance.Layout.w0Color()
-
- textColorBox.isHidden = true
- lineWidthBox.isHidden = true
-
- imageBoxLayoutConstraint.constant = CGRectGetHeight(textImageBoxView.frame)
-
- textImageBox.downCallback = { [weak self] downEntered, mouseBox, event in
- if downEntered {
- self?.openFreeTextStylesViewController()
- }
- }
-
- textImageUpDateButton.wantsLayer = true
- textImageBoxButton.image = NSImage(named: KMImageNameUXIconBtnTriDownNor)
- textImageBoxButton.isEnabled = true
- textImageUpDateButton.title = NSLocalizedString("Click to refresh", comment: "")
- textImageUpDateButton.setTitleColor(KMAppearance.Layout.w0Color())
- textImageUpDateButton.layer?.cornerRadius = 1.0
- textImageUpDateButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
- textImageUpDateButton.isHidden = true
-
- hiddenSubviews()
-
- fontViewCorloPV.noContentString = true
- fontViewCorloPV.annotationTypeString = NSLocalizedString("Color", comment: "")
- fillColorPickerView.noContentString = true
- fillColorPickerView.annotationTypeString = NSLocalizedString("Color", comment: "")
- lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
- lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
- leftAlignButton.toolTip = NSLocalizedString("Left Alignment", comment: "")
- centerAlignButton.toolTip = NSLocalizedString("Center", comment: "")
- rightAlignButton.toolTip = NSLocalizedString("Right Alignment", comment: "")
- lineWidthSlider.floatValue = Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_))
- leftRotateBox.borderColor = KMAppearance.Interactive.s0Color()
- leftRotateBox.borderWidth = 0.5
- leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
- leftRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
- if downEntered {
- self?.leftRotateBox.fillColor = KMAppearance.Status.selColor()
- self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwisePre)
- } else {
- self?.leftRotateBox.fillColor = KMAppearance.Layout.w0Color()
- self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
- }
- }
-
- rightRotateBox.borderColor = KMAppearance.Interactive.s0Color()
- rightRotateBox.borderWidth = 0.5
- rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
-
- rightRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
- if downEntered {
- self?.rightRotateBox.fillColor = KMAppearance.Status.selColor()
- self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwisePre)
- } else {
- self?.rightRotateBox.fillColor = KMAppearance.Layout.w0Color()
- self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
- }
- }
-
- lineStyleButton.wantsLayer = true
- dottedLineStyleButton.wantsLayer = true
- rightArrowStyleButton.wantsLayer = true
- lineStyleButton.image = borderStyleSolid(false)
- dottedLineStyleButton.image = borderStyleDashed(false)
- if annotationModel?.style() == .solid {
- lineStyleCount = 0
- lineStyleButton.image = borderStyleSolid(true)
- lineStyleBox.borderWidth = 0
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- lineStyleBox.borderWidth = 0
- } else if annotationModel?.style() == .dashed {
- lineStyleCount = 1
- dottedLineStyleButton.image = borderStyleDashed(true)
- dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
- dottedLineStyleBox.borderWidth = 0
- } else {
- lineStyleCount = 0
- lineStyleButton.image = borderStyleSolid(true)
- lineStyleBox.borderWidth = 0
- lineStyleBox.fillColor = KMAppearance.Status.selColor()
- lineStyleBox.borderWidth = 0
- }
-
- fontViewCorloPV.annotationType = .freeTextColors
- fillColorPickerView.annotationType = .freeTextFillColors
- }
-
- func alignmentTypeSelected(_ type: KMFreeTextAnnotationAlignmentType) {
- annotationAlignment = type
- selectedAlignmentButtonLayer?.removeFromSuperlayer()
- selectedAlignmentButtonLayer = CALayer()
- selectedAlignmentButtonLayer?.cornerRadius = 6.0
- leftAlignButton.wantsLayer = true
- centerAlignButton.wantsLayer = true
- rightAlignButton.wantsLayer = true
- selectedAlignmentButtonLayer?.bounds = leftAlignButton.layer?.bounds ?? CGRect.zero
- selectedAlignmentButtonLayer?.anchorPoint = CGPoint(x: 0, y: 0)
- var color = NSColor(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 0.16)
- if #available(macOS 10.14, *) {
- let appearanceName = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua])
- if appearanceName == .darkAqua {
- color = NSColor(red: 34/255.0, green: 122/255.0, blue: 255/255.0, alpha: 0.3)
- }
- }
- selectedAlignmentButtonLayer?.backgroundColor = color.cgColor
- leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftNor)
- centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterNor)
- rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightNor)
- if type == .left {
- leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftSel)
- } else if type == .center {
- centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterSel)
- } else if type == .right {
- rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightSel)
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { [weak self] in
- guard let self = self else { return }
- guard let data = self.selectedAlignmentButtonLayer else {
- return
- }
- if type == .left {
- self.leftAlignButton.layer?.insertSublayer(data, at: 0)
- } else if type == .center {
- self.centerAlignButton.layer?.insertSublayer(data, at: 0)
- } else if type == .right {
- self.rightAlignButton.layer?.insertSublayer(data, at: 0)
- }
- }
- }
-
- // MARK: SKFreeTextNote、KMSelfSignAnnotationFreeText Properties
-
- func createSelfSignFreeTextProperties() {
- let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
- colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
- textColorBoxConstraint.constant = 10
- borderColorBoxConstraint.constant = 0
- textColorBox.isHidden = true
- lineWidthBox.isHidden = true
- imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
- hiddenSubviews()
- fontViewTopConstant.constant = colorView.frame.size.height + opacityView.frame.size.height - 10 + 10
- ColorViewTopConstant.constant = -(fontView.frame.size.height + fontViewTopConstant.constant)
- dateViewTopConstant.constant = fontView.frame.size.height + 30
- fontViewCorloPV.noContentString = true
- fontViewCorloPV.annotationTypeString = ""
- textColorPickerView.annotationTypeString = NSLocalizedString("Background", comment: "")
- dateCheckButton.title = NSLocalizedString("Time", comment: "")
-
- if let attrTitle = dateCheckButton.attributedTitle.mutableCopy() as? NSMutableAttributedString {
- let len = attrTitle.length
- let range = NSRange(location: 0, length: len)
- attrTitle.addAttribute(.foregroundColor, value: KMAppearance.Layout.h1Color(), range: range)
- attrTitle.fixAttributes(in: range)
- dateCheckButton.attributedTitle = attrTitle
- }
- if activeAnnotationType == .selfSignFreeText { // 添加日期 字体默认颜色不要透明色
- textColorPickerView.annotationType = .freeTextFillColors
- fontViewCorloPV.annotationType = .freeTextColors
- } else {
- textColorPickerView.annotationType = .freeTextColors
- fontViewCorloPV.annotationType = .freeTextFillColors
- }
- }
- func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
- var selectIndex: UInt = 0
- let menu = NSMenu()
- if let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName) {
- for i in 0..<fontFamily.count {
- let array = fontFamily[i]
- if let styleName = array[1] as? String {
- if style == styleName {
- selectIndex = UInt(i)
- }
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
- let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0)
- let attrited = [NSAttributedString.Key.font: font]
- let string = NSAttributedString(string: styleName, attributes: attrited)
- let item = NSMenuItem()
- item.attributedTitle = string
-
- menu.addItem(item)
- }
- }
- }
-
- if style == nil {
- selectIndex = 0
- }
-
- fontStylePopUpButton.menu = menu
-
- return selectIndex
- }
-
- // MARK: UI Action
-
- @IBAction func colorPickerViewAction(_ sender: Any) {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
-
- if let textColor = textColorPickerView.color {
- textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- if fabs(opacity - 0) < 0.001 {
- if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
- annotationModel?.setInteriorOpacity(0)
- annotationModel?.setInteriorColor(.black)
- } else if annotationType == .freeText {
- annotationModel?.setOpacity(0)
- annotationModel?.setFontColor(.black)
- } else if annotationType == .signDate || annotationType == .signText {
- annotationModel?.setOpacity(0)
- annotationModel?.setFontColor(.black)
- } else {
- annotationModel?.setOpacity(0)
- annotationModel?.setFontColor(.black)
- }
- } else {
- if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
- annotationModel?.setInteriorOpacity(opacity)
- annotationModel?.setInteriorColor(textColor)
- } else if annotationType == .freeText {
- annotationModel?.setOpacity(opacity)
- annotationModel?.setFontColor(textColor)
- } else if annotationType == .signDate || annotationType == .signText {
- annotationModel?.setOpacity(opacity)
- annotationModel?.setColor(textColor)
- } else {
- annotationModel?.setOpacity(opacity)
- annotationModel?.setColor(textColor)
- if let model = self.annotationModel {
- if (model.annotation == nil || CPDFListViewConfig.defaultManager.isSaveDefault) && self.isannotationMode {
- if self.annotationType.isMarkup() {
- let type = self.annotationType
- if type == .highlight {
- KMPreferenceManager.shared.markupHighlightColor = textColor
- } else if type == .underline {
- KMPreferenceManager.shared.markupUnderlineColor = textColor
- } else if type == .strikeOut {
- KMPreferenceManager.shared.markupStrikthroughColor = textColor
- } else if type == .ink {
- KMPreferenceManager.shared.markupPenColor = textColor
- }
- }
- }
- }
- }
- }
-
- if annotationModel?.annotations != nil {
- if annotationModel?.annotation is CPDFFreeTextAnnotation {
- let textNote = (annotationModel?.annotation as! CPDFFreeTextAnnotation)
- if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
- pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
- } else {
- updateAnnotation(.color)
- }
- } else if annotationModel?.annotation is CSelfSignAnnotationFreeText {
- let textNote = (annotationModel?.annotation as! CSelfSignAnnotationFreeText)
- if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
- pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
- } else {
- updateAnnotation(.color)
- }
- } else {
- updateAnnotation(.color)
- }
- generalImageView.image = annotationModel?.annotationImage
- } else {
- updateAnnotation(.color)
- }
- }
- }
- @IBAction func fillColorPickerViewAction(_ sender: Any) {
- var fillColor = NSColor.clear
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- if let borderColor = borderColorPickerView.color {
- borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
- fillColor = color
- }
- if annotationType == .freeText {
- if let fillColorPickerColor = fillColorPickerView.color {
- annotationModel?.setColor(fillColorPickerColor)
- // annotationModel?.setOpacity(opacity)
- }
- } else {
- annotationModel?.setColor(fillColor)
- annotationModel?.setOpacity(opacity)
- }
- updateAnnotation(.color)
- }
-
- @IBAction func borderColorPickerViewAction(_ sender: Any) {
- var fillColor = NSColor.clear
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- if let borderColor = borderColorPickerView.color {
- borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
- fillColor = color
- }
-
- annotationModel?.setColor(fillColor)
- annotationModel?.setOpacity(opacity)
-
- updateAnnotation(.color)
- }
-
- @IBAction func fontViewCorloPVAction(_ sender: Any) {
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
- if let fontViewPickerColor = fontViewCorloPV.color {
- fontViewPickerColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
- if fabs(opacity - 0) < 0.001 {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- annotationModel?.setFontColor(.black)
- // annotationModel?.setOpacity(0)
- }
- } else {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- annotationModel?.setFontColor(fontViewPickerColor)
- // annotationModel?.setOpacity(opacity)
- } else {
- annotationModel?.setColor(fontViewPickerColor)
- }
- }
- }
-
- updateAnnotation(.color)
- }
- @IBAction func opacitySliderAction(_ sender: Any) {
- var opcity = CGFloat(opacitySlider.floatValue)
- if opcity == 0 {
- opcity = 0.0001
- }
- if annotationType == .signFalse ||
- annotationType == .signTure ||
- annotationType == .signDot ||
- annotationType == .signCircle ||
- annotationType == .signLine {
- annotationModel?.setOpacity(opcity)
- } else if annotationType == .square ||
- annotationType == .circle {
- if opcity != 0 {
- annotationModel?.setOpacity(opcity)
- annotationModel?.setInteriorOpacity(opcity)
- } else {
- annotationModel?.setColor(nil)
- annotationModel?.setInteriorColor(nil)
- }
- } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
- annotationModel?.setOpacity(opcity)
- } else {
- annotationModel?.setOpacity(opcity)
- }
- opacityComboBox.stringValue = "\(Int(opcity * 100))%"
- updateAnnotation(.opacity)
- }
-
- @IBAction func opacityComboBoxAction(_ sender: Any) {
- opacitySlider.floatValue = Float(opacityComboBox.floatValue / 100)
- var opcity = CGFloat(opacitySlider.floatValue)
- if opcity == 0 {
- opcity = 0.0001
- }
- if annotationType == .signFalse ||
- annotationType == .signTure ||
- annotationType == .signDot ||
- annotationType == .signCircle ||
- annotationType == .signLine {
- annotationModel?.setOpacity(opcity)
- } else if annotationType == .square ||
- annotationType == .circle {
- if opcity != 0 {
- annotationModel?.setOpacity(opcity)
- annotationModel?.setInteriorOpacity(opcity)
- } else {
- annotationModel?.setColor(nil)
- annotationModel?.setInteriorColor(nil)
- }
- } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
- annotationModel?.setOpacity(opcity)
- } else {
- annotationModel?.setOpacity(opcity)
- }
- opacitySlider.floatValue = Float(opcity)
- opacitySlider.toolTip = "\(Int(opcity * 100))%"
- updateAnnotation(.opacity)
- }
-
- @IBAction func lineWidthSliderAction(_ sender: Any) {
- annotationModel?.setLineWidth(CGFloat(lineWidthSlider.floatValue))
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
-
- updateAnnotation()
- }
- @IBAction func lineWidthComboBoxAction(_ sender: Any) {
- var lineWidth = lineWidthComboBox.floatValue
- if lineWidth > 18 {
- lineWidth = 18
- } else if lineWidth < 0.5 {
- lineWidth = 0.5
- }
- annotationModel?.setLineWidth(CGFloat(lineWidth))
- lineWidthSlider.floatValue = Float((annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_)
-
- updateAnnotation()
- }
- @IBAction func alignButtonAction(_ sender: NSButton) {
- var alignmentType: KMFreeTextAnnotationAlignmentType = .left
-
- if annotationModel?.annotations != nil {
- for tAnnotation in self.annotations {
- switch sender.tag {
- case 0:
- if tAnnotation is CPDFFreeTextAnnotation {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .left
- }
- alignmentType = .left
- case 1:
- if tAnnotation is CPDFFreeTextAnnotation {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .center
- }
- alignmentType = .center
- case 2:
- if tAnnotation is CPDFFreeTextAnnotation {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .right
- }
- alignmentType = .right
- default:
- break
- }
- }
- } else {
- switch sender.tag {
- case 0:
- annotationModel?.setAlignment(.left)
- alignmentType = .left
- case 1:
- annotationModel?.setAlignment(.center)
- alignmentType = .center
- case 2:
- annotationModel?.setAlignment(.right)
- alignmentType = .right
- default:
- break
- }
- }
- alignmentTypeSelected(alignmentType)
- updateAnnotation()
-
- NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationAlignmentTypeNotification"),
- object: "\(alignmentType.rawValue)")
- }
-
- @IBAction func currentFontColorButtonAction(_ sender: NSButton) {
- // if ([_annotation isKindOfClass:[PDFAnnotationFreeText class]]) {
- // KMFontColorViewController *vc = [[[KMFontColorViewController alloc] initWithNibName:@"KMFontColorViewController" bundle:[NSBundle mainBundle]] autorelease];
- // vc.annotationFontColor = _annotation.fontColor;
- // __block typeof(self) blockSelf = self;
- // vc.fontColorChangeCallback = ^(NSColor *fontColor) {
- // for (PDFAnnotation *tAnnotation in blockSelf.annotations) {
- // tAnnotation.fontColor = fontColor;
- // }
- // };
- // NSPopover *popover = [[NSPopover alloc] init];
- // popover.delegate = self;
- // popover.contentViewController = vc;
- // popover.animates = YES;
- // popover.behavior = NSPopoverBehaviorTransient;
- // [popover showRelativeToRect:CGRectMake([sender bounds].origin.x, 0, [sender bounds].size.width, [sender bounds].size.height) ofView:sender preferredEdge:CGRectMaxYEdge];
- // [popover release];
- // }
- }
-
- @IBAction func borderButtonAction(_ sender: NSButton) {
- var lineWidth: CGFloat = 0.0
- var lineStype: CPDFBorderStyle = .solid
- var startingPoint: CPDFLineStyle = .none
- var endPoint: CPDFLineStyle = .none
- var dashPattern: [NSNumber] = []
- var isLineAnnotation: Bool = false
- if annotationModel?.annotations != nil {
- for tAnnotation in self.annotations {
- lineWidth = tAnnotation.lineWidth()
- lineStype = tAnnotation.borderStyle()
- dashPattern = tAnnotation.dashPattern() as? [NSNumber] ?? []
- if tAnnotation is CPDFLineAnnotation {
- isLineAnnotation = true
- startingPoint = (tAnnotation as! CPDFLineAnnotation).startLineStyle
- endPoint = (tAnnotation as! CPDFLineAnnotation).endLineStyle
- } else {
- isLineAnnotation = false
- }
- }
- } else {
- lineWidth = (annotationModel?.lineWidth()) ?? self.kDefaultLineWidth_
- lineStype = (annotationModel?.style()) as? CPDFBorderStyle ?? .solid
- dashPattern = (annotationModel?.dashPattern()) as? [NSNumber] ?? []
- if annotationModel?.annotationType == .line {
- isLineAnnotation = true
- startingPoint = (annotationModel?.startLineStyle()) as? CPDFLineStyle ?? .none
- endPoint = (annotationModel?.endLineStyle()) as? CPDFLineStyle ?? .none
- } else {
- isLineAnnotation = false
- }
- }
- let lineModel = KMLineModel(lineWidth: lineWidth, lineStype: lineStype, startingPoint: startingPoint, endPoint: endPoint, dashPattern: dashPattern, isLineAnnotation: isLineAnnotation)
- let fontWindowController = KMAnnotationLineWindowController.initWindowController(lineModel)
- guard let window = fontWindowController.window else { return }
- fontWindowController.callback = { [self] model in
-
- if annotationType == .ink {
- lineWidthSlider.floatValue = Float(model.lineWidth)
- lineWidthComboBox.stringValue = String(format: "%0.1f pt", Float(model.lineWidth))
- }
- annotationModel?.setLineWidth(model.lineWidth)
- annotationModel?.setStyle(model.lineStype)
-
- if model.lineStype == .solid {
- lineTypeButtonAction(lineStyleButton)
- } else if model.lineStype == .dashed {
- lineTypeButtonAction(dottedLineStyleButton)
- for tAnnotation in self.annotations {
- tAnnotation.setDashPattern(model.dashPattern)
- }
- }
-
- annotationModel?.setStart(model.startingPoint)
- annotationModel?.setEnd(model.endPoint)
- }
-
- window.orderFront(sender)
- }
-
- func lineTypePopAction() {
- annotationModel?.setStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
- updateAnnotation()
- }
- @IBAction func lineTypeButtonAction(_ sender: NSButton) {
- let tag = sender.tag
-
- switch tag {
- case 0:
- lineStyleCount = 0
- refreshLineTypeUI()
- lineStyleButton.image = borderStyleSolid(true)
- dottedLineStyleButton.image = borderStyleDashed(false)
- rightArrowStyleButton.image = lineStyleRightArrow(false)
- lineTypePopAction()
-
- case 1:
- lineStyleCount = 1
- refreshLineTypeUI()
- lineStyleButton.image = borderStyleSolid(false)
- dottedLineStyleButton.image = borderStyleDashed(true)
- rightArrowStyleButton.image = lineStyleRightArrow(false)
- lineTypePopAction()
-
- case 2:
- lineStyleCount = 2
- refreshLineTypeUI()
- lineStyleButton.image = borderStyleSolid(false)
- dottedLineStyleButton.image = borderStyleDashed(false)
- rightArrowStyleButton.image = lineStyleRightArrow(true)
-
- for tAnnotation in annotations {
- if tAnnotation is CPDFLineAnnotation {
- (tAnnotation as! CPDFLineAnnotation).endLineStyle = .closedArrow
- }
- }
-
- default:
- break
- }
- }
-
- @IBAction func iconButtonAction(_ sender: NSButton) {
- var type: CPDFTextAnnotationIconType = .comment
-
- switch sender.tag {
- case 0:
- type = .comment
- case 1:
- type = .key
- case 2:
- type = .note
- case 3:
- type = .help
- case 4:
- type = .newParagraph
- case 5:
- type = .paragraph
- case 6, _:
- type = .insert
- }
-
- if annotationModel?.annotations != nil {
- for tAnnotation in self.annotations {
- if tAnnotation is CPDFTextAnnotation {
- (tAnnotation as! CPDFTextAnnotation).setIconType(type)
- }
- }
- self.updateAnnotation()
- } else {
- annotationModel?.setAnchoredIconType(type)
- }
-
- generalImageView.image = annotationModel?.annotationImage
- updateannotationMode()
-
- clickAnnotationTextIconButtonAction(iconType: UInt(sender.tag), boxArr: iconButtonArray)
- }
-
- @IBAction func textImageBoxButtonAction(_ sender: Any) {
- openFreeTextStylesViewController()
- }
-
- // MARK: Font、FontSize Action
-
- @IBAction func fontButtonAction(_ sender: NSButton) {
- let selectItem = self.fontPopUpButton.selectedItem
- guard let selectItem = selectItem else { return }
-
- let resultAtt = NSMutableAttributedString(attributedString: selectItem.attributedTitle!)
- let familyString = resultAtt.string
- let styleString = self.fontStylePopUpButton.selectedItem?.title ?? ""
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: familyString, .face: styleString as Any])
- let fontSizeString = fontSizeComboBox.stringValue.replacingOccurrences(of: " pt", with: "")
- let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSizeString.stringToCGFloat())
- let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString, fontSize: fontSizeString.stringToCGFloat(), fontColor: textColorPickerView.color ?? .black, fontAlignment: annotationAlignment, annotationType: annotationModel?.annotationType ?? .unkown)
-
- let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
- let window = fontWindowController.window
- fontWindowController.callback = { [weak self] model in
- self?.alignmentTypeSelected(model.fontAlignment)
- self?.textColorPickerView.color = model.fontColor
-
- DispatchQueue.global(qos: .default).async { [self]
- let fonts = NSFontManager.shared.availableFontFamilies
- let menu = NSMenu()
- var selectedIndex = 0
-
- for (index, fontName) in fonts.enumerated() {
- if let font = NSFont(name: fontName, size: 12.0) {
- let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
- let item = NSMenuItem()
- item.attributedTitle = attributedString
- menu.addItem(item)
-
- if let family = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
- let style = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
- if model.fontName == family {
- selectedIndex = index
- }
- }
- }
- }
- DispatchQueue.main.async {
- self?.fontPopUpButton.menu = menu
- self?.fontPopUpButton.selectItem(at: selectedIndex)
- }
- }
- let selectedStyleIndex = self?.setFontStyle(fontName: model.fontName, currentStyle: model.fontWeight) ?? 0
- self?.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
- self?.fontSizeComboBox.stringValue = String(format: "%.f pt", model.fontSize)
-
- if self?.annotationModel?.annotations != nil {
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: model.fontName, .face: model.fontWeight])
- let annotationFont = NSFont(descriptor: attributeFontDescriptor, size: model.fontSize)
- for tAnnotation in self?.annotations ?? [] {
- if tAnnotation is CPDFFreeTextAnnotation {
- (tAnnotation as! CPDFFreeTextAnnotation).font = annotationFont
- (tAnnotation as! CPDFFreeTextAnnotation).fontColor = model.fontColor
- if model.fontAlignment == .left {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .left
- } else if model.fontAlignment == .center {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .center
- } else if model.fontAlignment == .right {
- (tAnnotation as! CPDFFreeTextAnnotation).alignment = .right
- }
- }
- }
- self?.updateAnnotation()
- } else {
- self?.annotationModel?.setFontName(model.fontName)
- self?.annotationModel?.setFontSize(model.fontSize)
- self?.annotationModel?.setFontColor(model.fontColor)
- if model.fontAlignment == .left {
- self?.annotationModel?.setAlignment(.left)
- } else if model.fontAlignment == .center {
- self?.annotationModel?.setAlignment(.center)
- } else if model.fontAlignment == .right {
- self?.annotationModel?.setAlignment(.right)
- }
- }
- }
- window?.orderFront(sender)
- }
-
- @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
- guard let selectedItem = fontPopUpButton.selectedItem else { return }
-
- let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
- let familyString = resultAtt.string
- let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
- guard let styleString = fontStylePopUpButton.selectedItem?.title else { return }
-
- fontStylePopUpButton.selectItem(at: Int(selectIndex))
-
- if annotationModel?.annotations != nil {
- for tAnnotation in annotations {
- if tAnnotation is CPDFFreeTextAnnotation {
- let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
- if let font = newAnnotation.font {
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int {
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- newAnnotation.font = newFont
- }
- }
-
- if let tFont = newAnnotation.font {
- if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
- let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
- DispatchQueue.main.async {
- let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
- self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
- }
- }
- }
- }
- } else if tAnnotation is KMSelfSignAnnotationFreeText {
- let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
- if let font = newAnnotation.font {
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int {
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- newAnnotation.font = newFont
- }
- }
-
- newAnnotation.updateBounds()
-
- if let tFont = newAnnotation.font {
- if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
- let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
- DispatchQueue.main.async {
- let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
- self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
- }
- }
- }
- }
- }
- }
- } else {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- if let data = annotationModel?.fontName() {
- let font = NSFont(name: data, size: (annotationModel?.fontSize()) ?? self.kDefaultFontSize_) ?? NSFont.systemFont(ofSize: 16)
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int {
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- annotationModel?.setFontName(newFont.fontName)
- annotationModel?.setFontSize(newFont.pointSize)
- }
- }
-
- let tFont = NSFont(name: data, size: (annotationModel?.fontSize() ?? self.kDefaultFontSize_)) ?? NSFont.systemFont(ofSize: 16)
- if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
- let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
- DispatchQueue.main.async {
- let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
- self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
- }
- }
- }
- }
- }
-
- updateannotationMode()
- updateAnnotation()
- }
-
- @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
- if annotationModel?.annotations != nil {
- for tAnnotation in annotations {
- if tAnnotation is CPDFFreeTextAnnotation {
- let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
- if let font = newAnnotation.font {
- let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
- if let newFont = newFont {
- newAnnotation.font = newFont
- }
- }
- } else if tAnnotation is KMSelfSignAnnotationFreeText {
- let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
- if let font = newAnnotation.font {
- let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
- if let newFont = newFont {
- newAnnotation.font = newFont
- }
- }
- newAnnotation.updateBounds()
- }
- }
- } else {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- if let data = annotationModel?.fontName() {
- let font = NSFont(name: data, size: (annotationModel?.fontSize() ?? self.kDefaultFontSize_)) ?? NSFont.systemFont(ofSize: 16)
- let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
- if let newFont = newFont {
- annotationModel?.setFontName(newFont.fontName)
- annotationModel?.setFontSize(newFont.pointSize)
- }
- }
- }
- }
- updateAnnotation()
- NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationFontTypeNotification"),
- object: pdfView)
- adjustFreeText()
- }
-
- //字重 NSFontWeight
- @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
- if let styleString = self.fontStylePopUpButton.selectedItem?.title {
- if annotationModel?.annotations != nil {
- for tAnnotation in self.annotations {
- if tAnnotation is CPDFFreeTextAnnotation {
- let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
- if let font = newAnnotation.font {
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
- let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
-
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- newAnnotation.font = newFont
- }
- }
- }
- } else if tAnnotation is KMSelfSignAnnotationFreeText {
- let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
- if let font = newAnnotation.font {
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
- let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
-
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- newAnnotation.font = newFont
- }
- newAnnotation.updateBounds()
- }
- }
- }
- }
- } else {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- if let data = annotationModel?.fontName() {
- let font = NSFont(name: data, size: (annotationModel?.fontSize() ?? self.kDefaultFontSize_)) ?? NSFont.systemFont(ofSize: 16)
- if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? Int,
- let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
-
- let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
- if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
- annotationModel?.setFontName(newFont.fontName)
- annotationModel?.setFontSize(newFont.pointSize)
- }
- }
- }
- }
- }
- }
- updateAnnotation()
- }
-
- @IBAction func buttonClicked_SwitchIncludeTime(_ sender: NSButton) {
- annotationModel?.setIncludeTime(sender.state == .on ? true:false)
- self.updateDateView()
- updateAnnotation()
- }
-
- @IBAction func dateCheckButtonAction(_ sender: NSPopUpButton) {
- annotationModel?.setDateFormatIndex(sender.indexOfSelectedItem)
- updateAnnotation()
- }
-
- func annotationFontReload() {
- DispatchQueue.global(qos: .default).async { [weak self] in
- guard let self = self else { return }
- let fonts = NSFontManager.shared.availableFontFamilies
- let menu = NSMenu()
- var selectedIndex = 0
-
- for (index, fontName) in fonts.enumerated() {
- if let font = NSFont(name: fontName, size: 12.0) {
- let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
- let item = NSMenuItem()
- item.attributedTitle = attributedString
- menu.addItem(item)
- if [.freeText, .signText, .signDate].contains(self.annotationType),
- let modelFontName = self.annotationModel?.fontName(),
- modelFontName == font.fontName {
- selectedIndex = index
- }
- }
- }
-
- if [.freeText, .signText, .signDate].contains(self.annotationType),
- let data = self.annotationModel?.fontName() {
- let freetextFont = NSFont(name: data, size: 16) ?? NSFont.systemFont(ofSize: 16)
- if let family = freetextFont.fontDescriptor.object(forKey: .family) as? String,
- let style = freetextFont.fontDescriptor.object(forKey: .face) as? String {
-
- DispatchQueue.main.async { [weak self] in
- guard let self = self else { return }
-
- self.fontPopUpButton.menu = menu
- self.fontPopUpButton.selectItem(at: selectedIndex)
-
- let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
- self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
- }
- }
- }
- }
- }
-
- func adjustFreeText() {
- if annotationModel?.annotations != nil {
- if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
- for an in (annotationModel?.annotations) ?? [] {
- if let freeTextAn = an as? CPDFFreeTextAnnotation {
- let font = freeTextAn.font
- var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font as Any]
-
- let style = NSMutableParagraphStyle()
- style.alignment = freeTextAn.alignment
- attributes[NSAttributedString.Key.paragraphStyle] = style
-
- let contents = freeTextAn.contents ?? ""
- let textViewSize = contents.boundingRect(with: CGSize(width: freeTextAn.bounds.size.width, height: CGFloat(MAXFLOAT)),
- options: .usesLineFragmentOrigin,
- attributes: attributes).size
-
- var rect = freeTextAn.bounds
-
- if textViewSize.height != freeTextAn.bounds.size.height {
- rect.origin.y -= (textViewSize.height - rect.size.height)
- rect.size.height = textViewSize.height
- }
-
- if textViewSize.width < freeTextAn.bounds.size.width {
- rect.size.width = textViewSize.width
- }
-
- freeTextAn.bounds = rect
- }
- }
- }
- }
- }
-
- // MARK: NSNotification
-
- @objc func textDidChange(_ notification: Notification) {
- self.isTextEdit = true
-
- if self.subType == KMSelfSignAnnotationFreeTextSubType.freeText && isannotationMode {
- if let obj = notification.object as? NSObject {
- for tAnnotation in annotations {
- tAnnotation.setString(noteTextView.string)
- }
-
- if obj.isEqual(to: noteTextView) {
- UserDefaults.standard.set(noteTextView.string, forKey: SKAnnotationSelfSignPlaceHolderStringKey)
- }
-
- if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
- textAnnotationImageView.image = annotationModel?.annotationImage
- } else {
- generalImageView.image = annotationModel?.annotationImage
- }
- }
- } else {
- if let obj = notification.object as? NSObject, obj.isEqual(to: noteTextView) {
- for tAnnotation in self.annotations {
- if let markupNote = tAnnotation as? CPDFMarkupAnnotation {
- markupNote.setMarkupText(noteTextView.string)
- } else {
- tAnnotation.setString(noteTextView.string)
- }
- }
- }
- }
- }
- @objc func themeChanged(notification: NSNotification) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [self] in
- updateViewColor()
- }
- }
-
- @objc func annotationChangeNotification(_ notification: NSNotification) {
- if notification.object != nil {
- let anno: [String : Any] = notification.object as? [String : Any] ?? [:]
- if let textAnno: CPDFAnnotation = anno["object"] as? CPDFAnnotation {
- if textAnno.isKind(of: CPDFTextAnnotation.self) {
- textColorPickerView.setColor(textAnno.color)
- }
- }
- }
- }
- }
- class KMGeneralAnnotationColorButton: NSButton {
- var _circleColor: NSColor = .clear
- var _buttonBorderColor: NSColor = .clear
-
- // MARK: Get & Set
-
- var circleColor: NSColor {
- set {
- _circleColor = newValue
-
- needsDisplay = true
- }
- get {
- return _circleColor
- }
- }
-
- var buttonBorderColor: NSColor {
- set {
- _buttonBorderColor = newValue
-
- needsDisplay = true
- }
- get {
- return _buttonBorderColor
- }
- }
-
- // MARK: Init
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- wantsLayer = true
- layer?.cornerRadius = 12.0
- layer?.masksToBounds = true
- layer?.borderColor = NSColor.clear.cgColor
- }
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- let path3 = NSBezierPath(ovalIn: NSMakeRect(3, 3, dirtyRect.size.width - 6, dirtyRect.size.height - 6))
- }
- }
- class KMNoteIconButtonCell: NSButtonCell {
- override func drawImage(_ image: NSImage, withFrame frame: NSRect, in controlView: NSView) {
- let titleSize = self.attributedTitle.size()
- let imageSize = self.image?.size ?? .zero
-
- var rect = NSZeroRect
- let leftGap = (controlView.bounds.size.width - image.size.width) / 2.0
- rect.origin.x = leftGap
-
- if self.imagePosition == .imageAbove {
- rect.origin.y = 6
- } else {
- rect.origin.y = (controlView.frame.size.height - image.size.height) / 2.0
- }
- rect.size = imageSize
- if let data = self.image {
- super.drawImage(data, withFrame: rect, in: controlView)
- }
- }
-
- override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
- let titleSize = self.attributedTitle.size()
- let imageSize = self.image?.size ?? .zero
- let totalHeight = titleSize.height + imageSize.height + 1
-
- var rect = NSZeroRect
- rect.origin.x = (controlView.bounds.size.width - titleSize.width) / 2.0
- rect.origin.y = (controlView.bounds.size.height - totalHeight) / 2.0 + imageSize.height + 1
- rect.size = titleSize
- return super.drawTitle(title, withFrame: rect, in: controlView)
- }
- }
|