Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / ime / chromeos / character_composer.h
blob78fae696f26d70b6b14cffbbe94cb4c9acef9151
1 // Copyright 2013 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 UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_
6 #define UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_
8 #include <vector>
10 #include "base/strings/string_util.h"
11 #include "ui/base/ime/ui_base_ime_export.h"
12 #include "ui/events/keycodes/dom/dom_key.h"
14 namespace ui {
15 class KeyEvent;
17 // A class to recognize compose and dead key sequence.
18 // Outputs composed character.
19 class UI_BASE_IME_EXPORT CharacterComposer {
20 public:
21 using ComposeBuffer = std::vector<DomKey>;
23 CharacterComposer();
24 ~CharacterComposer();
26 void Reset();
28 // Filters keypress.
29 // Returns true if the keypress is recognized as a part of composition
30 // sequence.
31 // Fabricated events which don't have the native event, are not supported.
32 bool FilterKeyPress(const ui::KeyEvent& event);
34 // Returns a string consisting of composed character.
35 // Empty string is returned when there is no composition result.
36 const base::string16& composed_character() const {
37 return composed_character_;
40 // Returns the preedit string.
41 const base::string16& preedit_string() const { return preedit_string_; }
43 private:
44 // An enum to describe composition mode.
45 enum CompositionMode {
46 // This is the initial state.
47 // Composite a character with dead-keys and compose-key.
48 KEY_SEQUENCE_MODE,
49 // Composite a character with a hexadecimal unicode sequence.
50 HEX_MODE,
53 // Filters keypress in key sequence mode.
54 bool FilterKeyPressSequenceMode(const ui::KeyEvent& event);
56 // Filters keypress in hexadecimal mode.
57 bool FilterKeyPressHexMode(const ui::KeyEvent& event);
59 // Commit a character composed from hexadecimal uncode sequence
60 void CommitHex();
62 // Updates preedit string in hexadecimal mode.
63 void UpdatePreeditStringHexMode();
65 // Remembers keypresses previously filtered.
66 std::vector<DomKey> compose_buffer_;
68 // Records hexadecimal digits previously filtered.
69 std::vector<unsigned int> hex_buffer_;
71 // A string representing the composed character.
72 base::string16 composed_character_;
74 // Preedit string.
75 base::string16 preedit_string_;
77 // Composition mode which this instance is in.
78 CompositionMode composition_mode_;
80 DISALLOW_COPY_AND_ASSIGN(CharacterComposer);
83 // Abstract class for determining whether a ComposeBuffer forms a valid
84 // character composition sequence.
85 class ComposeChecker {
86 public:
87 enum class CheckSequenceResult {
88 // The sequence is not a composition sequence or the prefix of any
89 // composition sequence.
90 NO_MATCH,
91 // The sequence is a prefix of one or more composition sequences.
92 PREFIX_MATCH,
93 // The sequence matches a composition sequence.
94 FULL_MATCH
96 ComposeChecker() {}
97 virtual ~ComposeChecker() {}
98 virtual CheckSequenceResult CheckSequence(
99 const ui::CharacterComposer::ComposeBuffer& sequence,
100 uint32_t* composed_character) const = 0;
101 private:
102 DISALLOW_COPY_AND_ASSIGN(ComposeChecker);
105 // Implementation of |ComposeChecker| using a compact generated tree.
106 class TreeComposeChecker : public ComposeChecker {
107 public:
108 struct CompositionData {
109 size_t maximum_sequence_length;
110 int tree_entries;
111 const uint16_t* tree;
114 TreeComposeChecker(const CompositionData& data) : data_(data) {}
115 CheckSequenceResult CheckSequence(
116 const ui::CharacterComposer::ComposeBuffer& sequence,
117 uint32_t* composed_character) const override;
119 private:
120 bool Find(uint16_t index, uint16_t size, uint16_t key, uint16_t* value) const;
121 const CompositionData& data_;
124 } // namespace ui
126 #endif // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_