internal Glyph(Font font, GlyphData glyphData)
 {
     Font = font;
     Data = glyphData;
 }
Beispiel #2
0
        public Font(string path)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(path);

            Name       = doc.SelectSingleNode("/font/info").Attributes.GetNamedItem("face").Value;
            MaxSize    = GetInt(doc.SelectSingleNode("/font/info"), "size");
            LineHeight = GetInt(doc.SelectSingleNode("/font/common"), "lineHeight");

            texturePages = new Texture2D[doc.SelectSingleNode("/font/pages").ChildNodes.Count];
            Log($"Found {texturePages.Length} pages");

            foreach (XmlNode node in doc.SelectNodes("/font/pages/page"))
            {
                string tn = node.Attributes.GetNamedItem("file").Value;
                int    id = GetInt(node, "id");
                string p  = $"Fonts/{Name}/{Path.GetFileNameWithoutExtension(tn)}";
                texturePages[id] = Engine.Instance.Content.Load <Texture2D>(p);
            }

            int charCount = GetInt(doc.SelectSingleNode("/font/chars"), "count");

            glyphs = new Dictionary <char, GlyphData>(charCount);



            foreach (XmlNode node in doc.SelectNodes("/font/chars/char"))
            {
                char      c        = (char)GetInt(node, "id");
                Rectangle rect     = new Rectangle(GetInt(node, "x"), GetInt(node, "y"), GetInt(node, "width"), GetInt(node, "height"));
                Texture2D page     = texturePages[GetInt(node, "page")];
                Point     offset   = new Point(GetInt(node, "xoffset"), GetInt(node, "yoffset"));
                int       xadvance = GetInt(node, "xadvance");

                glyphs.Add(c, new GlyphData(c, page, rect, offset, xadvance));
            }
            if (glyphs.ContainsKey('\r') && !glyphs.ContainsKey('\n'))
            {
                glyphs.Add('\n', glyphs['\r']);
            }

            Log($"Loaded {glyphs.Count} characters");

            // 0xFFFD is the Unicode point for the missing character symbol.
            MissingCharacterGlyph = glyphs.ContainsKey((char)0xFFFD) ? glyphs[(char)0xFFFD] : glyphs[' '];
            Log($"Assigned missing character glyph to {TextUtil.GetUnicodePoint(MissingCharacterGlyph.Character)}");

            if (doc.SelectSingleNode("/font/kernings") != null)
            {
                int kerningCount = GetInt(doc.SelectSingleNode("/font/kernings"), "count");
                kerningPairs = new Dictionary <string, int>(kerningCount);

                foreach (XmlNode node in doc.SelectNodes("/font/kernings/kerning"))
                {
                    string pair    = $"{(char)GetInt(node, "first")}{(char)GetInt(node, "second")}";
                    int    kerning = GetInt(node, "amount");
                    kerningPairs.Add(pair, kerning);
                }

                Log($"Loaded {kerningPairs.Count} kerning pairs");
            }
            else
            {
                kerningPairs = null;
                Log($"Font contained no kerning pairs.");
            }

            Log("Load complete.");
        }