This class is simply a way of getting a font texture into the engine and to easily retrieve the texture coordinates required to accurately render them. Fonts can either be loaded from precreated textures, or the texture can be generated using a truetype font. You can either create the texture manually in code, or you can use an XML font script to define it (probably more practical since you can reuse the definition more easily)
This class extends both Resource and ManualResourceLoader since it is both a resource in it's own right, but it also provides the manual load implementation for the Texture it creates.
Inheritance: Axiom.Core.Resource, IManualResourceLoader
        /// <summary>
        ///    Parses an attribute of the font definitions.
        /// </summary>
        /// <param name="line"></param>
        /// <param name="font"></param>
        private void ParseAttribute(string line, Font font)
        {
            string[] parms = line.Split(new char[] {' ', '\t'});
            string attrib = parms[0].ToLower();

            switch(attrib) {
                case "type":
                    if(parms.Length != 2) {
                        ParseHelper.LogParserError(attrib, font.Name, "Invalid number of params for glyph ");
                        return;
                    }
                    else {
                        if(parms[0].ToLower() == "truetype") {
                            font.Type = FontType.TrueType;
                        }
                        else {
                            font.Type = FontType.Image;
                        }
                    }
                    break;

                case "source":
                    if(parms.Length != 2) {
                        ParseHelper.LogParserError("source", font.Name, "Invalid number of params.");
                        return;
                    }

                    // set the source of the font
                    font.Source = parms[1];

                    break;

                case "glyph":
                    if(parms.Length != 6) {
                        ParseHelper.LogParserError("glyph", font.Name, "Invalid number of params.");
                        return;
                    }

                    char glyph = parms[1][0];

                    // set the texcoords for this glyph
                    font.SetGlyphTexCoords(
                        glyph,
                        StringConverter.ParseFloat(parms[2]),
                        StringConverter.ParseFloat(parms[3]),
                        StringConverter.ParseFloat(parms[4]),
                        StringConverter.ParseFloat(parms[5]));

                    break;

                case "size":
                    if(parms.Length != 2) {
                        ParseHelper.LogParserError("size", font.Name, "Invalid number of params.");
                        return;
                    }

                    font.TrueTypeSize = int.Parse(parms[1]);

                    break;

                case "resolution":
                    if(parms.Length != 2) {
                        ParseHelper.LogParserError("resolution", font.Name, "Invalid number of params.");
                        return;
                    }

                    font.TrueTypeResolution = int.Parse(parms[1]);

                    break;

                case "antialias_colour":
                    if(parms.Length != 2) {
                        ParseHelper.LogParserError("antialias_colour", font.Name, "Invalid number of params.");
                        return;
                    }

                    font.AntialiasColor = bool.Parse(parms[1]);

                    break;
            }
        }
        public override Resource Create(string name, bool isManual)
        {
            // either return an existing font if already created, or create a new one
            if(GetByName(name) != null) {
                return GetByName(name);
            }
            else {
                // create a new font and add it to the list of resources
                Font font = new Font(name);

                Add(font);

                return font;
            }
        }
Esempio n. 3
0
        /// <summary>
        ///    Parses an attribute of the font definitions.
        /// </summary>
        /// <param name="line"></param>
        /// <param name="font"></param>
        protected void parseAttribute(string line, Font font)
        {
            var parms = line.Split(new char[]
            {
                ' ', '\t'
            });
            var attrib = parms[0].ToLower();

            switch (attrib)
            {
            case "type":
                if (parms.Length != 2)
                {
                    ParseHelper.LogParserError(attrib, font.Name, "Invalid number of params for glyph ");
                    return;
                }
                else
                {
                    if (parms[1].ToLower() == "truetype")
                    {
                        font.Type = FontType.TrueType;
                    }
                    else
                    {
                        font.Type = FontType.Image;
                    }
                }
                break;

            case "source":
                if (parms.Length != 2)
                {
                    ParseHelper.LogParserError("source", font.Name, "Invalid number of params.");
                    return;
                }

                // set the source of the font
                font.Source = parms[1];

                break;

            case "glyph":
                if (parms.Length != 6)
                {
                    ParseHelper.LogParserError("glyph", font.Name, "Invalid number of params.");
                    return;
                }

                var glyph = parms[1][0];

                // set the texcoords for this glyph
                font.SetGlyphTexCoords(glyph, StringConverter.ParseFloat(parms[2]), StringConverter.ParseFloat(parms[3]),
                                       StringConverter.ParseFloat(parms[4]), StringConverter.ParseFloat(parms[5]));

                break;

            case "size":
                if (parms.Length != 2)
                {
                    ParseHelper.LogParserError("size", font.Name, "Invalid number of params.");
                    return;
                }

                font.TrueTypeSize = int.Parse(parms[1]);

                break;

            case "resolution":
                if (parms.Length != 2)
                {
                    ParseHelper.LogParserError("resolution", font.Name, "Invalid number of params.");
                    return;
                }

                font.TrueTypeResolution = int.Parse(parms[1]);

                break;

            case "antialias_colour":
                if (parms.Length != 2)
                {
                    ParseHelper.LogParserError("antialias_colour", font.Name, "Invalid number of params.");
                    return;
                }

                font.AntialiasColor = bool.Parse(parms[1]);

                break;
            }
        }