// Imports the source font associated to the given command line options.
        public void Import(CommandLineOptions options)
        {
            // Create a bunch of GDI+ objects.
            using (Font font = CreateFont(options))
                using (Brush brush = new SolidBrush(Color.White))
                    using (StringFormat stringFormat = new StringFormat(StringFormatFlags.NoFontFallback))
                        using (Bitmap bitmap = new Bitmap(MaxGlyphSize, MaxGlyphSize, PixelFormat.Format32bppArgb))
                            using (Graphics graphics = Graphics.FromImage(bitmap)) {
                                graphics.PixelOffsetMode   = options.Sharp ? PixelOffsetMode.None : PixelOffsetMode.HighQuality;
                                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                                // Which characters do we want to include?
                                var characters = CharacterRegion.Flatten(options.CharacterRegions);

                                var glyphList = new List <Glyph>();

                                // Rasterize each character in turn.
                                int count = 0;

                                foreach (char character in characters)
                                {
                                    ++count;

                                    if (count == 500)
                                    {
                                        if (!options.FastPack)
                                        {
                                            Console.WriteLine("WARNING: capturing a large font. This may take a long time to complete and could result in too large a texture. Consider using /FastPack.");
                                        }
                                        Console.Write(".");
                                    }
                                    else if ((count % 500) == 0)
                                    {
                                        Console.Write(".");
                                    }

                                    Glyph glyph = ImportGlyph(character, font, brush, stringFormat, bitmap, graphics);

                                    glyphList.Add(glyph);
                                }

                                if (count > 500)
                                {
                                    Console.WriteLine();
                                }

                                Glyphs = glyphList;

                                // Store the font height.
                                LineSpacing = font.GetHeight();
                            }
        }
Пример #2
0
        // Imports the source font associated to the given command line options.
        public void Import(CommandLineOptions options)
        {
            if (options == null)
            {
                throw new NullReferenceException("The given command line options may not be equal to null.");
            }

            // Load the source bitmap.
            Bitmap bitmap;

            try {
                bitmap = new Bitmap(options.SourceFont);
            }
            catch {
                throw new Exception(string.Format("Unable to load '{0}'.", options.SourceFont));
            }

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

            // What characters are included in this font?
            var  characters       = CharacterRegion.Flatten(options.CharacterRegions).ToArray();
            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 region in BitmapUtils.GetGlyphRegions(bitmap, IsMarkerColor))
            {
                if (characterIndex < characters.Length)
                {
                    currentCharacter = characters[characterIndex];
                }
                ++currentCharacter;

                glyphList.Add(new Glyph(currentCharacter, bitmap, region));
                LineSpacing = Math.Max(LineSpacing, region.Height);
            }

            // If the bitmap doesn't already have an alpha channel, create one now.
            if (BitmapUtils.MatchesAlpha(255, bitmap))
            {
                BitmapUtils.ConvertGreyToAlpha(bitmap);
            }
        }