InputState // Also perform kanji conversion in attributedString.

This commit is contained in:
ShikiSuen 2022-06-18 14:12:00 +08:00
parent e65c567ab0
commit ece3f72ee5
4 changed files with 29 additions and 18 deletions

View File

@ -137,6 +137,15 @@ class InputState {
class NotEmpty: InputState {
private(set) var composingBuffer: String
private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } }
var composingBufferConverted: String {
let converted = IME.kanjiConversionIfRequired(composingBuffer)
if converted.utf16.count != composingBuffer.utf16.count
|| converted.count != composingBuffer.count
{
return composingBuffer
}
return converted
}
init(composingBuffer: String, cursorIndex: Int) {
self.composingBuffer = composingBuffer
@ -145,8 +154,10 @@ class InputState {
}
var attributedString: NSAttributedString {
///
/// JIS
let attributedString = NSAttributedString(
string: IME.kanjiConversionIfRequired(composingBuffer),
string: composingBufferConverted,
attributes: [
.underlineStyle: NSUnderlineStyle.single.rawValue,
.markedClauseSegment: 0,
@ -260,7 +271,9 @@ class InputState {
}
override var attributedString: NSAttributedString {
let attributedString = NSMutableAttributedString(string: IME.kanjiConversionIfRequired(composingBuffer))
///
/// JIS
let attributedString = NSMutableAttributedString(string: composingBufferConverted)
let end = markedRange.upperBound
attributedString.setAttributes(

View File

@ -446,19 +446,4 @@ class KeyHandler {
func deleteCompositorReadingToTheFrontOfCursor() {
compositor.deleteReadingToTheFrontOfCursor()
}
// MARK: -
func kanjiConversionIfRequired(_ text: String) -> String {
if inputMode == InputMode.imeModeCHT {
switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) {
case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text)
case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text)
//
case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text)
case (false, false): return text
}
}
return text
}
}

View File

@ -298,7 +298,7 @@ extension ctlInputMethod {
///
private func commit(text: String) {
let buffer = keyHandler.kanjiConversionIfRequired(text)
let buffer = IME.kanjiConversionIfRequired(text)
if buffer.isEmpty {
return
}

View File

@ -43,6 +43,19 @@ public enum IME {
static var currentInputMode: InputMode = .init(rawValue: mgrPrefs.mostRecentInputMode) ?? .imeModeNULL
static func kanjiConversionIfRequired(_ text: String) -> String {
if currentInputMode == InputMode.imeModeCHT {
switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) {
case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text)
case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text)
//
case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text)
case (false, false): return text
}
}
return text
}
// MARK: -
static var areWeUsingOurOwnPhraseEditor: Bool = false