Skip to content

Commit

Permalink
macos: fix backspace for latest syllable to continue composition
Browse files Browse the repository at this point in the history
  • Loading branch information
soomtong committed Jan 22, 2025
1 parent 07f5248 commit c30a4f4
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 5 deletions.
3 changes: 3 additions & 0 deletions macOS/Patal/HangulLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ protocol HangulAutomata {
func pickJungsung(by char: String) -> unichar?
func pickJongsung(by char: String) -> unichar?
func pickNonSyllable(by char: String) -> String?
func getChosungRawString(by chosung: 초성) -> String
func getJungsungRawString(by jungsung: 중성) -> String
func getJongsungRawString(by jongsung: 종성) -> String
}

/// 각 레이아웃이 가져야할 프로토콜의 공용 구현
Expand Down
14 changes: 9 additions & 5 deletions macOS/Patal/HangulProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ class HangulProcessor {

/// 백스페이스가 들어오면 첫/가/끝의 역순으로 지움
func applyBackspace() -> Int {
logger.debug("백스페이스 처리 전: \(String(describing: preedit)) \(previous)")
if previous.last != nil {
previous.removeLast()
}
Expand All @@ -444,12 +443,17 @@ class HangulProcessor {
print("아무것도 없음")
case (.some(_), nil, nil):
print("초성을 지워도 됨: \(String(describing: preedit))")
previous = []
preedit.chosung = nil
case (_, .some(_), nil):
print("중성을 지워야 함: \(String(describing: preedit))")
case (let chosung, .some(_), nil):
print("중성을 지워야 함: \(String(describing: preedit)); 초성을 복구한다.")
let rawString = hangulLayout.getChosungRawString(by: chosung!)
previous = [rawString]
preedit.jungsung = nil
case (_, _, .some(_)):
print("종성을 지움: \(String(describing: preedit))")
case (_, let jungsung, .some(_)):
print("종성을 지움: \(String(describing: preedit)); 중성을 복구한다.")
let rawString = hangulLayout.getJungsungRawString(by: jungsung!)
previous = [rawString]
preedit.jongsung = nil
}
logger.debug("백스페이스 처리 후: \(String(describing: preedit)) \(previous)")
Expand Down
28 changes: 28 additions & 0 deletions macOS/Patal/Layouts/Han3P3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,32 @@ struct Han3P3Layout: HangulAutomata {

return nonSyllable
}

let chosungReverseIndex: [String] = [
"k", "kk", "h", "u", "y", "i", ";", ";;", "n", "nn",
"j", "l", "ll", "o", "'", "0", "p", "m",
]

func getChosungRawString(by chosung: 초성) -> String {
return chosungReverseIndex[chosungMapOffset[chosung]!]
}

let jungsungReverseIndex: [String] = [
"f", "t", "6", "T", "r", "c", "e", "7", "v",
"/f", "/e", "/d", "4", "b", "9", "9r", "9c", "9d",
"5", "g", "8d", "d",
]

func getJungsungRawString(by jungsung: 중성) -> String {
return jungsungReverseIndex[jungsungMapOffset[jungsung]!]
}

let jongsungReverseIndex: [String] = [
"x", "xx", "xq", "s", "sv", "sd", "S", "c", "w", "wx", "wz", "w3", "wq",
"we", "wf", "wd", "z", "3", "3q", "q", "2", "a", "v", "r", "1", "e", "f", "d",
]

func getJongsungRawString(by jongsung: 종성) -> String {
return jongsungReverseIndex[jongsungMapOffset[jongsung]!]
}
}
29 changes: 29 additions & 0 deletions macOS/Patal/Layouts/Han3ShinP2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,33 @@ struct Han3ShinP2Layout: HangulAutomata {

return nonSyllable
}

let chosungReverseIndex: [String] = [
"k", "kk", "h", "u", "uu", "y", "i", ";", ";;",
"n", "nn", "j", "l", "ll", "o", "'", "/", "p", "m",
]

func getChosungRawString(by chosung: 초성) -> String {
return chosungReverseIndex[chosungMapOffset[chosung]!]
}

let jungsungReverseIndex: [String] = [
"f", "e", "w", "q", "r", "c", "t", "s", "v", "pf",
"pe", "pd", "x", "b", "o", "or", "oc", "od", "a",
"g", "g", "i", "id", "z", "d",
]

func getJungsungRawString(by jungsung: 중성) -> String {
return jungsungReverseIndex[jungsungMapOffset[jungsung]!]
}

let jongsungReverseIndex: [String] = [
"c", "cc", "cq", "s", "sv", "sd", "g", "w", "wc", "wz",
"we", "wq", "wr", "wf", "wd", "z", "e", "eq", "q",
"x", "a", "v", "b", "t", "r", "f", "d",
]

func getJongsungRawString(by jongsung: 종성) -> String {
return jongsungReverseIndex[jongsungMapOffset[jongsung]!]
}
}
29 changes: 29 additions & 0 deletions macOS/Patal/Layouts/Han3ShinPCS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,33 @@ struct Han3ShinPcsLayout: HangulAutomata {

return nonSyllable
}

let chosungReverseIndex: [String] = [
"k", "kk", "h", "u", "uu", "y", "i", ";", ";;",
"n", "nn", "j", "l", "ll", "o", "'", "/", "p", "m",
]

func getChosungRawString(by chosung: 초성) -> String {
return chosungReverseIndex[chosungMapOffset[chosung]!]
}

let jungsungReverseIndex: [String] = [
"f", "e", "w", "q", "r", "c", "t", "s", "v", "pf",
"pe", "pd", "x", "b", "o", "or", "oc", "od", "a",
"g", "g", "i", "id", "z", "d",
]

func getJungsungRawString(by jungsung: 중성) -> String {
return jungsungReverseIndex[jungsungMapOffset[jungsung]!]
}

let jongsungReverseIndex: [String] = [
"c", "cc", "cq", "s", "sv", "sd", "g", "w", "wc", "wz",
"we", "wq", "wr", "wf", "wd", "z", "e", "eq", "q",
"x", "a", "v", "b", "t", "r", "f", "d",
]

func getJongsungRawString(by jongsung: 종성) -> String {
return jongsungReverseIndex[jongsungMapOffset[jongsung]!]
}
}
33 changes: 33 additions & 0 deletions macOS/PatalTests/Processors/Hangul3ShinP2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,39 @@ struct Hangul3ShinP2Tests {
#expect(c6 == "")
}

@Test("가←네")
func getComposedChar_가백스페이스네() {
var r1 = processor.verifyCombosable("k")
#expect(r1 == true)
processor.rawChar = "k"
let s1 = processor.한글조합()
#expect(s1 == CommitState.composing)
let c1 = processor.getComposed()
#expect(c1 == "")

r1 = processor.verifyCombosable("f")
#expect(r1 == true)
processor.rawChar = "f"
let s2 = processor.한글조합()
#expect(s2 == CommitState.composing)
let c2 = processor.getComposed()
#expect(c2 == "")

let countComposable = processor.applyBackspace()
#expect(countComposable == 1)

processor.rawChar = "h"
let s3 = processor.한글조합()
#expect(s3 == CommitState.committed)
let c3 = processor.getComposed()
#expect(c3 == "")

processor.rawChar = "c"
let s4 = processor.한글조합()
#expect(s4 == CommitState.composing)
let c4 = processor.getComposed()
#expect(c4 == "")
}
// @Test("강^H")
// func getComposedChar_강백스페이스() {
// let processor = HangulProcessor(layout: Layout.HAN3_SHIN_PCS)
Expand Down

0 comments on commit c30a4f4

Please sign in to comment.