[dev]: Refactor punctuation logic

This commit is contained in:
fanlumaster committed 2025-06-22 23:21:50 +08:00
1 parent a63fd00f21
commit 1016052ccf
8 files changed
+667 -615

No files matched your search

+19 -9
View File
@@ -1,5 +1,6 @@
#ifndef UNICODE
#define UNICODE
#include <debugapi.h>
#endif
#include "Private.h"
@@ -615,7 +616,7 @@ BOOL CCompositionProcessorEngine::IsPunctuation(WCHAR wch)
//
//----------------------------------------------------------------------------
WCHAR CCompositionProcessorEngine::GetPunctuation(WCHAR wch)
const WCHAR *CCompositionProcessorEngine::GetPunctuation(WCHAR wch)
{
for (int i = 0; i < ARRAYSIZE(Global::PunctuationTable); i++)
{
@@ -1167,9 +1168,9 @@ void CCompositionProcessorEngine::SetupPunctuationPair()
// Punctuation pair
const int pair_count = 2;
// Left quotation mark and right quotation mark “”
CPunctuationPair punc_quotation_mark(L'"', 0x201C, 0x201D);
CPunctuationPair punc_quotation_mark(L'"', L"“", L"”");
// Left single quotation mark and right single quotation mark ‘’
CPunctuationPair punc_apostrophe(L'\'', 0x2018, 0x2019);
CPunctuationPair punc_apostrophe(L'\'', L"‘", L"’");
CPunctuationPair puncPairs[pair_count] = {
punc_quotation_mark,
@@ -1183,7 +1184,7 @@ void CCompositionProcessorEngine::SetupPunctuationPair()
}
// Punctuation nest pair
CPunctuationNestPair punc_angle_bracket(L'<', 0x300A, 0x3008, L'>', 0x300B, 0x3009);
CPunctuationNestPair punc_angle_bracket(L'<', L"《", L"〈", L'>', L"》", L"〉");
CPunctuationNestPair *pPuncNestPair = _PunctuationNestPair.Append();
*pPuncNestPair = punc_angle_bracket;
@@ -1678,9 +1679,14 @@ void CCompositionProcessorEngine::SetDefaultCandidateTextFont()
// If engine need this virtual key code, returns true. Otherwise returns false.
//----------------------------------------------------------------------------
BOOL CCompositionProcessorEngine::IsVirtualKeyNeed(UINT uCode, _In_reads_(1) WCHAR *pwch, BOOL fComposing,
CANDIDATE_MODE candidateMode, BOOL hasCandidateWithWildcard,
_Out_opt_ _KEYSTROKE_STATE *pKeyState)
BOOL CCompositionProcessorEngine::IsVirtualKeyNeed( //
UINT uCode, //
_In_reads_(1) WCHAR *pwch, //
BOOL fComposing, //
CANDIDATE_MODE candidateMode, //
BOOL hasCandidateWithWildcard, //
_Out_opt_ _KEYSTROKE_STATE *pKeyState //
)
{
if (pKeyState)
{
@@ -2303,6 +2309,7 @@ BOOL CCompositionProcessorEngine::IsVirtualKeyNeed(UINT uCode, _In_reads_(1) WCH
if (*pwch && !IsVirtualKeyKeystrokeComposition(uCode, pKeyState, FUNCTION_NONE))
{
OutputDebugString(L"Will commit original keystroke string.");
if (pKeyState)
{
pKeyState->Category = CATEGORY_INVOKE_COMPOSITION_EDIT_SESSION;
@@ -2323,8 +2330,11 @@ BOOL CCompositionProcessorEngine::IsVirtualKeyNeed(UINT uCode, _In_reads_(1) WCH
//
//----------------------------------------------------------------------------
BOOL CCompositionProcessorEngine::IsVirtualKeyKeystrokeComposition(UINT uCode, _Out_opt_ _KEYSTROKE_STATE *pKeyState,
KEYSTROKE_FUNCTION function)
BOOL CCompositionProcessorEngine::IsVirtualKeyKeystrokeComposition( //
UINT uCode, //
_Out_opt_ _KEYSTROKE_STATE *pKeyState, //
KEYSTROKE_FUNCTION function //
)
{
#ifdef FANY_DEBUG
OutputDebugString(L"Fany Here: IsVirtualKeyKeystrokeComposition 26.");
+9 -4
View File
@@ -49,9 +49,14 @@ class CCompositionProcessorEngine
};
WCHAR GetVirtualKey(DWORD_PTR dwIndex);
void GetReadingStrings(_Inout_ CMetasequoiaImeArray<CStringRange> *pReadingStrings, _Out_ BOOL *pIsWildcardIncluded);
void GetCandidateList(_Inout_ CMetasequoiaImeArray<CCandidateListItem> *pCandidateList, BOOL isIncrementalWordSearch,
BOOL isWildcardSearch);
void GetReadingStrings( //
_Inout_ CMetasequoiaImeArray<CStringRange> *pReadingStrings, //
_Out_ BOOL *pIsWildcardIncluded //
);
void GetCandidateList( //
_Inout_ CMetasequoiaImeArray<CCandidateListItem> *pCandidateList, //
BOOL isIncrementalWordSearch, BOOL isWildcardSearch //
);
void GetCandidateStringInConverted(CStringRange &searchString,
_In_ CMetasequoiaImeArray<CCandidateListItem> *pCandidateList);
@@ -64,7 +69,7 @@ class CCompositionProcessorEngine
// Punctuation
BOOL IsPunctuation(WCHAR wch);
WCHAR GetPunctuation(WCHAR wch);
const WCHAR *GetPunctuation(WCHAR wch);
BOOL IsDoubleSingleByte(WCHAR wch);
BOOL IsWildcard()
+24 -20
View File
@@ -260,26 +260,30 @@ extern const WCHAR FullWidthCharTable[] = {
//---------------------------------------------------------------------
// defined punctuation characters
//---------------------------------------------------------------------
extern const struct _PUNCTUATION PunctuationTable[19] = {
{L'!', 0xFF01}, // !
{L'$', 0xFFE5}, // ¥
{L'&', L'&'}, // &
{L'(', 0xFF08}, // (
{L')', 0xFF09}, // )
{L',', 0xFF0C}, // ,
{L'.', 0x3002}, // 。
{L':', 0xFF1A}, // :
{L';', 0xFF1B}, // ;
{L'?', 0xFF1F}, // ?
{L'@', L'@'}, // @
{L'\\', 0x3001}, // 、
{L'^', 0x2026}, // ……
{L'_', 0x2014}, // ——
{L'<', L'《'}, // 《
{L'>', L'》'}, // 》
{L'`', L'·'}, // ·
{L'[', L'【'}, // 【
{L']', L'】'} // 】
extern const struct _PUNCTUATION PunctuationTable[24] = {
{L'`', L"·"}, // ·
{L'~', L"~"}, // ~
{L'!', L"!"}, // !
{L'@', L"@"}, // @
{L'#', L"#"}, // #
{L'$', L"¥"}, // ¥
{L'%', L"%"}, // %
{L'^', L"……"}, // ……
{L'&', L"&"}, // &
{L'*', L"*"}, // *
{L'(', L"("}, // (
{L')', L")"}, // )
{L'_', L"——"}, // ——
{L'[', L"【"}, // 【
{L']', L"】"}, // 】
{L'\\', L"、"}, // 、
{L';', L";"}, // ;
{L':', L":"}, // :
{L',', L","}, // ,
{L'<', L"《"}, // 《
{L'.', L"。"}, // 。
{L'>', L"》"}, // 》
{L'?', L"?"}, // ?
};
//+---------------------------------------------------------------------------
+1 -1
View File
@@ -133,7 +133,7 @@ extern const GUID MetasequoiaIMEGuidCompartmentDoubleSingleByte;
extern const GUID MetasequoiaIMEGuidCompartmentPunctuation;
extern const WCHAR FullWidthCharTable[];
extern const struct _PUNCTUATION PunctuationTable[19];
extern const struct _PUNCTUATION PunctuationTable[24];
extern const GUID MetasequoiaIMEGuidLangBarIMEMode;
extern const GUID MetasequoiaIMEGuidLangBarDoubleSingleByte;
+46 -15
View File
@@ -292,44 +292,75 @@ int CCandidateRange::GetIndex(UINT vKey)
CPunctuationPair::CPunctuationPair()
{
_punctuation._Code = 0;
_punctuation._Punctuation = 0;
_pairPunctuation = 0;
_punctuation._Punctuation[0] = 0;
_pairPunctuation[0] = 0;
_isPairToggle = FALSE;
}
CPunctuationPair::CPunctuationPair(WCHAR code, WCHAR punctuation, WCHAR pair)
CPunctuationPair::CPunctuationPair( //
WCHAR code, //
const WCHAR *punctuation, //
const WCHAR *pair //
)
{
_punctuation._Code = code;
_punctuation._Punctuation = punctuation;
_pairPunctuation = pair;
wcsncpy_s(_punctuation._Punctuation, _countof(_punctuation._Punctuation), punctuation, _TRUNCATE);
wcsncpy_s(_pairPunctuation, _countof(_pairPunctuation), pair, _TRUNCATE);
_isPairToggle = FALSE;
}
CPunctuationNestPair::CPunctuationNestPair()
{
_punctuation_begin._Code = 0;
_punctuation_begin._Punctuation = 0;
_pairPunctuation_begin = 0;
_punctuation_begin._Punctuation[0] = 0;
_pairPunctuation_begin[0] = 0;
_punctuation_end._Code = 0;
_punctuation_end._Punctuation = 0;
_pairPunctuation_end = 0;
_punctuation_end._Punctuation[0] = 0;
_pairPunctuation_end[0] = 0;
_nestCount = 0;
}
CPunctuationNestPair::CPunctuationNestPair(WCHAR codeBegin, WCHAR punctuationBegin, WCHAR pairBegin, WCHAR codeEnd,
WCHAR punctuationEnd, WCHAR pairEnd)
CPunctuationNestPair::CPunctuationNestPair( //
WCHAR codeBegin, //
const WCHAR *punctuationBegin, //
const WCHAR *pairBegin, //
const WCHAR codeEnd, //
const WCHAR *punctuationEnd, //
const WCHAR *pairEnd //
)
{
pairEnd;
punctuationEnd;
_punctuation_begin._Code = codeBegin;
_punctuation_begin._Punctuation = punctuationBegin;
_pairPunctuation_begin = pairBegin;
wcsncpy_s( //
_punctuation_begin._Punctuation, //
_countof(_punctuation_begin._Punctuation), //
punctuationBegin, //
_TRUNCATE //
);
wcsncpy_s( //
_pairPunctuation_begin, //
_countof(_pairPunctuation_begin), //
pairBegin, //
_TRUNCATE //
);
_punctuation_end._Code = codeEnd;
_punctuation_end._Punctuation = punctuationBegin;
_pairPunctuation_end = pairBegin;
wcsncpy_s( //
_punctuation_end._Punctuation, //
_countof(_punctuation_end._Punctuation), //
punctuationEnd, //
_TRUNCATE //
);
wcsncpy_s( //
_pairPunctuation_end, //
_countof(_pairPunctuation_end), //
pairEnd, //
_TRUNCATE //
);
_nestCount = 0;
}
+13 -7
View File
@@ -86,7 +86,7 @@ struct _KEYSTROKE_STATE
struct _PUNCTUATION
{
WCHAR _Code;
WCHAR _Punctuation;
WCHAR _Punctuation[3];
};
BOOL CLSIDToString(REFGUID refGUID, _Out_writes_(39) WCHAR *pCLSIDString);
@@ -241,10 +241,10 @@ class CPunctuationPair
{
public:
CPunctuationPair();
CPunctuationPair(WCHAR code, WCHAR punctuation, WCHAR pair);
CPunctuationPair(WCHAR code, const WCHAR *punctuation, const WCHAR *pair);
struct _PUNCTUATION _punctuation;
WCHAR _pairPunctuation;
WCHAR _pairPunctuation[3];
BOOL _isPairToggle;
};
@@ -252,14 +252,20 @@ class CPunctuationNestPair
{
public:
CPunctuationNestPair();
CPunctuationNestPair(WCHAR wchCode_begin, WCHAR wch_begin, WCHAR wchPair_begin, WCHAR wchCode_end, WCHAR wch_end,
WCHAR wchPair_end);
CPunctuationNestPair( //
WCHAR codeBegin, //
const WCHAR *punctuationBegin, //
const WCHAR *pairBegin, //
const WCHAR codeEnd, //
const WCHAR *punctuationEnd, //
const WCHAR *pairEnd //
);
struct _PUNCTUATION _punctuation_begin;
WCHAR _pairPunctuation_begin;
WCHAR _pairPunctuation_begin[3];
struct _PUNCTUATION _punctuation_end;
WCHAR _pairPunctuation_end;
WCHAR _pairPunctuation_end[3];
int _nestCount;
};
+546 -535
View File
File diff suppressed because it is too large. Load diff
+9 -24
View File
@@ -192,7 +192,7 @@ Exit:
//----------------------------------------------------------------------------
HRESULT CMetasequoiaIME::_HandleCompositionInputWorker(_In_ CCompositionProcessorEngine *pCompositionProcessorEngine,
TfEditCookie ec, _In_ ITfContext *pContext)
TfEditCookie ec, _In_ ITfContext *pContext)
{
HRESULT hr = S_OK;
CMetasequoiaImeArray<CStringRange> readingStrings;
@@ -260,7 +260,7 @@ HRESULT CMetasequoiaIME::_HandleCompositionInputWorker(_In_ CCompositionProcesso
//----------------------------------------------------------------------------
HRESULT CMetasequoiaIME::_CreateAndStartCandidate(_In_ CCompositionProcessorEngine *pCompositionProcessorEngine,
TfEditCookie ec, _In_ ITfContext *pContext)
TfEditCookie ec, _In_ ITfContext *pContext)
{
HRESULT hr = S_OK;
@@ -523,7 +523,7 @@ Exit:
//----------------------------------------------------------------------------
HRESULT CMetasequoiaIME::_HandleCompositionArrowKey(TfEditCookie ec, _In_ ITfContext *pContext,
KEYSTROKE_FUNCTION keyFunction)
KEYSTROKE_FUNCTION keyFunction)
{
ITfRange *pRangeComposition = nullptr;
TF_SELECTION tfSelection;
@@ -566,11 +566,6 @@ Exit:
HRESULT CMetasequoiaIME::_HandleCompositionPunctuation(TfEditCookie ec, _In_ ITfContext *pContext, WCHAR wch)
{
HRESULT hr = S_OK;
#ifdef FANY_DEBUG
std::wstring msg = L"You pressed punctuation key: ";
msg += wch;
OutputDebugString(msg.c_str());
#endif
if (_candidateMode != CANDIDATE_NONE && _pCandidateListUIPresenter)
{
DWORD_PTR candidateLen = 0;
@@ -583,7 +578,8 @@ HRESULT CMetasequoiaIME::_HandleCompositionPunctuation(TfEditCookie ec, _In_ ITf
if (candidateLen)
{
_AddComposingAndChar(ec, pContext, &candidateString);
/* TODO: Currently we do not commit selected candidate while typing punctuation */
// _AddComposingAndChar(ec, pContext, &candidateString);
}
}
//
@@ -592,21 +588,10 @@ HRESULT CMetasequoiaIME::_HandleCompositionPunctuation(TfEditCookie ec, _In_ ITf
CCompositionProcessorEngine *pCompositionProcessorEngine = nullptr;
pCompositionProcessorEngine = _pCompositionProcessorEngine;
WCHAR punctuation = pCompositionProcessorEngine->GetPunctuation(wch);
const WCHAR *punctuation = pCompositionProcessorEngine->GetPunctuation(wch);
CStringRange punctuationString;
if (punctuation == L'…')
{
punctuationString.Set(L"……", 2);
}
else if (punctuation == L'—')
{
punctuationString.Set(L"——", 2);
}
else
{
punctuationString.Set(&punctuation, 1);
}
punctuationString.Set(punctuation, wcslen(punctuation));
// Finalize character
hr = _AddCharAndFinalize(ec, pContext, &punctuationString);
@@ -615,7 +600,7 @@ HRESULT CMetasequoiaIME::_HandleCompositionPunctuation(TfEditCookie ec, _In_ ITf
return hr;
}
_HandleCancel(ec, pContext);
_HandleComplete(ec, pContext);
return S_OK;
}
@@ -662,7 +647,7 @@ HRESULT CMetasequoiaIME::_HandleCompositionDoubleSingleByte(TfEditCookie ec, _In
//----------------------------------------------------------------------------
HRESULT CMetasequoiaIME::_InvokeKeyHandler(_In_ ITfContext *pContext, UINT code, WCHAR wch, DWORD flags,
_KEYSTROKE_STATE keyState)
_KEYSTROKE_STATE keyState)
{
flags;