예제 #1
0
        internal void Render(ref RShader shader, ref RVertexBuffer vertexBuffer, ref RIndexBuffer indexBuffer, string text, Vector2 location, RColor color, Matrix matrix)
        {
            Vector2 pen = location;

            pen.Y += MeasureString(text).Height;
            float x = pen.X;
            List <RVertexData2D> quads = new List <RVertexData2D>();

            foreach (char c in text)
            {
                if (c == '\r')
                {
                    continue;
                }
                if (c == '\n')
                {
                    pen.X  = x;
                    pen.Y += LineHeight;
                    continue;
                }
                if (c == ' ')
                {
                    pen.X += SpaceWidth;
                    continue;
                }
                if (c == '\t')
                {
                    pen.X += (SpaceWidth * 3);
                    continue;
                }
                RFontGlyph glyph = GetGlyphForChar(c);
                var        dest  = new Reactor.Math.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;

                vertexBuffer.SetData <RVertexData2D>(AddQuads(dest, glyph.UVBounds));
                vertexBuffer.Bind();
                vertexBuffer.BindVertexArray();
                indexBuffer.Bind();
                vertexBuffer.VertexDeclaration.Apply(shader, IntPtr.Zero);
                GL.DrawElements(PrimitiveType.Triangles, indexBuffer.IndexCount, DrawElementsType.UnsignedShort, IntPtr.Zero);
                REngine.CheckGLError();
                indexBuffer.Unbind();
                vertexBuffer.Unbind();
                vertexBuffer.UnbindVertexArray();
                pen.X += glyph.Advance;
            }
        }
예제 #2
0
        internal 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 <RFontGlyph>();
            for (var i = 0; i < glyph_count; i++)
            {
                var glyph = new RFontGlyph();
                glyph.Load(ref stream);
                Glyphs.Add(glyph);
            }
            stream.Close();
        }
예제 #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;
         }
         RFontGlyph glyph = GetGlyphForChar(c);
         r.Height = System.Math.Max(r.Height, glyph.Bounds.Height);
         r.Width += (int)glyph.Offset.X;
     }
     return(r);
 }
예제 #4
0
        internal void Generate(Face face, int size, int dpi)
        {
            face.SetCharSize(0, new Fixed26Dot6(size), 0, (uint)dpi);
            Name = face.FamilyName;
            face.LoadChar((uint)32, (LoadFlags.Render | LoadFlags.Monochrome | LoadFlags.Pedantic), LoadTarget.Normal);
            SpaceWidth = face.Glyph.Metrics.HorizontalAdvance.ToInt32();
            LineHeight = face.Height >> 6;
            Kerning    = face.HasKerning;
            Size       = size;
            Ascent     = face.Ascender >> 6;
            Descent    = face.Descender >> 6;
            Glyphs     = new List <RFontGlyph>();


            for (int i = 33; i < 126; i++)
            {
                uint charIndex = face.GetCharIndex((uint)i);
                face.LoadGlyph(charIndex, (LoadFlags.Render | LoadFlags.Color | LoadFlags.Pedantic | LoadFlags.CropBitmap), LoadTarget.Normal);
                if (face.Glyph.Bitmap.PixelMode == PixelMode.None)
                {
                    continue;
                }
                RFontGlyph glyph = new RFontGlyph();

                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();

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

            while (missed != 0)
            {
                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 < Glyphs.Count; i++)
                {
                    RFontGlyph glyph  = 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)bounds.Y, (float)bounds.Width, (float)bounds.Height);
                        glyph.UVBounds /= (float)width;
                        Glyphs[i]       = glyph;
                    }
                    else
                    {
                        missed += 1;
                        break;
                    }
                }
                width += 16;
            }
            Texture = new RTexture2D();
            Texture.LoadFromBitmap(b);
            Texture.SetTextureMagFilter(RTextureMagFilter.Linear);
            Texture.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
            Texture.SetTextureWrapMode(RTextureWrapMode.ClampToBorder, RTextureWrapMode.ClampToBorder);
            REngine.CheckGLError();
        }