Fixed TextureFont.MeasureString calculations.

Improved TextureFont.MeasureString whitespace calculations.
Added uncached TextPrinter.Draw implementation.
This commit is contained in:
the_fiddler 2008-02-02 12:29:21 +00:00
parent a9e2ce4a00
commit d7558f889f
5 changed files with 68 additions and 38 deletions

View file

@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
using OpenTK.Math;
namespace OpenTK.Fonts
{
@ -19,21 +20,14 @@ namespace OpenTK.Fonts
{
#region IPrinter Members
public TextHandle Load(OpenTK.Math.Vector2[] vertices, ushort[] indices, int index_count)
public TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count)
{
DisplayListTextHandle handle = new DisplayListTextHandle(GL.GenLists(1));
GL.NewList(handle.Handle, ListMode.Compile);
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]]);
}
this.Draw(vertices, indices, index_count);
GL.End();
GL.EndList();
return handle;
@ -44,6 +38,20 @@ namespace OpenTK.Fonts
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

@ -19,6 +19,6 @@ namespace OpenTK.Fonts
{
TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count);
void Draw(TextHandle handle);
//void Draw(Vector2[] vertices, ushort[] indices, int index_count);
void Draw(Vector2[] vertices, ushort[] indices, int index_count);
}
}

View file

@ -149,7 +149,6 @@ namespace OpenTK.Fonts
#endregion
#region void PerformLayout(string text, TextureFont font, float width, bool wordWarp, StringAlignment alignment, bool rightToLeft, ref Vector2[] vertices, ref ushort[] indices, out int num_indices)
// Performs layout on the given string.
@ -227,7 +226,7 @@ namespace OpenTK.Fonts
indices[index_count++] = (ushort)(vertex_count - 8);
font.MeasureString(text.Substring(i, 1), out measured_width, out measured_height);
font.MeasureString(text.Substring(i, 1), out measured_width, out measured_height, false);
x_pos += measured_width;
}
else if (c == '\n')
@ -275,7 +274,9 @@ namespace OpenTK.Fonts
/// <param name="font">The OpenTK.Fonts.TextureFont to draw the text in.</param>
public void Draw(string text, TextureFont font)
{
//printer.Draw(text);
int num_indices;
PerformLayout(text, font, 0, false, StringAlignment.Near, false, ref vertices, ref indices, out num_indices);
printer.Draw(vertices, indices, num_indices);
}
#endregion
@ -296,7 +297,7 @@ namespace OpenTK.Fonts
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], 0.0, 1.0);
GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
@ -324,10 +325,10 @@ namespace OpenTK.Fonts
GL.PopAttrib();
GL.PopAttrib();
GL.MatrixMode(MatrixMode.Projection);
GL.MatrixMode(MatrixMode.Modelview);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
}

View file

@ -270,6 +270,42 @@ namespace OpenTK.Fonts
#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="addSpace">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>
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;
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
System.Drawing.SizeF size = gfx.MeasureString(str, font, 16384, format);
height = size.Height;
width = size.Width;
// width = 0;
// height = 0;
// int i = 0;
// foreach (char c in str)
// {
// if (c != '\n' && c != '\r')
// {
// SizeF size = gfx.MeasureString(str.Substring(i, 1), font, 16384, System.Drawing.StringFormat.GenericTypographic);
// width += size.Width == 0 ? font.SizeInPoints * 0.5f : size.Width;
// if (height < size.Height)
// height = size.Height;
// }
// ++i;
// }
}
#endregion
#region public void MeasureString(string str, out float width, out float height)
/// <summary>
@ -278,30 +314,10 @@ namespace OpenTK.Fonts
/// <param name="str">The string to measure.</param>
/// <param name="width">The measured width.</param>
/// <param name="height">The measured height.</param>
/// <seealso cref="public void MeasureString(string str, out float width, out float height, bool accountForOverhangs)"/>
public void MeasureString(string str, out float width, out float height)
{
//System.Drawing.SizeF size = gfx.MeasureString(str, font, 16384, System.Drawing.StringFormat.GenericTypographic);
//height = size.Height;
//if (size.Width == 0)
// width = font.SizeInPoints * 0.5f;
//else
// width = size.Width;
width = 0;
height = 0;
int i = 0;
foreach (char c in str)
{
if (c != '\n' && c != '\r')
{
SizeF size = gfx.MeasureString(str.Substring(i, 1), font, 16384, System.Drawing.StringFormat.GenericTypographic);
width += size.Width == 0 ? font.SizeInPoints * 0.5f : size.Width;
if (height < size.Height)
height = size.Height;
}
++i;
}
MeasureString(str, out width, out height, true);
}
#endregion

View file

@ -71,6 +71,11 @@ namespace OpenTK.Fonts
//GL.PopClientAttrib();
}
public void Draw(Vector2[] vertices, ushort[] indices, int index_count)
{
throw new NotImplementedException();
}
#endregion
}