Esempio n. 1
0
        public virtual 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, Symbol>();

            document.Load(reader);
            XmlNode 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         = Convert.ToInt32(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.ParsePoint(properties.Attributes["spacing"].Value);
            OutlineSize     = Convert.ToInt32(properties.Attributes["outline"].Value);

            // common attributes
            properties   = root.SelectSingleNode("common");
            BaseHeight   = Convert.ToInt32(properties?.Attributes["lineHeight"].Value);
            LineHeight   = Convert.ToInt32(properties?.Attributes["base"].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 = Convert.ToInt32(properties.Attributes["alphaChnl"].Value);
            RedChannel   = Convert.ToInt32(properties.Attributes["redChnl"].Value);
            GreenChannel = Convert.ToInt32(properties.Attributes["greenChnl"].Value);
            BlueChannel  = Convert.ToInt32(properties.Attributes["blueChnl"].Value);

            // load texture information
            foreach (XmlNode node in root.SelectNodes("pages/page"))
            {
                var page = new Page()
                {
                    Id       = Convert.ToInt32(node?.Attributes["id"].Value),
                    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"))
            {
                if (node?.Attributes == null)
                {
                    continue;
                }

                var character = new Symbol
                {
                    Character = (char)Convert.ToInt32(node?.Attributes["id"].Value),
                    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)),
                    Offset      = new Point(Convert.ToInt32(node.Attributes["xoffset"].Value), Convert.ToInt32(node.Attributes["yoffset"].Value)),
                    XAdvance    = Convert.ToInt32(node.Attributes["xadvance"].Value),
                    TexturePage = Convert.ToInt32(node.Attributes["page"].Value),
                    Channel     = Convert.ToInt32(node.Attributes["chnl"].Value)
                };

                charDictionary.Add(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;
        }
Esempio n. 2
0
        public virtual void LoadText(TextReader reader)
        {
            string line;

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

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

            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.ParsePoint(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 Symbol
                            {
                                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;
        }