summaryrefslogtreecommitdiff
path: root/inputmethods/dasher/Alphabet.cpp
blob: dc58b3585fb31b9a7f64636ae218b89b2d63ba3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Alphabet.cpp
//
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2001-2002 David Ward
//
/////////////////////////////////////////////////////////////////////////////

#include "Alphabet.h"
#include "AlphabetMap.h"

//#include <iostream>
//WinCE doesn't have iostream!

using namespace Dasher;
using namespace std;


CAlphabet::CAlphabet() : m_Groups(0), m_DefaultEncoding(Opts::Western), m_Orientation(Opts::LeftToRight)
{
	m_Characters.push_back("");
	m_Display.push_back("");
	m_Colours.push_back("");
	m_Foreground.push_back("");
	m_Group.push_back(0);
}


void CAlphabet::GetSymbols(vector<symbol>* Symbols, string* Input, bool IsMore)
{
	string Tmp;
	symbol CurSymbol=0, TmpSymbol=0;
	bool KeyIsPrefix;
	int z= Input->size();
	int extras;
	unsigned int bit;

	for (unsigned int i=0; i<Input->size(); i++) {

		Tmp = (*Input)[i];

		/* The string we've been given is in UTF-8. The symbols are
		   also in UTF-8, so we need to pass the entire UTF-8 character
		   which may be several bytes long. RFC 2279 describes this
		   encoding */

		if ((*Input)[i] & 0x80) { // Character is more than 1 byte long
		  extras = 1;
		  for (bit = 0x20; ((*Input)[i] & bit) != 0; bit >>= 1)
		    extras++;
		  if (extras > 5) {
		  } // Malformed character
		  while (extras-->0) {
		    Tmp += (*Input)[++i];
		  }
		}

		CurSymbol = TextMap.Get(Tmp, &KeyIsPrefix);

		if (KeyIsPrefix) {
			CurSymbol = 0;
			for (; i<Input->size(); i++) {

				Tmp += (*Input)[i];

				TmpSymbol = TextMap.Get(Tmp, &KeyIsPrefix);
				if (TmpSymbol>0) {
					CurSymbol = TmpSymbol;
				}
				if (!KeyIsPrefix) {
					if (CurSymbol!=0) {
						Symbols->push_back(CurSymbol);
					} else {
						i -= Tmp.size()-1;
						//Tmp.erase(Tmp.begin(), Tmp.end());
						Tmp = "";
					}
					break;
				}
			}
		} else {
			if (CurSymbol!=0)
				Symbols->push_back(CurSymbol);
		}
	}

	if (IsMore)
		if (KeyIsPrefix)
			*Input = Tmp;
		else
			*Input = "";
	else
		if (KeyIsPrefix)
			Symbols->push_back(CurSymbol);
}


// add single char to the character set
void CAlphabet::AddChar(const string NewCharacter, const string Display, const string Colour, const string Foreground)
{
	m_Characters.push_back(NewCharacter);
	m_Display.push_back(Display);
	m_Colours.push_back(Colour);
	m_Foreground.push_back(Foreground);
	m_Group.push_back(m_Groups);

	symbol ThisSymbol = m_Characters.size()-1;
	TextMap.Add(NewCharacter, ThisSymbol);
}


void CAlphabet::StartNewGroup()
{
	m_Groups++;
}


// diagnostic dump of character set
void CAlphabet::dump() const {
// TODO
/*
	dchar deb[256];
	unsigned int i;
	for (i=1;i<m_vtCharacters.size();i++) {
		//wsprintf(deb,TEXT("%d %c %d\n"),i,m_vtCharacters[i],m_viGroup[i]); // Windows specific
		Usprintf(deb,TEXT("%d %c %d\n"),i,m_vtCharacters[i],m_viGroup[i]);
		DebugOutput(deb);
	}
*/
}

int CAlphabet::GetTextColour(symbol Symbol)
{
  std::string TextColour=m_Foreground[Symbol];
  if (TextColour != "") {
    return atoi(TextColour.c_str());
  } else {
    return 0;
  }
}