// // KMNAnnotationPopMode.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/12/1. // import Cocoa @objc public enum ListViewPopType: Int, CaseIterable{ case generaAnnotation = 0 case freeTextAnnotation case shapeAnnotation case linkAnnotation case formAnnotation case formRadioAnnotation case multpleAnnotation case popTypeNone } @objc public enum linkDetailType: Int, CaseIterable{ case linkSetting case page case url case email } @objc public enum DetailPopType: Int, CaseIterable{ case freeTextAlight = 0 case contentEditAlight case freeTextDoubleAlight case doubleObjectAlight case freeTextMultpleAlight case multpleObjectAlight case detailNone } @objc public enum ObjectAlignType: Int, CaseIterable{ case alightLeft = 0 case alightRight case alightTop case alightVer case alightHor case alightBottom case averageVer case averageHor } @objc public enum ListViewDetailPopType: Int, CaseIterable{ case textAlight = 0 case alight } let changeValue:CGFloat = 1.0 class KMNAnnotationPopMode: NSObject { private(set) var annotation: CPDFAnnotation? private(set) var annotations: [CPDFAnnotation] = [] private(set) var annotationType: CAnnotationType = CAnnotationType.unkown private(set) var popType: ListViewPopType = .popTypeNone init(pdfAnnotations: [CPDFAnnotation]) { super.init() if let firstAnnotation = pdfAnnotations.first { annotation = firstAnnotation } self.annotations = pdfAnnotations popType = self.annotationPopType() } private func annotationPopType()->ListViewPopType { let firstAnnotation:CPDFAnnotation = annotations.first ?? CPDFAnnotation.init() let type = annotationType(annotation: firstAnnotation) if annotations.count == 0 { return .popTypeNone } else if annotations.count == 1 { return type } else { var isSampleAnnotation = true for i in 1 ..< annotations.count { let annotation = annotations[i] if type != annotationType(annotation: annotation) { isSampleAnnotation = false break } } if(isSampleAnnotation) { return type } else { return .popTypeNone } } } public func annotationType(annotation:CPDFAnnotation)->ListViewPopType { if annotation.isKind(of: CPDFMarkupAnnotation.self) { return .generaAnnotation } else if annotation.isKind(of: CPDFFreeTextAnnotation.self) { return .freeTextAnnotation } else if (annotation.isKind(of: CPDFInkAnnotation.self) || annotation.isKind(of: CPDFCircleAnnotation.self) || annotation.isKind(of: CPDFSquareAnnotation.self) || annotation.isKind(of: CPDFLineAnnotation.self) || annotation.isKind(of: CPDFPolygonAnnotation.self) || annotation.isKind(of: CPDFPolylineAnnotation.self) || annotation.isKind(of: CSelfSignAnnotation.self)) { return .shapeAnnotation } else if annotation.isKind(of: CPDFLinkAnnotation.self) { if(annotations.count > 1) { return .multpleAnnotation } else { return .linkAnnotation } } else if annotation.isForm() { if(annotations.count > 1) { return .multpleAnnotation } else if(annotations.count == 1) { if(annotation.isKind(of: CPDFButtonWidgetAnnotation.self)) { let button = annotation as! CPDFButtonWidgetAnnotation if(button.controlType() == .radioButtonControl) { return .formRadioAnnotation } } return .formAnnotation } } return .popTypeNone } private func convertAnnotationType(firstAnnotation:CPDFAnnotation) -> CAnnotationType { var annotationType = CAnnotationType.unkown if firstAnnotation.isKind(of: CSelfSignAnnotationFreeText.self) { let freeText:CSelfSignAnnotationFreeText = firstAnnotation as! CSelfSignAnnotationFreeText annotationType = freeText.subType } else if firstAnnotation.isKind(of: CPDFPolygonAnnotation.self) { annotationType = CAnnotationType.measurePolyGon } else if firstAnnotation.isKind(of: CPDFPolylineAnnotation.self) { annotationType = CAnnotationType.measurePolyLine } else if firstAnnotation.isKind(of: CSelfSignAnnotation.self) { let selfSignAnnotation:CSelfSignAnnotation = firstAnnotation as! CSelfSignAnnotation annotationType = selfSignAnnotation.annotationType } else if firstAnnotation.isKind(of: CPDFFreeTextAnnotation.self) { annotationType = CAnnotationType.freeText } else if firstAnnotation.isKind(of: CPDFTextAnnotation.self) { annotationType = CAnnotationType.anchored } else if firstAnnotation.isKind(of: CPDFCircleAnnotation.self) { annotationType = CAnnotationType.circle } else if firstAnnotation.isKind(of: CPDFSquareAnnotation.self) { annotationType = CAnnotationType.square } else if firstAnnotation.isKind(of: CPDFMarkupAnnotation.self) { let markupAnnotation:CPDFMarkupAnnotation = firstAnnotation as! CPDFMarkupAnnotation let markupType = markupAnnotation.markupType() if markupType == .highlight { annotationType = CAnnotationType.highlight } else if(markupType == .strikeOut) { annotationType = CAnnotationType.strikeOut } else if(markupType == .underline) { annotationType = CAnnotationType.underline } else if(markupType == .squiggly) { annotationType = CAnnotationType.squiggly } } else if firstAnnotation.isKind(of: CPDFLineAnnotation.self) { let lineAnnotation:CPDFLineAnnotation = firstAnnotation as! CPDFLineAnnotation if lineAnnotation.isMeasure == true { annotationType = CAnnotationType.measureLine } else if lineAnnotation.isArrowLine() == true { annotationType = CAnnotationType.arrow } else { annotationType = CAnnotationType.line } } else if firstAnnotation.isKind(of: KMTableAnnotation.self) { annotationType = CAnnotationType.table } else if firstAnnotation.isKind(of: CPDFInkAnnotation.self) { annotationType = CAnnotationType.ink } else if firstAnnotation.isKind(of: CPDFLinkAnnotation.self) { annotationType = CAnnotationType.link } else if firstAnnotation.isKind(of: CPDFListSignatureAnnotation.self) { annotationType = CAnnotationType.signSignature } else if firstAnnotation.isKind(of: CPDFStampAnnotation.self) { annotationType = CAnnotationType.stamp } else if firstAnnotation.isKind(of: CPDFButtonWidgetAnnotation.self) { let buttonWidgetAnnotation:CPDFButtonWidgetAnnotation = firstAnnotation as! CPDFButtonWidgetAnnotation let controlType = buttonWidgetAnnotation.controlType() if controlType == .radioButtonControl { annotationType = CAnnotationType.radioButton } else if(controlType == .checkBoxControl) { annotationType = CAnnotationType.checkBox } else if(controlType == .pushButtonControl) { annotationType = CAnnotationType.actionButton } } else if firstAnnotation.isKind(of: CPDFTextWidgetAnnotation.self) { annotationType = CAnnotationType.textField } else if firstAnnotation.isKind(of: CPDFChoiceWidgetAnnotation.self) { annotationType = CAnnotationType.textField } else if firstAnnotation.isKind(of: CPDFChoiceWidgetAnnotation.self) { let choiceWidgetAnnotation:CPDFChoiceWidgetAnnotation = firstAnnotation as! CPDFChoiceWidgetAnnotation if(choiceWidgetAnnotation.isListChoice == true) { annotationType = CAnnotationType.listMenu } else { annotationType = CAnnotationType.comboBox } } else if firstAnnotation.isKind(of: CPDFSignatureWidgetAnnotation.self) { annotationType = CAnnotationType.signature } return annotationType } public func annotationColor()->NSColor? { //返回颜色是nil说明颜色不一样,返回透明则说明是没有颜色或者透明色 var color:NSColor? = nil let isNotSameColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color) if isNotSameColor == false { var anColor = annotation?.color if let selfAnnotation = annotation as? CSelfSignAnnotation { anColor = selfAnnotation.lineColor } color = (anColor != nil) ? anColor : .clear } return color } public func setAnnotationColor(annotationColor:NSColor?) { for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CSelfSignAnnotation.self) == true { if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation { signAnnotation.lineColor = annotationColor } } else { annotation.color = annotationColor } let annotationType = convertAnnotationType(firstAnnotation: annotation) var keyValue = "" if annotationType == .highlight { keyValue = CHighlightNoteColorKey } else if annotationType == .underline { keyValue = CUnderlineNoteColorKey } else if annotationType == .strikeOut { keyValue = CStrikeOutNoteColorKey } else if annotationType == .squiggly { keyValue = CSquigglyNoteColorKey } else if annotationType == .circle { keyValue = CCircleNoteColorKey } else if annotationType == .square { keyValue = CSquareNoteColorKey } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText { keyValue = CFreeTextNoteBorderColorKey } else if annotationType == .line { keyValue = CLineNoteColorKey } else if annotationType == .arrow { keyValue = CArrowNoteColorKey } else if annotationType == .ink { keyValue = CInkNoteColorKey } else if annotationType == .signFalse { keyValue = CAnnotationSelfSignFalseColorKey } else if annotationType == .signTure { keyValue = CAnnotationSelfSignTureColorKey } else if annotationType == .signLine { keyValue = CAnnotationSelfSignLineColorKey } else if annotationType == .signDot { keyValue = CAnnotationSelfSignDotColorKey } else if annotationType == .redact { keyValue = CAnnotationRedactBorderColorKey } if keyValue.isEmpty == false { if annotationColor != nil { UserDefaults.standard.setPDFListViewColor(annotationColor!, forKey: keyValue) } else { UserDefaults.standard.removeObject(forKey: keyValue) } } } } public func annotationFontColor()->NSColor? { //不管相同还是不相同 pop框都是显示第一个颜色 var fontColor:NSColor? = nil if annotation?.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation let anColor = freeTextAnnotation.fontColor fontColor = (anColor != nil) ? anColor : .clear } return fontColor } public func setAnnotationFontColor(annotationFontColor:NSColor?) { for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation freeTextAnnotation.fontColor = annotationFontColor let keyValue = CFreeTextNoteFontColorKey if annotationFontColor != nil { UserDefaults.standard.setPDFListViewColor(annotationFontColor!, forKey: keyValue) } else { UserDefaults.standard.removeObject(forKey: keyValue) } } } } public func annotationAlignment()->NSTextAlignment { //natural时说明不一致 var textAlignment:NSTextAlignment = .natural if annotation?.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation let isNotSameColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Alignment) if isNotSameColor == false { textAlignment = freeTextAnnotation.alignment } } return textAlignment } public func setAnnotationAlignment(annotationAlignment:NSTextAlignment) { for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation freeTextAnnotation.alignment = annotationAlignment let keyValue = CFreeTextNoteAlignmentKey UserDefaults.standard.set(annotationAlignment.rawValue, forKey: keyValue) } } } public func annotationFontName()->CPDFFont? { //返回字体是nil说明字体名不一样 var cFont:CPDFFont? = nil if annotation?.isKind(of: CPDFFreeTextAnnotation.self) == true { let isNotSameName: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Name) let isNotSameStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Style) if isNotSameName == false && isNotSameStyle == false { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation cFont = freeTextAnnotation.cFont } } return cFont } public func setAnnotationFont(font:CPDFFont) { for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation freeTextAnnotation.cFont = font let keyValue = CFreeTextNoteFontNameKey let appleFont:String = CPDFFont.convertAppleFont(font) ?? "Helvetica" UserDefaults.standard.set(appleFont, forKey: keyValue) } } } public func annotationFieldName()->String? { //pop只有一个的时候才会支持获取,不然是对齐 var fieldName:String? = nil if annotations.count == 1 && annotation?.isForm() == true { if(annotation?.isKind(of: CPDFWidgetAnnotation.self) == true) { if let widgetAnnotation = annotation as? CPDFWidgetAnnotation { fieldName = widgetAnnotation.fieldName() } } } return fieldName } public func setannotationFieldName(fieldName:String) { for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFWidgetAnnotation.self) == true { if let widgetAnnotation = annotation as? CPDFWidgetAnnotation { widgetAnnotation.setFieldName(fieldName) } } } } public func annotationGropName()->String? { //pop只有一个的时候才会支持获取,不然是对齐 var gropNameName:String? = nil if annotations.count == 1 && annotation?.isForm() == true { if(annotation?.isKind(of: CPDFButtonWidgetAnnotation.self) == true) { if let buttonWidgetAnnotation = annotation as? CPDFButtonWidgetAnnotation { if buttonWidgetAnnotation.controlType() == .radioButtonControl { gropNameName = buttonWidgetAnnotation.buttonWidgetStateString() } } } } return gropNameName } public func setannotationGropName(gropName:String) { for i in 0 ..< annotations.count { let annotation = annotations[i] if(annotation.isKind(of: CPDFButtonWidgetAnnotation.self) == true) { if let buttonWidgetAnnotation = annotation as? CPDFButtonWidgetAnnotation { buttonWidgetAnnotation.setButtonWidgetStateString(gropName) } } } } public func zoomOutFontSize()->Bool{ //悬浮菜单注释同一类型 var isChange:Bool = false for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation var fontSize = freeTextAnnotation.fontSize if(fontSize > changeValue) { fontSize -= changeValue freeTextAnnotation.fontSize = fontSize let keyValue = CFreeTextNoteFontSizeKey UserDefaults.standard.set(fontSize, forKey: keyValue) isChange = true } } } return isChange } public func zoomInFontSize()->Bool{ //悬浮菜单注释同一类型 for i in 0 ..< annotations.count { let annotation = annotations[i] if annotation.isKind(of: CPDFFreeTextAnnotation.self) == true { let freeTextAnnotation = annotation as! CPDFFreeTextAnnotation var fontSize = freeTextAnnotation.fontSize fontSize += changeValue freeTextAnnotation.fontSize = fontSize let keyValue = CFreeTextNoteFontSizeKey UserDefaults.standard.set(fontSize, forKey: keyValue) } } return true } public func zoomOutShapeWidth()->Bool{ for i in 0 ..< annotations.count { let annotation = annotations[i] var shapeWidth = annotation.borderWidth if annotation.isKind(of: CSelfSignAnnotation.self) == true { if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation { shapeWidth = signAnnotation.lineAnnotationWidth } } if(shapeWidth > changeValue) { shapeWidth -= changeValue if annotation.isKind(of: CSelfSignAnnotation.self) == true { if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation { signAnnotation.lineAnnotationWidth = shapeWidth } } else { annotation.borderWidth = shapeWidth } var keyValue = "" let annotationType = convertAnnotationType(firstAnnotation: annotation) if annotationType == .circle { keyValue = CCircleNoteLineWidthKey } else if annotationType == .square { keyValue = CSquareNoteLineWidthKey } else if annotationType == .line { keyValue = CLineNoteLineWidthKey } else if annotationType == .arrow { keyValue = CArrowNoteLineWidthKey } else if annotationType == .ink { keyValue = CInkNoteLineWidthKey } else if annotationType == .freeText { keyValue = CAnnotationTextWidgetLineWidthKey } else if annotationType == .signFalse { keyValue = CAnnotationSelfSignFalseLineWidthKey } else if annotationType == .signTure { keyValue = CAnnotationSelfSignTureLineWidthKey } else if annotationType == .signCircle { keyValue = CAnnotationSelfSignCircleLineWidthKey } else if annotationType == .signLine { keyValue = CAnnotationSelfSignLineLineWidthKey } if keyValue.isEmpty == false { UserDefaults.standard.set(shapeWidth, forKey: keyValue) } return true } } return false } public func zoomInShapeWidth()->Bool{ for i in 0 ..< annotations.count { let annotation = annotations[i] var shapeWidth = annotation.borderWidth if annotation.isKind(of: CSelfSignAnnotation.self) == true { if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation { shapeWidth = signAnnotation.lineAnnotationWidth } } shapeWidth += changeValue if annotation.isKind(of: CSelfSignAnnotation.self) == true { if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation { signAnnotation.lineAnnotationWidth = shapeWidth } } else { annotation.borderWidth = shapeWidth } var keyValue = "" let annotationType = convertAnnotationType(firstAnnotation: annotation) if annotationType == .circle { keyValue = CCircleNoteLineWidthKey } else if annotationType == .square { keyValue = CSquareNoteLineWidthKey } else if annotationType == .line { keyValue = CLineNoteLineWidthKey } else if annotationType == .arrow { keyValue = CArrowNoteLineWidthKey } else if annotationType == .ink { keyValue = CInkNoteLineWidthKey } else if annotationType == .freeText { keyValue = CAnnotationTextWidgetLineWidthKey } else if annotationType == .signFalse { keyValue = CAnnotationSelfSignFalseLineWidthKey } else if annotationType == .signTure { keyValue = CAnnotationSelfSignTureLineWidthKey } else if annotationType == .signCircle { keyValue = CAnnotationSelfSignCircleLineWidthKey } else if annotationType == .signLine { keyValue = CAnnotationSelfSignLineLineWidthKey } if keyValue.isEmpty == false { UserDefaults.standard.set(shapeWidth, forKey: keyValue) } } return true } }