示例#1
0
        /// <summary>
        /// Creates a bitmap font by loading an OS font, and drawing it to
        /// a bitmap to use as a Surface object.  You should only use this method
        /// if writing a driver.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static BitmapFontImpl ConstructFromOSFont(BitmapFontOptions options)
        {
            System.Drawing.FontStyle drawingStyle = System.Drawing.FontStyle.Regular;

            if ((options.FontStyle & FontStyle.Bold) > 0)
            {
                drawingStyle |= System.Drawing.FontStyle.Bold;
            }
            if ((options.FontStyle & FontStyle.Italic) > 0)
            {
                drawingStyle |= System.Drawing.FontStyle.Italic;
            }
            if ((options.FontStyle & FontStyle.Strikeout) > 0)
            {
                drawingStyle |= System.Drawing.FontStyle.Strikeout;
            }
            if ((options.FontStyle & FontStyle.Underline) > 0)
            {
                drawingStyle |= System.Drawing.FontStyle.Underline;
            }

            Drawing.Font   font = new Drawing.Font(options.FontFamily, options.SizeInPoints, drawingStyle);
            Drawing.Bitmap bmp;
            FontMetrics    glyphs;

            ICharacterRenderer rend = options.UseTextRenderer ?
                                      (ICharacterRenderer) new TextRend(font) :
                                      (ICharacterRenderer) new GraphicsRend(font);

            MakeBitmap(options, rend, out bmp, out glyphs);

            //bmp.Save("testfont.png", Drawing.Imaging.ImageFormat.Png);

            string tempFile = System.IO.Path.GetTempFileName() + ".png";

            bmp.Save(tempFile, Drawing.Imaging.ImageFormat.Png);
            bmp.Dispose();

            Surface surf = new Surface(tempFile);

            System.IO.File.Delete(tempFile);

            return(new BitmapFontImpl(surf, glyphs));
        }
示例#2
0
        public void Main(string[] args)
        {
            using (AgateSetup setup = new AgateSetup(args))
            {
                setup.AskUser = true;
                setup.Initialize(true, false, false);
                if (setup.WasCanceled)
                {
                    return;
                }

                DisplayWindow wind = DisplayWindow.CreateWindowed(
                    "Bitmap Font Tester", 800, 600, false);

                Display.BeginFrame();
                Display.Clear(Color.Navy);
                Display.EndFrame();
                Core.KeepAlive();

                BitmapFontOptions fontOptions = new BitmapFontOptions("Times", 18, FontStyle.Bold);
                fontOptions.UseTextRenderer = true;

                FontSurface font = new FontSurface(fontOptions);

                // TODO: Fix this
                //font.Save("testfont.xml");


                //FontSurface second = FontSurface.LoadBitmapFont("testfont.png", "testfont.xml");

                while (wind.IsClosed == false)
                {
                    Display.BeginFrame();
                    Display.Clear(Color.Navy);

                    font.DrawText("The quick brown fox jumps over the lazy dog.");

                    //second.DrawText(0, font.StringDisplayHeight("M"), "The quick brown fox jumps over the lazy dog.");

                    Display.EndFrame();
                    Core.KeepAlive();
                }
            }
        }
示例#3
0
        private BitmapFontOptions CreateBitmapFontOptions(FontSettings fontSetting)
        {
            BitmapFontOptions options = new BitmapFontOptions();

            options.BorderColor        = Parameters.BorderColor;
            options.BottomMarginAdjust = Parameters.BottomMarginAdjust;
            options.CreateBorder       = Parameters.CreateBorder;
            options.EdgeOptions        = Parameters.EdgeOptions;
            options.FontFamily         = Parameters.Family;
            options.MonospaceNumbers   = Parameters.MonospaceNumbers;
            options.NumberWidthAdjust  = Parameters.NumberWidthAdjust;
            options.TopMarginAdjust    = Parameters.TopMarginAdjust;
            options.BottomMarginAdjust = Parameters.BottomMarginAdjust;
            options.TextRenderer       = Parameters.TextRenderer;

            options.SizeInPoints = fontSetting.Size;
            options.FontStyle    = fontSetting.Style;

            return(options);
        }
示例#4
0
        public void Run(string[] args)
        {
            using (new DisplayWindowBuilder(args)
                   .BackbufferSize(800, 600)
                   .QuitOnClose()
                   .Build())
            {
                Display.BeginFrame();
                Display.Clear(Color.Navy);
                Display.EndFrame();
                AgateApp.KeepAlive();

                BitmapFontOptions fontOptions = new BitmapFontOptions("Times", 18, FontStyles.None);
                fontOptions.TextRenderer = TextRenderEngine.TextRenderer;

                FontSurface surface = new FontSurface(BitmapFontUtil.ConstructFromOSFont(fontOptions));
                Font        font    = new FontBuilder("Times")
                                      .AddFontSurface(new FontSettings(18, FontStyles.None), surface)
                                      .Build();

                // TODO: Fix this
                //font.Save("testfont.xml");

                //FontSurface second = FontSurface.LoadBitmapFont("testfont.png", "testfont.xml");

                while (AgateApp.IsAlive)
                {
                    Display.BeginFrame();
                    Display.Clear(Color.Navy);

                    font.DrawText("The quick brown fox jumped over the lazy dogs.");

                    //second.DrawText(0, font.StringDisplayHeight("M"), "The quick brown fox jumps over the lazy dog.");

                    Display.EndFrame();
                    AgateApp.KeepAlive();
                }
            }
        }
示例#5
0
        public void CreateFont()
        {
            if (string.IsNullOrEmpty(Parameters.Family))
            {
                return;
            }
            font?.Dispose();

            FontBuilder fontBuilder = new FontBuilder(Parameters.Family);

            foreach (var fontSetting in FontSettings)
            {
                BitmapFontOptions options = CreateBitmapFontOptions(fontSetting);

                var fontSurface = new FontSurface
                                      (AgateLib.Platform.WinForms.Fonts.BitmapFontUtil.ConstructFromOSFont(options));

                fontBuilder.AddFontSurface(fontSetting, fontSurface);
            }

            font = fontBuilder.Build();

            Draw();
        }
示例#6
0
 public override FontSurfaceImpl CreateFont(BitmapFontOptions bitmapOptions)
 {
     return(BitmapFontUtil.ConstructFromOSFont(bitmapOptions));
 }
示例#7
0
        public override FontSurfaceImpl CreateFont(string fontFamily, float sizeInPoints, FontStyle style)
        {
            BitmapFontOptions options = new BitmapFontOptions(fontFamily, sizeInPoints, style);

            return(BitmapFontUtil.ConstructFromOSFont(options));
        }
示例#8
0
        /// <summary>
        /// Creates a bitmap font using the options passed in.  The Display driver
        /// must be capable of this, which is indicated in Display.Caps.CanCreateBitmapFont.
        /// </summary>
        /// <param name="bitmapOptions"></param>
        public FontSurface(BitmapFontOptions bitmapOptions)
        {
            impl = Display.Impl.CreateFont(bitmapOptions);

            Display.DisposeDisplay += new Display.DisposeDisplayHandler(Dispose);
        }
示例#9
0
        private static void PostProcessFont(BitmapFontOptions options, System.Drawing.Bitmap bmp)
        {
            if (options.EdgeOptions == BitmapFontEdgeOptions.None)
            {
                return;
            }

            Drawing.Imaging.BitmapData data = bmp.LockBits(
                new Drawing.Rectangle(Drawing.Point.Empty, bmp.Size),
                Drawing.Imaging.ImageLockMode.ReadWrite, Drawing.Imaging.PixelFormat.Format32bppArgb);

            PixelFormat bitmapFormat = PixelFormat.BGRA8888;

            PixelBuffer buffer = new PixelBuffer(bitmapFormat, Interop.Convert(bmp.Size),
                                                 data.Scan0, bitmapFormat, data.Stride);

            // now convert pixels to gray scale.
            for (int j = 0; j < buffer.Height; j++)
            {
                for (int i = 0; i < buffer.Width; i++)
                {
                    Color clr = buffer.GetPixel(i, j);
                    if (clr.ToArgb() == 0)
                    {
                        continue;
                    }

                    int  alpha     = clr.A;
                    int  intensity = (int)Math.Round(0.30 * clr.R + 0.59 * clr.G + 0.11 * clr.B);
                    byte value     = (byte)intensity;

                    System.Diagnostics.Debug.Assert(0 <= intensity && intensity <= 255);
                    if (intensity < 0)
                    {
                        intensity = 0;
                    }
                    if (intensity > 255)
                    {
                        intensity = 255;
                    }


                    switch (options.EdgeOptions)
                    {
                    case BitmapFontEdgeOptions.IntensityAlphaWhite:
                        clr = Color.FromArgb(value, Color.White);
                        break;

                    case BitmapFontEdgeOptions.IntensityAlphaColor:
                        clr = Color.FromArgb(value, clr);
                        break;
                    }

                    buffer.SetPixel(i, j, clr);
                }
            }


            System.Runtime.InteropServices.Marshal.Copy(
                buffer.Data, 0, data.Scan0, buffer.Data.Length);

            bmp.UnlockBits(data);
        }
示例#10
0
        private static void MakeBitmap(BitmapFontOptions options, ICharacterRenderer rend,
                                       out Drawing.Bitmap bmp, out FontMetrics glyphs)
        {
            Size bitmapSize = new Size(256, 64);

            bmp = new System.Drawing.Bitmap(bitmapSize.Width, bitmapSize.Height);
            Drawing.Graphics g    = Drawing.Graphics.FromImage(bmp);
            Drawing.Font     font = rend.Font;

            g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            glyphs = new FontMetrics();

            const int bitmapPadding = 2;

            int  x = rend.Padding, y = 2;
            int  height   = 0;
            char lastChar = ' ';

            // first measure the required height of the image.
            foreach (BitmapFontOptions.CharacterRange range in options.CharacterRanges)
            {
                for (char i = range.StartChar; i <= range.EndChar; i++)
                {
                    Size sourceSize = rend.MeasureText(g, i.ToString());
                    if (options.CreateBorder)
                    {
                        sourceSize.Width  += 2;
                        sourceSize.Height += 2;
                    }

                    int thisWidth = sourceSize.Width + bitmapPadding;

                    x += thisWidth;

                    if (height < sourceSize.Height)
                    {
                        height = sourceSize.Height;
                    }

                    if (x > bitmapSize.Width)
                    {
                        x      = 1 + thisWidth;
                        y     += height + bitmapPadding + 1;
                        height = 0;
                    }

                    glyphs[i] = new GlyphMetrics(new Rectangle(0, 0, sourceSize.Width, sourceSize.Height));

                    lastChar = i;
                }
            }

            y += glyphs[lastChar].Height;

            if (y > bitmapSize.Height)
            {
                while (y > bitmapSize.Height)
                {
                    bitmapSize.Height *= 2;
                }

                g.Dispose();
                bmp.Dispose();

                bmp = new System.Drawing.Bitmap(bitmapSize.Width, bitmapSize.Height);
                g   = Drawing.Graphics.FromImage(bmp);
            }

            Drawing.Bitmap   borderBmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height);
            Drawing.Graphics borderG   = Drawing.Graphics.FromImage(borderBmp);

            x      = rend.Padding;
            y      = 2;
            height = 0;
            Drawing.Color borderColor = System.Drawing.Color.FromArgb(
                options.BorderColor.A, options.BorderColor.R, options.BorderColor.G, options.BorderColor.B);

            foreach (BitmapFontOptions.CharacterRange range in options.CharacterRanges)
            {
                for (char i = range.StartChar; i <= range.EndChar; i++)
                {
                    if (x + glyphs[i].Width > bitmapSize.Width)
                    {
                        x      = rend.Padding;
                        y     += height + bitmapPadding + 1;
                        height = 0;
                    }

                    if (options.CreateBorder)
                    {
                        rend.DrawText(borderG, i.ToString(), new Point(x, y + 1), borderColor);
                        rend.DrawText(borderG, i.ToString(), new Point(x + 2, y + 1), borderColor);
                        rend.DrawText(borderG, i.ToString(), new Point(x + 1, y), borderColor);
                        rend.DrawText(borderG, i.ToString(), new Point(x + 1, y + 2), borderColor);

                        rend.DrawText(g, i.ToString(), new Point(x + 1, y + 1), System.Drawing.Color.White);

                        if (font.SizeInPoints >= 14.0)
                        {
                            glyphs[i].LeftOverhang = 1;
                        }

                        glyphs[i].RightOverhang = 1;
                    }
                    else
                    {
                        rend.DrawText(g, i.ToString(), new Point(x, y), System.Drawing.Color.White);
                    }

                    glyphs[i].SourceRect = new Rectangle(
                        new Point(x, y),
                        glyphs[i].Size);

                    x += glyphs[i].Width + bitmapPadding;

                    if (height < glyphs[i].Height)
                    {
                        height = glyphs[i].Height;
                    }
                }
            }

            g.Dispose();

            // do post processing of chars.
            PostProcessFont(options, bmp);

            // place the chars on the border image
            borderG.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));

            bmp.Dispose();
            borderG.Dispose();

            bmp = borderBmp;
        }
示例#11
0
 /// <summary>
 /// Creates a BitmapFontImpl object from the specified options.
 /// </summary>
 /// <param name="bitmapOptions"></param>
 /// <returns></returns>
 public abstract FontSurfaceImpl CreateFont(BitmapFontOptions bitmapOptions);