Browse Source

【注释】修复“添加按钮form,属性面板,点击外观,崩溃”

wanjun 10 months ago
parent
commit
d46c888c20

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

@@ -148,8 +148,10 @@ private enum KMPDFAnnotationFontWeightType: Int {
             var family = ""
             var style = ""
             if annotations.count > 0 {
-                family = annotation?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String ?? ""
-                style = annotation?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String ?? ""
+                if annotation?.font != nil {
+                    family = annotation?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String ?? ""
+                    style = annotation?.font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String ?? ""
+                }
             } else {
                 var annotationFont: NSFont
                 if annotationModel.fontName() != nil {
@@ -172,8 +174,10 @@ private enum KMPDFAnnotationFontWeightType: Int {
                     menu.addItem(item)
                     
                     if annotations.count > 0 {
-                        if annotation?.font.fontName == font.fontName {
-                            selectedIndex = index
+                        if annotation?.font != nil {
+                            if annotation?.font.fontName == font.fontName {
+                                selectedIndex = index
+                            }
                         }
                     } else {
                         if annotationModel.fontName() == font.fontName {
@@ -372,38 +376,93 @@ private enum KMPDFAnnotationFontWeightType: Int {
     }
     
     @IBAction func buttonClicked_ChangeFont(_ sender: Any) {
-//        guard let weakSelf = self else { return }
+        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())
+        var fontAlignment: KMFreeTextAnnotationAlignmentType = .left
+        if annotationModel.alignment() == .left {
+            fontAlignment = .left
+        } else if annotationModel.alignment() == .center {
+            fontAlignment = .center
+        } else if annotationModel.alignment() == .right {
+            fontAlignment = .right
+        }
+        let fontModel = KMFontModel(fontName: familyString, fontWeight: styleString!, fontSize: fontSizeString.stringToCGFloat(), fontColor: colorPickerView.color ?? .black, fontAlignment: fontAlignment, annotationType: annotationModel.annotationType)
+        
+        let fontWindowController = KMAnnotationFontWindowController.initWindowController(fontModel)
+        let window = fontWindowController.window
+        fontWindowController.callback = { [weak self] model in
+            self!.colorPickerView.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 {
+                    if self != nil {
+                        self!.fontPopUpButton.menu = menu
+                        self!.fontPopUpButton.selectItem(at: selectedIndex)
+                    }
+                }
+            }
+            let selectedStyleIndex = self!.setFontStyleWithFontName(model.fontName, currentStyle: model.fontWeight)
+            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)
 
-//        let fontWindowController = KMAnnotationFontWindowController.sharedAnnotationFont()
-//        guard let window = fontWindowController.window else { return }
-//
-//        fontWindowController.annotations = weakSelf.annotations
-//
-//        fontWindowController.annotationAlignCallback = { selectedCount in
-//            for tAnnotation in weakSelf.annotations {
-//                tAnnotation.removeAllAppearanceStreams()
-//
-//                switch selectedCount {
-//                case 0:
-//                    tAnnotation.alignment = .left
-//                case 2:
-//                    tAnnotation.alignment = .center
-//                case 1:
-//                    tAnnotation.alignment = .right
-//                case 3:
-//                    tAnnotation.alignment = .justified
-//                default:
-//                    break
-//                }
-//            }
-//            weakSelf.pdfview.setNeedsDisplay(true)
-//        }
-//
-//        fontWindowController.annotationCallback = { annotation in
-//            // Handle annotation callback if needed
-//        }
-//
-//        window.orderFront(sender)
+                for tAnnotation in self!.annotations {
+                    if tAnnotation is CPDFAnnotation {
+                        (tAnnotation as! CPDFButtonWidgetAnnotation).font = annotationFont
+                        (tAnnotation as! CPDFButtonWidgetAnnotation).fontColor = model.fontColor
+//                        if model.fontAlignment == .left {
+//                            (tAnnotation as! CPDFButtonWidgetAnnotation).alignment = .left
+//                        } else if model.fontAlignment == .center {
+//                            (tAnnotation as! CPDFTextWidgetAnnotation).alignment = .center
+//                        } else if model.fontAlignment == .right {
+//                            (tAnnotation as! CPDFTextWidgetAnnotation).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 fontColorButtonAction(_ sender: NSButton) {