1 // Copyright 2018 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 
5 #ifndef CHROMEOS_SERVICES_IME_PUBLIC_CPP_RULEBASED_ENGINE_H_
6 #define CHROMEOS_SERVICES_IME_PUBLIC_CPP_RULEBASED_ENGINE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 
13 namespace chromeos {
14 namespace ime {
15 namespace rulebased {
16 
17 class RulesData;
18 
19 enum Modifiers {
20   MODIFIER_NONE = 0,
21   MODIFIER_SHIFT = 1 << 0,
22   MODIFIER_ALTGR = 1 << 1,
23   MODIFIER_CAPSLOCK = 1 << 2,
24 };
25 
26 struct ProcessKeyResult {
27   bool key_handled = false;
28   std::string commit_text;
29   std::string composition_text;
30 };
31 
32 class Engine {
33  public:
34   Engine();
35   ~Engine();
36 
37   static bool IsImeSupported(const std::string& id);
38 
39   void Activate(const std::string& id);
40   void Reset();
41   ProcessKeyResult ProcessKey(const std::string& code, uint8_t modifier_state);
42 
process_key_count()43   uint32_t process_key_count() const { return process_key_count_; }
44 
45  private:
46   void ClearHistory();
47   ProcessKeyResult ProcessBackspace();
48 
49   std::unique_ptr<const RulesData> current_data_;
50   std::string current_id_;
51   uint32_t process_key_count_;
52 
53   // Current state.
54   // The current context (composition).
55   std::string context_;
56   // The current transat position.
57   // Refers to RulesData::Transform for details about transat.
58   int transat_ = -1;
59 
60   // History state.
61   // The history context, before entering ambiguous transforms.
62   std::string history_context_;
63   // The history transat, before entering ambiguous transforms.
64   int history_transat_ = -1;
65   // The history ambiguous string which matches the history prune regexp.
66   std::string history_ambi_;
67 
68   DISALLOW_COPY_AND_ASSIGN(Engine);
69 };
70 
71 }  // namespace rulebased
72 }  // namespace ime
73 }  // namespace chromeos
74 
75 #endif  // CHROMEOS_SERVICES_IME_PUBLIC_CPP_RULEBASED_ENGINE_H_
76