Пример #1
0
        public static IZiFont FromFile(string fileName)
        {
            var version = 0;

            using (var fs = File.OpenRead(fileName)) {
                fs.Seek(FILE_VERSION_OFFSET, SeekOrigin.Begin);
                version = fs.ReadByte();
                fs.Close();
            }

            switch (version)
            {
            case 3:
                return(ZiFontV3.FromFile(fileName));

            case 5:
            case 6:
                return(ZiFontV5.FromFile(fileName));

            default:
                return(null);
            }
        }
        private void CreatePreview()
        {
            var codePage = new CodePage(ZiLib.CodePageIdentifier.ISO_8859_1);

            var fontName = lstFonts.SelectedItem?.ToString() ?? "";
            var fontSize = (int)numFontSize.Value;

            var size   = int.Parse(cmbNextionFontSize.Text);
            var width  = size / 2;
            var height = size;

            var font = new Font(fontName, fontSize, GraphicsUnit.Pixel);

            var pPreviews = new List <Bitmap>();

            var bLightCoral = new SolidBrush(Color.LightCoral);
            var bLightGreen = new SolidBrush(Color.LightGreen);
            var bWhite      = new SolidBrush(Color.White);
            var bBlack      = new SolidBrush(Color.Black);

            foreach (var cp in codePage.CodePoints)
            {
                var bPreview = new Bitmap(width, height);
                var txt      = codePage.Encoding.GetString(BitConverter.GetBytes(cp));

                using (var gPreview = CreateGraphics(bPreview)) {
                    var sChar = gPreview.MeasureString(txt, font, new PointF(0, 0), StringFormat.GenericTypographic).ToSize();
                    if (sChar.Width == 0)
                    {
                        sChar.Width = 1;
                    }

                    var bChar = new Bitmap(sChar.Width, sChar.Height);

                    using (var gChar = CreateGraphics(bChar)) {
                        var sb = PreviewTest.Checked ?
                                 bChar.Width > bPreview.Width ? bLightCoral : bLightGreen :
                                 PreviewBW.Checked ? bWhite : bBlack;

                        gChar.FillRectangle(sb, 0, 0, sChar.Width, sChar.Height);

                        gChar.DrawString(txt, font,
                                         PreviewWB.Checked ? bWhite : bBlack,
                                         (float)numCharOffsetX.Value, (float)numCharOffsetY.Value, StringFormat.GenericTypographic
                                         );
                    }

                    gPreview.FillRectangle(PreviewTest.Checked ?
                                           bChar.Width > bPreview.Width ? bLightCoral : bLightGreen :
                                           PreviewBW.Checked ? bWhite : bBlack,
                                           0, 0, width, height);

                    if (bChar.Width > bPreview.Width)
                    {
                        gPreview.DrawImage(bChar, 0, 0, width, height);
                    }
                    else
                    {
                        gPreview.DrawImage(bChar, (width / 2) - (bChar.Width / 2), 0, bChar.Width, height);
                    }
                }

                pPreviews.Add(bPreview);
            }

            panelPreview.SuspendLayout();
            panelPreview.Controls.Clear();
            panelPreview.BackColor = PreviewTest.Checked ? Color.Transparent : PreviewBW.Checked ? Color.White : Color.Black;
            panelPreview.Controls.AddRange(
                pPreviews.Select(x => new PictureBox()
            {
                Width  = width,
                Height = height,
                Image  = x,
                Margin = new Padding(3)
            }).ToArray());
            panelPreview.ResumeLayout();

            ziFont = ZiFontV3.FromCharacterBitmaps(fontName + " " + cmbNextionFontSize.Text, (byte)width, (byte)height, codePage, pPreviews, PreviewWB.Checked);
        }