Beispiel #1
0
        public void AddFont(string path)
        {
            int size = Marshal.SizeOf(typeof(InfoFont));

            using (StreamReader reader = new StreamReader(path))
            {
                byte[] buffer = new byte[size];
                reader.BaseStream.Read(buffer, 0, size);
                InfoFont item = Utility.ToStruct <InfoFont>(buffer);
                m_app.Fonts.Add(item);

                buffer = new byte[item.Size];
                reader.BaseStream.Read(buffer, 0, buffer.Length);
                m_app.FontImages.Add(buffer);

                reader.Close();
            }
            FontUpdate(this, EventArgs.Empty);
        }
Beispiel #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            string path = "";

            m_params.Strings[0] = textBox1.Text;
            m_params.Strings[1] = h.ToString();
            m_params.Strings[2] = Encoding.Default.GetString(m_app.FontImages[m_fontIndex], 0, m_app.Fonts[m_fontIndex].NameEnd + 1);
            Form form = new FontCreator(m_params);

            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                if (MessageBox.Show("Replace the generated font?".Translate(), "Tips".Translate(), MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    path = m_params.Strings[0];
                    int          count  = Marshal.SizeOf(typeof(InfoFont));
                    StreamReader reader = new StreamReader(path);

                    byte[] buffer = new byte[count];
                    reader.BaseStream.Read(buffer, 0, count);
                    InfoFont infoFont = Utility.ToStruct <InfoFont>(buffer);
                    m_app.Fonts[m_fontIndex] = infoFont;

                    buffer = new byte[infoFont.Size];
                    reader.BaseStream.Read(buffer, 0, buffer.Length);
                    m_app.FontImages[m_fontIndex] = buffer;
                    reader.Close();
                    reader.Dispose();
                    showzi();
                    m_params.Strings[1] = "edit";
                    RefreshMe();
                }
            }
            else
            {
                m_params.Strings[1] = "";
            }
        }
Beispiel #3
0
        private bool create()
        {
            Font font;
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num10;
            int num12;
            int index = 0;
            byte[] bytes = new byte[2];
            SaveFileDialog dialog = new SaveFileDialog
            {
                Filter = "zi|*.zi"
            };
            if (dialog.ShowDialog() != DialogResult.OK)
                return false;

            string fileName = dialog.FileName;
            byte[] fontBytes;
            byte[] fontNameBytes = tbFontName.Text.ToBytes();

            int fontHeight = int.Parse(cbHeight.Text);

            InfoFont fontInfo = new InfoFont();
            fontInfo.State = (byte)cbRange.SelectedIndex;
            fontInfo.DataOffset = 0;
            fontInfo.NameStart = 0;
            fontInfo.NameEnd = (ushort)(fontNameBytes.Length - 1);
            fontInfo.Height = byte.Parse(fontHeight.ToString());
            if (fontInfo.State == 0)
            {
                num12 = fontInfo.Height / 2;
                fontInfo.Width = byte.Parse(num12.ToString());
                fontInfo.Length = 0x5f;
                num10 = (fontInfo.Height * fontInfo.Width) / 8;
                fontBytes = new byte[0x5f * num10];
            }
            else
            {
                fontInfo.Width = fontInfo.Height;
                fontInfo.CodeHStart = 0xA1;
                fontInfo.CodeHEnd = 0xF7;
                fontInfo.CodeLStart = 0xA1;
                fontInfo.CodeLEnd = 0xFE;

                if (fontInfo.State == 1)
                    fontInfo.Length = 8273;

                if (fontInfo.State == 2)
                {
                    m_fontString = "";
                    num6 = 0;
                    while (num6 < textBox2.Text.Length)
                    {
                        fontStringAdd(textBox2.Text.Substring(num6, 1));
                        num6++;
                    }
                    m_fontString = m_fontString + "?";
                    if (m_fontString == "?")
                    {
                        MessageBox.Show("No available font".Translate());
                        return false;
                    }
                    textBox2.Text = m_fontString;
                    fontInfo.Length = (uint)m_fontString.Length;
                }
                num10 = (fontInfo.Height * fontInfo.Width) / 8;
                if (fontInfo.State == 2)
                    fontBytes = new byte[fontInfo.Length * (num10 + 2)];
                else
                    fontBytes = new byte[fontInfo.Length * num10];
            }

            Bitmap image = new Bitmap(fontInfo.Width, fontInfo.Height);
            Graphics graphics = Graphics.FromImage(image);
            Encoding ec = Encoding.GetEncoding(cbEncode.Text);
            label3.Text = "";
            label3.Visible = true;
            if (fontInfo.State == 1)
            {
                font = new Font(m_font.FontName, m_font.FontSize, m_font.FontStyle);
                for (int i = 0xa1; i < 0xf8; i++)
                {
                    Application.DoEvents();
                    num12 = ((i - 160) * 100) / 0x57;
                    label3.Text = "Progress:".Translate() + num12.ToString() + "%";
                    num3 = 0xa1;
                    while (num3 < 0xff)
                    {
                        if (m_closing)
                        {
                            return false;
                        }
                        Application.DoEvents();
                        bytes[0] = byte.Parse(i.ToString());
                        bytes[1] = byte.Parse(num3.ToString());
                        graphics.Clear(Color.FromArgb(0, 0, 0));
                        graphics.DrawString(ec.GetString(bytes), font, Brushes.Red, (PointF)new Point(m_font.Xpi, m_font.Ypi));
                        num4 = 0;
                        num5 = 0;
                        num6 = 0;
                        while (num6 < num10)
                        {
                            fontBytes[index] = 0;
                            num7 = 0;
                            while (num7 < 8)
                            {
                                if (image.GetPixel(num4, num5).R > 0)
                                {
                                    fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                }
                                num5++;
                                if (num5 >= image.Height)
                                {
                                    num4++;
                                    num5 = 0;
                                }
                                num7++;
                            }
                            index++;
                            num6++;
                        }
                        num3++;
                    }
                }
            }
            if (fontInfo.State == 0 || fontInfo.State == 1)
            {
                font = new Font(m_char.FontName, m_char.FontSize, m_char.FontStyle);
                for (num3 = 0x20; num3 < 0x7f; num3++)
                {
                    Application.DoEvents();
                    label3.Text = (num3 - 0x1f).ToString();
                    bytes[0] = byte.Parse(num3.ToString());
                    if (!adjustmentFont(image, m_char, ec, (byte)num3))
                    {
                        return false;
                    }
                    num4 = 0;
                    num5 = 0;
                    num6 = 0;
                    while (num6 < num10)
                    {
                        fontBytes[index] = 0;
                        num7 = 0;
                        while (num7 < 8)
                        {
                            if (image.GetPixel(num4, num5).R > 0)
                            {
                                fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                            }
                            num5++;
                            if (num5 >= image.Height)
                            {
                                num4++;
                                num5 = 0;
                            }
                            num7++;
                        }
                        index++;
                        num6++;
                    }
                }
            }

            if (fontInfo.State == 2)
            {
                for (int j = 0; j < m_fontString.Length; j++)
                {
                    Application.DoEvents();
                    label3.Text = j.ToString();
                    byte[] buffer4 = m_fontString.Substring(j, 1).ToBytes();
                    if (buffer4.Length >= 0)
                    {
                        if ((buffer4[0] > 0x1f) && (buffer4[0] < 0x7f))
                        {
                            if (!adjustmentFont(image, m_char, ec, buffer4[0]))
                                return false;

                            num4 = 0;
                            num5 = 0;
                            fontBytes[index] = 0;
                            index++;
                            fontBytes[index] = buffer4[0];
                            index++;
                            num6 = 0;
                            while (num6 < num10)
                            {
                                fontBytes[index] = 0;
                                num7 = 0;
                                while (num7 < 8)
                                {
                                    if (image.GetPixel(num4, num5).R > 0)
                                    {
                                        fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                    }
                                    num5++;
                                    if (num5 >= image.Height)
                                    {
                                        num4++;
                                        num5 = 0;
                                    }
                                    num7++;
                                }
                                index++;
                                num6++;
                            }
                        }
                        else if ((buffer4[0] > 0x7f) && (buffer4.Length == 2))
                        {
                            font = new Font(m_font.FontName, m_font.FontSize, m_font.FontStyle);
                            graphics.Clear(Color.FromArgb(0, 0, 0));
                            graphics.DrawString(ec.GetString(buffer4), font, Brushes.Red, (PointF)new Point(m_font.Xpi, m_font.Ypi));
                            num4 = 0;
                            num5 = 0;
                            fontBytes[index] = buffer4[0];
                            index++;
                            fontBytes[index] = buffer4[1];
                            index++;
                            for (num6 = 0; num6 < num10; num6++)
                            {
                                fontBytes[index] = 0;
                                for (num7 = 0; num7 < 8; num7++)
                                {
                                    if (image.GetPixel(num4, num5).R > 0)
                                    {
                                        fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                    }
                                    num5++;
                                    if (num5 >= image.Height)
                                    {
                                        num4++;
                                        num5 = 0;
                                    }
                                }
                                index++;
                            }
                        }
                    }
                }
            }

            fontInfo.Size = (uint)(fontBytes.Length + fontNameBytes.Length);

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                bytes = Utility.ToBytes(fontInfo);
                writer.BaseStream.Write(bytes, 0, bytes.Length);

                writer.BaseStream.Write(fontNameBytes, 0, fontNameBytes.Length);
                writer.BaseStream.Write(fontBytes, 0, fontBytes.Length);
                writer.Close();
            }
            m_params.Strings[0] = fileName;
            label3.Text = label3.Text + "  " + "Font Size:".Translate() + fontInfo.Size.ToString("0,000");
            MessageBox.Show("Font generate finished, size:".Translate() + fontInfo.Size.ToString("0,000"));
            return true;
        }
        private bool create()
        {
            Font font;
            int  num3;
            int  num4;
            int  num5;
            int  num6;
            int  num7;
            int  num10;
            int  num12;
            int  index = 0;

            byte[]         bytes  = new byte[2];
            SaveFileDialog dialog = new SaveFileDialog
            {
                Filter = "zi|*.zi"
            };

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return(false);
            }

            string fileName = dialog.FileName;

            byte[] fontBytes;
            byte[] fontNameBytes = tbFontName.Text.ToBytes();

            int fontHeight = int.Parse(cbHeight.Text);

            InfoFont fontInfo = new InfoFont();

            fontInfo.State      = (byte)cbRange.SelectedIndex;
            fontInfo.DataOffset = 0;
            fontInfo.NameStart  = 0;
            fontInfo.NameEnd    = (ushort)(fontNameBytes.Length - 1);
            fontInfo.Height     = byte.Parse(fontHeight.ToString());
            if (fontInfo.State == 0)
            {
                num12           = fontInfo.Height / 2;
                fontInfo.Width  = byte.Parse(num12.ToString());
                fontInfo.Length = 0x5f;
                num10           = (fontInfo.Height * fontInfo.Width) / 8;
                fontBytes       = new byte[0x5f * num10];
            }
            else
            {
                fontInfo.Width      = fontInfo.Height;
                fontInfo.CodeHStart = 0xA1;
                fontInfo.CodeHEnd   = 0xF7;
                fontInfo.CodeLStart = 0xA1;
                fontInfo.CodeLEnd   = 0xFE;

                if (fontInfo.State == 1)
                {
                    fontInfo.Length = 8273;
                }

                if (fontInfo.State == 2)
                {
                    m_fontString = "";
                    num6         = 0;
                    while (num6 < textBox2.Text.Length)
                    {
                        fontStringAdd(textBox2.Text.Substring(num6, 1));
                        num6++;
                    }
                    m_fontString = m_fontString + "?";
                    if (m_fontString == "?")
                    {
                        MessageBox.Show("No available font".Translate());
                        return(false);
                    }
                    textBox2.Text   = m_fontString;
                    fontInfo.Length = (uint)m_fontString.Length;
                }
                num10 = (fontInfo.Height * fontInfo.Width) / 8;
                if (fontInfo.State == 2)
                {
                    fontBytes = new byte[fontInfo.Length * (num10 + 2)];
                }
                else
                {
                    fontBytes = new byte[fontInfo.Length * num10];
                }
            }

            Bitmap   image    = new Bitmap(fontInfo.Width, fontInfo.Height);
            Graphics graphics = Graphics.FromImage(image);
            Encoding ec       = Encoding.GetEncoding(cbEncode.Text);

            label3.Text    = "";
            label3.Visible = true;
            if (fontInfo.State == 1)
            {
                font = new Font(m_font.FontName, m_font.FontSize, m_font.FontStyle);
                for (int i = 0xa1; i < 0xf8; i++)
                {
                    Application.DoEvents();
                    num12       = ((i - 160) * 100) / 0x57;
                    label3.Text = "Progress:".Translate() + num12.ToString() + "%";
                    num3        = 0xa1;
                    while (num3 < 0xff)
                    {
                        if (m_closing)
                        {
                            return(false);
                        }
                        Application.DoEvents();
                        bytes[0] = byte.Parse(i.ToString());
                        bytes[1] = byte.Parse(num3.ToString());
                        graphics.Clear(Color.FromArgb(0, 0, 0));
                        graphics.DrawString(ec.GetString(bytes), font, Brushes.Red, (PointF) new Point(m_font.Xpi, m_font.Ypi));
                        num4 = 0;
                        num5 = 0;
                        num6 = 0;
                        while (num6 < num10)
                        {
                            fontBytes[index] = 0;
                            num7             = 0;
                            while (num7 < 8)
                            {
                                if (image.GetPixel(num4, num5).R > 0)
                                {
                                    fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                }
                                num5++;
                                if (num5 >= image.Height)
                                {
                                    num4++;
                                    num5 = 0;
                                }
                                num7++;
                            }
                            index++;
                            num6++;
                        }
                        num3++;
                    }
                }
            }
            if (fontInfo.State == 0 || fontInfo.State == 1)
            {
                font = new Font(m_char.FontName, m_char.FontSize, m_char.FontStyle);
                for (num3 = 0x20; num3 < 0x7f; num3++)
                {
                    Application.DoEvents();
                    label3.Text = (num3 - 0x1f).ToString();
                    bytes[0]    = byte.Parse(num3.ToString());
                    if (!adjustmentFont(image, m_char, ec, (byte)num3))
                    {
                        return(false);
                    }
                    num4 = 0;
                    num5 = 0;
                    num6 = 0;
                    while (num6 < num10)
                    {
                        fontBytes[index] = 0;
                        num7             = 0;
                        while (num7 < 8)
                        {
                            if (image.GetPixel(num4, num5).R > 0)
                            {
                                fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                            }
                            num5++;
                            if (num5 >= image.Height)
                            {
                                num4++;
                                num5 = 0;
                            }
                            num7++;
                        }
                        index++;
                        num6++;
                    }
                }
            }

            if (fontInfo.State == 2)
            {
                for (int j = 0; j < m_fontString.Length; j++)
                {
                    Application.DoEvents();
                    label3.Text = j.ToString();
                    byte[] buffer4 = m_fontString.Substring(j, 1).ToBytes();
                    if (buffer4.Length >= 0)
                    {
                        if ((buffer4[0] > 0x1f) && (buffer4[0] < 0x7f))
                        {
                            if (!adjustmentFont(image, m_char, ec, buffer4[0]))
                            {
                                return(false);
                            }

                            num4             = 0;
                            num5             = 0;
                            fontBytes[index] = 0;
                            index++;
                            fontBytes[index] = buffer4[0];
                            index++;
                            num6 = 0;
                            while (num6 < num10)
                            {
                                fontBytes[index] = 0;
                                num7             = 0;
                                while (num7 < 8)
                                {
                                    if (image.GetPixel(num4, num5).R > 0)
                                    {
                                        fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                    }
                                    num5++;
                                    if (num5 >= image.Height)
                                    {
                                        num4++;
                                        num5 = 0;
                                    }
                                    num7++;
                                }
                                index++;
                                num6++;
                            }
                        }
                        else if ((buffer4[0] > 0x7f) && (buffer4.Length == 2))
                        {
                            font = new Font(m_font.FontName, m_font.FontSize, m_font.FontStyle);
                            graphics.Clear(Color.FromArgb(0, 0, 0));
                            graphics.DrawString(ec.GetString(buffer4), font, Brushes.Red, (PointF) new Point(m_font.Xpi, m_font.Ypi));
                            num4             = 0;
                            num5             = 0;
                            fontBytes[index] = buffer4[0];
                            index++;
                            fontBytes[index] = buffer4[1];
                            index++;
                            for (num6 = 0; num6 < num10; num6++)
                            {
                                fontBytes[index] = 0;
                                for (num7 = 0; num7 < 8; num7++)
                                {
                                    if (image.GetPixel(num4, num5).R > 0)
                                    {
                                        fontBytes[index] = (byte)(fontBytes[index] + (((int)1) << (7 - num7)));
                                    }
                                    num5++;
                                    if (num5 >= image.Height)
                                    {
                                        num4++;
                                        num5 = 0;
                                    }
                                }
                                index++;
                            }
                        }
                    }
                }
            }

            fontInfo.Size = (uint)(fontBytes.Length + fontNameBytes.Length);

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                bytes = Utility.ToBytes(fontInfo);
                writer.BaseStream.Write(bytes, 0, bytes.Length);

                writer.BaseStream.Write(fontNameBytes, 0, fontNameBytes.Length);
                writer.BaseStream.Write(fontBytes, 0, fontBytes.Length);
                writer.Close();
            }
            m_params.Strings[0] = fileName;
            label3.Text         = label3.Text + "  " + "Font Size:".Translate() + fontInfo.Size.ToString("0,000");
            MessageBox.Show("Font generate finished, size:".Translate() + fontInfo.Size.ToString("0,000"));
            return(true);
        }
        public bool SaveToFile(string binPath, bool compile, RichTextBox textCompile)
        {
            SaveToFileData data    = new SaveToFileData();
            StreamWriter   writer  = new StreamWriter(binPath);
            InfoApp        infoApp = new InfoApp();

            try
            {
                if (!prepareToSave(ref infoApp, compile, textCompile, data))
                {
                    textCompile.AddRichTextString(
                        string.Concat(
                            "Compile failure!".Translate(), " ",
                            Errors, " Errors,".Translate(), " ",
                            Warnings, " Warnings,".Translate()
                            ), Color.Red);
                    writer.Close();
                    writer.Dispose();
                    return(false);
                }

                infoApp.VersionMajor = HmiOptions.VersionMajor;
                infoApp.VersionMinor = HmiOptions.VersionMinor;

                infoApp.FileType = Utility.FileType(compile);

                byte[] buffer = Utility.ToBytes(infoApp);
                writer.BaseStream.Write(buffer, 0, HmiOptions.InfoAppSize);

                int idx;
                if (compile)
                {                       // Fill zeros to 4K boundary
                    buffer = new byte[0x1000 - HmiOptions.InfoAppSize];
                    for (idx = 0; idx < buffer.Length; idx++)
                    {
                        buffer[idx] = 0;
                    }
                    writer.BaseStream.Write(buffer, 0, buffer.Length);
                }

                for (idx = 0; idx < Pictures.Count; idx++)
                {
                    writer.BaseStream.Write(PictureImages[idx], 0, PictureImages[idx].Length);
                }

                for (idx = 0; idx < Fonts.Count; idx++)
                {
                    writer.BaseStream.Write(FontImages[idx], 0, FontImages[idx].Length);
                }

                if (compile)
                {                       // Compile :
                    int imgSize = (getPictureImagesSize() + getFontImagesSize()) % 0x1000;
                    if (imgSize != 0)
                    {
                        buffer = new byte[0x1000 - imgSize];
                        for (idx = 0; idx < buffer.Length; idx++)
                        {
                            buffer[idx] = 0;
                        }
                        writer.BaseStream.Write(buffer, 0, buffer.Length);
                    }
                }

                for (idx = 0; idx < data.m_infoStrings.Count; idx++)
                {
                    writer.BaseStream.Write(data.m_stringDatas[idx], 0, data.m_stringDatas[idx].Length);
                }

                if (compile)
                {
                    StreamReader reader = new StreamReader(Path.Combine(Application.StartupPath, "fwc.bin"));
                    buffer = new byte[reader.BaseStream.Length];
                    reader.BaseStream.Position = 0L;
                    reader.BaseStream.Read(buffer, 0, buffer.Length);
                    writer.BaseStream.Write(buffer, 0, buffer.Length);
                    reader.Close();
                }

                for (idx = 0; idx < data.m_infoPages.Count; idx++)
                {
                    buffer = Utility.ToBytes(data.m_infoPages[idx]);
                    writer.BaseStream.Write(buffer, 0, HmiOptions.InfoPageSize);
                }

                for (idx = 0; idx < data.m_infoObjects.Count; idx++)
                {
                    buffer = Utility.ToBytes(data.m_infoObjects[idx]);
                    writer.BaseStream.Write(buffer, 0, HmiOptions.InfoObjectSize);
                }

                uint dataStart = 0;
                for (idx = 0; idx < infoApp.PictureCount; idx++)
                {
                    InfoPicture picture = Pictures[idx];
                    picture.DataStart = dataStart;
                    buffer            = Utility.ToBytes(picture);
                    writer.BaseStream.Write(buffer, 0, HmiOptions.InfoPictureSize);
                    dataStart += (uint)PictureImages[idx].Length;
                }

                for (idx = 0; idx < infoApp.StringCount; idx++)
                {
                    buffer = Utility.ToBytes(data.m_infoStrings[idx]);
                    writer.BaseStream.Write(buffer, 0, HmiOptions.InfoStringSize);
                }

                dataStart = 0;
                for (idx = 0; idx < infoApp.FontCount; idx++)
                {
                    InfoFont font = Fonts[idx];
                    font.DataOffset = dataStart;
                    buffer          = Utility.ToBytes(font);
                    writer.BaseStream.Write(buffer, 0, HmiOptions.InfoFontSize);
                    dataStart += (uint)FontImages[idx].Length;
                }
                writer.Close();
                writer.Dispose();
                textCompile.AddRichTextString(
                    string.Concat(
                        "Compile Success!".Translate(), " ",
                        Errors, " Errors,".Translate(), " ",
                        Warnings, " Warnings, FileSize:".Translate(), infoApp.FontDataStart
                        ),
                    Color.Black);
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(false);
        }