示例#1
0
        private void GenerateBtn_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileSelector = new SaveFileDialog();

            fileSelector.Title      = "Export Font";
            fileSelector.DefaultExt = "png";
            fileSelector.Filter     = "Image files (*.png)|*.png|All files (*.*)|*.*";

            if (fileSelector.ShowDialog() == DialogResult.OK)
            {
                List <char> list = new List <char>();

                int minChar;
                int maxChar;

                ParseInt(MinChar.Text, out minChar);
                ParseInt(MaxChar.Text, out maxChar);

                for (int idx = minChar; idx <= maxChar; ++idx)
                {
                    list.Add((char)idx);
                }

                foreach (var ch in AdditionalCharacters.Text)
                {
                    if (!list.Contains(ch))
                    {
                        list.Add(ch);
                    }
                }

                Bitmap bitmap;

                int size = int.Parse(FontSize.Text);

                SitanaFont font = Generate(list, 2048, size, out bitmap);

                string directory = Path.GetDirectoryName(fileSelector.FileName);
                string fileNoExt = Path.GetFileNameWithoutExtension(fileSelector.FileName);
                string infoFile  = fileNoExt + ".sft";

                infoFile = Path.Combine(directory, infoFile);

                font.FontSheetPath = String.Empty;// fileNoExt;

                using (Stream stream = new FileStream(infoFile, FileMode.Create))
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        font.Save(writer);
                    }
                }

                bitmap.Save(fileSelector.FileName, ImageFormat.Png);
            }
        }
示例#2
0
        public void Generate(List <char> list, int width, int cutOpacity, out SitanaFont font, out Bitmap outBitmap)
        {
            int additional = _pen != null ? (int)_pen.Width : 0;

            Bitmap image = Generate("X", _font, _pen, _brush, additional);

            int capLine  = 0;
            int baseLine = image.Height - 1;
            int height   = image.Height;

            // Remove unused space from the left.
            while ((capLine < baseLine) && (BitmapIsEmpty(image, capLine, true, 1)))
            {
                capLine++;
            }

            // Remove unused space from the right.
            while ((baseLine > capLine) && (BitmapIsEmpty(image, baseLine, false, cutOpacity)))
            {
                baseLine--;
            }

            int top;
            int left;
            int right;

            image = CropCharacter(image, 0, out left, out top, out right);

            int emptyCut = left / 2;

            image.Dispose();

            Dictionary <char, Bitmap>      bitmaps = new Dictionary <char, Bitmap>();
            Dictionary <char, SitanaGlyph> glyphs  = new Dictionary <char, SitanaGlyph>();

            int maxHeight = 0;

            foreach (var ch in list)
            {
                Bitmap      bitmap;
                SitanaGlyph glyph;

                CreateGlyph(ch, emptyCut, list, out glyph, out bitmap);

                bitmaps.Add(ch, bitmap);
                glyphs.Add(ch, glyph);

                maxHeight = Math.Max(maxHeight, bitmap.Height);
            }

            int margin     = Math.Min(16, Math.Max(4, height / 8));
            int lineHeight = maxHeight + margin;

            int posX = margin;
            int posY = margin;

            int imageHeight = margin;
            int imageWidth  = 0;

            foreach (var gl in glyphs)
            {
                SitanaGlyph glyph = gl.Value;

                if (posX + glyph.Width + margin >= width)
                {
                    posX  = margin;
                    posY += lineHeight;
                }

                imageWidth  = Math.Max(imageWidth, posX + glyph.Width + margin);
                imageHeight = posY + lineHeight;

                glyph.X = (short)posX;
                glyph.Y = (short)posY;

                posX += glyph.Width + margin;
            }

            outBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(outBitmap))
            {
                graphics.Clear(Color.Transparent);

                foreach (var gl in glyphs)
                {
                    SitanaGlyph glyph = gl.Value;
                    Bitmap      bmp   = bitmaps[gl.Key];

                    graphics.DrawImage(bmp, glyph.X, glyph.Y, new Rectangle(0, 0, glyph.Width, glyph.Height), GraphicsUnit.Pixel);
                }
            }

            font = new SitanaFont();

            font.CapLine  = (short)capLine;
            font.BaseLine = (short)baseLine;
            font.Height   = (short)height;

            foreach (var glyph in glyphs)
            {
                font.AddGlyph(glyph.Value);
            }

            foreach (var bm in bitmaps)
            {
                bm.Value.Dispose();
            }
        }