/// <summary>Constructor</summary> /// <param name="text">String - The text to be coded</param> /// <param name="size">XSize - The size of the bar code</param> /// <param name="direction">CodeDirection - Indicates the direction to draw the bar code</param> /// <param name="code128Code">Code_128_Code_Types - Indicates which of the codes to use when rendering the bar code. /// <param name="isUCC">Set barcode to GS1 EAN/UCC</param> /// The options are A, B, or buffer.</param> public Code128(string text, XSize size, CodeDirection direction, Code128CodeType code128Code, bool isUCC) : base(text, size, direction) { if (Patterns == null) { Load(); } this.Code128Code = code128Code; this.Text = text; text = isUCC ? PrepareGS1Text(text) : text; if (Code128Code == Code128CodeType.CodeC) { // Ensure that the text is an even length //if ((text.Length % 2) == 1) throw new ArgumentOutOfRangeException("Parameter text (string) must have an even length for Code 128 - Code C"); if ((text.Length % 2) == 1) { text = "0" + text; } } BarCode = ParseTextToBarCode(text); CheckValues(); }
/// <summary> /// Get start code /// </summary> /// <param name="text"></param> /// <param name="type"></param> /// <returns></returns> private Code128CodeType GetStartCode(string text, Code128CodeType type) { if (type == Code128CodeType.Auto && GetNumberCount(text, 0) >= 2) { return(Code128CodeType.CodeC); } else if (type == Code128CodeType.Auto) { int firstValue = text[0]; if (firstValue < 32) { return(Code128CodeType.CodeA); } else { return(Code128CodeType.CodeB); } } return(type); }
/// <summary>Constructor</summary> /// <param name="text">String - The text to be coded</param> /// <param name="size">XSize - The size of the bar code</param> /// <param name="direction">CodeDirection - Indicates the direction to draw the bar code</param> /// <param name="code128Code">Code_128_Code_Types - Indicates which of the codes to use when rendering the bar code.</param> public Code128(string text, XSize size, CodeDirection direction, Code128CodeType code128Code) : this(text, size, direction, code128Code, false) { }
/// <summary> /// TODO: Validate Application Identifiers (AI) /// Parse text to raw data with start codes. Optimizes data if possible. /// </summary> /// <param name="text"></param> /// <returns></returns> private string ParseTextToBarCode(string text) { Code128CodeType startCode = GetStartCode(text, Code128Code); Code128CodeType currentCode = startCode; string values = string.Empty; values += (char)startCode; int index = 0; while (index < text.Length) { if (Code128Code == Code128CodeType.Auto && (currentCode == Code128CodeType.CodeA || currentCode == Code128CodeType.CodeB) && GetNumberCount(text, index) >= 4) { currentCode = Code128CodeType.CodeC; values += CodeC; continue; } int charNumber = text[index]; if (charNumber == FNC1) { values += FNC1; index++; continue; } if (currentCode == Code128CodeType.CodeA) { if (charNumber < 32) { values += (char)(charNumber + 64); } else if (charNumber >= 95) { currentCode = Code128CodeType.CodeB; values += CodeB; values += (char)(charNumber - 32); } else { values += (char)(charNumber - 32); } index++; } else if (currentCode == Code128CodeType.CodeB) { if (IsCharAType(charNumber)) { currentCode = Code128CodeType.CodeA; values += CodeA; values += (char)(charNumber + 64); } else { values += (char)(charNumber - 32); } index++; } else if (currentCode == Code128CodeType.CodeC) { if (GetNumberCount(text, index) >= 2) { var valuePair = GetNumberPairChar(text, index); values += valuePair.Value; index += valuePair.Key; } else { if (IsCharAType(charNumber)) { currentCode = Code128CodeType.CodeA; values += CodeA; values += (char)(charNumber + 64); } else { currentCode = Code128CodeType.CodeB; values += CodeB; values += (char)(charNumber - 32); } index++; } } if (Code128Code != Code128CodeType.Auto && currentCode != startCode) { throw new Exception($"Invalid characters in barcode for CodeType at index {index}"); } } return(values.ToString()); }