KeyHandler // Modifies the behavior of sole tone mark. (UPR315) (#79)

This commit is contained in:
ShikiSuen 2022-05-07 10:05:41 +08:00 committed by GitHub
parent c0c0e39c9b
commit d8fd398625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 7 deletions

View File

@ -33,7 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (BOOL)isBufferEmpty; + (BOOL)isBufferEmpty;
+ (void)clearBuffer; + (void)clearBuffer;
+ (void)combineReadingKey:(UniChar)charCode; + (void)combineReadingKey:(UniChar)charCode;
+ (BOOL)checkWhetherToneMarkerConfirms; + (BOOL)hasToneMarker;
+ (BOOL)hasToneMarkerOnly;
+ (NSString *)getSyllableComposition; + (NSString *)getSyllableComposition;
+ (void)doBackSpaceToBuffer; + (void)doBackSpaceToBuffer;
+ (NSString *)getComposition; + (NSString *)getComposition;

View File

@ -52,11 +52,16 @@ static Mandarin::BopomofoReadingBuffer *PhoneticBuffer;
PhoneticBuffer->combineKey((char)charCode); PhoneticBuffer->combineKey((char)charCode);
} }
+ (BOOL)checkWhetherToneMarkerConfirms + (BOOL)hasToneMarker
{ {
return PhoneticBuffer->hasToneMarker(); return PhoneticBuffer->hasToneMarker();
} }
+ (BOOL)hasToneMarkerOnly
{
return PhoneticBuffer->hasToneMarkerOnly();
}
+ (NSString *)getSyllableComposition + (NSString *)getSyllableComposition
{ {
return [NSString stringWithUTF8String:PhoneticBuffer->syllable().composedString().c_str()]; return [NSString stringWithUTF8String:PhoneticBuffer->syllable().composedString().c_str()];

View File

@ -569,6 +569,12 @@ extern "C" class BopomofoReadingBuffer
return syllable_.hasToneMarker(); return syllable_.hasToneMarker();
} }
bool hasToneMarkerOnly() const
{
return syllable_.hasToneMarker() &&
!(syllable_.hasConsonant() || syllable_.hasMiddleVowel() || syllable_.hasVowel());
}
protected: protected:
const BopomofoKeyboardLayout *layout_; const BopomofoKeyboardLayout *layout_;
BPMF syllable_; BPMF syllable_;

View File

@ -135,23 +135,26 @@ extension KeyHandler {
// MARK: Handle BPMF Keys. // MARK: Handle BPMF Keys.
var composeReading = false var keyConsumedByReading = false
let skipPhoneticHandling = input.isReservedKey || input.isControlHold || input.isOptionHold let skipPhoneticHandling = input.isReservedKey || input.isControlHold || input.isOptionHold
// See if Phonetic reading is valid. // See if Phonetic reading is valid.
if !skipPhoneticHandling && Composer.chkKeyValidity(charCode) { if !skipPhoneticHandling && Composer.chkKeyValidity(charCode) {
Composer.combineReadingKey(charCode) Composer.combineReadingKey(charCode)
keyConsumedByReading = true
// If we have a tone marker, we have to insert the reading to the // 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 // builder in other words, if we don't have a tone marker, we just
// update the composing buffer. // update the composing buffer.
composeReading = Composer.checkWhetherToneMarkerConfirms() let composeReading = Composer.hasToneMarker()
if !composeReading { if !composeReading {
stateCallback(buildInputtingState()) stateCallback(buildInputtingState())
return true return true
} }
} }
var composeReading = Composer.hasToneMarker() || Composer.hasToneMarkerOnly()
// See if we have composition if Enter/Space is hit and buffer is not empty. // 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. // We use "|=" conditioning so that the tone marker key is also taken into account.
// However, Swift does not support "|=". // However, Swift does not support "|=".
@ -159,10 +162,12 @@ extension KeyHandler {
if composeReading { if composeReading {
let reading = Composer.getSyllableComposition() let reading = Composer.getSyllableComposition()
// See whether we have a unigram for this...
if !ifLangModelHasUnigrams(forKey: reading) { if !ifLangModelHasUnigrams(forKey: reading) {
IME.prtDebugIntel("B49C0979語彙庫內無「\(reading)」的匹配記錄。") IME.prtDebugIntel("B49C0979語彙庫內無「\(reading)」的匹配記錄。")
errorCallback() errorCallback()
stateCallback(buildInputtingState()) Composer.clearBuffer()
stateCallback((getBuilderLength() == 0) ? InputState.EmptyIgnoringPreviousState() : buildInputtingState())
return true return true
} }
@ -210,6 +215,16 @@ extension KeyHandler {
stateCallback(choosingCandidates) 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 return true
} }

View File

@ -303,7 +303,9 @@ extension KeyHandler {
return false return false
} }
if Composer.isBufferEmpty() { if Composer.hasToneMarkerOnly() {
Composer.clearBuffer()
} else if Composer.isBufferEmpty() {
if getBuilderCursorIndex() >= 0 { if getBuilderCursorIndex() >= 0 {
deleteBuilderReadingInFrontOfCursor() deleteBuilderReadingInFrontOfCursor()
walk() walk()
@ -462,7 +464,7 @@ extension KeyHandler {
if !Composer.isBufferEmpty() { if !Composer.isBufferEmpty() {
Composer.clearBuffer() Composer.clearBuffer()
if getBuilderLength() == 0 { if getBuilderLength() == 0 {
stateCallback(InputState.Empty()) stateCallback(InputState.EmptyIgnoringPreviousState())
} else { } else {
stateCallback(buildInputtingState()) stateCallback(buildInputtingState())
} }