#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Nuclex.Fonts {
/// Vector-based font for creating three-dimensional text
///
///
/// Whereas bitmap based fonts copy pre-rendered image of the characters onto
/// the screen, vector based fonts store the vertices that make up a fonts 'hull'
/// and render the text with actual polygons at runtime.
///
///
/// For normal usage, after loading a VectorFont instance through the
/// Content Manager, use one of the three mesh generation methods:
///
/// -
/// Outline()
///
/// This method outlines the given string and returns a text mesh that
/// contains the side faces of the entire string in a single vertex list.
///
///
/// -
/// Fill()
///
/// Probably the most-used variant, this will build a flat mesh from the
/// string you provide. The mesh only contains front-facing polygons and
/// is ideal for normal text display, with the advantage that text won't
/// become distorted or blurry when it is zoomed / rotated.
///
///
/// -
/// Extrude()
///
/// This method builds a complete sealed 3D mesh from the string you
/// specify. The length of extrusion is always 1.0 units, centered
/// about the middle of that range, giving you the ability to scale the
/// extrusion level at will using the text's transformation matrix.
///
///
///
///
///
/// The vector font class also gives you full access to the underlying data of
/// a font, enabling you to use it for other purposes such as collision detection
/// or volume-based particle emitters that will make your credits or intro text
/// look more dynamic. This data is contained in the character instances you can
/// access through this class. To find the character index for a specific unicode
/// letter, use the CharacterMap which enlists any letter that the font can provide.
///
///
public class VectorFont {
#region struct KerningPair
/// Pair of characters for kerning informations
public struct KerningPair {
/// Initializes a new kerning pair
/// Left character of the kerning pair
/// Right character of the kerning pair
public KerningPair(char left, char right) {
this.Left = left;
this.Right = right;
}
/// The left character in the kerning pair
public char Left;
/// The right character in the kerning pair
public char Right;
/// Returns a hash code for the kerning pair
/// A hash code for the kerning pair
public override int GetHashCode() {
return ((int)this.Left) * 65536 + ((int)this.Right);
}
/// Compares this object to another object
/// Object to compare to
/// True if both objects are identical
public override bool Equals(object other) {
if(!(other is KerningPair))
return false;
KerningPair kerningPair = (KerningPair)other;
return
(kerningPair.Left == this.Left) &&
(kerningPair.Right == this.Right);
}
}
#endregion // struct KerningPair
/// Constructs a new vector font
///
/// Height of a single line of text in this font
///
/// List of Characters contained in the font
///
/// Map used to associate unicode characters with character indices
///
///
/// Kerning data for adjusting the space between specific characters
///
internal VectorFont(
float lineHeight,
List characters, Dictionary characterMap,
Dictionary kerningTable
) {
this.lineHeight = lineHeight;
this.characters = characters;
this.characterMap = characterMap;
this.kerningTable = kerningTable;
}
/// Constructs the outline of the specified string
/// String to construct an outline of
/// The outline of the specified string
public OutlinedText Outline(string text) {
return new OutlinedText(this, text);
}
/// Constructs a mesh of the strings face plane
/// Text to construct a flat polygon mesh of
/// The filled string mesh
public FilledText Fill(string text) {
return new FilledText(this, text);
}
/// Constructs an extruded polygon mesh of the string
/// String from which to construct a polygon mesh
/// The extruded string mesh
public ExtrudedText Extrude(string text) {
return new ExtrudedText(this, text);
}
/// Height of a single line of text in this font
public float LineHeight {
get { return this.lineHeight; }
}
/// List of the characters contained in this font
public List Characters {
get { return this.characters; }
}
/// Maps unicode character to indices into the character list
public Dictionary CharacterMap {
get { return this.characterMap; }
}
///
/// Kerning table for adjusting the positions of specific character combinations
///
///
/// Certain character combination, such as the two consecutive characters 'AV'
/// have diagonal shapes that would cause the characters to visually appear
/// is if they were further apart from each other. Kerning adjusts the distances
/// between such characters to keep the perceived character distance at the
/// same level for all character combinations.
///
public Dictionary KerningTable {
get { return this.kerningTable; }
}
/// Height of a single line of text in this font
private float lineHeight;
/// Characters contained in the font
private List characters;
/// Look-up map for indices by unicode character
private Dictionary characterMap;
///
/// Kerning table for adjusting the positions of specific character combinations
///
private Dictionary kerningTable;
}
} // namespace Nuclex.Fonts