コード例 #1
0
        /// <summary>
        /// Load font information from the specified file.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <exception cref="FileNotFoundException">Thrown when the requested file is not present.</exception>
        /// <param name="fileName">The file name to load.</param>
        public void Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("Cannot find file '{0}'.", fileName), fileName);
            }

            using (Stream stream = File.OpenRead(fileName))
            {
                this.Load(stream);
            }

            BitmapFontLoader.QualifyResourcePaths(this, Path.GetDirectoryName(fileName));
        }
コード例 #2
0
        /// <summary>
        /// Loads font information from the specified <see cref="TextReader"/>.
        /// </summary>
        /// <remarks>
        /// The source data must be in BMFont XML 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 void LoadXml(TextReader reader)
        {
            XmlDocument                   document;
            IDictionary <int, Page>       pageData;
            IDictionary <Kerning, int>    kerningDictionary;
            IDictionary <char, Character> charDictionary;
            XmlNode root;
            XmlNode properties;

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            document          = new XmlDocument();
            pageData          = new SortedDictionary <int, Page>();
            kerningDictionary = new Dictionary <Kerning, int>();
            charDictionary    = new Dictionary <char, Character>();

            document.Load(reader);
            root = document.DocumentElement;

            // load the basic attributes
            properties           = root.SelectSingleNode("info");
            this.FamilyName      = properties.Attributes["face"].Value;
            this.FontSize        = Convert.ToInt32(properties.Attributes["size"].Value);
            this.Bold            = Convert.ToInt32(properties.Attributes["bold"].Value) != 0;
            this.Italic          = Convert.ToInt32(properties.Attributes["italic"].Value) != 0;
            this.Unicode         = Convert.ToInt32(properties.Attributes["unicode"].Value) != 0;
            this.StretchedHeight = Convert.ToInt32(properties.Attributes["stretchH"].Value);
            this.Charset         = properties.Attributes["charset"].Value;
            this.Smoothed        = Convert.ToInt32(properties.Attributes["smooth"].Value) != 0;
            this.SuperSampling   = Convert.ToInt32(properties.Attributes["aa"].Value);
            this.Padding         = BitmapFontLoader.ParsePadding(properties.Attributes["padding"].Value);
            this.Spacing         = BitmapFontLoader.ParsePoint(properties.Attributes["spacing"].Value);
            this.OutlineSize     = Convert.ToInt32(properties.Attributes["outline"].Value);

            // common attributes
            properties       = root.SelectSingleNode("common");
            this.BaseHeight  = Convert.ToInt32(properties.Attributes["lineHeight"].Value);
            this.LineHeight  = Convert.ToInt32(properties.Attributes["base"].Value);
            this.TextureSize = new Point(Convert.ToInt32(properties.Attributes["scaleW"].Value),
                                         Convert.ToInt32(properties.Attributes["scaleH"].Value));
            this.Packed       = Convert.ToInt32(properties.Attributes["packed"].Value) != 0;
            this.AlphaChannel = Convert.ToInt32(properties.Attributes["alphaChnl"].Value);
            this.RedChannel   = Convert.ToInt32(properties.Attributes["redChnl"].Value);
            this.GreenChannel = Convert.ToInt32(properties.Attributes["greenChnl"].Value);
            this.BlueChannel  = Convert.ToInt32(properties.Attributes["blueChnl"].Value);

            // load texture information
            foreach (XmlNode node in root.SelectNodes("pages/page"))
            {
                Page page;

                page          = new Page();
                page.Id       = Convert.ToInt32(node.Attributes["id"].Value);
                page.FileName = node.Attributes["file"].Value;

                pageData.Add(page.Id, page);
            }
            this.Pages = BitmapFontLoader.ToArray(pageData.Values);

            // load character information
            foreach (XmlNode node in root.SelectNodes("chars/char"))
            {
                Character character;

                character        = new Character();
                character.Char   = (char)Convert.ToInt32(node.Attributes["id"].Value);
                character.Bounds = new Rectangle(Convert.ToInt32(node.Attributes["x"].Value),
                                                 Convert.ToInt32(node.Attributes["y"].Value),
                                                 Convert.ToInt32(node.Attributes["width"].Value),
                                                 Convert.ToInt32(node.Attributes["height"].Value));
                character.Offset = new Point(Convert.ToInt32(node.Attributes["xoffset"].Value),
                                             Convert.ToInt32(node.Attributes["yoffset"].Value));
                character.XAdvance    = Convert.ToInt32(node.Attributes["xadvance"].Value);
                character.TexturePage = Convert.ToInt32(node.Attributes["page"].Value);
                character.Channel     = Convert.ToInt32(node.Attributes["chnl"].Value);

                charDictionary.Add(character.Char, character);
            }
            this.Characters = charDictionary;

            // loading kerning information
            foreach (XmlNode node in root.SelectNodes("kernings/kerning"))
            {
                Kerning key;

                key = new Kerning((char)Convert.ToInt32(node.Attributes["first"].Value),
                                  (char)Convert.ToInt32(node.Attributes["second"].Value),
                                  Convert.ToInt32(node.Attributes["amount"].Value));

                if (!kerningDictionary.ContainsKey(key))
                {
                    kerningDictionary.Add(key, key.Amount);
                }
            }
            this.Kernings = kerningDictionary;
        }
コード例 #3
0
        /// <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 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 Point(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;
        }