Removed the GLib dependency from the Windows 8 spellcheck provider by swapping in Windows/stdlib UTF-8/UTF-16 conversions and standard allocation/free routines.

This commit is contained in:
2026-01-25 23:52:52 -07:00
parent c8ee118f00
commit 041288cdad

View File

@@ -23,7 +23,9 @@
#include "config.h" #include "config.h"
#include <Spellcheck.h> #include <Spellcheck.h>
#include <glib.h> #include <windows.h>
#include <cstdlib>
#include "typedef.h" // for ssize_t #include "typedef.h" // for ssize_t
#include <enchant-provider.h> #include <enchant-provider.h>
@@ -33,9 +35,24 @@ ENCHANT_PLUGIN_DECLARE ("win8")
/* --------- Utils ----------*/ /* --------- Utils ----------*/
static char * static char *
utf16_to_utf8 (const wchar_t * const str, gboolean from_bcp47) utf16_to_utf8 (const wchar_t * const str, bool from_bcp47)
{ {
char *utf8 = g_utf16_to_utf8 ((gunichar2*)str, -1, nullptr, nullptr, nullptr); if (!str)
return nullptr;
int needed = WideCharToMultiByte (CP_UTF8, 0, str, -1, nullptr, 0, nullptr, nullptr);
if (needed <= 0)
return nullptr;
char *utf8 = static_cast<char*>(std::malloc (static_cast<size_t>(needed)));
if (!utf8)
return nullptr;
if (WideCharToMultiByte (CP_UTF8, 0, str, -1, utf8, needed, nullptr, nullptr) != needed)
{
std::free (utf8);
return nullptr;
}
if (utf8 && from_bcp47) if (utf8 && from_bcp47)
{ {
char *p = utf8; char *p = utf8;
@@ -51,9 +68,27 @@ utf16_to_utf8 (const wchar_t * const str, gboolean from_bcp47)
} }
static wchar_t * static wchar_t *
utf8_to_utf16 (const char * const str, size_t len, gboolean to_bcp47) utf8_to_utf16 (const char * const str, int len, bool to_bcp47)
{ {
wchar_t *utf16 = (wchar_t*)g_utf8_to_utf16 (str, len, nullptr, nullptr, nullptr); if (!str)
return nullptr;
int needed = MultiByteToWideChar (CP_UTF8, 0, str, len, nullptr, 0);
if (needed <= 0)
return nullptr;
int alloc_len = (len == -1) ? needed : needed + 1;
wchar_t *utf16 = static_cast<wchar_t*>(std::malloc (sizeof (wchar_t) * static_cast<size_t>(alloc_len)));
if (!utf16)
return nullptr;
if (MultiByteToWideChar (CP_UTF8, 0, str, len, utf16, needed) != needed)
{
std::free (utf16);
return nullptr;
}
if (len != -1)
utf16[needed] = L'\0';
if (utf16 && to_bcp47) if (utf16 && to_bcp47)
{ {
wchar_t *p = utf16; wchar_t *p = utf16;
@@ -69,12 +104,19 @@ utf8_to_utf16 (const char * const str, size_t len, gboolean to_bcp47)
} }
static char ** static char **
enumstring_to_chararray (IEnumString *strings, size_t *out_len, gboolean from_bcp47) enumstring_to_chararray (IEnumString *strings, size_t *out_len, bool from_bcp47)
{ {
char **chars = g_new (char*, 256); /* Hopefully large enough */ char **chars = static_cast<char**>(std::calloc (256, sizeof (char*))); /* Hopefully large enough */
LPOLESTR wstr = nullptr; LPOLESTR wstr = nullptr;
size_t i = 0; size_t i = 0;
if (!chars)
{
*out_len = 0;
strings->Release ();
return nullptr;
}
while (SUCCEEDED (strings->Next (1, &wstr, nullptr)) && i < 256 && wstr) while (SUCCEEDED (strings->Next (1, &wstr, nullptr)) && i < 256 && wstr)
{ {
char *str = utf16_to_utf8 (wstr, from_bcp47); char *str = utf16_to_utf8 (wstr, from_bcp47);
@@ -98,33 +140,33 @@ static void
win8_dict_add_to_personal (EnchantDict *dict, const char *const word, size_t len) win8_dict_add_to_personal (EnchantDict *dict, const char *const word, size_t len)
{ {
auto checker = static_cast<ISpellChecker*>(dict->user_data); auto checker = static_cast<ISpellChecker*>(dict->user_data);
wchar_t *wword = utf8_to_utf16 (word, len, FALSE); wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
checker->Add (wword); checker->Add (wword);
g_free (wword); std::free (wword);
} }
static void static void
win8_dict_add_to_session (EnchantDict *dict, const char *const word, size_t len) win8_dict_add_to_session (EnchantDict *dict, const char *const word, size_t len)
{ {
auto checker = static_cast<ISpellChecker*>(dict->user_data); auto checker = static_cast<ISpellChecker*>(dict->user_data);
wchar_t *wword = utf8_to_utf16 (word, len, FALSE); wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
checker->Ignore (wword); checker->Ignore (wword);
g_free (wword); std::free (wword);
} }
static int static int
win8_dict_check (EnchantDict *dict, const char *const word, size_t len) win8_dict_check (EnchantDict *dict, const char *const word, size_t len)
{ {
auto checker = static_cast<ISpellChecker*>(dict->user_data); auto checker = static_cast<ISpellChecker*>(dict->user_data);
wchar_t *wword = utf8_to_utf16 (word, len, FALSE); wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
IEnumSpellingError *errors; IEnumSpellingError *errors;
ISpellingError *error = nullptr; ISpellingError *error = nullptr;
HRESULT hr; HRESULT hr;
hr = checker->Check (wword, &errors); hr = checker->Check (wword, &errors);
g_free (wword); std::free (wword);
if (FAILED (hr)) if (FAILED (hr))
return -1; /* Error */ return -1; /* Error */
@@ -146,12 +188,12 @@ static char **
win8_dict_suggest (EnchantDict *dict, const char *const word, size_t len, size_t *out_n_suggs) win8_dict_suggest (EnchantDict *dict, const char *const word, size_t len, size_t *out_n_suggs)
{ {
auto checker = static_cast<ISpellChecker*>(dict->user_data); auto checker = static_cast<ISpellChecker*>(dict->user_data);
wchar_t *wword = utf8_to_utf16 (word, len, FALSE); wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
IEnumString *suggestions; IEnumString *suggestions;
HRESULT hr; HRESULT hr;
hr = checker->Suggest (wword, &suggestions); hr = checker->Suggest (wword, &suggestions);
g_free (wword); std::free (wword);
if (FAILED (hr)) if (FAILED (hr))
{ {
@@ -159,7 +201,7 @@ win8_dict_suggest (EnchantDict *dict, const char *const word, size_t len, size_t
return nullptr; return nullptr;
} }
return enumstring_to_chararray (suggestions, out_n_suggs, FALSE); return enumstring_to_chararray (suggestions, out_n_suggs, false);
} }
/* ---------- Provider ------------ */ /* ---------- Provider ------------ */
@@ -170,16 +212,16 @@ win8_provider_request_dict (EnchantProvider *provider, const char *const tag)
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data); auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
ISpellChecker *checker; ISpellChecker *checker;
EnchantDict *dict; EnchantDict *dict;
wchar_t *wtag = utf8_to_utf16 (tag, -1, TRUE); wchar_t *wtag = utf8_to_utf16 (tag, -1, true);
HRESULT hr; HRESULT hr;
hr = factory->CreateSpellChecker (wtag, &checker); hr = factory->CreateSpellChecker (wtag, &checker);
g_free (wtag); std::free (wtag);
if (FAILED (hr)) if (FAILED (hr))
return nullptr; return nullptr;
dict = g_new0 (EnchantDict, 1); dict = static_cast<EnchantDict*>(std::calloc (1, sizeof (EnchantDict)));
dict->suggest = win8_dict_suggest; dict->suggest = win8_dict_suggest;
dict->check = win8_dict_check; dict->check = win8_dict_check;
dict->add_to_personal = win8_dict_add_to_personal; dict->add_to_personal = win8_dict_add_to_personal;
@@ -199,7 +241,7 @@ win8_provider_dispose_dict (EnchantProvider *provider, EnchantDict *dict)
auto checker = static_cast<ISpellChecker*>(dict->user_data); auto checker = static_cast<ISpellChecker*>(dict->user_data);
checker->Release (); checker->Release ();
g_free (dict); std::free (dict);
} }
} }
@@ -207,12 +249,12 @@ static int
win8_provider_dictionary_exists (EnchantProvider *provider, const char *const tag) win8_provider_dictionary_exists (EnchantProvider *provider, const char *const tag)
{ {
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data); auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
wchar_t *wtag = utf8_to_utf16 (tag, -1, TRUE); wchar_t *wtag = utf8_to_utf16 (tag, -1, true);
BOOL is_supported = FALSE; BOOL is_supported = FALSE;
factory->IsSupported (wtag, &is_supported); factory->IsSupported (wtag, &is_supported);
g_free (wtag); std::free (wtag);
return is_supported; return is_supported;
} }
@@ -229,13 +271,17 @@ win8_provider_list_dicts (EnchantProvider *provider, size_t *out_n_dicts)
return nullptr; return nullptr;
} }
return enumstring_to_chararray (dicts, out_n_dicts, TRUE); return enumstring_to_chararray (dicts, out_n_dicts, true);
} }
static void static void
win8_provider_free_string_list (EnchantProvider *provider, char **str_list) win8_provider_free_string_list (EnchantProvider *provider, char **str_list)
{ {
g_strfreev (str_list); if (!str_list)
return;
for (size_t i = 0; str_list[i]; ++i)
std::free (str_list[i]);
std::free (str_list);
} }
static void static void
@@ -246,7 +292,7 @@ win8_provider_dispose (EnchantProvider *provider)
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data); auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
factory->Release(); factory->Release();
g_free (provider); std::free (provider);
} }
} }
@@ -275,7 +321,7 @@ init_enchant_provider (void)
CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&factory)))) CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&factory))))
return nullptr; return nullptr;
provider = g_new0 (EnchantProvider, 1); provider = static_cast<EnchantProvider*>(std::calloc (1, sizeof (EnchantProvider)));
provider->dispose = win8_provider_dispose; provider->dispose = win8_provider_dispose;
provider->request_dict = win8_provider_request_dict; provider->request_dict = win8_provider_request_dict;
provider->dispose_dict = win8_provider_dispose_dict; provider->dispose_dict = win8_provider_dispose_dict;