2 * Copyright (C) 2011 Igalia S.L.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "webkitspellcheckerenchant.h"
22 #if ENABLE(SPELLCHECK)
25 #include "webkitspellchecker.h"
28 #include <wtf/text/CString.h>
30 static EnchantBroker* broker = 0;
32 struct _WebKitSpellCheckerEnchantPrivate {
36 static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* interface);
38 G_DEFINE_TYPE_WITH_CODE(WebKitSpellCheckerEnchant, webkit_spell_checker_enchant, G_TYPE_OBJECT,
39 G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_SPELL_CHECKER,
40 webkit_spell_checker_enchant_spell_checker_interface_init))
42 static void createEnchantBrokerIfNeeded()
45 broker = enchant_broker_init();
48 static void freeSpellCheckingLanguage(gpointer data, gpointer)
50 createEnchantBrokerIfNeeded();
52 enchant_broker_free_dict(broker, static_cast<EnchantDict*>(data));
55 static void webkit_spell_checker_enchant_finalize(GObject* object)
57 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(object)->priv;
59 g_slist_foreach(priv->enchantDicts, freeSpellCheckingLanguage, 0);
60 g_slist_free(priv->enchantDicts);
62 WEBKIT_SPELL_CHECKER_ENCHANT(object)->priv->~WebKitSpellCheckerEnchantPrivate();
65 static void webkit_spell_checker_enchant_class_init(WebKitSpellCheckerEnchantClass* klass)
67 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
69 objectClass->finalize = webkit_spell_checker_enchant_finalize;
71 g_type_class_add_private(klass, sizeof(WebKitSpellCheckerEnchantPrivate));
74 static void webkit_spell_checker_enchant_init(WebKitSpellCheckerEnchant* checker)
76 WebKitSpellCheckerEnchantPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(checker, WEBKIT_TYPE_SPELL_CHECKER_ENCHANT, WebKitSpellCheckerEnchantPrivate);
78 new (priv) WebKitSpellCheckerEnchantPrivate();
80 priv->enchantDicts = 0;
83 static void checkSpellingOfString(WebKitSpellChecker* checker, const char* string, int* misspellingLocation, int* misspellingLength)
85 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
87 GSList* dicts = priv->enchantDicts;
91 int length = g_utf8_strlen(string, -1);
93 PangoLanguage* language(pango_language_get_default());
94 GOwnPtr<PangoLogAttr> attrs(g_new(PangoLogAttr, length + 1));
96 // pango_get_log_attrs uses an aditional position at the end of the text.
97 pango_get_log_attrs(string, -1, -1, language, attrs.get(), length + 1);
99 for (int i = 0; i < length + 1; i++) {
100 // We go through each character until we find an is_word_start,
101 // then we get into an inner loop to find the is_word_end corresponding
103 if (attrs.get()[i].is_word_start) {
108 while (attrs.get()[end].is_word_end < 1)
111 wordLength = end - start;
112 // Set the iterator to be at the current word end, so we don't
113 // check characters twice.
116 gchar* cstart = g_utf8_offset_to_pointer(string, start);
117 gint bytes = static_cast<gint>(g_utf8_offset_to_pointer(string, end) - cstart);
118 GOwnPtr<gchar> word(g_new0(gchar, bytes + 1));
120 g_utf8_strncpy(word.get(), cstart, wordLength);
122 for (; dicts; dicts = dicts->next) {
123 EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
124 if (enchant_dict_check(dict, word.get(), wordLength)) {
125 *misspellingLocation = start;
126 *misspellingLength = wordLength;
128 // Stop checking, this word is ok in at least one dict.
129 *misspellingLocation = -1;
130 *misspellingLength = 0;
138 static char** getGuessesForWord(WebKitSpellChecker* checker, const char* word, const char* context)
140 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
142 GSList* dicts = priv->enchantDicts;
145 for (; dicts; dicts = dicts->next) {
146 size_t numberOfSuggestions;
149 EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
150 gchar** suggestions = enchant_dict_suggest(dict, word, -1, &numberOfSuggestions);
152 if (numberOfSuggestions > 0) {
153 if (numberOfSuggestions > 10)
154 numberOfSuggestions = 10;
156 guesses = static_cast<char**>(g_malloc0((numberOfSuggestions + 1) * sizeof(char*)));
157 for (i = 0; i < numberOfSuggestions && i < 10; i++)
158 guesses[i] = g_strdup(suggestions[i]);
162 enchant_dict_free_suggestions(dict, suggestions);
169 static void getAvailableDictionariesCallback(const char* const languageTag, const char* const, const char* const, const char* const, void* data)
171 Vector<CString>* dicts = static_cast<Vector<CString>*>(data);
173 dicts->append(languageTag);
176 static void updateSpellCheckingLanguages(WebKitSpellChecker* checker, const char* languages)
178 GSList* spellDictionaries = 0;
180 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
182 createEnchantBrokerIfNeeded();
185 char** langs = g_strsplit(languages, ",", -1);
186 for (int i = 0; langs[i]; i++) {
187 if (enchant_broker_dict_exists(broker, langs[i])) {
188 EnchantDict* dict = enchant_broker_request_dict(broker, langs[i]);
189 spellDictionaries = g_slist_append(spellDictionaries, dict);
194 const char* language = pango_language_to_string(gtk_get_default_language());
195 if (enchant_broker_dict_exists(broker, language)) {
196 EnchantDict* dict = enchant_broker_request_dict(broker, language);
197 spellDictionaries = g_slist_append(spellDictionaries, dict);
199 // No dictionaries selected, we get one from the list.
200 Vector<CString> allDictionaries;
201 enchant_broker_list_dicts(broker, getAvailableDictionariesCallback, &allDictionaries);
202 if (!allDictionaries.isEmpty()) {
203 EnchantDict* dict = enchant_broker_request_dict(broker, allDictionaries[0].data());
204 spellDictionaries = g_slist_append(spellDictionaries, dict);
208 g_slist_foreach(priv->enchantDicts, freeSpellCheckingLanguage, 0);
209 g_slist_free(priv->enchantDicts);
210 priv->enchantDicts = spellDictionaries;
213 static char* getAutocorrectSuggestionsForMisspelledWord(WebKitSpellChecker* checker, const char* word)
218 static void learnWord(WebKitSpellChecker* checker, const char* word)
220 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
221 GSList* dicts = priv->enchantDicts;
223 for (; dicts; dicts = dicts->next) {
224 EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
226 enchant_dict_add_to_personal(dict, word, -1);
230 static void ignoreWord(WebKitSpellChecker* checker, const char* word)
232 WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
233 GSList* dicts = priv->enchantDicts;
235 for (; dicts; dicts = dicts->next) {
236 EnchantDict* dict = static_cast<EnchantDict*>(dicts->data);
238 enchant_dict_add_to_session(dict, word, -1);
242 static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* interface)
244 interface->check_spelling_of_string = checkSpellingOfString;
245 interface->get_guesses_for_word = getGuessesForWord;
246 interface->update_spell_checking_languages = updateSpellCheckingLanguages;
247 interface->get_autocorrect_suggestions_for_misspelled_word = getAutocorrectSuggestionsForMisspelledWord;
248 interface->learn_word = learnWord;
249 interface->ignore_word = ignoreWord;
252 #endif /* ENABLE(SPELLCHECK) */