3 /////////////////////////////////////////////////////////////////////////////
5 // Copyright (c) 2001-2005 David Ward
7 /////////////////////////////////////////////////////////////////////////////
9 #include "../../Common/Common.h"
12 #include "AlphabetMap.h"
15 using namespace Dasher
;
18 // Track memory leaks on Windows to the line that new'd the memory
21 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
24 static char THIS_FILE
[] = __FILE__
;
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
);
93 m_pBaseGroup
= AlphInfo
.m_pBaseGroup
;
100 /////////////////////////////////////////////////////////////////////////////
102 void CAlphabet::GetSymbols(std::vector
<symbol
> *Symbols
, std::string
*Input
, bool IsMore
) const {
104 symbol CurSymbol
= 0, TmpSymbol
= 0;
105 bool KeyIsPrefix
= false;
109 for(unsigned int i
= 0; i
< Input
->size(); 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
118 if((*Input
)[i
] & 0x80) { // Character is more than 1 byte long
120 for(bit
= 0x20; ((*Input
)[i
] & bit
) != 0; bit
>>= 1)
123 } // Malformed character
124 while(extras
-- > 0) {
125 Tmp
+= (*Input
)[++i
];
129 CurSymbol
= TextMap
.Get(Tmp
, &KeyIsPrefix
);
133 for(; i
< Input
->size(); i
++) {
137 TmpSymbol
= TextMap
.Get(Tmp
, &KeyIsPrefix
);
139 CurSymbol
= TmpSymbol
;
143 Symbols
->push_back(CurSymbol
);
147 //Tmp.erase(Tmp.begin(), Tmp.end());
156 Symbols
->push_back(CurSymbol
);
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 {
220 // DASHER_TRACEOUTPUT("GetNumberSymbols() %d\n", GetNumberSymbols());
221 // DASHER_TRACEOUTPUT("GetNumberTextSymbols() %d\n", GetNumberTextSymbols());
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());
228 // if(iGroup < m_iGroups && i == m_GroupEnd[iGroup]) {
229 // DASHER_TRACEOUTPUT("--------\n");
233 // DASHER_TRACEOUTPUT("Symbol %d Character:'%s' Display:'%s'\n", i, m_Characters[i].c_str(), m_Display[i].c_str());
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());
251 /////////////////////////////////////////////////////////////////////////////
253 // CAlphabet::CGroupAdder * CAlphabet::GetGroupAdder(int iColour, const std::string &strLabel) {
254 // return new CGroupAdder(*this, iColour, strLabel);
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());
268 // /////////////////////////////////////////////////////////////////////////////
270 // CAlphabet::CGroupAdder::~CGroupAdder() {
271 // m_Alphabet.m_GroupEnd.push_back(m_Alphabet.GetNumberSymbols());
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);
282 /////////////////////////////////////////////////////////////////////////////