2007-11-06 13:29:18 +00:00
|
|
|
|
#region --- License ---
|
2008-05-05 17:13:22 +00:00
|
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
|
* See license.txt for licensing details.
|
2007-11-06 13:29:18 +00:00
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2007-11-01 23:22:00 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2007-11-06 20:59:15 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2008-11-24 16:43:56 +00:00
|
|
|
|
using System.Diagnostics;
|
2007-11-01 23:22:00 +00:00
|
|
|
|
|
|
|
|
|
using OpenTK.Math;
|
2008-11-24 16:43:56 +00:00
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Graphics.Text;
|
|
|
|
|
using OpenTK.Platform;
|
2007-11-01 23:22:00 +00:00
|
|
|
|
|
2008-03-08 14:38:10 +00:00
|
|
|
|
namespace OpenTK.Fonts { }
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Graphics
|
2007-11-01 23:22:00 +00:00
|
|
|
|
{
|
2007-11-06 13:29:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides methods to perform layout and print hardware accelerated text.
|
|
|
|
|
/// </summary>
|
2008-11-24 16:43:56 +00:00
|
|
|
|
public sealed class TextPrinter : ITextPrinter
|
2007-11-01 23:22:00 +00:00
|
|
|
|
{
|
2008-11-25 18:00:17 +00:00
|
|
|
|
#region Fields
|
2007-11-12 07:36:34 +00:00
|
|
|
|
|
2008-11-25 18:00:17 +00:00
|
|
|
|
IGlyphRasterizer glyph_rasterizer;
|
|
|
|
|
ITextOutputProvider text_output;
|
2009-02-12 16:27:24 +00:00
|
|
|
|
TextQuality text_quality;
|
2007-11-12 07:36:34 +00:00
|
|
|
|
|
2009-02-12 17:41:09 +00:00
|
|
|
|
bool disposed;
|
|
|
|
|
|
2007-11-12 07:36:34 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-11-25 18:00:17 +00:00
|
|
|
|
#region Constructors
|
2007-11-12 07:36:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// Constructs a new TextPrinter instance.
|
2007-11-12 07:36:34 +00:00
|
|
|
|
/// </summary>
|
2008-11-25 18:00:17 +00:00
|
|
|
|
public TextPrinter()
|
2009-02-12 16:27:24 +00:00
|
|
|
|
: this(null, null, TextQuality.Default) { }
|
2008-01-06 02:19:53 +00:00
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new TextPrinter instance with the specified TextQuality level.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="quality">The desired TextQuality of this TextPrinter.</param>
|
2009-02-12 16:27:24 +00:00
|
|
|
|
public TextPrinter(TextQuality quality)
|
|
|
|
|
: this(null, null, quality) { }
|
2008-04-13 18:29:36 +00:00
|
|
|
|
|
2009-02-12 16:27:24 +00:00
|
|
|
|
TextPrinter(IGlyphRasterizer rasterizer, ITextOutputProvider output, TextQuality quality)
|
|
|
|
|
{
|
2008-11-25 18:00:17 +00:00
|
|
|
|
glyph_rasterizer = rasterizer;
|
|
|
|
|
text_output = output;
|
2009-02-12 16:27:24 +00:00
|
|
|
|
text_quality = quality;
|
2007-11-12 07:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-11-25 18:00:17 +00:00
|
|
|
|
#region ITextPrinter Members
|
2007-11-12 07:36:34 +00:00
|
|
|
|
|
2008-11-24 16:43:56 +00:00
|
|
|
|
#region Print
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints text using the specified color and layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to print.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to print text.</param>
|
|
|
|
|
/// <param name="color">The System.Drawing.Color that will be used to print text.</param>
|
2008-11-26 21:49:05 +00:00
|
|
|
|
public void Print(string text, Font font, Color color)
|
2008-11-24 16:43:56 +00:00
|
|
|
|
{
|
2009-02-12 22:50:05 +00:00
|
|
|
|
Print(text, font, color, RectangleF.Empty, TextPrinterOptions.Default, TextAlignment.Near, TextDirection.LeftToRight);
|
2008-11-24 16:43:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints text using the specified color and layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to print.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to print text.</param>
|
|
|
|
|
/// <param name="color">The System.Drawing.Color that will be used to print text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public void Print(string text, Font font, Color color, RectangleF rect)
|
2008-11-24 16:43:56 +00:00
|
|
|
|
{
|
2009-02-12 22:50:05 +00:00
|
|
|
|
Print(text, font, color, rect, TextPrinterOptions.Default, TextAlignment.Near, TextDirection.LeftToRight);
|
2008-11-24 16:43:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints text using the specified color and layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to print.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to print text.</param>
|
|
|
|
|
/// <param name="color">The System.Drawing.Color that will be used to print text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to print text.</param>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public void Print(string text, Font font, Color color, RectangleF rect, TextPrinterOptions options)
|
|
|
|
|
{
|
|
|
|
|
Print(text, font, color, rect, options, TextAlignment.Near, TextDirection.LeftToRight);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints text using the specified color and layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to print.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to print text.</param>
|
|
|
|
|
/// <param name="color">The System.Drawing.Color that will be used to print text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to print text.</param>
|
|
|
|
|
/// <param name="alignment">The OpenTK.Graphics.TextAlignment that will be used to print text.</param>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public void Print(string text, Font font, Color color, RectangleF rect, TextPrinterOptions options, TextAlignment alignment)
|
|
|
|
|
{
|
|
|
|
|
Print(text, font, color, rect, options, alignment, TextDirection.LeftToRight);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints text using the specified color and layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to print.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to print text.</param>
|
|
|
|
|
/// <param name="color">The System.Drawing.Color that will be used to print text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to print text.</param>
|
|
|
|
|
/// <param name="alignment">The OpenTK.Graphics.TextAlignment that will be used to print text.</param>
|
|
|
|
|
/// <param name="direction">The OpenTK.Graphics.TextDirection that will be used to print text.</param>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public void Print(string text, Font font, Color color, RectangleF rect, TextPrinterOptions options, TextAlignment alignment, TextDirection direction)
|
2008-11-24 16:43:56 +00:00
|
|
|
|
{
|
2009-02-12 17:41:09 +00:00
|
|
|
|
if (disposed)
|
|
|
|
|
throw new ObjectDisposedException(this.GetType().ToString());
|
|
|
|
|
|
2009-02-12 22:50:05 +00:00
|
|
|
|
if (!ValidateParameters(text, font, rect))
|
2008-11-24 16:43:56 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2009-02-12 22:50:05 +00:00
|
|
|
|
TextOutput.Print(new TextBlock(text, font, rect, options, alignment, direction), color, Rasterizer);
|
2008-11-24 16:43:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-11-25 16:11:31 +00:00
|
|
|
|
#region Measure
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Measures text using the specified layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to measure.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to measure text.</param>
|
|
|
|
|
/// <returns>An OpenTK.Graphics.TextExtents instance that contains the results of the measurement.</returns>
|
2008-11-25 16:11:31 +00:00
|
|
|
|
public TextExtents Measure(string text, Font font)
|
|
|
|
|
{
|
2009-02-12 22:50:05 +00:00
|
|
|
|
return Measure(text, font, RectangleF.Empty, TextPrinterOptions.Default, TextAlignment.Near, TextDirection.LeftToRight);
|
2008-11-25 16:11:31 +00:00
|
|
|
|
}
|
2008-11-26 21:49:05 +00:00
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Measures text using the specified layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to measure.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to measure text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <returns>An OpenTK.Graphics.TextExtents instance that contains the results of the measurement.</returns>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public TextExtents Measure(string text, Font font, RectangleF rect)
|
|
|
|
|
{
|
|
|
|
|
return Measure(text, font, rect, TextPrinterOptions.Default, TextAlignment.Near, TextDirection.LeftToRight);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Measures text using the specified layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to measure.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to measure text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to measure text.</param>
|
|
|
|
|
/// <returns>An OpenTK.Graphics.TextExtents instance that contains the results of the measurement.</returns>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public TextExtents Measure(string text, Font font, RectangleF rect, TextPrinterOptions options)
|
|
|
|
|
{
|
|
|
|
|
return Measure(text, font, rect, options, TextAlignment.Near, TextDirection.LeftToRight);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Measures text using the specified layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to measure.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to measure text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to measure text.</param>
|
|
|
|
|
/// <param name="alignment">The OpenTK.Graphics.TextAlignment that will be used to measure text.</param>
|
|
|
|
|
/// <returns>An OpenTK.Graphics.TextExtents instance that contains the results of the measurement.</returns>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public TextExtents Measure(string text, Font font, RectangleF rect, TextPrinterOptions options, TextAlignment alignment)
|
2008-11-25 16:11:31 +00:00
|
|
|
|
{
|
2009-02-12 22:50:05 +00:00
|
|
|
|
return Measure(text, font, rect, options, alignment, TextDirection.LeftToRight);
|
2008-11-25 16:11:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Measures text using the specified layout options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The System.String to measure.</param>
|
|
|
|
|
/// <param name="font">The System.Drawing.Font that will be used to measure text.</param>
|
|
|
|
|
/// <param name="rect">The System.Drawing.Rectangle that defines the bounds for text layout.</param>
|
|
|
|
|
/// <param name="options">The OpenTK.Graphics.TextPrinterOptions that will be used to measure text.</param>
|
|
|
|
|
/// <param name="alignment">The OpenTK.Graphics.TextAlignment that will be used to measure text.</param>
|
|
|
|
|
/// <param name="direction">The OpenTK.Graphics.TextDirection that will be used to measure text.</param>
|
|
|
|
|
/// <returns>An OpenTK.Graphics.TextExtents instance that contains the results of the measurement.</returns>
|
2009-02-12 22:50:05 +00:00
|
|
|
|
public TextExtents Measure(string text, Font font, RectangleF rect, TextPrinterOptions options, TextAlignment alignment, TextDirection direction)
|
2008-11-25 16:11:31 +00:00
|
|
|
|
{
|
2009-02-12 17:41:09 +00:00
|
|
|
|
if (disposed)
|
|
|
|
|
throw new ObjectDisposedException(this.GetType().ToString());
|
|
|
|
|
|
2009-02-12 22:50:05 +00:00
|
|
|
|
if (!ValidateParameters(text, font, rect))
|
2009-02-12 16:27:24 +00:00
|
|
|
|
return TextExtents.Empty;
|
|
|
|
|
|
2009-02-12 22:50:05 +00:00
|
|
|
|
return Rasterizer.MeasureText(new TextBlock(text, font, rect, options, alignment, direction));
|
2009-02-12 16:27:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#region Clear
|
2009-02-12 16:27:24 +00:00
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2009-02-12 17:41:09 +00:00
|
|
|
|
if (disposed)
|
|
|
|
|
throw new ObjectDisposedException(this.GetType().ToString());
|
|
|
|
|
|
|
|
|
|
TextOutput.Clear();
|
|
|
|
|
Rasterizer.Clear();
|
2008-11-25 16:11:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#region Begin
|
2008-11-25 18:17:30 +00:00
|
|
|
|
|
2009-02-12 17:41:09 +00:00
|
|
|
|
/// <summary>
|
2009-02-12 23:40:48 +00:00
|
|
|
|
/// Sets up a resolution-dependent orthographic projection.
|
2009-02-12 17:41:09 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void Begin()
|
|
|
|
|
{
|
2009-02-12 23:40:48 +00:00
|
|
|
|
TextOutput.Begin();
|
|
|
|
|
}
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#endregion
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#region Begin
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2009-02-12 23:40:48 +00:00
|
|
|
|
/// Restores the projection and modelview matrices to their previous state.
|
2009-02-12 17:41:09 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void End()
|
|
|
|
|
{
|
2009-02-12 23:40:48 +00:00
|
|
|
|
TextOutput.End();
|
|
|
|
|
}
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#endregion
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
2009-02-12 23:40:48 +00:00
|
|
|
|
#region Obsolete
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
2008-11-25 18:17:30 +00:00
|
|
|
|
[Obsolete("Use TextPrinter.Print instead")]
|
|
|
|
|
public void Draw(TextHandle handle)
|
|
|
|
|
{
|
2008-11-26 21:49:05 +00:00
|
|
|
|
Print(handle.Text, handle.GdiPFont, Color.White);
|
2008-11-25 18:17:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use TextPrinter.Print instead")]
|
|
|
|
|
public void Draw(string text, TextureFont font)
|
|
|
|
|
{
|
2008-11-26 21:49:05 +00:00
|
|
|
|
Print(text, font.font, Color.White);
|
2008-11-25 18:17:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use TextPrinter.Print instead")]
|
|
|
|
|
public void Prepare(string text, TextureFont font, out TextHandle handle)
|
|
|
|
|
{
|
|
|
|
|
handle = new TextHandle(text, font.font);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-11-24 16:43:56 +00:00
|
|
|
|
#endregion
|
2009-02-12 16:27:24 +00:00
|
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
|
|
IGlyphRasterizer Rasterizer
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (glyph_rasterizer == null)
|
|
|
|
|
glyph_rasterizer = new GdiPlusGlyphRasterizer();
|
|
|
|
|
|
|
|
|
|
return glyph_rasterizer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ITextOutputProvider TextOutput
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (text_output == null)
|
|
|
|
|
text_output = GL1TextOutputProvider.Create(text_quality);
|
|
|
|
|
|
|
|
|
|
return text_output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Static Members
|
|
|
|
|
|
2009-02-12 22:50:05 +00:00
|
|
|
|
static bool ValidateParameters(string text, Font font, RectangleF rect)
|
2009-02-12 16:27:24 +00:00
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(text))
|
|
|
|
|
return false;
|
|
|
|
|
if (font == null)
|
|
|
|
|
throw new ArgumentNullException("font");
|
2009-02-12 22:50:05 +00:00
|
|
|
|
if (rect.Width < 0 || rect.Height < 0)
|
2009-02-12 16:27:24 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException("size");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2009-02-12 17:41:09 +00:00
|
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
2009-02-12 23:10:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Frees the resources consumed by this TextPrinter object.
|
|
|
|
|
/// </summary>
|
2009-02-12 17:41:09 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
|
|
|
|
TextOutput.Dispose();
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-11-06 13:29:18 +00:00
|
|
|
|
}
|
2007-11-01 23:22:00 +00:00
|
|
|
|
}
|