tagging release
[dasher.git] / Src / DasherCore / Alphabet / Alphabet.cpp
blob78404421ec10015aba19093d56027c3e00e82014
1 // Alphabet.cpp
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2001-2005 David Ward
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 #include "../../Common/Common.h"
11 #include "Alphabet.h"
12 #include "AlphabetMap.h"
15 using namespace Dasher;
16 using namespace std;
18 // Track memory leaks on Windows to the line that new'd the memory
19 #ifdef _WIN32
20 #ifdef _DEBUG
21 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
22 #define new DEBUG_NEW
23 #undef THIS_FILE
24 static char THIS_FILE[] = __FILE__;
25 #endif
26 #endif
28 /////////////////////////////////////////////////////////////////////////////
30 CAlphabet::CAlphabet()
31 :m_DefaultEncoding(Opts::Western), m_Orientation(Opts::LeftToRight), m_ControlSymbol(-1) {
32 m_Characters.push_back("");
33 m_Display.push_back("");
34 m_Colours.push_back(-1);
35 m_Foreground.push_back("");
38 /////////////////////////////////////////////////////////////////////////////
40 CAlphabet::CAlphabet(const CAlphIO::AlphInfo &AlphInfo)
41 :m_DefaultEncoding(Opts::Western), m_Orientation(Opts::LeftToRight), m_ControlSymbol(-1) {
42 m_Characters.push_back("");
43 m_Display.push_back("");
44 m_Colours.push_back(-1);
45 m_Foreground.push_back("");
47 m_StartConversionSymbol = -1;
48 m_EndConversionSymbol = -1;
50 m_strDefaultContext = AlphInfo.m_strDefaultContext;
52 // Set miscellaneous options
54 SetOrientation(AlphInfo.Orientation);
55 SetLanguage(AlphInfo.Type);
56 SetTrainingFile(AlphInfo.TrainingFile);
57 SetGameModeFile(AlphInfo.GameModeFile);
58 SetPalette(AlphInfo.PreferredColours);
60 for(std::vector<CAlphIO::AlphInfo::character>::const_iterator it(AlphInfo.m_vCharacters.begin()); it != AlphInfo.m_vCharacters.end(); ++it)
61 AddChar(it->Text, it->Display, it->Colour, it->Foreground);
64 // Set Space character if requested
66 // This line makes it a bit easier for our WindowsCE compiler
67 std::string empty = "";
69 if(AlphInfo.ParagraphCharacter.Text != empty)
70 AddParagraphSymbol(AlphInfo.ParagraphCharacter.Text, AlphInfo.ParagraphCharacter.Display, AlphInfo.ParagraphCharacter.Colour, AlphInfo.ParagraphCharacter.Foreground);
72 if(AlphInfo.SpaceCharacter.Text != empty)
73 AddSpaceSymbol(AlphInfo.SpaceCharacter.Text, AlphInfo.SpaceCharacter.Display, AlphInfo.SpaceCharacter.Colour, AlphInfo.SpaceCharacter.Foreground);
75 //-- Added for Kanji Conversion 13 July 2005 by T.Kaburagi START
76 if(AlphInfo.StartConvertCharacter.Text != empty)
77 AddStartConversionSymbol(AlphInfo.StartConvertCharacter.Text, AlphInfo.StartConvertCharacter.Display, AlphInfo.StartConvertCharacter.Colour, AlphInfo.StartConvertCharacter.Foreground);
79 if(AlphInfo.EndConvertCharacter.Text != empty)
80 AddEndConversionSymbol(AlphInfo.EndConvertCharacter.Text, AlphInfo.EndConvertCharacter.Display, AlphInfo.EndConvertCharacter.Colour, AlphInfo.EndConvertCharacter.Foreground);
81 //-- Added for Kanji Conversion 13 July 2005 by T.Kaburagi END
83 // DJW - now the control symbol is always a part of the alphabet
84 // DasherModel knows whether or not to use it
86 // FIXME - We really need to ensure that the control symbol is last in the alphabet with the current logic.
88 if(AlphInfo.ControlCharacter.Display != std::string("") && GetControlSymbol() == -1)
89 AddControlSymbol(AlphInfo.ControlCharacter.Text, AlphInfo.ControlCharacter.Display, AlphInfo.ControlCharacter.Colour, AlphInfo.ControlCharacter.Foreground);
91 // New group stuff
93 m_pBaseGroup = AlphInfo.m_pBaseGroup;
95 #ifdef DASHER_TRACE
96 Trace();
97 #endif
100 /////////////////////////////////////////////////////////////////////////////
102 void CAlphabet::GetSymbols(std::vector<symbol > *Symbols, std::string *Input, bool IsMore) const {
103 string Tmp;
104 symbol CurSymbol = 0, TmpSymbol = 0;
105 bool KeyIsPrefix = false;
106 int extras;
107 unsigned int bit;
109 for(unsigned int i = 0; i < Input->size(); i++) {
111 Tmp = (*Input)[i];
113 /* The string we've been given is in UTF-8. The symbols are
114 also in UTF-8, so we need to pass the entire UTF-8 character
115 which may be several bytes long. RFC 2279 describes this
116 encoding */
118 if((*Input)[i] & 0x80) { // Character is more than 1 byte long
119 extras = 1;
120 for(bit = 0x20; ((*Input)[i] & bit) != 0; bit >>= 1)
121 extras++;
122 if(extras > 5) {
123 } // Malformed character
124 while(extras-- > 0) {
125 Tmp += (*Input)[++i];
129 CurSymbol = TextMap.Get(Tmp, &KeyIsPrefix);
131 if(KeyIsPrefix) {
132 CurSymbol = 0;
133 for(; i < Input->size(); i++) {
135 Tmp += (*Input)[i];
137 TmpSymbol = TextMap.Get(Tmp, &KeyIsPrefix);
138 if(TmpSymbol > 0) {
139 CurSymbol = TmpSymbol;
141 if(!KeyIsPrefix) {
142 if(CurSymbol != 0) {
143 Symbols->push_back(CurSymbol);
145 else {
146 i -= Tmp.size() - 1;
147 //Tmp.erase(Tmp.begin(), Tmp.end());
148 Tmp = "";
150 break;
154 else {
155 if(CurSymbol != 0)
156 Symbols->push_back(CurSymbol);
160 if(IsMore)
161 if(KeyIsPrefix)
162 *Input = Tmp;
163 else
164 *Input = "";
165 else if(KeyIsPrefix)
166 Symbols->push_back(CurSymbol);
169 // add single char to the character set
170 void CAlphabet::AddChar(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
171 m_Characters.push_back(NewCharacter);
172 m_Display.push_back(Display);
173 m_Colours.push_back(Colour);
174 m_Foreground.push_back(Foreground);
176 symbol ThisSymbol = m_Characters.size() - 1;
177 TextMap.Add(NewCharacter, ThisSymbol);
180 /////////////////////////////////////////////////////////////////////////////
182 void CAlphabet::AddParagraphSymbol(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
183 AddChar(NewCharacter, Display, Colour, Foreground);
184 m_ParagraphSymbol = GetNumberSymbols() - 1;
187 /////////////////////////////////////////////////////////////////////////////
189 void CAlphabet::AddSpaceSymbol(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
190 AddChar(NewCharacter, Display, Colour, Foreground);
191 m_SpaceSymbol = GetNumberSymbols() - 1;
194 /////////////////////////////////////////////////////////////////////////////
196 void CAlphabet::AddControlSymbol(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
197 AddChar(NewCharacter, Display, Colour, Foreground);
198 m_ControlSymbol = GetNumberSymbols() - 1;
201 /////////////////////////////////////////////////////////////////////////////
203 void CAlphabet::AddStartConversionSymbol(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
204 AddChar(NewCharacter, Display, Colour, Foreground);
205 m_StartConversionSymbol = GetNumberSymbols() - 1;
208 /////////////////////////////////////////////////////////////////////////////
210 void CAlphabet::AddEndConversionSymbol(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
211 AddChar(NewCharacter, Display, Colour, Foreground);
212 m_EndConversionSymbol = GetNumberSymbols() - 1;
215 /////////////////////////////////////////////////////////////////////////////
216 // diagnostic dump of character set
218 void CAlphabet::Trace() const {
219 // int i;
220 // DASHER_TRACEOUTPUT("GetNumberSymbols() %d\n", GetNumberSymbols());
221 // DASHER_TRACEOUTPUT("GetNumberTextSymbols() %d\n", GetNumberTextSymbols());
223 // int iGroup = 0;
224 // for(i = 0; i < (int) m_Characters.size(); i++) {
225 // if(iGroup < m_iGroups && i == m_GroupStart[iGroup]) {
226 // DASHER_TRACEOUTPUT("Group %d '%s'\n", iGroup, m_GroupLabel[iGroup].c_str());
227 // }
228 // if(iGroup < m_iGroups && i == m_GroupEnd[iGroup]) {
229 // DASHER_TRACEOUTPUT("--------\n");
230 // iGroup++;
231 // }
233 // DASHER_TRACEOUTPUT("Symbol %d Character:'%s' Display:'%s'\n", i, m_Characters[i].c_str(), m_Display[i].c_str());
235 // }
239 /////////////////////////////////////////////////////////////////////////////
241 int CAlphabet::GetTextColour(symbol Symbol) {
242 std::string TextColour = m_Foreground[Symbol];
243 if(TextColour != std::string("")) {
244 return atoi(TextColour.c_str());
246 else {
247 return 4;
251 /////////////////////////////////////////////////////////////////////////////
253 // CAlphabet::CGroupAdder * CAlphabet::GetGroupAdder(int iColour, const std::string &strLabel) {
254 // return new CGroupAdder(*this, iColour, strLabel);
255 // }
257 // /////////////////////////////////////////////////////////////////////////////
259 // CAlphabet::CGroupAdder::CGroupAdder(CAlphabet &alphabet, int iColour, std::string strLabel)
260 // :m_Alphabet(alphabet) {
261 // m_Alphabet.m_iGroups++;
262 // m_Alphabet.m_GroupColour.push_back(iColour);
263 // m_Alphabet.m_GroupLabel.push_back(strLabel);
264 // m_Alphabet.m_GroupStart.push_back(m_Alphabet.GetNumberSymbols());
266 // }
268 // /////////////////////////////////////////////////////////////////////////////
270 // CAlphabet::CGroupAdder::~CGroupAdder() {
271 // m_Alphabet.m_GroupEnd.push_back(m_Alphabet.GetNumberSymbols());
273 // }
275 /////////////////////////////////////////////////////////////////////////////
277 // void CAlphabet::CGroupAdder::AddChar(const std::string NewCharacter, const std::string Display, int Colour, const std::string Foreground) {
278 // m_Alphabet.AddChar(NewCharacter, Display, Colour, Foreground);
280 // }
282 /////////////////////////////////////////////////////////////////////////////