/*****************************************************************
 * CodeName: VJProjector.Net
 * Copyright: Virtual Media Systems, S.L. 2003
 * Assembly: LibGFX
 * Type: C# Source Code
 * Version: 1.0
 * Description: 3D Font Renderer (truetype)
 * 
 * Revisions
 * ------------------------------------------------
 * [F] 08/03/2003, Jcl - Shaping the thing up
 * 
 *****************************************************************/
 
using System;
using Tao.OpenGl;
using Tao.Platform.Windows;
using VJProjector.GFX.Immerse.Types;
using VJProjector.GFX.Immerse.Math;
 
namespace VJProjector.GFX.Immerse.Render
{
	/// <summary>
	/// Descripción breve de Font3D.
	/// </summary>
	public class Font3D : IDisposable, IFont
	{
    private int startingChar;
    private bool initialized = false;
    private IntPtr font;
    private int listbase;
    private int numChars;
    private float zsize;
    private Gdi.GLYPHMETRICSFLOAT [] glyphMetrics;
    private string fontName;
 
    public Font3D(string fontName, string familyName, int startingChar, int numChars, int charwidth, int charheight, float chardepth, float deviation, bool solid, int escape, int orientation, bool bold, bool italic, bool underline, bool strikeout)
		{
      if(fontName.Trim() == "") fontName = familyName;
      this.fontName = fontName;
      this.startingChar = startingChar;
      this.numChars = numChars;
      this.zsize = chardepth;
 
      IntPtr hDC = Wgl.wglGetCurrentDC();
      listbase = Gl.glGenLists(numChars);
      font = Gdi.CreateFont(
        charheight,							// Height Of Font
        charwidth,								// Width Of Font
        escape,								// Angle Of Escapement
        orientation,								// Orientation Angle
        bold?Gdi.FW_BOLD:0,						// Font Weight
        italic,							// Italic
        underline,							// Underline
        strikeout,							// Strikeout
        Gdi.ANSI_CHARSET,					// Character Set Identifier
        Gdi.OUT_TT_PRECIS,					// Output Precision
        Gdi.CLIP_DEFAULT_PRECIS,			// Clipping Precision
        Gdi.ANTIALIASED_QUALITY,			// Output Quality
        Gdi.FF_DONTCARE|Gdi.DEFAULT_PITCH,		// Family And Pitch
        familyName);
      Gdi.SelectObject(hDC, font);	// Selects The Font We Created
      glyphMetrics = new Gdi.GLYPHMETRICSFLOAT[numChars];
      Wgl.wglUseFontOutlines(hDC, startingChar, numChars, listbase, deviation, chardepth, solid?Wgl.WGL_FONT_POLYGONS:Wgl.WGL_FONT_LINES, glyphMetrics);      
      Gdi.DeleteObject(font);
      initialized = true;
    }
 
    #region IDisposable Implementation
 
    public void Dispose()
    {
      if(initialized)
        Gl.glDeleteLists(listbase, numChars);
    }
 
    #endregion
 
    #region IFont Implementation
 
    public string FontName { get { return fontName; } set { fontName = value; } }
    public PointEX measureString(string text)
    {
      float lX=0, lY=0;
      for(int i = 0; i < text.Length; i++) 
      {
        lX += glyphMetrics[(int)text[i]-startingChar].gmfCellIncX;
        if(lY < glyphMetrics[(int)text[i]-startingChar].gmfBlackBoxY)
          lY = glyphMetrics[(int)text[i]-startingChar].gmfBlackBoxY;
      }
      return new PointEX(lX, lY, zsize);
    }
 
    public void RenderString(string text) 
    {
      RenderString(text, FontVerticalAlignment.Top, FontHorizontalAlignment.Left, new Vector(1,1,1));
    }
    public void RenderString(string text, FontVerticalAlignment valign, FontHorizontalAlignment halign) 
    {
      RenderString(text, valign, halign, new Vector(1,1,1));
    }
    public void RenderString(string text, Vector scale) 
    {
      RenderString(text, FontVerticalAlignment.Top, FontHorizontalAlignment.Left, scale);
    }
 
    public void RenderString(string text, FontVerticalAlignment valign, FontHorizontalAlignment halign, Vector scale) 
    {
      float tX = 0, tY = 0, tZ = 0;
      if(valign != FontVerticalAlignment.Top || halign != FontHorizontalAlignment.Left) 
      {
        PointEX p = measureString(text);
        switch(valign) 
        {
          case FontVerticalAlignment.Bottom:
            tY -= p.Y;
            break;
          case FontVerticalAlignment.Middle:
            tY -= p.Y/2;
            break;
        }
        switch(halign) 
        {
          case FontHorizontalAlignment.Center:
            tX -= p.X / 2;
            break;
          case FontHorizontalAlignment.Right:
            tX -= p.X;
            break;
        }
      }
      Gl.glPushAttrib(Gl.GL_TRANSFORM_BIT|Gl.GL_LIST_BIT); // Pushes The Display List Bits
      if(scale.X != 0 || scale.Y != 0 || scale.Z != 0)
        Gl.glScalef(scale.X, scale.Y, scale.Z);
      Gl.glTranslatef(tX, tY, tZ);
	    Gl.glListBase(listbase);									// Sets The Base Character to 0
	    Gl.glCallLists(text.Length, Gl.GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	    Gl.glPopAttrib();										// Pops The Display List Bits and transformation
    }
    #endregion
  }
}
 

Download Font3D.cs Font3D.cs - 5.0 KB