Skip to content

Commit 7effcf2

Browse files
committed
Code improvements
1 parent decf49f commit 7effcf2

File tree

5 files changed

+177
-191
lines changed

5 files changed

+177
-191
lines changed

Source/GenCode128/Code128Code.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,30 @@ public enum CodeSetAllowed
3535
/// </summary>
3636
/// <param name="charAscii">The ASCII value of the character to translate</param>
3737
/// <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;
38+
/// <param name="currentCodeSet">The current codeset, that the returned codes need to follow;
3939
/// if the returned codes change that, then this value will be changed to reflect it</param>
4040
/// <returns>An array of integers representing the codes that need to be output to produce the
4141
/// given character</returns>
42-
public static int[] CodesForChar(int charAscii, int lookAheadAscii, ref CodeSet currCodeSet)
42+
public static int[] CodesForChar(int charAscii, int lookAheadAscii, ref CodeSet currentCodeSet)
4343
{
4444
int[] result;
45-
int shifter = -1;
45+
var shifter = -1;
4646

47-
if (!CharCompatibleWithCodeset(charAscii, currCodeSet))
47+
if (!CharCompatibleWithCodeset(charAscii, currentCodeSet))
4848
{
4949
// if we have a lookahead character AND if the next character is ALSO not compatible
50-
if ((lookAheadAscii != -1) && !CharCompatibleWithCodeset(lookAheadAscii, currCodeSet))
50+
if ((lookAheadAscii != -1) && !CharCompatibleWithCodeset(lookAheadAscii, currentCodeSet))
5151
{
5252
// we need to switch code sets
53-
switch (currCodeSet)
53+
switch (currentCodeSet)
5454
{
5555
case CodeSet.CodeA:
5656
shifter = CCodeB;
57-
currCodeSet = CodeSet.CodeB;
57+
currentCodeSet = CodeSet.CodeB;
5858
break;
5959
case CodeSet.CodeB:
6060
shifter = CCodeA;
61-
currCodeSet = CodeSet.CodeA;
61+
currentCodeSet = CodeSet.CodeA;
6262
break;
6363
}
6464
}
@@ -97,21 +97,21 @@ public static CodeSetAllowed CodesetAllowedForChar(int charAscii)
9797
}
9898
else
9999
{
100-
return (charAscii < 32) ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
100+
return charAscii < 32 ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
101101
}
102102
}
103103

104104
/// <summary>
105105
/// Determine if a character can be represented in a given codeset
106106
/// </summary>
107107
/// <param name="charAscii">character to check for</param>
108-
/// <param name="currcs">codeset context to test</param>
108+
/// <param name="currentCodeSet">codeset context to test</param>
109109
/// <returns>true if the codeset contains a representation for the ASCII character</returns>
110-
public static bool CharCompatibleWithCodeset(int charAscii, CodeSet currcs)
110+
public static bool CharCompatibleWithCodeset(int charAscii, CodeSet currentCodeSet)
111111
{
112-
CodeSetAllowed csa = CodesetAllowedForChar(charAscii);
113-
return csa == CodeSetAllowed.CodeAorB || (csa == CodeSetAllowed.CodeA && currcs == CodeSet.CodeA)
114-
|| (csa == CodeSetAllowed.CodeB && currcs == CodeSet.CodeB);
112+
var csa = CodesetAllowedForChar(charAscii);
113+
return csa == CodeSetAllowed.CodeAorB || (csa == CodeSetAllowed.CodeA && currentCodeSet == CodeSet.CodeA)
114+
|| (csa == CodeSetAllowed.CodeB && currentCodeSet == CodeSet.CodeB);
115115
}
116116

117117
/// <summary>
@@ -121,7 +121,7 @@ public static bool CharCompatibleWithCodeset(int charAscii, CodeSet currcs)
121121
/// <returns>code128 symbol value for the character</returns>
122122
public static int CodeValueForChar(int charAscii)
123123
{
124-
return (charAscii >= 32) ? charAscii - 32 : charAscii + 64;
124+
return charAscii >= 32 ? charAscii - 32 : charAscii + 64;
125125
}
126126

127127
/// <summary>

Source/GenCode128/Code128Content.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
namespace GenCode128
22
{
3+
using System.Collections;
34
using System.Text;
45

56
/// <summary>
67
/// Represent the set of code values to be output into barcode form
78
/// </summary>
89
public class Code128Content
910
{
10-
private readonly int[] codeList;
11-
1211
/// <summary>
1312
/// Create content based on a string of ASCII data
1413
/// </summary>
1514
/// <param name="asciiData">the string that should be represented</param>
1615
public Code128Content(string asciiData)
1716
{
18-
this.codeList = this.StringToCode128(asciiData);
17+
this.Codes = this.StringToCode128(asciiData);
1918
}
2019

2120
/// <summary>
2221
/// Provides the Code128 code values representing the object's string
2322
/// </summary>
24-
public int[] Codes
25-
{
26-
get
27-
{
28-
return this.codeList;
29-
}
30-
}
23+
public int[] Codes { get; }
3124

3225
/// <summary>
3326
/// Transform the string into integers representing the Code128 codes
@@ -38,35 +31,33 @@ public int[] Codes
3831
private int[] StringToCode128(string asciiData)
3932
{
4033
// turn the string into ascii byte data
41-
byte[] asciiBytes = Encoding.ASCII.GetBytes(asciiData);
34+
var asciiBytes = Encoding.ASCII.GetBytes(asciiData);
4235

4336
// decide which codeset to start with
44-
Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0
45-
? Code128Code.CodesetAllowedForChar(asciiBytes[0])
46-
: Code128Code.CodeSetAllowed.CodeAorB;
47-
Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 0
48-
? Code128Code.CodesetAllowedForChar(asciiBytes[1])
49-
: Code128Code.CodeSetAllowed.CodeAorB;
50-
CodeSet currcs = this.GetBestStartSet(csa1, csa2);
37+
var csa1 = asciiBytes.Length > 0
38+
? Code128Code.CodesetAllowedForChar(asciiBytes[0])
39+
: Code128Code.CodeSetAllowed.CodeAorB;
40+
var csa2 = asciiBytes.Length > 0
41+
? Code128Code.CodesetAllowedForChar(asciiBytes[1])
42+
: Code128Code.CodeSetAllowed.CodeAorB;
43+
var currentCodeSet = this.GetBestStartSet(csa1, csa2);
5144

5245
// set up the beginning of the barcode
53-
System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3);
54-
5546
// assume no codeset changes, account for start, checksum, and stop
56-
codes.Add(Code128Code.StartCodeForCodeSet(currcs));
57-
47+
var codes = new ArrayList(asciiBytes.Length + 3) { Code128Code.StartCodeForCodeSet(currentCodeSet) };
48+
5849
// add the codes for each character in the string
59-
for (int i = 0; i < asciiBytes.Length; i++)
50+
for (var i = 0; i < asciiBytes.Length; i++)
6051
{
6152
int thischar = asciiBytes[i];
62-
int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1;
53+
var nextchar = asciiBytes.Length > i + 1 ? asciiBytes[i + 1] : -1;
6354

64-
codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs));
55+
codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currentCodeSet));
6556
}
6657

6758
// calculate the check digit
68-
int checksum = (int)codes[0];
69-
for (int i = 1; i < codes.Count; i++)
59+
var checksum = (int)codes[0];
60+
for (var i = 1; i < codes.Count; i++)
7061
{
7162
checksum += i * (int)codes[i];
7263
}
@@ -75,7 +66,7 @@ private int[] StringToCode128(string asciiData)
7566

7667
codes.Add(Code128Code.StopCode());
7768

78-
int[] result = codes.ToArray(typeof(int)) as int[];
69+
var result = codes.ToArray(typeof(int)) as int[];
7970
return result;
8071
}
8172

@@ -88,14 +79,14 @@ private int[] StringToCode128(string asciiData)
8879
/// <returns>The codeset determined to be best to start with</returns>
8980
private CodeSet GetBestStartSet(Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2)
9081
{
91-
int vote = 0;
82+
var vote = 0;
9283

93-
vote += (csa1 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
94-
vote += (csa1 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
95-
vote += (csa2 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
96-
vote += (csa2 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
84+
vote += csa1 == Code128Code.CodeSetAllowed.CodeA ? 1 : 0;
85+
vote += csa1 == Code128Code.CodeSetAllowed.CodeB ? -1 : 0;
86+
vote += csa2 == Code128Code.CodeSetAllowed.CodeA ? 1 : 0;
87+
vote += csa2 == Code128Code.CodeSetAllowed.CodeB ? -1 : 0;
9788

98-
return (vote > 0) ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices
89+
return vote > 0 ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices
9990
}
10091
}
10192
}

Source/GenCode128/Code128Rendering.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,59 +131,58 @@ public static class Code128Rendering
131131
/// </summary>
132132
/// <param name="inputData">Message to be encoded</param>
133133
/// <param name="barWeight">Base thickness for bar width (1 or 2 works well)</param>
134-
/// <param name="addQuietZone">Add required horiz margins (use if output is tight)</param>
134+
/// <param name="addQuietZone">Add required horizontal margins (use if output is tight)</param>
135135
/// <returns>An Image of the Code128 barcode representing the message</returns>
136136
public static Image MakeBarcodeImage(string inputData, int barWeight, bool addQuietZone)
137137
{
138138
// get the Code128 codes to represent the message
139-
Code128Content content = new Code128Content(inputData);
140-
int[] codes = content.Codes;
139+
var content = new Code128Content(inputData);
140+
var codes = content.Codes;
141141

142-
int width, height;
143-
width = (((codes.Length - 3) * 11) + 35) * barWeight;
144-
height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F));
142+
var width = (((codes.Length - 3) * 11) + 35) * barWeight;
143+
var height = Convert.ToInt32(Math.Ceiling(Convert.ToSingle(width) * .15F));
145144

146145
if (addQuietZone)
147146
{
148147
width += 2 * CQuietWidth * barWeight; // on both sides
149148
}
150149

151150
// get surface to draw on
152-
Image myimg = new System.Drawing.Bitmap(width, height);
153-
using (Graphics gr = Graphics.FromImage(myimg))
151+
Image myImage = new Bitmap(width, height);
152+
using (var gr = Graphics.FromImage(myImage))
154153
{
155154
// set to white so we don't have to fill the spaces with white
156-
gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
155+
gr.FillRectangle(Brushes.White, 0, 0, width, height);
157156

158157
// skip quiet zone
159-
int cursor = addQuietZone ? CQuietWidth * barWeight : 0;
158+
var cursor = addQuietZone ? CQuietWidth * barWeight : 0;
160159

161-
for (int codeidx = 0; codeidx < codes.Length; codeidx++)
160+
for (var codeIdx = 0; codeIdx < codes.Length; codeIdx++)
162161
{
163-
int code = codes[codeidx];
162+
var code = codes[codeIdx];
164163

165164
// take the bars two at a time: a black and a white
166-
for (int bar = 0; bar < 8; bar += 2)
165+
for (var bar = 0; bar < 8; bar += 2)
167166
{
168-
int barwidth = CPatterns[code, bar] * barWeight;
169-
int spcwidth = CPatterns[code, bar + 1] * barWeight;
167+
var barWidth = CPatterns[code, bar] * barWeight;
168+
var spcWidth = CPatterns[code, bar + 1] * barWeight;
170169

171170
// if width is zero, don't try to draw it
172-
if (barwidth > 0)
171+
if (barWidth > 0)
173172
{
174-
gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth, height);
173+
gr.FillRectangle(Brushes.Black, cursor, 0, barWidth, height);
175174
}
176175

177176
// note that we never need to draw the space, since we
178177
// initialized the graphics to all white
179178

180179
// advance cursor beyond this pair
181-
cursor += barwidth + spcwidth;
180+
cursor += barWidth + spcWidth;
182181
}
183182
}
184183
}
185184

186-
return myimg;
185+
return myImage;
187186
}
188187
}
189188
}

0 commit comments

Comments
 (0)