예제 #1
0
        public void Import(SpriteFontAsset options, List <char> characters)
        {
            // Load the source bitmap.
            Bitmap bitmap;

            try
            {
                // TODO Check if source can be used as is from here
                bitmap = new Bitmap(options.FontSource.GetFontPath());
            }
            catch
            {
                throw new FontNotFoundException(options.FontSource.GetFontPath());
            }

            // Convert to our desired pixel format.
            bitmap = BitmapUtils.ChangePixelFormat(bitmap, PixelFormat.Format32bppArgb);

            // What characters are included in this font?
            int  characterIndex   = 0;
            char currentCharacter = '\0';

            // Split the source image into a list of individual glyphs.
            var glyphList = new List <Glyph>();

            Glyphs      = glyphList;
            LineSpacing = 0;

            foreach (Rectangle rectangle in FindGlyphs(bitmap))
            {
                if (characterIndex < characters.Count)
                {
                    currentCharacter = characters[characterIndex++];
                }
                else
                {
                    currentCharacter++;
                }

                glyphList.Add(new Glyph(currentCharacter, bitmap, rectangle)
                {
                    XAdvance = rectangle.Width
                });

                LineSpacing = Math.Max(LineSpacing, rectangle.Height);
            }

            // If the bitmap doesn't already have an alpha channel, create one now.
            if (BitmapUtils.IsAlphaEntirely(255, bitmap))
            {
                BitmapUtils.ConvertGreyToAlpha(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
            }
        }
        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());
        }