FontManager.cs :  » Game » RealmForge » Axiom » Fonts » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » Fonts » FontManager.cs
#region LGPL License
/*
Axiom Game Engine Library
Copyright (C) 2003  Axiom Project Team

The overall design, and a majority of the core engine and rendering code 
contained within this library is a derivative of the open source Object Oriented 
Graphics Engine OGRE, which can be found at http://ogre.sourceforge.net.  
Many thanks to the OGRE team for maintaining such a high quality project.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#endregion

using System;
using System.IO;
using Axiom.Core;
using Axiom.FileSystem;
using Axiom.Scripting;

namespace Axiom.Fonts{
    /// <summary>
    ///    Manages Font resources, parsing .fontdef files and generally organizing them.
    /// </summary>
    public class FontManager : ResourceManager {
        #region Singleton implementation

        /// <summary>
        ///     Singleton instance of this class.
        /// </summary>
        private static FontManager instance;

        /// <summary>
        ///     Internal constructor.  This class cannot be instantiated externally.
        /// </summary>
        internal FontManager() {
            if (instance == null) {
                instance = this;
            }
        }

        /// <summary>
        ///     Gets the singleton instance of this class.
        /// </summary>
        public static FontManager Instance {
            get { 
                return instance; 
            }
        }

        #endregion Singleton implementation

        #region Methods

        /// <summary>
        ///    Parses all .fontdef scripts available in all resource locations.
        /// </summary>
        public void ParseAllSources() {
            string extension = ".fontdef";

            // search archives
            for(int i = 0; i < archives.Count; i++) {
                Archive archive = (Archive)archives[i];
                string[] files = archive.GetFileNamesLike("", extension);

                for(int j = 0; j < files.Length; j++) {
                    Stream data = archive.ReadFile(files[j]);

                    // parse the materials
                    ParseScript(data);
                }
            }

            // search common archives
            for(int i = 0; i < commonArchives.Count; i++) {
                Archive archive = (Archive)commonArchives[i];
                string[] files = archive.GetFileNamesLike("", extension);

                for(int j = 0; j < files.Length; j++) {
                    Stream data = archive.ReadFile(files[j]);

                    // parse the materials
                    ParseScript(data);
                }
            }
        }

        /// <summary>
        ///    Parse a .fontdef script passed in as a chunk.
        /// </summary>
        /// <param name="script"></param>
        public void ParseScript(Stream stream) {
            StreamReader script = new StreamReader(stream, System.Text.Encoding.ASCII);

            Font font = null;

            string line = "";

            // parse through the data to the end
            while((line = ParseHelper.ReadLine(script)) != null) {
                // ignore blank lines and comments
                if(line.Length == 0 || line.StartsWith("//")) {
                    continue;
                }
                else {
                    if(font == null) {
                        // first valid data should be the font name
                        font = (Font)Create(line);

                        ParseHelper.SkipToNextOpenBrace(script);
                    }
                    else {
                        // currently in a font
                        if(line == "}") {
                            // finished
                            font = null;
                        }
                        else {
                            ParseAttribute(line, font);
                        }
                    }
                }
            }
        }

        /// <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;
            }
        }

        #endregion Methods

        #region Implementation of ResourceManager

        public override void Load(Resource resource, int priority) {
            base.Load (resource, priority);
        }

        public override Resource Create(string name) {
            // 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);

                resourceList[name] = font;

                return font;
            }
        }

        public override void Dispose() {
            base.Dispose();

            instance = null;
        }

    #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.