This source file includes following definitions.
- Init
- InitializeIfNeeded
- SpellCheckWord
- IsValidContraction
- IsEnabled
#include "chrome/renderer/spellchecker/spellcheck_language.h"
#include "base/logging.h"
#include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
#include "chrome/renderer/spellchecker/spelling_engine.h"
SpellcheckLanguage::SpellcheckLanguage()
: platform_spelling_engine_(CreateNativeSpellingEngine()) {
}
SpellcheckLanguage::~SpellcheckLanguage() {
}
void SpellcheckLanguage::Init(base::File file, const std::string& language) {
DCHECK(platform_spelling_engine_.get());
platform_spelling_engine_->Init(file.Pass());
character_attributes_.SetDefaultLanguage(language);
text_iterator_.Reset();
contraction_iterator_.Reset();
}
bool SpellcheckLanguage::InitializeIfNeeded() {
DCHECK(platform_spelling_engine_.get());
return platform_spelling_engine_->InitializeIfNeeded();
}
bool SpellcheckLanguage::SpellCheckWord(
const base::char16* in_word,
int in_word_len,
int tag,
int* misspelling_start,
int* misspelling_len,
std::vector<base::string16>* optional_suggestions) {
DCHECK(in_word_len >= 0);
DCHECK(misspelling_start && misspelling_len) << "Out vars must be given.";
if (InitializeIfNeeded())
return true;
if (!platform_spelling_engine_.get() ||
!platform_spelling_engine_->IsEnabled())
return true;
*misspelling_start = 0;
*misspelling_len = 0;
if (in_word_len == 0)
return true;
base::string16 word;
int word_start;
int word_length;
if (!text_iterator_.IsInitialized() &&
!text_iterator_.Initialize(&character_attributes_, true)) {
VLOG(1) << "Failed to initialize SpellcheckWordIterator";
return true;
}
text_iterator_.SetText(in_word, in_word_len);
DCHECK(platform_spelling_engine_.get());
while (text_iterator_.GetNextWord(&word, &word_start, &word_length)) {
if (platform_spelling_engine_->CheckSpelling(word, tag))
continue;
if (IsValidContraction(word, tag))
continue;
*misspelling_start = word_start;
*misspelling_len = word_length;
if (optional_suggestions) {
platform_spelling_engine_->FillSuggestionList(word,
optional_suggestions);
}
return false;
}
return true;
}
bool SpellcheckLanguage::IsValidContraction(const base::string16& contraction,
int tag) {
if (!contraction_iterator_.IsInitialized() &&
!contraction_iterator_.Initialize(&character_attributes_, false)) {
VLOG(1) << "Failed to initialize contraction_iterator_";
return true;
}
contraction_iterator_.SetText(contraction.c_str(), contraction.length());
base::string16 word;
int word_start;
int word_length;
DCHECK(platform_spelling_engine_.get());
while (contraction_iterator_.GetNextWord(&word, &word_start, &word_length)) {
if (!platform_spelling_engine_->CheckSpelling(word, tag))
return false;
}
return true;
}
bool SpellcheckLanguage::IsEnabled() {
DCHECK(platform_spelling_engine_.get());
return platform_spelling_engine_->IsEnabled();
}