Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / renderer / spellchecker / spellcheck_language.h
blob3ffc6a2c6b3a4643ae3b07a85894862546962194
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_LANGUAGE_H_
6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_LANGUAGE_H_
8 #include <queue>
9 #include <string>
10 #include <vector>
12 #include "base/files/file.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
17 class SpellingEngine;
19 class SpellcheckLanguage {
20 public:
21 enum SpellcheckWordResult {
22 // Denotes that every recognized word is spelled correctly, from the point
23 // of spellchecking to the end of the text.
24 IS_CORRECT,
25 // A sequence of skippable characters, such as punctuation, spaces, or
26 // characters not recognized by the current spellchecking language.
27 IS_SKIPPABLE,
28 // A misspelled word has been found in the text.
29 IS_MISSPELLED
32 SpellcheckLanguage();
33 ~SpellcheckLanguage();
35 void Init(base::File file, const std::string& language);
37 // Spellcheck a sequence of text.
38 // |text_length| is the length of the text being spellchecked. The |tag|
39 // parameter should either be a unique identifier for the document that the
40 // word came from (if the current platform requires it), or 0.
42 // - Returns IS_CORRECT if every word from |position_in_text| to the end of
43 // the text is recognized and spelled correctly. Also, returns IS_CORRECT if
44 // the spellchecker failed to initialize.
46 // - Returns IS_SKIPPABLE if a sequence of skippable characters, such as
47 // punctuation, spaces, or unrecognized characters, is found.
48 // |skip_or_misspelling_start| and |skip_or_misspelling_len| are then set to
49 // the position and the length of the sequence of skippable characters.
51 // - Returns IS_MISSPELLED if a misspelled word is found.
52 // |skip_or_misspelling_start| and |skip_or_misspelling_len| are then set to
53 // the position and length of the misspelled word. In addition, finds the
54 // suggested words for a given misspelled word and puts them into
55 // |*optional_suggestions|. If optional_suggestions is NULL, suggested words
56 // will not be looked up. Note that doing suggest lookups can be slow.
57 SpellcheckWordResult SpellCheckWord(
58 const base::char16* text_begin,
59 int position_in_text,
60 int text_length,
61 int tag,
62 int* skip_or_misspelling_start,
63 int* skip_or_misspelling_len,
64 std::vector<base::string16>* optional_suggestions);
66 // Initialize |spellcheck_| if that hasn't happened yet.
67 bool InitializeIfNeeded();
69 // Return true if the underlying spellcheck engine is enabled.
70 bool IsEnabled();
72 private:
73 friend class SpellCheckTest;
75 // Returns whether or not the given word is a contraction of valid words
76 // (e.g. "word:word").
77 bool IsValidContraction(const base::string16& word, int tag);
79 // Represents character attributes used for filtering out characters which
80 // are not supported by this SpellCheck object.
81 SpellcheckCharAttribute character_attributes_;
83 // Represents word iterators used in this spellchecker. The |text_iterator_|
84 // splits text provided by WebKit into words, contractions, or concatenated
85 // words. The |contraction_iterator_| splits a concatenated word extracted by
86 // |text_iterator_| into word components so we can treat a concatenated word
87 // consisting only of correct words as a correct word.
88 SpellcheckWordIterator text_iterator_;
89 SpellcheckWordIterator contraction_iterator_;
91 // Pointer to a platform-specific spelling engine, if it is in use. This
92 // should only be set if hunspell is not used. (I.e. on OSX, for now)
93 scoped_ptr<SpellingEngine> platform_spelling_engine_;
95 DISALLOW_COPY_AND_ASSIGN(SpellcheckLanguage);
98 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_LANGUAGE_H_