diff --git a/Source/3rdParty/OVMandarin/Composer.hh b/Source/3rdParty/OVMandarin/Composer.hh index d620b766..fa49864f 100644 --- a/Source/3rdParty/OVMandarin/Composer.hh +++ b/Source/3rdParty/OVMandarin/Composer.hh @@ -33,7 +33,8 @@ NS_ASSUME_NONNULL_BEGIN + (BOOL)isBufferEmpty; + (void)clearBuffer; + (void)combineReadingKey:(UniChar)charCode; -+ (BOOL)checkWhetherToneMarkerConfirms; ++ (BOOL)hasToneMarker; ++ (BOOL)hasToneMarkerOnly; + (NSString *)getSyllableComposition; + (void)doBackSpaceToBuffer; + (NSString *)getComposition; diff --git a/Source/3rdParty/OVMandarin/Composer.mm b/Source/3rdParty/OVMandarin/Composer.mm index 09379bc7..6efb5d08 100644 --- a/Source/3rdParty/OVMandarin/Composer.mm +++ b/Source/3rdParty/OVMandarin/Composer.mm @@ -52,11 +52,16 @@ static Mandarin::BopomofoReadingBuffer *PhoneticBuffer; PhoneticBuffer->combineKey((char)charCode); } -+ (BOOL)checkWhetherToneMarkerConfirms ++ (BOOL)hasToneMarker { return PhoneticBuffer->hasToneMarker(); } ++ (BOOL)hasToneMarkerOnly +{ + return PhoneticBuffer->hasToneMarkerOnly(); +} + + (NSString *)getSyllableComposition { return [NSString stringWithUTF8String:PhoneticBuffer->syllable().composedString().c_str()]; diff --git a/Source/3rdParty/OVMandarin/Mandarin.h b/Source/3rdParty/OVMandarin/Mandarin.h index 03a46c08..50ae8718 100644 --- a/Source/3rdParty/OVMandarin/Mandarin.h +++ b/Source/3rdParty/OVMandarin/Mandarin.h @@ -569,6 +569,12 @@ extern "C" class BopomofoReadingBuffer return syllable_.hasToneMarker(); } + bool hasToneMarkerOnly() const + { + return syllable_.hasToneMarker() && + !(syllable_.hasConsonant() || syllable_.hasMiddleVowel() || syllable_.hasVowel()); + } + protected: const BopomofoKeyboardLayout *layout_; BPMF syllable_; diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift index 8d425a49..cdea1012 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift @@ -135,23 +135,26 @@ extension KeyHandler { // MARK: Handle BPMF Keys. - var composeReading = false + var keyConsumedByReading = false let skipPhoneticHandling = input.isReservedKey || input.isControlHold || input.isOptionHold // See if Phonetic reading is valid. if !skipPhoneticHandling && Composer.chkKeyValidity(charCode) { Composer.combineReadingKey(charCode) + keyConsumedByReading = true // If we have a tone marker, we have to insert the reading to the // builder in other words, if we don't have a tone marker, we just // update the composing buffer. - composeReading = Composer.checkWhetherToneMarkerConfirms() + let composeReading = Composer.hasToneMarker() if !composeReading { stateCallback(buildInputtingState()) return true } } + var composeReading = Composer.hasToneMarker() || Composer.hasToneMarkerOnly() + // See if we have composition if Enter/Space is hit and buffer is not empty. // We use "|=" conditioning so that the tone marker key is also taken into account. // However, Swift does not support "|=". @@ -159,10 +162,12 @@ extension KeyHandler { if composeReading { let reading = Composer.getSyllableComposition() + // See whether we have a unigram for this... if !ifLangModelHasUnigrams(forKey: reading) { IME.prtDebugIntel("B49C0979:語彙庫內無「\(reading)」的匹配記錄。") errorCallback() - stateCallback(buildInputtingState()) + Composer.clearBuffer() + stateCallback((getBuilderLength() == 0) ? InputState.EmptyIgnoringPreviousState() : buildInputtingState()) return true } @@ -210,6 +215,16 @@ extension KeyHandler { stateCallback(choosingCandidates) } } + return true // Telling the client that the key is consumed. + } + + // The only possibility for this to be true is that the Bopomofo reading + // already has a tone marker but the last key is *not* a tone marker key. An + // example is the sequence "6u" with the Standard layout, which produces "ㄧˊ" + // but does not compose. Only sequences such as "ㄧˊ", "ˊㄧˊ", "ˊㄧˇ", or "ˊㄧ " + // would compose. + if keyConsumedByReading { + stateCallback(buildInputtingState()) return true } diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index a11cd900..103e02cd 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -303,7 +303,9 @@ extension KeyHandler { return false } - if Composer.isBufferEmpty() { + if Composer.hasToneMarkerOnly() { + Composer.clearBuffer() + } else if Composer.isBufferEmpty() { if getBuilderCursorIndex() >= 0 { deleteBuilderReadingInFrontOfCursor() walk() @@ -462,7 +464,7 @@ extension KeyHandler { if !Composer.isBufferEmpty() { Composer.clearBuffer() if getBuilderLength() == 0 { - stateCallback(InputState.Empty()) + stateCallback(InputState.EmptyIgnoringPreviousState()) } else { stateCallback(buildInputtingState()) }