|
| 1 | +/** |
| 2 | + * @file llspellcheckengine_win32.cpp |
| 3 | + * @brief Spell-check engine backed by the native Windows Spell Checking API |
| 4 | + * |
| 5 | + * $LicenseInfo:firstyear=2001&license=viewerlgpl$ |
| 6 | + * Second Life Viewer Source Code |
| 7 | + * Copyright (C) 2010, Linden Research, Inc. |
| 8 | + * |
| 9 | + * This library is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; |
| 12 | + * version 2.1 of the License only. |
| 13 | + * |
| 14 | + * This library is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with this library; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | + * |
| 23 | + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA |
| 24 | + * $/LicenseInfo$ |
| 25 | + */ |
| 26 | + |
| 27 | +#include "linden_common.h" |
| 28 | + |
| 29 | +#include "llstring.h" |
| 30 | +#include "llthread.h" |
| 31 | + |
| 32 | +#include "llspellcheckengine.h" |
| 33 | + |
| 34 | +#include <objbase.h> |
| 35 | +#include <spellcheck.h> |
| 36 | + |
| 37 | +#include <cwchar> |
| 38 | +#include <string> |
| 39 | + |
| 40 | +namespace |
| 41 | +{ |
| 42 | + // std::string (UTF-8) -> std::wstring (UTF-16). The default code page is CP_UTF8. |
| 43 | + std::wstring toWide(const std::string& str) |
| 44 | + { |
| 45 | + return ll_convert_string_to_wide(str.c_str(), str.length()); |
| 46 | + } |
| 47 | + |
| 48 | + // A null-terminated wide string (e.g. an LPOLESTR returned by COM) -> UTF-8 std::string. |
| 49 | + std::string fromWide(const wchar_t* wstr) |
| 50 | + { |
| 51 | + return (wstr) ? ll_convert_wide_to_string(wstr, wcslen(wstr)) : std::string(); |
| 52 | + } |
| 53 | + |
| 54 | + class LLWinSpellEngine final : public LLSpellCheckEngine |
| 55 | + { |
| 56 | + public: |
| 57 | + LLWinSpellEngine(); |
| 58 | + ~LLWinSpellEngine() override; |
| 59 | + bool setLanguage(const std::string& name) override; |
| 60 | + bool isActive() const override { return (nullptr != mChecker); } |
| 61 | + bool checkWord(const std::string& word) const override; |
| 62 | + S32 getSuggestions(const std::string& word, std::vector<std::string>& suggestions) const override; |
| 63 | + std::set<std::string> getInstalledLanguages(const std::vector<std::string>& candidate_names) const override; |
| 64 | + private: |
| 65 | + // The factory's supported language tags (e.g. "en-US"), as UTF-8 strings. |
| 66 | + std::vector<std::string> availableTags() const; |
| 67 | + |
| 68 | + ISpellCheckerFactory* mFactory = nullptr; |
| 69 | + ISpellChecker* mChecker = nullptr; |
| 70 | + HRESULT mComInitResult = E_FAIL; // gates the matching CoUninitialize |
| 71 | + }; |
| 72 | + |
| 73 | + LLWinSpellEngine::LLWinSpellEngine() |
| 74 | + { |
| 75 | + // The Windows Spell Checking API is COM-based and the viewer keeps no standing COM apartment |
| 76 | + // on the main thread, so own one for this engine's lifetime. Apartment-threaded (STA) matches |
| 77 | + // every existing scoped COM site and is reference-counted, so it nests safely. |
| 78 | + mComInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); |
| 79 | + if (SUCCEEDED(mComInitResult) || (RPC_E_CHANGED_MODE == mComInitResult)) |
| 80 | + { |
| 81 | + if (FAILED(CoCreateInstance(__uuidof(SpellCheckerFactory), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&mFactory)))) |
| 82 | + { |
| 83 | + mFactory = nullptr; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + LLWinSpellEngine::~LLWinSpellEngine() |
| 89 | + { |
| 90 | + // Release COM interfaces before tearing down the apartment. |
| 91 | + if (mChecker) |
| 92 | + { |
| 93 | + mChecker->Release(); |
| 94 | + mChecker = nullptr; |
| 95 | + } |
| 96 | + if (mFactory) |
| 97 | + { |
| 98 | + mFactory->Release(); |
| 99 | + mFactory = nullptr; |
| 100 | + } |
| 101 | + // Balance CoInitializeEx only when we actually initialized (S_OK or S_FALSE); never on |
| 102 | + // RPC_E_CHANGED_MODE (a thread already in another apartment that we did not initialize). |
| 103 | + if (SUCCEEDED(mComInitResult)) |
| 104 | + { |
| 105 | + CoUninitialize(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + std::vector<std::string> LLWinSpellEngine::availableTags() const |
| 110 | + { |
| 111 | + std::vector<std::string> tags; |
| 112 | + if (!mFactory) |
| 113 | + { |
| 114 | + return tags; |
| 115 | + } |
| 116 | + IEnumString* langs = nullptr; |
| 117 | + if (SUCCEEDED(mFactory->get_SupportedLanguages(&langs)) && langs) |
| 118 | + { |
| 119 | + LPOLESTR psz = nullptr; |
| 120 | + ULONG fetched = 0; |
| 121 | + while ((langs->Next(1, &psz, &fetched) == S_OK) && (1 == fetched) && psz) |
| 122 | + { |
| 123 | + tags.push_back(fromWide(psz)); |
| 124 | + CoTaskMemFree(psz); |
| 125 | + psz = nullptr; |
| 126 | + } |
| 127 | + langs->Release(); |
| 128 | + } |
| 129 | + return tags; |
| 130 | + } |
| 131 | + |
| 132 | + bool LLWinSpellEngine::setLanguage(const std::string& name) |
| 133 | + { |
| 134 | + llassert(on_main_thread()); |
| 135 | + if (mChecker) |
| 136 | + { |
| 137 | + mChecker->Release(); |
| 138 | + mChecker = nullptr; |
| 139 | + } |
| 140 | + if ( (!mFactory) || (name.empty()) ) |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + const std::string tag = LLSpellCheckEngine::matchLanguage(name, availableTags(), '-'); |
| 146 | + if (tag.empty()) |
| 147 | + { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + const std::wstring wtag = toWide(tag); |
| 152 | + // CreateSpellChecker returns E_INVALIDARG when no checker is available for the language. |
| 153 | + if (FAILED(mFactory->CreateSpellChecker(wtag.c_str(), &mChecker)) || (!mChecker)) |
| 154 | + { |
| 155 | + mChecker = nullptr; |
| 156 | + return false; |
| 157 | + } |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + bool LLWinSpellEngine::checkWord(const std::string& word) const |
| 162 | + { |
| 163 | + llassert(on_main_thread()); |
| 164 | + if (!mChecker) |
| 165 | + { |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + const std::wstring wword = toWide(word); |
| 170 | + if (wword.empty()) |
| 171 | + { |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + IEnumSpellingError* errors = nullptr; |
| 176 | + if (FAILED(mChecker->Check(wword.c_str(), &errors)) || (!errors)) |
| 177 | + { |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + // A correctly spelled word yields an empty enumeration: Next() returns S_FALSE. Any fetched |
| 182 | + // ISpellingError (S_OK) means the word is misspelled. |
| 183 | + ISpellingError* error = nullptr; |
| 184 | + const bool misspelled = (S_OK == errors->Next(&error)) && (nullptr != error); |
| 185 | + if (error) |
| 186 | + { |
| 187 | + error->Release(); |
| 188 | + } |
| 189 | + errors->Release(); |
| 190 | + |
| 191 | + return !misspelled; |
| 192 | + } |
| 193 | + |
| 194 | + S32 LLWinSpellEngine::getSuggestions(const std::string& word, std::vector<std::string>& suggestions) const |
| 195 | + { |
| 196 | + llassert(on_main_thread()); |
| 197 | + suggestions.clear(); |
| 198 | + if (!mChecker) |
| 199 | + { |
| 200 | + return 0; |
| 201 | + } |
| 202 | + |
| 203 | + const std::wstring wword = toWide(word); |
| 204 | + if (wword.empty()) |
| 205 | + { |
| 206 | + return 0; |
| 207 | + } |
| 208 | + |
| 209 | + IEnumString* enum_str = nullptr; |
| 210 | + const HRESULT hr = mChecker->Suggest(wword.c_str(), &enum_str); |
| 211 | + if (FAILED(hr) || (!enum_str)) |
| 212 | + { |
| 213 | + return 0; |
| 214 | + } |
| 215 | + |
| 216 | + // Suggest() returns S_FALSE when the word is already spelled correctly, in which case the |
| 217 | + // enumeration just echoes the input word - skip it so we don't offer the word as its own fix. |
| 218 | + if (S_OK == hr) |
| 219 | + { |
| 220 | + LPOLESTR psz = nullptr; |
| 221 | + ULONG fetched = 0; |
| 222 | + while ((enum_str->Next(1, &psz, &fetched) == S_OK) && (1 == fetched) && psz) |
| 223 | + { |
| 224 | + suggestions.push_back(fromWide(psz)); |
| 225 | + CoTaskMemFree(psz); |
| 226 | + psz = nullptr; |
| 227 | + } |
| 228 | + } |
| 229 | + enum_str->Release(); |
| 230 | + |
| 231 | + return static_cast<S32>(suggestions.size()); |
| 232 | + } |
| 233 | + |
| 234 | + std::set<std::string> LLWinSpellEngine::getInstalledLanguages(const std::vector<std::string>& candidate_names) const |
| 235 | + { |
| 236 | + llassert(on_main_thread()); |
| 237 | + std::set<std::string> installed; |
| 238 | + if (!mFactory) |
| 239 | + { |
| 240 | + return installed; |
| 241 | + } |
| 242 | + const std::vector<std::string> available = availableTags(); // one OS query for the batch |
| 243 | + for (const std::string& name : candidate_names) |
| 244 | + { |
| 245 | + if (!LLSpellCheckEngine::matchLanguage(name, available, '-').empty()) |
| 246 | + { |
| 247 | + installed.insert(name); |
| 248 | + } |
| 249 | + } |
| 250 | + return installed; |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +// static |
| 255 | +std::unique_ptr<LLSpellCheckEngine> LLSpellCheckEngine::create() |
| 256 | +{ |
| 257 | + return std::make_unique<LLWinSpellEngine>(); |
| 258 | +} |
0 commit comments