RenderStack 11.06.1
Classes | Public Member Functions | Properties
RenderStack.UI.FontStyle Class Reference

List of all members.

Classes

class  FontChar
class  FontCommon
class  FontInfo
class  FontKerning

Public Member Functions

 FontStyle (string filename)
void Print (Mesh.Mesh mesh, float x, float y, float z, string text)
void Print (Mesh.Mesh mesh, float x, float y, float z, string text, out Rectangle bounds)

Properties

Texture Texture [get]
float LineHeight [get]

Detailed Description

Definition at line 37 of file FontStyle.cs.


Constructor & Destructor Documentation

RenderStack.UI.FontStyle.FontStyle ( string  filename)

Definition at line 184 of file FontStyle.cs.

References RenderStack.UI.FontStyle.FontKerning.Amount, and RenderStack.UI.FontStyle.FontKerning.Second.

        {
            XmlDocument spec = new XmlDocument();
            spec.Load(filename);

            //info = new FontInfo(spec.SelectSingleNode("font/info"));
            common = new FontCommon(spec.SelectSingleNode("font/common"));

            XmlNode pagesNode   = spec.SelectSingleNode("font/pages");
            XmlNode pageNode    = pagesNode.ChildNodes.Item(0);
            /*int     pageId      = */int.Parse(pageNode.Attributes["id"].Value);
            string  file        = pageNode.Attributes["file"].Value;
            var     image       = new RenderStack.Graphics.Image("res/images/" + file);

            texture = new Texture(image, false);

            XmlNode charsNode = spec.SelectSingleNode("font/chars");
            foreach(XmlNode charNode in charsNode.ChildNodes)
            {
                int charId = int.Parse(charNode.Attributes["id"].Value);
                chars[charId] = new FontChar(charNode, common);
            }
            XmlNode kerningsNode = spec.SelectSingleNode("font/kernings");
            if(kerningsNode != null)
            {
                foreach(XmlNode kerningNode in kerningsNode.ChildNodes)
                {
                    short first   = short.Parse(kerningNode.Attributes["first"].Value);
                    short second  = short.Parse(kerningNode.Attributes["second"].Value);
                    if(first >= 0 && second >= 0 && first < 256 && second < 256)
                    {
                        short amount  = short.Parse(kerningNode.Attributes["amount"].Value);
                        bool modifiedExisting = false;
                        foreach(FontKerning existingKerning in chars[first].Kernings)
                        {
                            /*  This part is overly paranoid - if unicode wasn't used,  */ 
                            /*  kerning pairs are broken...  */ 
                            if(existingKerning.Second == second)
                            {
                                if(
                                    (existingKerning.Amount == 0) ||
                                    (System.Math.Sign(existingKerning.Amount) != System.Math.Sign(amount))
                                )
                                {
                                    existingKerning.Amount = 0;
                                    break;
                                }
                                else if(amount < 0 && amount < existingKerning.Amount)
                                {
                                    existingKerning.Amount = amount;
                                    modifiedExisting = true;
                                    break;
                                }
                                else if(amount > 0 && amount > existingKerning.Amount)
                                {
                                    existingKerning.Amount = amount;
                                    modifiedExisting = true;
                                    break;
                                }
                            }
                        }
                        if(modifiedExisting == false)
                        {
                            chars[first].Kernings.Add(new FontKerning(second, amount));
                        }
                    }
                }
            }
            else
            {
                System.Diagnostics.Trace.TraceWarning("No kerning info"); 
            }
            foreach(FontChar @char in chars)
            {
                if(@char != null)
                {
                    if(@char.Kernings != null)
                    {
                        @char.Kernings.Sort();
                    }
                }
            }

        }

Member Function Documentation

void RenderStack.UI.FontStyle.Print ( Mesh.Mesh  mesh,
float  x,
float  y,
float  z,
string  text 
)

Definition at line 269 of file FontStyle.cs.

        {
            Rectangle bounds;
            Print(
                mesh,
                x, 
                y, 
                z, 
                text,
                out bounds
            );
        }
void RenderStack.UI.FontStyle.Print ( Mesh.Mesh  mesh,
float  x,
float  y,
float  z,
string  text,
out Rectangle  bounds 
)

Definition at line 288 of file FontStyle.cs.

References RenderStack.Graphics.Buffer.BeginEdit(), RenderStack.Graphics.Buffer.CurrentIndex, RenderStack.Graphics.Buffer.EndEdit(), RenderStack.Graphics.VertexFormat.FindAttribute(), RenderStack.UI.FontStyle.FontChar.Height, RenderStack.UI.FontStyle.FontChar.Kernings, RenderStack.Graphics.Buffer.Quad(), RenderStack.Graphics.Buffer.Set(), RenderStack.UI.FontStyle.FontChar.U, RenderStack.UI.FontStyle.FontChar.U2, RenderStack.UI.FontStyle.FontChar.V, RenderStack.UI.FontStyle.FontChar.V2, RenderStack.Graphics.Buffer.VertexFormat, RenderStack.UI.FontStyle.FontChar.Width, RenderStack.UI.FontStyle.FontChar.XAdvance, RenderStack.UI.FontStyle.FontChar.XOffset, and RenderStack.UI.FontStyle.FontChar.YOffset.

        {
            bounds = new Rectangle();
            bounds.ResetForGrow();
            Buffer vertexBuffer = mesh.VertexBuffer;
            Buffer indexBuffer  = mesh.IndexBuffer(MeshMode.PolygonFill);

            vertexBuffer.BeginEdit();
            indexBuffer.BeginEdit();

            y += common.Base;

            var position    = vertexBuffer.VertexFormat.FindAttribute(VertexUsage.Position, 0);
            var texCoord    = vertexBuffer.VertexFormat.FindAttribute(VertexUsage.TexCoord, 0);
            var color       = vertexBuffer.VertexFormat.FindAttribute(VertexUsage.Color,    0);

            if(string.IsNullOrEmpty(text) == false)
            {
                FontChar fontChar = null;
                for(int i = 0; i < text.Length; ++i)
                {
                    char c      = text[i];

                    fontChar = chars[c];
                    if(fontChar == null)
                    {
                        continue;
                    }

                    float a     = fontChar.XAdvance;
                    float w     = fontChar.Width;
                    float h     = fontChar.Height;
                    float ox    = fontChar.XOffset;
                    float oy    = fontChar.YOffset;

                    indexBuffer.Quad(
                        vertexBuffer.CurrentIndex, 
                        vertexBuffer.CurrentIndex + 1, 
                        vertexBuffer.CurrentIndex + 2, 
                        vertexBuffer.CurrentIndex + 3
                    );
                    indexBuffer.CurrentIndex += 6;
                    vertexBuffer.Set(position,  x +     ox, y -     oy, z);
                    vertexBuffer.Set(texCoord,  fontChar.U, fontChar.V);
                    vertexBuffer.Set(color,     1.0f, 1.0f, 1.0f);
                    ++vertexBuffer.CurrentIndex; 
                    bounds.Extend(x +     ox, y -     oy);

                    vertexBuffer.Set(position, x + w + ox, y -     oy, z);
                    vertexBuffer.Set(texCoord, fontChar.U2, fontChar.V);
                    vertexBuffer.Set(color,     1.0f, 1.0f, 1.0f);
                    ++vertexBuffer.CurrentIndex; 
                    bounds.Extend(x + w + ox, y -     oy);

                    vertexBuffer.Set(position, x + w + ox, y - h - oy, z);
                    vertexBuffer.Set(texCoord, fontChar.U2, fontChar.V2);
                    vertexBuffer.Set(color,     1.0f, 1.0f, 1.0f);
                    ++vertexBuffer.CurrentIndex; 
                    bounds.Extend(x + w + ox, y - h - oy);

                    vertexBuffer.Set(position, x +     ox, y - h - oy, z);
                    vertexBuffer.Set(texCoord, fontChar.U, fontChar.V2);
                    vertexBuffer.Set(color,     1.0f, 1.0f, 1.0f);
                    ++vertexBuffer.CurrentIndex; 
                    bounds.Extend(x +     ox, y - h - oy);

                    x += a;

                    if(
                        (i + 1 < text.Length - 1) &&
                        (fontChar.Kernings != null)
                    )
                    {
                        char next = text[i + 1];

                        FontKerning compare = new FontKerning((short)next);

                        int index = fontChar.Kernings.BinarySearch(compare);

                        if(index >= 0)
                        {
                            short amount = fontChar.Kernings[index].Amount;
                            x += (float)(amount);
                        }
                    }
                }
            }

            vertexBuffer.EndEdit();
            indexBuffer.EndEdit();

        }

Property Documentation

Texture RenderStack.UI.FontStyle.Texture [get]
float RenderStack.UI.FontStyle.LineHeight [get]

Definition at line 182 of file FontStyle.cs.

Referenced by example.UI.TextRenderer.DrawDebugLines().


The documentation for this class was generated from the following file: