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_
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"
17 // A class to recognize compose and dead key sequence.
18 // Outputs composed character.
19 class UI_BASE_IME_EXPORT CharacterComposer
{
21 using ComposeBuffer
= std::vector
<DomKey
>;
29 // Returns true if the keypress is recognized as a part of composition
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_
; }
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.
49 // Composite a character with a hexadecimal unicode sequence.
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
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_
;
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
{
87 enum class CheckSequenceResult
{
88 // The sequence is not a composition sequence or the prefix of any
89 // composition sequence.
91 // The sequence is a prefix of one or more composition sequences.
93 // The sequence matches a composition sequence.
97 virtual ~ComposeChecker() {}
98 virtual CheckSequenceResult
CheckSequence(
99 const ui::CharacterComposer::ComposeBuffer
& sequence
,
100 uint32_t* composed_character
) const = 0;
102 DISALLOW_COPY_AND_ASSIGN(ComposeChecker
);
105 // Implementation of |ComposeChecker| using a compact generated tree.
106 class TreeComposeChecker
: public ComposeChecker
{
108 struct CompositionData
{
109 size_t maximum_sequence_length
;
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
;
120 bool Find(uint16_t index
, uint16_t size
, uint16_t key
, uint16_t* value
) const;
121 const CompositionData
& data_
;
126 #endif // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_