Beispiel #1
0
        static void ParseGlyph(string[] parts, out Glyph glyph)
        {
            glyph = new Glyph();

            CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            ci.NumberFormat.CurrencyDecimalSeparator = ".";

            Vector2 pos = Vector2.Zero, size = Vector2.Zero, offset = Vector2.Zero;

            for (int i = 1; i < parts.Length; i++)
            {
                if (parts[i] == string.Empty)
                    continue;

                var kvp = parts[i].Split('=');

                kvp[0] = kvp[0].ToLower();

                if (kvp[0] == "id")
                {
                    glyph.Character = (char)Int32.Parse(kvp[1]);
                }
                else if (kvp[0] == "x")
                {
                    pos.X = Int32.Parse(kvp[1]);
                }
                else if (kvp[0] == "y")
                {
                    pos.Y = Int32.Parse(kvp[1]);
                }
                else if (kvp[0] == "width")
                {
                    size.X = Int32.Parse(kvp[1]);
                }
                else if (kvp[0] == "height")
                {
                    size.Y = Int32.Parse(kvp[1]);
                }
                else if (kvp[0] == "xoffset")
                {
                    offset.X = float.Parse(kvp[1], NumberStyles.Any, ci);
                }
                else if (kvp[0] == "yoffset")
                {
                    offset.Y = -float.Parse(kvp[1], NumberStyles.Any, ci);
                }
                else if (kvp[0] == "xadvance")
                {
                    glyph.XAdvance = float.Parse(kvp[1], NumberStyles.Any, ci);
                }
            }

            glyph.Position = pos;
            glyph.Size = size;
            glyph.Offset = offset;
        }
Beispiel #2
0
        void ReadGlyph(XmlReader reader)
        {
            Glyph glyph = new Glyph();

            bool idfound = false;

            while (reader.MoveToNextAttribute())
            {
                idfound = reader.Name == "ID";

                if (idfound)
                    break;
            }

            if (!idfound)
                throw new Exception("Unable to deserialize font. Unknown format. Error: Glyph tag has no ID attribute.");

            glyph.Character = (char)Int32.Parse(reader.Value);

            reader.MoveToElement();

            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            culture.NumberFormat.CurrencyDecimalSeparator = ".";

            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    if(reader.Name == "Position")
                    {
                        var x = float.Parse(reader.GetAttribute("x"), NumberStyles.Any, culture);
                        var y = float.Parse(reader.GetAttribute("y"), NumberStyles.Any, culture);
                        glyph.Position = new Vector2(x, y);
                    }

                    if(reader.Name == "Size")
                    {
                        var x = float.Parse(reader.GetAttribute("x"), NumberStyles.Any, culture);
                        var y = float.Parse(reader.GetAttribute("y"), NumberStyles.Any, culture);
                        glyph.Size = new Vector2(x, y);
                    }

                    if(reader.Name == "Offset")
                    {
                        var x = float.Parse(reader.GetAttribute("x"), NumberStyles.Any, culture);
                        var y = float.Parse(reader.GetAttribute("y"), NumberStyles.Any, culture);
                        glyph.Offset = new Vector2(x, y);
                    }

                    if(reader.Name == "Scale")
                    {
                        var x = float.Parse(reader.GetAttribute("x"), NumberStyles.Any, culture);
                        var y = float.Parse(reader.GetAttribute("y"), NumberStyles.Any, culture);
                        glyph.Scale = new Vector2(x, y);
                    }

                    if(reader.Name == "XAdvance")
                    {
                        glyph.XAdvance = float.Parse(reader.GetAttribute("x"), NumberStyles.Any, culture);
                    }
                }
                else
                {
                    if (reader.Name == "Glyph")
                        break;
                }
            }

            Glyphs.Add(glyph.Character, glyph);
        }