From 2abc0461b024868ed2a0b6938b0ba107be4424df Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sat, 29 Nov 2008 17:45:43 +0000 Subject: [PATCH] Moved Begin/End to ITextOutputProvider. Fixed text location. --- .../Graphics/Text/GL1TextOutputProvider.cs | 82 ++++++++++++++----- .../Graphics/Text/GdiPlusGlyphRasterizer .cs | 6 +- .../Graphics/Text/ITextOutputProvider.cs | 2 + Source/Utilities/Graphics/TextPrinter.cs | 46 ++--------- 4 files changed, 73 insertions(+), 63 deletions(-) diff --git a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs index d7428830..b1d4f876 100644 --- a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs @@ -25,11 +25,8 @@ // #endregion -using System; using System.Collections.Generic; -using System.Text; using System.Drawing; - using OpenTK.Math; namespace OpenTK.Graphics.Text @@ -41,6 +38,7 @@ namespace OpenTK.Graphics.Text // Triangle lists, sorted by texture. Dictionary> active_lists = new Dictionary>(); Queue> inactive_lists = new Queue>(); + float[] viewport = new float[4]; #endregion @@ -55,27 +53,12 @@ namespace OpenTK.Graphics.Text #region ITextOutputProvider Members + #region Print + public void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache) { using (TextExtents extents = rasterizer.MeasureText(block, location)) { - //GL.BindTexture(TextureTarget.Texture2D, 2); - - //GL.Begin(BeginMode.Quads); - - //GL.TexCoord2(0, 0); - //GL.Vertex2(0, 0); - //GL.TexCoord2(1, 0); - //GL.Vertex2(256, 0); - //GL.TexCoord2(1, 1); - //GL.Vertex2(256, 256); - //GL.TexCoord2(0, 1); - //GL.Vertex2(0, 256); - - //GL.End(); - - //GL.Translate(0, 256, 0); - // Build layout int current = 0; foreach (Glyph glyph in block) @@ -124,6 +107,9 @@ namespace OpenTK.Graphics.Text List list = active_lists[key]; key.Bind(); + + GL.Translate(location.X, location.Y, 0); + GL.Begin(BeginMode.Triangles); for (int i = 0; i < list.Count; i += 2) @@ -146,5 +132,61 @@ namespace OpenTK.Graphics.Text } #endregion + + #region Begin + + public void Begin() + { + if (GraphicsContext.CurrentContext == null) + throw new GraphicsContextException("No GraphicsContext is current in the calling thread."); + + GL.GetFloat(GetPName.Viewport, viewport); + + // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode, + // with size equal to the window (in pixels). + // While we could also render text in 3D mode, it would be very hard to get + // pixel-perfect precision. + GL.MatrixMode(MatrixMode.Projection); + GL.PushMatrix(); + GL.LoadIdentity(); + GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0); + + GL.MatrixMode(MatrixMode.Modelview); + GL.PushMatrix(); + GL.LoadIdentity(); + + GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit); + + //GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate); + + //GL.Enable(EnableCap.ColorMaterial); + GL.Enable(EnableCap.Texture2D); + GL.Enable(EnableCap.Blend); + //GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // For grayscale + GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcColor); // For subpixel + //GL.BlendFunc(BlendingFactorSrc.ConstantColorExt, BlendingFactorDest.OneMinusSrcColor); // For subpixel with color + + + GL.Disable(EnableCap.DepthTest); + } + + #endregion + + #region End + + public void End() + { + GL.PopAttrib(); + + GL.MatrixMode(MatrixMode.Modelview); + GL.PopMatrix(); + + GL.MatrixMode(MatrixMode.Projection); + GL.PopMatrix(); + } + + #endregion + + #endregion } } diff --git a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs index 5588ab1d..6f5878d5 100644 --- a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs +++ b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs @@ -136,7 +136,7 @@ namespace OpenTK.Graphics.Text return block_cache[block]; // If this block is not cached, we have to measure it and (potentially) place it in the cache. - TextExtents extents = MeasureTextExtents(block, location); + TextExtents extents = MeasureTextExtents(block); if ((block.Options & TextPrinterOptions.NoCache) == 0) block_cache.Add(block, extents); @@ -185,7 +185,7 @@ namespace OpenTK.Graphics.Text #region MeasureTextExtents - TextExtents MeasureTextExtents(TextBlock block, PointF location) + TextExtents MeasureTextExtents(TextBlock block) { // Todo: Parse layout options: //StringFormat format = default_string_format; @@ -193,7 +193,7 @@ namespace OpenTK.Graphics.Text TextExtents extents = text_extents_pool.Acquire(); - RectangleF rect = new RectangleF(location, block.Bounds); + RectangleF rect = new RectangleF(PointF.Empty, block.Bounds); // Work around Mono/GDI+ bug, which causes incorrect // text wraping when block.Bounds == SizeF.Empty. if (block.Bounds == SizeF.Empty) diff --git a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs index 1f007932..34a219d2 100644 --- a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs @@ -35,5 +35,7 @@ namespace OpenTK.Graphics.Text interface ITextOutputProvider { void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache); + void Begin(); + void End(); } } diff --git a/Source/Utilities/Graphics/TextPrinter.cs b/Source/Utilities/Graphics/TextPrinter.cs index 22474c81..0e67a698 100644 --- a/Source/Utilities/Graphics/TextPrinter.cs +++ b/Source/Utilities/Graphics/TextPrinter.cs @@ -34,8 +34,6 @@ namespace OpenTK.Graphics IGlyphRasterizer glyph_rasterizer; ITextOutputProvider text_output; - float[] viewport = new float[4]; - #endregion #region Constructors @@ -70,39 +68,10 @@ namespace OpenTK.Graphics /// /// Sets up OpenGL state for drawing text. /// + [Obsolete] public void Begin() { - if (GraphicsContext.CurrentContext == null) - throw new GraphicsContextException("No GraphicsContext is current in the calling thread."); - - GL.GetFloat(GetPName.Viewport, viewport); - - // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode, - // with size equal to the window (in pixels). - // While we could also render text in 3D mode, it would be very hard to get - // pixel-perfect precision. - GL.MatrixMode(MatrixMode.Projection); - GL.PushMatrix(); - GL.LoadIdentity(); - GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0); - - GL.MatrixMode(MatrixMode.Modelview); - GL.PushMatrix(); - GL.LoadIdentity(); - - GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit); - - //GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate); - - //GL.Enable(EnableCap.ColorMaterial); - GL.Enable(EnableCap.Texture2D); - GL.Enable(EnableCap.Blend); - //GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // For grayscale - GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcColor); // For subpixel - //GL.BlendFunc(BlendingFactorSrc.ConstantColorExt, BlendingFactorDest.OneMinusSrcColor); // For subpixel with color - - - GL.Disable(EnableCap.DepthTest); + text_output.Begin(); } #endregion @@ -112,15 +81,10 @@ namespace OpenTK.Graphics /// /// Restores OpenGL state. /// + [Obsolete] public void End() { - GL.PopAttrib(); - - GL.MatrixMode(MatrixMode.Modelview); - GL.PopMatrix(); - - GL.MatrixMode(MatrixMode.Projection); - GL.PopMatrix(); + text_output.End(); } #endregion @@ -145,7 +109,9 @@ namespace OpenTK.Graphics if (font == null) throw new ArgumentNullException("font"); + text_output.Begin(); text_output.Print(new TextBlock(text, font, options, layoutRectangle.Size), layoutRectangle.Location, color, glyph_rasterizer, glyph_cache); + text_output.End(); } #endregion