using System; using System.Collections.Generic; using System.Text; namespace Dfs.WayneChina.HyperPrinterHandler { internal class ReceiptLineItem { /// /// items included in one receipt line /// public List ColumnItems = new List(); /// /// text for total line that will print on receipt /// public string[] LineText { get; } /// /// Reserve several bytes at the beginning of each line, default value is zero /// private int prefixLen = 0; /// /// Reserve several bytes at the end of each line /// private int endLen = 0; /// /// maximun length for each line, default value is 37 /// private int totalLength = 37; private char space = ' '; /// /// /// /// Reserve several bytes at the beginning of each line, default value is zero /// Reserve several bytes at the end of each line , default value is zero /// maximun length for each line, default value is 37 public ReceiptLineItem(int prefixLength, int remainingLength, int totalLength) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); prefixLen = prefixLength; endLen = remainingLength; this.totalLength = totalLength; } /// /// Returns a list of HEX string, format for this string is as it on real receipt /// /// a list of HEX string public List Format() { List tempTexts = new List(); foreach (var ci in ColumnItems) { int lengthForEachColumn = 0; if (ci.colspan > 0) { lengthForEachColumn = (int)((float)ci.colspan / GetTotalColSpanForEachLine() * (totalLength - prefixLen - endLen)); } else if (ci.width > 0) { lengthForEachColumn = (int)((float)ci.width / 100 * (totalLength - prefixLen - endLen)); } byte[] b = Encoding.GetEncoding("GB2312").GetBytes(ci.Text); string tempCiText; //if the length of current column is longer than the maximun length for each column, then need to split it to serval //lines if (b.Length > lengthForEachColumn) { int tempindex = 0; int bytesToFetch = lengthForEachColumn % 2 > 0 ? lengthForEachColumn - 1 : lengthForEachColumn; int splitLines = b.Length % bytesToFetch > 0 ? b.Length / bytesToFetch + 1 : b.Length / bytesToFetch; for (int i = 0; i < splitLines; i++) { int bytesFetched = 0; List tempb = new List(); if (i + 1 != splitLines)//not the last line { while (bytesFetched < bytesToFetch) { tempb.Add(b[tempindex++]); bytesFetched++; } tempCiText = FormatColumnItem(Encoding.GetEncoding("GB2312").GetString(tempb.ToArray()), ci.align, lengthForEachColumn, ColumnItems.IndexOf(ci)); if (tempTexts.Count < i + 1) { tempTexts.Add(ToHexString(Encoding.GetEncoding("GB2312").GetBytes("".PadRight(lengthForEachColumn * ColumnItems.IndexOf(ci), space)))); tempTexts[i] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText)); //tempTexts.Add("".PadRight(lengthForEachColumn * ColumnItems.IndexOf(ci), space)); //tempTexts[i] += tempCiText; } else { tempTexts[i] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText)); //tempTexts[i] += tempCiText; } } else//need to add space to make sure the legth equals to lengthForEachColumn { bytesToFetch = b.Length - bytesToFetch * i; while (bytesFetched < bytesToFetch) { tempb.Add(b[tempindex++]); bytesFetched++; } tempCiText = FormatColumnItem(Encoding.GetEncoding("GB2312").GetString(tempb.ToArray()), ci.align, lengthForEachColumn, ColumnItems.IndexOf(ci)); if (tempTexts.Count < i + 1) { tempTexts.Add(ToHexString(Encoding.GetEncoding("GB2312").GetBytes("".PadRight(lengthForEachColumn * ColumnItems.IndexOf(ci), space)))); tempTexts[i] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText)); //tempTexts.Add("".PadRight(lengthForEachColumn * ColumnItems.IndexOf(ci), space)); //tempTexts[i] += tempCiText; } else { tempTexts[i] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText)); //tempTexts[i] += tempCiText; } } } } else { tempCiText = FormatColumnItem(ci.Text, ci.align, lengthForEachColumn, ColumnItems.IndexOf(ci)); if (tempTexts.Count > 0) tempTexts[0] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText)); //tempTexts[0] += tempCiText; else tempTexts.Add(ToHexString(Encoding.GetEncoding("GB2312").GetBytes(tempCiText))); //tempTexts.Add(tempCiText); //if there're serval lines exits, need to add space to rest columns for (int i = 1; i < tempTexts.Count; i++) tempTexts[i] += ToHexString(Encoding.GetEncoding("GB2312").GetBytes("".PadRight(lengthForEachColumn, space))); //tempTexts[i] += "".PadRight(lengthForEachColumn, space); } } return tempTexts; } /// /// /// /// data will be formatted /// agliment, left, right or center /// total length of result string /// private string FormatColumnItem(string rawData, string agline, int targetLength, int index) { string temp = index == 0 ? "".PadRight(prefixLen, space) : string.Empty; if (rawData.Contains("---")) { temp += rawData.PadRight(targetLength, '-'); return temp; } int spaceLen = targetLength - Encoding.GetEncoding("GB2312").GetBytes(rawData).Length; if (agline.Equals("left", StringComparison.CurrentCultureIgnoreCase)) { temp += rawData.PadRight(rawData.Length + spaceLen, space); } else if (agline.Equals("right", StringComparison.CurrentCultureIgnoreCase)) { temp += rawData.PadLeft(rawData.Length + spaceLen, space); } else { temp += rawData.PadLeft(spaceLen / 2 + rawData.Length, space).PadRight(rawData.Length + spaceLen, space); } return temp; } /// /// return total count of column spans in each line /// /// private int GetTotalColSpanForEachLine() { var totalColSpan = 0; foreach (var ci in ColumnItems) { totalColSpan += ci.colspan > 0 ? ci.colspan : 0; } return totalColSpan; } private string ToHexString(byte[] sourceBytes) { string tempHexString = ""; foreach (var b in sourceBytes) { tempHexString += System.Convert.ToString(b, 16); } return tempHexString; } } }