mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-02-02 21:21:02 +00:00
Fixed TextureFont.MeasureString calculations.
Improved TextureFont.MeasureString whitespace calculations. Added uncached TextPrinter.Draw implementation.
This commit is contained in:
parent
4fe6d37770
commit
57429c04e1
|
@ -9,6 +9,7 @@ using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
using OpenTK.Graphics.OpenGL;
|
using OpenTK.Graphics.OpenGL;
|
||||||
|
using OpenTK.Math;
|
||||||
|
|
||||||
namespace OpenTK.Fonts
|
namespace OpenTK.Fonts
|
||||||
{
|
{
|
||||||
|
@ -19,21 +20,14 @@ namespace OpenTK.Fonts
|
||||||
{
|
{
|
||||||
#region IPrinter Members
|
#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));
|
DisplayListTextHandle handle = new DisplayListTextHandle(GL.GenLists(1));
|
||||||
|
|
||||||
GL.NewList(handle.Handle, ListMode.Compile);
|
GL.NewList(handle.Handle, ListMode.Compile);
|
||||||
GL.Begin(BeginMode.Triangles);
|
|
||||||
|
|
||||||
for (int i = 0; i < index_count; i++)
|
this.Draw(vertices, indices, index_count);
|
||||||
//foreach (ushort index in indices)
|
|
||||||
{
|
|
||||||
GL.TexCoord2(vertices[indices[i] + 1]);
|
|
||||||
GL.Vertex2(vertices[indices[i]]);
|
|
||||||
}
|
|
||||||
|
|
||||||
GL.End();
|
|
||||||
GL.EndList();
|
GL.EndList();
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
|
@ -44,6 +38,20 @@ namespace OpenTK.Fonts
|
||||||
GL.CallList(handle.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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,6 @@ namespace OpenTK.Fonts
|
||||||
{
|
{
|
||||||
TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count);
|
TextHandle Load(Vector2[] vertices, ushort[] indices, int index_count);
|
||||||
void Draw(TextHandle handle);
|
void Draw(TextHandle handle);
|
||||||
//void Draw(Vector2[] vertices, ushort[] indices, int index_count);
|
void Draw(Vector2[] vertices, ushort[] indices, int index_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,6 @@ namespace OpenTK.Fonts
|
||||||
|
|
||||||
#endregion
|
#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)
|
#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.
|
// Performs layout on the given string.
|
||||||
|
@ -227,7 +226,7 @@ namespace OpenTK.Fonts
|
||||||
indices[index_count++] = (ushort)(vertex_count - 8);
|
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;
|
x_pos += measured_width;
|
||||||
}
|
}
|
||||||
else if (c == '\n')
|
else if (c == '\n')
|
||||||
|
@ -275,7 +274,9 @@ namespace OpenTK.Fonts
|
||||||
/// <param name="font">The OpenTK.Fonts.TextureFont to draw the text in.</param>
|
/// <param name="font">The OpenTK.Fonts.TextureFont to draw the text in.</param>
|
||||||
public void Draw(string text, TextureFont font)
|
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
|
#endregion
|
||||||
|
@ -296,7 +297,7 @@ namespace OpenTK.Fonts
|
||||||
GL.MatrixMode(MatrixMode.Projection);
|
GL.MatrixMode(MatrixMode.Projection);
|
||||||
GL.PushMatrix();
|
GL.PushMatrix();
|
||||||
GL.LoadIdentity();
|
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.MatrixMode(MatrixMode.Modelview);
|
||||||
GL.PushMatrix();
|
GL.PushMatrix();
|
||||||
|
@ -324,10 +325,10 @@ namespace OpenTK.Fonts
|
||||||
GL.PopAttrib();
|
GL.PopAttrib();
|
||||||
GL.PopAttrib();
|
GL.PopAttrib();
|
||||||
|
|
||||||
GL.MatrixMode(MatrixMode.Projection);
|
GL.MatrixMode(MatrixMode.Modelview);
|
||||||
GL.PopMatrix();
|
GL.PopMatrix();
|
||||||
|
|
||||||
GL.MatrixMode(MatrixMode.Modelview);
|
GL.MatrixMode(MatrixMode.Projection);
|
||||||
GL.PopMatrix();
|
GL.PopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -270,6 +270,42 @@ namespace OpenTK.Fonts
|
||||||
|
|
||||||
#endregion
|
#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)
|
#region public void MeasureString(string str, out float width, out float height)
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -278,30 +314,10 @@ namespace OpenTK.Fonts
|
||||||
/// <param name="str">The string to measure.</param>
|
/// <param name="str">The string to measure.</param>
|
||||||
/// <param name="width">The measured width.</param>
|
/// <param name="width">The measured width.</param>
|
||||||
/// <param name="height">The measured height.</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)
|
public void MeasureString(string str, out float width, out float height)
|
||||||
{
|
{
|
||||||
//System.Drawing.SizeF size = gfx.MeasureString(str, font, 16384, System.Drawing.StringFormat.GenericTypographic);
|
MeasureString(str, out width, out height, true);
|
||||||
//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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
@ -71,6 +71,11 @@ namespace OpenTK.Fonts
|
||||||
//GL.PopClientAttrib();
|
//GL.PopClientAttrib();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Draw(Vector2[] vertices, ushort[] indices, int index_count)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue