From 20ead0a84fdd8c9e870e98ee6a2712bfa263d7fb Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Mon, 25 Mar 2013 23:12:52 +0100 Subject: Adding basic support for native spell checking on Mac OS X. Change-Id: Id29313a06f052ecbaef54be0c185cd7f1df375a2 License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. diff --git a/SwifTools/MacOSXChecker.h b/SwifTools/MacOSXChecker.h new file mode 100644 index 0000000..f2f8ebc --- /dev/null +++ b/SwifTools/MacOSXChecker.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include + +namespace Swift { + class MacOSXChecker : public SpellChecker { + public: + MacOSXChecker(); + virtual ~MacOSXChecker(); + virtual bool isCorrect(const std::string& word); + virtual void getSuggestions(const std::string& word, std::vector& list); + virtual void checkFragment(const std::string& fragment, PositionPairList& misspelledPositions); + }; +} diff --git a/SwifTools/MacOSXChecker.mm b/SwifTools/MacOSXChecker.mm new file mode 100644 index 0000000..3e687d1 --- /dev/null +++ b/SwifTools/MacOSXChecker.mm @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +#include +#include + +#include + +namespace Swift { + +MacOSXChecker::MacOSXChecker() { +} + +MacOSXChecker::~MacOSXChecker() { +} + +bool MacOSXChecker::isCorrect(const std::string& /*word*/) { + // No content since it doesn't seem to be used anywhere. + return false; +} + +void MacOSXChecker::getSuggestions(const std::string& word, std::vector& list) { + NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; + NSString* wordString = [[NSString alloc] initWithUTF8String: word.c_str()]; + NSArray* suggestions = [spellChecker guessesForWord: wordString]; + for(unsigned int i = 0; i < [suggestions count]; ++i) { + list.push_back(std::string([[suggestions objectAtIndex:i] UTF8String])); + } + [wordString release]; +} + +void MacOSXChecker::checkFragment(const std::string& fragment, PositionPairList& misspelledPositions) { + NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; + size_t nextLocation = 0; + NSRange range; + NSString *fragmentString = [[NSString alloc] initWithUTF8String: fragment.c_str()]; + do { + range = [spellChecker checkSpellingOfString:fragmentString startingAt:static_cast(nextLocation)]; + if (range.location != NSNotFound) { + if (range.location < nextLocation) + break; + misspelledPositions.push_back(PositionPair(static_cast(range.location), static_cast(range.location + range.length))); + nextLocation = range.location + range.length + 1; + } + } while (range.location != NSNotFound); + [fragmentString release]; +} + +} diff --git a/SwifTools/SConscript b/SwifTools/SConscript index eaf5787..b30a79c 100644 --- a/SwifTools/SConscript +++ b/SwifTools/SConscript @@ -41,6 +41,12 @@ if env["SCONS_STAGE"] == "build" : "HunspellChecker.cpp", "SpellParser.cpp", ] + elif swiftools_env["PLATFORM"] == "darwin" : + sources += [ + "SpellCheckerFactory.cpp", + "MacOSXChecker.mm", + "SpellParser.cpp", + ] if swiftools_env.get("HAVE_SPARKLE", 0) : diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h index 746fcaf..fd38418 100644 --- a/SwifTools/SpellChecker.h +++ b/SwifTools/SpellChecker.h @@ -20,7 +20,7 @@ namespace Swift { } virtual ~SpellChecker() { delete parser_; - }; + } virtual bool isCorrect(const std::string& word) = 0; virtual void getSuggestions(const std::string& word, std::vector& list) = 0; virtual void checkFragment(const std::string& fragment, PositionPairList& misspelledPositions) = 0; diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp index 6061d78..d068d90 100644 --- a/SwifTools/SpellCheckerFactory.cpp +++ b/SwifTools/SpellCheckerFactory.cpp @@ -9,9 +9,12 @@ #include #include #include +#include #ifdef HAVE_HUNSPELL #include +#elif defined(SWIFTEN_PLATFORM_MACOSX) +#include #endif namespace Swift { @@ -27,6 +30,8 @@ SpellChecker* SpellCheckerFactory::createSpellChecker(const std::string& dictFil return new HunspellChecker(affixFile.c_str(), dictFile.c_str()); } // If dictionaries don't exist disable the checker +#elif defined(SWIFTEN_PLATFORM_MACOSX) + return new MacOSXChecker(); #endif return NULL; } diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h index 086ea66..91118f9 100644 --- a/SwifTools/SpellCheckerFactory.h +++ b/SwifTools/SpellCheckerFactory.h @@ -6,8 +6,12 @@ #pragma once +#include + #ifdef HAVE_HUNSPELL #define HAVE_SPELLCHECKER +#elif defined(SWIFTEN_PLATFORM_MACOSX) +#define HAVE_SPELLCHECKER #endif namespace Swift { -- cgit v0.10.2-6-g49f6