From d524a512fbea37070bc63f065a3474ac08027838 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 10 Mar 2022 17:02:05 +0800 Subject: [PATCH] IME // Reintroducing OpenCCBridge with CrossConvert support. - This is only for cross-converting in-place user phrases. --- .../3rdParty/OpenCCBridge/OpenCCBridge.swift | 52 +++++++++++++ vChewing.xcodeproj/project.pbxproj | 73 +++++++++++++++++-- 2 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 Source/3rdParty/OpenCCBridge/OpenCCBridge.swift diff --git a/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift b/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift new file mode 100644 index 00000000..f1f23af8 --- /dev/null +++ b/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift @@ -0,0 +1,52 @@ +// Copyright (c) 2011 and onwards The OpenVanilla Project (MIT License). +// All possible vChewing-specific modifications are (c) 2021 and onwards The vChewing Project (MIT-NTL License). +/* +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and +to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +2. No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, + except as required to fulfill notice requirements above. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +import Foundation +import OpenCC + +/// A bridge to let Objctive-C code to access SwiftyOpenCC. +/// +/// Since SwiftyOpenCC only provide Swift classes, we create an NSObject subclass +/// in Swift in order to bridge the Swift classes into our Objective-C++ project. +public class OpenCCBridge: NSObject { + private static let shared = OpenCCBridge() + private var simplify: ChineseConverter? + private var traditionalize: ChineseConverter? + + private override init() { + try? simplify = ChineseConverter(options: .simplify) + try? traditionalize = ChineseConverter(options: [.traditionalize, .twStandard]) + super.init() + } + + /// CrossConvert. + /// + /// - Parameter string: Text in Original Script. + /// - Returns: Text converted to Different Script. + @objc public static func crossConvert(_ string: String) -> String? { + switch ctlInputMethod.currentKeyHandler.inputMode { + case InputMode.imeModeCHS: + return shared.traditionalize?.convert(string) + case InputMode.imeModeCHT: + return shared.simplify?.convert(string) + default: + return string + } + } +} diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index eef59a57..971ec6e3 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ @@ -31,6 +31,8 @@ 5B62A34927AE7CD900A19448 /* TooltipController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A34327AE7CD900A19448 /* TooltipController.swift */; }; 5B62A34A27AE7CD900A19448 /* NotifierController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A34527AE7CD900A19448 /* NotifierController.swift */; }; 5B62A35327AE89C400A19448 /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33127AE792F00A19448 /* InputSourceHelper.swift */; }; + 5B707CE827D9F4590099EF99 /* OpenCCBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B707CE727D9F4590099EF99 /* OpenCCBridge.swift */; }; + 5B707CEC27D9F4870099EF99 /* OpenCC in Frameworks */ = {isa = PBXBuildFile; productRef = 5B707CEB27D9F4870099EF99 /* OpenCC */; }; 5B73FB5E27B2BE1300E9BF49 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5B73FB6027B2BE1300E9BF49 /* InfoPlist.strings */; }; 5B7BC4B027AFFBE800F66C24 /* frmPrefWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5B7BC4AE27AFFBE800F66C24 /* frmPrefWindow.xib */; }; 5BAD0CD527D701F6003D127F /* vChewingKeyLayout.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 5B30F11227BA568800484E24 /* vChewingKeyLayout.bundle */; }; @@ -193,6 +195,8 @@ 5B62A34127AE7CD900A19448 /* VerticalCandidateController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerticalCandidateController.swift; sourceTree = ""; }; 5B62A34327AE7CD900A19448 /* TooltipController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TooltipController.swift; sourceTree = ""; }; 5B62A34527AE7CD900A19448 /* NotifierController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotifierController.swift; sourceTree = ""; }; + 5B707CE527D9F3A10099EF99 /* SwiftyOpenCC */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SwiftyOpenCC; path = Packages/SwiftyOpenCC; sourceTree = ""; }; + 5B707CE727D9F4590099EF99 /* OpenCCBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenCCBridge.swift; sourceTree = ""; }; 5B73FB5427B2BD6900E9BF49 /* PhraseEditor-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "PhraseEditor-Info.plist"; path = "UserPhraseEditor/PhraseEditor-Info.plist"; sourceTree = SOURCE_ROOT; }; 5B73FB5F27B2BE1300E9BF49 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 5B7BC4AF27AFFBE800F66C24 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Source/WindowNIBs/Base.lproj/frmPrefWindow.xib; sourceTree = ""; }; @@ -307,6 +311,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 5B707CEC27D9F4870099EF99 /* OpenCC in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -352,6 +357,7 @@ 5B62A30127AE732800A19448 /* 3rdParty */ = { isa = PBXGroup; children = ( + 5B707CE627D9F43E0099EF99 /* OpenCCBridge */, 5B62A31427AE73A700A19448 /* AWFileHash */, 5B62A30727AE73A700A19448 /* SSZipArchive */, 5B62A30227AE733500A19448 /* OVMandarin */, @@ -582,6 +588,22 @@ name = Data; sourceTree = ""; }; + 5B707CE427D9F3A10099EF99 /* Packages */ = { + isa = PBXGroup; + children = ( + 5B707CE527D9F3A10099EF99 /* SwiftyOpenCC */, + ); + name = Packages; + sourceTree = ""; + }; + 5B707CE627D9F43E0099EF99 /* OpenCCBridge */ = { + isa = PBXGroup; + children = ( + 5B707CE727D9F4590099EF99 /* OpenCCBridge.swift */, + ); + path = OpenCCBridge; + sourceTree = ""; + }; 5BBBB75C27AED54C0023B93A /* SoundFiles */ = { isa = PBXGroup; children = ( @@ -651,6 +673,7 @@ 6A0D4E9215FC0CFA00ABF4B3 = { isa = PBXGroup; children = ( + 5B707CE427D9F3A10099EF99 /* Packages */, 5BBD627827B6C4D900271480 /* Update-Info.plist */, 5B18BA7027C7BD8B0056EB19 /* Makefile */, 5B0C5EDE27C7D94C0078037C /* DataCompiler */, @@ -802,11 +825,13 @@ buildRules = ( ); dependencies = ( + 5B707CEA27D9F47A0099EF99 /* PBXTargetDependency */, 5B0AF8B327B2C4E20096FE54 /* PBXTargetDependency */, 6A38BC2615FC131100A8A51F /* PBXTargetDependency */, ); name = vChewing; packageProductDependencies = ( + 5B707CEB27D9F4870099EF99 /* OpenCC */, ); productName = vChewing; productReference = 6A0D4EA215FC0D2D00ABF4B3 /* vChewing.app */; @@ -978,6 +1003,7 @@ files = ( 5B62A31927AE73A700A19448 /* ioapi.m in Sources */, 5B62A31C27AE73A700A19448 /* AWFileHash.m in Sources */, + 5B707CE827D9F4590099EF99 /* OpenCCBridge.swift in Sources */, D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */, 6ACC3D442793701600F1B140 /* ParselessPhraseDB.cpp in Sources */, D461B792279DAC010070E734 /* InputState.swift in Sources */, @@ -1037,6 +1063,10 @@ target = 5BD05BB727B2A429004C4F1D /* vChewingPhraseEditor */; targetProxy = 5B0AF8B227B2C4E20096FE54 /* PBXContainerItemProxy */; }; + 5B707CEA27D9F47A0099EF99 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + productRef = 5B707CE927D9F47A0099EF99 /* OpenCC */; + }; 6A38BC2615FC131100A8A51F /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 6A38BC2115FC12FD00A8A51F /* Data */; @@ -1223,7 +1253,10 @@ INFOPLIST_KEY_NSHumanReadableCopyright = "© 2021-2022 vChewing Project."; INFOPLIST_KEY_NSMainStoryboardFile = Main; INFOPLIST_KEY_NSPrincipalClass = NSApplication; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; @@ -1271,7 +1304,10 @@ INFOPLIST_KEY_NSHumanReadableCopyright = "© 2021-2022 vChewing Project."; INFOPLIST_KEY_NSMainStoryboardFile = Main; INFOPLIST_KEY_NSPrincipalClass = NSApplication; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; MTL_ENABLE_DEBUG_INFO = NO; @@ -1418,7 +1454,10 @@ GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_VARIABLE = YES; INFOPLIST_FILE = "Source/Resources/IME-Info.plist"; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; ONLY_ACTIVE_ARCH = YES; @@ -1477,7 +1516,10 @@ GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_VARIABLE = YES; INFOPLIST_FILE = "Source/Resources/IME-Info.plist"; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; PRODUCT_BUNDLE_IDENTIFIER = org.atelierInmu.inputmethod.vChewing; @@ -1582,7 +1624,10 @@ GCC_WARN_UNUSED_VARIABLE = YES; HEADER_SEARCH_PATHS = ../; INFOPLIST_FILE = "Installer/Installer-Info.plist"; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; ONLY_ACTIVE_ARCH = YES; @@ -1626,7 +1671,10 @@ GCC_WARN_UNUSED_VARIABLE = YES; HEADER_SEARCH_PATHS = ../; INFOPLIST_FILE = "Installer/Installer-Info.plist"; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.11.5; MARKETING_VERSION = 1.3.7; PRODUCT_BUNDLE_IDENTIFIER = "org.atelierInmu.vChewing.${PRODUCT_NAME:rfc1034identifier}"; @@ -1688,6 +1736,17 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + 5B707CE927D9F47A0099EF99 /* OpenCC */ = { + isa = XCSwiftPackageProductDependency; + productName = OpenCC; + }; + 5B707CEB27D9F4870099EF99 /* OpenCC */ = { + isa = XCSwiftPackageProductDependency; + productName = OpenCC; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = 6A0D4E9415FC0CFA00ABF4B3 /* Project object */; }