diff --git a/Source/Examples/OpenGL/FrameBufferObject.cs b/Source/Examples/OpenGL/FrameBufferObject.cs index b87e5a7b..c8c38e0e 100644 --- a/Source/Examples/OpenGL/FrameBufferObject.cs +++ b/Source/Examples/OpenGL/FrameBufferObject.cs @@ -237,7 +237,7 @@ namespace Examples.Tutorial GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); text.Begin(); - text.Print((1.0 / e.Time).ToString("F2"), sans); + text.Print((1.0 / e.Time).ToString("F2"), sans, Color.White); text.End(); GL.PushMatrix(); diff --git a/Source/Examples/OpenGL/JuliaSetFractal.cs b/Source/Examples/OpenGL/JuliaSetFractal.cs index 08442e24..df5658a0 100644 --- a/Source/Examples/OpenGL/JuliaSetFractal.cs +++ b/Source/Examples/OpenGL/JuliaSetFractal.cs @@ -289,8 +289,7 @@ namespace Examples.Tutorial // Then, render the fps: GL.UseProgram(0); printer.Begin(); - GL.Color3(Color.PaleGoldenrod); - printer.Print((1 / e.Time).ToString("F2"), font, TextPrinterOptions.NoCache); + printer.Print((1 / e.Time).ToString("F2"), font, Color.PaleGoldenrod, RectangleF .Empty, TextPrinterOptions.NoCache); printer.End(); SwapBuffers(); diff --git a/Source/Examples/Tests/GameWindowStates.cs b/Source/Examples/Tests/GameWindowStates.cs index 4ef3761c..862989c3 100644 --- a/Source/Examples/Tests/GameWindowStates.cs +++ b/Source/Examples/Tests/GameWindowStates.cs @@ -118,13 +118,12 @@ namespace Examples.Tests printer.Begin(); - printer.Print("Instructions:", font); GL.Translate(0, font.Height, 0); - printer.Print(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font); - GL.Translate(0, font.Height, 0); - printer.Print(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font); + printer.Print("Instructions:", font, Color.White); + printer.Print(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font, Color.White, new RectangleF(0, font.Height, 0, 0)); + printer.Print(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font, Color.White, new RectangleF(0, 2 * font.Height, 0, 0)); GL.Translate(0, font.Height, 0); printer.Print(String.Format("3 - toggle fullscreen (current: {0}).", - this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font); + this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font, Color.White, new RectangleF(0, 2 * font.Height, 0, 0)); printer.End(); diff --git a/Source/Examples/Tutorial/Fonts.cs b/Source/Examples/Tutorial/Fonts.cs index 3973c3fe..e675012d 100644 --- a/Source/Examples/Tutorial/Fonts.cs +++ b/Source/Examples/Tutorial/Fonts.cs @@ -162,7 +162,7 @@ namespace Examples.Tutorial // Print using the first font. for (int i = 0; i < fonts.Length / 2; i++) { - printer.Print(text, fonts[i]); + printer.Print(text, fonts[i], Color.White); GL.Translate(0, fonts[i].Height, 0); } @@ -174,7 +174,7 @@ namespace Examples.Tutorial GL.Translate(rect.Width + 32.0f, 0, 0); for (int i = fonts.Length / 2; i < fonts.Length; i++) { - printer.Print(text, fonts[i]); + printer.Print(text, fonts[i], Color.White); GL.Translate(0, fonts[i].Height, 0); } diff --git a/Source/Examples/Tutorial/Text.cs b/Source/Examples/Tutorial/Text.cs index fa2dcf72..08a198e4 100644 --- a/Source/Examples/Tutorial/Text.cs +++ b/Source/Examples/Tutorial/Text.cs @@ -129,13 +129,10 @@ namespace Examples.Tutorial // Print FPS counter. Since the counter changes per frame, // it shouldn't be cached (TextPrinterOptions.NoCache). - GL.Color3(Color.LightYellow); - text.Print((1.0 / e.Time).ToString("F2"), sans, TextPrinterOptions.NoCache); + text.Print((1.0 / e.Time).ToString("F2"), sans, Color.LightYellow, RectangleF.Empty, TextPrinterOptions.NoCache); // Print the actual text. - GL.Translate(0.0f, current_position, 0.0f); - GL.Color3(Color.White); - text.Print(poem, serif); + text.Print(poem, serif, Color.White, new RectangleF(0, current_position, 0, 0), TextPrinterOptions.Default); text.End(); diff --git a/Source/Examples/WinForms/FontRendering.Designer.cs b/Source/Examples/WinForms/FontRendering.Designer.cs index c0b43c16..c8a6f324 100644 --- a/Source/Examples/WinForms/FontRendering.Designer.cs +++ b/Source/Examples/WinForms/FontRendering.Designer.cs @@ -62,6 +62,8 @@ // // textBox1 // + this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.textBox1.Location = new System.Drawing.Point(12, 11); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(550, 22); diff --git a/Source/Examples/WinForms/FontRendering.cs b/Source/Examples/WinForms/FontRendering.cs index 4427d611..aa98f9f1 100644 --- a/Source/Examples/WinForms/FontRendering.cs +++ b/Source/Examples/WinForms/FontRendering.cs @@ -14,7 +14,7 @@ namespace Examples.WinForms { #region Fields - float[] sizes = new float[] { 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 26, 28, 32, 36, 48 }; + float[] sizes = new float[] { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 28, 32, 36, 42, 48 }; List fonts = new List(); TextPrinter text = new TextPrinter(); @@ -70,15 +70,18 @@ namespace Examples.WinForms private void glControl1_Paint(object sender, PaintEventArgs e) { + GL.ClearColor(Color.Red); glControl1.MakeCurrent(); + //GL.ClearColor(Color.Gainsboro); GL.Clear(ClearBufferMask.ColorBufferBit); - GL.Color3(Color.White); + //GL.Color4(Color.Blue); + //GL.BlendColor(0, 0, 0, 0); text.Begin(); foreach (Font font in fonts) { - text.Print(textBox1.Text, font); + text.Print(textBox1.Text, font, Color.White); GL.Translate(0, font.Height, 0); } diff --git a/Source/Examples/WinForms/W01_First_Window.cs b/Source/Examples/WinForms/W01_First_Window.cs index 022192d2..e4d545b8 100644 --- a/Source/Examples/WinForms/W01_First_Window.cs +++ b/Source/Examples/WinForms/W01_First_Window.cs @@ -59,6 +59,8 @@ namespace Examples.WinForms private void glControl1_Paint(object sender, PaintEventArgs e) { + glControl1.MakeCurrent(); + GL.Clear(ClearBufferMask.ColorBufferBit); glControl1.SwapBuffers(); } diff --git a/Source/Utilities/Graphics/AlphaTexture2D.cs b/Source/Utilities/Graphics/AlphaTexture2D.cs index 637e4ec5..959f75cd 100644 --- a/Source/Utilities/Graphics/AlphaTexture2D.cs +++ b/Source/Utilities/Graphics/AlphaTexture2D.cs @@ -38,10 +38,6 @@ namespace OpenTK.Graphics /// class AlphaTexture2D : Texture2D { - #region Fields - - #endregion - #region Constructors /// @@ -53,10 +49,6 @@ namespace OpenTK.Graphics #endregion - #region Public Members - - #endregion - #region Protected Members protected override PixelInternalFormat InternalFormat diff --git a/Source/Utilities/Graphics/ITextPrinter.cs b/Source/Utilities/Graphics/ITextPrinter.cs index 717b2870..f9c3dc22 100644 --- a/Source/Utilities/Graphics/ITextPrinter.cs +++ b/Source/Utilities/Graphics/ITextPrinter.cs @@ -19,12 +19,12 @@ namespace OpenTK.Graphics { void Begin(); void End(); - void Print(string text, Font font); - void Print(string text, Font font, TextPrinterOptions options); - void Print(string text, Font font, TextPrinterOptions options, RectangleF layoutRectangle); + void Print(string text, Font font, Color color); + void Print(string text, Font font, Color color, RectangleF layoutRectangle); + void Print(string text, Font font, Color color, RectangleF layoutRectangle, TextPrinterOptions options); TextExtents Measure(string text, Font font); - TextExtents Measure(string text, Font font, TextPrinterOptions options); - TextExtents Measure(string text, Font font, TextPrinterOptions options, RectangleF layoutRectangle); + TextExtents Measure(string text, Font font, RectangleF layoutRectangle); + TextExtents Measure(string text, Font font, RectangleF layoutRectangle, TextPrinterOptions options); [Obsolete("Use TextPrinter.Print instead")] void Draw(TextHandle handle); diff --git a/Source/Utilities/Graphics/RgbaTexture2D.cs b/Source/Utilities/Graphics/RgbaTexture2D.cs new file mode 100644 index 00000000..7959ac65 --- /dev/null +++ b/Source/Utilities/Graphics/RgbaTexture2D.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Graphics +{ + class RgbaTexture2D : Texture2D + { + public RgbaTexture2D(int width, int height) + : base(width, height) + { } + + protected override PixelInternalFormat InternalFormat + { + get { return PixelInternalFormat.CompressedRgba; } + } + } +} diff --git a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs index c75dbe9e..d7428830 100644 --- a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs @@ -55,9 +55,9 @@ namespace OpenTK.Graphics.Text #region ITextOutputProvider Members - public void Print(TextBlock block, IGlyphRasterizer rasterizer, GlyphCache cache) + public void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache) { - using (TextExtents extents = rasterizer.MeasureText(block)) + using (TextExtents extents = rasterizer.MeasureText(block, location)) { //GL.BindTexture(TextureTarget.Texture2D, 2); diff --git a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs index e1f722c9..e3f8ac11 100644 --- a/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs +++ b/Source/Utilities/Graphics/Text/GdiPlusGlyphRasterizer .cs @@ -79,7 +79,11 @@ namespace OpenTK.Graphics.Text public Bitmap Rasterize(Glyph glyph) { - RectangleF r = MeasureText(new TextBlock(glyph.Character.ToString(), glyph.Font, TextPrinterOptions.NoCache, RectangleF.Empty)).BoundingBox; + RectangleF r = MeasureText( + new TextBlock( + glyph.Character.ToString(), glyph.Font, + TextPrinterOptions.NoCache, SizeF.Empty), + PointF.Empty).BoundingBox; EnsureSurfaceSize(ref glyph_surface, ref glyph_renderer, glyph.Font); @@ -102,9 +106,15 @@ namespace OpenTK.Graphics.Text { SetTextRenderingOptions(gfx, glyph.Font); - gfx.Clear(Color.Transparent); - gfx.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, PointF.Empty, - glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : default_string_format); + //gfx.Clear(Color.Transparent); + //gfx.Clear(Color.FromArgb(255, 0, 0, 0)); + //gfx.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, PointF.Empty, + // glyph.Font.Style & FontStyle.Italic != 0 ? load_glyph_string_format : default_string_format); + System.Windows.Forms.TextRenderer.DrawText(gfx, glyph.Character.ToString(), glyph.Font, Point.Empty, Color.White); + //, + // (glyph.Font.Style & FontStyle.Italic) != 0 ? + // System.Windows.Forms.TextFormatFlags.GlyphOverhangPadding : + // System.Windows.Forms.TextFormatFlags.Default); rect = FindEdges(bmp); } } @@ -113,7 +123,7 @@ namespace OpenTK.Graphics.Text #region MeasureText - public TextExtents MeasureText(TextBlock block) + public TextExtents MeasureText(TextBlock block, PointF location) { // First, check if we have cached this text block. Do not use block_cache.TryGetValue, to avoid thrashing // the user's TextBlockExtents struct. @@ -121,7 +131,8 @@ 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); + TextExtents extents = MeasureTextExtents(block, location); + if ((block.Options & TextPrinterOptions.NoCache) == 0) block_cache.Add(block, extents); @@ -158,29 +169,25 @@ namespace OpenTK.Graphics.Text void SetTextRenderingOptions(System.Drawing.Graphics gfx, Font font) { // Small sizes look blurry without gridfitting, so turn that on. - if (font.Size <= 18.0f) - gfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; - else - gfx.TextRenderingHint = TextRenderingHint.AntiAlias; + //if (font.Size <= 18.0f) + // gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; + //else + gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; + //gfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; } #endregion #region MeasureTextExtents - TextExtents MeasureTextExtents(TextBlock block) + TextExtents MeasureTextExtents(TextBlock block, PointF location) { // Todo: Parse layout options: StringFormat format = default_string_format; - //if (block.Font.Style != FontStyle.Regular) - // format = load_glyph_string_format; - //else - // format = default_string_format; TextExtents extents = text_extents_pool.Acquire(); - PointF origin = PointF.Empty; - SizeF size = SizeF.Empty; + RectangleF rect = new RectangleF(location, block.Bounds); SetTextRenderingOptions(graphics, block.Font); @@ -203,7 +210,7 @@ namespace OpenTK.Graphics.Text { extents.AddRange(MeasureGlyphExtents( s, height, 0, s.Length, - block.LayoutRectangle, + rect, native_graphics, native_font, native_string_format)); height += block.Font.Height; } diff --git a/Source/Utilities/Graphics/Text/GlyphSheet.cs b/Source/Utilities/Graphics/Text/GlyphSheet.cs index b2fbbc5d..3b929437 100644 --- a/Source/Utilities/Graphics/Text/GlyphSheet.cs +++ b/Source/Utilities/Graphics/Text/GlyphSheet.cs @@ -36,27 +36,25 @@ namespace OpenTK.Graphics.Text { #region Fields - AlphaTexture2D texture; - GlyphPacker packer; + //AlphaTexture2D texture; + RgbaTexture2D texture = new RgbaTexture2D(512, 512); + GlyphPacker packer = new GlyphPacker(512, 512); #endregion #region Constructors public GlyphSheet() - { - Texture = new AlphaTexture2D(256, 256); - Packer = new GlyphPacker(256, 256); - } + { } #endregion #region Public Members - public AlphaTexture2D Texture + public Texture2D Texture { get { return texture; } - private set { texture = value; } + private set { texture = (RgbaTexture2D)value; } } public GlyphPacker Packer diff --git a/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs b/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs index d07f7429..e52260c2 100644 --- a/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs +++ b/Source/Utilities/Graphics/Text/IGlyphRasterizer.cs @@ -37,7 +37,7 @@ namespace OpenTK.Graphics.Text interface IGlyphRasterizer { Bitmap Rasterize(Glyph glyph); - TextExtents MeasureText(TextBlock block); + TextExtents MeasureText(TextBlock block, PointF location); void Rasterize(Glyph glyph, ref Bitmap bmp, out Rectangle rect); } } diff --git a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs index 095a5646..1f007932 100644 --- a/Source/Utilities/Graphics/Text/ITextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/ITextOutputProvider.cs @@ -28,11 +28,12 @@ using System; using System.Collections.Generic; using System.Text; +using System.Drawing; namespace OpenTK.Graphics.Text { interface ITextOutputProvider { - void Print(TextBlock block, IGlyphRasterizer rasterizer, GlyphCache cache); + void Print(TextBlock block, PointF location, Color color, IGlyphRasterizer rasterizer, GlyphCache cache); } } diff --git a/Source/Utilities/Graphics/Text/TextBlock.cs b/Source/Utilities/Graphics/Text/TextBlock.cs index 2c8df7e0..579acd11 100644 --- a/Source/Utilities/Graphics/Text/TextBlock.cs +++ b/Source/Utilities/Graphics/Text/TextBlock.cs @@ -41,7 +41,7 @@ namespace OpenTK.Graphics.Text public readonly Font Font; - public readonly RectangleF LayoutRectangle; + public readonly SizeF Bounds; public readonly TextPrinterOptions Options; @@ -51,11 +51,11 @@ namespace OpenTK.Graphics.Text #region Constructors - public TextBlock(string text, Font font, TextPrinterOptions options, RectangleF layoutRectangle) + public TextBlock(string text, Font font, TextPrinterOptions options, SizeF bounds) { Text = text; Font = font; - LayoutRectangle = layoutRectangle; + Bounds = bounds; Options = options; UsageCount = 0; } @@ -74,7 +74,7 @@ namespace OpenTK.Graphics.Text public override int GetHashCode() { - return Text.GetHashCode() ^ Font.GetHashCode() ^ LayoutRectangle.GetHashCode() ^ Options.GetHashCode(); + return Text.GetHashCode() ^ Font.GetHashCode() ^ Bounds.GetHashCode() ^ Options.GetHashCode(); } public Glyph this[int i] @@ -91,7 +91,7 @@ namespace OpenTK.Graphics.Text return Text == other.Text && Font == other.Font && - LayoutRectangle == other.LayoutRectangle && + Bounds == other.Bounds && Options == other.Options; } diff --git a/Source/Utilities/Graphics/TextPrinter.cs b/Source/Utilities/Graphics/TextPrinter.cs index bdabe00b..22474c81 100644 --- a/Source/Utilities/Graphics/TextPrinter.cs +++ b/Source/Utilities/Graphics/TextPrinter.cs @@ -92,9 +92,15 @@ namespace OpenTK.Graphics 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); + //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); } @@ -121,17 +127,17 @@ namespace OpenTK.Graphics #region Print - public void Print(string text, Font font) + public void Print(string text, Font font, Color color) { - Print(text, font, 0, RectangleF.Empty); + Print(text, font, color, RectangleF.Empty, TextPrinterOptions.Default); } - public void Print(string text, Font font, TextPrinterOptions options) + public void Print(string text, Font font, Color color, RectangleF layoutRectangle) { - Print(text, font, options, RectangleF.Empty); + Print(text, font, color, layoutRectangle, TextPrinterOptions.Default); } - public void Print(string text, Font font, TextPrinterOptions options, RectangleF layoutRectangle) + public void Print(string text, Font font, Color color, RectangleF layoutRectangle, TextPrinterOptions options) { if (String.IsNullOrEmpty(text)) return; @@ -139,7 +145,7 @@ namespace OpenTK.Graphics if (font == null) throw new ArgumentNullException("font"); - text_output.Print(new TextBlock(text, font, options, layoutRectangle), glyph_rasterizer, glyph_cache); + text_output.Print(new TextBlock(text, font, options, layoutRectangle.Size), layoutRectangle.Location, color, glyph_rasterizer, glyph_cache); } #endregion @@ -148,17 +154,17 @@ namespace OpenTK.Graphics public TextExtents Measure(string text, Font font) { - return Measure(text, font, 0, RectangleF.Empty); - } - - public TextExtents Measure(string text, Font font, TextPrinterOptions options) - { - return Measure(text, font, options, RectangleF.Empty); + return Measure(text, font, RectangleF.Empty, TextPrinterOptions.Default); } - public TextExtents Measure(string text, Font font, TextPrinterOptions options, RectangleF layoutRectangle) + public TextExtents Measure(string text, Font font, RectangleF layoutRectangle) { - return glyph_rasterizer.MeasureText(new TextBlock(text, font, options, layoutRectangle)); + return Measure(text, font, layoutRectangle, TextPrinterOptions.Default); + } + + public TextExtents Measure(string text, Font font, RectangleF layoutRectangle, TextPrinterOptions options) + { + return glyph_rasterizer.MeasureText(new TextBlock(text, font, options, layoutRectangle.Size), layoutRectangle.Location); } #endregion @@ -168,13 +174,13 @@ namespace OpenTK.Graphics [Obsolete("Use TextPrinter.Print instead")] public void Draw(TextHandle handle) { - Print(handle.Text, handle.GdiPFont); + Print(handle.Text, handle.GdiPFont, Color.White); } [Obsolete("Use TextPrinter.Print instead")] public void Draw(string text, TextureFont font) { - Print(text, font.font); + Print(text, font.font, Color.White); } [Obsolete("Use TextPrinter.Print instead")]