123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- //
- // 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
- }
-
-
- }
|