1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_COMPOSER_COMPOSITION_INTERFACE_H_
31 #define MOZC_COMPOSER_COMPOSITION_INTERFACE_H_
32 
33 #include <set>
34 #include <string>
35 #include "composer/internal/transliterators.h"
36 
37 namespace mozc {
38 namespace composer {
39 
40 class Table;
41 class CompositionInput;
42 
43 enum TrimMode {
44   TRIM,  // "かn" => "か"
45   ASIS,  // "かn" => "かn"
46   FIX,   // "かn" => "かん"
47 };
48 
49 class CompositionInterface {
50  public:
~CompositionInterface()51   virtual ~CompositionInterface() {}
52   virtual size_t DeleteAt(size_t position) = 0;
53   virtual size_t InsertAt(size_t position, const string &input) = 0;
54   virtual size_t InsertKeyAndPreeditAt(size_t pos,
55                                        const string &key,
56                                        const string &preedit) = 0;
57 
58   // Insert the given |input| to the composition at the given |position|
59   // and return the new position.
60   virtual size_t InsertInput(size_t position,
61                              const CompositionInput &input) = 0;
62 
63   virtual void Erase() = 0;
64 
65   // Get the position on mode_to from position_from on mode_from.
66   virtual size_t ConvertPosition(
67       size_t position_from,
68       Transliterators::Transliterator transliterator_from,
69       Transliterators::Transliterator transliterator_to) = 0;
70 
71   // TODO(komatsu): To be deleted.
72   virtual size_t SetDisplayMode(
73       size_t position,
74       Transliterators::Transliterator transliterator) = 0;
75 
76   virtual void SetTransliterator(
77       size_t position_from,
78       size_t position_to,
79       Transliterators::Transliterator transliterator) = 0;
80   virtual Transliterators::Transliterator GetTransliterator(
81       size_t position) = 0;
82 
83   virtual size_t GetLength() const = 0;
84 
85   // Return string with the default translitarator of each char_chunk
86   // and TrimeMode::ASIS.
87   virtual void GetString(string *composition) const = 0;
88 
89   // Return string with the specified transliterator and TrimeMode::FIX.
90   virtual void GetStringWithTransliterator(
91       Transliterators::Transliterator transliterator,
92       string *output) const = 0;
93 
94   // Get string with consideration for ambiguity from pending input
95   virtual void GetExpandedStrings(string *base,
96                                   std::set<string> *expanded) const = 0;
97 
98   virtual void GetExpandedStringsWithTransliterator(
99       Transliterators::Transliterator transliterator, string *base,
100       std::set<string> *expanded) const = 0;
101 
102   // Return string with the specified trim mode and the current display mode.
103   virtual void GetStringWithTrimMode(TrimMode trim_mode,
104                                      string *output) const = 0;
105 
106   // Return string with the default translitarator of each char_chunk
107   // and TrimMode::ASIS.
108   virtual void GetPreedit(size_t position,
109                           string *left,
110                           string *focused,
111                           string *right) const = 0;
112 
113   virtual void SetInputMode(Transliterators::Transliterator transliterator) = 0;
114 
115   // Return true if the composition is adviced to be committed immediately.
116   virtual bool ShouldCommit() const = 0;
117 
118   // Get clone of the composition.
119   // This class does NOT take the ownership of the return value.
120   //
121   // This interface implements Clone method instead of CopyFrom method because
122   // it is a little hard to implements CopyFrom using CompositionInterface
123   // without making CharChunk accessible from Composer.
124   // If we decided to discard this interface, we should refactor
125   // internal/composer_test.cc.
126   virtual CompositionInterface *Clone() const = 0;
127 
128   // Set composition table.
129   // This class does NOT take the ownership of the table;
130   virtual void SetTable(const Table *table) = 0;
131 };
132 
133 }  // namespace composer
134 }  // namespace mozc
135 
136 #endif  // MOZC_COMPOSER_COMPOSITION_INTERFACE_H_
137