/// <summary> /// Setup constructor /// </summary> /// <param name="fontData">Font data</param> public OpenGlFont( FontData fontData ) { m_RenderState = Graphics.Factory.CreateRenderState( ); m_RenderState.DepthTest = false; m_RenderState.Blend = true; m_RenderState.SourceBlend = BlendFactor.SrcAlpha; m_RenderState.DestinationBlend = BlendFactor.OneMinusSrcAlpha; m_RenderState.FaceRenderMode = PolygonRenderMode.Fill; m_RenderState.Enable2dTextures = true; m_RenderState.Lighting = false; Setup( fontData.Font, fontData.Characters ); }
/// <summary> /// Builds this font from a System.Drawing.Font object /// </summary> /// <param name="font">Font to build from</param> /// <param name="characters">Set of characters to build the font texture from</param> /// <returns>Returns this</returns> public void Setup( Font font, FontData.CharacterSet characters ) { Bitmap img = BuildFontImage( font, characters ); img.Save( string.Format( "{0}{1}.png", font.Name, font.Size ), ImageFormat.Png ); m_FontTextureSampler = Graphics.Factory.CreateTexture2dSampler( ); m_FontTextureSampler.Texture = Graphics.Factory.CreateTexture2d( ); m_FontTextureSampler.Texture.Create( img, false ); m_FontTextureSampler.Mode = TextureMode.Modulate; m_FontTextureSampler.MinFilter = TextureFilter.NearestTexel; m_FontTextureSampler.MagFilter = TextureFilter.NearestTexel; }
/// <summary> /// Builds an image from this font /// </summary> private Bitmap BuildFontImage( Font font, FontData.CharacterSet characterSet ) { string chars = new string( characterSet.Chars ); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage( new Bitmap( 1, 1 ) ); Size charSetSize = MeasureString( graphics, chars, font ); graphics.Dispose( ); m_MaxHeight = charSetSize.Height; // HACK: Add a fair bit of padding to width and height when calculating required area int area = ( charSetSize.Width + 1 ) * ( charSetSize.Height + 1 ); int size = 128; for ( ; ( size * size ) < area; size *= 2 ) { } // Set up new image and graphics object to render to it Bitmap img = new Bitmap( size, size, PixelFormat.Format32bppArgb ); graphics = System.Drawing.Graphics.FromImage( img ); graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; StringFormat format = new StringFormat( StringFormatFlags.FitBlackBox ); format.Alignment = StringAlignment.Near; int x = 0; int y = 0; for ( int charIndex = 0; charIndex < chars.Length; ++charIndex ) { string charStr = chars.Substring( charIndex, 1 ); SizeF charSize = graphics.MeasureString( charStr, font ); int charWidth = ( int )charSize.Width; if ( ( x + charWidth ) >= size ) { x = 0; y += ( charSetSize.Height ); } if ( chars[ charIndex ] < 256 ) { int xPadding = ( int )( font.Size / 3 ); int yPadding = 0; m_CharacterData[ chars[ charIndex ] ] = new CharacterData ( checked( ( short )( x + xPadding ) ), checked( ( short )( y + yPadding ) ), checked( ( byte )( charSize.Width - xPadding * 2 ) ), checked( ( byte )( charSize.Height - yPadding * 2 ) ) ); } else { throw new ApplicationException( "not handling non-ascii characters yet, sorry" ); } graphics.DrawString( charStr, font, Brushes.White, x, y, format ); x += charWidth; } graphics.Dispose( ); FillBmpAlpha( img ); return img; }