123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Dfs.WayneChina.HyperPrinterHandler
- {
- internal class ReceiptLineItem
- {
- /// <summary>
- /// items included in one receipt line
- /// </summary>
- public List<ReceiptColumnItem> ColumnItems = new List<ReceiptColumnItem>();
- /// <summary>
- /// text for total line that will print on receipt
- /// </summary>
- public string[] LineText { get; }
- /// <summary>
- /// Reserve several bytes at the beginning of each line, default value is zero
- /// </summary>
- private int prefixLen = 0;
- /// <summary>
- /// Reserve several bytes at the end of each line
- /// </summary>
- private int endLen = 0;
- /// <summary>
- /// maximun length for each line, default value is 37
- /// </summary>
- private int totalLength = 37;
- private char space = ' ';
- /// <summary>
- ///
- /// </summary>
- /// <param name="prefixLength">Reserve several bytes at the beginning of each line, default value is zero</param>
- /// <param name="remainingLength">Reserve several bytes at the end of each line , default value is zero</param>
- /// <param name="totalLength">maximun length for each line, default value is 37</param>
- public ReceiptLineItem(int prefixLength, int remainingLength, int totalLength)
- {
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- prefixLen = prefixLength;
- endLen = remainingLength;
- this.totalLength = totalLength;
- }
- /// <summary>
- /// Returns a list of HEX string, format for this string is as it on real receipt
- /// </summary>
- /// <returns>a list of HEX string</returns>
- public List<string> Format()
- {
- List<string> tempTexts = new List<string>();
- 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<byte> tempb = new List<byte>();
- 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;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="rawData">data will be formatted</param>
- /// <param name="agline">agliment, left, right or center</param>
- /// <param name="targetLength">total length of result string</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// return total count of column spans in each line
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
- }
|