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;
+ (void)clearBuffer;
+ (void)combineReadingKey:(UniChar)charCode;
+ (BOOL)checkWhetherToneMarkerConfirms;
+ (BOOL)hasToneMarker;
+ (BOOL)hasToneMarkerOnly;
+ (NSString *)getSyllableComposition;
+ (void)doBackSpaceToBuffer;
+ (NSString *)getComposition;

View File

@ -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()];

View File

@ -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_;

View File

@ -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
}

View File

@ -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())
}