vChewing-macOS/Source/Modules/ChineseConverterBridge.swift

110 lines
5.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// 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 defined in MIT License.
import Hotenka
public enum ChineseConverter {
public static let shared = HotenkaChineseConverter(plistDir: LMMgr.getBundleDataPath("convdict"))
private static var punctuationConversionTable: [(String, String)] = [
("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("", "︿"), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("", ""), ("", ""), ("", ""), ("", ""),
]
///
/// 使使
/// - Parameters:
/// - target:
/// - convert:
public static func hardenVerticalPunctuations(target: inout String, convert: Bool = false) {
guard convert else { return }
for neta in Self.punctuationConversionTable {
target = target.replacingOccurrences(of: neta.0, with: neta.1)
}
}
// JIS
private static func processKanjiRepeatSymbol(target: inout String) {
guard !target.isEmpty else { return }
var arr = target.charComponents
for (i, char) in arr.enumerated() {
if i == 0 { continue }
if char == target.charComponents[i - 1] {
arr[i] = ""
}
}
target = arr.joined()
}
///
private static let currencyNumeralDictTable: [String: (String, String, String, String)] = [
"": ("", "", "", ""), "": ("", "", "", ""), "": ("", "", "", ""),
"": ("", "", "", ""), "": ("", "", "", ""), "": ("", "", "", ""),
"": ("", "", "", ""), "": ("", "", "", ""), "": ("", "", "", ""),
"": ("", "", "", ""), "": ("", "", "", ""), "": ("", "", "", ""),
"": ("", "", "", ""), "": ("", "", "", ""),
]
///
/// - Parameter target:
public static func ensureCurrencyNumerals(target: inout String) {
if !PrefMgr.shared.currencyNumeralsEnabled { return }
for key in currencyNumeralDictTable.keys {
guard let result = currencyNumeralDictTable[key] else { continue }
if IMEApp.currentInputMode == .imeModeCHS {
target = target.replacingOccurrences(of: key, with: result.3) // Simplified Chinese
continue
}
switch (PrefMgr.shared.chineseConversionEnabled, PrefMgr.shared.shiftJISShinjitaiOutputEnabled) {
case (false, true), (true, true): target = target.replacingOccurrences(of: key, with: result.2) // JIS
case (true, false): target = target.replacingOccurrences(of: key, with: result.0) // KangXi
default: target = target.replacingOccurrences(of: key, with: result.1) // Contemporary
}
}
}
/// CrossConvert.
///
/// - Parameter string: Text in Original Script.
/// - Returns: Text converted to Different Script.
public static func crossConvert(_ string: String) -> String {
switch IMEApp.currentInputMode {
case .imeModeCHS:
return shared.convert(string, to: .zhHantTW)
case .imeModeCHT:
return shared.convert(string, to: .zhHansCN)
default:
return string
}
}
public static func cnvTradToKangXi(_ strObj: String) -> String {
shared.convert(strObj, to: .zhHantKX)
}
public static func cnvTradToJIS(_ strObj: String) -> String {
//
let strObj = cnvTradToKangXi(strObj)
var result = shared.convert(strObj, to: .zhHansJP)
processKanjiRepeatSymbol(target: &result)
return result
}
static func kanjiConversionIfRequired(_ text: String) -> String {
guard IMEApp.currentInputMode == .imeModeCHT else { return text }
switch (PrefMgr.shared.chineseConversionEnabled, PrefMgr.shared.shiftJISShinjitaiOutputEnabled) {
case (false, true): return ChineseConverter.cnvTradToJIS(text)
case (true, false): return ChineseConverter.cnvTradToKangXi(text)
//
case (true, true): return ChineseConverter.cnvTradToJIS(text)
case (false, false): return text
}
}
}