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_INTERNAL_COMPOSITION_H_
31 #define MOZC_COMPOSER_INTERNAL_COMPOSITION_H_
32 
33 #include "composer/composition_interface.h"
34 
35 #include <list>
36 #include <set>
37 #include <string>
38 
39 #include "base/port.h"
40 
41 namespace mozc {
42 namespace composer {
43 
44 class CharChunk;
45 typedef std::list<CharChunk*> CharChunkList;
46 
47 class CompositionInput;
48 class Table;
49 
50 class Composition : public CompositionInterface {
51  public:
52   explicit Composition(const Table *table);
53   virtual ~Composition();
54 
55   virtual size_t DeleteAt(size_t position);
56   virtual size_t InsertAt(size_t position, const string &input);
57   virtual size_t InsertKeyAndPreeditAt(size_t position,
58                                        const string &key,
59                                        const string &preedit);
60   // Insert the given |input| to the composition at the given |position|
61   // and return the new position.
62   virtual size_t InsertInput(size_t position, const CompositionInput &input);
63 
64   virtual void Erase();
65 
66   // Get the position on mode_to from position_from on mode_from.
67   virtual size_t ConvertPosition(
68       size_t position_from,
69       Transliterators::Transliterator transliterator_from,
70       Transliterators::Transliterator transliterator_to);
71 
72   // TODO(komatsu): To be deleted.
73   virtual size_t SetDisplayMode(size_t position,
74                                 Transliterators::Transliterator transliterator);
75 
76   // NOTE(komatsu) Kind of SetDisplayMode.
77   virtual void SetTransliterator(
78       size_t position_from,
79       size_t position_to,
80       Transliterators::Transliterator transliterator);
81   virtual Transliterators::Transliterator GetTransliterator(size_t position);
82 
83   virtual size_t GetLength() const;
84   virtual void GetString(string *composition) const;
85   virtual void GetStringWithTransliterator(
86       Transliterators::Transliterator transliterator,
87       string *output) const;
88   virtual void GetStringWithTrimMode(TrimMode trim_mode, string* output) const;
89   // Get string with consideration for ambiguity from pending input
90   virtual void GetExpandedStrings(string *base,
91                                   std::set<string> *expanded) const;
92   virtual void GetExpandedStringsWithTransliterator(
93       Transliterators::Transliterator transliterator,
94       string *base,
95       std::set<string> *expanded) const;
96   virtual void GetPreedit(
97       size_t position, string *left, string *focused, string *right) const;
98 
99   virtual void SetInputMode(Transliterators::Transliterator transliterator);
100 
101   // Return true if the composition is adviced to be committed immediately.
102   virtual bool ShouldCommit() const;
103 
104   // Get a clone.
105   // Clone is a thin wrapper of CloneImpl.
106   // CloneImpl is created to write test codes without dynamic_cast.
107   virtual CompositionInterface *Clone() const;
108   Composition *CloneImpl() const;
109 
110   virtual void SetTable(const Table *table);
111 
112   // Following methods are declared as public for unit test.
113   void GetChunkAt(size_t position,
114                   Transliterators::Transliterator transliterator,
115                   CharChunkList::iterator *chunk_it,
116                   size_t *inner_position);
117   size_t GetPosition(Transliterators::Transliterator transliterator,
118                      const CharChunkList::const_iterator &it) const;
119 
120   CharChunkList::iterator GetInsertionChunk(CharChunkList::iterator *it);
121   CharChunkList::iterator InsertChunk(CharChunkList::iterator *left_it);
122 
123   CharChunk *MaybeSplitChunkAt(size_t position, CharChunkList::iterator *it);
124 
125   // Combine |input| and chunks from |it| to left direction,
126   // which have pending data and can be combined.
127   // e.g. [pending='q']+[pending='k']+[pending='y']+[input='o'] are combined
128   //      into [pending='q']+[pending='ky'] because [pending='ky']+[input='o']
129   //      can turn to be a fixed chunk.
130   // e.g. [pending='k']+[pending='y']+[input='q'] are not combined.
131   void CombinePendingChunks(CharChunkList::iterator it,
132                             const CompositionInput &input);
133   const CharChunkList &GetCharChunkList() const;
table()134   const Table *table() const {
135     return table_;
136   }
chunks()137   const CharChunkList &chunks() const {
138     return chunks_;
139   }
input_t12r()140   Transliterators::Transliterator input_t12r() const {
141     return input_t12r_;
142   }
143 
144  private:
145   void GetStringWithModes(Transliterators::Transliterator transliterator,
146                           TrimMode trim_mode,
147                           string *output) const;
148 
149   const Table *table_;
150   CharChunkList chunks_;
151   Transliterators::Transliterator input_t12r_;
152 
153   DISALLOW_COPY_AND_ASSIGN(Composition);
154 };
155 
156 }  // namespace composer
157 }  // namespace mozc
158 
159 #endif  // MOZC_COMPOSER_INTERNAL_COMPOSITION_H_
160