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"
17 // A class to recognize compose and dead key sequence.
18 // Outputs composed character.
19 class UI_BASE_IME_EXPORT CharacterComposer
{
21 struct KeystrokeMeaning
{
22 KeystrokeMeaning(DomKey k
, base::char16 c
) : key(k
), character(c
) {}
24 base::char16 character
;
26 using ComposeBuffer
= std::vector
<KeystrokeMeaning
>;
34 // Returns true if the keypress is recognized as a part of composition
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_
; }
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.
54 // Composite a character with a hexadecimal unicode sequence.
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
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_
;
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
{
92 enum class CheckSequenceResult
{
93 // The sequence is not a composition sequence or the prefix of any
94 // composition sequence.
96 // The sequence is a prefix of one or more composition sequences.
98 // The sequence matches a composition sequence.
102 virtual ~ComposeChecker() {}
103 virtual CheckSequenceResult
CheckSequence(
104 const ui::CharacterComposer::ComposeBuffer
& sequence
,
105 uint32_t* composed_character
) const = 0;
107 DISALLOW_COPY_AND_ASSIGN(ComposeChecker
);
110 // Implementation of |ComposeChecker| using a compact generated tree.
111 class TreeComposeChecker
: public ComposeChecker
{
113 struct CompositionData
{
114 size_t maximum_sequence_length
;
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
;
125 bool Find(uint16_t index
, uint16_t size
, uint16_t key
, uint16_t* value
) const;
126 const CompositionData
& data_
;
131 #endif // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_