示例#1
0
文件: Fonts.cs 项目: reelgood/dcsg
 public void AdvanceHead(FontLetter fl, float scale = 1f)
 {
     _writerPos += fl.xadvance;
 }
示例#2
0
文件: Fonts.cs 项目: reelgood/dcsg
        public static void LoadFont(string fontName, string sfDirectory = "Spritefonts")
        {
            if (sfDirectory.Substring(sfDirectory.Length - 1, 1) != "\\") { sfDirectory += "\\"; }
            if (!System.IO.File.Exists(DCSG.Contents.RootDirectory + "\\" + sfDirectory + fontName + ".fnt"))
                throw new System.IO.FileNotFoundException("No font file associated with " + fontName);
            if (FontDocument == null)
                FontDocument = new XmlDocument();
            FontDocument.Load(DCSG.Contents.RootDirectory + "\\" + sfDirectory + fontName + ".fnt");
            XmlNode font = FontDocument.FirstChild;
            if (font.Name == "xml") { font = FontDocument.ChildNodes[1]; }
            if (font.Name != "font")
                throw new XmlException("Wrong first node in font file");

            Font nf = new Font();
            foreach (XmlElement item in font.ChildNodes)
            {
                switch (item.Name)
                {
                    case "info":
                        nf.Facename = item.GetAttribute("face");
                        nf.Size = Convert.ToInt32(item.GetAttribute("size"));
                        break;
                    case "common":

                        break;
                    case "chars":
                        int count = Convert.ToInt32(item.GetAttribute("count"));
                        for (int i = 0; i < item.ChildNodes.Count; i++)
                        {
                            int w = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("width"));
                            int h = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("height"));
                            int x = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("xadvance"));
                            int yp = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("y"));
                            int xp = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("x"));
                            int yoff = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("yoffset"));
                            int xoff = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("xoffset"));
                            int id = Convert.ToInt32(((XmlElement)item.ChildNodes[i]).GetAttribute("id"));
                            string let = ((XmlElement)item.ChildNodes[i]).GetAttribute("letter");
                            FontLetter fl = new FontLetter() { id = id, name = let, xadvance = x, xOff = xoff, yOff = yoff, rect = new Microsoft.Xna.Framework.Rectangle(xp, yp, w, h) };
                            nf.Letters.Add(fl);
                        }
                        break;
                    case "kernings":

                        break;
                }
            }
            nf.texture = DCSG.Contents.Load<Texture2D>(sfDirectory + fontName);
            nf.Name = new System.IO.FileInfo(DCSG.Contents.RootDirectory + "\\" + fontName + ".fnt").Name.Replace(".fnt", "");
            LoadedFonts.Add(nf);
            Console.WriteLine(nf.Name);
        }