Просмотр исходного кода

Merge branch 'develop_2025' of git.kdan.cc:Mac_PDF/PDF_Office into develop_2025

niehaoyu 2 месяцев назад
Родитель
Сommit
7b98ea23a9

+ 2 - 2
PDF Office/PDF Master/Class/Common/OC/OCR/KMGOCRManager.h

@@ -9,8 +9,8 @@
 #import <Foundation/Foundation.h>
 
 typedef NS_ENUM(NSUInteger, KMOCRType) {
-    KMOCRType_Google,
-    KMOCRType_Apple
+    KMOCRType_Google, //在线
+    KMOCRType_Apple //离线
 };
 
 extern NSString * KMGOCRLanguageCodeKey;

+ 1 - 0
PDF Office/PDF Master/Class/Common/OC/OCR/KMGOCRManager.m

@@ -79,6 +79,7 @@ NSString * KMGOCRLanguageStringKey = @"KMGOCRLanguageStringKey";
     static dispatch_once_t pred;
     dispatch_once(&pred, ^{
         singleton = [[KMGOCRManager alloc] init];
+        singleton.OCRType = KMOCRType_Google;
     });
     return singleton;
 }

+ 18 - 2
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/KMOCRController.swift

@@ -16,7 +16,12 @@ class KMOCRController: NSViewController {
     @IBOutlet weak var pageView: KMOCRPageView!
     @IBOutlet weak var areaView: KMOCRAreaView!
     
-    var model: KMOCRModel = KMOCRModel()
+    var model: KMOCRModel = KMOCRModel() {
+        didSet {
+            self.pageView.model = model;
+            self.areaView.model = model
+        }
+    }
     
     private var fileTabProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: KMLocalizedString("Page"))
     private var areaTabProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: KMLocalizedString("Area"))
@@ -24,6 +29,7 @@ class KMOCRController: NSViewController {
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do view setup here.
+        self.model = KMOCRModel()
         self.updateUI()
         self.reloadData()
     }
@@ -36,6 +42,10 @@ class KMOCRController: NSViewController {
         
         saveButton.properties = ComponentButtonProperty(type: .primary, size: .m, buttonText: KMLocalizedString("Apply"), keepPressState: false)
         saveButton.setTarget(self, action: #selector(saveButtonClicked(_:)))
+        
+        self.pageView.changeAction = { [unowned self] view, model in
+            self.reloadData()
+        }
     }
     
     func reloadData() {
@@ -47,13 +57,19 @@ class KMOCRController: NSViewController {
             pageView.isHidden = false
             areaView.isHidden = true
             fileTabProperty.state = .pressed
-            saveButton.properties.buttonText = KMLocalizedString("Apply")
+            
+            if model.saveAsPDF {
+                saveButton.properties.buttonText = KMLocalizedString("Save as PDF")
+            } else {
+                saveButton.properties.buttonText = KMLocalizedString("Apply")
+            }
         } else {
             pageView.isHidden = true
             areaView.isHidden = false
             areaTabProperty.state = .pressed
             saveButton.properties.buttonText = KMLocalizedString("Save as TXT")
         }
+        
         saveButton.reloadData()
         typeTabs.refreshItems()
     }

+ 20 - 0
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Manager/KMOCRManager.swift

@@ -0,0 +1,20 @@
+//
+//  KMOCRManager.swift
+//  PDF Reader Pro
+//
+//  Created by lizhe on 2024/12/23.
+//
+
+import Cocoa
+
+class KMOCRManager: NSObject {
+    static let manager = KMOCRManager()
+    
+    let languages: [String] = (KMGOCRManager.languages() as NSArray).value(forKeyPath: KMGOCRLanguageStringKey) as! [String]
+    var type: KMOCRType = .google {
+        didSet {
+            KMGOCRManager.default().ocrType = type
+        }
+    }
+    
+}

+ 6 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Model/KMOCRModel.swift

@@ -14,5 +14,10 @@ enum KMOCRShowType : Int {
 
 class KMOCRModel: NSObject {
     var showType: KMOCRShowType = .page
-    
+    var ocrType: KMOCRType = .google
+    var language: String = ""
+    var imageEnhancement: Bool = false
+    var imageCorrection: Bool = false
+    var pageRange: String = ""
+    var saveAsPDF: Bool = false
 }

+ 41 - 27
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/View/Area/KMOCRAreaView.swift

@@ -16,7 +16,14 @@ class KMOCRAreaView: BaseXibView {
     @IBOutlet weak var loadingProgress: ComponentProgressCircle!
     @IBOutlet weak var textArea: ComponentTextarea!
     
+    var selectIndex: Int = 0
     
+    var model: KMOCRModel = KMOCRModel() {
+        didSet {
+            self.reloadData()
+        }
+    }
+        
     override func draw(_ dirtyRect: NSRect) {
         super.draw(dirtyRect)
 
@@ -36,6 +43,7 @@ class KMOCRAreaView: BaseXibView {
     
     override func setup() {
         self.updateUI()
+        self.reloadData()
     }
     
     func updateUI() {
@@ -50,39 +58,45 @@ class KMOCRAreaView: BaseXibView {
                                                               errorText: nil,
                                                               creatable: false,
                                                               text: NSLocalizedString("", comment: ""))
-        var menuItemArr: [ComponentMenuitemProperty] = []
-//        for familyName in CPDFFont.familyNames {
-//            let fontNames = CPDFFont.fontNames(forFamilyName: familyName)
-//            let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
-//                                                                                    itemSelected: false,
-//                                                                                    isDisabled: false,
-//                                                                                    keyEquivalent: nil,
-//                                                                                    text: familyName,identifier: "1")
-//            var subMenuItemArr: [ComponentMenuitemProperty] = []
-//
-//            for fontName in fontNames {
-//                let subItemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
-//                                                                                           itemSelected: false,
-//                                                                                           isDisabled: false,
-//                                                                                           keyEquivalent: nil,
-//                                                                                           text: fontName,identifier: "2")
-//                subMenuItemArr.append(subItemProperty)
-//            }
-//
-//            itemProperty.subPropertys = subMenuItemArr
-//            menuItemArr.append(itemProperty)
-//        }
-        
-        languageSelectButton.updateMenuItemsArr(menuItemArr)
+        self.updateLanguages()
         languageSelectButton.delegate = self
         
-//        enhancementSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
-//        enhancementSwitch.setTarget(self, action: #selector(switchAction(_:)))
-        
         textArea.properties = ComponentTextareaProperty(size: .m, state: .normal)
         textArea.delegate = self
     }
+ 
+    func reloadData() {
+        
+    }
     
+    func updateLanguages() {
+        languageSelectButton.properties = ComponentSelectProperties(size: .s,
+                                                              state: .normal,
+                                                              isDisabled: false,
+                                                              isError: false,
+                                                              leftIcon: false,
+                                                              placeholder: nil,
+                                                              errorText: nil,
+                                                              creatable: false,
+                                                              text: NSLocalizedString("", comment: ""))
+        var menuItemArr: [ComponentMenuitemProperty] = []
+        var languages: [String] = KMOCRManager.manager.languages
+        
+        languages.insert(KMLocalizedString("Auto Detection"), at: 0)
+        for language in languages  {
+            let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
+                                                                                    itemSelected: false,
+                                                                                    isDisabled: false,
+                                                                                    keyEquivalent: nil,
+                                                                                    text: language, identifier: "1")
+            menuItemArr.append(itemProperty)
+        }
+        
+        languageSelectButton.updateMenuItemsArr(menuItemArr)
+        
+        let position = languages.firstIndex(of: model.language) ?? 0
+        self.selectIndex = position
+    }
 }
 
 //MARK: Action

+ 88 - 27
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/View/Page/KMOCRPageView.swift

@@ -8,6 +8,8 @@
 import Cocoa
 import KMComponentLibrary
 
+typealias KMOCRPageViewValueChangeAction = (_ view: KMOCRPageView, _ model: KMOCRModel) -> (Void)
+
 class KMOCRPageView: BaseXibView {
     @IBOutlet weak var ocrPlanLabel: NSTextField!
     @IBOutlet weak var OCRPlan1Button: ComponentRadio!
@@ -30,6 +32,16 @@ class KMOCRPageView: BaseXibView {
     
     @IBOutlet weak var saveAsPDFButton: ComponentCheckBox!
     
+    var selectIndex: Int = 0
+    
+    var changeAction: KMOCRPageViewValueChangeAction?
+    
+    var model: KMOCRModel = KMOCRModel() {
+        didSet {
+            self.updateLanguages()
+            self.reloadData()
+        }
+    }
     override func draw(_ dirtyRect: NSRect) {
         super.draw(dirtyRect)
 
@@ -63,6 +75,7 @@ class KMOCRPageView: BaseXibView {
     
     override func setup() {
         self.updateUI()
+        self.reloadData()
     }
     
     func updateUI() {
@@ -76,6 +89,41 @@ class KMOCRPageView: BaseXibView {
         OCRPlan2ButtonWidthConstraint.constant = OCRPlan2Button.properties.propertyInfo.viewWidth
         OCRPlan2Button.reloadData()
         
+        self.updateLanguages()
+        languageSelectButton.delegate = self
+        
+        enhancementSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
+        enhancementSwitch.setTarget(self, action: #selector(switchAction(_:)))
+        
+        correctionSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
+        correctionSwitch.setTarget(self, action: #selector(switchAction(_:)))
+        
+        saveAsPDFButton.setTarget(self, action: #selector(saveAsPDFAction(_:)))
+    }
+    
+    func reloadData() {
+        
+        languageSelectButton.selectItemAtIndex(self.selectIndex)
+        
+        self.OCRPlan1Button.properties.checkboxType = .normal
+        self.OCRPlan2Button.properties.checkboxType = .normal
+        if model.ocrType == .google {
+            self.OCRPlan1Button.properties.checkboxType = .selected
+        } else {
+            self.OCRPlan2Button.properties.checkboxType = .selected
+        }
+        self.OCRPlan1Button.reloadData()
+        self.OCRPlan2Button.reloadData()
+        
+        
+        self.saveAsPDFButton.properties.state = model.saveAsPDF ? .pressed : .normal
+        self.enhancementSwitch.properties.open = model.imageEnhancement
+        self.correctionSwitch.properties.open = model.imageCorrection
+        
+        self.pageSelectButton.pageSelectView.selectItemAtIndex(0)
+    }
+    
+    func updateLanguages() {
         languageSelectButton.properties = ComponentSelectProperties(size: .s,
                                                               state: .normal,
                                                               isDisabled: false,
@@ -86,51 +134,64 @@ class KMOCRPageView: BaseXibView {
                                                               creatable: false,
                                                               text: NSLocalizedString("", comment: ""))
         var menuItemArr: [ComponentMenuitemProperty] = []
-//        for familyName in CPDFFont.familyNames {
-//            let fontNames = CPDFFont.fontNames(forFamilyName: familyName)
-//            let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
-//                                                                                    itemSelected: false,
-//                                                                                    isDisabled: false,
-//                                                                                    keyEquivalent: nil,
-//                                                                                    text: familyName,identifier: "1")
-//            var subMenuItemArr: [ComponentMenuitemProperty] = []
-//            
-//            for fontName in fontNames {
-//                let subItemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
-//                                                                                           itemSelected: false,
-//                                                                                           isDisabled: false,
-//                                                                                           keyEquivalent: nil,
-//                                                                                           text: fontName,identifier: "2")
-//                subMenuItemArr.append(subItemProperty)
-//            }
-//            
-//            itemProperty.subPropertys = subMenuItemArr
-//            menuItemArr.append(itemProperty)
-//        }
+        var languages: [String] = KMOCRManager.manager.languages
         
-        languageSelectButton.updateMenuItemsArr(menuItemArr)
-        languageSelectButton.delegate = self
+        languages.insert(KMLocalizedString("Auto Detection"), at: 0)
+        for language in languages  {
+            let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
+                                                                                    itemSelected: false,
+                                                                                    isDisabled: false,
+                                                                                    keyEquivalent: nil,
+                                                                                    text: language, identifier: "1")
+            menuItemArr.append(itemProperty)
+        }
         
-        enhancementSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
-        enhancementSwitch.setTarget(self, action: #selector(switchAction(_:)))
+        languageSelectButton.updateMenuItemsArr(menuItemArr)
         
-        correctionSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
-        correctionSwitch.setTarget(self, action: #selector(switchAction(_:)))
+        let position = languages.firstIndex(of: model.language) ?? 0
+        self.selectIndex = position
     }
 }
 
 //MARK: Action
 extension KMOCRPageView {
     func OCRPlan1ButtonAction(_ sender: ComponentCheckBox) {
+        model.ocrType = .google
+        self.reloadData()
         
+        guard let callBack = changeAction else { return }
+        
+        callBack(self, model)
     }
     
     func OCRPlan2ButtonAction(_ sender: ComponentCheckBox) {
+        model.ocrType = .apple
+        self.reloadData()
+        
+        guard let callBack = changeAction else { return }
         
+        callBack(self, model)
     }
     
     func switchAction(_ sender: ComponentSwitch) {
+        if sender == enhancementSwitch {
+            model.imageEnhancement = sender.properties.open
+        } else if sender == correctionSwitch {
+            model.imageCorrection = sender.properties.open
+        }
+//        self.reloadData()
+        guard let callBack = changeAction else { return }
+        
+        callBack(self, model)
+    }
+    
+    func saveAsPDFAction(_ sender: ComponentCheckBox) {
+        model.saveAsPDF = (sender.properties.checkboxType == .normal) ? false : true
+//        self.reloadData()
+        
+        guard let callBack = changeAction else { return }
         
+        callBack(self, model)
     }
 }
 

+ 18 - 0
PDF Office/PDF Reader Pro.xcodeproj/project.pbxproj

@@ -1388,6 +1388,9 @@
 		AD8E48A92D154E120055D3E6 /* KMOCRPageView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AD8E48A82D154E120055D3E6 /* KMOCRPageView.xib */; };
 		AD8E48AA2D154E120055D3E6 /* KMOCRPageView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AD8E48A82D154E120055D3E6 /* KMOCRPageView.xib */; };
 		AD8E48AB2D154E120055D3E6 /* KMOCRPageView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AD8E48A82D154E120055D3E6 /* KMOCRPageView.xib */; };
+		AD8E48B22D1904320055D3E6 /* KMOCRManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8E48B12D1904320055D3E6 /* KMOCRManager.swift */; };
+		AD8E48B32D1904320055D3E6 /* KMOCRManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8E48B12D1904320055D3E6 /* KMOCRManager.swift */; };
+		AD8E48B42D1904320055D3E6 /* KMOCRManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8E48B12D1904320055D3E6 /* KMOCRManager.swift */; };
 		AD8F06172999DB5900D93CBC /* KMPrintDrawPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8F06162999DB5900D93CBC /* KMPrintDrawPage.swift */; };
 		AD8F06182999DB5900D93CBC /* KMPrintDrawPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8F06162999DB5900D93CBC /* KMPrintDrawPage.swift */; };
 		AD8F06192999DB5900D93CBC /* KMPrintDrawPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8F06162999DB5900D93CBC /* KMPrintDrawPage.swift */; };
@@ -5165,6 +5168,7 @@
 		AD8E48A02D1542630055D3E6 /* KMOCRPageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMOCRPageView.swift; sourceTree = "<group>"; };
 		AD8E48A42D154E070055D3E6 /* KMOCRAreaView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMOCRAreaView.xib; sourceTree = "<group>"; };
 		AD8E48A82D154E120055D3E6 /* KMOCRPageView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMOCRPageView.xib; sourceTree = "<group>"; };
+		AD8E48B12D1904320055D3E6 /* KMOCRManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMOCRManager.swift; sourceTree = "<group>"; };
 		AD8F06162999DB5900D93CBC /* KMPrintDrawPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMPrintDrawPage.swift; sourceTree = "<group>"; };
 		AD9527B9295291F20039D2BC /* KMPrintPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMPrintPage.swift; sourceTree = "<group>"; };
 		AD9527BD295294E20039D2BC /* KMPrintPageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMPrintPageModel.swift; sourceTree = "<group>"; };
@@ -8346,6 +8350,14 @@
 			path = Model;
 			sourceTree = "<group>";
 		};
+		AD8E48AF2D19038B0055D3E6 /* Manager */ = {
+			isa = PBXGroup;
+			children = (
+				AD8E48B12D1904320055D3E6 /* KMOCRManager.swift */,
+			);
+			path = Manager;
+			sourceTree = "<group>";
+		};
 		AD9527B8295291BD0039D2BC /* Category */ = {
 			isa = PBXGroup;
 			children = (
@@ -8758,6 +8770,7 @@
 		ADD3EBEB2D13EA54005B711B /* OCR */ = {
 			isa = PBXGroup;
 			children = (
+				AD8E48AF2D19038B0055D3E6 /* Manager */,
 				AD8E48962D153D070055D3E6 /* Model */,
 				ADD3EC062D13F26F005B711B /* View */,
 				ADD3EBFE2D13F23D005B711B /* KMOCRController.swift */,
@@ -14092,6 +14105,7 @@
 				AD1CA4052A06040B0070541F /* KMAnnotationScreenCollectionView.xib in Resources */,
 				BB716D6D2CDDB727009787ED /* KMHeaderPropertyController.xib in Resources */,
 				AD3AAD4F2B0B7B9300DE5FE7 /* KMCompareTextView.xib in Resources */,
+				BB65A0522AF8B64B003A27A0 /* DisplayPreferences.xib in Resources */,
 				AD8E48A72D154E070055D3E6 /* KMOCRAreaView.xib in Resources */,
 				F337CC212CC6456D00D46AF4 /* KMNThumbnailBaseViewController.xib in Resources */,
 				9F8539FE2947137500DF644E /* throbber_waiting.png in Resources */,
@@ -14634,6 +14648,7 @@
 				F3A65ECD2CDF19C300F00102 /* KMNThumDecorationView.swift in Sources */,
 				BB9AEB4D2D0FC9E1004BF8D2 /* FormsButtonController.swift in Sources */,
 				BB328B632B55512F00B382C6 /* KMVerificationMessageViewController.m in Sources */,
+				AD8E48B22D1904320055D3E6 /* KMOCRManager.swift in Sources */,
 				89D9895E28FD21E6003A3E87 /* KMAnnotationCollectionView.swift in Sources */,
 				BB65A07C2AF8E5A4003A27A0 /* KMLineWell.swift in Sources */,
 				BBD1F793296FE92500343885 /* KMPageEditSplitSettingView.swift in Sources */,
@@ -15455,6 +15470,8 @@
 				BB5DA54B2BCFF4B300849E86 /* KMPageEditPopViewController.m in Sources */,
 				9FF371D62C69B8C9005F9CC5 /* CAreaMeasureInfoWindowController.swift in Sources */,
 				ADDF83362B391A5C00A81A4E /* CPDFListViewConfig.m in Sources */,
+				AD8E48B32D1904320055D3E6 /* KMOCRManager.swift in Sources */,
+				AD867FB429DFBB2700F00440 /* KMAnnotationOutlineSectionView.swift in Sources */,
 				BB792A052CF0280E006FFD5D /* KMFileThumbManager.swift in Sources */,
 				9F8539BF2943085A00DF644E /* KMBrowser.swift in Sources */,
 				BB7FF5082A60E84400901C2D /* KMEnumExtensions.swift in Sources */,
@@ -16601,6 +16618,7 @@
 				BB74DA792AC41182006EDFE7 /* NSFont+KMExtension.swift in Sources */,
 				BB19A7332CB79D6E008204DC /* KMHomeRecommondView.swift in Sources */,
 				BBFEF7212B3A787900C28AC0 /* KMSystemAnnotationMenu.swift in Sources */,
+				AD8E48B42D1904320055D3E6 /* KMOCRManager.swift in Sources */,
 				BBE789162CBD2464008086E2 /* DividerDemoVC.swift in Sources */,
 				9F1FE4F529406E4700E952CA /* FastResizeView.m in Sources */,
 				9F1FE4FE29406E4700E952CA /* CTTabStripController.m in Sources */,