Exemple #1
0
        static Glyph[] ImportFont(SpriteFontAsset options, out float lineSpacing, out float baseLine)
        {
            // Which importer knows how to read this source font?
            IFontImporter importer;

            var sourceExtension      = (Path.GetExtension(options.FontSource.GetFontPath()) ?? "").ToLowerInvariant();
            var bitmapFileExtensions = new List <string> {
                ".bmp", ".png", ".gif"
            };
            var importFromBitmap = bitmapFileExtensions.Contains(sourceExtension);

            importer = importFromBitmap ? (IFontImporter) new BitmapImporter() : new TrueTypeImporter();

            // create the list of character to import
            var characters = GetCharactersToImport(options);

            // Import the source font data.
            importer.Import(options, characters);

            lineSpacing = importer.LineSpacing;
            baseLine    = importer.BaseLine;

            // Get all glyphs
            var glyphs = new List <Glyph>(importer.Glyphs);

            // Validate.
            if (glyphs.Count == 0)
            {
                throw new Exception("Font does not contain any glyphs.");
            }
            if (!importFromBitmap && options.FontType.AntiAlias != FontAntiAliasMode.ClearType)
            {
                foreach (var glyph in importer.Glyphs)
                {
                    BitmapUtils.ConvertGreyToAlpha(glyph.Bitmap, glyph.Subrect);
                }
            }

            // Sort the glyphs
            glyphs.Sort((left, right) => left.Character.CompareTo(right.Character));


            // Check that the default character is part of the glyphs
            if (options.DefaultCharacter != 0)
            {
                bool defaultCharacterFound = false;
                foreach (var glyph in glyphs)
                {
                    if (glyph.Character == options.DefaultCharacter)
                    {
                        defaultCharacterFound = true;
                        break;
                    }
                }
                if (!defaultCharacterFound)
                {
                    throw new InvalidOperationException("The specified DefaultCharacter is not part of this font.");
                }
            }

            return(glyphs.ToArray());
        }