Browse Source

【文字图片编辑】times字体调试

lizhe 1 year ago
parent
commit
a64d80684f

+ 38 - 19
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/EditPDF/KMEditPDFTextPropertyViewController.swift

@@ -145,20 +145,23 @@ class KMEditPDFTextPropertyViewController: NSViewController {
         fontNameVC?.selectItem(at: 0)
         fontNameVC?.delete = self
         
-        var font: NSFont
-        if fontStyle == "Regular" {
-            font = NSFont(name: fontName, size: CGFloat(fontSize))!
-        } else {
-            font = NSFont(name: "\(fontName)-\(fontStyle)", size: CGFloat(fontSize))!
-        }
+//        var font: NSFont
+//        if fontStyle == "Regular" {
+//            font = NSFont(name: fontName, size: CGFloat(fontSize))!
+//        } else {
+//            font = NSFont(name: "\(fontName)-\(fontStyle)", size: CGFloat(fontSize))!
+//        }
+//
+//        var styleArray: [String] = []
+//        for style in font.styles() {
+//            if style is String {
+//                let styleString = style as? String
+//                styleArray.append(styleString!)
+//            }
+//        }
+//
+        let styleArray = self.fetchFontStyleWithFontName(fontName: fontName)
         
-        var styleArray: [String] = []
-        for style in font.styles() {
-            if style is String {
-                let styleString = style as? String
-                styleArray.append(styleString!)
-            }
-        }
         fontStyleVC = KMDesignSelect.init(withType: .PopButton)
         fontStyleBox.contentView = fontStyleVC?.view
         fontStyleVC?.addItems(withObjectValues: styleArray)
@@ -679,7 +682,7 @@ extension KMEditPDFTextPropertyViewController {
     
     func updateFontNameAndStyle(name: String, style: String, needChangeListView: Bool = true) {
         if fontName != name || fontStyle != style {
-            let styleArray = self.updateFontStyleWithFontName(fontName: name, currentStyle: "")
+            let styleArray = self.fetchFontStyleWithFontName(fontName: name)
             
             let fontName = "\(name)-\(style)"
             
@@ -776,16 +779,32 @@ extension KMEditPDFTextPropertyViewController {
 //        [self createFreeTextProperties];
 //    }
     
-    func updateFontStyleWithFontName(fontName: String, currentStyle style: String) -> [String] {
+    func fetchFontStyleWithFontName(fontName: String) -> [String] {
         var styleArray: [String] = []
-        for i in 0 ... self.fonts.count-1 {
-            let dic : NSDictionary = self.fonts[i] 
+        let fontArray = CPDFAnnotationModel.supportFonts() as! [NSDictionary]
+        
+        for i in 0 ... fontArray.count-1 {
+            let dic : NSDictionary = fontArray[i]
             let familyString = dic.allKeys.first as! String
-            if fontName == familyString {
+            
+            let fontNameArray = fontName.components(separatedBy: "-")
+            var name = ""
+            if fontNameArray.count > 0 {
+                name = fontNameArray.first!
+            } else {
+                name = "Helvetica"
+            }
+            
+            if name == familyString {
                 let styles : NSArray = dic.object(forKey: fontName) as! NSArray
                 for j in 0 ... styles.count-1 {
                     let style = styles.object(at: j) as! String
-                    let attributeFontDescriptor = NSFontDescriptor.init(fontAttributes: [NSFontDescriptor.AttributeName.family : fontName,NSFontDescriptor.AttributeName.face : style])
+                    var attributeFontDescriptor = NSFontDescriptor()
+                    if style == "Regular" || style == "Roman" {
+                        attributeFontDescriptor = NSFontDescriptor.init(fontAttributes: [NSFontDescriptor.AttributeName.family : fontName])
+                    } else {
+                        attributeFontDescriptor = NSFontDescriptor.init(fontAttributes: [NSFontDescriptor.AttributeName.family : fontName,NSFontDescriptor.AttributeName.face : style])
+                    }
                     let font = NSFont.init(descriptor: attributeFontDescriptor, size: 12)
                     let attrited : [NSAttributedString.Key : Any] = [NSAttributedString.Key.font:font!]
                     let string : NSAttributedString = NSAttributedString(string: style, attributes: attrited)