From ebcc0a0479839e5179b4dbe2e15f559e318181ed Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 1 Aug 2022 23:32:19 +0800 Subject: [PATCH] InputState // Let attributedString drawn as node segments when OK. --- .../ControllerModules/InputState.swift | 39 ++++++++++++++----- .../ControllerModules/KeyHandler_States.swift | 9 +++-- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Source/Modules/ControllerModules/InputState.swift b/Source/Modules/ControllerModules/InputState.swift index 50fc85b0..74026bc6 100644 --- a/Source/Modules/ControllerModules/InputState.swift +++ b/Source/Modules/ControllerModules/InputState.swift @@ -175,6 +175,8 @@ public enum InputState { public var type: StateType { .ofNotEmpty } private(set) var composingBuffer: String private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } } + private(set) var reading: String = "" + private(set) var nodeValuesArray = [String]() public var composingBufferConverted: String { let converted = IME.kanjiConversionIfRequired(composingBuffer) if converted.utf16.count != composingBuffer.utf16.count @@ -185,21 +187,37 @@ public enum InputState { return converted } - init(composingBuffer: String, cursorIndex: Int) { + init(composingBuffer: String, cursorIndex: Int, reading: String = "", nodeValuesArray: [String] = []) { self.composingBuffer = composingBuffer + self.reading = reading + self.nodeValuesArray = nodeValuesArray defer { self.cursorIndex = cursorIndex } } var attributedString: NSMutableAttributedString { /// 考慮到因為滑鼠點擊等其它行為導致的組字區內容遞交情況, /// 這裡對組字區內容也加上康熙字轉換或者 JIS 漢字轉換處理。 - let attributedString = NSMutableAttributedString( - string: composingBufferConverted, - attributes: [ - .underlineStyle: NSUnderlineStyle.single.rawValue, - .markedClauseSegment: 0, - ] - ) + guard reading.isEmpty else { + let attributedString = NSMutableAttributedString( + string: composingBufferConverted, + attributes: [ + .underlineStyle: NSUnderlineStyle.thick.rawValue, + .markedClauseSegment: 0, + ] + ) + return attributedString + } + let attributedString = NSMutableAttributedString(string: composingBufferConverted) + var newBegin = 0 + for (i, neta) in nodeValuesArray.enumerated() { + attributedString.setAttributes( + [ + .underlineStyle: NSUnderlineStyle.thick.rawValue, + .markedClauseSegment: i, + ], range: NSRange(location: newBegin, length: neta.utf16.count) + ) + newBegin += neta.utf16.count + } return attributedString } @@ -216,8 +234,9 @@ public enum InputState { var textToCommit: String = "" var tooltip: String = "" - override init(composingBuffer: String, cursorIndex: Int) { - super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) + override init(composingBuffer: String, cursorIndex: Int, reading: String = "", nodeValuesArray: [String] = []) { + super.init( + composingBuffer: composingBuffer, cursorIndex: cursorIndex, reading: reading, nodeValuesArray: nodeValuesArray) } override var description: String { diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index 0410e8bd..3c15a79d 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -38,7 +38,7 @@ extension KeyHandler { /// 「更新內文組字區 (Update the composing buffer)」是指要求客體軟體將組字緩衝區的內容 /// 換成由此處重新生成的組字字串(NSAttributeString,否則會不顯示)。 var tooltipParameterRef: [String] = ["", ""] - var composingBuffer = "" + let nodeValuesArray: [String] = walkedAnchors.map(\.node.currentPair.value) var composedStringCursorIndex = 0 var readingCursorIndex = 0 /// IMK 協定的內文組字區的游標長度與游標位置無法正確統計 UTF8 高萬字(比如 emoji)的長度, @@ -47,7 +47,6 @@ extension KeyHandler { for theAnchor in walkedAnchors { let theNode = theAnchor.node let strNodeValue = theNode.currentPair.value - composingBuffer += strNodeValue let arrSplit: [String] = Array(strNodeValue).map { String($0) } let codepointCount = arrSplit.count /// 藉下述步驟重新將「可見游標位置」對齊至「組字器內的游標所在的讀音位置」。 @@ -95,7 +94,7 @@ extension KeyHandler { var arrHead = [String.UTF16View.Element]() var arrTail = [String.UTF16View.Element]() - for (i, n) in composingBuffer.utf16.enumerated() { + for (i, n) in nodeValuesArray.joined().utf16.enumerated() { if i < composedStringCursorIndex { arrHead.append(n) } else { @@ -121,7 +120,9 @@ extension KeyHandler { } /// 這裡生成準備要拿來回呼的「正在輸入」狀態,但還不能立即使用,因為工具提示仍未完成。 - return InputState.Inputting(composingBuffer: cleanedComposition, cursorIndex: cursorIndex) + return InputState.Inputting( + composingBuffer: cleanedComposition, cursorIndex: cursorIndex, reading: reading, nodeValuesArray: nodeValuesArray + ) } // MARK: - 用以生成候選詞陣列及狀態