Ejemplo n.º 1
0
        public void Render(string text, IntPtr handle)
        {
            Graphics g = Graphics.FromHwnd(handle);

            g.Clear(Color.Black);
            Vector2 pen = new Vector2();

            pen.Y += MeasureString(text).Height;
            foreach (char c in text)
            {
                if (c == ' ')
                {
                    pen.X += SpaceWidth;
                    continue;
                }
                if (c == '\t')
                {
                    pen.X += (SpaceWidth * 3);
                    continue;
                }
                FontGlyph glyph = GetGlyphForChar(c);
                System.Drawing.Rectangle dest = new System.Drawing.Rectangle();
                dest.X      = (int)(pen.X + glyph.Offset.X);
                dest.Y      = (int)pen.Y - ((int)glyph.Offset.Y);
                dest.Width  = glyph.Bounds.Width;
                dest.Height = glyph.Bounds.Height;
                g.DrawImage(Bitmap, dest, glyph.Bounds, GraphicsUnit.Pixel);
                pen.X += glyph.Advance;
            }
        }
Ejemplo n.º 2
0
        public void Load(ref BinaryReader stream)
        {
            Size       = stream.ReadInt32();
            Name       = stream.ReadString();
            Kerning    = stream.ReadBoolean();
            LineHeight = stream.ReadInt32();
            SpaceWidth = stream.ReadInt32();
            Ascent     = stream.ReadInt32();
            Descent    = stream.ReadInt32();
            int glyph_count = stream.ReadInt32();

            Glyphs = new List <FontGlyph>();
            for (var i = 0; i < glyph_count; i++)
            {
                var glyph = new FontGlyph();
                glyph.Load(ref stream);
                Glyphs.Add(glyph);
            }
            stream.Close();
        }
Ejemplo n.º 3
0
 public Reactor.Math.Rectangle MeasureString(string text)
 {
     Reactor.Math.Rectangle r = new Reactor.Math.Rectangle();
     foreach (char c in text)
     {
         if (c == ' ')
         {
             r.Width += SpaceWidth;
             continue;
         }
         if (c == '\t')
         {
             r.Width += (SpaceWidth * 3);
             continue;
         }
         FontGlyph glyph = GetGlyphForChar(c);
         r.Height = Math.Max(r.Height, glyph.Bounds.Height);
         r.Width += (int)glyph.Offset.X;
     }
     return(r);
 }
Ejemplo n.º 4
0
        public Font Build(string filename, int size, int dpi)
        {
            Face face = new Face(FreeTypeLibrary, filename);

            face.SetCharSize(0, new Fixed26Dot6(size), 0, (uint)dpi);
            Font font = new Font();

            font.Name = face.FamilyName;
            face.LoadChar((uint)32, (LoadFlags.Render | LoadFlags.Monochrome | LoadFlags.Pedantic), LoadTarget.Normal);
            font.SpaceWidth = face.Glyph.Metrics.HorizontalAdvance.ToInt32();
            font.LineHeight = face.Height;
            font.Kerning    = face.HasKerning;
            font.Size       = size;
            font.Ascent     = face.Ascender >> 6;
            font.Descent    = face.Descender >> 6;
            font.Glyphs     = new List <FontGlyph>();


            for (int i = 33; i < 126; i++)
            {
                uint charIndex = face.GetCharIndex((uint)i);
                face.LoadGlyph(charIndex, (LoadFlags.Render | LoadFlags.Color | LoadFlags.Pedantic), LoadTarget.Normal);
                if (face.Glyph.Bitmap.Width == 0)
                {
                    continue;
                }
                FontGlyph glyph = new FontGlyph();
                glyph.bitmap    = face.Glyph.Bitmap.ToGdipBitmap(Color.White);
                glyph.Bounds    = new Reactor.Math.Rectangle(0, 0, glyph.bitmap.Width, glyph.bitmap.Height);
                glyph.CharIndex = i;
                glyph.Offset    = new Vector2(face.Glyph.Metrics.HorizontalBearingX.ToInt32(), face.Glyph.Metrics.HorizontalBearingY.ToInt32());
                glyph.Advance   = face.Glyph.Advance.X.ToInt32();

                font.Glyphs.Add(glyph);
            }
            font.Glyphs.Sort(new FontGlyphSizeSorter());
            font.Glyphs.Reverse();
            var    missed = -1;
            var    width  = 16;
            Bitmap b      = new Bitmap(1, 1);

            while (missed != 0)
            {
                Application.DoEvents();
                missed = 0;
                AtlasNode root = new AtlasNode();
                root.bounds = new Reactor.Math.Rectangle(0, 0, width, width);
                b.Dispose();
                b = new Bitmap(width, width);
                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.Transparent);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                for (var i = 0; i < font.Glyphs.Count; i++)
                {
                    FontGlyph glyph  = font.Glyphs[i];
                    AtlasNode result = root.Insert(glyph.Bounds);

                    if (result != null)
                    {
                        Reactor.Math.Rectangle bounds = result.bounds;
                        //g.DrawImageUnscaledAndClipped(glyph.bitmap, bounds);
                        g.DrawImage(glyph.bitmap, bounds);
                        glyph.Bounds   = bounds;
                        glyph.UVBounds = new Vector4((float)bounds.X / (float)width, (float)bounds.Y / (float)width, (float)bounds.Width / (float)width, (float)bounds.Height / (float)width);
                        font.Glyphs[i] = glyph;
                    }
                    else
                    {
                        missed += 1;
                        break;
                    }
                }
                width += 1;
            }

            if (missed > 0)
            {
                MessageBox.Show("Oops, looks like there wasn't enough room!\r\nMissed: " + missed, "Missed Glyphs", MessageBoxButtons.OK);
            }

            font.Bitmap = b;
            return(font);
        }