Exemplo n.º 1
0
        private static int Main(string[] args)
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            // Test if input arguments were supplied.
            int options = Options(args);

            switch (options)
            {
            case 1: HelpGeneral(); return(options);

            case 2: HelpFontPattern(); return(options);

            case 3: HelpCodePage(); return(options);

            default: break;
            }

            byte[] escposdata = ReadFile(inputpath);

            if ((escposdata is null) || (escposdata.Length == 0))
            {
                Console.Error.WriteLine("No input data.");
                return(11);
            }

            var EPToken = new EscPosTokenizer();
            List <EscPosCmd> escposlist = EPToken.Scan(escposdata, deviceType, sbcsfontpattern, mbcsfontpattern, vfdfontpattern);

            if ((escposlist is null) || (escposlist.Count == 0))
            {
                Console.Error.WriteLine("No tokenized data.");
                return(12);
            }

            if (decode)
            {
                escposlist = EscPosDecoder.Convert(escposlist);
                escposlist = Convertstrings(escposlist);
            }

            string  result        = string.Empty;
            Boolean graphicsexist = false;

            foreach (EscPosCmd item in escposlist)
            {
                if (string.IsNullOrEmpty(item.paramdetail))
                {
                    result += string.Format(CultureInfo.InvariantCulture, "<! {0}\n  | {1} !>\n", item.cmdtype.ToString(), BitConverter.ToString(item.cmddata));
                }
                else
                {
                    result += string.Format(CultureInfo.InvariantCulture, "<! {0}\n  | {1}\n  # {2} !>\n", item.cmdtype.ToString(), BitConverter.ToString(item.cmddata), item.paramdetail);
                }
                if (item.somebinary != null)
                {
                    graphicsexist = true;
                }
            }

            if (stdout)
            {
                Console.Write(result);
            }
            else
            {
                WritedFile(outputpath, result);
            }
            if (graphicsoutput && graphicsexist)
            {
                try
                {
                    if (!Directory.Exists(graphicspath))
                    {
                        Directory.CreateDirectory(graphicspath);
                    }
                    Outputgraphics(escposlist);
                }
                catch { }
            }
            return(0);
        }
Exemplo n.º 2
0
        private static List <EscPosCmd> Convertstrings(List <EscPosCmd> escposlist)
        {
            Boolean  prtutf8     = false;
            Encoding prtencoding = Encoding.Default;
            Dictionary <byte, string>   prtembedded = new Dictionary <byte, string>();
            Dictionary <string, string> prtreplaced = new Dictionary <string, string>();
            UTF8Encoding utf8encoding = new UTF8Encoding();;
            Encoding     vfdencoding  = Encoding.Default;
            Dictionary <byte, string>   vfdembedded = new Dictionary <byte, string>();
            Dictionary <string, string> vfdreplaced = new Dictionary <string, string>();

            try
            {
                if (initialcodepage < 0)
                {
                    initialcodepage = CultureInfo.CurrentCulture.TextInfo.OEMCodePage;
                    initialkanjion  = !Encoding.GetEncoding(initialcodepage).IsSingleByte;
                }
            }
            catch
            {
                return(escposlist);
            }
            int     prtcodepage  = initialcodepage;
            int     prtICS       = initialICS;
            int     vfdcodepage  = initialcodepage;
            int     vfdICS       = initialICS;
            Boolean prtkanjimode = initialkanjion;
            Boolean vfdkanjimode = initialkanjion;

            try
            {
                if (prtcodepage < 0x100)
                {
                    prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage);
                }
                else
                {
                    prtencoding = Encoding.GetEncoding(prtcodepage);
                }
                if (prtICS != 0)
                {
                    prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS];
                }
                if (vfdcodepage < 0x100)
                {
                    vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage);
                }
                else
                {
                    prtencoding = Encoding.GetEncoding(vfdcodepage);
                }
                if ((vfdICS >= 1) && (vfdICS <= 17))
                {
                    vfdreplaced = EscPosDecoder.s_StringESCRICS[(byte)vfdICS];
                }
                else
                {
                    vfdICS = 0;
                }
            }
            catch
            {
                return(escposlist);
            }

            foreach (EscPosCmd item in escposlist)
            {
                switch (item.cmdtype)
                {
                case EscPosCmdType.EscInitialize:
                    prtcodepage  = initialcodepage;
                    prtICS       = initialICS;
                    prtkanjimode = initialkanjion;
                    try
                    {
                        if (prtcodepage < 0x100)
                        {
                            prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage);
                        }
                        else
                        {
                            prtencoding = Encoding.GetEncoding(prtcodepage);
                        }
                        if (prtICS != 0)
                        {
                            prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS];
                        }
                    }
                    catch { }
                    break;

                case EscPosCmdType.EscSelectInternationalCharacterSet:
                    try
                    {
                        if (EscPosDecoder.s_StringESCRICS.ContainsKey(item.cmddata[2]))
                        {
                            prtICS = item.cmddata[2];
                            if (prtICS != 0)
                            {
                                prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS];
                            }
                        }
                    }
                    catch { }
                    break;

                case EscPosCmdType.EscSelectCharacterCodeTable:
                    try
                    {
                        if (EscPosDecoder.PrtESCtCodePage.TryGetValue(item.cmddata[2], out prtcodepage))
                        {
                            if (prtcodepage < 0x100)
                            {
                                prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage);
                            }
                            else
                            {
                                prtencoding = Encoding.GetEncoding(prtcodepage);
                            }
                        }
                    }
                    catch { }
                    break;

                case EscPosCmdType.FsSelectKanjiCharacterMode:
                    prtkanjimode = true;
                    break;

                case EscPosCmdType.FsCancelKanjiCharacterMode:
                    prtkanjimode = false;
                    break;

                case EscPosCmdType.FsSelectCharacterEncodeSystem:
                {
                    byte m = item.cmddata[6];
                    prtutf8 = ((m == 2) || (m == 50));
                }
                break;

                case EscPosCmdType.PrtPrintables:
                    try
                    {
                        List <string> listwork = new List <string>();
                        if (prtutf8)
                        {
                            listwork.AddRange(utf8encoding.GetChars(item.cmddata).Select(c => c.ToString()));
                        }
                        else if (prtcodepage < 0x100)
                        {
                            listwork.AddRange(item.cmddata.Select(c => prtembedded[c]));
                        }
                        else
                        {
                            listwork.AddRange(prtencoding.GetChars(item.cmddata).Select(c => c.ToString()));
                        }
                        item.paramdetail = string.Join("", (prtICS == 0 ? listwork : listwork.Select(s => (prtreplaced.ContainsKey(s) ? prtreplaced[s] : s))));
                    }
                    catch { }
                    break;

                case EscPosCmdType.VfdEscInitialize:
                    vfdcodepage  = initialcodepage;
                    vfdICS       = initialICS;
                    vfdkanjimode = initialkanjion;
                    try
                    {
                        if (vfdcodepage < 0x100)
                        {
                            vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage);
                        }
                        else
                        {
                            prtencoding = Encoding.GetEncoding(vfdcodepage);
                        }
                    }
                    catch { }
                    break;

                case EscPosCmdType.VfdEscSelectInternationalCharacterSet:
                    if (EscPosDecoder.s_StringESCRICS.ContainsKey(item.cmddata[2]))
                    {
                        vfdICS = item.cmddata[2];
                        if ((vfdICS >= 1) && (vfdICS <= 17))
                        {
                            vfdreplaced = EscPosDecoder.s_StringESCRICS[(byte)vfdICS];
                        }
                        else
                        {
                            vfdICS = 0;
                        }
                    }
                    break;

                case EscPosCmdType.VfdEscSelectCharacterCodeTable:
                    try
                    {
                        if (EscPosDecoder.VfdESCtCodePage.TryGetValue(item.cmddata[2], out vfdcodepage))
                        {
                            if (vfdcodepage < 0x100)
                            {
                                vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage);
                            }
                            else
                            {
                                prtencoding = Encoding.GetEncoding(vfdcodepage);
                            }
                        }
                    }
                    catch { }
                    break;

                case EscPosCmdType.VfdUsKanjiCharacterModeOnOff:
                    vfdkanjimode = item.cmddata[6] switch
                    {
                        0 => false,
                        48 => false,
                        1 => true,
                        49 => true,
                        _ => vfdkanjimode,
                    };
                    break;

                case EscPosCmdType.VfdDisplayables:
                    try
                    {
                        List <string> listwork = new List <string>();
                        if (vfdcodepage < 0x100)
                        {
                            listwork.AddRange(item.cmddata.Select(c => vfdembedded[c]));
                        }
                        else
                        {
                            listwork.AddRange(vfdencoding.GetChars(item.cmddata).Select(c => c.ToString()));
                        }
                        item.paramdetail = string.Join("", (vfdICS == 0 ? listwork : listwork.Select(s => (vfdreplaced.ContainsKey(s) ? vfdreplaced[s] : s))));
                    }
                    catch { }
                    break;
                }
                ;
            }
            return(escposlist);
        }