|
| 1 | +/** |
| 2 | + * @file llspellcheckengine_mac.mm |
| 3 | + * @brief Spell-check engine backed by the native macOS NSSpellChecker |
| 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 "llspellcheckengine.h" |
| 30 | + |
| 31 | +#import <AppKit/AppKit.h> |
| 32 | + |
| 33 | +namespace |
| 34 | +{ |
| 35 | + NSSpellChecker* nsChecker() |
| 36 | + { |
| 37 | + return [NSSpellChecker sharedSpellChecker]; |
| 38 | + } |
| 39 | + |
| 40 | + // std::string (UTF-8) -> NSString. Returns nil on invalid UTF-8 (callers must guard). |
| 41 | + NSString* toNS(const std::string& str) |
| 42 | + { |
| 43 | + return [NSString stringWithUTF8String:str.c_str()]; |
| 44 | + } |
| 45 | + |
| 46 | + std::string fromNS(NSString* str) |
| 47 | + { |
| 48 | + if (!str) |
| 49 | + { |
| 50 | + return std::string(); |
| 51 | + } |
| 52 | + const char* utf8 = [str UTF8String]; |
| 53 | + return (utf8) ? std::string(utf8) : std::string(); |
| 54 | + } |
| 55 | + |
| 56 | + class LLNSSpellEngine final : public LLSpellCheckEngine |
| 57 | + { |
| 58 | + public: |
| 59 | + bool setLanguage(const std::string& name) override; |
| 60 | + bool isActive() const override { return mEnabled; } |
| 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 spell checker's installed language codes (e.g. "en_US"), as UTF-8 strings. |
| 66 | + static std::vector<std::string> availableTags(); |
| 67 | + |
| 68 | + bool mEnabled = false; |
| 69 | + std::string mNSLanguage; // Resolved NSSpellChecker language code (e.g. "en_US") |
| 70 | + }; |
| 71 | + |
| 72 | + // static |
| 73 | + std::vector<std::string> LLNSSpellEngine::availableTags() |
| 74 | + { |
| 75 | + std::vector<std::string> tags; |
| 76 | + @autoreleasepool |
| 77 | + { |
| 78 | + for (NSString* lang in [nsChecker() availableLanguages]) |
| 79 | + { |
| 80 | + tags.push_back(fromNS(lang)); |
| 81 | + } |
| 82 | + } |
| 83 | + return tags; |
| 84 | + } |
| 85 | + |
| 86 | + bool LLNSSpellEngine::setLanguage(const std::string& name) |
| 87 | + { |
| 88 | + llassert([NSThread isMainThread]); |
| 89 | + mEnabled = false; |
| 90 | + mNSLanguage.clear(); |
| 91 | + if (name.empty()) |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + const std::string ns_lang = LLSpellCheckEngine::matchLanguage(name, availableTags(), '_'); |
| 97 | + if (ns_lang.empty()) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + @autoreleasepool |
| 103 | + { |
| 104 | + NSString* ns_lang_str = toNS(ns_lang); |
| 105 | + // setLanguage: returns NO if the language can't be matched; treat that as disabled. |
| 106 | + if ( (!ns_lang_str) || (![nsChecker() setLanguage:ns_lang_str]) ) |
| 107 | + { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + mNSLanguage = ns_lang; |
| 112 | + mEnabled = true; |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + bool LLNSSpellEngine::checkWord(const std::string& word) const |
| 117 | + { |
| 118 | + // NSSpellChecker is AppKit; use it only on the main thread (where text widgets lay out). |
| 119 | + llassert([NSThread isMainThread]); |
| 120 | + if (!mEnabled) |
| 121 | + { |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + @autoreleasepool |
| 126 | + { |
| 127 | + NSString* ns_word = toNS(word); |
| 128 | + if (!ns_word) |
| 129 | + { |
| 130 | + return true; |
| 131 | + } |
| 132 | + NSRange range = [nsChecker() checkSpellingOfString:ns_word |
| 133 | + startingAt:0 |
| 134 | + language:toNS(mNSLanguage) |
| 135 | + wrap:NO |
| 136 | + inSpellDocumentWithTag:0 |
| 137 | + wordCount:NULL]; |
| 138 | + return (range.location == NSNotFound); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + S32 LLNSSpellEngine::getSuggestions(const std::string& word, std::vector<std::string>& suggestions) const |
| 143 | + { |
| 144 | + llassert([NSThread isMainThread]); |
| 145 | + suggestions.clear(); |
| 146 | + if (!mEnabled) |
| 147 | + { |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + @autoreleasepool |
| 152 | + { |
| 153 | + NSString* ns_word = toNS(word); |
| 154 | + if (!ns_word) |
| 155 | + { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + // The range must be expressed in NSString (UTF-16) units, not std::string bytes. |
| 159 | + NSArray* guesses = [nsChecker() guessesForWordRange:NSMakeRange(0, [ns_word length]) |
| 160 | + inString:ns_word |
| 161 | + language:toNS(mNSLanguage) |
| 162 | + inSpellDocumentWithTag:0]; |
| 163 | + for (NSString* guess in guesses) |
| 164 | + { |
| 165 | + suggestions.push_back(fromNS(guess)); |
| 166 | + } |
| 167 | + } |
| 168 | + return static_cast<S32>(suggestions.size()); |
| 169 | + } |
| 170 | + |
| 171 | + std::set<std::string> LLNSSpellEngine::getInstalledLanguages(const std::vector<std::string>& candidate_names) const |
| 172 | + { |
| 173 | + llassert([NSThread isMainThread]); |
| 174 | + std::set<std::string> installed; |
| 175 | + const std::vector<std::string> available = availableTags(); // one OS query for the batch |
| 176 | + for (const std::string& name : candidate_names) |
| 177 | + { |
| 178 | + if (!LLSpellCheckEngine::matchLanguage(name, available, '_').empty()) |
| 179 | + { |
| 180 | + installed.insert(name); |
| 181 | + } |
| 182 | + } |
| 183 | + return installed; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// static |
| 188 | +std::unique_ptr<LLSpellCheckEngine> LLSpellCheckEngine::create() |
| 189 | +{ |
| 190 | + return std::make_unique<LLNSSpellEngine>(); |
| 191 | +} |
0 commit comments