-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the ability to specify codeset switches manually in a Code128 barcode. #1
base: master
Are you sure you want to change the base?
Changes from all commits
f406ed0
fbdc6c4
32611c6
33fda11
2b0be28
d0f4a97
6691ded
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Data; | ||
|
@@ -11,7 +12,7 @@ namespace BarcodeLib.Symbologies | |
/// </summary> | ||
class Code128 : BarcodeCommon, IBarcode | ||
{ | ||
public enum TYPES:int { DYNAMIC, A, B, C }; | ||
public enum TYPES : int {DYNAMIC , A , B , C , CUSTOM }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra spaces added here |
||
private DataTable C128_Code = new DataTable("C128"); | ||
private List<string> _FormattedData = new List<string>(); | ||
private List<string> _EncodedData = new List<string>(); | ||
|
@@ -265,6 +266,86 @@ private string CalculateCheckDigit() | |
DataRow[] RetRows = this.C128_Code.Select("Value = '" + Remainder.ToString() + "'"); | ||
return RetRows[0]["Encoding"].ToString(); | ||
} | ||
|
||
private void BreakUpAndInsertCodeCharacters() | ||
{ | ||
string temp = ""; | ||
string tempRawData = Raw_Data; | ||
var currentType = Code128.TYPES.DYNAMIC; | ||
var starting = true; | ||
|
||
if (tempRawData[0] != Convert.ToChar(140)) | ||
Error("EC128-7: Custom CODE 128 string does not start with a specified code."); | ||
|
||
var codeBlocks = tempRawData.Split(Convert.ToChar(140)); | ||
foreach (var codeBlock in codeBlocks) | ||
{ | ||
if (String.IsNullOrEmpty(codeBlock)) | ||
continue; | ||
|
||
var startCodeVal = codeBlock[0]; | ||
switch (startCodeVal) | ||
{ | ||
case 'A': | ||
if (starting) | ||
{ | ||
_FormattedData.Add("START_A"); | ||
starting = false; | ||
} | ||
else | ||
{ | ||
_FormattedData.Add("CODE_A"); | ||
} | ||
currentType = TYPES.A; | ||
break; | ||
case 'B': | ||
if (starting) | ||
{ | ||
_FormattedData.Add("START_B"); | ||
starting = false; | ||
} | ||
else | ||
{ | ||
_FormattedData.Add("CODE_B"); | ||
} | ||
currentType = TYPES.B; | ||
break; | ||
case 'C': | ||
if (starting) | ||
{ | ||
_FormattedData.Add("START_C"); | ||
starting = false; | ||
} | ||
else | ||
{ | ||
_FormattedData.Add("CODE_C"); | ||
} | ||
currentType = TYPES.C; | ||
break; | ||
default: | ||
Error("EC128-8: Attempting to use unsuported code in custom CODE 128 barcode."); | ||
break; | ||
} | ||
var dataToAdd = codeBlock.Substring(1); | ||
if (currentType == TYPES.A || currentType == TYPES.B) | ||
{ | ||
foreach (var c in dataToAdd) | ||
{ | ||
_FormattedData.Add(c.ToString()); | ||
} | ||
} | ||
else | ||
{ | ||
if (dataToAdd.Length % 2 != 0) | ||
Error("EC128-9: Attempting to encode a CODE 128 C block with odd number of digits"); | ||
for (int i = 0; i < dataToAdd.Length; i += 2) | ||
{ | ||
var toAdd = string.Format("{0}{1}", dataToAdd[i], dataToAdd[i + 1]); | ||
_FormattedData.Add(toAdd); | ||
} | ||
} | ||
} | ||
} | ||
private void BreakUpDataForEncoding() | ||
{ | ||
string temp = ""; | ||
|
@@ -396,15 +477,26 @@ private void InsertStartandCodeCharacters() | |
} | ||
private string GetEncoding() | ||
{ | ||
//break up data for encoding | ||
BreakUpDataForEncoding(); | ||
if (type == TYPES.CUSTOM) | ||
{ | ||
BreakUpAndInsertCodeCharacters(); | ||
} | ||
else | ||
{ | ||
|
||
//break up data for encoding | ||
BreakUpDataForEncoding(); | ||
|
||
//insert the start characters | ||
InsertStartandCodeCharacters(); | ||
//insert the start characters | ||
InsertStartandCodeCharacters(); | ||
} | ||
|
||
string CheckDigit = CalculateCheckDigit(); | ||
|
||
string Encoded_Data = ""; | ||
|
||
var encodingType = TYPES.A; //Always start with code A | ||
|
||
foreach (string s in _FormattedData) | ||
{ | ||
//handle exception with apostrophes in select statements | ||
|
@@ -414,27 +506,44 @@ private string GetEncoding() | |
//select encoding only for type selected | ||
switch (this.type) | ||
{ | ||
case TYPES.A: E_Row = this.C128_Code.Select("A = '" + s1 + "'"); | ||
case TYPES.A: | ||
E_Row = this.C128_Code.Select("A = '" + s1 + "'"); | ||
break; | ||
case TYPES.B: E_Row = this.C128_Code.Select("B = '" + s1 + "'"); | ||
case TYPES.B: | ||
E_Row = this.C128_Code.Select("B = '" + s1 + "'"); | ||
break; | ||
case TYPES.C: E_Row = this.C128_Code.Select("C = '" + s1 + "'"); | ||
case TYPES.C: | ||
E_Row = this.C128_Code.Select("C = '" + s1 + "'"); | ||
break; | ||
case TYPES.DYNAMIC: E_Row = this.C128_Code.Select("A = '" + s1 + "'"); | ||
|
||
if (E_Row.Length <= 0) | ||
{ | ||
E_Row = this.C128_Code.Select("B = '" + s1 + "'"); | ||
|
||
if (E_Row.Length <= 0) | ||
{ | ||
E_Row = this.C128_Code.Select("C = '" + s1 + "'"); | ||
}//if | ||
}//if | ||
case TYPES.DYNAMIC: | ||
case TYPES.CUSTOM: | ||
//Always use value from current encoding | ||
E_Row = this.C128_Code.Select(encodingType.ToString() + " = '" + s1 + "'"); | ||
//If we have a START or CODE element then determine which encoding to switch to. | ||
if (s1.StartsWith("START") || s1.StartsWith("CODE")) | ||
{ | ||
var typeSplit = s1.Split('_'); | ||
switch (typeSplit[1]) | ||
{ | ||
case "A": | ||
encodingType = TYPES.A; | ||
break; | ||
case "B": | ||
encodingType = TYPES.B; | ||
break; | ||
case "C": | ||
encodingType = TYPES.C; | ||
break; | ||
default: | ||
Error("EC128-8: Attempting to use unsuported code in custom CODE 128 barcode."); | ||
break; | ||
} | ||
} | ||
break; | ||
default: E_Row = null; | ||
default: | ||
E_Row = null; | ||
break; | ||
}//switch | ||
} //switch | ||
|
||
if (E_Row == null || E_Row.Length <= 0) | ||
Error("EC128-5: Could not find encoding of a value( " + s1 + " ) in C128 type " + this.type.ToString()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace BarcodeLib.Symbologies | ||
{ | ||
public class Code128CustomBuilder | ||
{ | ||
public enum Code128Type { A, B, C }; | ||
private string Code128String { get; set; } | ||
|
||
public static Code128CustomBuilder Start(Code128Type type, string barcodePart) | ||
{ | ||
return new Code128CustomBuilder() | ||
{ | ||
Code128String = String.Format("{0}{1}{2}", char.ConvertFromUtf32(140), type.ToString(), barcodePart) | ||
}; | ||
} | ||
|
||
public Code128CustomBuilder AddPart(Code128Type type, string barcodePart) | ||
{ | ||
return new Code128CustomBuilder() | ||
{ | ||
Code128String = | ||
string.Format("{0}{1}{2}{3}", Code128String, char.ConvertFromUtf32(140), type.ToString(), barcodePart) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary wrapping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bbarnhill I think that wrapping on lines 14–17 is just what VS automatically does. No point in trying to override VS because whenever you edit near that it’ll just reperform that same whitespace change again. Line 25 is so long maybe it needs to be split up more ;-). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its probably fine in length ... just wrapped for some reason |
||
}; | ||
} | ||
|
||
public string GetBarcodeString() | ||
{ | ||
return Code128String; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Custom can be removed from this title since its really just building a Code128 string to be encoded.