// MonoGame - Copyright (C) The MonoGame Team // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. using System; using System.Collections.Generic; using System.Linq; namespace Microsoft.Xna.Framework.Content.Pipeline.Graphics { internal class CharacterCollection : ICollection { private List _items; public CharacterCollection() { _items = new List(); } public CharacterCollection(IEnumerable characters) { _items = new List(); foreach (var c in characters) Add(c); } #region ICollection Members public void Add(char item) { if (!_items.Contains(item)) _items.Add(item); } public void Clear() { _items.Clear(); } public bool Contains(char item) { return _items.Contains(item); } public void CopyTo(char[] array, int arrayIndex) { _items.CopyTo(array, arrayIndex); } public int Count { get { return _items.Count; } } public bool IsReadOnly { get { return false; } } public bool Remove(char item) { return _items.Remove(item); } #endregion #region IEnumerable Members public IEnumerator GetEnumerator() { return _items.GetEnumerator(); } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _items.GetEnumerator(); } #endregion } /// /// Provides information to the FontDescriptionProcessor describing which font to rasterize, which font size to utilize, and which Unicode characters to include in the processor output. /// public class FontDescription : ContentItem { private char? defaultCharacter; private string fontName; private float size; private float spacing; private FontDescriptionStyle style; private bool useKerning; private CharacterCollection characters = new CharacterCollection(); /// /// Gets or sets the name of the font, such as "Times New Roman" or "Arial". This value cannot be null or empty. /// [ContentSerializer(AllowNull = false)] public string FontName { get { return fontName; } set { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("FontName is null or an empty string."); fontName = value; } } /// /// Gets or sets the size, in points, of the font. /// public float Size { get { return size; } set { if (value <= 0.0f) throw new ArgumentOutOfRangeException("Size must be greater than zero."); size = value; } } /// /// Gets or sets the amount of space, in pixels, to insert between letters in a string. /// [ContentSerializer(Optional = true)] public float Spacing { get { return spacing; } set { spacing = value; } } /// /// Indicates if kerning information is used when drawing characters. /// [ContentSerializer(Optional = true)] public bool UseKerning { get { return useKerning; } set { useKerning = value; } } /// /// Gets or sets the style of the font, expressed as a combination of one or more FontDescriptionStyle flags. /// public FontDescriptionStyle Style { get { return style; } set { style = value; } } /// /// Gets or sets the default character for the font. /// [ContentSerializer(Optional = true)] public Nullable DefaultCharacter { get { return defaultCharacter; } set { defaultCharacter = value; } } [ContentSerializer(CollectionItemName = "CharacterRegion")] internal CharacterRegion[] CharacterRegions { get { var regions = new List(); var chars = Characters.ToList(); chars.Sort(); var start = chars[0]; var end = chars[0]; for (var i=1; i < chars.Count; i++) { if (chars[i] != (end+1)) { regions.Add(new CharacterRegion(start, end)); start = chars[i]; } end = chars[i]; } regions.Add(new CharacterRegion(start, end)); return regions.ToArray(); } set { for (int index = 0; index < value.Length; ++index) { CharacterRegion characterRegion = value[index]; if (characterRegion.End < characterRegion.Start) throw new ArgumentException("CharacterRegion.End must be greater than CharacterRegion.Start"); for (var start = characterRegion.Start; start <= characterRegion.End; start++) Characters.Add(start); } } } [ContentSerializerIgnore] public ICollection Characters { get { return characters; } internal set { characters = new CharacterCollection(value); } } internal FontDescription() { } /// /// Initializes a new instance of FontDescription and initializes its members to the specified font, size, and spacing, using FontDescriptionStyle.Regular as the default value for Style. /// /// The name of the font, such as Times New Roman. /// The size, in points, of the font. /// The amount of space, in pixels, to insert between letters in a string. public FontDescription(string fontName, float size, float spacing) : this(fontName, size, spacing, FontDescriptionStyle.Regular, false) { } /// /// Initializes a new instance of FontDescription and initializes its members to the specified font, size, spacing, and style. /// /// The name of the font, such as Times New Roman. /// The size, in points, of the font. /// The amount of space, in pixels, to insert between letters in a string. /// The font style for the font. public FontDescription(string fontName, float size, float spacing, FontDescriptionStyle fontStyle) : this(fontName, size, spacing, fontStyle, false) { } /// /// Initializes a new instance of FontDescription using the specified values. /// /// The name of the font, such as Times New Roman. /// The size, in points, of the font. /// The amount of space, in pixels, to insert between letters in a string. /// The font style for the font. /// true if kerning information is used when drawing characters; false otherwise. public FontDescription(string fontName, float size, float spacing, FontDescriptionStyle fontStyle, bool useKerning) { // Write to the properties so the validation is run FontName = fontName; Size = size; Spacing = spacing; Style = fontStyle; UseKerning = useKerning; } } }