Add ICU message format support
[chromium-blink-merge.git] / ui / base / ime / chromeos / character_composer.h
blobbcb4db90bc086fc9993df6d4cfbe8d8c0f778c19
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"
13 namespace ui {
14 class KeyEvent;
15 enum class DomKey;
17 // A class to recognize compose and dead key sequence.
18 // Outputs composed character.
19 class UI_BASE_IME_EXPORT CharacterComposer {
20 public:
21 struct KeystrokeMeaning {
22 KeystrokeMeaning(DomKey k, base::char16 c) : key(k), character(c) {}
23 DomKey key;
24 base::char16 character;
26 using ComposeBuffer = std::vector<KeystrokeMeaning>;
28 CharacterComposer();
29 ~CharacterComposer();
31 void Reset();
33 // Filters keypress.
34 // Returns true if the keypress is recognized as a part of composition
35 // sequence.
36 // Fabricated events which don't have the native event, are not supported.
37 bool FilterKeyPress(const ui::KeyEvent& event);
39 // Returns a string consisting of composed character.
40 // Empty string is returned when there is no composition result.
41 const base::string16& composed_character() const {
42 return composed_character_;
45 // Returns the preedit string.
46 const base::string16& preedit_string() const { return preedit_string_; }
48 private:
49 // An enum to describe composition mode.
50 enum CompositionMode {
51 // This is the initial state.
52 // Composite a character with dead-keys and compose-key.
53 KEY_SEQUENCE_MODE,
54 // Composite a character with a hexadecimal unicode sequence.
55 HEX_MODE,
58 // Filters keypress in key sequence mode.
59 bool FilterKeyPressSequenceMode(const ui::KeyEvent& event);
61 // Filters keypress in hexadecimal mode.
62 bool FilterKeyPressHexMode(const ui::KeyEvent& event);
64 // Commit a character composed from hexadecimal uncode sequence
65 void CommitHex();
67 // Updates preedit string in hexadecimal mode.
68 void UpdatePreeditStringHexMode();
70 // Remembers keypresses previously filtered.
71 std::vector<KeystrokeMeaning> compose_buffer_;
73 // Records hexadecimal digits previously filtered.
74 std::vector<unsigned int> hex_buffer_;
76 // A string representing the composed character.
77 base::string16 composed_character_;
79 // Preedit string.
80 base::string16 preedit_string_;
82 // Composition mode which this instance is in.
83 CompositionMode composition_mode_;
85 DISALLOW_COPY_AND_ASSIGN(CharacterComposer);
88 // Abstract class for determining whether a ComposeBuffer forms a valid
89 // character composition sequence.
90 class ComposeChecker {
91 public:
92 enum class CheckSequenceResult {
93 // The sequence is not a composition sequence or the prefix of any
94 // composition sequence.
95 NO_MATCH,
96 // The sequence is a prefix of one or more composition sequences.
97 PREFIX_MATCH,
98 // The sequence matches a composition sequence.
99 FULL_MATCH
101 ComposeChecker() {}
102 virtual ~ComposeChecker() {}
103 virtual CheckSequenceResult CheckSequence(
104 const ui::CharacterComposer::ComposeBuffer& sequence,
105 uint32_t* composed_character) const = 0;
106 private:
107 DISALLOW_COPY_AND_ASSIGN(ComposeChecker);
110 // Implementation of |ComposeChecker| using a compact generated tree.
111 class TreeComposeChecker : public ComposeChecker {
112 public:
113 struct CompositionData {
114 size_t maximum_sequence_length;
115 int tree_entries;
116 const uint16_t* tree;
119 TreeComposeChecker(const CompositionData& data) : data_(data) {}
120 CheckSequenceResult CheckSequence(
121 const ui::CharacterComposer::ComposeBuffer& sequence,
122 uint32_t* composed_character) const override;
124 private:
125 bool Find(uint16_t index, uint16_t size, uint16_t key, uint16_t* value) const;
126 const CompositionData& data_;
129 } // namespace ui
131 #endif // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_