コード例 #1
0
        /// <summary>
        /// Loads font information from the specified <see cref="TextReader"/>.
        /// </summary>
        public void LoadXml(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

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

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

            // load the basic attributes
            var properties = root.SelectSingleNode("info");

            familyName      = properties.Attributes["face"].Value;
            fontSize        = Convert.ToInt32(properties.Attributes["size"].Value);
            bold            = Convert.ToInt32(properties.Attributes["bold"].Value) != 0;
            italic          = Convert.ToInt32(properties.Attributes["italic"].Value) != 0;
            unicode         = properties.Attributes["unicode"].Value != "0";
            stretchedHeight = Convert.ToInt32(properties.Attributes["stretchH"].Value);
            charset         = properties.Attributes["charset"].Value;
            smoothed        = Convert.ToInt32(properties.Attributes["smooth"].Value) != 0;
            superSampling   = Convert.ToInt32(properties.Attributes["aa"].Value);
            padding         = BitmapFontLoader.ParsePadding(properties.Attributes["padding"].Value);
            spacing         = BitmapFontLoader.ParseInt2(properties.Attributes["spacing"].Value);
            outlineSize     = properties.Attributes["outline"] != null?Convert.ToInt32(properties.Attributes["outline"].Value) : 0;

            // common attributes
            properties  = root.SelectSingleNode("common");
            baseHeight  = Convert.ToInt32(properties.Attributes["base"].Value);
            lineHeight  = Convert.ToInt32(properties.Attributes["lineHeight"].Value);
            textureSize = new Point(Convert.ToInt32(properties.Attributes["scaleW"].Value), Convert.ToInt32(properties.Attributes["scaleH"].Value));
            packed      = Convert.ToInt32(properties.Attributes["packed"].Value) != 0;

            alphaChannel = properties.Attributes["alphaChnl"] != null?Convert.ToInt32(properties.Attributes["alphaChnl"].Value) : 0;

            redChannel = properties.Attributes["redChnl"] != null?Convert.ToInt32(properties.Attributes["redChnl"].Value) : 0;

            greenChannel = properties.Attributes["greenChnl"] != null?Convert.ToInt32(properties.Attributes["greenChnl"].Value) : 0;

            blueChannel = properties.Attributes["blueChnl"] != null?Convert.ToInt32(properties.Attributes["blueChnl"].Value) : 0;

            // load texture information
            foreach (XmlNode node in root.SelectNodes("pages/page"))
            {
                var page = new Page();
                page.id       = Convert.ToInt32(node.Attributes["id"].Value);
                page.filename = node.Attributes["file"].Value;

                pageData.Add(page.id, page);
            }
            pages = BitmapFontLoader.ToArray(pageData.Values);

            // load character information
            foreach (XmlNode node in root.SelectNodes("chars/char"))
            {
                var character = new Character();
                character.character = (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[character.character] = character;
            }
            characters = charDictionary;

            // loading kerning information
            foreach (XmlNode node in root.SelectNodes("kernings/kerning"))
            {
                var 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);
                }
            }
            kernings = kerningDictionary;
        }
コード例 #2
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)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

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

            string line;

            do
            {
                line = reader.ReadLine();
                if (line != null)
                {
                    var parts = BitmapFontLoader.Split(line, ' ');
                    if (parts.Length != 0)
                    {
                        switch (parts[0])
                        {
                        case "info":
                            familyName      = BitmapFontLoader.GetNamedString(parts, "face");
                            fontSize        = BitmapFontLoader.GetNamedInt(parts, "size");
                            bold            = BitmapFontLoader.GetNamedBool(parts, "bold");
                            italic          = BitmapFontLoader.GetNamedBool(parts, "italic");
                            charset         = BitmapFontLoader.GetNamedString(parts, "charset");
                            unicode         = BitmapFontLoader.GetNamedBool(parts, "unicode");
                            stretchedHeight = BitmapFontLoader.GetNamedInt(parts, "stretchH");
                            smoothed        = BitmapFontLoader.GetNamedBool(parts, "smooth");
                            superSampling   = BitmapFontLoader.GetNamedInt(parts, "aa");
                            padding         = BitmapFontLoader.ParsePadding(BitmapFontLoader.GetNamedString(parts, "padding"));
                            spacing         = BitmapFontLoader.ParseInt2(BitmapFontLoader.GetNamedString(parts, "spacing"));
                            outlineSize     = BitmapFontLoader.GetNamedInt(parts, "outline");
                            break;

                        case "common":
                            lineHeight  = BitmapFontLoader.GetNamedInt(parts, "lineHeight");
                            baseHeight  = BitmapFontLoader.GetNamedInt(parts, "base");
                            textureSize = new Point(BitmapFontLoader.GetNamedInt(parts, "scaleW"),
                                                    BitmapFontLoader.GetNamedInt(parts, "scaleH"));
                            packed       = BitmapFontLoader.GetNamedBool(parts, "packed");
                            alphaChannel = BitmapFontLoader.GetNamedInt(parts, "alphaChnl");
                            redChannel   = BitmapFontLoader.GetNamedInt(parts, "redChnl");
                            greenChannel = BitmapFontLoader.GetNamedInt(parts, "greenChnl");
                            blueChannel  = BitmapFontLoader.GetNamedInt(parts, "blueChnl");
                            break;

                        case "page":
                            var id   = BitmapFontLoader.GetNamedInt(parts, "id");
                            var name = BitmapFontLoader.GetNamedString(parts, "file");

                            pageData.Add(id, new Page(id, name));
                            break;

                        case "char":
                            var charData = new Character
                            {
                                character = (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.character, charData);
                            break;

                        case "kerning":
                            var 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);

            pages      = BitmapFontLoader.ToArray(pageData.Values);
            characters = charDictionary;
            kernings   = kerningDictionary;
        }