Browse Source

【注释】修复“文本域外观,字重显示是乱的”

wanjun 10 tháng trước cách đây
mục cha
commit
8542f13f0c

+ 76 - 48
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/AnnotationProperty/ViewController/FormProperties/KMAnnotationFromViewController.swift

@@ -314,19 +314,20 @@ import Cocoa
     func setFontStyle(withFontName fontName: String, currentStyle style: String?) -> UInt {
     func setFontStyle(withFontName fontName: String, currentStyle style: String?) -> UInt {
         var selectIndex: UInt = 0
         var selectIndex: UInt = 0
         let menu = NSMenu()
         let menu = NSMenu()
-        let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName)
-        if fontFamily != nil {
-            for (index, array) in fontFamily!.enumerated() {
-                let styleName = array[1]
-                if let currentStyle = style, styleName as! String == currentStyle {
-                    selectIndex = UInt(index)
-                }
-                let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
-                if let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0) {
+        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 attrited = [NSAttributedString.Key.font: font]
-                    let string = NSAttributedString(string: styleName as! String, attributes: attrited)
+                    let string = NSAttributedString(string: styleName, attributes: attrited as [NSAttributedString.Key : Any])
                     let item = NSMenuItem()
                     let item = NSMenuItem()
                     item.attributedTitle = string
                     item.attributedTitle = string
+                    
                     menu.addItem(item)
                     menu.addItem(item)
                 }
                 }
             }
             }
@@ -476,57 +477,84 @@ import Cocoa
     }
     }
     
     
     @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
     @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
-        if let selectedItem = fontPopUpButton.selectedItem {
-            let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
-            let familyString = resultAtt.string
-            let selectIndex = setFontStyle(withFontName: familyString, currentStyle: nil)
-            let styleString = fontStylePopUpButton.selectedItem?.title ?? ""
-            fontStylePopUpButton.selectItem(at: Int(selectIndex))
-            
-            if annotations.count > 0 {
-                for tAnnotation in annotations {
-                    var annotationString = ""
-                    
-                    if tAnnotation.buttonWidgetStateString().isEmpty {
-                        annotationString = " "
-                    } else {
-                        let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
-                        if lastString == " " {
-                            annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
-                        } else {
-                            annotationString = tAnnotation.buttonWidgetStateString() + " "
+        guard let selectedItem = fontPopUpButton.selectedItem else { return }
+        
+        let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
+        let familyString = resultAtt.string
+        let selectIndex = setFontStyle(withFontName: 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(withFontName: family, currentStyle: style)
+                                    self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
+                                }
+                            }
                         }
                         }
                     }
                     }
-                    
-                    if let font = tAnnotation.font {
-                        if let font = (annotation as! CPDFTextWidgetAnnotation).font {
-                            let fontSize = font.pointSize
+                } 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])
                             let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
-                            if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize) {
-                                tAnnotation.font = newFont
+                            if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
+                                newAnnotation.font = newFont
                             }
                             }
                         }
                         }
                         
                         
-                        if let family = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
-                           let style = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
-                            DispatchQueue.main.async {
-                                let selectedStyleIndex = self.setFontStyle(withFontName: family, currentStyle: style)
-                                self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
+                        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(withFontName: family, currentStyle: style)
+                                    self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
+                                }
                             }
                             }
                         }
                         }
                     }
                     }
-                    
-                    tAnnotation.setButtonWidgetStateString(annotationString)
                 }
                 }
-            } 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)
+            }
+        } else {
+            if annotationModel.annotationType == .freeText || annotationModel.annotationType == .signText || annotationModel.annotationType == .signDate {
+                let font = NSFont(name: (annotationModel.fontName())!, size: (annotationModel.fontSize())) ?? 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: (annotationModel.fontName())!, size: (annotationModel.fontSize())) ?? 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(withFontName: family, currentStyle: style)
+                        self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
+                    }
                 }
                 }
-                updateAnnotationMode()
             }
             }
-            updateAnnotation()
         }
         }
+        
+        updateAnnotation()
     }
     }
     
     
     @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
     @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {