/// <summary> /// Returns an integer from an array of name/value pairs. /// </summary> /// <param name="parts">The array of parts.</param> /// <param name="name">The name of the value to return.</param> private static int GetNamedInt(string[] parts, string name) { return(Convert.ToInt32(BitmapFontLoader.GetNamedString(parts, name))); }
///// <summary> ///// Loads a bitmap font from a file, attempting to auto detect the file type ///// </summary> ///// <param name="fileName">Name of the file to load.</param> ///// <returns></returns> //public static BitmapFont LoadFontFromFile(string fileName) //{ // BitmapFont result; // if (string.IsNullOrEmpty(fileName)) // throw new ArgumentNullException("fileName", "File name not specified"); // else if (!File.Exists(fileName)) // throw new FileNotFoundException(string.Format("Cannot find file '{0}'", fileName), fileName); // using (FileStream file = File.OpenRead(fileName)) // { // using (TextReader reader = new StreamReader(file)) // { // string line; // line = reader.ReadLine(); // if (line.StartsWith("info ")) // result = BitmapFontLoader.LoadFontFromTextFile(fileName); // else if (line.StartsWith("<?xml")) // result = BitmapFontLoader.LoadFontFromXmlFile(fileName); // else // throw new InvalidDataException("Unknown file format."); // } // } // return result; //} /// <summary> /// Loads a bitmap font from a text file. /// </summary> /// <param name="fileName">Name of the file to load.</param> public static BitmapFont LoadFontFromTextFile(string fileName) { BitmapFont font; IDictionary <int, Page> pageData; IDictionary <Kerning, int> kerningDictionary; IDictionary <char, Character> charDictionary; string resourcePath; string[] lines; if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName), "File name not specified"); } else if (!File.Exists(fileName)) { throw new FileNotFoundException(string.Format("Cannot find file '{0}'", fileName), fileName); } pageData = new SortedDictionary <int, Page>(); kerningDictionary = new Dictionary <Kerning, int>(); charDictionary = new Dictionary <char, Character>(); font = new BitmapFont(); resourcePath = Path.GetDirectoryName(fileName); lines = File.ReadAllLines(fileName); foreach (string line in lines) { string[] parts; parts = BitmapFontLoader.Split(line, ' '); if (parts.Length != 0) { switch (parts[0]) { case "info": font.FamilyName = BitmapFontLoader.GetNamedString(parts, "face"); font.FontSize = BitmapFontLoader.GetNamedInt(parts, "size"); font.Bold = BitmapFontLoader.GetNamedBool(parts, "bold"); font.Italic = BitmapFontLoader.GetNamedBool(parts, "italic"); font.Charset = BitmapFontLoader.GetNamedString(parts, "charset"); font.Unicode = BitmapFontLoader.GetNamedBool(parts, "unicode"); font.StretchedHeight = BitmapFontLoader.GetNamedInt(parts, "stretchH"); font.Smoothed = BitmapFontLoader.GetNamedBool(parts, "smooth"); font.SuperSampling = BitmapFontLoader.GetNamedInt(parts, "aa"); font.Padding = BitmapFontLoader.ParsePadding(BitmapFontLoader.GetNamedString(parts, "padding")); font.Spacing = BitmapFontLoader.ParsePoint(BitmapFontLoader.GetNamedString(parts, "spacing")); font.OutlineSize = BitmapFontLoader.GetNamedInt(parts, "outline"); break; case "common": font.LineHeight = BitmapFontLoader.GetNamedInt(parts, "lineHeight"); font.BaseHeight = BitmapFontLoader.GetNamedInt(parts, "base"); font.TextureSize = new Size ( BitmapFontLoader.GetNamedInt(parts, "scaleW"), BitmapFontLoader.GetNamedInt(parts, "scaleH") ); font.Packed = BitmapFontLoader.GetNamedBool(parts, "packed"); font.AlphaChannel = BitmapFontLoader.GetNamedInt(parts, "alphaChnl"); font.RedChannel = BitmapFontLoader.GetNamedInt(parts, "redChnl"); font.GreenChannel = BitmapFontLoader.GetNamedInt(parts, "greenChnl"); font.BlueChannel = BitmapFontLoader.GetNamedInt(parts, "blueChnl"); break; case "page": int id; string name; string textureId; id = BitmapFontLoader.GetNamedInt(parts, "id"); name = BitmapFontLoader.GetNamedString(parts, "file"); textureId = Path.GetFileNameWithoutExtension(name); pageData.Add(id, new Page(id, Path.Combine(resourcePath, name))); break; case "char": Character charData; charData = new Character { Char = (char)BitmapFontLoader.GetNamedInt(parts, "id"), Bounds = new Rectangle ( BitmapFontLoader.GetNamedInt(parts, "x"), BitmapFontLoader.GetNamedInt(parts, "y"), BitmapFontLoader.GetNamedInt(parts, "width"), BitmapFontLoader.GetNamedInt(parts, "height") ), Offset = new Point ( BitmapFontLoader.GetNamedInt(parts, "xoffset"), BitmapFontLoader.GetNamedInt(parts, "yoffset") ), XAdvance = BitmapFontLoader.GetNamedInt(parts, "xadvance"), TexturePage = BitmapFontLoader.GetNamedInt(parts, "page"), Channel = BitmapFontLoader.GetNamedInt(parts, "chnl") }; charDictionary.Add(charData.Char, charData); break; case "kerning": Kerning key; key = new Kerning((char)BitmapFontLoader.GetNamedInt(parts, "first"), (char)BitmapFontLoader.GetNamedInt(parts, "second"), GetNamedInt(parts, "amount")); if (!kerningDictionary.ContainsKey(key)) { kerningDictionary.Add(key, key.Amount); } break; } } } font.Pages = BitmapFontLoader.ToArray(pageData.Values); font.Characters = charDictionary; font.Kernings = kerningDictionary; return(font); }
/// <summary> /// Loads font information from the specified <see cref="TextReader"/>. /// </summary> /// <remarks> /// The source data must be in BMFont text format. /// </remarks> /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception> /// <param name="reader">The <strong>TextReader</strong> used to feed the data into the font.</param> public virtual void LoadText(TextReader reader) { IDictionary <int, Page> pageData; IDictionary <Kerning, int> kerningDictionary; IDictionary <char, Character> charDictionary; string line; if (reader == null) { throw new ArgumentNullException("reader"); } pageData = new SortedDictionary <int, Page>(); kerningDictionary = new Dictionary <Kerning, int>(); charDictionary = new Dictionary <char, Character>(); do { line = reader.ReadLine(); if (line != null) { string[] parts; parts = BitmapFontLoader.Split(line, ' '); if (parts.Length != 0) { switch (parts[0]) { case "info": this.FamilyName = BitmapFontLoader.GetNamedString(parts, "face"); this.FontSize = BitmapFontLoader.GetNamedInt(parts, "size"); this.Bold = BitmapFontLoader.GetNamedBool(parts, "bold"); this.Italic = BitmapFontLoader.GetNamedBool(parts, "italic"); this.Charset = BitmapFontLoader.GetNamedString(parts, "charset"); this.Unicode = BitmapFontLoader.GetNamedBool(parts, "unicode"); this.StretchedHeight = BitmapFontLoader.GetNamedInt(parts, "stretchH"); this.Smoothed = BitmapFontLoader.GetNamedBool(parts, "smooth"); this.SuperSampling = BitmapFontLoader.GetNamedInt(parts, "aa"); this.Padding = BitmapFontLoader.ParsePadding(BitmapFontLoader.GetNamedString(parts, "padding")); this.Spacing = BitmapFontLoader.ParsePoint(BitmapFontLoader.GetNamedString(parts, "spacing")); this.OutlineSize = BitmapFontLoader.GetNamedInt(parts, "outline"); break; case "common": this.LineHeight = BitmapFontLoader.GetNamedInt(parts, "lineHeight"); this.BaseHeight = BitmapFontLoader.GetNamedInt(parts, "base"); this.TextureSize = new Size(BitmapFontLoader.GetNamedInt(parts, "scaleW"), BitmapFontLoader.GetNamedInt(parts, "scaleH")); this.Packed = BitmapFontLoader.GetNamedBool(parts, "packed"); this.AlphaChannel = BitmapFontLoader.GetNamedInt(parts, "alphaChnl"); this.RedChannel = BitmapFontLoader.GetNamedInt(parts, "redChnl"); this.GreenChannel = BitmapFontLoader.GetNamedInt(parts, "greenChnl"); this.BlueChannel = BitmapFontLoader.GetNamedInt(parts, "blueChnl"); break; case "page": int id; string name; id = BitmapFontLoader.GetNamedInt(parts, "id"); name = BitmapFontLoader.GetNamedString(parts, "file"); pageData.Add(id, new Page(id, name)); break; case "char": Character charData; charData = new Character { Char = (char)BitmapFontLoader.GetNamedInt(parts, "id"), Bounds = new Rectangle(BitmapFontLoader.GetNamedInt(parts, "x"), BitmapFontLoader.GetNamedInt(parts, "y"), BitmapFontLoader.GetNamedInt(parts, "width"), BitmapFontLoader.GetNamedInt(parts, "height")), Offset = new Point(BitmapFontLoader.GetNamedInt(parts, "xoffset"), BitmapFontLoader.GetNamedInt(parts, "yoffset")), XAdvance = BitmapFontLoader.GetNamedInt(parts, "xadvance"), TexturePage = BitmapFontLoader.GetNamedInt(parts, "page"), Channel = BitmapFontLoader.GetNamedInt(parts, "chnl") }; charDictionary.Add(charData.Char, charData); break; case "kerning": Kerning key; key = new Kerning((char)BitmapFontLoader.GetNamedInt(parts, "first"), (char)BitmapFontLoader.GetNamedInt(parts, "second"), BitmapFontLoader.GetNamedInt(parts, "amount")); if (!kerningDictionary.ContainsKey(key)) { kerningDictionary.Add(key, key.Amount); } break; } } } } while (line != null); this.Pages = BitmapFontLoader.ToArray(pageData.Values); this.Characters = charDictionary; this.Kernings = kerningDictionary; }