1 using System;
2 using System.Text;
3
4 namespace GenCode128
5 {
6 public enum CodeSet
7 {
8 CodeA
9 ,CodeB
10
11 }
12
13 public class Code128Content
17 {
18 private int[] mCodeList;
19
20 public Code128Content( string AsciiData )
25 {
26 mCodeList = StringToCode128(AsciiData);
27 }
28
29 public int[] Codes
33 {
34 get
35 {
36 return mCodeList;
37 }
38 }
39
40 private int[] StringToCode128( string AsciiData )
47 {
48
49 byte[] asciiBytes = Encoding.ASCII.GetBytes( AsciiData );
50
51
52 Code128Code.CodeSetAllowed csa1 = asciiBytes.Length>0 ? Code128Code.CodesetAllowedForChar( asciiBytes[0] ) : Code128Code.CodeSetAllowed.CodeAorB;
53 Code128Code.CodeSetAllowed csa2 = asciiBytes.Length>0 ? Code128Code.CodesetAllowedForChar( asciiBytes[1] ) : Code128Code.CodeSetAllowed.CodeAorB;
54 CodeSet currcs = GetBestStartSet(csa1,csa2);
55
56
57 System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3);
58 codes.Add(Code128Code.StartCodeForCodeSet(currcs));
59
60
61 for (int i = 0; i < asciiBytes.Length; i++)
62 {
63 int thischar = asciiBytes[i];
64 int nextchar = asciiBytes.Length>(i+1) ? asciiBytes[i+1] : -1;
65
66 codes.AddRange( Code128Code.CodesForChar(thischar, nextchar, ref currcs) );
67 }
68
69
70 int checksum = (int)(codes[0]);
71 for (int i = 1; i < codes.Count; i++)
72 {
73 checksum += i * (int)(codes[i]);
74 }
75 codes.Add( checksum % 103 );
76
77 codes.Add( Code128Code.StopCode() );
78
79 int[] result = codes.ToArray(typeof(int)) as int[];
80 return result;
81 }
82
83 private CodeSet GetBestStartSet( Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2 )
91 {
92 int vote = 0;
93
94 vote += (csa1==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
95 vote += (csa1==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
96 vote += (csa2==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
97 vote += (csa2==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
98
99 return (vote>0) ? CodeSet.CodeA : CodeSet.CodeB;
100 }
101 }
102
103 public static class Code128Code
107 {
108 #region Constants
109
110 private const int cSHIFT = 98;
111 private const int cCODEA = 101;
112 private const int cCODEB = 100;
113
114 private const int cSTARTA = 103;
115 private const int cSTARTB = 104;
116 private const int cSTOP = 106;
117
118 #endregion
119
120 public static int[] CodesForChar(int CharAscii, int LookAheadAscii, ref CodeSet CurrCodeSet)
131 {
132 int[] result;
133 int shifter = -1;
134
135 if (!CharCompatibleWithCodeset(CharAscii,CurrCodeSet))
136 {
137
138 if ( (LookAheadAscii != -1) && !CharCompatibleWithCodeset(LookAheadAscii,CurrCodeSet) )
139 {
140
141 switch (CurrCodeSet)
142 {
143 case CodeSet.CodeA:
144 shifter = cCODEB;
145 CurrCodeSet = CodeSet.CodeB;
146 break;
147 case CodeSet.CodeB:
148 shifter = cCODEA;
149 CurrCodeSet = CodeSet.CodeA;
150 break;
151 }
152 }
153 else
154 {
155
156 shifter = cSHIFT;
157 }
158 }
159
160 if (shifter!=-1)
161 {
162 result = new int[2];
163 result[0] = shifter;
164 result[1] = CodeValueForChar(CharAscii);
165 }
166 else
167 {
168 result = new int[1];
169 result[0] = CodeValueForChar(CharAscii);
170 }
171
172 return result;
173 }
174
175 public static CodeSetAllowed CodesetAllowedForChar(int CharAscii)
181 {
182 if (CharAscii>=32 && CharAscii<=95)
183 {
184 return CodeSetAllowed.CodeAorB;
185 }
186 else
187 {
188 return (CharAscii<32) ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
189 }
190 }
191
192 public static bool CharCompatibleWithCodeset(int CharAscii, CodeSet currcs)
199 {
200 CodeSetAllowed csa = CodesetAllowedForChar(CharAscii);
201 return csa==CodeSetAllowed.CodeAorB
202 || (csa==CodeSetAllowed.CodeA && currcs==CodeSet.CodeA)
203 || (csa==CodeSetAllowed.CodeB && currcs==CodeSet.CodeB);
204 }
205
206 public static int CodeValueForChar(int CharAscii)
212 {
213 return (CharAscii>=32) ? CharAscii-32 : CharAscii+64;
214 }
215
216 public static int StartCodeForCodeSet(CodeSet cs)
222 {
223 return cs==CodeSet.CodeA ? cSTARTA : cSTARTB;
224 }
225
226 public static int StopCode()
231 {
232 return cSTOP;
233 }
234
235 public enum CodeSetAllowed
239 {
240 CodeA,
241 CodeB,
242 CodeAorB
243 }
244
245 }
246 }