Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new <see cref="Font" /> instance using the specified builder configuration.
        /// </summary>
        /// <param name="name">
        /// Name to use for identifying this font, must be unique.
        /// Can be set to null to allow the constructor to auto-generate a name for the font.
        /// </param>
        /// <param name="file">Path to the font file (TTF or qfont).</param>
        /// <param name="size">Size (in points) to use for this font.</param>
        /// <param name="type">
        /// The type of font. This will be detected by the file extension,
        /// but can be manually specified to control the fallback type used if
        /// one was not detected from the file name
        /// </param>
        /// <param name="fontConstructionConfig">The <see cref="FontConstructionConfig" /> containing relevant font build/load configurations.</param>
        public Font(string name, string file, int size, FontType type, FontConstructionConfig fontConstructionConfig)
        {
            _file = file;
            _size = size;

            var ext = Path.GetExtension(_file);

            if (string.IsNullOrEmpty(ext))
            {
                throw new EngineException("Failed to get file extension of font file!",
                                          new IOException("System.IO.Path.GetExtension returned null or empty for specified file."));
            }

            // Disable resharper warning, we are checking for null, resharper doesn't like IsNullOrEmpty
            // ReSharper disable PossibleNullReferenceException
            ext = ext.TrimStart('.').ToLower();
            // ReSharper restore PossibleNullReferenceException

            switch (ext)
            {
            case "ttf":
                type = FontType.TTF;
                break;

            case "qfont":
                type = FontType.QFont;
                break;
            }

            _type = type;

            QFont font;

            switch (_type)
            {
            case FontType.TTF:
                if (fontConstructionConfig.BuildConfig == null)
                {
                    throw new EngineException("Builder configuration was null but requested font type requires a builder config!",
                                              new ArgumentException("BuildConfig was null.", "fontConstructionConfig"));
                }
                font = new QFont(_file, _size, fontConstructionConfig.BuildConfig);
                break;

            case FontType.QFont:
                if (fontConstructionConfig.LoadConfig == null)
                {
                    throw new EngineException("Loader configuration was null but requested font type requires a loader config!",
                                              new ArgumentException("LoadConfig was null.", "fontConstructionConfig"));
                }
                font = QFont.FromQFontFile(_file, fontConstructionConfig.LoadConfig);
                break;

            default:
                throw new EngineException("Unsupported font type: " + _type,
                                          new ArgumentException("Font type unsupported.", "type"));
            }

            if (font == null)
            {
                throw new EngineException("Font failed to initialize!",
                                          new ArgumentException("Font failed to initialize.", "file"));
            }

            _qfont = font;

            _name = name ?? GetDefaultName(file, size);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new <see cref="Font" /> instance using the specified builder configuration.
        /// </summary>
        /// <param name="name">
        /// Name to use for identifying this font, must be unique.
        /// Can be set to null to allow the constructor to auto-generate a name for the font.
        /// </param>
        /// <param name="file">Path to the font file (TTF or qfont).</param>
        /// <param name="size">Size (in points) to use for this font.</param>
        /// <param name="type">
        /// The type of font. This will be detected by the file extension,
        /// but can be manually specified to control the fallback type used if
        /// one was not detected from the file name
        /// </param>
        /// <param name="fontConstructionConfig">The <see cref="FontConstructionConfig" /> containing relevant font build/load configurations.</param>
        public Font(string name, string file, int size, FontType type, FontConstructionConfig fontConstructionConfig)
        {
            _file = file;
            _size = size;

            var ext = Path.GetExtension(_file);

            if (string.IsNullOrEmpty(ext))
                throw new EngineException("Failed to get file extension of font file!",
                                          new IOException("System.IO.Path.GetExtension returned null or empty for specified file."));

            // Disable resharper warning, we are checking for null, resharper doesn't like IsNullOrEmpty
            // ReSharper disable PossibleNullReferenceException
            ext = ext.TrimStart('.').ToLower();
            // ReSharper restore PossibleNullReferenceException

            switch (ext)
            {
                case "ttf":
                    type = FontType.TTF;
                    break;
                case "qfont":
                    type = FontType.QFont;
                    break;
            }

            _type = type;

            QFont font;

            switch (_type)
            {
                case FontType.TTF:
                    if (fontConstructionConfig.BuildConfig == null)
                        throw new EngineException("Builder configuration was null but requested font type requires a builder config!",
                                                  new ArgumentException("BuildConfig was null.", "fontConstructionConfig"));
                    font = new QFont(_file, _size, fontConstructionConfig.BuildConfig);
                    break;
                case FontType.QFont:
                    if (fontConstructionConfig.LoadConfig == null)
                        throw new EngineException("Loader configuration was null but requested font type requires a loader config!",
                                                  new ArgumentException("LoadConfig was null.", "fontConstructionConfig"));
                    font = QFont.FromQFontFile(_file, fontConstructionConfig.LoadConfig);
                    break;
                default:
                    throw new EngineException("Unsupported font type: " + _type,
                                              new ArgumentException("Font type unsupported.", "type"));
            }

            if (font == null)
                throw new EngineException("Font failed to initialize!",
                                          new ArgumentException("Font failed to initialize.", "file"));

            _qfont = font;

            _name = name ?? GetDefaultName(file, size);
        }