示例#1
0
 public static byte[] CustomFontDelete(EPLFont name)
 {
     if (name != EPLFont.CUSTOM_ALL && name < EPLFont.CUSTOM_A)
     {
         throw new ApplicationException("Unable to delete standard fonts");
     }
     return(System.Text.Encoding.GetEncoding(437).GetBytes("EK\"" + (char)name + "\"\n"));
 }
示例#2
0
 public static byte[] TextWrite(int left, int top, ElementDrawRotation rotation, EPLFont font,
                                int horizontalMult, int verticalMult, bool isReverse, string text, PrinterSettings settings, int codepage = 437)
 {
     return(Encoding.GetEncoding(codepage).GetBytes(string.Format("A{0},{1},{2},{3},{4},{5},{6},\"{7}\"\n",
                                                                  left + settings.AlignLeft,
                                                                  top + settings.AlignTop, EPLConvert.Rotation(rotation), (char)font, horizontalMult, verticalMult,
                                                                  isReverse ? "R" : "N", text.Replace(@"\", @"\\").Replace("\"", "\\\""))));
 }
示例#3
0
        public static byte[] CustomFontStore(Font font, EPLFont name, ElementUploadRotation rotation, FontCharsetType charset)
        {
            if (name < EPLFont.CUSTOM_A)
            {
                throw new ApplicationException("Invalid font name selected for storing");
            }

            List <byte> sendBytes = new List <byte>();

            byte[] init = { (byte)10,  (byte)'N', (byte)10,  (byte)'E',  (byte)'K', (byte)'"', (byte)name, (byte)'"', (byte)10,
                            (byte)'E', (byte)'S', (byte)'"', (byte)name, (byte)'"' };
            //EK"a"
            //ES"a"
            sendBytes.AddRange(init);

            int characterCount = FontCharset.CharList[(int)charset].Length;

            sendBytes.Add((byte)characterCount); //P1 - Number of characters we're sending
            sendBytes.Add((byte)(int)rotation);  //P2 - 0 - portrait, 1 - Landscape, 2 - both
            sendBytes.Add((byte)font.Height);    //P3 - Font height

            //calculate character spacing - 5% of font height or 1 dot, whichever is more
            int spacing = Math.Max(1, (int)(font.Height * 0.05));

            Bitmap[] chr = new Bitmap[characterCount];
            int      currentChr;

            int[] fontstart = new int[characterCount];
            int[] width     = new int[characterCount];
            int   bytewidth;

            for (int i = 0; i < characterCount; i++)
            {
                //Use OEM encoding.
                currentChr = System.Text.Encoding.GetEncoding(437).GetBytes(FontCharset.CharList[(int)charset])[i];
                chr[i]     = RenderChar(font, FontCharset.CharList[(int)charset][i]);

                fontstart[i] = chr[i].Width;
                int fontend = 0;
                for (int h = 0; h < chr[i].Height; h++)
                {
                    for (int w = 0; w < chr[i].Width; w++)
                    {
                        bool pixel = chr[i].GetPixel(w, h).A == 255;
                        if (pixel && w > fontend)
                        {
                            fontend = w;
                        }
                        if (pixel && w < fontstart[i])
                        {
                            fontstart[i] = w;
                        }
                    }
                }
                if (fontend < fontstart[i])
                {
                    width[i]     = font.Height / 4; //set empty character width to be 1/4 height
                    fontstart[i] = 0;
                }
                else
                {
                    width[i] = fontend - fontstart[i] + 1 + spacing;
                }

                if (rotation == ElementUploadRotation.NO_ROTATION || rotation == ElementUploadRotation.BOTH_ROTATIONS)
                {
                    bytewidth = width[i] % 8 > 0 ? (int)width[i] / 8 + 1 : width[i] / 8;
                    sendBytes.Add((byte)currentChr); //An - Character
                    sendBytes.Add((byte)width[i]);   //Bn - number of dots in character width (EPL manual is wrong)
                    sendBytes.Add((byte)bytewidth);  //Cn - number of bytes after padding (EPL manual is wrong)

                    for (int h = 0; h < chr[i].Height; h++)
                    {
                        BitArray ba = new BitArray(8);
                        int      k  = 0;

                        for (int w = fontstart[i]; w < bytewidth * 8 + fontstart[i]; w++)
                        {
                            if (w < chr[i].Width)
                            {
                                ba[7 - k] = chr[i].GetPixel(w, h).A == 255;
                            }
                            else
                            {
                                ba[7 - k] = false;
                            }
                            k++;
                            if (k == 8)
                            {
                                byte b = convertToByte(ba);
                                sendBytes.Add(b);
                                k = 0;

/*                                for (int x = 7; x > 0; x--)
 *                              {
 *                                  if (ba[x])
 *                                      Console.Write("X");
 *                                  else
 *                                      Console.Write("_");
 *                              }
 */                         }
                        }
                        //                    Console.WriteLine();
                    }
                }
            }

            if (rotation == ElementUploadRotation.ROTATE_90_DEGREES || rotation == ElementUploadRotation.BOTH_ROTATIONS)
            {
                bytewidth = font.Height % 8 > 0 ? (int)font.Height / 8 + 1 : font.Height / 8;
                for (int i = 0; i < characterCount; i++)
                {
                    sendBytes.Add((byte)FontCharset.CharList[(int)charset][i]); //An
                    sendBytes.Add((byte)(width[i] + spacing));                  //Bn
                    sendBytes.Add((byte)(width[i] + spacing));                  //Cn

                    for (int w = fontstart[i]; w < width[i] + fontstart[i] + spacing; w++)
                    {
                        BitArray ba = new BitArray(8);
                        int      k  = 0;

                        for (int h = bytewidth * 8 - 1; h >= 0; h--)
                        {
                            if (h < chr[i].Height && w < chr[i].Width)
                            {
                                ba[7 - k] = chr[i].GetPixel(w, h).A == 255;
                            }
                            else
                            {
                                ba[7 - k] = false;
                            }
                            k++;
                            if (k == 8)
                            {
                                byte b = convertToByte(ba);
                                sendBytes.Add(b);
                                k = 0;

/*                                for (int x = 7; x > 0; x--)
 *                              {
 *                                  if (ba[x])
 *                                      Console.Write("X");
 *                                  else
 *                                      Console.Write("_");
 *                              }
 */                         }
                        }
//                        Console.WriteLine();
                    }
                }
            }
            return(sendBytes.ToArray());
        }