spellcheck_language.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.
  4. #ifndef COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_LANGUAGE_H_
  5. #define COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_LANGUAGE_H_
  6. #include <memory>
  7. #include <queue>
  8. #include <string>
  9. #include <vector>
  10. #include "base/files/file.h"
  11. #include "components/spellcheck/renderer/spellcheck_worditerator.h"
  12. namespace service_manager {
  13. class LocalInterfaceProvider;
  14. }
  15. class SpellingEngine;
  16. class SpellcheckLanguage {
  17. public:
  18. enum SpellcheckWordResult {
  19. // Denotes that every recognized word is spelled correctly, from the point
  20. // of spellchecking to the end of the text.
  21. IS_CORRECT,
  22. // A sequence of skippable characters, such as punctuation, spaces, or
  23. // characters not recognized by the current spellchecking language.
  24. IS_SKIPPABLE,
  25. // A misspelled word has been found in the text.
  26. IS_MISSPELLED
  27. };
  28. explicit SpellcheckLanguage(
  29. service_manager::LocalInterfaceProvider* embedder_provider);
  30. SpellcheckLanguage(const SpellcheckLanguage&) = delete;
  31. SpellcheckLanguage& operator=(const SpellcheckLanguage&) = delete;
  32. ~SpellcheckLanguage();
  33. void Init(base::File file, const std::string& language);
  34. // Spellcheck a sequence of text.
  35. // |text_length| is the length of the text being spellchecked. The |tag|
  36. // parameter should either be a unique identifier for the document that the
  37. // word came from (if the current platform requires it), or 0.
  38. //
  39. // - Returns IS_CORRECT if every word from |position_in_text| to the end of
  40. // the text is recognized and spelled correctly. Also, returns IS_CORRECT if
  41. // the spellchecker failed to initialize.
  42. //
  43. // - Returns IS_SKIPPABLE if a sequence of skippable characters, such as
  44. // punctuation, spaces, or unrecognized characters, is found.
  45. // |skip_or_misspelling_start| and |skip_or_misspelling_len| are then set to
  46. // the position and the length of the sequence of skippable characters.
  47. //
  48. // - Returns IS_MISSPELLED if a misspelled word is found.
  49. // |skip_or_misspelling_start| and |skip_or_misspelling_len| are then set to
  50. // the position and length of the misspelled word. In addition, finds the
  51. // suggested words for a given misspelled word and puts them into
  52. // |*optional_suggestions|. If optional_suggestions is nullptr, suggested
  53. // words will not be looked up. Note that doing suggest lookups can be slow.
  54. SpellcheckWordResult SpellCheckWord(
  55. const char16_t* text_begin,
  56. size_t position_in_text,
  57. size_t text_length,
  58. int tag,
  59. size_t* skip_or_misspelling_start,
  60. size_t* skip_or_misspelling_len,
  61. std::vector<std::u16string>* optional_suggestions);
  62. // Initialize |spellcheck_| if that hasn't happened yet.
  63. bool InitializeIfNeeded();
  64. // Return true if the underlying spellcheck engine is enabled.
  65. bool IsEnabled();
  66. // Returns true if all the characters in a text string are in the script
  67. // associated with this spellcheck language.
  68. bool IsTextInSameScript(const std::u16string& text) const;
  69. private:
  70. friend class SpellCheckTest;
  71. friend class FakeSpellCheck;
  72. // Returns whether or not the given word is a contraction of valid words
  73. // (e.g. "word:word").
  74. bool IsValidContraction(const std::u16string& word, int tag);
  75. // Represents character attributes used for filtering out characters which
  76. // are not supported by this SpellCheck object.
  77. SpellcheckCharAttribute character_attributes_;
  78. // Represents word iterators used in this spellchecker. The |text_iterator_|
  79. // splits text provided by WebKit into words, contractions, or concatenated
  80. // words. The |contraction_iterator_| splits a concatenated word extracted by
  81. // |text_iterator_| into word components so we can treat a concatenated word
  82. // consisting only of correct words as a correct word.
  83. SpellcheckWordIterator text_iterator_;
  84. SpellcheckWordIterator contraction_iterator_;
  85. // Pointer to a platform-specific spelling engine, if it is in use. This
  86. // should only be set if hunspell is not used. (I.e. on OSX, for now)
  87. std::unique_ptr<SpellingEngine> platform_spelling_engine_;
  88. };
  89. #endif // COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_LANGUAGE_H_