From cdf971c4f90ff0e7cbd3d81d89e0096ae7b807c9 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 6 Nov 2007 13:30:46 +0000 Subject: [PATCH] Removed unneeded functions. Modified several function definitions. --- Source/OpenTK/Fonts/TextureFont.cs | 249 +++++++---------------------- 1 file changed, 59 insertions(+), 190 deletions(-) diff --git a/Source/OpenTK/Fonts/TextureFont.cs b/Source/OpenTK/Fonts/TextureFont.cs index 0e335050..39936da3 100644 --- a/Source/OpenTK/Fonts/TextureFont.cs +++ b/Source/OpenTK/Fonts/TextureFont.cs @@ -7,7 +7,7 @@ using System; using System.Collections.Generic; using System.Text; -using System.Drawing; +//using System.Drawing; using System.Drawing.Text; using OpenTK.Math; @@ -20,13 +20,10 @@ namespace OpenTK.Fonts { public class TextureFont : IFont { - //class BBox { public int X, Y, Width, Height; } + System.Drawing.Font font; + Dictionary loaded_glyphs = new Dictionary(64); - Font font; - //Dictionary loaded_glyphs = new Dictionary(36); - Dictionary loaded_glyphs = new Dictionary(64); - - Graphics gfx = Graphics.FromImage(new Bitmap(1, 1)); + System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1)); static int texture; static TexturePacker pack; static int texture_width, texture_height; @@ -38,7 +35,7 @@ namespace OpenTK.Fonts /// Constructs a new TextureFont object, using the specified System.Drawing.Font. /// /// The System.Drawing.Font to use. - public TextureFont(Font font) + public TextureFont(System.Drawing.Font font) { if (font == null) throw new ArgumentNullException("font", "Argument to TextureFont constructor cannot be null."); @@ -87,130 +84,76 @@ namespace OpenTK.Fonts /// The glyphs to prepare for rendering. public void LoadGlyphs(string glyphs) { + Box2 rect = new Box2(); foreach (char c in glyphs) { if (!loaded_glyphs.ContainsKey(c)) - LoadGlyph(c); + LoadGlyph(c, out rect); } } #endregion - #region private RectangleF LoadGlyph(char c) + #region private void LoadGlyph(char c, out Box2 rectangle) - private RectangleF LoadGlyph(char c) + /// + /// Adds a glyph to the texture packer. + /// + /// The character of the glyph. + /// A RectangleF that will hold the data for this glyph. + private void LoadGlyph(char c, out Box2 rectangle) { if (pack == null) PrepareTexturePacker(); Glyph g = new Glyph(c, font); - Rectangle rect = pack.Add(g); + System.Drawing.Rectangle rect = pack.Add(g); - using (Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) - using (Graphics gfx = Graphics.FromImage(bmp)) + using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( + rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) + using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp)) { - // Upload texture and create Display List: + // Upload texture data. GL.BindTexture(TextureTarget.Texture2d, texture); //gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; //gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + gfx.TextContrast = 0; + gfx.Clear(System.Drawing.Color.Transparent); + gfx.DrawString(g.Character.ToString(), g.Font, System.Drawing.Brushes.White, 0.0f, 0.0f); - gfx.Clear(Color.Transparent); - gfx.DrawString(g.Character.ToString(), g.Font, Brushes.White, 0.0f, 0.0f); - - BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, rect.Width, rect.Height), ImageLockMode.ReadOnly, + BitmapData bmp_data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexSubImage2D(TextureTarget.Texture2d, 0, rect.Left, rect.Top, rect.Width, rect.Height, OpenTK.OpenGL.Enums.PixelFormat.Rgba, PixelType.UnsignedByte, bmp_data.Scan0); bmp.UnlockBits(bmp_data); - RectangleF f_rect = RectangleF.FromLTRB( + rectangle = new Box2( rect.Left / (float)texture_width, rect.Top / (float)texture_height, rect.Right / (float)texture_width, rect.Bottom / (float)texture_height); - - loaded_glyphs.Add(g.Character, f_rect); - - return f_rect; - /* - byte[] data = new byte[rect.Width * rect.Height * 4]; - int i = 0; - for (int y = 0; y < rect.Height - 1; y++) - { - for (int x = 0; x < rect.Width - 1; x++) - { - data[i++] = bmp.GetPixel(x, y).R; - data[i++] = bmp.GetPixel(x, y).G; - data[i++] = bmp.GetPixel(x, y).B; - data[i++] = bmp.GetPixel(x, y).A; - } - } - - unsafe - { - fixed (byte* data_ptr = data) - GL.TexSubImage2D(GL.Enums.TextureTarget.TEXTURE_2D, 0, rect.Left, rect.Top, rect.Width, rect.Height, - GL.Enums.PixelFormat.RGBA, GL.Enums.PixelType.UNSIGNED_BYTE, (IntPtr)data_ptr); - } - - float left = rect.Left / (float)texture_width; - float bottom = rect.Bottom / (float)texture_height; - float right = rect.Right / (float)texture_width; - float top = rect.Top / (float)texture_height; - float width = rect.Right - rect.Left; - float height = rect.Bottom - rect.Top; - */ - /* - int list = GL.GenLists(1); - GL.NewList(list, GL.Enums.ListMode.COMPILE); - - GL.Enable(GL.Enums.EnableCap.BLEND); - GL.BlendFunc(GL.Enums.BlendingFactorSrc.SRC_ALPHA, GL.Enums.BlendingFactorDest.ONE_MINUS_SRC_ALPHA); - - GL.Begin(GL.Enums.BeginMode.QUADS); - - GL.TexCoord2(left, top); - //GL.Vertex2(0.375f, 0.375f); - GL.Vertex2(0.0f, 0.0f); - - GL.TexCoord2(right, top); - //GL.Vertex2(0.375f + 2 * width, 0.375f); - GL.Vertex2(width, 0.0f); - - GL.TexCoord2(right, bottom); - //GL.Vertex2(0.375f + 2 * width, 0.375f + 2 * height); - GL.Vertex2(width, height); - - GL.TexCoord2(left, bottom); - //GL.Vertex2(0.375f, 0.375f + 2 * height); - GL.Vertex2(0.0f, height); - - GL.End(); - - GL.EndList(); - - loaded_glyphs.Add(g.Character, list); - - return list; - */ + loaded_glyphs.Add(g.Character, rectangle); } } #endregion - public void Draw() - { - ILayoutProvider l = new DefaultLayoutProvider(); - l.PerformLayout("ato e -et uq39tu, /q3t.q/t.q3t q 34t34qwt .", this, - new RectangleF(9.0f, 9.0f, 300.0f, 100.0f), StringAlignment.Near, false); - } + #region public bool GlyphData(char glyph, out float width, out float height, out Box2 textureRectangle, out int texture) - #region - - public bool GlyphData(char glyph, ref RectangleF textureRectangle, out float width, out float height, out int texture) + /// + /// Returns the characteristics of a loaded glyph. + /// + /// The character corresponding to this glyph. + /// The width of this glyph. + /// The height of this glyph (line spacing). + /// The bounding box of the texture data of this glyph. + /// The handle to the texture that contains this glyph. + /// True if the glyph has been loaded, false otherwise. + /// + public bool GlyphData(char glyph, out float width, out float height, out Box2 textureRectangle, out int texture) { if (loaded_glyphs.TryGetValue(glyph, out textureRectangle)) { @@ -225,95 +168,6 @@ namespace OpenTK.Fonts #endregion - #region public RectangleF FindRectangle(char c) - - /// - /// Looks up the specified glyph and returns it's bounding System.Drawing.Rectangle in the font texture. - /// This function will add the glyph to the texture font, if it hasn't been previoulsy loaded through LoadGlyphs(). - /// - /// The character representing the glyph. - /// The System.Drawing.Rectangle that represents the glyph's bounding box in the font texture. - /// - public RectangleF FindRectangle(char c) - { - RectangleF rect; - return loaded_glyphs.TryGetValue(c, out rect) ? rect : LoadGlyph(c); - } - - #endregion - - #region public int FindTexture(char c) - - /// - /// Returns the handle to the OpenGL texture where the specified glyph is located or zero, if the glyph has not been loaded. - /// - /// The character that corresponds to the glyph. - /// A handle to the OpenGL texture, or zero. - public int FindTexture(char c) - { - if (loaded_glyphs.ContainsKey(c)) - return texture; - return 0; - } - - #endregion - - #region public void Print(char c) -#if false - /// - /// Prints a glyph. - /// - /// The character corresponding to the glyph to print. - /// - /// The print position is specified by the active Modelview matrix. - /// - /// To print pixel perfect fonts, you must setup a Projection matrix that is maps one texel to one pixel. This - /// can be achieved by calling GL.Ortho with width/height set to the actual viewport size, or alternatively, - /// by calling GL.Scale(1.0f/viewport_width, 1.0f/viewport_height, 0.0f). - /// - /// - /// To avoid filtering artifacts, avoid positioning characters on fractional pixels. - /// This is usually achieved by adding 0.5f to the glyph's position and extracting the integer component, - /// i.e. GL.Translate((int)(x_pos + 0.5f), (int)(y_pos + 0.5f), 0.0f); - /// - /// - /// - public void Print(char c) - { - GL.BindTexture(GL.Enums.TextureTarget.TEXTURE_2D, texture); - - int list; - if (loaded_glyphs.TryGetValue(c, out list)) - { - GL.CallList(list); - } - else - { - GL.CallList(LoadGlyph(c)); - } - } -#endif - #endregion - - #region public void PrintFast(char c) -#if false - /// - /// Prints a previously loaded glyph. - /// - /// The character corresponding to the glyph to print. - /// - /// You must call the LoadGlyphs function with the corresponding glyph, before using - /// PrintFast. Otherwise, this function works exactly like Print. - /// - /// - /// - public void PrintFast(char c) - { - GL.CallList(loaded_glyphs[c]); - } -#endif - #endregion - #region public float Height /// @@ -326,19 +180,34 @@ namespace OpenTK.Fonts #endregion - #region public float MeasureString(string str) + #region internal int Texture + + /// + /// Gets the handle to the texture were this font resides. + /// + internal int Texture + { + get { return TextureFont.texture; } + } + + #endregion + + #region public void MeasureString(string str, out float width, out float height) /// /// Measures the width of the specified string. /// - /// - /// - public SizeF MeasureString(string str) + /// The string to measure. + /// The measured width. + /// The measured height. + public void MeasureString(string str, out float width, out float height) { - SizeF size = gfx.MeasureString(str, font, 16384, StringFormat.GenericTypographic); + System.Drawing.SizeF size = gfx.MeasureString(str, font, 16384, System.Drawing.StringFormat.GenericTypographic); if (size.Width == 0) - size.Width = font.SizeInPoints * 0.5f; - return size; + width = font.SizeInPoints * 0.5f; + else + width = size.Width; + height = size.Height; } #endregion