Esempio n. 1
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();
        }