1+ namespace GenCode128
2+ {
3+ /// <summary>
4+ /// Static tools for determining codes for individual characters in the content
5+ /// </summary>
6+ public static class Code128Code
7+ {
8+ private const int CShift = 98 ;
9+
10+ private const int CCodeA = 101 ;
11+
12+ private const int CCodeB = 100 ;
13+
14+ private const int CStartA = 103 ;
15+
16+ private const int CStartB = 104 ;
17+
18+ private const int CStop = 106 ;
19+
20+ /// <summary>
21+ /// Indicates which code sets can represent a character -- CodeA, CodeB, or either
22+ /// </summary>
23+ public enum CodeSetAllowed
24+ {
25+ CodeA ,
26+
27+ CodeB ,
28+
29+ CodeAorB
30+ }
31+
32+ /// <summary>
33+ /// Get the Code128 code value(s) to represent an ASCII character, with
34+ /// optional look-ahead for length optimization
35+ /// </summary>
36+ /// <param name="charAscii">The ASCII value of the character to translate</param>
37+ /// <param name="lookAheadAscii">The next character in sequence (or -1 if none)</param>
38+ /// <param name="currCodeSet">The current codeset, that the returned codes need to follow;
39+ /// if the returned codes change that, then this value will be changed to reflect it</param>
40+ /// <returns>An array of integers representing the codes that need to be output to produce the
41+ /// given character</returns>
42+ public static int [ ] CodesForChar ( int charAscii , int lookAheadAscii , ref CodeSet currCodeSet )
43+ {
44+ int [ ] result ;
45+ int shifter = - 1 ;
46+
47+ if ( ! CharCompatibleWithCodeset ( charAscii , currCodeSet ) )
48+ {
49+ // if we have a lookahead character AND if the next character is ALSO not compatible
50+ if ( ( lookAheadAscii != - 1 ) && ! CharCompatibleWithCodeset ( lookAheadAscii , currCodeSet ) )
51+ {
52+ // we need to switch code sets
53+ switch ( currCodeSet )
54+ {
55+ case CodeSet . CodeA :
56+ shifter = CCodeB ;
57+ currCodeSet = CodeSet . CodeB ;
58+ break ;
59+ case CodeSet . CodeB :
60+ shifter = CCodeA ;
61+ currCodeSet = CodeSet . CodeA ;
62+ break ;
63+ }
64+ }
65+ else
66+ {
67+ // no need to switch code sets, a temporary SHIFT will suffice
68+ shifter = CShift ;
69+ }
70+ }
71+
72+ if ( shifter != - 1 )
73+ {
74+ result = new int [ 2 ] ;
75+ result [ 0 ] = shifter ;
76+ result [ 1 ] = CodeValueForChar ( charAscii ) ;
77+ }
78+ else
79+ {
80+ result = new int [ 1 ] ;
81+ result [ 0 ] = CodeValueForChar ( charAscii ) ;
82+ }
83+
84+ return result ;
85+ }
86+
87+ /// <summary>
88+ /// Tells us which codesets a given character value is allowed in
89+ /// </summary>
90+ /// <param name="charAscii">ASCII value of character to look at</param>
91+ /// <returns>Which codeset(s) can be used to represent this character</returns>
92+ public static CodeSetAllowed CodesetAllowedForChar ( int charAscii )
93+ {
94+ if ( charAscii >= 32 && charAscii <= 95 )
95+ {
96+ return CodeSetAllowed . CodeAorB ;
97+ }
98+ else
99+ {
100+ return ( charAscii < 32 ) ? CodeSetAllowed . CodeA : CodeSetAllowed . CodeB ;
101+ }
102+ }
103+
104+ /// <summary>
105+ /// Determine if a character can be represented in a given codeset
106+ /// </summary>
107+ /// <param name="charAscii">character to check for</param>
108+ /// <param name="currcs">codeset context to test</param>
109+ /// <returns>true if the codeset contains a representation for the ASCII character</returns>
110+ public static bool CharCompatibleWithCodeset ( int charAscii , CodeSet currcs )
111+ {
112+ CodeSetAllowed csa = CodesetAllowedForChar ( charAscii ) ;
113+ return csa == CodeSetAllowed . CodeAorB || ( csa == CodeSetAllowed . CodeA && currcs == CodeSet . CodeA )
114+ || ( csa == CodeSetAllowed . CodeB && currcs == CodeSet . CodeB ) ;
115+ }
116+
117+ /// <summary>
118+ /// Gets the integer code128 code value for a character (assuming the appropriate code set)
119+ /// </summary>
120+ /// <param name="charAscii">character to convert</param>
121+ /// <returns>code128 symbol value for the character</returns>
122+ public static int CodeValueForChar ( int charAscii )
123+ {
124+ return ( charAscii >= 32 ) ? charAscii - 32 : charAscii + 64 ;
125+ }
126+
127+ /// <summary>
128+ /// Return the appropriate START code depending on the codeset we want to be in
129+ /// </summary>
130+ /// <param name="cs">The codeset you want to start in</param>
131+ /// <returns>The code128 code to start a barcode in that codeset</returns>
132+ public static int StartCodeForCodeSet ( CodeSet cs )
133+ {
134+ return cs == CodeSet . CodeA ? CStartA : CStartB ;
135+ }
136+
137+ /// <summary>
138+ /// Return the Code128 stop code
139+ /// </summary>
140+ /// <returns>the stop code</returns>
141+ public static int StopCode ( )
142+ {
143+ return CStop ;
144+ }
145+ }
146+ }
0 commit comments