ソースを参照

【综合】form表单属性面板数据检查

tangchao 10 ヶ月 前
コミット
c94f2dd5c3

+ 64 - 56
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/AnnotationProperty/ViewController/FormProperties/KMAnnotationChoiceWidgetAppearanceViewController.swift

@@ -89,13 +89,13 @@ private enum KMPDFAnnotationFontWeightType: Int {
         fontStylePopUpButton.wantsLayer = true
         fontSizeComboBox.wantsLayer = true
         
-        fontPopUpButton.layer!.borderWidth = 1.0
-        fontStylePopUpButton.layer!.borderWidth = 1.0
-        fontSizeComboBox.layer!.borderWidth = 1.0
+        fontPopUpButton.layer?.borderWidth = 1.0
+        fontStylePopUpButton.layer?.borderWidth = 1.0
+        fontSizeComboBox.layer?.borderWidth = 1.0
         
-        fontPopUpButton.layer!.cornerRadius = 1.0
-        fontStylePopUpButton.layer!.cornerRadius = 1.0
-        fontSizeComboBox.layer!.cornerRadius = 1.0
+        fontPopUpButton.layer?.cornerRadius = 1.0
+        fontStylePopUpButton.layer?.cornerRadius = 1.0
+        fontSizeComboBox.layer?.cornerRadius = 1.0
         
         updateViewColor()
         
@@ -119,17 +119,17 @@ private enum KMPDFAnnotationFontWeightType: Int {
     func reloadData() {
         var opacity: CGFloat = 1
         
-        if let color = annotationModel.backgroundColor() {
+        if let color = annotationModel?.backgroundColor() {
             color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(nil, green: nil, blue: nil, alpha: &opacity)
         }
-        if let fontColor = annotationModel.fontColor() {
-            fillColorPickerView.color = annotationModel.backgroundColor()
+        if let fontColor = annotationModel?.fontColor() {
+            fillColorPickerView.color = annotationModel!.backgroundColor()
             colorPickerView.color = fontColor
         }
-        if annotationModel.fontSize() <= 0 {
+        if let data = annotationModel?.fontSize(), data <= 0 {
             fontSizeComboBox.stringValue = "11 pt"
         } else {
-            fontSizeComboBox.stringValue = String(format: "%.f pt", annotationModel.fontSize())
+            fontSizeComboBox.stringValue = String(format: "%.f pt", annotationModel?.fontSize() ?? 12.0)
         }
 
         DispatchQueue.global(qos: .default).async { [self] in
@@ -137,14 +137,16 @@ private enum KMPDFAnnotationFontWeightType: Int {
             var selectedIndex = 0
             var family = ""
             var style = ""
-            var annotationFont: NSFont
-            if annotationModel.fontName() != nil {
-                annotationFont = NSFont(name: annotationModel.fontName(), size: annotationModel.fontSize())!
+            var annotationFont: NSFont?
+            if annotationModel?.fontName() != nil {
+                if let data = NSFont(name: annotationModel!.fontName(), size: annotationModel!.fontSize()) {
+                    annotationFont = data
+                }
             } else {
-                annotationFont = NSFont.systemFont(ofSize: annotationModel.fontSize())
+                annotationFont = NSFont.systemFont(ofSize: annotationModel?.fontSize() ?? 12.0)
             }
-            family = annotationFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String ?? ""
-            style = annotationFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String ?? ""
+            family = annotationFont?.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String ?? ""
+            style = annotationFont?.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String ?? ""
 
             let menu = NSMenu()
             
@@ -163,7 +165,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
                             }
                         }
                     } else {
-                        if annotationModel.fontName() == font.fontName {
+                        if let data = annotationModel?.fontName(), data == font.fontName {
                             selectedIndex = index
                         }
                     }
@@ -181,10 +183,10 @@ private enum KMPDFAnnotationFontWeightType: Int {
         if annotations.count > 0 {
             annotationFont = annotation?.font
         } else {
-            if annotationModel.fontName() != nil {
-                annotationFont = NSFont(name: annotationModel.fontName(), size: annotationModel.fontSize())
+            if annotationModel?.fontName() != nil {
+                annotationFont = NSFont(name: annotationModel!.fontName(), size: annotationModel!.fontSize())
             } else {
-                annotationFont = NSFont.systemFont(ofSize: annotationModel.fontSize())
+                annotationFont = NSFont.systemFont(ofSize: annotationModel?.fontSize() ?? 12.0)
             }
         }
 
@@ -199,20 +201,26 @@ private enum KMPDFAnnotationFontWeightType: Int {
         
         switch annotationMode {
         case .actionButton:
-            userDefaults.safe_setColor(annotationModel.backgroundColor(), forKey: SKAnnotationActionButtonWidgetBackgroundColorKey)
-            userDefaults.safe_setColor(annotationModel.fontColor(), forKey: SKAnnotationActionButtonWidgetFontColorKey)
-            userDefaults.set(annotationModel.fontName(), forKey: SKAnnotationActionButtonWidgetFontNameKey)
-            userDefaults.set(annotationModel.fontSize(), forKey: SKAnnotationActionButtonWidgetFontSizeKey)
+            if let model = self.annotationModel {
+                userDefaults.safe_setColor(model.backgroundColor(), forKey: SKAnnotationActionButtonWidgetBackgroundColorKey)
+                userDefaults.safe_setColor(model.fontColor(), forKey: SKAnnotationActionButtonWidgetFontColorKey)
+                userDefaults.set(model.fontName(), forKey: SKAnnotationActionButtonWidgetFontNameKey)
+                userDefaults.set(model.fontSize(), forKey: SKAnnotationActionButtonWidgetFontSizeKey)
+            }
         case .comboBox:
-            userDefaults.safe_setColor(annotationModel.backgroundColor(), forKey: SKAnnotationChoiceWidgetBackgroundColorKey)
-            userDefaults.safe_setColor(annotationModel.fontColor(), forKey: SKAnnotationChoiceWidgetFontColorKey)
-            userDefaults.set(annotationModel.fontName(), forKey: SKAnnotationChoiceWidgetFontNameKey)
-            userDefaults.set(annotationModel.fontSize(), forKey: SKAnnotationChoiceWidgetFontSizeKey)
+            if let model = self.annotationModel {
+                userDefaults.safe_setColor(model.backgroundColor(), forKey: SKAnnotationChoiceWidgetBackgroundColorKey)
+                userDefaults.safe_setColor(model.fontColor(), forKey: SKAnnotationChoiceWidgetFontColorKey)
+                userDefaults.set(model.fontName(), forKey: SKAnnotationChoiceWidgetFontNameKey)
+                userDefaults.set(model.fontSize(), forKey: SKAnnotationChoiceWidgetFontSizeKey)
+            }
         case .listMenu:
-            userDefaults.safe_setColor(annotationModel.backgroundColor(), forKey: SKAnnotationChoiceListWidgetBackgroundColorKey)
-            userDefaults.safe_setColor(annotationModel.fontColor(), forKey: SKAnnotationChoiceListWidgetFontColorKey)
-            userDefaults.set(annotationModel.fontName(), forKey: SKAnnotationChoiceListWidgetFontNameKey)
-            userDefaults.set(annotationModel.fontSize(), forKey: SKAnnotationChoiceListWidgetFontSizeKey)
+            if let model = self.annotationModel {
+                userDefaults.safe_setColor(model.backgroundColor(), forKey: SKAnnotationChoiceListWidgetBackgroundColorKey)
+                userDefaults.safe_setColor(model.fontColor(), forKey: SKAnnotationChoiceListWidgetFontColorKey)
+                userDefaults.set(model.fontName(), forKey: SKAnnotationChoiceListWidgetFontNameKey)
+                userDefaults.set(model.fontSize(), forKey: SKAnnotationChoiceListWidgetFontSizeKey)
+            }
         default:
             break
         }
@@ -288,7 +296,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
     }
     
     private func updateAnnotation() {
-        if annotationModel.annotation != nil {
+        if annotationModel?.annotation != nil {
             for tAnnotation in annotations {
                 tAnnotation.updateAppearanceStream()
                 pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
@@ -318,16 +326,16 @@ private enum KMPDFAnnotationFontWeightType: Int {
         }
     }
     
-    var annotationModel: CPDFAnnotationModel {
+    var annotationModel: CPDFAnnotationModel? {
         get {
-            return _annotationModel!
+            return _annotationModel
         }
         set {
             _annotationModel = newValue
             formMode = _annotationModel?.annotationType ?? .textField
             if _annotationModel?.annotations != nil {
-                if annotationModel.annotations.count > 0 {
-                    annotations = annotationModel.annotations as! [CPDFWidgetAnnotation]
+                if let cnt = annotationModel?.annotations.count, cnt > 0 {
+                    annotations = annotationModel?.annotations as? [CPDFWidgetAnnotation] ?? []
                 }
             }
         }
@@ -341,7 +349,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
                 tAnnotation.fontColor = colorPickerView.color
             }
         } else {
-            annotationModel.setFontColor(colorPickerView.color)
+            annotationModel?.setFontColor(colorPickerView.color)
             updateAnnotationMode()
         }
         updateAnnotation()
@@ -353,7 +361,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
                 tAnnotation.backgroundColor = fillColorPickerView.color
             }
         } else {
-            annotationModel.setBackgroundColor(fillColorPickerView.color)
+            annotationModel?.setBackgroundColor(fillColorPickerView.color)
             updateAnnotationMode()
         }
         updateAnnotation()
@@ -370,14 +378,14 @@ private enum KMPDFAnnotationFontWeightType: Int {
         let fontSizeString = fontSizeComboBox.stringValue.replacingOccurrences(of: " pt", with: "")
         let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSizeString.stringToCGFloat())
         var fontAlignment: KMFreeTextAnnotationAlignmentType = .left
-        if annotationModel.alignment() == .left {
+        if let data = annotationModel?.alignment(), data == .left {
             fontAlignment = .left
-        } else if annotationModel.alignment() == .center {
+        } else if let data = annotationModel?.alignment(), data == .center {
             fontAlignment = .center
-        } else if annotationModel.alignment() == .right {
+        } else if let data = annotationModel?.alignment(), data == .right {
             fontAlignment = .right
         }
-        let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString!, fontSize: fontSizeString.stringToCGFloat(), fontColor: colorPickerView.color ?? .black, fontAlignment: fontAlignment, annotationType: annotationModel.annotationType)
+        let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString!, fontSize: fontSizeString.stringToCGFloat(), fontColor: colorPickerView.color ?? .black, fontAlignment: fontAlignment, annotationType: annotationModel?.annotationType ?? .radioButton)
         
         let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
         let window = fontWindowController.window
@@ -415,7 +423,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
             self?.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
             self?.fontSizeComboBox.stringValue = String(format: "%.f pt", model.fontSize)
             
-            if self?.annotationModel.annotations != nil {
+            if self?.annotationModel?.annotations != nil {
                 let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [.family: model.fontName, .face: model.fontWeight])
                 let annotationFont = NSFont(descriptor: attributeFontDescriptor, size: model.fontSize)
 
@@ -434,15 +442,15 @@ private enum KMPDFAnnotationFontWeightType: Int {
                 }
                 self!.updateAnnotation()
             } else {
-                self?.annotationModel.setFontName(model.fontName)
-                self?.annotationModel.setFontSize(model.fontSize)
-                self?.annotationModel.setFontColor(model.fontColor)
+                self?.annotationModel?.setFontName(model.fontName)
+                self?.annotationModel?.setFontSize(model.fontSize)
+                self?.annotationModel?.setFontColor(model.fontColor)
                 if model.fontAlignment == .left {
-                    self?.annotationModel.setAlignment(.left)
+                    self?.annotationModel?.setAlignment(.left)
                 } else if model.fontAlignment == .center {
-                    self?.annotationModel.setAlignment(.center)
+                    self?.annotationModel?.setAlignment(.center)
                 } else if model.fontAlignment == .right {
-                    self?.annotationModel.setAlignment(.right)
+                    self?.annotationModel?.setAlignment(.right)
                 }
             }
         }
@@ -503,8 +511,8 @@ private enum KMPDFAnnotationFontWeightType: Int {
                     }
                 } else {
                     let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
-                    if let newFont = NSFont(descriptor: attributeFontDescriptor, size: annotationModel.fontSize()) {
-                        annotationModel.setFontName(newFont.fontName)
+                    if let newFont = NSFont(descriptor: attributeFontDescriptor, size: annotationModel?.fontSize() ?? 12.0) {
+                        annotationModel?.setFontName(newFont.fontName)
                     }
                     updateAnnotationMode()
 
@@ -522,7 +530,7 @@ private enum KMPDFAnnotationFontWeightType: Int {
                 }
             }
         } else {
-            annotationModel.setFontSize(CGFloat(fontSizeComboBox.floatValue))
+            annotationModel?.setFontSize(CGFloat(fontSizeComboBox.floatValue))
             updateAnnotationMode()
         }
         updateAnnotation()
@@ -554,8 +562,8 @@ private enum KMPDFAnnotationFontWeightType: Int {
             let resultAtt = NSMutableAttributedString(attributedString: fontPopUpButton.selectedItem!.attributedTitle!)
             let familyString = resultAtt.string
             let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
-            if let newFont = NSFont(descriptor: attributeFontDescriptor, size: annotationModel.fontSize()) {
-                annotationModel.setFontName(newFont.fontName)
+            if let newFont = NSFont(descriptor: attributeFontDescriptor, size: annotationModel?.fontSize() ?? 12.0) {
+                annotationModel?.setFontName(newFont.fontName)
             }
             updateAnnotationMode()
         }