Moved GdiPlus wrappers to OpenTK.Compatibility.

Moved TextPrinter to OpenTK.Compatibility.
This commit is contained in:
the_fiddler 2009-08-17 10:29:51 +00:00
parent 192e99a6ad
commit 7f970763a7
17 changed files with 1370 additions and 4 deletions

View file

@ -0,0 +1,37 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
namespace OpenTK.Graphics
{
[Obsolete()]
class DisplayListTextHandle : TextHandle
{
public DisplayListTextHandle(int handle) : base(handle) { }
public override string ToString()
{
return String.Format("TextHandle (display list): {0}", Handle);
}
protected override void Dispose(bool manual)
{
if (!disposed)
{
if (manual)
{
GL.DeleteLists(Handle, 1);
}
disposed = true;
}
}
}
}

View file

@ -0,0 +1,57 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
namespace OpenTK.Graphics
{
/// <summary>
/// Provides text printing through OpenGL 1.1 Display Lists.
/// </summary>
[Obsolete()]
class DisplayListTextPrinter : ITextPrinterImplementation
{
#region IPrinter Members
public TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count)
{
DisplayListTextHandle handle = new DisplayListTextHandle(GL.GenLists(1));
GL.NewList(handle.Handle, ListMode.Compile);
this.Draw(vertices, indices, index_count);
GL.EndList();
return handle;
}
public void Draw(TextHandle handle)
{
GL.CallList(handle.Handle);
}
public void Draw(Vector2[] vertices, ushort[] indices, int index_count)
{
GL.Begin(BeginMode.Triangles);
for (int i = 0; i < index_count; i++)
//foreach (ushort index in indices)
{
GL.TexCoord2(vertices[indices[i] + 1]);
GL.Vertex2(vertices[indices[i]]);
}
GL.End();
}
#endregion
}
}

View file

@ -0,0 +1,176 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace OpenTK.Graphics
{
using Graphics = System.Drawing.Graphics;
/// <summary>
/// Represents a single character of a specific Font.
/// </summary>
[Obsolete]
struct Glyph : IPackable<Glyph>
{
char character;
Font font;
SizeF size;
#region --- Constructors ---
// Constructs a new Glyph that represents the given character and Font.
public Glyph(char c, Font f, SizeF s)
{
if (f == null)
throw new ArgumentNullException("f", "You must specify a valid font");
character = c;
font = f;
size = s;
}
#endregion
#region --- Public Methods ---
#region public char Character
/// <summary>
/// Gets the character represented by this Glyph.
/// </summary>
public char Character
{
get { return character; }
private set { character = value; }
}
#endregion
#region public Font Font
/// <summary>
/// Gets the Font of this Glyph.
/// </summary>
public Font Font
{
get { return font; }
private set
{
if (value == null)
throw new ArgumentNullException("Font", "Glyph font cannot be null");
font = value;
}
}
#endregion
#region public override bool Equals(object obj)
/// <summary>
/// Checks whether the given object is equal (memberwise) to the current Glyph.
/// </summary>
/// <param name="obj">The obj to check.</param>
/// <returns>True, if the object is identical to the current Glyph.</returns>
public override bool Equals(object obj)
{
if (obj is Glyph)
return this.Equals((Glyph)obj);
return base.Equals(obj);
}
#endregion
#region public override string ToString()
/// <summary>
/// Describes this Glyph object.
/// </summary>
/// <returns>Returns a System.String describing this Glyph.</returns>
public override string ToString()
{
return String.Format("'{0}', {1} {2}, {3} {4}, ({5}, {6})", Character, Font.Name, font.Style, font.Size, font.Unit, Width, Height);
}
#endregion
#region public override int GetHashCode()
/// <summary>
/// Calculates the hashcode for this Glyph.
/// </summary>
/// <returns>A System.Int32 containing a hashcode that uniquely identifies this Glyph.</returns>
public override int GetHashCode()
{
return character.GetHashCode() ^ font.GetHashCode() ^ size.GetHashCode();
}
#endregion
#region public SizeF Size
/// <summary>
/// Gets the size of this Glyph.
/// </summary>
public SizeF Size { get { return size; } }
#endregion
#region public RectangleF Rectangle
/// <summary>
/// Gets the bounding box of this Glyph.
/// </summary>
public RectangleF Rectangle { get { return new RectangleF(PointF.Empty, Size); } }
#endregion
#endregion
#region --- IPackable<T> Members ---
/// <summary>
/// Gets an integer representing the width of the Glyph in pixels.
/// </summary>
public int Width
{
get
{
return (int)System.Math.Ceiling(size.Width);
}
}
/// <summary>
/// Gets an integer representing the height of the Glyph in pixels.
/// </summary>
public int Height
{
get
{
return (int)System.Math.Ceiling(size.Height);
}
}
#endregion
#region --- IEquatable<Glyph> Members ---
/// <summary>
/// Compares the current Glyph with the given Glyph.
/// </summary>
/// <param name="other">The Glyph to compare to.</param>
/// <returns>True if both Glyphs represent the same character of the same Font, false otherwise.</returns>
public bool Equals(Glyph other)
{
return Character == other.Character && Font == other.Font && Size == other.Size;
}
#endregion
}
}

View file

@ -0,0 +1,21 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Graphics
{
[Obsolete]
public interface IFont : IDisposable
{
void LoadGlyphs(string glyphs);
float Height { get; }
void MeasureString(string str, out float width, out float height);
}
}

View file

@ -0,0 +1,45 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Graphics
{
/// <summary>
/// Defines the interface for TextPrinter implementations.
/// </summary>
[Obsolete("Use ITextOutputProvider instead")]
public interface ITextPrinterImplementation
{
/// <summary>
/// Caches a text fragment for future use.
/// </summary>
/// <param name="vertices">The vertex array for the text fragment.</param>
/// <param name="indices">The index array for the text fragment. Please use the indexCount parameter
/// instead of indices.Count, as the indices array may be larger than necessary for performance reasons.</param>
/// <param name="indexCount">The actual number of indices in the text fragment.</param>
/// <returns>A TextHandle that can be used to draw the text fragment.</returns>
TextHandle Load(Vector2[] vertices, ushort[] indices, int indexCount);
/// <summary>
/// Draws the specified cached text fragment.
/// </summary>
/// <param name="handle">The TextHandle corresponding to the desired text fragment.</param>
void Draw(TextHandle handle);
/// <summary>
/// Draws a text fragment, without caching.
/// </summary>
/// <param name="vertices">The vertex array for the text fragment.</param>
/// <param name="indices">The index array for the text fragment. Please use the indexCount parameter
/// instead of indices.Count, as the indices array may be larger than necessary for performance reasons.</param>
/// <param name="indexCount">The actual number of indices in the text fragment.</param>
void Draw(Vector2[] vertices, ushort[] indices, int indexCount);
}
}

View file

@ -0,0 +1,88 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Graphics
{
/// <summary>
/// Represents a handle to cached text.
/// </summary>
[Obsolete("Use TextPrinter.Print instead")]
public class TextHandle : IDisposable
{
internal string Text;
internal System.Drawing.Font GdiPFont;
/// <summary>
/// Constructs a new TextHandle,
/// </summary>
/// <param name="handle"></param>
internal TextHandle(int handle)
{
Handle = handle;
}
internal TextHandle(string text, System.Drawing.Font font)
{
Text = text;
GdiPFont = font;
}
private int handle;
protected TextureFont font;
protected bool disposed;
/// <summary>
/// Gets the handle of the cached text run. Call the OpenTK.Graphics.ITextPrinter.Draw() method
/// to draw the text represented by this TextHandle.
/// </summary>
public int Handle
{
get { return handle; }
protected set { handle = value; }
}
/// <summary>
/// Gets the TextureFont used for this text run.
/// </summary>
public TextureFont Font
{
get { return font; }
internal set { font = value; }
}
#region public override string ToString()
/// <summary>
/// Returns a System.String that represents the current TextHandle.
/// </summary>
/// <returns>a System.String that descibes the current TextHandle.</returns>
public override string ToString()
{
return String.Format("TextHandle: {0}", Handle);
}
#endregion
#region --- IDisposable Members ---
/// <summary>
/// Frees the resource consumed by the TextHandle.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool manual) { }
~TextHandle() { this.Dispose(false); }
#endregion
}
}

View file

@ -0,0 +1,563 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
namespace OpenTK.Graphics
{
using Graphics = System.Drawing.Graphics;
using PixelFormat = OpenTK.Graphics.OpenGL.PixelFormat;
using System.Text.RegularExpressions;
[Obsolete("Use System.Drawing.Font instead")]
public class TextureFont : IFont
{
internal Font font;
Dictionary<char, RectangleF> loaded_glyphs = new Dictionary<char, RectangleF>(64);
Bitmap bmp;
Graphics gfx;
// TODO: We need to be able to use multiple font sheets.
static int texture;
static TexturePacker<Glyph> pack;
static int texture_width, texture_height;
static readonly StringFormat default_string_format = StringFormat.GenericTypographic; // Check the constructor, too, for additional flags.
static readonly StringFormat load_glyph_string_format = StringFormat.GenericDefault;
static SizeF maximum_graphics_size;
int[] data = new int[256]; // Used to upload the glyph buffer to the OpenGL texture.
object upload_lock = new object();
static readonly char[] newline_characters = new char[] { '\n', '\r' };
#region --- Constructor ---
/// <summary>
/// Constructs a new TextureFont, using the specified System.Drawing.Font.
/// </summary>
/// <param name="font">The System.Drawing.Font to use.</param>
public TextureFont(Font font)
{
if (font == null)
throw new ArgumentNullException("font", "Argument to TextureFont constructor cannot be null.");
this.font = font;
bmp = new Bitmap(font.Height * 2, font.Height * 2);
gfx = Graphics.FromImage(bmp);
maximum_graphics_size = gfx.ClipBounds.Size;
// Adjust font rendering mode. Small sizes look blurry without gridfitting, so turn
// that on. Increasing contrast also seems to help.
if (font.Size <= 18.0f)
{
gfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//gfx.TextContrast = 11;
}
else
{
gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
//gfx.TextContrast = 0;
}
default_string_format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
}
/// <summary>
/// Constructs a new TextureFont, using the specified parameters.
/// </summary>
/// <param name="family">The System.Drawing.FontFamily to use for the typeface.</param>
/// <param name="emSize">The em size to use for the typeface.</param>
public TextureFont(FontFamily family, float emSize)
: this(new Font(family, emSize))
{ }
/// <summary>
/// Constructs a new TextureFont, using the specified parameters.
/// </summary>
/// <param name="family">The System.Drawing.FontFamily to use for the typeface.</param>
/// <param name="emSize">The em size to use for the typeface.</param>
/// <param name="style">The style to use for the typeface.</param>
public TextureFont(FontFamily family, float emSize, FontStyle style)
: this(new Font(family, emSize, style))
{ }
#endregion
#region --- Public Methods ---
#region public void LoadGlyphs(string glyphs)
/// <summary>
/// Prepares the specified glyphs for rendering.
/// </summary>
/// <param name="glyphs">The glyphs to prepare for rendering.</param>
public void LoadGlyphs(string glyphs)
{
RectangleF rect = new RectangleF();
foreach (char c in glyphs)
{
if (Char.IsWhiteSpace(c))
continue;
try
{
if (!loaded_glyphs.ContainsKey(c))
LoadGlyph(c, out rect);
}
catch (Exception e)
{
Debug.Print(e.ToString());
throw;
}
}
}
#endregion
#region public void LoadGlyph(char glyph)
/// <summary>
/// Prepares the specified glyph for rendering.
/// </summary>
/// <param name="glyph">The glyph to prepare for rendering.</param>
public void LoadGlyph(char glyph)
{
RectangleF rect = new RectangleF();
if (!loaded_glyphs.ContainsKey(glyph))
LoadGlyph(glyph, out rect);
}
#endregion
#region public bool GlyphData(char glyph, out float width, out float height, out RectangleF textureRectangle, out int texture)
/// <summary>
/// Returns the characteristics of a loaded glyph.
/// </summary>
/// <param name="glyph">The character corresponding to this glyph.</param>
/// <param name="width">The width of this glyph.</param>
/// <param name="height">The height of this glyph (line spacing).</param>
/// <param name="textureRectangle">The bounding box of the texture buffer of this glyph.</param>
/// <param name="texture">The handle to the texture that contains this glyph.</param>
/// <returns>True if the glyph has been loaded, false otherwise.</returns>
/// <seealso cref="LoadGlyphs"/>
public bool GlyphData(char glyph, out float width, out float height, out RectangleF textureRectangle, out int texture)
{
if (loaded_glyphs.TryGetValue(glyph, out textureRectangle))
{
width = textureRectangle.Width * texture_width;
height = textureRectangle.Height * texture_height;
texture = TextureFont.texture;
return true;
}
width = height = texture = 0;
return false;
}
#endregion
#region public float Height
/// <summary>
/// Gets a float indicating the default line spacing of this font.
/// </summary>
public float Height
{
get { return font.Height; }
}
#endregion
#region public float Width
/// <summary>
/// Gets a float indicating the default size of this font, in points.
/// </summary>
public float Width
{
get { return font.SizeInPoints; }
}
#endregion
#region public void MeasureString(string str, out float width, out float height, bool accountForOverhangs)
/// <summary>
/// Measures the width of the specified string.
/// </summary>
/// <param name="str">The string to measure.</param>
/// <param name="width">The measured width.</param>
/// <param name="height">The measured height.</param>
/// <param name="accountForOverhangs">If true, adds space to account for glyph overhangs. Set to true if you wish to measure a complete string. Set to false if you wish to perform layout on adjacent strings.</param>
[Obsolete("Returns invalid results - use MeasureText() instead")]
public void MeasureString(string str, out float width, out float height, bool accountForOverhangs)
{
System.Drawing.StringFormat format = accountForOverhangs ? System.Drawing.StringFormat.GenericDefault : System.Drawing.StringFormat.GenericTypographic;
System.Drawing.SizeF size = gfx.MeasureString(str, font, 16384, format);
height = size.Height;
width = size.Width;
}
#endregion
#region public void MeasureString(string str, out float width, out float height)
/// <summary>
/// Measures the width of the specified string.
/// </summary>
/// <param name="str">The string to measure.</param>
/// <param name="width">The measured width.</param>
/// <param name="height">The measured height.</param>
/// <seealso cref="MeasureString(string, out float, out float, bool)"/>
[Obsolete("Returns invalid results - use MeasureText() instead")]
public void MeasureString(string str, out float width, out float height)
{
MeasureString(str, out width, out height, true);
}
#endregion
#region public RectangleF MeasureText(string text)
/// <summary>
/// Calculates size information for the specified text.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <returns>A RectangleF containing the bounding box for the specified text.</returns>
public RectangleF MeasureText(string text)
{
return MeasureText(text, SizeF.Empty, default_string_format, null);
}
#endregion
#region public RectangleF MeasureText(string text, SizeF bounds)
/// <summary>
/// Calculates size information for the specified text.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="bounds">A SizeF structure containing the maximum desired width and height of the text. Default is SizeF.Empty.</param>
/// <returns>A RectangleF containing the bounding box for the specified text.</returns>
public RectangleF MeasureText(string text, SizeF bounds)
{
return MeasureText(text, bounds, default_string_format, null);
}
#endregion
#region public RectangleF MeasureText(string text, SizeF bounds, StringFormat format)
/// <summary>
/// Calculates size information for the specified text.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="bounds">A SizeF structure containing the maximum desired width and height of the text. Pass SizeF.Empty to disable wrapping calculations. A width or height of 0 disables the relevant calculation.</param>
/// <param name="format">A StringFormat object which specifies the measurement format of the string. Pass null to use the default StringFormat (StringFormat.GenericTypographic).</param>
/// <returns>A RectangleF containing the bounding box for the specified text.</returns>
public RectangleF MeasureText(string text, SizeF bounds, StringFormat format)
{
return MeasureText(text, bounds, format, null);
}
#endregion
#region public RectangleF MeasureText(string text, SizeF bounds, StringFormat format, IList<RectangleF> ranges)
IntPtr[] regions = new IntPtr[GdiPlus.MaxMeasurableCharacterRanges];
CharacterRange[] characterRanges = new CharacterRange[GdiPlus.MaxMeasurableCharacterRanges];
/// <summary>
/// Calculates size information for the specified text.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="bounds">A SizeF structure containing the maximum desired width and height of the text. Pass SizeF.Empty to disable wrapping calculations. A width or height of 0 disables the relevant calculation.</param>
/// <param name="format">A StringFormat object which specifies the measurement format of the string. Pass null to use the default StringFormat (StringFormat.GenericDefault).</param>
/// <param name="ranges">Fills the specified IList of RectangleF structures with position information for individual characters. If this argument is null, these calculations are skipped.</param>
/// <returns>A RectangleF containing the bounding box for the specified text.</returns>
public RectangleF MeasureText(string text, SizeF bounds, StringFormat format, List<RectangleF> ranges)
{
if (String.IsNullOrEmpty(text))
return RectangleF.Empty;
if (bounds == SizeF.Empty)
bounds = maximum_graphics_size;
if (format == null)
format = default_string_format;
// TODO: What should we do in this case?
if (ranges == null)
ranges = new List<RectangleF>();
ranges.Clear();
PointF origin = PointF.Empty;
SizeF size = SizeF.Empty;
IntPtr native_graphics = GdiPlus.GetNativeGraphics(gfx);
IntPtr native_font = GdiPlus.GetNativeFont(font);
IntPtr native_string_format = GdiPlus.GetNativeStringFormat(format);
RectangleF layoutRect = new RectangleF(PointF.Empty, bounds);
int height = 0;
// It seems that the mere presence of \n and \r characters
// is enough for Mono to botch the layout (even if these
// characters are not processed.) We'll need to find a
// different way to perform layout on Mono, probably
// through Pango.
// Todo: This workaround allocates memory.
//if (Configuration.RunningOnMono)
{
string[] lines = text.Replace("\r", String.Empty).Split(newline_characters);
foreach (string s in lines)
{
ranges.AddRange(GetCharExtents(
s, height, 0, s.Length, layoutRect,
native_graphics, native_font, native_string_format));
height += font.Height;
}
}
return new RectangleF(ranges[0].X, ranges[0].Y, ranges[ranges.Count - 1].Right, ranges[ranges.Count - 1].Bottom);
}
#endregion
#endregion
#region --- Private Methods ---
#region private void PrepareTexturePacker()
/// <summary>
/// Calculates the optimal size for the font texture and TexturePacker, and creates both.
/// </summary>
private void PrepareTexturePacker()
{
// Calculate the size of the texture packer. We want a power-of-two size
// that is less than 1024 (supported in Geforce256-era cards), but large
// enough to hold at least 256 (16*16) font glyphs.
// TODO: Find the actual card limits, maybe?
int size = (int)(font.Size * 16);
size = (int)System.Math.Pow(2.0, System.Math.Ceiling(System.Math.Log((double)size, 2.0)));
if (size > 1024)
size = 1024;
texture_width = size;
texture_height = size;
pack = new TexturePacker<Glyph>(texture_width, texture_height);
GL.GenTextures(1, out texture);
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
if (GL.SupportsExtension("Version12"))
{
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);
}
else
{
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Clamp);
}
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Alpha, texture_width, texture_height, 0,
OpenTK.Graphics.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
}
#endregion
#region private void LoadGlyph(char c, out RectangleF rectangle)
// Adds the specified caharacter to the texture packer.
private void LoadGlyph(char c, out RectangleF rectangle)
{
if (pack == null)
PrepareTexturePacker();
RectangleF glyph_rect = MeasureText(c.ToString(), SizeF.Empty, load_glyph_string_format);
SizeF glyph_size = new SizeF(glyph_rect.Right, glyph_rect.Bottom); // We need to do this, since the origin might not be (0, 0)
Glyph g = new Glyph(c, font, glyph_size);
Rectangle rect;
try
{
pack.Add(g, out rect);
}
catch (InvalidOperationException expt)
{
// TODO: The TexturePacker is full, create a new font sheet.
Trace.WriteLine(expt);
throw;
}
GL.BindTexture(TextureTarget.Texture2D, texture);
gfx.Clear(System.Drawing.Color.Transparent);
gfx.DrawString(g.Character.ToString(), g.Font, System.Drawing.Brushes.White, 0.0f, 0.0f, default_string_format);
BitmapData bitmap_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.PushClientAttrib(ClientAttribMask.ClientPixelStoreBit);
try
{
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1.0f);
GL.PixelStore(PixelStoreParameter.UnpackRowLength, bmp.Width);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, (int)rect.Left, (int)rect.Top,
rect.Width, rect.Height,
OpenTK.Graphics.PixelFormat.Rgba,
PixelType.UnsignedByte, bitmap_data.Scan0);
}
finally
{
GL.PopClientAttrib();
}
bmp.UnlockBits(bitmap_data);
rectangle = RectangleF.FromLTRB(
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, rectangle);
}
#endregion
#region GetCharExtents
// Gets the bounds of each character in a line of text.
// The line is processed in blocks of 32 characters (GdiPlus.MaxMeasurableCharacterRanges).
IEnumerable<RectangleF> GetCharExtents(string text, int height, int line_start, int line_length,
RectangleF layoutRect, IntPtr native_graphics, IntPtr native_font, IntPtr native_string_format)
{
RectangleF rect = new RectangleF();
int line_end = line_start + line_length;
while (line_start < line_end)
{
//if (text[line_start] == '\n' || text[line_start] == '\r')
//{
// line_start++;
// continue;
//}
int num_characters = (line_end - line_start) > GdiPlus.MaxMeasurableCharacterRanges ?
GdiPlus.MaxMeasurableCharacterRanges :
line_end - line_start;
int status = 0;
for (int i = 0; i < num_characters; i++)
{
characterRanges[i] = new CharacterRange(line_start + i, 1);
IntPtr region;
status = GdiPlus.CreateRegion(out region);
regions[i] = region;
Debug.Assert(status == 0, String.Format("GDI+ error: {0}", status));
}
status = GdiPlus.SetStringFormatMeasurableCharacterRanges(native_string_format, num_characters, characterRanges);
Debug.Assert(status == 0, String.Format("GDI+ error: {0}", status));
status = GdiPlus.MeasureCharacterRanges(native_graphics, text, text.Length,
native_font, ref layoutRect, native_string_format, num_characters, regions);
Debug.Assert(status == 0, String.Format("GDI+ error: {0}", status));
for (int i = 0; i < num_characters; i++)
{
GdiPlus.GetRegionBounds(regions[i], native_graphics, ref rect);
Debug.Assert(status == 0, String.Format("GDI+ error: {0}", status));
GdiPlus.DeleteRegion(regions[i]);
Debug.Assert(status == 0, String.Format("GDI+ error: {0}", status));
rect.Y += height;
yield return rect;
}
line_start += num_characters;
}
}
#endregion
#endregion
#region --- Internal Methods ---
#region internal int Texture
/// <summary>
/// Gets the handle to the texture were this font resides.
/// </summary>
internal int Texture
{
get { return TextureFont.texture; }
}
#endregion
#endregion
#region --- IDisposable Members ---
bool disposed;
/// <summary>
/// Releases all resources used by this OpenTK.Graphics.TextureFont.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}
private void Dispose(bool manual)
{
if (!disposed)
{
pack = null;
if (manual)
{
GL.DeleteTextures(1, ref texture);
font.Dispose();
gfx.Dispose();
}
disposed = true;
}
}
/// <summary>
/// Finalizes this TextureFont.
/// </summary>
~TextureFont()
{
Dispose(false);
}
#endregion
}
}

View file

@ -0,0 +1,104 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
namespace OpenTK.Graphics
{
/// <summary>
/// Provides text printing through OpenGL 1.5 vertex buffer objects.
/// </summary>
[Obsolete]
class VboTextPrinter : ITextPrinterImplementation
{
static int allocated_handles;
static int vector2_size = Marshal.SizeOf(new Vector2());
#region --- IPrinter Members ---
public TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count)
{
VboTextHandle handle = new VboTextHandle(++allocated_handles);
GL.GenBuffers(1, out handle.vbo_id);
GL.BindBuffer(BufferTarget.ArrayBuffer, handle.vbo_id);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * vector2_size), vertices,
BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.GenBuffers(1, out handle.ebo_id);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.ebo_id);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices,
BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
handle.element_count = indices.Length;
return handle;
}
public void Draw(TextHandle handle)
{
VboTextHandle vbo = (VboTextHandle)handle;
//GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit);
//GL.EnableClientState(EnableCap.TextureCoordArray);
GL.EnableClientState(EnableCap.VertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.vbo_id);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo.ebo_id);
GL.TexCoordPointer(2, TexCoordPointerType.Float, vector2_size, (IntPtr)vector2_size);
GL.VertexPointer(2, VertexPointerType.Float, vector2_size, IntPtr.Zero);
GL.DrawElements(BeginMode.Triangles, vbo.element_count, DrawElementsType.UnsignedShort, IntPtr.Zero);
//GL.DrawArrays(BeginMode.LineLoop, 0, vbo.element_count);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.DisableClientState(EnableCap.VertexArray);
//GL.DisableClientState(EnableCap.TextureCoordArray);
//GL.PopClientAttrib();
}
public void Draw(Vector2[] vertices, ushort[] indices, int index_count)
{
throw new NotImplementedException();
}
#endregion
}
#region class VboTextHandle : TextHandle
/// <summary>
/// Contains the necessary information to print text through the VboTextPrinter implementation.
/// </summary>
[Obsolete]
class VboTextHandle : TextHandle
{
public VboTextHandle(int handle) : base(handle) { }
internal int vbo_id; // vertex buffer object id.
internal int ebo_id; // index buffer object id.
internal int element_count; // Number of elements in the ebo.
public override string ToString()
{
return String.Format("TextHandle (vbo): {0} ({1} element(s), vbo id: {2}, ebo id: {3}",
Handle, element_count / 6, vbo_id, ebo_id);
}
}
#endregion
}

View file

@ -0,0 +1,22 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK
{
/// <summary>
/// Represents an item that can be packed with the TexturePacker.
/// </summary>
/// <typeparam name="T">The type of the packable item.</typeparam>
interface IPackable<T> : IEquatable<T>
{
int Width { get; }
int Height { get; }
}
}

View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK
{
interface IPoolable : IDisposable
{
void OnAcquire();
void OnRelease();
}
interface IPoolable<T> : IPoolable where T : IPoolable<T>, new()
{
ObjectPool<T> Owner { get; set; }
}
}

View file

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK
{
class ObjectPool<T> where T : IPoolable<T>, new()
{
Queue<T> pool = new Queue<T>();
public ObjectPool()
{ }
public T Acquire()
{
T item;
if (pool.Count > 0)
{
item = pool.Dequeue();
item.OnAcquire();
}
else
{
item = new T();
item.Owner = this;
item.OnAcquire();
}
return item;
}
public void Release(T item)
{
if (item == null)
throw new ArgumentNullException("item");
item.OnRelease();
pool.Enqueue(item);
}
}
}

View file

@ -0,0 +1,195 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace OpenTK
{
class TexturePacker<T> where T : IPackable<T>
{
Node root;
#region --- Constructors ---
public TexturePacker(int width, int height)
{
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Must be greater than zero.");
root = new Node();
root.Rect = new Rectangle(0, 0, width, width);
}
#endregion
#region --- Public Methods ---
#region public Rectangle Add(T item)
// Packs the given item into the free space of the TexturePacker. Returns the Rectangle of the packed item.
public void Add(T item, out Rectangle rect)
{
if (item.Width > root.Rect.Width || item.Height > root.Rect.Height)
throw new InvalidOperationException("The item is too large for this TexturePacker");
Node node;
//if (!items.ContainsKey(item))
{
node = root.Insert(item);
// Tree is full and insertion failed:
if (node == null)
throw new TexturePackerFullException();
//items.Add(item, node);
rect = node.Rect;
}
//throw new ArgumentException("The item already exists in the TexturePacker.", "item");
}
#endregion
#region public void Clear()
/// <summary>
/// Discards all packed items.
/// </summary>
public void Clear()
{
//items.Clear();
root.Clear();
}
#endregion
#region public void ChangeSize(int new_width, int new_height)
/// <summary>
/// Changes the dimensions of the TexturePacker surface.
/// </summary>
/// <param name="new_width">The new width of the TexturePacker surface.</param>
/// <param name="new_height">The new height of the TexturePacker surface.</param>
/// <remarks>Changing the size of the TexturePacker surface will implicitly call TexturePacker.Clear().</remarks>
/// <seealso cref="Clear"/>
public void ChangeSize(int new_width, int new_height)
{
throw new NotImplementedException();
}
#endregion
#endregion
#region Node
class Node
{
public Node()
{
}
Node left, right;
Rectangle rect;
int use_count;
public Rectangle Rect { get { return rect; } set { rect = value; } }
public Node Left { get { return left; } set { left = value; } }
public Node Right { get { return right; } set { right = value; } }
#region --- Constructor ---
public bool Leaf
{
get { return left == null && right == null; }
}
#endregion
#region public Node Insert(T item)
public Node Insert(T item)
{
if (!this.Leaf)
{
// Recurse towards left child, and if that fails, towards the right.
Node new_node = left.Insert(item);
return new_node ?? right.Insert(item);
}
else
{
// We have recursed to a leaf.
// If it is not empty go back.
if (use_count != 0)
return null;
// If this leaf is too small go back.
if (rect.Width < item.Width || rect.Height < item.Height)
return null;
// If this leaf is the right size, insert here.
if (rect.Width == item.Width && rect.Height == item.Height)
{
use_count = 1;
return this;
}
// This leaf is too large, split it up. We'll decide which way to split
// by checking the width and height difference between this rectangle and
// out item's bounding box. If the width difference is larger, we'll split
// horizontaly, else verticaly.
left = new Node();
right = new Node();
int dw = this.rect.Width - item.Width + 1;
int dh = this.rect.Height - item.Height + 1;
if (dw > dh)
{
left.rect = new Rectangle(rect.Left, rect.Top, item.Width, rect.Height);
right.rect = new Rectangle(rect.Left + item.Width, rect.Top, rect.Width - item.Width, rect.Height);
}
else
{
left.rect = new Rectangle(rect.Left, rect.Top, rect.Width, item.Height);
right.rect = new Rectangle(rect.Left, rect.Top + item.Height, rect.Width, rect.Height - item.Height);
}
return left.Insert(item);
}
}
#endregion
#region public void Clear()
public void Clear()
{
if (left != null)
left.Clear();
if (right != null)
right.Clear();
left = right = null;
}
#endregion
}
#endregion
}
class TexturePackerFullException : Exception
{
public TexturePackerFullException() : base("There is not enough space to add this item. Consider calling the Clear() method.") { }
}
}

View file

@ -31,7 +31,6 @@ using System.Text;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Diagnostics; using System.Diagnostics;
using OpenTK.Graphics.OpenGL;
namespace OpenTK.Graphics namespace OpenTK.Graphics
{ {
@ -137,7 +136,7 @@ namespace OpenTK.Graphics
GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel, GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel,
target.Left, target.Top, target.Left, target.Top,
target.Width, target.Height, target.Width, target.Height,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, OpenTK.Graphics.PixelFormat.Bgra,
PixelType.UnsignedByte, data.Scan0); PixelType.UnsignedByte, data.Scan0);
} }
finally finally
@ -159,7 +158,7 @@ namespace OpenTK.Graphics
TextureRegion2D<int> region = new TextureRegion2D<int>(rect); TextureRegion2D<int> region = new TextureRegion2D<int>(rect);
GL.GetTexImage(TextureTarget.Texture2D, mipLevel, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, region.Data); GL.GetTexImage(TextureTarget.Texture2D, mipLevel, OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, region.Data);
return region; return region;
} }
@ -223,7 +222,7 @@ namespace OpenTK.Graphics
SetDefaultTextureParameters(id); SetDefaultTextureParameters(id);
GL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat, Width, Height, 0, GL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat, Width, Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); OpenTK.Graphics.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
return id; return id;
} }